ncgld.pas 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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 is_implicit_pointer_object_type(left.resultdef) or
  453. (left.resultdef.typ=classrefdef) then
  454. begin
  455. location.registerhi:=hlcg.getaddressregister(current_asmdata.CurrAsmList,left.resultdef);
  456. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location.reference,location.registerhi)
  457. end
  458. else
  459. begin
  460. location.registerhi:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  461. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,voidpointertype,left.location.reference,location.registerhi);
  462. end;
  463. location_freetemp(current_asmdata.CurrAsmList,left.location);
  464. end;
  465. else
  466. internalerror(200610311);
  467. end;
  468. { virtual method ? }
  469. if (po_virtualmethod in procdef.procoptions) and
  470. not(loadnf_inherited in loadnodeflags) and
  471. not is_objectpascal_helper(procdef.struct) then
  472. begin
  473. if (not assigned(current_procinfo) or
  474. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  475. tobjectdef(procdef.struct).register_vmt_call(procdef.extnumber);
  476. {$ifdef vtentry}
  477. if not is_interface(procdef.struct) then
  478. begin
  479. inc(current_asmdata.NextVTEntryNr);
  480. current_asmdata.CurrAsmList.Concat(tai_symbol.CreateName('VTREF'+tostr(current_asmdata.NextVTEntryNr)+'_'+procdef._class.vmt_mangledname+'$$'+tostr(vmtoffset div sizeof(pint)),AT_FUNCTION,0));
  481. end;
  482. {$endif vtentry}
  483. { a classrefdef already points to the VMT }
  484. if (left.resultdef.typ<>classrefdef) then
  485. begin
  486. { load vmt pointer }
  487. hlcg.reference_reset_base(href,voidpointertype,location.registerhi,tobjectdef(left.resultdef).vmt_offset,voidpointertype.alignment);
  488. hregister:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  489. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,href,hregister);
  490. end
  491. else
  492. hregister:=location.registerhi;
  493. { load method address }
  494. hlcg.reference_reset_base(href,voidpointertype,hregister,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),voidpointertype.alignment);
  495. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdef.address_type);
  496. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,procdef.address_type,procdef.address_type,href,location.register);
  497. end
  498. else
  499. begin
  500. { load address of the function }
  501. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0,procdef.address_type.alignment);
  502. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdef.address_type);
  503. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,procdef,procdef.address_type,href,location.register);
  504. end;
  505. { to get methodpointers stored correctly, code and self register must be swapped on
  506. big endian targets }
  507. if target_info.endian=endian_big then
  508. begin
  509. hregister:=location.register;
  510. location.register:=location.registerhi;
  511. location.registerhi:=hregister;
  512. end;
  513. end
  514. else
  515. begin
  516. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  517. { def_cgsize does not work for tprocdef, so we use pd.address_type }
  518. location.size:=def_cgsize(pd.address_type);
  519. if not(po_weakexternal in pd.procoptions) then
  520. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname)
  521. else
  522. location.reference.symbol:=current_asmdata.WeakRefAsmSymbol(procdef.mangledname);
  523. end;
  524. end;
  525. labelsym :
  526. if assigned(tlabelsym(symtableentry).asmblocklabel) then
  527. location.reference.symbol:=tlabelsym(symtableentry).asmblocklabel
  528. else
  529. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  530. else internalerror(200510032);
  531. end;
  532. end;
  533. {*****************************************************************************
  534. SecondAssignment
  535. *****************************************************************************}
  536. procedure tcgassignmentnode.pass_generate_code;
  537. var
  538. otlabel,hlabel,oflabel : tasmlabel;
  539. href : treference;
  540. releaseright : boolean;
  541. alignmentrequirement,
  542. len : aint;
  543. r : tregister;
  544. r64 : tregister64;
  545. oldflowcontrol : tflowcontrol;
  546. begin
  547. { previously, managed types were handled in firstpass
  548. newer FPCs however can identify situations when
  549. assignments of managed types require no special code and the
  550. value could be just copied so this could should be able also to handle
  551. managed types without any special "managing code"}
  552. location_reset(location,LOC_VOID,OS_NO);
  553. otlabel:=current_procinfo.CurrTrueLabel;
  554. oflabel:=current_procinfo.CurrFalseLabel;
  555. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  556. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  557. {
  558. in most cases we can process first the right node which contains
  559. the most complex code. Exceptions for this are:
  560. - result is in flags, loading left will then destroy the flags
  561. - result is a jump, loading left must be already done before the jump is made
  562. - result need reference count, when left points to a value used in
  563. right then decreasing the refcnt on left can possibly release
  564. the memory before right increased the refcnt, result is that an
  565. empty value is assigned
  566. But not when the result is in the flags, then
  567. loading the left node afterwards can destroy the flags.
  568. }
  569. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  570. (node_complexity(right)>node_complexity(left)) then
  571. begin
  572. secondpass(right);
  573. if codegenerror then
  574. exit;
  575. secondpass(left);
  576. if codegenerror then
  577. exit;
  578. end
  579. else
  580. begin
  581. { calculate left sides }
  582. secondpass(left);
  583. if codegenerror then
  584. exit;
  585. { tell the SSA/SSL code that the left side was handled first so
  586. ni SSL is done
  587. }
  588. oldflowcontrol:=flowcontrol;
  589. include(flowcontrol,fc_lefthandled);
  590. secondpass(right);
  591. flowcontrol:=oldflowcontrol;
  592. if codegenerror then
  593. exit;
  594. end;
  595. releaseright:=true;
  596. { shortstring assignments are handled separately }
  597. if is_shortstring(left.resultdef) then
  598. begin
  599. {
  600. we can get here only in the following situations
  601. for the right node:
  602. - empty constant string
  603. - char
  604. }
  605. { The addn is replaced by a blockn or calln that already returns
  606. a shortstring }
  607. if is_shortstring(right.resultdef) and
  608. (right.nodetype in [blockn,calln]) then
  609. begin
  610. { nothing to do }
  611. end
  612. { empty constant string }
  613. else if (right.nodetype=stringconstn) and
  614. (tstringconstnode(right).len=0) then
  615. begin
  616. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u8inttype,0,left.location.reference);
  617. end
  618. { char loading }
  619. else if is_char(right.resultdef) then
  620. begin
  621. if right.nodetype=ordconstn then
  622. begin
  623. if (target_info.endian = endian_little) then
  624. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u16inttype,(tordconstnode(right).value.svalue shl 8) or 1,
  625. setalignment(left.location.reference,1))
  626. else
  627. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u16inttype,tordconstnode(right).value.svalue or (1 shl 8),
  628. setalignment(left.location.reference,1));
  629. end
  630. else
  631. begin
  632. href:=left.location.reference;
  633. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,u8inttype,1,href);
  634. inc(href.offset,1);
  635. case right.location.loc of
  636. LOC_REGISTER,
  637. LOC_CREGISTER :
  638. begin
  639. {$ifndef cpuhighleveltarget}
  640. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  641. {$else not cpuhighleveltarget}
  642. r:=hlcg.getintregister(current_asmdata.CurrAsmList,u8inttype);
  643. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,u8inttype,u8inttype,right.location.register,r);
  644. {$endif cpuhighleveltarget}
  645. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,u8inttype,u8inttype,r,href);
  646. end;
  647. LOC_REFERENCE,
  648. LOC_CREFERENCE :
  649. hlcg.a_load_ref_ref(current_asmdata.CurrAsmList,u8inttype,u8inttype,right.location.reference,href);
  650. else
  651. internalerror(200205111);
  652. end;
  653. end;
  654. end
  655. else
  656. internalerror(2002042410);
  657. end
  658. { try to reuse memory locations instead of copying }
  659. { copy to a memory location ... }
  660. else if (right.location.loc = LOC_REFERENCE) and
  661. maybechangetemp(current_asmdata.CurrAsmList,left,right.location.reference) then
  662. begin
  663. { if it worked, we're done }
  664. end
  665. else
  666. begin
  667. { SSA support }
  668. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,left,false);
  669. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,right,true);
  670. case right.location.loc of
  671. LOC_CONSTANT :
  672. begin
  673. {$ifndef cpu64bitalu}
  674. if (left.location.size in [OS_64,OS_S64]) or (right.location.size in [OS_64,OS_S64]) then
  675. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  676. else
  677. {$endif not cpu64bitalu}
  678. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,right.location.value,left.location);
  679. end;
  680. LOC_REFERENCE,
  681. LOC_CREFERENCE :
  682. begin
  683. case left.location.loc of
  684. LOC_REGISTER,
  685. LOC_CREGISTER :
  686. begin
  687. {$ifdef cpu64bitalu}
  688. if left.location.size in [OS_128,OS_S128] then
  689. cg128.a_load128_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register128)
  690. else
  691. {$else cpu64bitalu}
  692. if left.location.size in [OS_64,OS_S64] then
  693. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  694. else
  695. {$endif cpu64bitalu}
  696. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.reference,left.location.register);
  697. end;
  698. LOC_FPUREGISTER,
  699. LOC_CFPUREGISTER :
  700. begin
  701. hlcg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  702. right.resultdef,left.resultdef,
  703. right.location.reference,
  704. left.location.register);
  705. end;
  706. LOC_REFERENCE,
  707. LOC_CREFERENCE :
  708. begin
  709. if (left.resultdef.typ=floatdef) and
  710. (right.resultdef.typ=floatdef) and
  711. (left.location.size<>right.location.size) then
  712. begin
  713. { assume that all float types can be handed by the
  714. fpu if one can be handled by the fpu }
  715. if not use_vectorfpu(left.resultdef) or
  716. not use_vectorfpu(right.resultdef) then
  717. hlcg.a_loadfpu_ref_ref(current_asmdata.CurrAsmList,
  718. right.resultdef,left.resultdef,
  719. right.location.reference,left.location.reference)
  720. else
  721. hlcg.a_loadmm_ref_ref(current_asmdata.CurrAsmList,
  722. right.resultdef,left.resultdef,
  723. right.location.reference,left.location.reference,mms_movescalar)
  724. end
  725. else
  726. begin
  727. { TODO: HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  728. { Use unaligned copy when the offset is not aligned }
  729. len:=left.resultdef.size;
  730. { can be 0 in case of formaldef on JVM target }
  731. if len=0 then
  732. len:=sizeof(pint);
  733. { data smaller than an aint has less alignment requirements }
  734. { max(1,...) avoids div by zero in case of an empty record }
  735. alignmentrequirement:=min(max(1,len),sizeof(aint));
  736. if (right.location.reference.offset mod alignmentrequirement<>0) or
  737. (left.location.reference.offset mod alignmentrequirement<>0) or
  738. (right.resultdef.alignment<alignmentrequirement) or
  739. ((right.location.reference.alignment<>0) and
  740. (right.location.reference.alignment<alignmentrequirement)) or
  741. ((left.location.reference.alignment<>0) and
  742. (left.location.reference.alignment<alignmentrequirement)) then
  743. hlcg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,left.resultdef,right.location.reference,left.location.reference)
  744. else
  745. hlcg.g_concatcopy(current_asmdata.CurrAsmList,left.resultdef,right.location.reference,left.location.reference);
  746. end;
  747. end;
  748. LOC_MMREGISTER,
  749. LOC_CMMREGISTER:
  750. begin
  751. {$ifdef x86}
  752. if (right.resultdef.typ=floatdef) and
  753. not use_vectorfpu(right.resultdef) then
  754. begin
  755. { perform size conversion if needed (the mm-code cannot }
  756. { convert an extended into a double/single, since sse }
  757. { doesn't support extended) }
  758. r:=cg.getfpuregister(current_asmdata.CurrAsmList,right.location.size);
  759. tg.gethltemp(current_asmdata.CurrAsmList,left.resultdef,left.resultdef.size,tt_normal,href);
  760. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,right.location.size,right.location.size,right.location.reference,r);
  761. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,r,href);
  762. if releaseright then
  763. location_freetemp(current_asmdata.CurrAsmList,right.location);
  764. releaseright:=true;
  765. location_reset_ref(right.location,LOC_REFERENCE,left.location.size,0);
  766. right.location.reference:=href;
  767. right.resultdef:=left.resultdef;
  768. end;
  769. {$endif}
  770. hlcg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  771. right.resultdef,
  772. left.resultdef,
  773. right.location.reference,
  774. left.location.register,mms_movescalar);
  775. end;
  776. LOC_SUBSETREG,
  777. LOC_CSUBSETREG:
  778. hlcg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.reference,left.location.sreg);
  779. LOC_SUBSETREF,
  780. LOC_CSUBSETREF:
  781. {$ifndef cpu64bitalu}
  782. if right.location.size in [OS_64,OS_S64] then
  783. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  784. else
  785. {$endif not cpu64bitalu}
  786. hlcg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.reference,left.location.sref);
  787. else
  788. internalerror(200203284);
  789. end;
  790. end;
  791. {$ifdef SUPPORT_MMX}
  792. LOC_CMMXREGISTER,
  793. LOC_MMXREGISTER:
  794. begin
  795. if left.location.loc=LOC_CMMXREGISTER then
  796. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  797. else
  798. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  799. end;
  800. {$endif SUPPORT_MMX}
  801. LOC_MMREGISTER,
  802. LOC_CMMREGISTER:
  803. begin
  804. if left.resultdef.typ=arraydef then
  805. begin
  806. end
  807. else
  808. begin
  809. case left.location.loc of
  810. LOC_CMMREGISTER,
  811. LOC_MMREGISTER:
  812. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.register,left.location.register,mms_movescalar);
  813. LOC_REFERENCE,
  814. LOC_CREFERENCE:
  815. hlcg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.register,left.location.reference,mms_movescalar);
  816. else
  817. internalerror(2009112601);
  818. end;
  819. end;
  820. end;
  821. LOC_REGISTER,
  822. LOC_CREGISTER :
  823. begin
  824. {$ifdef cpu64bitalu}
  825. if left.location.size in [OS_128,OS_S128] then
  826. cg128.a_load128_reg_loc(current_asmdata.CurrAsmList,
  827. right.location.register128,left.location)
  828. else
  829. {$else cpu64bitalu}
  830. { also OS_F64 in case of mmreg -> intreg }
  831. if left.location.size in [OS_64,OS_S64,OS_F64] then
  832. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  833. right.location.register64,left.location)
  834. else
  835. {$endif cpu64bitalu}
  836. {$ifdef i8086}
  837. { prefer a_load_loc_ref, because it supports i8086-specific types
  838. that use registerhi (like 6-byte method pointers)
  839. (todo: maybe we should add a_load_loc_loc?) }
  840. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  841. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location,left.location.reference)
  842. else
  843. {$endif i8086}
  844. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,right.resultdef,left.resultdef,right.location.register,left.location);
  845. end;
  846. LOC_FPUREGISTER,
  847. LOC_CFPUREGISTER :
  848. begin
  849. { we can't do direct moves between fpu and mm registers }
  850. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  851. begin
  852. {$ifdef x86}
  853. if not use_vectorfpu(right.resultdef) then
  854. begin
  855. { perform size conversion if needed (the mm-code cannot convert an }
  856. { extended into a double/single, since sse doesn't support extended) }
  857. tg.gethltemp(current_asmdata.CurrAsmList,left.resultdef,left.resultdef.size,tt_normal,href);
  858. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,href);
  859. location_reset_ref(right.location,LOC_REFERENCE,left.location.size,0);
  860. right.location.reference:=href;
  861. right.resultdef:=left.resultdef;
  862. end;
  863. {$endif}
  864. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,false);
  865. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  866. right.resultdef,left.resultdef,
  867. right.location.register,left.location.register,mms_movescalar);
  868. end
  869. else
  870. hlcg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  871. right.resultdef,left.resultdef,
  872. right.location.register,left.location);
  873. end;
  874. LOC_SUBSETREG,
  875. LOC_CSUBSETREG:
  876. begin
  877. hlcg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  878. right.resultdef,left.resultdef,right.location.sreg,left.location);
  879. end;
  880. LOC_SUBSETREF,
  881. LOC_CSUBSETREF:
  882. begin
  883. {$ifndef cpu64bitalu}
  884. if right.location.size in [OS_64,OS_S64] then
  885. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  886. else
  887. {$endif not cpu64bitalu}
  888. hlcg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  889. right.resultdef,left.resultdef,right.location.sref,left.location);
  890. end;
  891. LOC_JUMP :
  892. begin
  893. current_asmdata.getjumplabel(hlabel);
  894. hlcg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  895. if is_pasbool(left.resultdef) then
  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. else
  905. begin
  906. {$ifndef cpu64bitalu}
  907. if left.location.size in [OS_64,OS_S64] then
  908. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,-1,left.location)
  909. else
  910. {$endif not cpu64bitalu}
  911. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,-1,left.location);
  912. end;
  913. hlcg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  914. hlcg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  915. {$ifndef cpu64bitalu}
  916. if left.location.size in [OS_64,OS_S64] then
  917. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,0,left.location)
  918. else
  919. {$endif not cpu64bitalu}
  920. hlcg.a_load_const_loc(current_asmdata.CurrAsmList,left.resultdef,0,left.location);
  921. hlcg.a_label(current_asmdata.CurrAsmList,hlabel);
  922. end;
  923. {$ifdef cpuflags}
  924. LOC_FLAGS :
  925. begin
  926. if is_pasbool(left.resultdef) then
  927. begin
  928. case left.location.loc of
  929. LOC_REGISTER,LOC_CREGISTER:
  930. {$ifndef cpu64bitalu}
  931. if left.location.size in [OS_S64,OS_64] then
  932. begin
  933. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,left.location.register64.reglo);
  934. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  935. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,left.location.register64.reghi);
  936. end
  937. else
  938. {$endif not cpu64bitalu}
  939. begin
  940. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,left.location.register);
  941. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  942. end;
  943. LOC_REFERENCE:
  944. { i8086 and i386 have hacks in their code generators so that they can
  945. deal with 64 bit locations in this parcticular case }
  946. {$if not defined(cpu64bitalu) and not defined(x86)}
  947. if left.location.size in [OS_S64,OS_64] then
  948. begin
  949. r64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  950. r64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  951. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,r64.reglo);
  952. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  953. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  954. cg64.a_load64_reg_ref(current_asmdata.CurrAsmList,r64,left.location.reference);
  955. end
  956. else
  957. {$endif not cpu64bitalu}
  958. begin
  959. cg.g_flags2ref(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,left.location.reference);
  960. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  961. end;
  962. LOC_SUBSETREG,LOC_SUBSETREF:
  963. begin
  964. r:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  965. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,r);
  966. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  967. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,r,left.location);
  968. end;
  969. else
  970. internalerror(200203273);
  971. end;
  972. end
  973. else
  974. begin
  975. {$ifndef cpu64bitalu}
  976. if left.location.size in [OS_S64,OS_64] then
  977. begin
  978. r64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  979. r64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  980. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,r64.reglo);
  981. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  982. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  983. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_S64,
  984. r64,r64);
  985. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,r64,left.location);
  986. end
  987. else
  988. {$endif not cpu64bitalu}
  989. begin
  990. r:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  991. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,r);
  992. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  993. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,left.location.size,r,r);
  994. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,r,left.location);
  995. end
  996. end;
  997. end;
  998. {$endif cpuflags}
  999. end;
  1000. end;
  1001. if releaseright then
  1002. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1003. current_procinfo.CurrTrueLabel:=otlabel;
  1004. current_procinfo.CurrFalseLabel:=oflabel;
  1005. end;
  1006. {*****************************************************************************
  1007. SecondArrayConstruct
  1008. *****************************************************************************}
  1009. const
  1010. vtInteger = 0;
  1011. vtBoolean = 1;
  1012. vtChar = 2;
  1013. vtExtended = 3;
  1014. vtString = 4;
  1015. vtPointer = 5;
  1016. vtPChar = 6;
  1017. vtObject = 7;
  1018. vtClass = 8;
  1019. vtWideChar = 9;
  1020. vtPWideChar = 10;
  1021. vtAnsiString32 = 11;
  1022. vtCurrency = 12;
  1023. vtVariant = 13;
  1024. vtInterface = 14;
  1025. vtWideString = 15;
  1026. vtInt64 = 16;
  1027. vtQWord = 17;
  1028. vtUnicodeString = 18;
  1029. vtAnsiString16 = 19;
  1030. vtAnsiString64 = 20;
  1031. procedure tcgarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  1032. begin
  1033. { do nothing by default }
  1034. end;
  1035. procedure tcgarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  1036. begin
  1037. inc(ref.offset,elesize);
  1038. end;
  1039. procedure tcgarrayconstructornode.pass_generate_code;
  1040. var
  1041. hp : tarrayconstructornode;
  1042. href : treference;
  1043. lt : tdef;
  1044. paraloc : tcgparalocation;
  1045. otlabel,
  1046. oflabel : tasmlabel;
  1047. vtype : longint;
  1048. eledef: tdef;
  1049. elesize : longint;
  1050. tmpreg : tregister;
  1051. vaddr : boolean;
  1052. freetemp,
  1053. dovariant: boolean;
  1054. begin
  1055. otlabel:=nil;
  1056. oflabel:=nil;
  1057. if is_packed_array(resultdef) then
  1058. internalerror(200608042);
  1059. dovariant:=
  1060. ((nf_forcevaria in flags) or is_variant_array(resultdef)) and
  1061. not(target_info.system in systems_managed_vm);
  1062. if dovariant then
  1063. begin
  1064. eledef:=search_system_type('TVARREC').typedef;
  1065. elesize:=eledef.size;
  1066. end
  1067. else
  1068. begin
  1069. eledef:=tarraydef(resultdef).elementdef;
  1070. elesize:=tarraydef(resultdef).elesize;
  1071. end;
  1072. { alignment is filled in by tg.gethltemp below }
  1073. location_reset_ref(location,LOC_CREFERENCE,OS_NO,0);
  1074. fillchar(paraloc,sizeof(paraloc),0);
  1075. { Allocate always a temp, also if no elements are required, to
  1076. be sure that location is valid (PFV) }
  1077. { on the JVM platform, an array can have 0 elements; since the length
  1078. of the array is part of the array itself, make sure we allocate one
  1079. of the proper length to avoid getting unexpected results later --
  1080. allocating a temp of size 0 also forces it to be size 4 on regular
  1081. targets }
  1082. if tarraydef(resultdef).highrange=-1 then
  1083. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,0,tt_normal,location.reference)
  1084. else
  1085. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  1086. href:=location.reference;
  1087. makearrayref(href,eledef);
  1088. { Process nodes in array constructor }
  1089. hp:=self;
  1090. while assigned(hp) do
  1091. begin
  1092. if assigned(hp.left) then
  1093. begin
  1094. freetemp:=true;
  1095. if (hp.left.expectloc=LOC_JUMP) then
  1096. begin
  1097. otlabel:=current_procinfo.CurrTrueLabel;
  1098. oflabel:=current_procinfo.CurrFalseLabel;
  1099. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  1100. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  1101. end;
  1102. secondpass(hp.left);
  1103. { Move flags and jump in register }
  1104. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  1105. hlcg.location_force_reg(current_asmdata.CurrAsmList,hp.left.location,hp.left.resultdef,hp.left.resultdef,false);
  1106. if (hp.left.location.loc=LOC_JUMP) then
  1107. begin
  1108. if (hp.left.expectloc<>LOC_JUMP) then
  1109. internalerror(2007103101);
  1110. current_procinfo.CurrTrueLabel:=otlabel;
  1111. current_procinfo.CurrFalseLabel:=oflabel;
  1112. end;
  1113. if dovariant then
  1114. begin
  1115. { find the correct vtype value }
  1116. vtype:=$ff;
  1117. vaddr:=false;
  1118. lt:=hp.left.resultdef;
  1119. case lt.typ of
  1120. enumdef,
  1121. orddef :
  1122. begin
  1123. if is_64bit(lt) then
  1124. begin
  1125. case torddef(lt).ordtype of
  1126. scurrency:
  1127. vtype:=vtCurrency;
  1128. s64bit:
  1129. vtype:=vtInt64;
  1130. u64bit:
  1131. vtype:=vtQWord;
  1132. end;
  1133. freetemp:=false;
  1134. vaddr:=true;
  1135. end
  1136. else if (lt.typ=enumdef) or
  1137. is_integer(lt) then
  1138. vtype:=vtInteger
  1139. else
  1140. if is_boolean(lt) then
  1141. vtype:=vtBoolean
  1142. else
  1143. if (lt.typ=orddef) then
  1144. begin
  1145. case torddef(lt).ordtype of
  1146. uchar:
  1147. vtype:=vtChar;
  1148. uwidechar:
  1149. vtype:=vtWideChar;
  1150. end;
  1151. end;
  1152. end;
  1153. floatdef :
  1154. begin
  1155. if is_currency(lt) then
  1156. vtype:=vtCurrency
  1157. else
  1158. vtype:=vtExtended;
  1159. freetemp:=false;
  1160. vaddr:=true;
  1161. end;
  1162. procvardef,
  1163. pointerdef :
  1164. begin
  1165. if is_pchar(lt) then
  1166. vtype:=vtPChar
  1167. else if is_pwidechar(lt) then
  1168. vtype:=vtPWideChar
  1169. else
  1170. vtype:=vtPointer;
  1171. end;
  1172. variantdef :
  1173. begin
  1174. vtype:=vtVariant;
  1175. vaddr:=true;
  1176. freetemp:=false;
  1177. end;
  1178. classrefdef :
  1179. vtype:=vtClass;
  1180. objectdef :
  1181. if is_interface(lt) then
  1182. vtype:=vtInterface
  1183. { vtObject really means a class based on TObject }
  1184. else if is_class(lt) then
  1185. vtype:=vtObject
  1186. else
  1187. internalerror(200505171);
  1188. stringdef :
  1189. begin
  1190. if is_shortstring(lt) then
  1191. begin
  1192. vtype:=vtString;
  1193. vaddr:=true;
  1194. freetemp:=false;
  1195. end
  1196. else
  1197. if is_ansistring(lt) then
  1198. begin
  1199. vtype:=vtAnsiString;
  1200. freetemp:=false;
  1201. end
  1202. else
  1203. if is_widestring(lt) then
  1204. begin
  1205. vtype:=vtWideString;
  1206. freetemp:=false;
  1207. end
  1208. else
  1209. if is_unicodestring(lt) then
  1210. begin
  1211. vtype:=vtUnicodeString;
  1212. freetemp:=false;
  1213. end;
  1214. end;
  1215. end;
  1216. if vtype=$ff then
  1217. internalerror(14357);
  1218. { write changing field update href to the next element }
  1219. inc(href.offset,sizeof(pint));
  1220. if vaddr then
  1221. begin
  1222. hlcg.location_force_mem(current_asmdata.CurrAsmList,hp.left.location,hp.left.resultdef);
  1223. tmpreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidpointertype);
  1224. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.resultdef,voidpointertype,hp.left.location.reference,tmpreg);
  1225. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,tmpreg,href);
  1226. end
  1227. else
  1228. { todo: proper type information for hlcg }
  1229. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.resultdef,{$ifdef cpu16bitaddr}u32inttype{$else}voidpointertype{$endif},hp.left.location,href);
  1230. { update href to the vtype field and write it }
  1231. dec(href.offset,sizeof(pint));
  1232. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  1233. { goto next array element }
  1234. advancearrayoffset(href,elesize);
  1235. end
  1236. else
  1237. { normal array constructor of the same type }
  1238. begin
  1239. if is_managed_type(resultdef) then
  1240. freetemp:=false;
  1241. case hp.left.location.loc of
  1242. LOC_MMREGISTER,
  1243. LOC_CMMREGISTER:
  1244. hlcg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.resultdef,
  1245. hp.left.location.register,href,mms_movescalar);
  1246. LOC_FPUREGISTER,
  1247. LOC_CFPUREGISTER :
  1248. hlcg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.resultdef,hp.left.resultdef,hp.left.location.register,href);
  1249. LOC_REFERENCE,
  1250. LOC_CREFERENCE :
  1251. begin
  1252. if is_shortstring(hp.left.resultdef) then
  1253. hlcg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  1254. Tstringdef(hp.left.resultdef))
  1255. else
  1256. hlcg.g_concatcopy(current_asmdata.CurrAsmList,eledef,hp.left.location.reference,href);
  1257. end;
  1258. else
  1259. begin
  1260. {$ifdef cpu64bitalu}
  1261. if hp.left.location.size in [OS_128,OS_S128] then
  1262. cg128.a_load128_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  1263. else
  1264. {$else cpu64bitalu}
  1265. if hp.left.location.size in [OS_64,OS_S64] then
  1266. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  1267. else
  1268. {$endif cpu64bitalu}
  1269. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,eledef,eledef,hp.left.location,href);
  1270. end;
  1271. end;
  1272. advancearrayoffset(href,elesize);
  1273. end;
  1274. if freetemp then
  1275. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  1276. end;
  1277. { load next entry }
  1278. hp:=tarrayconstructornode(hp.right);
  1279. end;
  1280. end;
  1281. {*****************************************************************************
  1282. SecondRTTI
  1283. *****************************************************************************}
  1284. procedure tcgrttinode.pass_generate_code;
  1285. begin
  1286. location_reset_ref(location,LOC_CREFERENCE,OS_NO,sizeof(pint));
  1287. case rttidatatype of
  1288. rdt_normal:
  1289. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  1290. rdt_ord2str:
  1291. location.reference.symbol:=RTTIWriter.get_rtti_label_ord2str(rttidef,rttitype);
  1292. rdt_str2ord:
  1293. location.reference.symbol:=RTTIWriter.get_rtti_label_str2ord(rttidef,rttitype);
  1294. end;
  1295. end;
  1296. begin
  1297. cloadnode:=tcgloadnode;
  1298. cassignmentnode:=tcgassignmentnode;
  1299. carrayconstructornode:=tcgarrayconstructornode;
  1300. crttinode:=tcgrttinode;
  1301. end.