ncgld.pas 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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,
  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 := cg.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(loadnf_inherited in loadnodeflags) 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)) 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),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,
  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)
  505. else
  506. location.reference.symbol:=current_asmdata.WeakRefAsmSymbol(procdef.mangledname);
  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. r64 : tregister64;
  529. oldflowcontrol : tflowcontrol;
  530. begin
  531. location_reset(location,LOC_VOID,OS_NO);
  532. { managed types should be handled in firstpass }
  533. if is_managed_type(left.resultdef) or is_managed_type(right.resultdef) then
  534. InternalError(2012011901);
  535. otlabel:=current_procinfo.CurrTrueLabel;
  536. oflabel:=current_procinfo.CurrFalseLabel;
  537. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  538. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  539. {
  540. in most cases we can process first the right node which contains
  541. the most complex code. Exceptions for this are:
  542. - result is in flags, loading left will then destroy the flags
  543. - result is a jump, loading left must be already done before the jump is made
  544. - result need reference count, when left points to a value used in
  545. right then decreasing the refcnt on left can possibly release
  546. the memory before right increased the refcnt, result is that an
  547. empty value is assigned
  548. But not when the result is in the flags, then
  549. loading the left node afterwards can destroy the flags.
  550. }
  551. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  552. (node_complexity(right)>node_complexity(left)) then
  553. begin
  554. secondpass(right);
  555. if codegenerror then
  556. exit;
  557. secondpass(left);
  558. if codegenerror then
  559. exit;
  560. end
  561. else
  562. begin
  563. { calculate left sides }
  564. secondpass(left);
  565. if codegenerror then
  566. exit;
  567. { tell the SSA/SSL code that the left side was handled first so
  568. ni SSL is done
  569. }
  570. oldflowcontrol:=flowcontrol;
  571. include(flowcontrol,fc_lefthandled);
  572. secondpass(right);
  573. flowcontrol:=oldflowcontrol;
  574. if codegenerror then
  575. exit;
  576. end;
  577. releaseright:=true;
  578. { shortstring assignments are handled separately }
  579. if is_shortstring(left.resultdef) then
  580. begin
  581. {
  582. we can get here only in the following situations
  583. for the right node:
  584. - empty constant string
  585. - char
  586. }
  587. { The addn is replaced by a blockn or calln that already returns
  588. a shortstring }
  589. if is_shortstring(right.resultdef) and
  590. (right.nodetype in [blockn,calln]) then
  591. begin
  592. { nothing to do }
  593. end
  594. { empty constant string }
  595. else if (right.nodetype=stringconstn) and
  596. (tstringconstnode(right).len=0) then
  597. begin
  598. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,0,left.location.reference);
  599. end
  600. { char loading }
  601. else if is_char(right.resultdef) then
  602. begin
  603. if right.nodetype=ordconstn then
  604. begin
  605. if (target_info.endian = endian_little) then
  606. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,(tordconstnode(right).value.svalue shl 8) or 1,
  607. setalignment(left.location.reference,1))
  608. else
  609. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,tordconstnode(right).value.svalue or (1 shl 8),
  610. setalignment(left.location.reference,1));
  611. end
  612. else
  613. begin
  614. href:=left.location.reference;
  615. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,1,href);
  616. inc(href.offset,1);
  617. case right.location.loc of
  618. LOC_REGISTER,
  619. LOC_CREGISTER :
  620. begin
  621. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  622. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,href);
  623. end;
  624. LOC_REFERENCE,
  625. LOC_CREFERENCE :
  626. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,href);
  627. else
  628. internalerror(200205111);
  629. end;
  630. end;
  631. end
  632. else
  633. internalerror(2002042410);
  634. end
  635. { try to reuse memory locations instead of copying }
  636. { copy to a memory location ... }
  637. else if (right.location.loc = LOC_REFERENCE) and
  638. maybechangetemp(current_asmdata.CurrAsmList,left,right.location.reference) then
  639. begin
  640. { if it worked, we're done }
  641. end
  642. else
  643. begin
  644. { SSA support }
  645. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  646. maybechangeloadnodereg(current_asmdata.CurrAsmList,right,true);
  647. case right.location.loc of
  648. LOC_CONSTANT :
  649. begin
  650. {$ifndef cpu64bitalu}
  651. if (left.location.size in [OS_64,OS_S64]) or (right.location.size in [OS_64,OS_S64]) then
  652. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  653. else
  654. {$endif not cpu64bitalu}
  655. cg.a_load_const_loc(current_asmdata.CurrAsmList,right.location.value,left.location);
  656. end;
  657. LOC_REFERENCE,
  658. LOC_CREFERENCE :
  659. begin
  660. case left.location.loc of
  661. LOC_REGISTER,
  662. LOC_CREGISTER :
  663. begin
  664. {$ifndef cpu64bitalu}
  665. if left.location.size in [OS_64,OS_S64] then
  666. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  667. else
  668. {$endif not cpu64bitalu}
  669. cg.a_load_ref_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.register);
  670. end;
  671. LOC_FPUREGISTER,
  672. LOC_CFPUREGISTER :
  673. begin
  674. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  675. right.location.size,left.location.size,
  676. right.location.reference,
  677. left.location.register);
  678. end;
  679. LOC_REFERENCE,
  680. LOC_CREFERENCE :
  681. begin
  682. if (left.resultdef.typ=floatdef) and
  683. (right.resultdef.typ=floatdef) and
  684. (left.location.size<>right.location.size) then
  685. begin
  686. cg.a_loadfpu_ref_ref(current_asmdata.CurrAsmList,
  687. right.location.size,left.location.size,
  688. right.location.reference,left.location.reference)
  689. end
  690. else
  691. begin
  692. { TODO: HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  693. { Use unaligned copy when the offset is not aligned }
  694. len:=left.resultdef.size;
  695. { data smaller than an aint has less alignment requirements }
  696. { max(1,...) avoids div by zero in case of an empty record }
  697. alignmentrequirement:=min(max(1,len),sizeof(aint));
  698. if (right.location.reference.offset mod alignmentrequirement<>0) or
  699. (left.location.reference.offset mod alignmentrequirement<>0) or
  700. (right.resultdef.alignment<alignmentrequirement) or
  701. ((right.location.reference.alignment<>0) and
  702. (right.location.reference.alignment<alignmentrequirement)) or
  703. ((left.location.reference.alignment<>0) and
  704. (left.location.reference.alignment<alignmentrequirement)) then
  705. cg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len)
  706. else
  707. cg.g_concatcopy(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len);
  708. end;
  709. end;
  710. LOC_MMREGISTER,
  711. LOC_CMMREGISTER:
  712. begin
  713. {$ifdef x86}
  714. if (right.resultdef.typ=floatdef) and
  715. not use_vectorfpu(right.resultdef) then
  716. begin
  717. { perform size conversion if needed (the mm-code cannot }
  718. { convert an extended into a double/single, since sse }
  719. { doesn't support extended) }
  720. r:=cg.getfpuregister(current_asmdata.CurrAsmList,right.location.size);
  721. tg.gettemp(current_asmdata.CurrAsmList,left.resultdef.size,left.resultdef.alignment,tt_normal,href);
  722. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,right.location.size,right.location.size,right.location.reference,r);
  723. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,r,href);
  724. if releaseright then
  725. location_freetemp(current_asmdata.CurrAsmList,right.location);
  726. releaseright:=true;
  727. location_reset_ref(right.location,LOC_REFERENCE,left.location.size,0);
  728. right.location.reference:=href;
  729. end;
  730. {$endif}
  731. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  732. right.location.size,
  733. left.location.size,
  734. right.location.reference,
  735. left.location.register,mms_movescalar);
  736. end;
  737. LOC_SUBSETREG,
  738. LOC_CSUBSETREG:
  739. cg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sreg);
  740. LOC_SUBSETREF,
  741. LOC_CSUBSETREF:
  742. {$ifndef cpu64bitalu}
  743. if right.location.size in [OS_64,OS_S64] then
  744. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  745. else
  746. {$endif not cpu64bitalu}
  747. cg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sref);
  748. else
  749. internalerror(200203284);
  750. end;
  751. end;
  752. {$ifdef SUPPORT_MMX}
  753. LOC_CMMXREGISTER,
  754. LOC_MMXREGISTER:
  755. begin
  756. if left.location.loc=LOC_CMMXREGISTER then
  757. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  758. else
  759. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  760. end;
  761. {$endif SUPPORT_MMX}
  762. LOC_MMREGISTER,
  763. LOC_CMMREGISTER:
  764. begin
  765. if left.resultdef.typ=arraydef then
  766. begin
  767. end
  768. else
  769. begin
  770. case left.location.loc of
  771. LOC_CMMREGISTER,
  772. LOC_MMREGISTER:
  773. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar);
  774. LOC_REFERENCE,
  775. LOC_CREFERENCE:
  776. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  777. else
  778. internalerror(2009112601);
  779. end;
  780. end;
  781. end;
  782. LOC_REGISTER,
  783. LOC_CREGISTER :
  784. begin
  785. {$ifndef cpu64bitalu}
  786. { also OS_F64 in case of mmreg -> intreg }
  787. if left.location.size in [OS_64,OS_S64,OS_F64] then
  788. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  789. right.location.register64,left.location)
  790. else
  791. {$endif not cpu64bitalu}
  792. cg.a_load_reg_loc(current_asmdata.CurrAsmList,right.location.size,right.location.register,left.location);
  793. end;
  794. LOC_FPUREGISTER,
  795. LOC_CFPUREGISTER :
  796. begin
  797. { we can't do direct moves between fpu and mm registers }
  798. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  799. begin
  800. {$ifdef x86}
  801. if not use_vectorfpu(right.resultdef) then
  802. begin
  803. { perform size conversion if needed (the mm-code cannot convert an }
  804. { extended into a double/single, since sse doesn't support extended) }
  805. tg.gettemp(current_asmdata.CurrAsmList,left.resultdef.size,left.resultdef.alignment,tt_normal,href);
  806. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,href);
  807. location_reset_ref(right.location,LOC_REFERENCE,left.location.size,0);
  808. right.location.reference:=href;
  809. end;
  810. {$endif}
  811. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,false);
  812. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  813. right.location.size,left.location.size,
  814. right.location.register,left.location.register,mms_movescalar);
  815. end
  816. else
  817. cg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  818. right.location.size,
  819. right.location.register,left.location);
  820. end;
  821. LOC_SUBSETREG,
  822. LOC_CSUBSETREG:
  823. begin
  824. cg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  825. right.location.size,right.location.sreg,left.location);
  826. end;
  827. LOC_SUBSETREF,
  828. LOC_CSUBSETREF:
  829. begin
  830. {$ifndef cpu64bitalu}
  831. if right.location.size in [OS_64,OS_S64] then
  832. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  833. else
  834. {$endif not cpu64bitalu}
  835. cg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  836. right.location.size,right.location.sref,left.location);
  837. end;
  838. LOC_JUMP :
  839. begin
  840. current_asmdata.getjumplabel(hlabel);
  841. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  842. if is_pasbool(left.resultdef) then
  843. begin
  844. {$ifndef cpu64bitalu}
  845. if left.location.size in [OS_64,OS_S64] then
  846. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,1,left.location)
  847. else
  848. {$endif not cpu64bitalu}
  849. cg.a_load_const_loc(current_asmdata.CurrAsmList,1,left.location)
  850. end
  851. else
  852. begin
  853. {$ifndef cpu64bitalu}
  854. if left.location.size in [OS_64,OS_S64] then
  855. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,-1,left.location)
  856. else
  857. {$endif not cpu64bitalu}
  858. cg.a_load_const_loc(current_asmdata.CurrAsmList,-1,left.location);
  859. end;
  860. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  861. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  862. {$ifndef cpu64bitalu}
  863. if left.location.size in [OS_64,OS_S64] then
  864. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,0,left.location)
  865. else
  866. {$endif not cpu64bitalu}
  867. cg.a_load_const_loc(current_asmdata.CurrAsmList,0,left.location);
  868. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  869. end;
  870. {$ifdef cpuflags}
  871. LOC_FLAGS :
  872. begin
  873. if is_pasbool(left.resultdef) then
  874. begin
  875. case left.location.loc of
  876. LOC_REGISTER,LOC_CREGISTER:
  877. {$ifdef cpu32bitalu}
  878. if left.location.size in [OS_S64,OS_64] then
  879. begin
  880. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,left.location.register64.reglo);
  881. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,left.location.register64.reghi);
  882. end
  883. else
  884. {$endif cpu32bitalu}
  885. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,left.location.register);
  886. LOC_REFERENCE:
  887. { i386 has a hack in its code generator so that it can
  888. deal with 64 bit locations in this parcticular case }
  889. {$if defined(cpu32bitalu) and not defined(x86)}
  890. if left.location.size in [OS_S64,OS_64] then
  891. begin
  892. r64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  893. r64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  894. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,r64.reglo);
  895. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  896. cg64.a_load64_reg_ref(current_asmdata.CurrAsmList,r64,left.location.reference);
  897. end
  898. else
  899. {$endif cpu32bitalu}
  900. cg.g_flags2ref(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,left.location.reference);
  901. LOC_SUBSETREG,LOC_SUBSETREF:
  902. begin
  903. r:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  904. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,r);
  905. cg.a_load_reg_loc(current_asmdata.CurrAsmList,left.location.size,r,left.location);
  906. end;
  907. else
  908. internalerror(200203273);
  909. end;
  910. end
  911. else
  912. begin
  913. {$ifdef cpu32bitalu}
  914. if left.location.size in [OS_S64,OS_64] then
  915. begin
  916. r64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  917. r64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  918. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,right.location.resflags,r64.reglo);
  919. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,r64.reghi);
  920. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_S64,
  921. r64,r64);
  922. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,r64,left.location);
  923. end
  924. else
  925. {$endif cpu32bitalu}
  926. begin
  927. r:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  928. cg.g_flags2reg(current_asmdata.CurrAsmList,left.location.size,right.location.resflags,r);
  929. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,left.location.size,r,r);
  930. cg.a_load_reg_loc(current_asmdata.CurrAsmList,left.location.size,r,left.location);
  931. end
  932. end;
  933. end;
  934. {$endif cpuflags}
  935. end;
  936. end;
  937. if releaseright then
  938. location_freetemp(current_asmdata.CurrAsmList,right.location);
  939. current_procinfo.CurrTrueLabel:=otlabel;
  940. current_procinfo.CurrFalseLabel:=oflabel;
  941. end;
  942. {*****************************************************************************
  943. SecondArrayConstruct
  944. *****************************************************************************}
  945. const
  946. vtInteger = 0;
  947. vtBoolean = 1;
  948. vtChar = 2;
  949. vtExtended = 3;
  950. vtString = 4;
  951. vtPointer = 5;
  952. vtPChar = 6;
  953. vtObject = 7;
  954. vtClass = 8;
  955. vtWideChar = 9;
  956. vtPWideChar = 10;
  957. vtAnsiString32 = 11;
  958. vtCurrency = 12;
  959. vtVariant = 13;
  960. vtInterface = 14;
  961. vtWideString = 15;
  962. vtInt64 = 16;
  963. vtQWord = 17;
  964. vtUnicodeString = 18;
  965. vtAnsiString16 = 19;
  966. vtAnsiString64 = 20;
  967. procedure tcgarrayconstructornode.pass_generate_code;
  968. var
  969. hp : tarrayconstructornode;
  970. href : treference;
  971. lt : tdef;
  972. paraloc : tcgparalocation;
  973. otlabel,
  974. oflabel : tasmlabel;
  975. vtype : longint;
  976. elesize,
  977. elealign : longint;
  978. tmpreg : tregister;
  979. vaddr : boolean;
  980. freetemp,
  981. dovariant : boolean;
  982. begin
  983. if is_packed_array(resultdef) then
  984. internalerror(200608042);
  985. dovariant:=(nf_forcevaria in flags) or is_variant_array(resultdef);
  986. if dovariant then
  987. begin
  988. elesize:=sizeof(pint)+sizeof(pint);
  989. elealign:=sizeof(pint);
  990. end
  991. else
  992. begin
  993. elesize:=tarraydef(resultdef).elesize;
  994. elealign:=tarraydef(resultdef).elementdef.alignment;
  995. end;
  996. { alignment is filled in by tg.gettemp below }
  997. location_reset_ref(location,LOC_CREFERENCE,OS_NO,0);
  998. fillchar(paraloc,sizeof(paraloc),0);
  999. { Allocate always a temp, also if no elements are required, to
  1000. be sure that location is valid (PFV) }
  1001. if tarraydef(resultdef).highrange=-1 then
  1002. tg.GetTemp(current_asmdata.CurrAsmList,elesize,elealign,tt_normal,location.reference)
  1003. else
  1004. tg.GetTemp(current_asmdata.CurrAsmList,(tarraydef(resultdef).highrange+1)*elesize,resultdef.alignment,tt_normal,location.reference);
  1005. href:=location.reference;
  1006. { Process nodes in array constructor }
  1007. hp:=self;
  1008. while assigned(hp) do
  1009. begin
  1010. if assigned(hp.left) then
  1011. begin
  1012. freetemp:=true;
  1013. if (hp.left.expectloc=LOC_JUMP) then
  1014. begin
  1015. otlabel:=current_procinfo.CurrTrueLabel;
  1016. oflabel:=current_procinfo.CurrFalseLabel;
  1017. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  1018. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  1019. end;
  1020. secondpass(hp.left);
  1021. { Move flags and jump in register }
  1022. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  1023. location_force_reg(current_asmdata.CurrAsmList,hp.left.location,def_cgsize(hp.left.resultdef),false);
  1024. if (hp.left.location.loc=LOC_JUMP) then
  1025. begin
  1026. if (hp.left.expectloc<>LOC_JUMP) then
  1027. internalerror(2007103101);
  1028. current_procinfo.CurrTrueLabel:=otlabel;
  1029. current_procinfo.CurrFalseLabel:=oflabel;
  1030. end;
  1031. if dovariant then
  1032. begin
  1033. { find the correct vtype value }
  1034. vtype:=$ff;
  1035. vaddr:=false;
  1036. lt:=hp.left.resultdef;
  1037. case lt.typ of
  1038. enumdef,
  1039. orddef :
  1040. begin
  1041. if is_64bit(lt) then
  1042. begin
  1043. case torddef(lt).ordtype of
  1044. scurrency:
  1045. vtype:=vtCurrency;
  1046. s64bit:
  1047. vtype:=vtInt64;
  1048. u64bit:
  1049. vtype:=vtQWord;
  1050. end;
  1051. freetemp:=false;
  1052. vaddr:=true;
  1053. end
  1054. else if (lt.typ=enumdef) or
  1055. is_integer(lt) then
  1056. vtype:=vtInteger
  1057. else
  1058. if is_boolean(lt) then
  1059. vtype:=vtBoolean
  1060. else
  1061. if (lt.typ=orddef) then
  1062. begin
  1063. case torddef(lt).ordtype of
  1064. uchar:
  1065. vtype:=vtChar;
  1066. uwidechar:
  1067. vtype:=vtWideChar;
  1068. end;
  1069. end;
  1070. end;
  1071. floatdef :
  1072. begin
  1073. if is_currency(lt) then
  1074. vtype:=vtCurrency
  1075. else
  1076. vtype:=vtExtended;
  1077. freetemp:=false;
  1078. vaddr:=true;
  1079. end;
  1080. procvardef,
  1081. pointerdef :
  1082. begin
  1083. if is_pchar(lt) then
  1084. vtype:=vtPChar
  1085. else if is_pwidechar(lt) then
  1086. vtype:=vtPWideChar
  1087. else
  1088. vtype:=vtPointer;
  1089. end;
  1090. variantdef :
  1091. begin
  1092. vtype:=vtVariant;
  1093. vaddr:=true;
  1094. freetemp:=false;
  1095. end;
  1096. classrefdef :
  1097. vtype:=vtClass;
  1098. objectdef :
  1099. if is_interface(lt) then
  1100. vtype:=vtInterface
  1101. { vtObject really means a class based on TObject }
  1102. else if is_class(lt) then
  1103. vtype:=vtObject
  1104. else
  1105. internalerror(200505171);
  1106. stringdef :
  1107. begin
  1108. if is_shortstring(lt) then
  1109. begin
  1110. vtype:=vtString;
  1111. vaddr:=true;
  1112. freetemp:=false;
  1113. end
  1114. else
  1115. if is_ansistring(lt) then
  1116. begin
  1117. vtype:=vtAnsiString;
  1118. freetemp:=false;
  1119. end
  1120. else
  1121. if is_widestring(lt) then
  1122. begin
  1123. vtype:=vtWideString;
  1124. freetemp:=false;
  1125. end
  1126. else
  1127. if is_unicodestring(lt) then
  1128. begin
  1129. vtype:=vtUnicodeString;
  1130. freetemp:=false;
  1131. end;
  1132. end;
  1133. end;
  1134. if vtype=$ff then
  1135. internalerror(14357);
  1136. { write changing field update href to the next element }
  1137. inc(href.offset,sizeof(pint));
  1138. if vaddr then
  1139. begin
  1140. location_force_mem(current_asmdata.CurrAsmList,hp.left.location);
  1141. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  1142. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.location.reference,tmpreg);
  1143. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,href);
  1144. end
  1145. else
  1146. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,hp.left.location,href);
  1147. { update href to the vtype field and write it }
  1148. dec(href.offset,sizeof(pint));
  1149. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  1150. { goto next array element }
  1151. inc(href.offset,sizeof(pint)*2);
  1152. end
  1153. else
  1154. { normal array constructor of the same type }
  1155. begin
  1156. if is_managed_type(resultdef) then
  1157. freetemp:=false;
  1158. case hp.left.location.loc of
  1159. LOC_MMREGISTER,
  1160. LOC_CMMREGISTER:
  1161. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,
  1162. hp.left.location.register,href,mms_movescalar);
  1163. LOC_FPUREGISTER,
  1164. LOC_CFPUREGISTER :
  1165. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,hp.left.location.register,href);
  1166. LOC_REFERENCE,
  1167. LOC_CREFERENCE :
  1168. begin
  1169. if is_shortstring(hp.left.resultdef) then
  1170. cg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  1171. Tstringdef(hp.left.resultdef).len)
  1172. else
  1173. cg.g_concatcopy(current_asmdata.CurrAsmList,hp.left.location.reference,href,elesize);
  1174. end;
  1175. else
  1176. begin
  1177. {$ifndef cpu64bitalu}
  1178. if hp.left.location.size in [OS_64,OS_S64] then
  1179. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  1180. else
  1181. {$endif not cpu64bitalu}
  1182. cg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location,href);
  1183. end;
  1184. end;
  1185. inc(href.offset,elesize);
  1186. end;
  1187. if freetemp then
  1188. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  1189. end;
  1190. { load next entry }
  1191. hp:=tarrayconstructornode(hp.right);
  1192. end;
  1193. end;
  1194. {*****************************************************************************
  1195. SecondRTTI
  1196. *****************************************************************************}
  1197. procedure tcgrttinode.pass_generate_code;
  1198. begin
  1199. location_reset_ref(location,LOC_CREFERENCE,OS_NO,sizeof(pint));
  1200. case rttidatatype of
  1201. rdt_normal:
  1202. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  1203. rdt_ord2str:
  1204. location.reference.symbol:=RTTIWriter.get_rtti_label_ord2str(rttidef,rttitype);
  1205. rdt_str2ord:
  1206. location.reference.symbol:=RTTIWriter.get_rtti_label_str2ord(rttidef,rttitype);
  1207. end;
  1208. end;
  1209. begin
  1210. cloadnode:=tcgloadnode;
  1211. cassignmentnode:=tcgassignmentnode;
  1212. carrayconstructornode:=tcgarrayconstructornode;
  1213. crttinode:=tcgrttinode;
  1214. end.