ncgld.pas 63 KB

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