ncgld.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. node,nld,cgutils;
  23. type
  24. tcgloadnode = class(tloadnode)
  25. procedure pass_generate_code;override;
  26. procedure generate_picvaraccess;virtual;
  27. procedure changereflocation(const ref: treference);
  28. end;
  29. tcgassignmentnode = class(tassignmentnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. tcgarrayconstructornode = class(tarrayconstructornode)
  33. procedure pass_generate_code;override;
  34. end;
  35. tcgrttinode = class(trttinode)
  36. procedure pass_generate_code;override;
  37. end;
  38. implementation
  39. uses
  40. cutils,
  41. systems,
  42. verbose,globtype,globals,constexp,
  43. nutils,
  44. symconst,symtype,symdef,symsym,defutil,paramgr,
  45. ncnv,ncon,nmem,nbas,ncgrtti,
  46. aasmbase,aasmtai,aasmdata,aasmcpu,
  47. cgbase,pass_2,
  48. procinfo,
  49. cpubase,parabase,
  50. tgobj,ncgutil,
  51. cgobj,
  52. ncgbas,ncgflw;
  53. {*****************************************************************************
  54. SSA (for memory temps) support
  55. *****************************************************************************}
  56. type
  57. preplacerefrec = ^treplacerefrec;
  58. treplacerefrec = record
  59. old, new: preference;
  60. ressym: tsym;
  61. end;
  62. function doreplaceref(var n: tnode; para: pointer): foreachnoderesult;
  63. var
  64. rr: preplacerefrec absolute para;
  65. begin
  66. result := fen_false;
  67. case n.nodetype of
  68. loadn:
  69. begin
  70. { regular variable }
  71. if (tabstractvarsym(tloadnode(n).symtableentry).varoptions * [vo_is_dll_var, vo_is_thread_var] = []) and
  72. not assigned(tloadnode(n).left) and
  73. { not function result, or no exit in function }
  74. (((tloadnode(n).symtableentry <> rr^.ressym) and
  75. not(vo_is_funcret in tabstractvarsym(tloadnode(n).symtableentry).varoptions)) or
  76. not(fc_exit in flowcontrol)) and
  77. { stored in memory... }
  78. (tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.loc in [LOC_REFERENCE]) and
  79. { ... at the place we are looking for }
  80. references_equal(tabstractnormalvarsym(tloadnode(n).symtableentry).localloc.reference,rr^.old^) then
  81. begin
  82. { relocate variable }
  83. tcgloadnode(n).changereflocation(rr^.new^);
  84. result := fen_norecurse_true;
  85. end;
  86. end;
  87. temprefn:
  88. begin
  89. if (ti_valid in ttemprefnode(n).tempinfo^.flags) and
  90. { memory temp... }
  91. (ttemprefnode(n).tempinfo^.location.loc in [LOC_REFERENCE]) and
  92. { ... at the place we are looking for }
  93. references_equal(ttemprefnode(n).tempinfo^.location.reference,rr^.old^) then
  94. begin
  95. { relocate the temp }
  96. tcgtemprefnode(n).changelocation(rr^.new^);
  97. result := fen_norecurse_true;
  98. end;
  99. end;
  100. { optimize the searching a bit }
  101. derefn,addrn,
  102. calln,inlinen,casen,
  103. addn,subn,muln,
  104. andn,orn,xorn,
  105. ltn,lten,gtn,gten,equaln,unequaln,
  106. slashn,divn,shrn,shln,notn,
  107. inn,
  108. asn,isn:
  109. result := fen_norecurse_false;
  110. end;
  111. end;
  112. function maybechangetemp(list: TAsmList; var n: tnode; const newref: treference): boolean;
  113. var
  114. rr: treplacerefrec;
  115. begin
  116. result := false;
  117. { only do for -O2 or higher (breaks debugging since }
  118. { variables move to different memory locations) }
  119. if not(cs_opt_level2 in current_settings.optimizerswitches) or
  120. { must be a copy to a memory location ... }
  121. (n.location.loc <> LOC_REFERENCE) or
  122. { not inside a control flow statement and no goto's in sight }
  123. ([fc_inflowcontrol,fc_gotolabel] * flowcontrol <> []) or
  124. { source and destination are temps (= not global variables) }
  125. not tg.istemp(n.location.reference) or
  126. not tg.istemp(newref) or
  127. { and both point to the start of a temp, and the source is a }
  128. { non-persistent temp (otherwise we need some kind of copy- }
  129. { on-write support in case later on both are still used) }
  130. (tg.gettypeoftemp(newref) <> tt_normal) or
  131. not (tg.gettypeoftemp(n.location.reference) in [tt_normal,tt_persistent]) or
  132. { and both have the same size }
  133. (tg.sizeoftemp(current_asmdata.CurrAsmList,newref) <> tg.sizeoftemp(current_asmdata.CurrAsmList,n.location.reference)) then
  134. exit;
  135. { find the source of the old reference (loadnode or tempnode) }
  136. { and replace it with the new reference }
  137. rr.old := @n.location.reference;
  138. rr.new := @newref;
  139. rr.ressym := nil;
  140. if (current_procinfo.procdef.funcretloc[calleeside].loc<>LOC_VOID) and
  141. assigned(current_procinfo.procdef.funcretsym) and
  142. (tabstractvarsym(current_procinfo.procdef.funcretsym).refs <> 0) then
  143. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  144. rr.ressym:=tsym(current_procinfo.procdef.parast.Find('self'))
  145. else
  146. rr.ressym:=current_procinfo.procdef.funcretsym;
  147. { if source not found, don't do anything }
  148. if not foreachnodestatic(n,@doreplaceref,@rr) then
  149. exit;
  150. n.location.reference := newref;
  151. result:=true;
  152. end;
  153. {*****************************************************************************
  154. SecondLoad
  155. *****************************************************************************}
  156. procedure tcgloadnode.generate_picvaraccess;
  157. begin
  158. {$ifndef sparc}
  159. location.reference.base:=current_procinfo.got;
  160. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname+'@GOT');
  161. {$endif sparc}
  162. end;
  163. procedure tcgloadnode.changereflocation(const ref: treference);
  164. var
  165. oldtemptype: ttemptype;
  166. begin
  167. if (location.loc<>LOC_REFERENCE) then
  168. internalerror(2007020812);
  169. if not tg.istemp(location.reference) then
  170. internalerror(2007020813);
  171. oldtemptype:=tg.gettypeoftemp(location.reference);
  172. if (oldtemptype = tt_persistent) then
  173. tg.ChangeTempType(current_asmdata.CurrAsmList,location.reference,tt_normal);
  174. tg.ungettemp(current_asmdata.CurrAsmList,location.reference);
  175. location.reference:=ref;
  176. tg.ChangeTempType(current_asmdata.CurrAsmList,location.reference,oldtemptype);
  177. tabstractnormalvarsym(symtableentry).localloc:=location;
  178. end;
  179. procedure tcgloadnode.pass_generate_code;
  180. var
  181. hregister : tregister;
  182. vs : tabstractnormalvarsym;
  183. gvs : tstaticvarsym;
  184. pd : tprocdef;
  185. href : treference;
  186. newsize : tcgsize;
  187. endrelocatelab,
  188. norelocatelab : tasmlabel;
  189. paraloc1 : tcgpara;
  190. begin
  191. { we don't know the size of all arrays }
  192. newsize:=def_cgsize(resultdef);
  193. location_reset(location,LOC_REFERENCE,newsize);
  194. case symtableentry.typ of
  195. absolutevarsym :
  196. begin
  197. { this is only for toasm and toaddr }
  198. case tabsolutevarsym(symtableentry).abstyp of
  199. toaddr :
  200. begin
  201. {$ifdef i386}
  202. if tabsolutevarsym(symtableentry).absseg then
  203. location.reference.segment:=NR_FS;
  204. {$endif i386}
  205. location.reference.offset:=tabsolutevarsym(symtableentry).addroffset;
  206. end;
  207. toasm :
  208. location.reference.symbol:=current_asmdata.RefAsmSymbol(tabsolutevarsym(symtableentry).mangledname);
  209. else
  210. internalerror(200310283);
  211. end;
  212. end;
  213. constsym:
  214. begin
  215. if tconstsym(symtableentry).consttyp=constresourcestring then
  216. begin
  217. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  218. location.reference.symbol:=current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',symtableentry.owner,symtableentry.name));
  219. { Resourcestring layout:
  220. TResourceStringRecord = Packed Record
  221. Name,
  222. CurrentValue,
  223. DefaultValue : AnsiString;
  224. HashValue : LongWord;
  225. end;
  226. }
  227. location.reference.offset:=sizeof(aint);
  228. end
  229. else
  230. internalerror(22798);
  231. end;
  232. staticvarsym :
  233. begin
  234. gvs:=tstaticvarsym(symtableentry);
  235. if ([vo_is_dll_var,vo_is_external] * gvs.varoptions <> []) then
  236. begin
  237. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,tstaticvarsym(symtableentry).mangledname);
  238. if (location.reference.base <> NR_NO) then
  239. exit;
  240. end;
  241. if (vo_is_dll_var in gvs.varoptions) then
  242. { DLL variable }
  243. begin
  244. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  245. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname);
  246. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,location.reference,hregister);
  247. reference_reset_base(location.reference,hregister,0);
  248. end
  249. { Thread variable }
  250. else if (vo_is_thread_var in gvs.varoptions) and
  251. not(tf_section_threadvars in target_info.flags) then
  252. begin
  253. if (tf_section_threadvars in target_info.flags) then
  254. begin
  255. if gvs.localloc.loc=LOC_INVALID then
  256. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0)
  257. else
  258. location:=gvs.localloc;
  259. {$ifdef i386}
  260. case target_info.system of
  261. system_i386_linux:
  262. location.reference.segment:=NR_GS;
  263. system_i386_win32:
  264. location.reference.segment:=NR_FS;
  265. end;
  266. {$endif i386}
  267. end
  268. else
  269. begin
  270. {
  271. Thread var loading is optimized to first check if
  272. a relocate function is available. When the function
  273. is available it is called to retrieve the address.
  274. Otherwise the address is loaded with the symbol
  275. The code needs to be in the order to first handle the
  276. call and then the address load to be sure that the
  277. register that is used for returning is the same (PFV)
  278. }
  279. current_asmdata.getjumplabel(norelocatelab);
  280. current_asmdata.getjumplabel(endrelocatelab);
  281. { make sure hregister can't allocate the register necessary for the parameter }
  282. paraloc1.init;
  283. paramanager.getintparaloc(pocall_default,1,paraloc1);
  284. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  285. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_THREADVAR_RELOCATE'),0);
  286. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  287. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,hregister,norelocatelab);
  288. { don't save the allocated register else the result will be destroyed later }
  289. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname),0);
  290. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  291. cg.a_param_ref(current_asmdata.CurrAsmList,OS_32,href,paraloc1);
  292. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  293. paraloc1.done;
  294. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  295. cg.a_call_reg(current_asmdata.CurrAsmList,hregister);
  296. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  297. cg.getcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  298. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  299. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  300. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  301. cg.a_jmp_always(current_asmdata.CurrAsmList,endrelocatelab);
  302. cg.a_label(current_asmdata.CurrAsmList,norelocatelab);
  303. { no relocation needed, load the address of the variable only, the
  304. layout of a threadvar is (4 bytes pointer):
  305. 0 - Threadvar index
  306. 4 - Threadvar value in single threading }
  307. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname),sizeof(aint));
  308. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  309. cg.a_label(current_asmdata.CurrAsmList,endrelocatelab);
  310. location.reference.base:=hregister;
  311. end;
  312. end
  313. { Normal (or external) variable }
  314. else
  315. begin
  316. if gvs.localloc.loc=LOC_INVALID then
  317. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0)
  318. else
  319. location:=gvs.localloc;
  320. end;
  321. { make const a LOC_CREFERENCE }
  322. if (gvs.varspez=vs_const) and
  323. (location.loc=LOC_REFERENCE) then
  324. location.loc:=LOC_CREFERENCE;
  325. end;
  326. paravarsym,
  327. localvarsym :
  328. begin
  329. vs:=tabstractnormalvarsym(symtableentry);
  330. { Nested variable }
  331. if assigned(left) then
  332. begin
  333. secondpass(left);
  334. if left.location.loc<>LOC_REGISTER then
  335. internalerror(200309286);
  336. if vs.localloc.loc<>LOC_REFERENCE then
  337. internalerror(200409241);
  338. reference_reset_base(location.reference,left.location.register,vs.localloc.reference.offset);
  339. end
  340. else
  341. location:=vs.localloc;
  342. { handle call by reference variables when they are not
  343. alreayd copied to local copies. Also ignore the reference
  344. when we need to load the self pointer for objects }
  345. if is_addr_param_load then
  346. begin
  347. if (location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  348. hregister:=location.register
  349. else
  350. begin
  351. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  352. { we need to load only an address }
  353. location.size:=OS_ADDR;
  354. cg.a_load_loc_reg(current_asmdata.CurrAsmList,location.size,location,hregister);
  355. end;
  356. location_reset(location,LOC_REFERENCE,newsize);
  357. location.reference.base:=hregister;
  358. end;
  359. { make const a LOC_CREFERENCE }
  360. if (vs.varspez=vs_const) and
  361. (location.loc=LOC_REFERENCE) then
  362. location.loc:=LOC_CREFERENCE;
  363. end;
  364. procsym:
  365. begin
  366. if not assigned(procdef) then
  367. internalerror(200312011);
  368. if assigned(left) then
  369. begin
  370. if (sizeof(aint) = 4) then
  371. location_reset(location,LOC_CREFERENCE,OS_64)
  372. else if (sizeof(aint) = 8) then
  373. location_reset(location,LOC_CREFERENCE,OS_128)
  374. else
  375. internalerror(20020520);
  376. tg.GetTemp(current_asmdata.CurrAsmList,2*sizeof(aint),tt_normal,location.reference);
  377. secondpass(left);
  378. { load class instance/classrefdef address }
  379. if left.location.loc=LOC_CONSTANT then
  380. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_ADDR,false);
  381. case left.location.loc of
  382. LOC_CREGISTER,
  383. LOC_REGISTER:
  384. begin
  385. { this is not possible for objects }
  386. if is_object(left.resultdef) then
  387. internalerror(200304234);
  388. hregister:=left.location.register;
  389. end;
  390. LOC_CREFERENCE,
  391. LOC_REFERENCE:
  392. begin
  393. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  394. if not is_object(left.resultdef) then
  395. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,hregister)
  396. else
  397. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,hregister);
  398. location_freetemp(current_asmdata.CurrAsmList,left.location);
  399. end;
  400. else
  401. internalerror(200610311);
  402. end;
  403. { store the class instance or classredef address }
  404. href:=location.reference;
  405. inc(href.offset,sizeof(aint));
  406. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,href);
  407. { virtual method ? }
  408. if (po_virtualmethod in procdef.procoptions) and
  409. not(nf_inherited in flags) then
  410. begin
  411. { a classrefdef already points to the VMT }
  412. if (left.resultdef.typ<>classrefdef) then
  413. begin
  414. { load vmt pointer }
  415. reference_reset_base(href,hregister,0);
  416. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  417. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  418. end;
  419. { load method address }
  420. reference_reset_base(href,hregister,procdef._class.vmtmethodoffset(procdef.extnumber));
  421. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  422. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  423. { ... and store it }
  424. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  425. end
  426. else
  427. begin
  428. { load address of the function }
  429. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0);
  430. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  431. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  432. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  433. end;
  434. end
  435. else
  436. begin
  437. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  438. if (po_external in pd.procoptions) then
  439. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,pd.mangledname);
  440. {!!!!! Be aware, work on virtual methods too }
  441. if (location.reference.base = NR_NO) then
  442. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  443. end;
  444. end;
  445. labelsym :
  446. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  447. else internalerror(200510032);
  448. end;
  449. end;
  450. {*****************************************************************************
  451. SecondAssignment
  452. *****************************************************************************}
  453. procedure tcgassignmentnode.pass_generate_code;
  454. var
  455. otlabel,hlabel,oflabel : tasmlabel;
  456. href : treference;
  457. releaseright : boolean;
  458. len : aint;
  459. r:Tregister;
  460. begin
  461. location_reset(location,LOC_VOID,OS_NO);
  462. otlabel:=current_procinfo.CurrTrueLabel;
  463. oflabel:=current_procinfo.CurrFalseLabel;
  464. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  465. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  466. {
  467. in most cases we can process first the right node which contains
  468. the most complex code. Exceptions for this are:
  469. - result is in flags, loading left will then destroy the flags
  470. - result is a jump, loading left must be already done before the jump is made
  471. - result need reference count, when left points to a value used in
  472. right then decreasing the refcnt on left can possibly release
  473. the memory before right increased the refcnt, result is that an
  474. empty value is assigned
  475. But not when the result is in the flags, then
  476. loading the left node afterwards can destroy the flags.
  477. }
  478. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  479. ((right.resultdef.needs_inittable) or
  480. (node_complexity(right)>node_complexity(left))) then
  481. begin
  482. secondpass(right);
  483. { increment source reference counter, this is
  484. useless for constants }
  485. if (right.resultdef.needs_inittable) and
  486. not is_constnode(right) then
  487. begin
  488. location_force_mem(current_asmdata.CurrAsmList,right.location);
  489. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  490. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  491. end;
  492. if codegenerror then
  493. exit;
  494. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  495. { can be false }
  496. secondpass(left);
  497. { decrement destination reference counter }
  498. if (left.resultdef.needs_inittable) then
  499. begin
  500. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  501. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  502. end;
  503. if codegenerror then
  504. exit;
  505. end
  506. else
  507. begin
  508. { calculate left sides }
  509. secondpass(left);
  510. { decrement destination reference counter }
  511. if (left.resultdef.needs_inittable) then
  512. begin
  513. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  514. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  515. end;
  516. if codegenerror then
  517. exit;
  518. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  519. { can be false }
  520. secondpass(right);
  521. { increment source reference counter, this is
  522. useless for string constants}
  523. if (right.resultdef.needs_inittable) and
  524. (right.nodetype<>stringconstn) then
  525. begin
  526. location_force_mem(current_asmdata.CurrAsmList,right.location);
  527. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  528. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  529. end;
  530. if codegenerror then
  531. exit;
  532. end;
  533. releaseright:=true;
  534. { shortstring assignments are handled separately }
  535. if is_shortstring(left.resultdef) then
  536. begin
  537. {
  538. we can get here only in the following situations
  539. for the right node:
  540. - empty constant string
  541. - char
  542. }
  543. { The addn is replaced by a blockn or calln that already returns
  544. a shortstring }
  545. if is_shortstring(right.resultdef) and
  546. (right.nodetype in [blockn,calln]) then
  547. begin
  548. { nothing to do }
  549. end
  550. { empty constant string }
  551. else if (right.nodetype=stringconstn) and
  552. (tstringconstnode(right).len=0) then
  553. begin
  554. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,0,left.location.reference);
  555. end
  556. { char loading }
  557. else if is_char(right.resultdef) then
  558. begin
  559. if right.nodetype=ordconstn then
  560. begin
  561. if (target_info.endian = endian_little) then
  562. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,(tordconstnode(right).value.svalue shl 8) or 1,
  563. left.location.reference)
  564. else
  565. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,tordconstnode(right).value.svalue or (1 shl 8),
  566. left.location.reference);
  567. end
  568. else
  569. begin
  570. href:=left.location.reference;
  571. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,1,href);
  572. inc(href.offset,1);
  573. case right.location.loc of
  574. LOC_REGISTER,
  575. LOC_CREGISTER :
  576. begin
  577. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  578. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,href);
  579. end;
  580. LOC_REFERENCE,
  581. LOC_CREFERENCE :
  582. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,href);
  583. else
  584. internalerror(200205111);
  585. end;
  586. end;
  587. end
  588. else
  589. internalerror(200204249);
  590. end
  591. { try to reuse memory locations instead of copying }
  592. { copy to a memory location ... }
  593. else if (right.location.loc = LOC_REFERENCE) and
  594. maybechangetemp(current_asmdata.CurrAsmList,left,right.location.reference) then
  595. begin
  596. { if it worked, we're done }
  597. end
  598. else
  599. begin
  600. { SSA support }
  601. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  602. maybechangeloadnodereg(current_asmdata.CurrAsmList,right,true);
  603. case right.location.loc of
  604. LOC_CONSTANT :
  605. begin
  606. {$ifndef cpu64bit}
  607. if (left.location.size in [OS_64,OS_S64]) or (right.location.size in [OS_64,OS_S64]) then
  608. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  609. else
  610. {$endif cpu64bit}
  611. cg.a_load_const_loc(current_asmdata.CurrAsmList,right.location.value,left.location);
  612. end;
  613. LOC_REFERENCE,
  614. LOC_CREFERENCE :
  615. begin
  616. case left.location.loc of
  617. LOC_REGISTER,
  618. LOC_CREGISTER :
  619. begin
  620. {$ifndef cpu64bit}
  621. if left.location.size in [OS_64,OS_S64] then
  622. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  623. else
  624. {$endif cpu64bit}
  625. cg.a_load_ref_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.register);
  626. end;
  627. LOC_FPUREGISTER,
  628. LOC_CFPUREGISTER :
  629. begin
  630. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  631. right.location.size,left.location.size,
  632. right.location.reference,
  633. left.location.register);
  634. end;
  635. LOC_REFERENCE,
  636. LOC_CREFERENCE :
  637. begin
  638. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  639. { Use unaligned copy when the offset is not aligned }
  640. len:=left.resultdef.size;
  641. if (right.location.reference.offset mod sizeof(aint)<>0) or
  642. (left.location.reference.offset mod sizeof(aint)<>0) or
  643. (right.resultdef.alignment<sizeof(aint)) then
  644. cg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len)
  645. else
  646. cg.g_concatcopy(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len);
  647. end;
  648. LOC_MMREGISTER,
  649. LOC_CMMREGISTER:
  650. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  651. right.location.size,
  652. left.location.size,
  653. right.location.reference,
  654. left.location.register,mms_movescalar);
  655. LOC_SUBSETREG,
  656. LOC_CSUBSETREG:
  657. cg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sreg);
  658. LOC_SUBSETREF,
  659. LOC_CSUBSETREF:
  660. {$ifndef cpu64bit}
  661. if right.location.size in [OS_64,OS_S64] then
  662. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  663. else
  664. {$endif cpu64bit}
  665. cg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sref);
  666. else
  667. internalerror(200203284);
  668. end;
  669. end;
  670. {$ifdef SUPPORT_MMX}
  671. LOC_CMMXREGISTER,
  672. LOC_MMXREGISTER:
  673. begin
  674. if left.location.loc=LOC_CMMXREGISTER then
  675. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  676. else
  677. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  678. end;
  679. {$endif SUPPORT_MMX}
  680. LOC_MMREGISTER,
  681. LOC_CMMREGISTER:
  682. begin
  683. if left.resultdef.typ=arraydef then
  684. begin
  685. end
  686. else
  687. begin
  688. if left.location.loc=LOC_CMMREGISTER then
  689. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  690. else
  691. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  692. end;
  693. end;
  694. LOC_REGISTER,
  695. LOC_CREGISTER :
  696. begin
  697. {$ifndef cpu64bit}
  698. if left.location.size in [OS_64,OS_S64] then
  699. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  700. right.location.register64,left.location)
  701. else
  702. {$endif cpu64bit}
  703. cg.a_load_reg_loc(current_asmdata.CurrAsmList,right.location.size,right.location.register,left.location);
  704. end;
  705. LOC_FPUREGISTER,
  706. LOC_CFPUREGISTER :
  707. begin
  708. { we can't do direct moves between fpu and mm registers }
  709. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  710. begin
  711. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,false);
  712. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  713. right.location.size,left.location.size,
  714. right.location.register,left.location.register,mms_movescalar);
  715. end
  716. else
  717. cg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  718. right.location.size,
  719. right.location.register,left.location);
  720. end;
  721. LOC_SUBSETREG,
  722. LOC_CSUBSETREG:
  723. begin
  724. cg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  725. right.location.size,right.location.sreg,left.location);
  726. end;
  727. LOC_SUBSETREF,
  728. LOC_CSUBSETREF:
  729. begin
  730. {$ifndef cpu64bit}
  731. if right.location.size in [OS_64,OS_S64] then
  732. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  733. else
  734. {$endif cpu64bit}
  735. cg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  736. right.location.size,right.location.sref,left.location);
  737. end;
  738. LOC_JUMP :
  739. begin
  740. current_asmdata.getjumplabel(hlabel);
  741. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  742. cg.a_load_const_loc(current_asmdata.CurrAsmList,1,left.location);
  743. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  744. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  745. cg.a_load_const_loc(current_asmdata.CurrAsmList,0,left.location);
  746. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  747. end;
  748. {$ifdef cpuflags}
  749. LOC_FLAGS :
  750. begin
  751. {This can be a wordbool or longbool too, no?}
  752. case left.location.loc of
  753. LOC_REGISTER,LOC_CREGISTER:
  754. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.register);
  755. LOC_REFERENCE:
  756. cg.g_flags2ref(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.reference);
  757. LOC_SUBSETREG,LOC_SUBSETREF:
  758. begin
  759. r:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(left.resultdef));
  760. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,r);
  761. cg.a_load_reg_loc(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),r,left.location);
  762. end;
  763. else
  764. internalerror(200203273);
  765. end;
  766. end;
  767. {$endif cpuflags}
  768. end;
  769. end;
  770. if releaseright then
  771. location_freetemp(current_asmdata.CurrAsmList,right.location);
  772. current_procinfo.CurrTrueLabel:=otlabel;
  773. current_procinfo.CurrFalseLabel:=oflabel;
  774. end;
  775. {*****************************************************************************
  776. SecondArrayConstruct
  777. *****************************************************************************}
  778. const
  779. vtInteger = 0;
  780. vtBoolean = 1;
  781. vtChar = 2;
  782. vtExtended = 3;
  783. vtString = 4;
  784. vtPointer = 5;
  785. vtPChar = 6;
  786. vtObject = 7;
  787. vtClass = 8;
  788. vtWideChar = 9;
  789. vtPWideChar = 10;
  790. vtAnsiString32 = 11;
  791. vtCurrency = 12;
  792. vtVariant = 13;
  793. vtInterface = 14;
  794. vtWideString = 15;
  795. vtInt64 = 16;
  796. vtQWord = 17;
  797. vtAnsiString16 = 18;
  798. vtAnsiString64 = 19;
  799. procedure tcgarrayconstructornode.pass_generate_code;
  800. var
  801. hp : tarrayconstructornode;
  802. href : treference;
  803. lt : tdef;
  804. vaddr : boolean;
  805. vtype : longint;
  806. freetemp,
  807. dovariant : boolean;
  808. elesize : longint;
  809. tmpreg : tregister;
  810. paraloc : tcgparalocation;
  811. begin
  812. if is_packed_array(resultdef) then
  813. internalerror(200608042);
  814. dovariant:=(nf_forcevaria in flags) or is_variant_array(resultdef);
  815. if dovariant then
  816. elesize:=sizeof(aint)+sizeof(aint)
  817. else
  818. elesize:=tarraydef(resultdef).elesize;
  819. location_reset(location,LOC_CREFERENCE,OS_NO);
  820. fillchar(paraloc,sizeof(paraloc),0);
  821. { Allocate always a temp, also if no elements are required, to
  822. be sure that location is valid (PFV) }
  823. if tarraydef(resultdef).highrange=-1 then
  824. tg.GetTemp(current_asmdata.CurrAsmList,elesize,tt_normal,location.reference)
  825. else
  826. tg.GetTemp(current_asmdata.CurrAsmList,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  827. href:=location.reference;
  828. { Process nodes in array constructor }
  829. hp:=self;
  830. while assigned(hp) do
  831. begin
  832. if assigned(hp.left) then
  833. begin
  834. freetemp:=true;
  835. secondpass(hp.left);
  836. if codegenerror then
  837. exit;
  838. { Move flags and jump in register }
  839. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  840. location_force_reg(current_asmdata.CurrAsmList,hp.left.location,def_cgsize(hp.left.resultdef),false);
  841. if dovariant then
  842. begin
  843. { find the correct vtype value }
  844. vtype:=$ff;
  845. vaddr:=false;
  846. lt:=hp.left.resultdef;
  847. case lt.typ of
  848. enumdef,
  849. orddef :
  850. begin
  851. if is_64bit(lt) then
  852. begin
  853. case torddef(lt).ordtype of
  854. scurrency:
  855. vtype:=vtCurrency;
  856. s64bit:
  857. vtype:=vtInt64;
  858. u64bit:
  859. vtype:=vtQWord;
  860. end;
  861. freetemp:=false;
  862. vaddr:=true;
  863. end
  864. else if (lt.typ=enumdef) or
  865. is_integer(lt) then
  866. vtype:=vtInteger
  867. else
  868. if is_boolean(lt) then
  869. vtype:=vtBoolean
  870. else
  871. if (lt.typ=orddef) then
  872. begin
  873. case torddef(lt).ordtype of
  874. uchar:
  875. vtype:=vtChar;
  876. uwidechar:
  877. vtype:=vtWideChar;
  878. end;
  879. end;
  880. end;
  881. floatdef :
  882. begin
  883. if is_currency(lt) then
  884. vtype:=vtCurrency
  885. else
  886. vtype:=vtExtended;
  887. freetemp:=false;
  888. vaddr:=true;
  889. end;
  890. procvardef,
  891. pointerdef :
  892. begin
  893. if is_pchar(lt) then
  894. vtype:=vtPChar
  895. else if is_pwidechar(lt) then
  896. vtype:=vtPWideChar
  897. else
  898. vtype:=vtPointer;
  899. end;
  900. variantdef :
  901. begin
  902. vtype:=vtVariant;
  903. vaddr:=true;
  904. freetemp:=false;
  905. end;
  906. classrefdef :
  907. vtype:=vtClass;
  908. objectdef :
  909. if is_interface(lt) then
  910. vtype:=vtInterface
  911. { vtObject really means a class based on TObject }
  912. else if is_class(lt) then
  913. vtype:=vtObject
  914. else
  915. internalerror(200505171);
  916. stringdef :
  917. begin
  918. if is_shortstring(lt) then
  919. begin
  920. vtype:=vtString;
  921. vaddr:=true;
  922. freetemp:=false;
  923. end
  924. else
  925. if is_ansistring(lt) then
  926. begin
  927. vtype:=vtAnsiString;
  928. freetemp:=false;
  929. end
  930. else
  931. if is_widestring(lt) then
  932. begin
  933. vtype:=vtWideString;
  934. freetemp:=false;
  935. end;
  936. end;
  937. end;
  938. if vtype=$ff then
  939. internalerror(14357);
  940. { write changing field update href to the next element }
  941. inc(href.offset,sizeof(aint));
  942. if vaddr then
  943. begin
  944. location_force_mem(current_asmdata.CurrAsmList,hp.left.location);
  945. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  946. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.location.reference,tmpreg);
  947. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,href);
  948. end
  949. else
  950. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,hp.left.location,href);
  951. { update href to the vtype field and write it }
  952. dec(href.offset,sizeof(aint));
  953. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  954. { goto next array element }
  955. inc(href.offset,sizeof(aint)*2);
  956. end
  957. else
  958. { normal array constructor of the same type }
  959. begin
  960. if resultdef.needs_inittable then
  961. freetemp:=false;
  962. case hp.left.location.loc of
  963. LOC_MMREGISTER,
  964. LOC_CMMREGISTER:
  965. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,
  966. hp.left.location.register,href,mms_movescalar);
  967. LOC_FPUREGISTER,
  968. LOC_CFPUREGISTER :
  969. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,hp.left.location.register,href);
  970. LOC_REFERENCE,
  971. LOC_CREFERENCE :
  972. begin
  973. if is_shortstring(hp.left.resultdef) then
  974. cg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  975. Tstringdef(hp.left.resultdef).len)
  976. else
  977. cg.g_concatcopy(current_asmdata.CurrAsmList,hp.left.location.reference,href,elesize);
  978. end;
  979. else
  980. begin
  981. {$ifndef cpu64bit}
  982. if hp.left.location.size in [OS_64,OS_S64] then
  983. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  984. else
  985. {$endif cpu64bit}
  986. cg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location,href);
  987. end;
  988. end;
  989. inc(href.offset,elesize);
  990. end;
  991. if freetemp then
  992. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  993. end;
  994. { load next entry }
  995. hp:=tarrayconstructornode(hp.right);
  996. end;
  997. end;
  998. {*****************************************************************************
  999. SecondRTTI
  1000. *****************************************************************************}
  1001. procedure tcgrttinode.pass_generate_code;
  1002. begin
  1003. location_reset(location,LOC_CREFERENCE,OS_NO);
  1004. case rttidatatype of
  1005. rdt_normal:
  1006. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  1007. rdt_ord2str:
  1008. location.reference.symbol:=RTTIWriter.get_rtti_label_ord2str(rttidef,rttitype);
  1009. rdt_str2ord:
  1010. location.reference.symbol:=RTTIWriter.get_rtti_label_str2ord(rttidef,rttitype);
  1011. end;
  1012. end;
  1013. begin
  1014. cloadnode:=tcgloadnode;
  1015. cassignmentnode:=tcgassignmentnode;
  1016. carrayconstructornode:=tcgarrayconstructornode;
  1017. crttinode:=tcgrttinode;
  1018. end.