ncgld.pas 66 KB

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