ncgld.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for nodes that handle loads and assignments which
  4. are the same for all (most) processors
  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 ncgld;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nld,cgutils;
  23. type
  24. tcgloadnode = class(tloadnode)
  25. procedure pass_generate_code;override;
  26. procedure generate_picvaraccess;virtual;
  27. procedure changereflocation(const ref: treference);
  28. end;
  29. tcgassignmentnode = class(tassignmentnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. tcgarrayconstructornode = class(tarrayconstructornode)
  33. procedure pass_generate_code;override;
  34. end;
  35. tcgrttinode = class(trttinode)
  36. procedure pass_generate_code;override;
  37. end;
  38. implementation
  39. uses
  40. cutils,
  41. systems,
  42. verbose,globtype,globals,constexp,
  43. nutils,
  44. symconst,symtype,symdef,symsym,defutil,paramgr,
  45. ncnv,ncon,nmem,nbas,ncgrtti,
  46. aasmbase,aasmtai,aasmdata,aasmcpu,
  47. cgbase,pass_2,
  48. procinfo,
  49. cpubase,parabase,
  50. tgobj,ncgutil,
  51. cgobj,
  52. ncgbas,ncgflw;
  53. {*****************************************************************************
  54. SSA (for memory temps) support
  55. *****************************************************************************}
  56. type
  57. preplacerefrec = ^treplacerefrec;
  58. treplacerefrec = record
  59. old, new: preference;
  60. ressym: tsym;
  61. end;
  62. function doreplaceref(var n: tnode; para: pointer): foreachnoderesult;
  63. var
  64. rr: preplacerefrec absolute para;
  65. begin
  66. result := fen_false;
  67. case n.nodetype of
  68. loadn:
  69. begin
  70. { regular variable }
  71. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  72. not assigned(tloadnode(n).left) and
  73. { not function result, or no exit in function }
  74. (((tloadnode(n).symtableentry <> rr^.ressym) and
  75. not(vo_is_funcret in tabstractvarsym(tloadnode(n).symtableentry).varoptions)) or
  76. not(fc_exit in flowcontrol)) and
  77. { stored in memory... }
  78. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_REFERENCE]) and
  79. { ... at the place we are looking for }
  80. references_equal(tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.reference,rr^.old^) then
  81. begin
  82. { relocate variable }
  83. tcgloadnode(n).changereflocation(rr^.new^);
  84. result := fen_norecurse_true;
  85. end;
  86. end;
  87. temprefn:
  88. begin
  89. if (ttemprefnode(n).tempinfo^.valid) and
  90. { memory temp... }
  91. (ttemprefnode(n).tempinfo^.location.loc in [LOC_REFERENCE]) and
  92. { ... at the place we are looking for }
  93. references_equal(ttemprefnode(n).tempinfo^.location.reference,rr^.old^) then
  94. begin
  95. { relocate the temp }
  96. tcgtemprefnode(n).changelocation(rr^.new^);
  97. result := fen_norecurse_true;
  98. end;
  99. end;
  100. { optimize the searching a bit }
  101. derefn,addrn,
  102. calln,inlinen,casen,
  103. addn,subn,muln,
  104. andn,orn,xorn,
  105. ltn,lten,gtn,gten,equaln,unequaln,
  106. slashn,divn,shrn,shln,notn,
  107. inn,
  108. asn,isn:
  109. result := fen_norecurse_false;
  110. end;
  111. end;
  112. function maybechangetemp(list: TAsmList; var n: tnode; const newref: treference): boolean;
  113. var
  114. rr: treplacerefrec;
  115. begin
  116. result := false;
  117. { only do for -O2 or higher (breaks debugging since }
  118. { variables move to different memory locations) }
  119. if not(cs_opt_level2 in current_settings.optimizerswitches) or
  120. { must be a copy to a memory location ... }
  121. (n.location.loc <> LOC_REFERENCE) or
  122. { not inside a control flow statement and no goto's in sight }
  123. ([fc_inflowcontrol,fc_gotolabel] * flowcontrol <> []) or
  124. { source and destination are temps (= not global variables) }
  125. not tg.istemp(n.location.reference) or
  126. not tg.istemp(newref) or
  127. { and both point to the start of a temp, and the source is a }
  128. { non-persistent temp (otherwise we need some kind of copy- }
  129. { on-write support in case later on both are still used) }
  130. (tg.gettypeoftemp(newref) <> tt_normal) or
  131. not (tg.gettypeoftemp(n.location.reference) in [tt_normal,tt_persistent]) or
  132. { and both have the same size }
  133. (tg.sizeoftemp(current_asmdata.CurrAsmList,newref) <> tg.sizeoftemp(current_asmdata.CurrAsmList,n.location.reference)) then
  134. exit;
  135. { find the source of the old reference (loadnode or tempnode) }
  136. { and replace it with the new reference }
  137. rr.old := @n.location.reference;
  138. rr.new := @newref;
  139. rr.ressym := nil;
  140. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  141. assigned(current_procinfo.procdef.funcretsym) and
  142. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  143. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  144. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  145. else
  146. rr.ressym:=current_procinfo.procdef.funcretsym;
  147. { if source not found, don't do anything }
  148. if not foreachnodestatic(n,@doreplaceref,@rr) then
  149. exit;
  150. n.location.reference := newref;
  151. result:=true;
  152. end;
  153. {*****************************************************************************
  154. SecondLoad
  155. *****************************************************************************}
  156. procedure tcgloadnode.generate_picvaraccess;
  157. begin
  158. {$ifndef sparc}
  159. location.reference.base:=current_procinfo.got;
  160. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname+'@GOT');
  161. {$endif sparc}
  162. end;
  163. procedure tcgloadnode.changereflocation(const ref: treference);
  164. var
  165. oldtemptype: ttemptype;
  166. begin
  167. if (location.loc<>LOC_REFERENCE) then
  168. internalerror(2007020812);
  169. if not tg.istemp(location.reference) then
  170. internalerror(2007020813);
  171. oldtemptype:=tg.gettypeoftemp(location.reference);
  172. if (oldtemptype = tt_persistent) then
  173. tg.ChangeTempType(current_asmdata.CurrAsmList,location.reference,tt_normal);
  174. tg.ungettemp(current_asmdata.CurrAsmList,location.reference);
  175. location.reference:=ref;
  176. tg.ChangeTempType(current_asmdata.CurrAsmList,location.reference,oldtemptype);
  177. tabstractnormalvarsym(symtableentry).localloc:=location;
  178. end;
  179. procedure tcgloadnode.pass_generate_code;
  180. var
  181. hregister : tregister;
  182. vs : tabstractnormalvarsym;
  183. gvs : tstaticvarsym;
  184. pd : tprocdef;
  185. href : treference;
  186. newsize : tcgsize;
  187. endrelocatelab,
  188. norelocatelab : tasmlabel;
  189. paraloc1 : tcgpara;
  190. begin
  191. { we don't know the size of all arrays }
  192. newsize:=def_cgsize(resultdef);
  193. location_reset(location,LOC_REFERENCE,newsize);
  194. case symtableentry.typ of
  195. absolutevarsym :
  196. begin
  197. { this is only for toasm and toaddr }
  198. case tabsolutevarsym(symtableentry).abstyp of
  199. toaddr :
  200. begin
  201. {$ifdef i386}
  202. if tabsolutevarsym(symtableentry).absseg then
  203. location.reference.segment:=NR_FS;
  204. {$endif i386}
  205. location.reference.offset:=tabsolutevarsym(symtableentry).addroffset;
  206. end;
  207. toasm :
  208. location.reference.symbol:=current_asmdata.RefAsmSymbol(tabsolutevarsym(symtableentry).mangledname);
  209. else
  210. internalerror(200310283);
  211. end;
  212. end;
  213. constsym:
  214. begin
  215. if tconstsym(symtableentry).consttyp=constresourcestring then
  216. begin
  217. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  218. location.reference.symbol:=current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',symtableentry.owner,symtableentry.name));
  219. { Resourcestring layout:
  220. TResourceStringRecord = Packed Record
  221. Name,
  222. CurrentValue,
  223. DefaultValue : AnsiString;
  224. HashValue : LongWord;
  225. end;
  226. }
  227. location.reference.offset:=sizeof(aint);
  228. end
  229. else
  230. internalerror(22798);
  231. end;
  232. staticvarsym :
  233. begin
  234. gvs:=tstaticvarsym(symtableentry);
  235. if ([vo_is_dll_var,vo_is_external] * gvs.varoptions <> []) then
  236. begin
  237. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,tstaticvarsym(symtableentry).mangledname);
  238. if (location.reference.base <> NR_NO) then
  239. exit;
  240. end;
  241. if (vo_is_dll_var in gvs.varoptions) then
  242. { DLL variable }
  243. begin
  244. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  245. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname);
  246. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,location.reference,hregister);
  247. reference_reset_base(location.reference,hregister,0);
  248. end
  249. { Thread variable }
  250. else if (vo_is_thread_var in gvs.varoptions) and
  251. not(tf_section_threadvars in target_info.flags) then
  252. begin
  253. if (tf_section_threadvars in target_info.flags) then
  254. begin
  255. if gvs.localloc.loc=LOC_INVALID then
  256. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0)
  257. else
  258. location:=gvs.localloc;
  259. {$ifdef i386}
  260. case target_info.system of
  261. system_i386_linux:
  262. location.reference.segment:=NR_GS;
  263. system_i386_win32:
  264. location.reference.segment:=NR_FS;
  265. end;
  266. {$endif i386}
  267. end
  268. else
  269. begin
  270. {
  271. Thread var loading is optimized to first check if
  272. a relocate function is available. When the function
  273. is available it is called to retrieve the address.
  274. Otherwise the address is loaded with the symbol
  275. The code needs to be in the order to first handle the
  276. call and then the address load to be sure that the
  277. register that is used for returning is the same (PFV)
  278. }
  279. current_asmdata.getjumplabel(norelocatelab);
  280. current_asmdata.getjumplabel(endrelocatelab);
  281. { make sure hregister can't allocate the register necessary for the parameter }
  282. paraloc1.init;
  283. paramanager.getintparaloc(pocall_default,1,paraloc1);
  284. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  285. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_THREADVAR_RELOCATE'),0);
  286. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  287. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,hregister,norelocatelab);
  288. { don't save the allocated register else the result will be destroyed later }
  289. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname),0);
  290. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  291. cg.a_param_ref(current_asmdata.CurrAsmList,OS_32,href,paraloc1);
  292. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  293. paraloc1.done;
  294. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  295. cg.a_call_reg(current_asmdata.CurrAsmList,hregister);
  296. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  297. cg.getcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  298. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  299. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  300. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  301. cg.a_jmp_always(current_asmdata.CurrAsmList,endrelocatelab);
  302. cg.a_label(current_asmdata.CurrAsmList,norelocatelab);
  303. { no relocation needed, load the address of the variable only, the
  304. layout of a threadvar is (4 bytes pointer):
  305. 0 - Threadvar index
  306. 4 - Threadvar value in single threading }
  307. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname),sizeof(aint));
  308. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  309. cg.a_label(current_asmdata.CurrAsmList,endrelocatelab);
  310. location.reference.base:=hregister;
  311. end;
  312. end
  313. { Normal (or external) variable }
  314. else
  315. begin
  316. if gvs.localloc.loc=LOC_INVALID then
  317. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0)
  318. else
  319. location:=gvs.localloc;
  320. end;
  321. { make const a LOC_CREFERENCE }
  322. if (gvs.varspez=vs_const) and
  323. (location.loc=LOC_REFERENCE) then
  324. location.loc:=LOC_CREFERENCE;
  325. end;
  326. paravarsym,
  327. localvarsym :
  328. begin
  329. vs:=tabstractnormalvarsym(symtableentry);
  330. { Nested variable }
  331. if assigned(left) then
  332. begin
  333. secondpass(left);
  334. if left.location.loc<>LOC_REGISTER then
  335. internalerror(200309286);
  336. if vs.localloc.loc<>LOC_REFERENCE then
  337. internalerror(200409241);
  338. reference_reset_base(location.reference,left.location.register,vs.localloc.reference.offset);
  339. end
  340. else
  341. location:=vs.localloc;
  342. { handle call by reference variables when they are not
  343. alreayd copied to local copies. Also ignore the reference
  344. when we need to load the self pointer for objects }
  345. if is_addr_param_load then
  346. begin
  347. if (location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  348. hregister:=location.register
  349. else
  350. begin
  351. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  352. { we need to load only an address }
  353. location.size:=OS_ADDR;
  354. cg.a_load_loc_reg(current_asmdata.CurrAsmList,location.size,location,hregister);
  355. end;
  356. location_reset(location,LOC_REFERENCE,newsize);
  357. location.reference.base:=hregister;
  358. end;
  359. { make const a LOC_CREFERENCE }
  360. if (vs.varspez=vs_const) and
  361. (location.loc=LOC_REFERENCE) then
  362. location.loc:=LOC_CREFERENCE;
  363. end;
  364. procsym:
  365. begin
  366. if not assigned(procdef) then
  367. internalerror(200312011);
  368. if assigned(left) then
  369. begin
  370. if (sizeof(aint) = 4) then
  371. location_reset(location,LOC_CREFERENCE,OS_64)
  372. else if (sizeof(aint) = 8) then
  373. location_reset(location,LOC_CREFERENCE,OS_128)
  374. else
  375. internalerror(20020520);
  376. tg.GetTemp(current_asmdata.CurrAsmList,2*sizeof(aint),tt_normal,location.reference);
  377. secondpass(left);
  378. { load class instance address }
  379. if left.location.loc=LOC_CONSTANT then
  380. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_ADDR,false);
  381. case left.location.loc of
  382. LOC_CREGISTER,
  383. LOC_REGISTER:
  384. begin
  385. { this is not possible for objects }
  386. if is_object(left.resultdef) then
  387. internalerror(200304234);
  388. hregister:=left.location.register;
  389. end;
  390. LOC_CREFERENCE,
  391. LOC_REFERENCE:
  392. begin
  393. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  394. if is_class_or_interface(left.resultdef) then
  395. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,hregister)
  396. else
  397. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,hregister);
  398. location_freetemp(current_asmdata.CurrAsmList,left.location);
  399. end;
  400. else
  401. internalerror(200610311);
  402. end;
  403. { store the class instance address }
  404. href:=location.reference;
  405. inc(href.offset,sizeof(aint));
  406. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,href);
  407. { virtual method ? }
  408. if (po_virtualmethod in procdef.procoptions) and
  409. not(nf_inherited in flags) then
  410. begin
  411. { load vmt pointer }
  412. reference_reset_base(href,hregister,0);
  413. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  414. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  415. { load method address }
  416. reference_reset_base(href,hregister,procdef._class.vmtmethodoffset(procdef.extnumber));
  417. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  418. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  419. { ... and store it }
  420. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  421. end
  422. else
  423. begin
  424. { load address of the function }
  425. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0);
  426. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  427. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  428. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  429. end;
  430. end
  431. else
  432. begin
  433. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  434. if (po_external in pd.procoptions) then
  435. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,pd.mangledname);
  436. {!!!!! Be aware, work on virtual methods too }
  437. if (location.reference.base = NR_NO) then
  438. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  439. end;
  440. end;
  441. labelsym :
  442. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  443. else internalerror(200510032);
  444. end;
  445. end;
  446. {*****************************************************************************
  447. SecondAssignment
  448. *****************************************************************************}
  449. procedure tcgassignmentnode.pass_generate_code;
  450. var
  451. otlabel,hlabel,oflabel : tasmlabel;
  452. href : treference;
  453. releaseright : boolean;
  454. len : aint;
  455. r:Tregister;
  456. begin
  457. location_reset(location,LOC_VOID,OS_NO);
  458. otlabel:=current_procinfo.CurrTrueLabel;
  459. oflabel:=current_procinfo.CurrFalseLabel;
  460. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  461. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  462. {
  463. in most cases we can process first the right node which contains
  464. the most complex code. Exceptions for this are:
  465. - result is in flags, loading left will then destroy the flags
  466. - result is a jump, loading left must be already done before the jump is made
  467. - result need reference count, when left points to a value used in
  468. right then decreasing the refcnt on left can possibly release
  469. the memory before right increased the refcnt, result is that an
  470. empty value is assigned
  471. - calln, call destroys most registers and is therefor 'complex'
  472. But not when the result is in the flags, then
  473. loading the left node afterwards can destroy the flags.
  474. }
  475. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  476. ((right.nodetype=calln) or
  477. (right.resultdef.needs_inittable) or
  478. (right.registersint>=left.registersint)) then
  479. begin
  480. secondpass(right);
  481. { increment source reference counter, this is
  482. useless for string constants}
  483. if (right.resultdef.needs_inittable) and
  484. (right.nodetype<>stringconstn) then
  485. begin
  486. location_force_mem(current_asmdata.CurrAsmList,right.location);
  487. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  488. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  489. end;
  490. if codegenerror then
  491. exit;
  492. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  493. { can be false }
  494. secondpass(left);
  495. { decrement destination reference counter }
  496. if (left.resultdef.needs_inittable) then
  497. begin
  498. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  499. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  500. end;
  501. if codegenerror then
  502. exit;
  503. end
  504. else
  505. begin
  506. { calculate left sides }
  507. secondpass(left);
  508. { decrement destination reference counter }
  509. if (left.resultdef.needs_inittable) then
  510. begin
  511. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  512. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  513. end;
  514. if codegenerror then
  515. exit;
  516. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  517. { can be false }
  518. secondpass(right);
  519. { increment source reference counter, this is
  520. useless for string constants}
  521. if (right.resultdef.needs_inittable) and
  522. (right.nodetype<>stringconstn) then
  523. begin
  524. location_force_mem(current_asmdata.CurrAsmList,right.location);
  525. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  526. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  527. end;
  528. if codegenerror then
  529. exit;
  530. end;
  531. releaseright:=true;
  532. { shortstring assignments are handled separately }
  533. if is_shortstring(left.resultdef) then
  534. begin
  535. {
  536. we can get here only in the following situations
  537. for the right node:
  538. - empty constant string
  539. - char
  540. }
  541. { The addn is replaced by a blockn or calln that already returns
  542. a shortstring }
  543. if is_shortstring(right.resultdef) and
  544. (right.nodetype in [blockn,calln]) then
  545. begin
  546. { nothing to do }
  547. end
  548. { empty constant string }
  549. else if (right.nodetype=stringconstn) and
  550. (tstringconstnode(right).len=0) then
  551. begin
  552. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,0,left.location.reference);
  553. end
  554. { char loading }
  555. else if is_char(right.resultdef) then
  556. begin
  557. if right.nodetype=ordconstn then
  558. begin
  559. if (target_info.endian = endian_little) then
  560. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,(tordconstnode(right).value.svalue shl 8) or 1,
  561. left.location.reference)
  562. else
  563. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,tordconstnode(right).value.svalue or (1 shl 8),
  564. left.location.reference);
  565. end
  566. else
  567. begin
  568. href:=left.location.reference;
  569. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,1,href);
  570. inc(href.offset,1);
  571. case right.location.loc of
  572. LOC_REGISTER,
  573. LOC_CREGISTER :
  574. begin
  575. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  576. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,href);
  577. end;
  578. LOC_REFERENCE,
  579. LOC_CREFERENCE :
  580. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,href);
  581. else
  582. internalerror(200205111);
  583. end;
  584. end;
  585. end
  586. else
  587. internalerror(200204249);
  588. end
  589. { try to reuse memory locations instead of copying }
  590. { copy to a memory location ... }
  591. else if (right.location.loc = LOC_REFERENCE) and
  592. maybechangetemp(current_asmdata.CurrAsmList,left,right.location.reference) then
  593. begin
  594. { if it worked, we're done }
  595. end
  596. else
  597. begin
  598. { SSA support }
  599. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  600. maybechangeloadnodereg(current_asmdata.CurrAsmList,right,true);
  601. case right.location.loc of
  602. LOC_CONSTANT :
  603. begin
  604. {$ifndef cpu64bit}
  605. if right.location.size in [OS_64,OS_S64] then
  606. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  607. else
  608. {$endif cpu64bit}
  609. cg.a_load_const_loc(current_asmdata.CurrAsmList,right.location.value,left.location);
  610. end;
  611. LOC_REFERENCE,
  612. LOC_CREFERENCE :
  613. begin
  614. case left.location.loc of
  615. LOC_REGISTER,
  616. LOC_CREGISTER :
  617. begin
  618. {$ifndef cpu64bit}
  619. if left.location.size in [OS_64,OS_S64] then
  620. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  621. else
  622. {$endif cpu64bit}
  623. cg.a_load_ref_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.register);
  624. end;
  625. LOC_FPUREGISTER,
  626. LOC_CFPUREGISTER :
  627. begin
  628. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  629. right.location.size,left.location.size,
  630. right.location.reference,
  631. left.location.register);
  632. end;
  633. LOC_REFERENCE,
  634. LOC_CREFERENCE :
  635. begin
  636. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  637. { Use unaligned copy when the offset is not aligned }
  638. len:=left.resultdef.size;
  639. if (right.location.reference.offset mod sizeof(aint)<>0) or
  640. (left.location.reference.offset mod sizeof(aint)<>0) or
  641. (right.resultdef.alignment<sizeof(aint)) then
  642. cg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len)
  643. else
  644. cg.g_concatcopy(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len);
  645. end;
  646. LOC_MMREGISTER,
  647. LOC_CMMREGISTER:
  648. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  649. right.location.size,
  650. left.location.size,
  651. right.location.reference,
  652. left.location.register,mms_movescalar);
  653. LOC_SUBSETREG,
  654. LOC_CSUBSETREG:
  655. cg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sreg);
  656. LOC_SUBSETREF,
  657. LOC_CSUBSETREF:
  658. {$ifndef cpu64bit}
  659. if right.location.size in [OS_64,OS_S64] then
  660. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  661. else
  662. {$endif cpu64bit}
  663. cg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sref);
  664. else
  665. internalerror(200203284);
  666. end;
  667. end;
  668. {$ifdef SUPPORT_MMX}
  669. LOC_CMMXREGISTER,
  670. LOC_MMXREGISTER:
  671. begin
  672. if left.location.loc=LOC_CMMXREGISTER then
  673. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  674. else
  675. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  676. end;
  677. {$endif SUPPORT_MMX}
  678. LOC_MMREGISTER,
  679. LOC_CMMREGISTER:
  680. begin
  681. if left.resultdef.typ=arraydef then
  682. begin
  683. end
  684. else
  685. begin
  686. if left.location.loc=LOC_CMMREGISTER then
  687. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  688. else
  689. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  690. end;
  691. end;
  692. LOC_REGISTER,
  693. LOC_CREGISTER :
  694. begin
  695. {$ifndef cpu64bit}
  696. if left.location.size in [OS_64,OS_S64] then
  697. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  698. right.location.register64,left.location)
  699. else
  700. {$endif cpu64bit}
  701. cg.a_load_reg_loc(current_asmdata.CurrAsmList,right.location.size,right.location.register,left.location);
  702. end;
  703. LOC_FPUREGISTER,
  704. LOC_CFPUREGISTER :
  705. begin
  706. { we can't do direct moves between fpu and mm registers }
  707. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  708. begin
  709. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,false);
  710. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  711. right.location.size,left.location.size,
  712. right.location.register,left.location.register,mms_movescalar);
  713. end
  714. else
  715. cg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  716. right.location.size,
  717. right.location.register,left.location);
  718. end;
  719. LOC_SUBSETREG,
  720. LOC_CSUBSETREG:
  721. begin
  722. cg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  723. right.location.size,right.location.sreg,left.location);
  724. end;
  725. LOC_SUBSETREF,
  726. LOC_CSUBSETREF:
  727. begin
  728. {$ifndef cpu64bit}
  729. if right.location.size in [OS_64,OS_S64] then
  730. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  731. else
  732. {$endif cpu64bit}
  733. cg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  734. right.location.size,right.location.sref,left.location);
  735. end;
  736. LOC_JUMP :
  737. begin
  738. current_asmdata.getjumplabel(hlabel);
  739. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  740. cg.a_load_const_loc(current_asmdata.CurrAsmList,1,left.location);
  741. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  742. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  743. cg.a_load_const_loc(current_asmdata.CurrAsmList,0,left.location);
  744. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  745. end;
  746. {$ifdef cpuflags}
  747. LOC_FLAGS :
  748. begin
  749. {This can be a wordbool or longbool too, no?}
  750. case left.location.loc of
  751. LOC_REGISTER,LOC_CREGISTER:
  752. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.register);
  753. LOC_REFERENCE:
  754. cg.g_flags2ref(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.reference);
  755. LOC_SUBSETREG,LOC_SUBSETREF:
  756. begin
  757. r:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(left.resultdef));
  758. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,r);
  759. cg.a_load_reg_loc(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),r,left.location);
  760. end;
  761. else
  762. internalerror(200203273);
  763. end;
  764. end;
  765. {$endif cpuflags}
  766. end;
  767. end;
  768. if releaseright then
  769. location_freetemp(current_asmdata.CurrAsmList,right.location);
  770. current_procinfo.CurrTrueLabel:=otlabel;
  771. current_procinfo.CurrFalseLabel:=oflabel;
  772. end;
  773. {*****************************************************************************
  774. SecondArrayConstruct
  775. *****************************************************************************}
  776. const
  777. vtInteger = 0;
  778. vtBoolean = 1;
  779. vtChar = 2;
  780. vtExtended = 3;
  781. vtString = 4;
  782. vtPointer = 5;
  783. vtPChar = 6;
  784. vtObject = 7;
  785. vtClass = 8;
  786. vtWideChar = 9;
  787. vtPWideChar = 10;
  788. vtAnsiString32 = 11;
  789. vtCurrency = 12;
  790. vtVariant = 13;
  791. vtInterface = 14;
  792. vtWideString = 15;
  793. vtInt64 = 16;
  794. vtQWord = 17;
  795. vtAnsiString16 = 18;
  796. vtAnsiString64 = 19;
  797. procedure tcgarrayconstructornode.pass_generate_code;
  798. var
  799. hp : tarrayconstructornode;
  800. href : treference;
  801. lt : tdef;
  802. vaddr : boolean;
  803. vtype : longint;
  804. freetemp,
  805. dovariant : boolean;
  806. elesize : longint;
  807. tmpreg : tregister;
  808. paraloc : tcgparalocation;
  809. begin
  810. if is_packed_array(resultdef) then
  811. internalerror(200608042);
  812. dovariant:=(nf_forcevaria in flags) or is_variant_array(resultdef);
  813. if dovariant then
  814. elesize:=sizeof(aint)+sizeof(aint)
  815. else
  816. elesize:=tarraydef(resultdef).elesize;
  817. location_reset(location,LOC_CREFERENCE,OS_NO);
  818. fillchar(paraloc,sizeof(paraloc),0);
  819. { Allocate always a temp, also if no elements are required, to
  820. be sure that location is valid (PFV) }
  821. if tarraydef(resultdef).highrange=-1 then
  822. tg.GetTemp(current_asmdata.CurrAsmList,elesize,tt_normal,location.reference)
  823. else
  824. tg.GetTemp(current_asmdata.CurrAsmList,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  825. href:=location.reference;
  826. { Process nodes in array constructor }
  827. hp:=self;
  828. while assigned(hp) do
  829. begin
  830. if assigned(hp.left) then
  831. begin
  832. freetemp:=true;
  833. secondpass(hp.left);
  834. if codegenerror then
  835. exit;
  836. { Move flags and jump in register }
  837. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  838. location_force_reg(current_asmdata.CurrAsmList,hp.left.location,def_cgsize(hp.left.resultdef),false);
  839. if dovariant then
  840. begin
  841. { find the correct vtype value }
  842. vtype:=$ff;
  843. vaddr:=false;
  844. lt:=hp.left.resultdef;
  845. case lt.typ of
  846. enumdef,
  847. orddef :
  848. begin
  849. if is_64bit(lt) then
  850. begin
  851. case torddef(lt).ordtype of
  852. scurrency:
  853. vtype:=vtCurrency;
  854. s64bit:
  855. vtype:=vtInt64;
  856. u64bit:
  857. vtype:=vtQWord;
  858. end;
  859. freetemp:=false;
  860. vaddr:=true;
  861. end
  862. else if (lt.typ=enumdef) or
  863. is_integer(lt) then
  864. vtype:=vtInteger
  865. else
  866. if is_boolean(lt) then
  867. vtype:=vtBoolean
  868. else
  869. if (lt.typ=orddef) then
  870. begin
  871. case torddef(lt).ordtype of
  872. uchar:
  873. vtype:=vtChar;
  874. uwidechar:
  875. vtype:=vtWideChar;
  876. end;
  877. end;
  878. end;
  879. floatdef :
  880. begin
  881. if is_currency(lt) then
  882. vtype:=vtCurrency
  883. else
  884. vtype:=vtExtended;
  885. freetemp:=false;
  886. vaddr:=true;
  887. end;
  888. procvardef,
  889. pointerdef :
  890. begin
  891. if is_pchar(lt) then
  892. vtype:=vtPChar
  893. else if is_pwidechar(lt) then
  894. vtype:=vtPWideChar
  895. else
  896. vtype:=vtPointer;
  897. end;
  898. variantdef :
  899. begin
  900. vtype:=vtVariant;
  901. vaddr:=true;
  902. freetemp:=false;
  903. end;
  904. classrefdef :
  905. vtype:=vtClass;
  906. objectdef :
  907. if is_interface(lt) then
  908. vtype:=vtInterface
  909. { vtObject really means a class based on TObject }
  910. else if is_class(lt) then
  911. vtype:=vtObject
  912. else
  913. internalerror(200505171);
  914. stringdef :
  915. begin
  916. if is_shortstring(lt) then
  917. begin
  918. vtype:=vtString;
  919. vaddr:=true;
  920. freetemp:=false;
  921. end
  922. else
  923. if is_ansistring(lt) then
  924. begin
  925. vtype:=vtAnsiString;
  926. freetemp:=false;
  927. end
  928. else
  929. if is_widestring(lt) then
  930. begin
  931. vtype:=vtWideString;
  932. freetemp:=false;
  933. end;
  934. end;
  935. end;
  936. if vtype=$ff then
  937. internalerror(14357);
  938. { write changing field update href to the next element }
  939. inc(href.offset,sizeof(aint));
  940. if vaddr then
  941. begin
  942. location_force_mem(current_asmdata.CurrAsmList,hp.left.location);
  943. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  944. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.location.reference,tmpreg);
  945. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,href);
  946. end
  947. else
  948. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,hp.left.location,href);
  949. { update href to the vtype field and write it }
  950. dec(href.offset,sizeof(aint));
  951. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  952. { goto next array element }
  953. inc(href.offset,sizeof(aint)*2);
  954. end
  955. else
  956. { normal array constructor of the same type }
  957. begin
  958. if resultdef.needs_inittable then
  959. freetemp:=false;
  960. case hp.left.location.loc of
  961. LOC_MMREGISTER,
  962. LOC_CMMREGISTER:
  963. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,
  964. hp.left.location.register,href,mms_movescalar);
  965. LOC_FPUREGISTER,
  966. LOC_CFPUREGISTER :
  967. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,hp.left.location.register,href);
  968. LOC_REFERENCE,
  969. LOC_CREFERENCE :
  970. begin
  971. if is_shortstring(hp.left.resultdef) then
  972. cg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  973. Tstringdef(hp.left.resultdef).len)
  974. else
  975. cg.g_concatcopy(current_asmdata.CurrAsmList,hp.left.location.reference,href,elesize);
  976. end;
  977. else
  978. begin
  979. {$ifndef cpu64bit}
  980. if hp.left.location.size in [OS_64,OS_S64] then
  981. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  982. else
  983. {$endif cpu64bit}
  984. cg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location,href);
  985. end;
  986. end;
  987. inc(href.offset,elesize);
  988. end;
  989. if freetemp then
  990. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  991. end;
  992. { load next entry }
  993. hp:=tarrayconstructornode(hp.right);
  994. end;
  995. end;
  996. {*****************************************************************************
  997. SecondRTTI
  998. *****************************************************************************}
  999. procedure tcgrttinode.pass_generate_code;
  1000. begin
  1001. location_reset(location,LOC_CREFERENCE,OS_NO);
  1002. case rttidatatype of
  1003. rdt_normal:
  1004. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  1005. rdt_ord2str:
  1006. location.reference.symbol:=RTTIWriter.get_rtti_label_ord2str(rttidef,rttitype);
  1007. rdt_str2ord:
  1008. location.reference.symbol:=RTTIWriter.get_rtti_label_str2ord(rttidef,rttitype);
  1009. end;
  1010. end;
  1011. begin
  1012. cloadnode:=tcgloadnode;
  1013. cassignmentnode:=tcgassignmentnode;
  1014. carrayconstructornode:=tcgarrayconstructornode;
  1015. crttinode:=tcgrttinode;
  1016. end.