ncgld.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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,
  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 (ttemprefnode(n).tempinfo^.valid) 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 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 is_class_or_interface(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 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. { load vmt pointer }
  412. reference_reset_base(href,hregister,0);
  413. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  414. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  415. { load method address }
  416. reference_reset_base(href,hregister,procdef._class.vmtmethodoffset(procdef.extnumber));
  417. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  418. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  419. { ... and store it }
  420. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  421. end
  422. else
  423. begin
  424. { load address of the function }
  425. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0);
  426. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  427. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  428. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  429. end;
  430. end
  431. else
  432. begin
  433. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  434. if (po_external in pd.procoptions) then
  435. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,pd.mangledname);
  436. {!!!!! Be aware, work on virtual methods too }
  437. if (location.reference.base = NR_NO) then
  438. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  439. end;
  440. end;
  441. labelsym :
  442. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  443. else internalerror(200510032);
  444. end;
  445. end;
  446. {*****************************************************************************
  447. SecondAssignment
  448. *****************************************************************************}
  449. procedure tcgassignmentnode.pass_generate_code;
  450. var
  451. otlabel,hlabel,oflabel : tasmlabel;
  452. href : treference;
  453. releaseright : boolean;
  454. len : aint;
  455. r:Tregister;
  456. begin
  457. location_reset(location,LOC_VOID,OS_NO);
  458. otlabel:=current_procinfo.CurrTrueLabel;
  459. oflabel:=current_procinfo.CurrFalseLabel;
  460. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  461. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  462. {
  463. in most cases we can process first the right node which contains
  464. the most complex code. Exceptions for this are:
  465. - result is in flags, loading left will then destroy the flags
  466. - result is a jump, loading left must be already done before the jump is made
  467. - result need reference count, when left points to a value used in
  468. right then decreasing the refcnt on left can possibly release
  469. the memory before right increased the refcnt, result is that an
  470. empty value is assigned
  471. - calln, call destroys most registers and is therefor 'complex'
  472. But not when the result is in the flags, then
  473. loading the left node afterwards can destroy the flags.
  474. }
  475. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  476. ((right.nodetype=calln) or
  477. (right.resultdef.needs_inittable) or
  478. (right.registersint>=left.registersint)) then
  479. begin
  480. secondpass(right);
  481. { increment source reference counter, this is
  482. useless for string constants}
  483. if (right.resultdef.needs_inittable) and
  484. (right.nodetype<>stringconstn) then
  485. begin
  486. location_force_mem(current_asmdata.CurrAsmList,right.location);
  487. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  488. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  489. end;
  490. if codegenerror then
  491. exit;
  492. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  493. { can be false }
  494. secondpass(left);
  495. { decrement destination reference counter }
  496. if (left.resultdef.needs_inittable) then
  497. begin
  498. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  499. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  500. end;
  501. if codegenerror then
  502. exit;
  503. end
  504. else
  505. begin
  506. { calculate left sides }
  507. secondpass(left);
  508. { decrement destination reference counter }
  509. if (left.resultdef.needs_inittable) then
  510. begin
  511. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  512. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  513. end;
  514. if codegenerror then
  515. exit;
  516. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  517. { can be false }
  518. secondpass(right);
  519. { increment source reference counter, this is
  520. useless for string constants}
  521. if (right.resultdef.needs_inittable) and
  522. (right.nodetype<>stringconstn) then
  523. begin
  524. location_force_mem(current_asmdata.CurrAsmList,right.location);
  525. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  526. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  527. end;
  528. if codegenerror then
  529. exit;
  530. end;
  531. releaseright:=true;
  532. { shortstring assignments are handled separately }
  533. if is_shortstring(left.resultdef) then
  534. begin
  535. {
  536. we can get here only in the following situations
  537. for the right node:
  538. - empty constant string
  539. - char
  540. }
  541. { The addn is replaced by a blockn or calln }
  542. if right.nodetype in [blockn,calln] then
  543. begin
  544. { nothing to do }
  545. end
  546. { empty constant string }
  547. else if (right.nodetype=stringconstn) and
  548. (tstringconstnode(right).len=0) then
  549. begin
  550. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,0,left.location.reference);
  551. end
  552. { char loading }
  553. else if is_char(right.resultdef) then
  554. begin
  555. if right.nodetype=ordconstn then
  556. begin
  557. if (target_info.endian = endian_little) then
  558. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,(tordconstnode(right).value shl 8) or 1,
  559. left.location.reference)
  560. else
  561. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,tordconstnode(right).value or (1 shl 8),
  562. left.location.reference);
  563. end
  564. else
  565. begin
  566. href:=left.location.reference;
  567. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,1,href);
  568. inc(href.offset,1);
  569. case right.location.loc of
  570. LOC_REGISTER,
  571. LOC_CREGISTER :
  572. begin
  573. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  574. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,href);
  575. end;
  576. LOC_REFERENCE,
  577. LOC_CREFERENCE :
  578. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,href);
  579. else
  580. internalerror(200205111);
  581. end;
  582. end;
  583. end
  584. else
  585. internalerror(200204249);
  586. end
  587. { try to reuse memory locations instead of copying }
  588. { copy to a memory location ... }
  589. else if (right.location.loc = LOC_REFERENCE) and
  590. maybechangetemp(current_asmdata.CurrAsmList,left,right.location.reference) then
  591. begin
  592. { if it worked, we're done }
  593. end
  594. else
  595. begin
  596. { SSA support }
  597. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  598. maybechangeloadnodereg(current_asmdata.CurrAsmList,right,true);
  599. case right.location.loc of
  600. LOC_CONSTANT :
  601. begin
  602. {$ifndef cpu64bit}
  603. if right.location.size in [OS_64,OS_S64] then
  604. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  605. else
  606. {$endif cpu64bit}
  607. cg.a_load_const_loc(current_asmdata.CurrAsmList,right.location.value,left.location);
  608. end;
  609. LOC_REFERENCE,
  610. LOC_CREFERENCE :
  611. begin
  612. case left.location.loc of
  613. LOC_REGISTER,
  614. LOC_CREGISTER :
  615. begin
  616. {$ifndef cpu64bit}
  617. if left.location.size in [OS_64,OS_S64] then
  618. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  619. else
  620. {$endif cpu64bit}
  621. cg.a_load_ref_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.register);
  622. end;
  623. LOC_FPUREGISTER,
  624. LOC_CFPUREGISTER :
  625. begin
  626. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  627. right.location.size,left.location.size,
  628. right.location.reference,
  629. left.location.register);
  630. end;
  631. LOC_REFERENCE,
  632. LOC_CREFERENCE :
  633. begin
  634. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  635. { Use unaligned copy when the offset is not aligned }
  636. len:=left.resultdef.size;
  637. if (right.location.reference.offset mod sizeof(aint)<>0) or
  638. (left.location.reference.offset mod sizeof(aint)<>0) or
  639. (right.resultdef.alignment<sizeof(aint)) then
  640. cg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len)
  641. else
  642. cg.g_concatcopy(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len);
  643. end;
  644. LOC_MMREGISTER,
  645. LOC_CMMREGISTER:
  646. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  647. right.location.size,
  648. left.location.size,
  649. right.location.reference,
  650. left.location.register,mms_movescalar);
  651. LOC_SUBSETREG,
  652. LOC_CSUBSETREG:
  653. cg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sreg);
  654. LOC_SUBSETREF,
  655. LOC_CSUBSETREF:
  656. {$ifndef cpu64bit}
  657. if right.location.size in [OS_64,OS_S64] then
  658. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  659. else
  660. {$endif cpu64bit}
  661. cg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sref);
  662. else
  663. internalerror(200203284);
  664. end;
  665. end;
  666. {$ifdef SUPPORT_MMX}
  667. LOC_CMMXREGISTER,
  668. LOC_MMXREGISTER:
  669. begin
  670. if left.location.loc=LOC_CMMXREGISTER then
  671. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  672. else
  673. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  674. end;
  675. {$endif SUPPORT_MMX}
  676. LOC_MMREGISTER,
  677. LOC_CMMREGISTER:
  678. begin
  679. if left.resultdef.typ=arraydef then
  680. begin
  681. end
  682. else
  683. begin
  684. if left.location.loc=LOC_CMMREGISTER then
  685. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  686. else
  687. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  688. end;
  689. end;
  690. LOC_REGISTER,
  691. LOC_CREGISTER :
  692. begin
  693. {$ifndef cpu64bit}
  694. if left.location.size in [OS_64,OS_S64] then
  695. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  696. right.location.register64,left.location)
  697. else
  698. {$endif cpu64bit}
  699. cg.a_load_reg_loc(current_asmdata.CurrAsmList,right.location.size,right.location.register,left.location);
  700. end;
  701. LOC_FPUREGISTER,
  702. LOC_CFPUREGISTER :
  703. begin
  704. { we can't do direct moves between fpu and mm registers }
  705. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  706. begin
  707. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,false);
  708. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  709. right.location.size,left.location.size,
  710. right.location.register,left.location.register,mms_movescalar);
  711. end
  712. else
  713. cg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  714. right.location.size,
  715. right.location.register,left.location);
  716. end;
  717. LOC_SUBSETREG,
  718. LOC_CSUBSETREG:
  719. begin
  720. cg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  721. right.location.size,right.location.sreg,left.location);
  722. end;
  723. LOC_SUBSETREF,
  724. LOC_CSUBSETREF:
  725. begin
  726. {$ifndef cpu64bit}
  727. if right.location.size in [OS_64,OS_S64] then
  728. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  729. else
  730. {$endif cpu64bit}
  731. cg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  732. right.location.size,right.location.sref,left.location);
  733. end;
  734. LOC_JUMP :
  735. begin
  736. current_asmdata.getjumplabel(hlabel);
  737. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  738. cg.a_load_const_loc(current_asmdata.CurrAsmList,1,left.location);
  739. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  740. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  741. cg.a_load_const_loc(current_asmdata.CurrAsmList,0,left.location);
  742. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  743. end;
  744. {$ifdef cpuflags}
  745. LOC_FLAGS :
  746. begin
  747. {This can be a wordbool or longbool too, no?}
  748. case left.location.loc of
  749. LOC_REGISTER,LOC_CREGISTER:
  750. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.register);
  751. LOC_REFERENCE:
  752. cg.g_flags2ref(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.reference);
  753. LOC_SUBSETREG,LOC_SUBSETREF:
  754. begin
  755. r:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(left.resultdef));
  756. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,r);
  757. cg.a_load_reg_loc(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),r,left.location);
  758. end;
  759. else
  760. internalerror(200203273);
  761. end;
  762. end;
  763. {$endif cpuflags}
  764. end;
  765. end;
  766. if releaseright then
  767. location_freetemp(current_asmdata.CurrAsmList,right.location);
  768. current_procinfo.CurrTrueLabel:=otlabel;
  769. current_procinfo.CurrFalseLabel:=oflabel;
  770. end;
  771. {*****************************************************************************
  772. SecondArrayConstruct
  773. *****************************************************************************}
  774. const
  775. vtInteger = 0;
  776. vtBoolean = 1;
  777. vtChar = 2;
  778. vtExtended = 3;
  779. vtString = 4;
  780. vtPointer = 5;
  781. vtPChar = 6;
  782. vtObject = 7;
  783. vtClass = 8;
  784. vtWideChar = 9;
  785. vtPWideChar = 10;
  786. vtAnsiString32 = 11;
  787. vtCurrency = 12;
  788. vtVariant = 13;
  789. vtInterface = 14;
  790. vtWideString = 15;
  791. vtInt64 = 16;
  792. vtQWord = 17;
  793. vtAnsiString16 = 18;
  794. vtAnsiString64 = 19;
  795. procedure tcgarrayconstructornode.pass_generate_code;
  796. var
  797. hp : tarrayconstructornode;
  798. href : treference;
  799. lt : tdef;
  800. vaddr : boolean;
  801. vtype : longint;
  802. freetemp,
  803. dovariant : boolean;
  804. elesize : longint;
  805. tmpreg : tregister;
  806. paraloc : tcgparalocation;
  807. begin
  808. if is_packed_array(resultdef) then
  809. internalerror(200608042);
  810. dovariant:=(nf_forcevaria in flags) or is_variant_array(resultdef);
  811. if dovariant then
  812. elesize:=sizeof(aint)+sizeof(aint)
  813. else
  814. elesize:=tarraydef(resultdef).elesize;
  815. location_reset(location,LOC_CREFERENCE,OS_NO);
  816. fillchar(paraloc,sizeof(paraloc),0);
  817. { Allocate always a temp, also if no elements are required, to
  818. be sure that location is valid (PFV) }
  819. if tarraydef(resultdef).highrange=-1 then
  820. tg.GetTemp(current_asmdata.CurrAsmList,elesize,tt_normal,location.reference)
  821. else
  822. tg.GetTemp(current_asmdata.CurrAsmList,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  823. href:=location.reference;
  824. { Process nodes in array constructor }
  825. hp:=self;
  826. while assigned(hp) do
  827. begin
  828. if assigned(hp.left) then
  829. begin
  830. freetemp:=true;
  831. secondpass(hp.left);
  832. if codegenerror then
  833. exit;
  834. { Move flags and jump in register }
  835. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  836. location_force_reg(current_asmdata.CurrAsmList,hp.left.location,def_cgsize(hp.left.resultdef),false);
  837. if dovariant then
  838. begin
  839. { find the correct vtype value }
  840. vtype:=$ff;
  841. vaddr:=false;
  842. lt:=hp.left.resultdef;
  843. case lt.typ of
  844. enumdef,
  845. orddef :
  846. begin
  847. if is_64bit(lt) then
  848. begin
  849. case torddef(lt).ordtype of
  850. scurrency:
  851. vtype:=vtCurrency;
  852. s64bit:
  853. vtype:=vtInt64;
  854. u64bit:
  855. vtype:=vtQWord;
  856. end;
  857. freetemp:=false;
  858. vaddr:=true;
  859. end
  860. else if (lt.typ=enumdef) or
  861. is_integer(lt) then
  862. vtype:=vtInteger
  863. else
  864. if is_boolean(lt) then
  865. vtype:=vtBoolean
  866. else
  867. if (lt.typ=orddef) then
  868. begin
  869. case torddef(lt).ordtype of
  870. uchar:
  871. vtype:=vtChar;
  872. uwidechar:
  873. vtype:=vtWideChar;
  874. end;
  875. end;
  876. end;
  877. floatdef :
  878. begin
  879. if is_currency(lt) then
  880. vtype:=vtCurrency
  881. else
  882. vtype:=vtExtended;
  883. freetemp:=false;
  884. vaddr:=true;
  885. end;
  886. procvardef,
  887. pointerdef :
  888. begin
  889. if is_pchar(lt) then
  890. vtype:=vtPChar
  891. else if is_pwidechar(lt) then
  892. vtype:=vtPWideChar
  893. else
  894. vtype:=vtPointer;
  895. end;
  896. variantdef :
  897. begin
  898. vtype:=vtVariant;
  899. vaddr:=true;
  900. freetemp:=false;
  901. end;
  902. classrefdef :
  903. vtype:=vtClass;
  904. objectdef :
  905. if is_interface(lt) then
  906. vtype:=vtInterface
  907. { vtObject really means a class based on TObject }
  908. else if is_class(lt) then
  909. vtype:=vtObject
  910. else
  911. internalerror(200505171);
  912. stringdef :
  913. begin
  914. if is_shortstring(lt) then
  915. begin
  916. vtype:=vtString;
  917. vaddr:=true;
  918. freetemp:=false;
  919. end
  920. else
  921. if is_ansistring(lt) then
  922. begin
  923. vtype:=vtAnsiString;
  924. freetemp:=false;
  925. end
  926. else
  927. if is_widestring(lt) then
  928. begin
  929. vtype:=vtWideString;
  930. freetemp:=false;
  931. end;
  932. end;
  933. end;
  934. if vtype=$ff then
  935. internalerror(14357);
  936. { write changing field update href to the next element }
  937. inc(href.offset,sizeof(aint));
  938. if vaddr then
  939. begin
  940. location_force_mem(current_asmdata.CurrAsmList,hp.left.location);
  941. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  942. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.location.reference,tmpreg);
  943. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,href);
  944. end
  945. else
  946. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,hp.left.location,href);
  947. { update href to the vtype field and write it }
  948. dec(href.offset,sizeof(aint));
  949. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  950. { goto next array element }
  951. inc(href.offset,sizeof(aint)*2);
  952. end
  953. else
  954. { normal array constructor of the same type }
  955. begin
  956. if resultdef.needs_inittable then
  957. freetemp:=false;
  958. case hp.left.location.loc of
  959. LOC_MMREGISTER,
  960. LOC_CMMREGISTER:
  961. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,
  962. hp.left.location.register,href,mms_movescalar);
  963. LOC_FPUREGISTER,
  964. LOC_CFPUREGISTER :
  965. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,hp.left.location.register,href);
  966. LOC_REFERENCE,
  967. LOC_CREFERENCE :
  968. begin
  969. if is_shortstring(hp.left.resultdef) then
  970. cg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  971. Tstringdef(hp.left.resultdef).len)
  972. else
  973. cg.g_concatcopy(current_asmdata.CurrAsmList,hp.left.location.reference,href,elesize);
  974. end;
  975. else
  976. begin
  977. {$ifndef cpu64bit}
  978. if hp.left.location.size in [OS_64,OS_S64] then
  979. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  980. else
  981. {$endif cpu64bit}
  982. cg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location,href);
  983. end;
  984. end;
  985. inc(href.offset,elesize);
  986. end;
  987. if freetemp then
  988. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  989. end;
  990. { load next entry }
  991. hp:=tarrayconstructornode(hp.right);
  992. end;
  993. end;
  994. {*****************************************************************************
  995. SecondRTTI
  996. *****************************************************************************}
  997. procedure tcgrttinode.pass_generate_code;
  998. begin
  999. location_reset(location,LOC_CREFERENCE,OS_NO);
  1000. case rttidatatype of
  1001. rdt_normal:
  1002. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  1003. rdt_ord2str:
  1004. location.reference.symbol:=RTTIWriter.get_rtti_label_ord2str(rttidef,rttitype);
  1005. rdt_str2ord:
  1006. location.reference.symbol:=RTTIWriter.get_rtti_label_str2ord(rttidef,rttitype);
  1007. end;
  1008. end;
  1009. begin
  1010. cloadnode:=tcgloadnode;
  1011. cassignmentnode:=tcgassignmentnode;
  1012. carrayconstructornode:=tcgarrayconstructornode;
  1013. crttinode:=tcgrttinode;
  1014. end.