2
0

ncgld.pas 58 KB

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