ncgld.pas 59 KB

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