ncgld.pas 61 KB

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