ncgld.pas 58 KB

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