ncgld.pas 63 KB

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