ncgld.pas 71 KB

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