ncgld.pas 58 KB

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