ncgld.pas 60 KB

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