cg386ld.pas 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for load/assignment nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386ld;
  19. interface
  20. uses
  21. tree;
  22. procedure secondload(var p : ptree);
  23. procedure secondassignment(var p : ptree);
  24. procedure secondfuncret(var p : ptree);
  25. procedure secondarrayconstruct(var p : ptree);
  26. implementation
  27. uses
  28. globtype,systems,
  29. cobjects,verbose,globals,files,
  30. symconst,symtable,aasm,types,
  31. hcodegen,temp_gen,pass_2,
  32. cpubase,cpuasm,
  33. cgai386,tgeni386,cg386cnv,cresstr;
  34. {*****************************************************************************
  35. SecondLoad
  36. *****************************************************************************}
  37. procedure secondload(var p : ptree);
  38. var
  39. hregister : tregister;
  40. symtabletype : tsymtabletype;
  41. i : longint;
  42. hp : preference;
  43. s : pasmsymbol;
  44. popeax : boolean;
  45. pushed : tpushed;
  46. hr : treference;
  47. begin
  48. simple_loadn:=true;
  49. reset_reference(p^.location.reference);
  50. case p^.symtableentry^.typ of
  51. { this is only for toasm and toaddr }
  52. absolutesym :
  53. begin
  54. p^.location.reference.symbol:=nil;
  55. if (pabsolutesym(p^.symtableentry)^.abstyp=toaddr) then
  56. begin
  57. if pabsolutesym(p^.symtableentry)^.absseg then
  58. p^.location.reference.segment:=R_FS;
  59. p^.location.reference.offset:=pabsolutesym(p^.symtableentry)^.address;
  60. end
  61. else
  62. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  63. end;
  64. constsym:
  65. begin
  66. if pconstsym(p^.symtableentry)^.consttype=constresourcestring then
  67. begin
  68. pushusedregisters(pushed,$ff);
  69. emit_const(A_PUSH,S_L,
  70. pconstsym(p^.symtableentry)^.resstrindex);
  71. emit_sym(A_PUSH,S_L,newasmsymbol(pconstsym(p^.symtableentry)^.owner^.name^+'_RESOURCESTRINGLIST'));
  72. emitcall('FPC_GETRESOURCESTRING');
  73. hregister:=getexplicitregister32(R_EAX);
  74. emit_reg_reg(A_MOV,S_L,R_EAX,hregister);
  75. if gettempansistringreference(hr) then
  76. decrstringref(p^.resulttype,hr);
  77. emit_reg_ref(A_MOV,S_L,hregister,
  78. newreference(hr));
  79. ungetregister32(hregister);
  80. popusedregisters(pushed);
  81. p^.location.loc:=LOC_MEM;
  82. p^.location.reference:=hr;
  83. end
  84. else
  85. internalerror(22798);
  86. end;
  87. varsym :
  88. begin
  89. hregister:=R_NO;
  90. { C variable }
  91. if (vo_is_C_var in pvarsym(p^.symtableentry)^.varoptions) then
  92. begin
  93. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  94. end
  95. { DLL variable }
  96. else if (vo_is_dll_var in pvarsym(p^.symtableentry)^.varoptions) then
  97. begin
  98. hregister:=getregister32;
  99. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  100. emit_ref_reg(A_MOV,S_L,newreference(p^.location.reference),hregister);
  101. p^.location.reference.symbol:=nil;
  102. p^.location.reference.base:=hregister;
  103. end
  104. { external variable }
  105. else if (vo_is_external in pvarsym(p^.symtableentry)^.varoptions) then
  106. begin
  107. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  108. end
  109. { thread variable }
  110. else if (vo_is_thread_var in pvarsym(p^.symtableentry)^.varoptions) then
  111. begin
  112. popeax:=not(R_EAX in unused);
  113. if popeax then
  114. emit_reg(A_PUSH,S_L,R_EAX);
  115. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  116. emit_ref(A_PUSH,S_L,newreference(p^.location.reference));
  117. { the called procedure isn't allowed to change }
  118. { any register except EAX }
  119. emitcall('FPC_RELOCATE_THREADVAR');
  120. reset_reference(p^.location.reference);
  121. p^.location.reference.base:=getregister32;
  122. emit_reg_reg(A_MOV,S_L,R_EAX,p^.location.reference.base);
  123. if popeax then
  124. emit_reg(A_POP,S_L,R_EAX);
  125. end
  126. { normal variable }
  127. else
  128. begin
  129. symtabletype:=p^.symtable^.symtabletype;
  130. { in case it is a register variable: }
  131. if pvarsym(p^.symtableentry)^.reg<>R_NO then
  132. begin
  133. if pvarsym(p^.symtableentry)^.reg in [R_ST0..R_ST7] then
  134. begin
  135. p^.location.loc:=LOC_CFPUREGISTER;
  136. p^.location.register:=pvarsym(p^.symtableentry)^.reg;
  137. end
  138. else
  139. begin
  140. p^.location.loc:=LOC_CREGISTER;
  141. p^.location.register:=pvarsym(p^.symtableentry)^.reg;
  142. unused:=unused-[pvarsym(p^.symtableentry)^.reg];
  143. end;
  144. end
  145. else
  146. begin
  147. { first handle local and temporary variables }
  148. if (symtabletype in [parasymtable,inlinelocalsymtable,
  149. inlineparasymtable,localsymtable]) then
  150. begin
  151. p^.location.reference.base:=procinfo.framepointer;
  152. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address+p^.symtable^.address_fixup;
  153. if (symtabletype in [localsymtable,inlinelocalsymtable]) then
  154. begin
  155. if use_esp_stackframe then
  156. dec(p^.location.reference.offset,
  157. pvarsym(p^.symtableentry)^.getsize)
  158. else
  159. p^.location.reference.offset:=-p^.location.reference.offset;
  160. end;
  161. if (lexlevel>(p^.symtable^.symtablelevel)) then
  162. begin
  163. hregister:=getregister32;
  164. { make a reference }
  165. hp:=new_reference(procinfo.framepointer,
  166. procinfo.framepointer_offset);
  167. emit_ref_reg(A_MOV,S_L,hp,hregister);
  168. simple_loadn:=false;
  169. i:=lexlevel-1;
  170. while i>(p^.symtable^.symtablelevel) do
  171. begin
  172. { make a reference }
  173. hp:=new_reference(hregister,8);
  174. emit_ref_reg(A_MOV,S_L,hp,hregister);
  175. dec(i);
  176. end;
  177. p^.location.reference.base:=hregister;
  178. end;
  179. end
  180. else
  181. case symtabletype of
  182. unitsymtable,globalsymtable,
  183. staticsymtable :
  184. begin
  185. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  186. end;
  187. stt_exceptsymtable:
  188. begin
  189. p^.location.reference.base:=procinfo.framepointer;
  190. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address;
  191. end;
  192. objectsymtable:
  193. begin
  194. if (sp_static in pvarsym(p^.symtableentry)^.symoptions) then
  195. begin
  196. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  197. end
  198. else
  199. begin
  200. p^.location.reference.base:=R_ESI;
  201. p^.location.reference.offset:=pvarsym(p^.symtableentry)^.address;
  202. end;
  203. end;
  204. withsymtable:
  205. begin
  206. { make a reference }
  207. { symtable datasize field
  208. contains the offset of the temp
  209. stored }
  210. { hp:=new_reference(procinfo.framepointer,
  211. p^.symtable^.datasize);
  212. emit_ref_reg(A_MOV,S_L,hp,hregister);}
  213. if ptree(pwithsymtable(p^.symtable)^.withnode)^.islocal then
  214. begin
  215. p^.location.reference:=ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^;
  216. end
  217. else
  218. begin
  219. hregister:=getregister32;
  220. p^.location.reference.base:=hregister;
  221. emit_ref_reg(A_MOV,S_L,
  222. newreference(ptree(pwithsymtable(p^.symtable)^.withnode)^.withreference^),
  223. hregister);
  224. end;
  225. inc(p^.location.reference.offset,pvarsym(p^.symtableentry)^.address);
  226. end;
  227. end;
  228. end;
  229. { in case call by reference, then calculate. Open array
  230. is always an reference! }
  231. if (pvarsym(p^.symtableentry)^.varspez=vs_var) or
  232. is_open_array(pvarsym(p^.symtableentry)^.definition) or
  233. is_array_of_const(pvarsym(p^.symtableentry)^.definition) or
  234. ((pvarsym(p^.symtableentry)^.varspez=vs_const) and
  235. push_addr_param(pvarsym(p^.symtableentry)^.definition)) then
  236. begin
  237. simple_loadn:=false;
  238. if hregister=R_NO then
  239. hregister:=getregister32;
  240. if p^.location.loc=LOC_CREGISTER then
  241. begin
  242. emit_reg_reg(A_MOV,S_L,
  243. p^.location.register,hregister);
  244. p^.location.loc:=LOC_REFERENCE;
  245. end
  246. else
  247. begin
  248. emit_ref_reg(A_MOV,S_L,
  249. newreference(p^.location.reference),
  250. hregister);
  251. end;
  252. reset_reference(p^.location.reference);
  253. p^.location.reference.base:=hregister;
  254. end;
  255. end;
  256. end;
  257. procsym:
  258. begin
  259. if assigned(p^.left) then
  260. begin
  261. secondpass(p^.left);
  262. p^.location.loc:=LOC_MEM;
  263. gettempofsizereference(8,p^.location.reference);
  264. { load class instance address }
  265. case p^.left^.location.loc of
  266. LOC_CREGISTER,
  267. LOC_REGISTER:
  268. begin
  269. hregister:=p^.left^.location.register;
  270. ungetregister32(p^.left^.location.register);
  271. { such code is allowed !
  272. CGMessage(cg_e_illegal_expression); }
  273. end;
  274. LOC_MEM,
  275. LOC_REFERENCE:
  276. begin
  277. hregister:=R_EDI;
  278. emit_ref_reg(A_MOV,S_L,
  279. newreference(p^.left^.location.reference),R_EDI);
  280. del_reference(p^.left^.location.reference);
  281. ungetiftemp(p^.left^.location.reference);
  282. end;
  283. else internalerror(26019);
  284. end;
  285. { store the class instance address }
  286. new(hp);
  287. hp^:=p^.location.reference;
  288. inc(hp^.offset,4);
  289. emit_reg_ref(A_MOV,S_L,
  290. hregister,hp);
  291. { virtual method ? }
  292. if (po_virtualmethod in pprocsym(p^.symtableentry)^.definition^.procoptions) then
  293. begin
  294. new(hp);
  295. reset_reference(hp^);
  296. hp^.base:=hregister;
  297. { load vmt pointer }
  298. emit_ref_reg(A_MOV,S_L,
  299. hp,R_EDI);
  300. {$IfDef regallocfix}
  301. del_reference(hp^);
  302. {$EndIf regallocfix}
  303. { load method address }
  304. new(hp);
  305. reset_reference(hp^);
  306. hp^.base:=R_EDI;
  307. hp^.offset:=pprocsym(p^.symtableentry)^.definition^._class^.vmtmethodoffset(
  308. pprocsym(p^.symtableentry)^.definition^.extnumber);
  309. emit_ref_reg(A_MOV,S_L,
  310. hp,R_EDI);
  311. { ... and store it }
  312. emit_reg_ref(A_MOV,S_L,
  313. R_EDI,newreference(p^.location.reference));
  314. end
  315. else
  316. begin
  317. s:=newasmsymbol(pprocsym(p^.symtableentry)^.definition^.mangledname);
  318. emit_sym_ofs_ref(A_MOV,S_L,s,0,
  319. newreference(p^.location.reference));
  320. end;
  321. end
  322. else
  323. begin
  324. {!!!!! Be aware, work on virtual methods too }
  325. p^.location.reference.symbol:=newasmsymbol(pprocsym(p^.symtableentry)^.definition^.mangledname);
  326. end;
  327. end;
  328. typedconstsym :
  329. begin
  330. p^.location.reference.symbol:=newasmsymbol(p^.symtableentry^.mangledname);
  331. end;
  332. else internalerror(4);
  333. end;
  334. end;
  335. {*****************************************************************************
  336. SecondAssignment
  337. *****************************************************************************}
  338. procedure secondassignment(var p : ptree);
  339. var
  340. opsize : topsize;
  341. otlabel,hlabel,oflabel : pasmlabel;
  342. hregister : tregister;
  343. fputyp : tfloattype;
  344. loc : tloc;
  345. r : preference;
  346. ai : paicpu;
  347. op : tasmop;
  348. begin
  349. otlabel:=truelabel;
  350. oflabel:=falselabel;
  351. getlabel(truelabel);
  352. getlabel(falselabel);
  353. { calculate left sides }
  354. if not(p^.concat_string) then
  355. secondpass(p^.left);
  356. if codegenerror then
  357. exit;
  358. case p^.left^.location.loc of
  359. LOC_REFERENCE : begin
  360. { in case left operator uses to register }
  361. { but to few are free then LEA }
  362. if (p^.left^.location.reference.base<>R_NO) and
  363. (p^.left^.location.reference.index<>R_NO) and
  364. (usablereg32<p^.right^.registers32) then
  365. begin
  366. del_reference(p^.left^.location.reference);
  367. hregister:=getregister32;
  368. emit_ref_reg(A_LEA,S_L,newreference(
  369. p^.left^.location.reference),
  370. hregister);
  371. reset_reference(p^.left^.location.reference);
  372. p^.left^.location.reference.base:=hregister;
  373. p^.left^.location.reference.index:=R_NO;
  374. end;
  375. loc:=LOC_REFERENCE;
  376. end;
  377. LOC_CFPUREGISTER:
  378. loc:=LOC_CFPUREGISTER;
  379. LOC_CREGISTER:
  380. loc:=LOC_CREGISTER;
  381. LOC_MMXREGISTER:
  382. loc:=LOC_MMXREGISTER;
  383. LOC_CMMXREGISTER:
  384. loc:=LOC_CMMXREGISTER;
  385. else
  386. begin
  387. CGMessage(cg_e_illegal_expression);
  388. exit;
  389. end;
  390. end;
  391. { lets try to optimize this (PM) }
  392. { define a dest_loc that is the location }
  393. { and a ptree to verify that it is the right }
  394. { place to insert it }
  395. {$ifdef test_dest_loc}
  396. if (aktexprlevel<4) then
  397. begin
  398. dest_loc_known:=true;
  399. dest_loc:=p^.left^.location;
  400. dest_loc_tree:=p^.right;
  401. end;
  402. {$endif test_dest_loc}
  403. secondpass(p^.right);
  404. if codegenerror then
  405. exit;
  406. {$ifdef test_dest_loc}
  407. dest_loc_known:=false;
  408. if in_dest_loc then
  409. begin
  410. truelabel:=otlabel;
  411. falselabel:=oflabel;
  412. in_dest_loc:=false;
  413. exit;
  414. end;
  415. {$endif test_dest_loc}
  416. if p^.left^.resulttype^.deftype=stringdef then
  417. begin
  418. if is_ansistring(p^.left^.resulttype) then
  419. begin
  420. { the source and destinations are released
  421. in loadansistring, because an ansi string can
  422. also be in a register
  423. }
  424. loadansistring(p);
  425. end
  426. else
  427. if is_shortstring(p^.left^.resulttype) and
  428. not (p^.concat_string) then
  429. begin
  430. if is_ansistring(p^.right^.resulttype) then
  431. begin
  432. if (p^.right^.treetype=stringconstn) and
  433. (p^.right^.length=0) then
  434. begin
  435. emit_const_ref(A_MOV,S_B,
  436. 0,newreference(p^.left^.location.reference));
  437. {$IfDef regallocfix}
  438. del_reference(p^.left^.location.reference);
  439. {$EndIf regallocfix}
  440. end
  441. else
  442. loadansi2short(p^.right,p^.left);
  443. end
  444. else
  445. begin
  446. { we do not need destination anymore }
  447. del_reference(p^.left^.location.reference);
  448. del_reference(p^.right^.location.reference);
  449. loadshortstring(p);
  450. ungetiftemp(p^.right^.location.reference);
  451. end;
  452. end
  453. else if is_longstring(p^.left^.resulttype) then
  454. begin
  455. end
  456. else
  457. begin
  458. { its the only thing we have to do }
  459. del_reference(p^.right^.location.reference);
  460. end
  461. end
  462. else case p^.right^.location.loc of
  463. LOC_REFERENCE,
  464. LOC_MEM : begin
  465. { extra handling for ordinal constants }
  466. if (p^.right^.treetype in [ordconstn,fixconstn]) or
  467. (loc=LOC_CREGISTER) then
  468. begin
  469. case p^.left^.resulttype^.size of
  470. 1 : opsize:=S_B;
  471. 2 : opsize:=S_W;
  472. 4 : opsize:=S_L;
  473. { S_L is correct, the copy is done }
  474. { with two moves }
  475. 8 : opsize:=S_L;
  476. end;
  477. if loc=LOC_CREGISTER then
  478. begin
  479. emit_ref_reg(A_MOV,opsize,
  480. newreference(p^.right^.location.reference),
  481. p^.left^.location.register);
  482. if is_64bitint(p^.right^.resulttype) then
  483. begin
  484. r:=newreference(p^.right^.location.reference);
  485. inc(r^.offset,4);
  486. emit_ref_reg(A_MOV,opsize,r,
  487. p^.left^.location.registerhigh);
  488. end;
  489. {$IfDef regallocfix}
  490. del_reference(p^.right^.location.reference);
  491. {$EndIf regallocfix}
  492. end
  493. else
  494. begin
  495. emit_const_ref(A_MOV,opsize,
  496. p^.right^.location.reference.offset,
  497. newreference(p^.left^.location.reference));
  498. if is_64bitint(p^.right^.resulttype) then
  499. begin
  500. r:=newreference(p^.left^.location.reference);
  501. inc(r^.offset,4);
  502. emit_const_ref(A_MOV,opsize,
  503. 0,r);
  504. end;
  505. {$IfDef regallocfix}
  506. del_reference(p^.left^.location.reference);
  507. {$EndIf regallocfix}
  508. {emit_const_loc(A_MOV,opsize,
  509. p^.right^.location.reference.offset,
  510. p^.left^.location);}
  511. end;
  512. end
  513. else if loc=LOC_CFPUREGISTER then
  514. begin
  515. floatloadops(pfloatdef(p^.right^.resulttype)^.typ,op,opsize);
  516. emit_ref(op,opsize,
  517. newreference(p^.right^.location.reference));
  518. emit_reg(A_FSTP,S_NO,
  519. correct_fpuregister(p^.left^.location.register,fpuvaroffset+1));
  520. end
  521. else
  522. begin
  523. if (p^.right^.resulttype^.needs_inittable) and
  524. ( (p^.right^.resulttype^.deftype<>objectdef) or
  525. not(pobjectdef(p^.right^.resulttype)^.is_class)) then
  526. begin
  527. { this would be a problem }
  528. if not(p^.left^.resulttype^.needs_inittable) then
  529. internalerror(3457);
  530. { increment source reference counter }
  531. new(r);
  532. reset_reference(r^);
  533. r^.symbol:=p^.right^.resulttype^.get_inittable_label;
  534. emitpushreferenceaddr(r^);
  535. emitpushreferenceaddr(p^.right^.location.reference);
  536. emitcall('FPC_ADDREF');
  537. { decrement destination reference counter }
  538. new(r);
  539. reset_reference(r^);
  540. r^.symbol:=p^.left^.resulttype^.get_inittable_label;
  541. emitpushreferenceaddr(r^);
  542. emitpushreferenceaddr(p^.left^.location.reference);
  543. emitcall('FPC_DECREF');
  544. end;
  545. {$ifdef regallocfix}
  546. concatcopy(p^.right^.location.reference,
  547. p^.left^.location.reference,p^.left^.resulttype^.size,true,false);
  548. ungetiftemp(p^.right^.location.reference);
  549. {$Else regallocfix}
  550. concatcopy(p^.right^.location.reference,
  551. p^.left^.location.reference,p^.left^.resulttype^.size,false,false);
  552. ungetiftemp(p^.right^.location.reference);
  553. {$endif regallocfix}
  554. end;
  555. end;
  556. {$ifdef SUPPORT_MMX}
  557. LOC_CMMXREGISTER,
  558. LOC_MMXREGISTER:
  559. begin
  560. if loc=LOC_CMMXREGISTER then
  561. emit_reg_reg(A_MOVQ,S_NO,
  562. p^.right^.location.register,p^.left^.location.register)
  563. else
  564. emit_reg_ref(A_MOVQ,S_NO,
  565. p^.right^.location.register,newreference(p^.left^.location.reference));
  566. end;
  567. {$endif SUPPORT_MMX}
  568. LOC_REGISTER,
  569. LOC_CREGISTER : begin
  570. case p^.right^.resulttype^.size of
  571. 1 : opsize:=S_B;
  572. 2 : opsize:=S_W;
  573. 4 : opsize:=S_L;
  574. 8 : opsize:=S_L;
  575. end;
  576. { simplified with op_reg_loc }
  577. if loc=LOC_CREGISTER then
  578. begin
  579. emit_reg_reg(A_MOV,opsize,
  580. p^.right^.location.register,
  581. p^.left^.location.register);
  582. {$IfDef regallocfix}
  583. ungetregister(p^.right^.location.register);
  584. {$EndIf regallocfix}
  585. end
  586. else
  587. Begin
  588. emit_reg_ref(A_MOV,opsize,
  589. p^.right^.location.register,
  590. newreference(p^.left^.location.reference));
  591. {$IfDef regallocfix}
  592. ungetregister(p^.right^.location.register);
  593. del_reference(p^.left^.location.reference);
  594. {$EndIf regallocfix}
  595. end;
  596. if is_64bitint(p^.right^.resulttype) then
  597. begin
  598. { simplified with op_reg_loc }
  599. if loc=LOC_CREGISTER then
  600. emit_reg_reg(A_MOV,opsize,
  601. p^.right^.location.registerhigh,
  602. p^.left^.location.registerhigh)
  603. else
  604. begin
  605. r:=newreference(p^.left^.location.reference);
  606. inc(r^.offset,4);
  607. emit_reg_ref(A_MOV,opsize,
  608. p^.right^.location.registerhigh,r);
  609. end;
  610. end;
  611. {emit_reg_loc(A_MOV,opsize,
  612. p^.right^.location.register,
  613. p^.left^.location); }
  614. end;
  615. LOC_FPU : begin
  616. if (p^.left^.resulttype^.deftype=floatdef) then
  617. fputyp:=pfloatdef(p^.left^.resulttype)^.typ
  618. else
  619. if (p^.right^.resulttype^.deftype=floatdef) then
  620. fputyp:=pfloatdef(p^.right^.resulttype)^.typ
  621. else
  622. if (p^.right^.treetype=typeconvn) and
  623. (p^.right^.left^.resulttype^.deftype=floatdef) then
  624. fputyp:=pfloatdef(p^.right^.left^.resulttype)^.typ
  625. else
  626. fputyp:=s32real;
  627. case loc of
  628. LOC_CFPUREGISTER:
  629. begin
  630. emit_reg(A_FSTP,S_NO,
  631. correct_fpuregister(p^.left^.location.register,fpuvaroffset));
  632. dec(fpuvaroffset);
  633. end;
  634. LOC_REFERENCE:
  635. floatstore(fputyp,p^.left^.location.reference);
  636. else
  637. internalerror(48991);
  638. end;
  639. end;
  640. LOC_CFPUREGISTER: begin
  641. if (p^.left^.resulttype^.deftype=floatdef) then
  642. fputyp:=pfloatdef(p^.left^.resulttype)^.typ
  643. else
  644. if (p^.right^.resulttype^.deftype=floatdef) then
  645. fputyp:=pfloatdef(p^.right^.resulttype)^.typ
  646. else
  647. if (p^.right^.treetype=typeconvn) and
  648. (p^.right^.left^.resulttype^.deftype=floatdef) then
  649. fputyp:=pfloatdef(p^.right^.left^.resulttype)^.typ
  650. else
  651. fputyp:=s32real;
  652. emit_reg(A_FLD,S_NO,
  653. correct_fpuregister(p^.right^.location.register,fpuvaroffset));
  654. inc(fpuvaroffset);
  655. case loc of
  656. LOC_CFPUREGISTER:
  657. begin
  658. emit_reg(A_FSTP,S_NO,
  659. correct_fpuregister(p^.right^.location.register,fpuvaroffset));
  660. dec(fpuvaroffset);
  661. end;
  662. LOC_REFERENCE:
  663. floatstore(fputyp,p^.left^.location.reference);
  664. else
  665. internalerror(48992);
  666. end;
  667. end;
  668. LOC_JUMP : begin
  669. getlabel(hlabel);
  670. emitlab(truelabel);
  671. if loc=LOC_CREGISTER then
  672. emit_const_reg(A_MOV,S_B,
  673. 1,p^.left^.location.register)
  674. else
  675. emit_const_ref(A_MOV,S_B,
  676. 1,newreference(p^.left^.location.reference));
  677. {emit_const_loc(A_MOV,S_B,
  678. 1,p^.left^.location);}
  679. emitjmp(C_None,hlabel);
  680. emitlab(falselabel);
  681. if loc=LOC_CREGISTER then
  682. emit_reg_reg(A_XOR,S_B,
  683. p^.left^.location.register,
  684. p^.left^.location.register)
  685. else
  686. begin
  687. emit_const_ref(A_MOV,S_B,
  688. 0,newreference(p^.left^.location.reference));
  689. {$IfDef regallocfix}
  690. del_reference(p^.left^.location.reference);
  691. {$EndIf regallocfix}
  692. end;
  693. emitlab(hlabel);
  694. end;
  695. LOC_FLAGS : begin
  696. if loc=LOC_CREGISTER then
  697. emit_flag2reg(p^.right^.location.resflags,p^.left^.location.register)
  698. else
  699. begin
  700. ai:=new(paicpu,op_ref(A_Setcc,S_B,newreference(p^.left^.location.reference)));
  701. ai^.SetCondition(flag_2_cond[p^.right^.location.resflags]);
  702. exprasmlist^.concat(ai);
  703. end;
  704. {$IfDef regallocfix}
  705. del_reference(p^.left^.location.reference);
  706. {$EndIf regallocfix}
  707. end;
  708. end;
  709. freelabel(truelabel);
  710. freelabel(falselabel);
  711. truelabel:=otlabel;
  712. falselabel:=oflabel;
  713. end;
  714. {*****************************************************************************
  715. SecondFuncRet
  716. *****************************************************************************}
  717. procedure secondfuncret(var p : ptree);
  718. var
  719. hr : tregister;
  720. hp : preference;
  721. pp : pprocinfo;
  722. hr_valid : boolean;
  723. begin
  724. reset_reference(p^.location.reference);
  725. hr_valid:=false;
  726. if @procinfo<>pprocinfo(p^.funcretprocinfo) then
  727. begin
  728. hr:=getregister32;
  729. hr_valid:=true;
  730. hp:=new_reference(procinfo.framepointer,
  731. procinfo.framepointer_offset);
  732. emit_ref_reg(A_MOV,S_L,hp,hr);
  733. pp:=procinfo.parent;
  734. { walk up the stack frame }
  735. while pp<>pprocinfo(p^.funcretprocinfo) do
  736. begin
  737. hp:=new_reference(hr,
  738. pp^.framepointer_offset);
  739. emit_ref_reg(A_MOV,S_L,hp,hr);
  740. pp:=pp^.parent;
  741. end;
  742. p^.location.reference.base:=hr;
  743. end
  744. else
  745. p^.location.reference.base:=procinfo.framepointer;
  746. p^.location.reference.offset:=procinfo.retoffset;
  747. if ret_in_param(p^.retdef) then
  748. begin
  749. if not hr_valid then
  750. hr:=getregister32;
  751. emit_ref_reg(A_MOV,S_L,newreference(p^.location.reference),hr);
  752. p^.location.reference.base:=hr;
  753. p^.location.reference.offset:=0;
  754. end;
  755. end;
  756. {*****************************************************************************
  757. SecondArrayConstruct
  758. *****************************************************************************}
  759. const
  760. vtInteger = 0;
  761. vtBoolean = 1;
  762. vtChar = 2;
  763. vtExtended = 3;
  764. vtString = 4;
  765. vtPointer = 5;
  766. vtPChar = 6;
  767. vtObject = 7;
  768. vtClass = 8;
  769. vtWideChar = 9;
  770. vtPWideChar = 10;
  771. vtAnsiString = 11;
  772. vtCurrency = 12;
  773. vtVariant = 13;
  774. vtInterface = 14;
  775. vtWideString = 15;
  776. vtInt64 = 16;
  777. procedure secondarrayconstruct(var p : ptree);
  778. var
  779. hp : ptree;
  780. href : treference;
  781. lt : pdef;
  782. vaddr : boolean;
  783. vtype : longint;
  784. dovariant : boolean;
  785. elesize : longint;
  786. begin
  787. dovariant:=p^.forcevaria or parraydef(p^.resulttype)^.isvariant;
  788. if dovariant then
  789. elesize:=8
  790. else
  791. begin
  792. elesize:=parraydef(p^.resulttype)^.elesize;
  793. if elesize>4 then
  794. internalerror(8765678);
  795. end;
  796. if not p^.cargs then
  797. begin
  798. reset_reference(p^.location.reference);
  799. { Allocate always a temp, also if no elements are required, to
  800. be sure that location is valid (PFV) }
  801. if parraydef(p^.resulttype)^.highrange=-1 then
  802. gettempofsizereference(elesize,p^.location.reference)
  803. else
  804. gettempofsizereference((parraydef(p^.resulttype)^.highrange+1)*elesize,p^.location.reference);
  805. href:=p^.location.reference;
  806. end;
  807. hp:=p;
  808. while assigned(hp) do
  809. begin
  810. if assigned(hp^.left) then
  811. begin
  812. secondpass(hp^.left);
  813. if codegenerror then
  814. exit;
  815. if dovariant then
  816. begin
  817. { find the correct vtype value }
  818. vtype:=$ff;
  819. vaddr:=false;
  820. lt:=hp^.left^.resulttype;
  821. case lt^.deftype of
  822. enumdef,
  823. orddef :
  824. begin
  825. if (lt^.deftype=enumdef) or
  826. is_integer(lt) then
  827. vtype:=vtInteger
  828. else
  829. if is_boolean(lt) then
  830. vtype:=vtBoolean
  831. else
  832. if (lt^.deftype=orddef) and (porddef(lt)^.typ=uchar) then
  833. vtype:=vtChar;
  834. end;
  835. floatdef :
  836. begin
  837. vtype:=vtExtended;
  838. vaddr:=true;
  839. end;
  840. procvardef,
  841. pointerdef :
  842. begin
  843. if is_pchar(lt) then
  844. vtype:=vtPChar
  845. else
  846. vtype:=vtPointer;
  847. end;
  848. classrefdef :
  849. vtype:=vtClass;
  850. objectdef :
  851. begin
  852. vtype:=vtObject;
  853. end;
  854. stringdef :
  855. begin
  856. if is_shortstring(lt) then
  857. begin
  858. vtype:=vtString;
  859. vaddr:=true;
  860. end
  861. else
  862. if is_ansistring(lt) then
  863. vtype:=vtAnsiString;
  864. end;
  865. end;
  866. if vtype=$ff then
  867. internalerror(14357);
  868. { write C style pushes or an pascal array }
  869. if p^.cargs then
  870. begin
  871. if vaddr then
  872. begin
  873. emit_to_reference(hp^.left);
  874. emit_push_lea_loc(hp^.left^.location);
  875. end
  876. else
  877. emit_push_loc(hp^.left^.location);
  878. end
  879. else
  880. begin
  881. { update href to the vtype field and write it }
  882. emit_const_ref(A_MOV,S_L,
  883. vtype,newreference(href));
  884. inc(href.offset,4);
  885. { write changing field update href to the next element }
  886. if vaddr then
  887. begin
  888. emit_to_reference(hp^.left);
  889. emit_lea_loc_ref(hp^.left^.location,href);
  890. end
  891. else
  892. emit_mov_loc_ref(hp^.left^.location,href,S_L);
  893. inc(href.offset,4);
  894. end;
  895. end
  896. else
  897. { normal array constructor of the same type }
  898. begin
  899. case elesize of
  900. 1 :
  901. emit_mov_loc_ref(hp^.left^.location,href,S_B);
  902. 2 :
  903. emit_mov_loc_ref(hp^.left^.location,href,S_W);
  904. 4 :
  905. emit_mov_loc_ref(hp^.left^.location,href,S_L);
  906. else
  907. internalerror(87656781);
  908. end;
  909. inc(href.offset,elesize);
  910. end;
  911. end;
  912. { load next entry }
  913. hp:=hp^.right;
  914. end;
  915. end;
  916. end.
  917. {
  918. $Log$
  919. Revision 1.80 1999-08-26 20:24:37 michael
  920. + Hopefuly last fixes for resourcestrings
  921. Revision 1.79 1999/08/25 16:41:05 peter
  922. * resources are working again
  923. Revision 1.78 1999/08/25 11:59:43 jonas
  924. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  925. Revision 1.77 1999/08/24 22:38:51 michael
  926. * more resourcestring changes
  927. Revision 1.76 1999/08/23 11:45:39 michael
  928. * Hopefully final attempt at resourcestrings
  929. Revision 1.75 1999/08/19 13:08:49 pierre
  930. * emit_??? used
  931. Revision 1.74 1999/08/17 13:26:06 peter
  932. * arrayconstructor -> arrayofconst fixed when arraycosntructor was not
  933. variant.
  934. Revision 1.73 1999/08/13 21:33:09 peter
  935. * support for array constructors extended and more error checking
  936. Revision 1.72 1999/08/09 22:19:50 peter
  937. * classes vmt changed to only positive addresses
  938. * sharedlib creation is working
  939. Revision 1.71 1999/08/07 14:20:55 florian
  940. * some small problems fixed
  941. Revision 1.70 1999/08/04 13:45:22 florian
  942. + floating point register variables !!
  943. * pairegalloc is now generated for register variables
  944. Revision 1.69 1999/08/04 00:22:50 florian
  945. * renamed i386asm and i386base to cpuasm and cpubase
  946. Revision 1.68 1999/08/03 22:02:43 peter
  947. * moved bitmask constants to sets
  948. * some other type/const renamings
  949. Revision 1.67 1999/07/27 23:36:36 peter
  950. * try to determine the fpu type needed in assignment
  951. Revision 1.66 1999/07/24 15:12:56 michael
  952. changes for resourcestrings
  953. Revision 1.65 1999/07/23 23:09:06 peter
  954. * resourcestring fix
  955. Revision 1.64 1999/07/22 09:37:37 florian
  956. + resourcestring implemented
  957. + start of longstring support
  958. Revision 1.63 1999/07/05 20:13:12 peter
  959. * removed temp defines
  960. Revision 1.62 1999/06/30 15:43:18 florian
  961. * two bugs regarding method variables fixed
  962. - if you take in a method the address of another method
  963. don't need self anymore
  964. - if the class pointer was in a register, wrong code for a method
  965. variable load was generated
  966. Revision 1.61 1999/06/28 22:29:11 florian
  967. * qword division fixed
  968. + code for qword/int64 type casting added:
  969. range checking isn't implemented yet
  970. Revision 1.60 1999/05/31 12:42:43 peter
  971. * fixed crash with empty array constructor
  972. Revision 1.59 1999/05/27 19:44:14 peter
  973. * removed oldasm
  974. * plabel -> pasmlabel
  975. * -a switches to source writing automaticly
  976. * assembler readers OOPed
  977. * asmsymbol automaticly external
  978. * jumptables and other label fixes for asm readers
  979. Revision 1.58 1999/05/23 18:42:02 florian
  980. * better error recovering in typed constants
  981. * some problems with arrays of const fixed, some problems
  982. due my previous
  983. - the location type of array constructor is now LOC_MEM
  984. - the pushing of high fixed
  985. - parameter copying fixed
  986. - zero temp. allocation removed
  987. * small problem in the assembler writers fixed:
  988. ref to nil wasn't written correctly
  989. Revision 1.57 1999/05/21 13:54:51 peter
  990. * NEWLAB for label as symbol
  991. Revision 1.56 1999/05/17 23:51:38 peter
  992. * with temp vars now use a reference with a persistant temp instead
  993. of setting datasize
  994. Revision 1.55 1999/05/17 21:57:04 florian
  995. * new temporary ansistring handling
  996. Revision 1.54 1999/05/12 00:19:43 peter
  997. * removed R_DEFAULT_SEG
  998. * uniform float names
  999. Revision 1.53 1999/05/06 09:05:16 peter
  1000. * generic write_float and str_float
  1001. * fixed constant float conversions
  1002. Revision 1.52 1999/05/01 13:24:10 peter
  1003. * merged nasm compiler
  1004. * old asm moved to oldasm/
  1005. Revision 1.51 1999/04/28 06:01:55 florian
  1006. * changes of Bruessel:
  1007. + message handler can now take an explicit self
  1008. * typinfo fixed: sometimes the type names weren't written
  1009. * the type checking for pointer comparisations and subtraction
  1010. and are now more strict (was also buggy)
  1011. * small bug fix to link.pas to support compiling on another
  1012. drive
  1013. * probable bug in popt386 fixed: call/jmp => push/jmp
  1014. transformation didn't count correctly the jmp references
  1015. + threadvar support
  1016. * warning if ln/sqrt gets an invalid constant argument
  1017. Revision 1.50 1999/04/16 13:42:26 jonas
  1018. * more regalloc fixes (still not complete)
  1019. Revision 1.49 1999/04/13 18:57:48 florian
  1020. * classes which contain ansistring get unnecessary calls
  1021. to addref/decref when they are assigned, fixed
  1022. Revision 1.48 1999/04/09 15:48:47 jonas
  1023. * added fix for missing register deallocation (-dregallocfix)
  1024. Revision 1.47 1999/03/31 13:55:07 peter
  1025. * assembler inlining working for ag386bin
  1026. Revision 1.46 1999/03/24 23:16:52 peter
  1027. * fixed bugs 212,222,225,227,229,231,233
  1028. Revision 1.45 1999/02/25 21:02:28 peter
  1029. * ag386bin updates
  1030. + coff writer
  1031. Revision 1.44 1999/02/22 02:15:12 peter
  1032. * updates for ag386bin
  1033. Revision 1.43 1999/01/27 00:13:54 florian
  1034. * "procedure of object"-stuff fixed
  1035. Revision 1.42 1999/01/21 22:10:40 peter
  1036. * fixed array of const
  1037. * generic platform independent high() support
  1038. Revision 1.41 1999/01/20 10:20:18 peter
  1039. * don't make localvar copies for assembler procedures
  1040. Revision 1.40 1998/12/30 13:41:07 peter
  1041. * released valuepara
  1042. Revision 1.39 1998/12/19 00:23:45 florian
  1043. * ansistring memory leaks fixed
  1044. Revision 1.38 1998/12/11 00:02:51 peter
  1045. + globtype,tokens,version unit splitted from globals
  1046. Revision 1.37 1998/12/10 09:47:17 florian
  1047. + basic operations with int64/qord (compiler with -dint64)
  1048. + rtti of enumerations extended: names are now written
  1049. Revision 1.36 1998/12/04 10:18:06 florian
  1050. * some stuff for procedures of object added
  1051. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  1052. Revision 1.35 1998/11/30 09:43:04 pierre
  1053. * some range check bugs fixed (still not working !)
  1054. + added DLL writing support for win32 (also accepts variables)
  1055. + TempAnsi for code that could be used for Temporary ansi strings
  1056. handling
  1057. Revision 1.34 1998/11/28 16:20:48 peter
  1058. + support for dll variables
  1059. Revision 1.33 1998/11/27 14:50:33 peter
  1060. + open strings, $P switch support
  1061. Revision 1.32 1998/11/26 09:53:36 florian
  1062. * for classes no init/final. code is necessary, fixed
  1063. Revision 1.31 1998/11/20 15:35:54 florian
  1064. * problems with rtti fixed, hope it works
  1065. Revision 1.30 1998/11/18 17:45:24 peter
  1066. * fixes for VALUEPARA
  1067. Revision 1.29 1998/11/18 15:44:11 peter
  1068. * VALUEPARA for tp7 compatible value parameters
  1069. Revision 1.28 1998/11/17 11:32:44 peter
  1070. * optimize str:='' in H+ mode
  1071. + -! to test ansistrings
  1072. Revision 1.27 1998/11/16 15:35:39 peter
  1073. * rename laod/copystring -> load/copyshortstring
  1074. * fixed int-bool cnv bug
  1075. + char-ansistring conversion
  1076. Revision 1.26 1998/11/10 10:09:10 peter
  1077. * va_list -> array of const
  1078. Revision 1.25 1998/11/05 12:02:35 peter
  1079. * released useansistring
  1080. * removed -Sv, its now available in fpc modes
  1081. Revision 1.24 1998/10/14 08:47:14 pierre
  1082. * bugs in secondfuncret for result in subprocedures removed
  1083. Revision 1.23 1998/10/06 17:16:44 pierre
  1084. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1085. Revision 1.22 1998/10/01 09:22:53 peter
  1086. * fixed value openarray
  1087. * ungettemp of arrayconstruct
  1088. Revision 1.21 1998/09/28 11:07:39 peter
  1089. + floatdef support for array of const
  1090. Revision 1.20 1998/09/24 14:26:03 peter
  1091. * updated for new tvarrec
  1092. Revision 1.19 1998/09/23 17:49:59 peter
  1093. * high(arrayconstructor) is now correct
  1094. * procvardef support for variant record
  1095. Revision 1.18 1998/09/23 09:58:48 peter
  1096. * first working array of const things
  1097. Revision 1.17 1998/09/20 18:00:19 florian
  1098. * small compiling problems fixed
  1099. Revision 1.16 1998/09/20 17:46:48 florian
  1100. * some things regarding ansistrings fixed
  1101. Revision 1.15 1998/09/17 09:42:16 peter
  1102. + pass_2 for cg386
  1103. * Message() -> CGMessage() for pass_1/pass_2
  1104. Revision 1.14 1998/09/14 10:43:50 peter
  1105. * all internal RTL functions start with FPC_
  1106. Revision 1.13 1998/09/04 12:24:24 florian
  1107. * bug0159 fixed
  1108. Revision 1.12 1998/09/04 11:55:17 florian
  1109. * problem with -Or fixed
  1110. Revision 1.11 1998/09/03 16:03:14 florian
  1111. + rtti generation
  1112. * init table generation changed
  1113. Revision 1.10 1998/08/21 14:08:40 pierre
  1114. + TEST_FUNCRET now default (old code removed)
  1115. works also for m68k (at least compiles)
  1116. Revision 1.9 1998/08/20 09:26:37 pierre
  1117. + funcret setting in underproc testing
  1118. compile with _dTEST_FUNCRET
  1119. Revision 1.8 1998/08/10 14:49:48 peter
  1120. + localswitches, moduleswitches, globalswitches splitting
  1121. Revision 1.7 1998/07/30 13:30:33 florian
  1122. * final implemenation of exception support, maybe it needs
  1123. some fixes :)
  1124. Revision 1.6 1998/07/26 21:58:57 florian
  1125. + better support for switch $H
  1126. + index access to ansi strings added
  1127. + assigment of data (records/arrays) containing ansi strings
  1128. Revision 1.5 1998/07/24 22:16:54 florian
  1129. * internal error 10 together with array access fixed. I hope
  1130. that's the final fix.
  1131. Revision 1.4 1998/06/11 13:58:45 peter
  1132. * fixed too long line
  1133. Revision 1.3 1998/06/09 16:01:35 pierre
  1134. + added procedure directive parsing for procvars
  1135. (accepted are popstack cdecl and pascal)
  1136. + added C vars with the following syntax
  1137. var C calias 'true_c_name';(can be followed by external)
  1138. reason is that you must add the Cprefix
  1139. which is target dependent
  1140. Revision 1.2 1998/06/08 13:13:34 pierre
  1141. + temporary variables now in temp_gen.pas unit
  1142. because it is processor independent
  1143. * mppc68k.bat modified to undefine i386 and support_mmx
  1144. (which are defaults for i386)
  1145. Revision 1.1 1998/06/05 17:44:12 peter
  1146. * splitted cgi386
  1147. }