ncgld.pas 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. globtype,
  23. symtype,symsym,
  24. aasmdata,
  25. node,nld,cgutils;
  26. type
  27. tcgloadnode = class(tloadnode)
  28. protected
  29. procedure generate_nested_access(vs: tsym);virtual;
  30. procedure generate_absaddr_access(vs: tabsolutevarsym); virtual;
  31. public
  32. procedure pass_generate_code;override;
  33. procedure changereflocation(const ref: treference);
  34. end;
  35. tcgassignmentnode = class(tassignmentnode)
  36. protected
  37. function maybechangetemp(list: TAsmList; var n: tnode; const newref: treference): boolean;virtual;
  38. public
  39. procedure pass_generate_code;override;
  40. end;
  41. tcgarrayconstructornode = class(tarrayconstructornode)
  42. protected
  43. procedure makearrayref(var ref: treference; eledef: tdef);virtual;
  44. procedure advancearrayoffset(var ref: treference; elesize: asizeint);virtual;
  45. public
  46. procedure pass_generate_code;override;
  47. end;
  48. tcgrttinode = class(trttinode)
  49. procedure pass_generate_code;override;
  50. end;
  51. implementation
  52. uses
  53. cutils,
  54. systems,
  55. verbose,globals,constexp,
  56. nutils,
  57. symtable,symconst,symdef,defutil,paramgr,ncon,nbas,ncgrtti,
  58. aasmbase,
  59. cgbase,pass_2,
  60. procinfo,
  61. cpubase,parabase,cpuinfo,
  62. tgobj,ncgutil,
  63. cgobj,hlcgobj,
  64. ncgbas,ncgflw,
  65. wpobase;
  66. {*****************************************************************************
  67. SSA (for memory temps) support
  68. *****************************************************************************}
  69. type
  70. preplacerefrec = ^treplacerefrec;
  71. treplacerefrec = record
  72. old, new: preference;
  73. ressym: tsym;
  74. end;
  75. function doreplaceref(var n: tnode; para: pointer): foreachnoderesult;
  76. var
  77. rr: preplacerefrec absolute para;
  78. begin
  79. result := fen_false;
  80. case n.nodetype of
  81. loadn:
  82. begin
  83. { regular variable }
  84. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  85. not assigned(tloadnode(n).left) and
  86. { not function result, or no exit in function }
  87. (((tloadnode(n).symtableentry <> rr^.ressym) and
  88. not(vo_is_funcret in tabstractvarsym(tloadnode(n).symtableentry).varoptions)) or
  89. not(fc_exit in flowcontrol)) and
  90. { stored in memory... }
  91. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_REFERENCE]) and
  92. { ... at the place we are looking for }
  93. references_equal(tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.reference,rr^.old^) and
  94. { its address cannot have escaped the current routine }
  95. not(tabstractvarsym(tloadnode(n).symtableentry).addr_taken) then
  96. begin
  97. { relocate variable }
  98. tcgloadnode(n).changereflocation(rr^.new^);
  99. result := fen_norecurse_true;
  100. end;
  101. end;
  102. temprefn:
  103. begin
  104. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  105. { memory temp... }
  106. (ttemprefnode(n).tempinfo^.location.loc in [LOC_REFERENCE]) and
  107. { ... at the place we are looking for }
  108. references_equal(ttemprefnode(n).tempinfo^.location.reference,rr^.old^) and
  109. { its address cannot have escaped the current routine }
  110. not(ti_addr_taken in ttemprefnode(n).tempinfo^.flags) then
  111. begin
  112. { relocate the temp }
  113. tcgtemprefnode(n).changelocation(rr^.new^);
  114. result := fen_norecurse_true;
  115. end;
  116. end;
  117. { Subscriptn must be rejected, otherwise we may replace an
  118. an entire record with a temp for its first field, mantis #13948)
  119. Exception: the field's size is the same as the entire record
  120. The same goes for array indexing
  121. }
  122. subscriptn,
  123. vecn:
  124. if not(tunarynode(n).left.resultdef.typ in [recorddef,objectdef,arraydef,stringdef]) or
  125. { make sure we don't try to call resultdef.size for types that
  126. don't have a compile-time size such as open arrays }
  127. is_special_array(tunarynode(n).left.resultdef) or
  128. (tunarynode(n).left.resultdef.size<>tunarynode(n).resultdef.size) then
  129. result := fen_norecurse_false;
  130. { optimize the searching a bit }
  131. derefn,addrn,
  132. calln,inlinen,casen,
  133. addn,subn,muln,
  134. andn,orn,xorn,
  135. ltn,lten,gtn,gten,equaln,unequaln,
  136. slashn,divn,shrn,shln,notn,
  137. inn,
  138. asn,isn:
  139. result := fen_norecurse_false;
  140. end;
  141. end;
  142. function tcgassignmentnode.maybechangetemp(list: TAsmList; var n: tnode; const newref: treference): boolean;
  143. var
  144. rr: treplacerefrec;
  145. begin
  146. result := false;
  147. { only do for -O2 or higher (breaks debugging since }
  148. { variables move to different memory locations) }
  149. if not(cs_opt_level2 in current_settings.optimizerswitches) or
  150. { must be a copy to a memory location ... }
  151. (n.location.loc <> LOC_REFERENCE) or
  152. { not inside a control flow statement and no goto's in sight }
  153. ([fc_inflowcontrol,fc_gotolabel] * flowcontrol <> []) or
  154. { not for refcounted types, because those locations are }
  155. { still used later on in initialisation/finalisation code }
  156. is_managed_type(n.resultdef) or
  157. { source and destination are temps (= not global variables) }
  158. not tg.istemp(n.location.reference) or
  159. not tg.istemp(newref) or
  160. { and both point to the start of a temp, and the source is a }
  161. { non-persistent temp (otherwise we need some kind of copy- }
  162. { on-write support in case later on both are still used) }
  163. (tg.gettypeoftemp(newref) <> tt_normal) or
  164. not (tg.gettypeoftemp(n.location.reference) in [tt_normal,tt_persistent]) or
  165. { and both have the same size }
  166. (tg.sizeoftemp(current_asmdata.CurrAsmList,newref) <> tg.sizeoftemp(current_asmdata.CurrAsmList,n.location.reference)) then
  167. exit;
  168. { find the source of the old reference (loadnode or tempnode) }
  169. { and replace it with the new reference }
  170. rr.old := @n.location.reference;
  171. rr.new := @newref;
  172. rr.ressym := nil;
  173. if assigned(current_procinfo.procdef.funcretsym) and
  174. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  175. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  176. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  177. else
  178. rr.ressym:=current_procinfo.procdef.funcretsym;
  179. { if source not found, don't do anything }
  180. if not foreachnodestatic(n,@doreplaceref,@rr) then
  181. exit;
  182. n.location.reference := newref;
  183. result:=true;
  184. end;
  185. {*****************************************************************************
  186. SecondLoad
  187. *****************************************************************************}
  188. procedure tcgloadnode.changereflocation(const ref: treference);
  189. var
  190. oldtemptype: ttemptype;
  191. begin
  192. if (location.loc<>LOC_REFERENCE) then
  193. internalerror(2007020812);
  194. if not tg.istemp(location.reference) then
  195. internalerror(2007020813);
  196. oldtemptype:=tg.gettypeoftemp(location.reference);
  197. if (oldtemptype = tt_persistent) then
  198. tg.ChangeTempType(current_asmdata.CurrAsmList,location.reference,tt_normal);
  199. tg.ungettemp(current_asmdata.CurrAsmList,location.reference);
  200. location.reference:=ref;
  201. tg.ChangeTempType(current_asmdata.CurrAsmList,location.reference,oldtemptype);
  202. tabstractnormalvarsym(symtableentry).localloc:=location;
  203. end;
  204. procedure tcgloadnode.generate_nested_access(vs: tsym);
  205. var
  206. { paramter declared as tsym to reduce interface unit dependencies }
  207. lvs: tabstractnormalvarsym absolute vs;
  208. begin
  209. secondpass(left);
  210. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  211. internalerror(200309286);
  212. if lvs.localloc.loc<>LOC_REFERENCE then
  213. internalerror(200409241);
  214. hlcg.reference_reset_base(location.reference,left.resultdef,left.location.register,lvs.localloc.reference.offset,lvs.localloc.reference.alignment);
  215. end;
  216. procedure tcgloadnode.generate_absaddr_access(vs: tabsolutevarsym);
  217. begin
  218. location.reference.offset:=aint(vs.addroffset);
  219. end;
  220. procedure tcgloadnode.pass_generate_code;
  221. var
  222. hregister : tregister;
  223. vs : tabstractnormalvarsym;
  224. gvs : tstaticvarsym;
  225. pd : tprocdef;
  226. href : treference;
  227. newsize : tcgsize;
  228. endrelocatelab,
  229. norelocatelab : tasmlabel;
  230. paraloc1 : tcgpara;
  231. pvd : tdef;
  232. begin
  233. { we don't know the size of all arrays }
  234. newsize:=def_cgsize(resultdef);
  235. { alignment is overridden per case below }
  236. location_reset_ref(location,LOC_REFERENCE,newsize,resultdef.alignment);
  237. case symtableentry.typ of
  238. absolutevarsym :
  239. begin
  240. { this is only for toasm and toaddr }
  241. case tabsolutevarsym(symtableentry).abstyp of
  242. toaddr :
  243. generate_absaddr_access(tabsolutevarsym(symtableentry));
  244. toasm :
  245. location.reference.symbol:=current_asmdata.RefAsmSymbol(tabsolutevarsym(symtableentry).mangledname);
  246. else
  247. internalerror(200310283);
  248. end;
  249. end;
  250. constsym:
  251. begin
  252. if tconstsym(symtableentry).consttyp=constresourcestring then
  253. begin
  254. location_reset_ref(location,LOC_CREFERENCE,def_cgsize(cansistringtype),cansistringtype.size);
  255. location.reference.symbol:=current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',symtableentry.owner,symtableentry.name),AT_DATA);
  256. { Resourcestring layout:
  257. TResourceStringRecord = Packed Record
  258. Name,
  259. CurrentValue,
  260. DefaultValue : AnsiString;
  261. HashValue : LongWord;
  262. end;
  263. }
  264. location.reference.offset:=cansistringtype.size;
  265. end
  266. else
  267. internalerror(22798);
  268. end;
  269. staticvarsym :
  270. begin
  271. gvs:=tstaticvarsym(symtableentry);
  272. if (vo_is_dll_var in gvs.varoptions) then
  273. { DLL variable }
  274. begin
  275. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  276. if not(vo_is_weak_external in gvs.varoptions) then
  277. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname)
  278. else
  279. location.reference.symbol:=current_asmdata.WeakRefAsmSymbol(tstaticvarsym(symtableentry).mangledname);
  280. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,location.reference,hregister);
  281. reference_reset_base(location.reference,hregister,0,location.reference.alignment);
  282. end
  283. { Thread variable }
  284. else if (vo_is_thread_var in gvs.varoptions) then
  285. begin
  286. if (tf_section_threadvars in target_info.flags) then
  287. begin
  288. if target_info.system in [system_i386_win32,system_x86_64_win64] then
  289. begin
  290. paraloc1.init;
  291. pd:=search_system_proc('fpc_tls_add');
  292. paramanager.getintparaloc(pd,1,paraloc1);
  293. if not(vo_is_weak_external in gvs.varoptions) then
  294. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(gvs.mangledname),0,sizeof(pint))
  295. else
  296. reference_reset_symbol(href,current_asmdata.WeakRefAsmSymbol(gvs.mangledname),0,sizeof(pint));
  297. cg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,href,paraloc1);
  298. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  299. paraloc1.done;
  300. cg.g_call(current_asmdata.CurrAsmList,'FPC_TLS_ADD');
  301. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  302. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  303. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  304. location.reference.base:=hregister;
  305. end
  306. else
  307. begin
  308. if gvs.localloc.loc=LOC_INVALID then
  309. if not(vo_is_weak_external in gvs.varoptions) then
  310. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0,location.reference.alignment)
  311. else
  312. reference_reset_symbol(location.reference,current_asmdata.WeakRefAsmSymbol(gvs.mangledname),0,location.reference.alignment)
  313. else
  314. location:=gvs.localloc;
  315. {$ifdef i386}
  316. case target_info.system of
  317. system_i386_linux,system_i386_android:
  318. location.reference.segment:=NR_GS;
  319. end;
  320. {$endif i386}
  321. end;
  322. end
  323. else
  324. begin
  325. {
  326. Thread var loading is optimized to first check if
  327. a relocate function is available. When the function
  328. is available it is called to retrieve the address.
  329. Otherwise the address is loaded with the symbol
  330. The code needs to be in the order to first handle the
  331. call and then the address load to be sure that the
  332. register that is used for returning is the same (PFV)
  333. }
  334. current_asmdata.getjumplabel(norelocatelab);
  335. current_asmdata.getjumplabel(endrelocatelab);
  336. { make sure hregister can't allocate the register necessary for the parameter }
  337. pvd:=search_system_type('TRELOCATETHREADVARHANDLER').typedef;
  338. if pvd.typ<>procvardef then
  339. internalerror(2012120901);
  340. paraloc1.init;
  341. paramanager.getintparaloc(tprocvardef(pvd),1,paraloc1);
  342. hregister:=hlcg.getaddressregister(current_asmdata.CurrAsmList,pvd);
  343. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_THREADVAR_RELOCATE'),0,pvd.size);
  344. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,pvd,pvd,href,hregister);
  345. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,pvd,OC_EQ,0,hregister,norelocatelab);
  346. { don't save the allocated register else the result will be destroyed later }
  347. if not(vo_is_weak_external in gvs.varoptions) then
  348. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(gvs.mangledname),0,sizeof(pint))
  349. else
  350. reference_reset_symbol(href,current_asmdata.WeakRefAsmSymbol(gvs.mangledname),0,sizeof(pint));
  351. cg.a_load_ref_cgpara(current_asmdata.CurrAsmList,OS_32,href,paraloc1);
  352. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  353. paraloc1.done;
  354. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  355. cg.a_call_reg(current_asmdata.CurrAsmList,hregister);
  356. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  357. cg.getcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  358. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  359. hregister:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  360. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  361. cg.a_jmp_always(current_asmdata.CurrAsmList,endrelocatelab);
  362. cg.a_label(current_asmdata.CurrAsmList,norelocatelab);
  363. { no relocation needed, load the address of the variable only, the
  364. layout of a threadvar is (4 bytes pointer):
  365. 0 - Threadvar index
  366. 4 - Threadvar value in single threading }
  367. if not(vo_is_weak_external in gvs.varoptions) then
  368. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(gvs.mangledname),sizeof(pint),sizeof(pint))
  369. else
  370. reference_reset_symbol(href,current_asmdata.WeakRefAsmSymbol(gvs.mangledname),sizeof(pint),sizeof(pint));
  371. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,resultdef,voidpointertype,href,hregister);
  372. cg.a_label(current_asmdata.CurrAsmList,endrelocatelab);
  373. hlcg.reference_reset_base(location.reference,voidpointertype,hregister,0,location.reference.alignment);
  374. end;
  375. end
  376. { Normal (or external) variable }
  377. else
  378. begin
  379. if gvs.localloc.loc=LOC_INVALID then
  380. if not(vo_is_weak_external in gvs.varoptions) then
  381. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0,location.reference.alignment)
  382. else
  383. reference_reset_symbol(location.reference,current_asmdata.WeakRefAsmSymbol(gvs.mangledname),0,location.reference.alignment)
  384. else
  385. location:=gvs.localloc;
  386. end;
  387. { make const a LOC_CREFERENCE }
  388. if (gvs.varspez=vs_const) and
  389. (location.loc=LOC_REFERENCE) then
  390. location.loc:=LOC_CREFERENCE;
  391. end;
  392. paravarsym,
  393. localvarsym :
  394. begin
  395. vs:=tabstractnormalvarsym(symtableentry);
  396. { Nested variable }
  397. if assigned(left) then
  398. generate_nested_access(vs)
  399. else
  400. location:=vs.localloc;
  401. { handle call by reference variables when they are not
  402. already copied to local copies. Also ignore the reference
  403. when we need to load the self pointer for objects }
  404. if is_addr_param_load then
  405. begin
  406. if (location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  407. hregister:=location.register
  408. else
  409. begin
  410. hregister:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  411. { we need to load only an address }
  412. location.size:=int_cgsize(voidpointertype.size);
  413. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,location,hregister);
  414. end;
  415. { assume packed records may always be unaligned }
  416. if not(resultdef.typ in [recorddef,objectdef]) or
  417. (tabstractrecordsymtable(tabstractrecorddef(resultdef).symtable).usefieldalignment<>1) then
  418. location_reset_ref(location,LOC_REFERENCE,newsize,resultdef.alignment)
  419. else
  420. location_reset_ref(location,LOC_REFERENCE,newsize,1);
  421. hlcg.reference_reset_base(location.reference,voidpointertype,hregister,0,location.reference.alignment);
  422. end;
  423. { make const a LOC_CREFERENCE }
  424. if (vs.varspez=vs_const) and
  425. (location.loc=LOC_REFERENCE) then
  426. location.loc:=LOC_CREFERENCE;
  427. end;
  428. procsym:
  429. begin
  430. if not assigned(procdef) then
  431. internalerror(200312011);
  432. if assigned(left) then
  433. begin
  434. location_reset(location,LOC_CREGISTER,int_cgsize(voidpointertype.size*2));
  435. secondpass(left);
  436. { load class instance/classrefdef address }
  437. if left.location.loc=LOC_CONSTANT then
  438. { todo: exact type for hlcg (can't use left.resultdef, because can be TP-style object, which is not pointer-sized) }
  439. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,voidpointertype,false);
  440. case left.location.loc of
  441. LOC_CREGISTER,
  442. LOC_REGISTER:
  443. begin
  444. { this is not possible for objects }
  445. if is_object(left.resultdef) then
  446. internalerror(200304234);
  447. location.registerhi:=left.location.register;
  448. end;
  449. LOC_CREFERENCE,
  450. LOC_REFERENCE:
  451. begin
  452. if not is_object(left.resultdef) then
  453. begin
  454. location.registerhi:=hlcg.getaddressregister(current_asmdata.CurrAsmList,left.resultdef);
  455. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location.reference,location.registerhi)
  456. end
  457. else
  458. begin
  459. location.registerhi:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  460. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,voidpointertype,left.location.reference,location.registerhi);
  461. end;
  462. location_freetemp(current_asmdata.CurrAsmList,left.location);
  463. end;
  464. else
  465. internalerror(200610311);
  466. end;
  467. { virtual method ? }
  468. if (po_virtualmethod in procdef.procoptions) and
  469. not(loadnf_inherited in loadnodeflags) and
  470. not is_objectpascal_helper(procdef.struct) then
  471. begin
  472. if (not assigned(current_procinfo) or
  473. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  474. tobjectdef(procdef.struct).register_vmt_call(procdef.extnumber);
  475. {$ifdef vtentry}
  476. if not is_interface(procdef.struct) then
  477. begin
  478. inc(current_asmdata.NextVTEntryNr);
  479. current_asmdata.CurrAsmList.Concat(tai_symbol.CreateName('VTREF'+tostr(current_asmdata.NextVTEntryNr)+'_'+procdef._class.vmt_mangledname+'$$'+tostr(vmtoffset div sizeof(pint)),AT_FUNCTION,0));
  480. end;
  481. {$endif vtentry}
  482. { a classrefdef already points to the VMT }
  483. if (left.resultdef.typ<>classrefdef) then
  484. begin
  485. { load vmt pointer }
  486. hlcg.reference_reset_base(href,voidpointertype,location.registerhi,tobjectdef(left.resultdef).vmt_offset,voidpointertype.alignment);
  487. hregister:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  488. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,href,hregister);
  489. end
  490. else
  491. hregister:=location.registerhi;
  492. { load method address }
  493. hlcg.reference_reset_base(href,voidpointertype,hregister,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),voidpointertype.alignment);
  494. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdef.address_type);
  495. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,procdef.address_type,procdef.address_type,href,location.register);
  496. end
  497. else
  498. begin
  499. { load address of the function }
  500. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0,procdef.address_type.alignment);
  501. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdef.address_type);
  502. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,procdef,procdef.address_type,href,location.register);
  503. end;
  504. { to get methodpointers stored correctly, code and self register must be swapped on
  505. big endian targets }
  506. if target_info.endian=endian_big then
  507. begin
  508. hregister:=location.register;
  509. location.register:=location.registerhi;
  510. location.registerhi:=hregister;
  511. end;
  512. end
  513. else
  514. begin
  515. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  516. { def_cgsize does not work for tprocdef, so we use pd.address_type }
  517. location.size:=def_cgsize(pd.address_type);
  518. if not(po_weakexternal in pd.procoptions) then
  519. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname)
  520. else
  521. location.reference.symbol:=current_asmdata.WeakRefAsmSymbol(procdef.mangledname);
  522. end;
  523. end;
  524. labelsym :
  525. if assigned(tlabelsym(symtableentry).asmblocklabel) then
  526. location.reference.symbol:=tlabelsym(symtableentry).asmblocklabel
  527. else
  528. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  529. else internalerror(200510032);
  530. end;
  531. end;
  532. {*****************************************************************************
  533. SecondAssignment
  534. *****************************************************************************}
  535. procedure tcgassignmentnode.pass_generate_code;
  536. var
  537. otlabel,hlabel,oflabel : tasmlabel;
  538. href : treference;
  539. releaseright : boolean;
  540. alignmentrequirement,
  541. len : aint;
  542. r : tregister;
  543. r64 : tregister64;
  544. oldflowcontrol : tflowcontrol;
  545. begin
  546. { previously, managed types were handled in firstpass
  547. newer FPCs however can identify situations when
  548. assignments of managed types require no special code and the
  549. value could be just copied so this could should be able also to handle
  550. managed types without any special "managing code"}
  551. location_reset(location,LOC_VOID,OS_NO);
  552. otlabel:=current_procinfo.CurrTrueLabel;
  553. oflabel:=current_procinfo.CurrFalseLabel;
  554. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  555. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  556. {
  557. in most cases we can process first the right node which contains
  558. the most complex code. Exceptions for this are:
  559. - result is in flags, loading left will then destroy the flags
  560. - result is a jump, loading left must be already done before the jump is made
  561. - result need reference count, when left points to a value used in
  562. right then decreasing the refcnt on left can possibly release
  563. the memory before right increased the refcnt, result is that an
  564. empty value is assigned
  565. But not when the result is in the flags, then
  566. loading the left node afterwards can destroy the flags.
  567. }
  568. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  569. (node_complexity(right)>node_complexity(left)) then
  570. begin
  571. secondpass(right);
  572. if codegenerror then
  573. exit;
  574. secondpass(left);
  575. if codegenerror then
  576. exit;
  577. end
  578. else
  579. begin
  580. { calculate left sides }
  581. secondpass(left);
  582. if codegenerror then
  583. exit;
  584. { tell the SSA/SSL code that the left side was handled first so
  585. ni SSL is done
  586. }
  587. oldflowcontrol:=flowcontrol;
  588. include(flowcontrol,fc_lefthandled);
  589. secondpass(right);
  590. flowcontrol:=oldflowcontrol;
  591. if codegenerror then
  592. exit;
  593. end;
  594. releaseright:=true;
  595. { shortstring assignments are handled separately }
  596. if is_shortstring(left.resultdef) then
  597. begin
  598. {
  599. we can get here only in the following situations
  600. for the right node:
  601. - empty constant string
  602. - char
  603. }
  604. { The addn is replaced by a blockn or calln that already returns
  605. a shortstring }
  606. if is_shortstring(right.resultdef) and
  607. (right.nodetype in [blockn,calln]) then
  608. begin
  609. { nothing to do }
  610. end
  611. { empty constant string }
  612. else if (right.nodetype=stringconstn) and
  613. (tstringconstnode(right).len=0) then
  614. begin
  615. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u8inttype,0,left.location.reference);
  616. end
  617. { char loading }
  618. else if is_char(right.resultdef) then
  619. begin
  620. if right.nodetype=ordconstn then
  621. begin
  622. if (target_info.endian = endian_little) then
  623. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u16inttype,(tordconstnode(right).value.svalue shl 8) or 1,
  624. setalignment(left.location.reference,1))
  625. else
  626. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u16inttype,tordconstnode(right).value.svalue or (1 shl 8),
  627. setalignment(left.location.reference,1));
  628. end
  629. else
  630. begin
  631. href:=left.location.reference;
  632. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u8inttype,1,href);
  633. inc(href.offset,1);
  634. case right.location.loc of
  635. LOC_REGISTER,
  636. LOC_CREGISTER :
  637. begin
  638. {$ifndef cpuhighleveltarget}
  639. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  640. {$else not cpuhighleveltarget}
  641. r:=hlcg.getintregister(current_asmdata.CurrAsmList,u8inttype);
  642. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,u8inttype,u8inttype,right.location.register,r);
  643. {$endif cpuhighleveltarget}
  644. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,u8inttype,u8inttype,r,href);
  645. end;
  646. LOC_REFERENCE,
  647. LOC_CREFERENCE :
  648. hlcg.a_load_ref_ref(current_asmdata.CurrAsmList,u8inttype,u8inttype,right.location.reference,href);
  649. else
  650. internalerror(200205111);
  651. end;
  652. end;
  653. end
  654. else
  655. internalerror(2002042410);
  656. end
  657. { try to reuse memory locations instead of copying }
  658. { copy to a memory location ... }
  659. else if (right.location.loc = LOC_REFERENCE) and
  660. maybechangetemp(current_asmdata.CurrAsmList,left,right.location.reference) then
  661. begin
  662. { if it worked, we're done }
  663. end
  664. else
  665. begin
  666. { SSA support }
  667. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,left,false);
  668. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,right,true);
  669. case right.location.loc of
  670. LOC_CONSTANT :
  671. begin
  672. {$ifndef cpu64bitalu}
  673. if (left.location.size in [OS_64,OS_S64]) or (right.location.size in [OS_64,OS_S64]) then
  674. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  675. else
  676. {$endif not cpu64bitalu}
  677. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,right.location.value,left.location);
  678. end;
  679. LOC_REFERENCE,
  680. LOC_CREFERENCE :
  681. begin
  682. case left.location.loc of
  683. LOC_REGISTER,
  684. LOC_CREGISTER :
  685. begin
  686. {$ifdef cpu64bitalu}
  687. if left.location.size in [OS_128,OS_S128] then
  688. cg128.a_load128_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register128)
  689. else
  690. {$else cpu64bitalu}
  691. if left.location.size in [OS_64,OS_S64] then
  692. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  693. else
  694. {$endif cpu64bitalu}
  695. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.reference,left.location.register);
  696. end;
  697. LOC_FPUREGISTER,
  698. LOC_CFPUREGISTER :
  699. begin
  700. hlcg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  701. right.resultdef,left.resultdef,
  702. right.location.reference,
  703. left.location.register);
  704. end;
  705. LOC_REFERENCE,
  706. LOC_CREFERENCE :
  707. begin
  708. if (left.resultdef.typ=floatdef) and
  709. (right.resultdef.typ=floatdef) and
  710. (left.location.size<>right.location.size) then
  711. begin
  712. hlcg.a_loadfpu_ref_ref(current_asmdata.CurrAsmList,
  713. right.resultdef,left.resultdef,
  714. right.location.reference,left.location.reference)
  715. end
  716. else
  717. begin
  718. { TODO: HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  719. { Use unaligned copy when the offset is not aligned }
  720. len:=left.resultdef.size;
  721. { can be 0 in case of formaldef on JVM target }
  722. if len=0 then
  723. len:=sizeof(pint);
  724. { data smaller than an aint has less alignment requirements }
  725. { max(1,...) avoids div by zero in case of an empty record }
  726. alignmentrequirement:=min(max(1,len),sizeof(aint));
  727. if (right.location.reference.offset mod alignmentrequirement<>0) or
  728. (left.location.reference.offset mod alignmentrequirement<>0) or
  729. (right.resultdef.alignment<alignmentrequirement) or
  730. ((right.location.reference.alignment<>0) and
  731. (right.location.reference.alignment<alignmentrequirement)) or
  732. ((left.location.reference.alignment<>0) and
  733. (left.location.reference.alignment<alignmentrequirement)) then
  734. hlcg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,left.resultdef,right.location.reference,left.location.reference)
  735. else
  736. hlcg.g_concatcopy(current_asmdata.CurrAsmList,left.resultdef,right.location.reference,left.location.reference);
  737. end;
  738. end;
  739. LOC_MMREGISTER,
  740. LOC_CMMREGISTER:
  741. begin
  742. {$ifdef x86}
  743. if (right.resultdef.typ=floatdef) and
  744. not use_vectorfpu(right.resultdef) then
  745. begin
  746. { perform size conversion if needed (the mm-code cannot }
  747. { convert an extended into a double/single, since sse }
  748. { doesn't support extended) }
  749. r:=cg.getfpuregister(current_asmdata.CurrAsmList,right.location.size);
  750. tg.gethltemp(current_asmdata.CurrAsmList,left.resultdef,left.resultdef.size,tt_normal,href);
  751. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,right.location.size,right.location.size,right.location.reference,r);
  752. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,r,href);
  753. if releaseright then
  754. location_freetemp(current_asmdata.CurrAsmList,right.location);
  755. releaseright:=true;
  756. location_reset_ref(right.location,LOC_REFERENCE,left.location.size,0);
  757. right.location.reference:=href;
  758. right.resultdef:=left.resultdef;
  759. end;
  760. {$endif}
  761. hlcg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  762. right.resultdef,
  763. left.resultdef,
  764. right.location.reference,
  765. left.location.register,mms_movescalar);
  766. end;
  767. LOC_SUBSETREG,
  768. LOC_CSUBSETREG:
  769. hlcg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.reference,left.location.sreg);
  770. LOC_SUBSETREF,
  771. LOC_CSUBSETREF:
  772. {$ifndef cpu64bitalu}
  773. if right.location.size in [OS_64,OS_S64] then
  774. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  775. else
  776. {$endif not cpu64bitalu}
  777. hlcg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.reference,left.location.sref);
  778. else
  779. internalerror(200203284);
  780. end;
  781. end;
  782. {$ifdef SUPPORT_MMX}
  783. LOC_CMMXREGISTER,
  784. LOC_MMXREGISTER:
  785. begin
  786. if left.location.loc=LOC_CMMXREGISTER then
  787. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  788. else
  789. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  790. end;
  791. {$endif SUPPORT_MMX}
  792. LOC_MMREGISTER,
  793. LOC_CMMREGISTER:
  794. begin
  795. if left.resultdef.typ=arraydef then
  796. begin
  797. end
  798. else
  799. begin
  800. case left.location.loc of
  801. LOC_CMMREGISTER,
  802. LOC_MMREGISTER:
  803. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.register,left.location.register,mms_movescalar);
  804. LOC_REFERENCE,
  805. LOC_CREFERENCE:
  806. hlcg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.register,left.location.reference,mms_movescalar);
  807. else
  808. internalerror(2009112601);
  809. end;
  810. end;
  811. end;
  812. LOC_REGISTER,
  813. LOC_CREGISTER :
  814. begin
  815. {$ifdef cpu64bitalu}
  816. if left.location.size in [OS_128,OS_S128] then
  817. cg128.a_load128_reg_loc(current_asmdata.CurrAsmList,
  818. right.location.register128,left.location)
  819. else
  820. {$else cpu64bitalu}
  821. { also OS_F64 in case of mmreg -> intreg }
  822. if left.location.size in [OS_64,OS_S64,OS_F64] then
  823. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  824. right.location.register64,left.location)
  825. else
  826. {$endif cpu64bitalu}
  827. {$ifdef i8086}
  828. { prefer a_load_loc_ref, because it supports i8086-specific types
  829. that use registerhi (like 6-byte method pointers)
  830. (todo: maybe we should add a_load_loc_loc?) }
  831. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  832. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location,left.location.reference)
  833. else
  834. {$endif i8086}
  835. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.register,left.location);
  836. end;
  837. LOC_FPUREGISTER,
  838. LOC_CFPUREGISTER :
  839. begin
  840. { we can't do direct moves between fpu and mm registers }
  841. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  842. begin
  843. {$ifdef x86}
  844. if not use_vectorfpu(right.resultdef) then
  845. begin
  846. { perform size conversion if needed (the mm-code cannot convert an }
  847. { extended into a double/single, since sse doesn't support extended) }
  848. tg.gethltemp(current_asmdata.CurrAsmList,left.resultdef,left.resultdef.size,tt_normal,href);
  849. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,href);
  850. location_reset_ref(right.location,LOC_REFERENCE,left.location.size,0);
  851. right.location.reference:=href;
  852. right.resultdef:=left.resultdef;
  853. end;
  854. {$endif}
  855. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,false);
  856. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  857. right.resultdef,left.resultdef,
  858. right.location.register,left.location.register,mms_movescalar);
  859. end
  860. else
  861. hlcg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  862. right.resultdef,left.resultdef,
  863. right.location.register,left.location);
  864. end;
  865. LOC_SUBSETREG,
  866. LOC_CSUBSETREG:
  867. begin
  868. hlcg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  869. right.resultdef,left.resultdef,right.location.sreg,left.location);
  870. end;
  871. LOC_SUBSETREF,
  872. LOC_CSUBSETREF:
  873. begin
  874. {$ifndef cpu64bitalu}
  875. if right.location.size in [OS_64,OS_S64] then
  876. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  877. else
  878. {$endif not cpu64bitalu}
  879. hlcg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  880. right.resultdef,left.resultdef,right.location.sref,left.location);
  881. end;
  882. LOC_JUMP :
  883. begin
  884. current_asmdata.getjumplabel(hlabel);
  885. hlcg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  886. if is_pasbool(left.resultdef) then
  887. begin
  888. {$ifndef cpu64bitalu}
  889. if left.location.size in [OS_64,OS_S64] then
  890. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,1,left.location)
  891. else
  892. {$endif not cpu64bitalu}
  893. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,1,left.location)
  894. end
  895. else
  896. begin
  897. {$ifndef cpu64bitalu}
  898. if left.location.size in [OS_64,OS_S64] then
  899. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,-1,left.location)
  900. else
  901. {$endif not cpu64bitalu}
  902. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,-1,left.location);
  903. end;
  904. hlcg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  905. hlcg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  906. {$ifndef cpu64bitalu}
  907. if left.location.size in [OS_64,OS_S64] then
  908. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,0,left.location)
  909. else
  910. {$endif not cpu64bitalu}
  911. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,0,left.location);
  912. hlcg.a_label(current_asmdata.CurrAsmList,hlabel);
  913. end;
  914. {$ifdef cpuflags}
  915. LOC_FLAGS :
  916. begin
  917. if is_pasbool(left.resultdef) then
  918. begin
  919. case left.location.loc of
  920. LOC_REGISTER,LOC_CREGISTER:
  921. {$ifndef cpu64bitalu}
  922. if left.location.size in [OS_S64,OS_64] then
  923. begin
  924. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,left.location.register64.reglo);
  925. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  926. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,left.location.register64.reghi);
  927. end
  928. else
  929. {$endif not cpu64bitalu}
  930. begin
  931. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,left.location.register);
  932. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  933. end;
  934. LOC_REFERENCE:
  935. { i8086 and i386 have hacks in their code generators so that they can
  936. deal with 64 bit locations in this parcticular case }
  937. {$if not defined(cpu64bitalu) and not defined(x86)}
  938. if left.location.size in [OS_S64,OS_64] then
  939. begin
  940. r64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  941. r64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  942. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,r64.reglo);
  943. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  944. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  945. cg64.a_load64_reg_ref(current_asmdata.CurrAsmList,r64,left.location.reference);
  946. end
  947. else
  948. {$endif not cpu64bitalu}
  949. begin
  950. cg.g_flags2ref(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,left.location.reference);
  951. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  952. end;
  953. LOC_SUBSETREG,LOC_SUBSETREF:
  954. begin
  955. r:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  956. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,r);
  957. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  958. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,r,left.location);
  959. end;
  960. else
  961. internalerror(200203273);
  962. end;
  963. end
  964. else
  965. begin
  966. {$ifndef cpu64bitalu}
  967. if left.location.size in [OS_S64,OS_64] then
  968. begin
  969. r64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  970. r64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  971. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,r64.reglo);
  972. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  973. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  974. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_S64,
  975. r64,r64);
  976. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,r64,left.location);
  977. end
  978. else
  979. {$endif not cpu64bitalu}
  980. begin
  981. r:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  982. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,r);
  983. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  984. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,left.location.size,r,r);
  985. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,r,left.location);
  986. end
  987. end;
  988. end;
  989. {$endif cpuflags}
  990. end;
  991. end;
  992. if releaseright then
  993. location_freetemp(current_asmdata.CurrAsmList,right.location);
  994. current_procinfo.CurrTrueLabel:=otlabel;
  995. current_procinfo.CurrFalseLabel:=oflabel;
  996. end;
  997. {*****************************************************************************
  998. SecondArrayConstruct
  999. *****************************************************************************}
  1000. const
  1001. vtInteger = 0;
  1002. vtBoolean = 1;
  1003. vtChar = 2;
  1004. vtExtended = 3;
  1005. vtString = 4;
  1006. vtPointer = 5;
  1007. vtPChar = 6;
  1008. vtObject = 7;
  1009. vtClass = 8;
  1010. vtWideChar = 9;
  1011. vtPWideChar = 10;
  1012. vtAnsiString32 = 11;
  1013. vtCurrency = 12;
  1014. vtVariant = 13;
  1015. vtInterface = 14;
  1016. vtWideString = 15;
  1017. vtInt64 = 16;
  1018. vtQWord = 17;
  1019. vtUnicodeString = 18;
  1020. vtAnsiString16 = 19;
  1021. vtAnsiString64 = 20;
  1022. procedure tcgarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  1023. begin
  1024. { do nothing by default }
  1025. end;
  1026. procedure tcgarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  1027. begin
  1028. inc(ref.offset,elesize);
  1029. end;
  1030. procedure tcgarrayconstructornode.pass_generate_code;
  1031. var
  1032. hp : tarrayconstructornode;
  1033. href : treference;
  1034. lt : tdef;
  1035. paraloc : tcgparalocation;
  1036. otlabel,
  1037. oflabel : tasmlabel;
  1038. vtype : longint;
  1039. eledef: tdef;
  1040. elesize : longint;
  1041. tmpreg : tregister;
  1042. vaddr : boolean;
  1043. freetemp,
  1044. dovariant: boolean;
  1045. begin
  1046. otlabel:=nil;
  1047. oflabel:=nil;
  1048. if is_packed_array(resultdef) then
  1049. internalerror(200608042);
  1050. dovariant:=
  1051. ((nf_forcevaria in flags) or is_variant_array(resultdef)) and
  1052. not(target_info.system in systems_managed_vm);
  1053. if dovariant then
  1054. begin
  1055. eledef:=search_system_type('TVARREC').typedef;
  1056. elesize:=eledef.size;
  1057. end
  1058. else
  1059. begin
  1060. eledef:=tarraydef(resultdef).elementdef;
  1061. elesize:=tarraydef(resultdef).elesize;
  1062. end;
  1063. { alignment is filled in by tg.gethltemp below }
  1064. location_reset_ref(location,LOC_CREFERENCE,OS_NO,0);
  1065. fillchar(paraloc,sizeof(paraloc),0);
  1066. { Allocate always a temp, also if no elements are required, to
  1067. be sure that location is valid (PFV) }
  1068. { on the JVM platform, an array can have 0 elements; since the length
  1069. of the array is part of the array itself, make sure we allocate one
  1070. of the proper length to avoid getting unexpected results later --
  1071. allocating a temp of size 0 also forces it to be size 4 on regular
  1072. targets }
  1073. if tarraydef(resultdef).highrange=-1 then
  1074. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,0,tt_normal,location.reference)
  1075. else
  1076. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  1077. href:=location.reference;
  1078. makearrayref(href,eledef);
  1079. { Process nodes in array constructor }
  1080. hp:=self;
  1081. while assigned(hp) do
  1082. begin
  1083. if assigned(hp.left) then
  1084. begin
  1085. freetemp:=true;
  1086. if (hp.left.expectloc=LOC_JUMP) then
  1087. begin
  1088. otlabel:=current_procinfo.CurrTrueLabel;
  1089. oflabel:=current_procinfo.CurrFalseLabel;
  1090. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  1091. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  1092. end;
  1093. secondpass(hp.left);
  1094. { Move flags and jump in register }
  1095. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  1096. hlcg.location_force_reg(current_asmdata.CurrAsmList,hp.left.location,hp.left.resultdef,hp.left.resultdef,false);
  1097. if (hp.left.location.loc=LOC_JUMP) then
  1098. begin
  1099. if (hp.left.expectloc<>LOC_JUMP) then
  1100. internalerror(2007103101);
  1101. current_procinfo.CurrTrueLabel:=otlabel;
  1102. current_procinfo.CurrFalseLabel:=oflabel;
  1103. end;
  1104. if dovariant then
  1105. begin
  1106. { find the correct vtype value }
  1107. vtype:=$ff;
  1108. vaddr:=false;
  1109. lt:=hp.left.resultdef;
  1110. case lt.typ of
  1111. enumdef,
  1112. orddef :
  1113. begin
  1114. if is_64bit(lt) then
  1115. begin
  1116. case torddef(lt).ordtype of
  1117. scurrency:
  1118. vtype:=vtCurrency;
  1119. s64bit:
  1120. vtype:=vtInt64;
  1121. u64bit:
  1122. vtype:=vtQWord;
  1123. end;
  1124. freetemp:=false;
  1125. vaddr:=true;
  1126. end
  1127. else if (lt.typ=enumdef) or
  1128. is_integer(lt) then
  1129. vtype:=vtInteger
  1130. else
  1131. if is_boolean(lt) then
  1132. vtype:=vtBoolean
  1133. else
  1134. if (lt.typ=orddef) then
  1135. begin
  1136. case torddef(lt).ordtype of
  1137. uchar:
  1138. vtype:=vtChar;
  1139. uwidechar:
  1140. vtype:=vtWideChar;
  1141. end;
  1142. end;
  1143. end;
  1144. floatdef :
  1145. begin
  1146. if is_currency(lt) then
  1147. vtype:=vtCurrency
  1148. else
  1149. vtype:=vtExtended;
  1150. freetemp:=false;
  1151. vaddr:=true;
  1152. end;
  1153. procvardef,
  1154. pointerdef :
  1155. begin
  1156. if is_pchar(lt) then
  1157. vtype:=vtPChar
  1158. else if is_pwidechar(lt) then
  1159. vtype:=vtPWideChar
  1160. else
  1161. vtype:=vtPointer;
  1162. end;
  1163. variantdef :
  1164. begin
  1165. vtype:=vtVariant;
  1166. vaddr:=true;
  1167. freetemp:=false;
  1168. end;
  1169. classrefdef :
  1170. vtype:=vtClass;
  1171. objectdef :
  1172. if is_interface(lt) then
  1173. vtype:=vtInterface
  1174. { vtObject really means a class based on TObject }
  1175. else if is_class(lt) then
  1176. vtype:=vtObject
  1177. else
  1178. internalerror(200505171);
  1179. stringdef :
  1180. begin
  1181. if is_shortstring(lt) then
  1182. begin
  1183. vtype:=vtString;
  1184. vaddr:=true;
  1185. freetemp:=false;
  1186. end
  1187. else
  1188. if is_ansistring(lt) then
  1189. begin
  1190. vtype:=vtAnsiString;
  1191. freetemp:=false;
  1192. end
  1193. else
  1194. if is_widestring(lt) then
  1195. begin
  1196. vtype:=vtWideString;
  1197. freetemp:=false;
  1198. end
  1199. else
  1200. if is_unicodestring(lt) then
  1201. begin
  1202. vtype:=vtUnicodeString;
  1203. freetemp:=false;
  1204. end;
  1205. end;
  1206. end;
  1207. if vtype=$ff then
  1208. internalerror(14357);
  1209. { write changing field update href to the next element }
  1210. inc(href.offset,sizeof(pint));
  1211. if vaddr then
  1212. begin
  1213. hlcg.location_force_mem(current_asmdata.CurrAsmList,hp.left.location,hp.left.resultdef);
  1214. tmpreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  1215. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.resultdef,voidpointertype,hp.left.location.reference,tmpreg);
  1216. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,tmpreg,href);
  1217. end
  1218. else
  1219. { todo: proper type information for hlcg }
  1220. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.resultdef,{$ifdef cpu16bitaddr}u32inttype{$else}voidpointertype{$endif},hp.left.location,href);
  1221. { update href to the vtype field and write it }
  1222. dec(href.offset,sizeof(pint));
  1223. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  1224. { goto next array element }
  1225. advancearrayoffset(href,elesize);
  1226. end
  1227. else
  1228. { normal array constructor of the same type }
  1229. begin
  1230. if is_managed_type(resultdef) then
  1231. freetemp:=false;
  1232. case hp.left.location.loc of
  1233. LOC_MMREGISTER,
  1234. LOC_CMMREGISTER:
  1235. hlcg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.resultdef,
  1236. hp.left.location.register,href,mms_movescalar);
  1237. LOC_FPUREGISTER,
  1238. LOC_CFPUREGISTER :
  1239. hlcg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.resultdef,hp.left.location.register,href);
  1240. LOC_REFERENCE,
  1241. LOC_CREFERENCE :
  1242. begin
  1243. if is_shortstring(hp.left.resultdef) then
  1244. hlcg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  1245. Tstringdef(hp.left.resultdef))
  1246. else
  1247. hlcg.g_concatcopy(current_asmdata.CurrAsmList,eledef,hp.left.location.reference,href);
  1248. end;
  1249. else
  1250. begin
  1251. {$ifdef cpu64bitalu}
  1252. if hp.left.location.size in [OS_128,OS_S128] then
  1253. cg128.a_load128_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  1254. else
  1255. {$else cpu64bitalu}
  1256. if hp.left.location.size in [OS_64,OS_S64] then
  1257. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  1258. else
  1259. {$endif cpu64bitalu}
  1260. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,eledef,eledef,hp.left.location,href);
  1261. end;
  1262. end;
  1263. advancearrayoffset(href,elesize);
  1264. end;
  1265. if freetemp then
  1266. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  1267. end;
  1268. { load next entry }
  1269. hp:=tarrayconstructornode(hp.right);
  1270. end;
  1271. end;
  1272. {*****************************************************************************
  1273. SecondRTTI
  1274. *****************************************************************************}
  1275. procedure tcgrttinode.pass_generate_code;
  1276. begin
  1277. location_reset_ref(location,LOC_CREFERENCE,OS_NO,sizeof(pint));
  1278. case rttidatatype of
  1279. rdt_normal:
  1280. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  1281. rdt_ord2str:
  1282. location.reference.symbol:=RTTIWriter.get_rtti_label_ord2str(rttidef,rttitype);
  1283. rdt_str2ord:
  1284. location.reference.symbol:=RTTIWriter.get_rtti_label_str2ord(rttidef,rttitype);
  1285. end;
  1286. end;
  1287. begin
  1288. cloadnode:=tcgloadnode;
  1289. cassignmentnode:=tcgassignmentnode;
  1290. carrayconstructornode:=tcgarrayconstructornode;
  1291. crttinode:=tcgrttinode;
  1292. end.