ncgld.pas 67 KB

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