ncgld.pas 63 KB

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