ncgld.pas 60 KB

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