ncgld.pas 63 KB

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