ncgld.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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;
  23. type
  24. tcgloadnode = class(tloadnode)
  25. procedure pass_generate_code;override;
  26. procedure generate_picvaraccess;virtual;
  27. end;
  28. tcgassignmentnode = class(tassignmentnode)
  29. procedure pass_generate_code;override;
  30. end;
  31. tcgarrayconstructornode = class(tarrayconstructornode)
  32. procedure pass_generate_code;override;
  33. end;
  34. tcgrttinode = class(trttinode)
  35. procedure pass_generate_code;override;
  36. end;
  37. implementation
  38. uses
  39. cutils,
  40. systems,
  41. verbose,globtype,globals,
  42. symconst,symtype,symdef,symsym,defutil,paramgr,
  43. ncnv,ncon,nmem,nbas,ncgrtti,
  44. aasmbase,aasmtai,aasmdata,aasmcpu,
  45. cgbase,pass_2,
  46. procinfo,
  47. cpubase,parabase,
  48. tgobj,ncgutil,
  49. cgutils,cgobj,
  50. ncgbas,ncgflw;
  51. {*****************************************************************************
  52. SecondLoad
  53. *****************************************************************************}
  54. procedure tcgloadnode.generate_picvaraccess;
  55. begin
  56. {$ifndef sparc}
  57. location.reference.base:=current_procinfo.got;
  58. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname+'@GOT');
  59. {$endif sparc}
  60. end;
  61. procedure tcgloadnode.pass_generate_code;
  62. var
  63. hregister : tregister;
  64. vs : tabstractnormalvarsym;
  65. gvs : tstaticvarsym;
  66. pd : tprocdef;
  67. href : treference;
  68. newsize : tcgsize;
  69. endrelocatelab,
  70. norelocatelab : tasmlabel;
  71. paraloc1 : tcgpara;
  72. begin
  73. { we don't know the size of all arrays }
  74. newsize:=def_cgsize(resultdef);
  75. location_reset(location,LOC_REFERENCE,newsize);
  76. case symtableentry.typ of
  77. absolutevarsym :
  78. begin
  79. { this is only for toasm and toaddr }
  80. case tabsolutevarsym(symtableentry).abstyp of
  81. toaddr :
  82. begin
  83. {$ifdef i386}
  84. if tabsolutevarsym(symtableentry).absseg then
  85. location.reference.segment:=NR_FS;
  86. {$endif i386}
  87. location.reference.offset:=tabsolutevarsym(symtableentry).addroffset;
  88. end;
  89. toasm :
  90. location.reference.symbol:=current_asmdata.RefAsmSymbol(tabsolutevarsym(symtableentry).mangledname);
  91. else
  92. internalerror(200310283);
  93. end;
  94. end;
  95. constsym:
  96. begin
  97. if tconstsym(symtableentry).consttyp=constresourcestring then
  98. begin
  99. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  100. location.reference.symbol:=current_asmdata.RefAsmSymbol(make_mangledname('RESSTR',symtableentry.owner,symtableentry.name));
  101. { Resourcestring layout:
  102. TResourceStringRecord = Packed Record
  103. Name,
  104. CurrentValue,
  105. DefaultValue : AnsiString;
  106. HashValue : LongWord;
  107. end;
  108. }
  109. location.reference.offset:=sizeof(aint);
  110. end
  111. else
  112. internalerror(22798);
  113. end;
  114. staticvarsym :
  115. begin
  116. gvs:=tstaticvarsym(symtableentry);
  117. if ([vo_is_dll_var,vo_is_external] * gvs.varoptions <> []) then
  118. begin
  119. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,tstaticvarsym(symtableentry).mangledname);
  120. if (location.reference.base <> NR_NO) then
  121. exit;
  122. end;
  123. if (vo_is_dll_var in gvs.varoptions) then
  124. { DLL variable }
  125. begin
  126. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  127. location.reference.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname);
  128. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,location.reference,hregister);
  129. reference_reset_base(location.reference,hregister,0);
  130. end
  131. { Thread variable }
  132. else if (vo_is_thread_var in gvs.varoptions) and
  133. not(tf_section_threadvars in target_info.flags) then
  134. begin
  135. if (tf_section_threadvars in target_info.flags) then
  136. begin
  137. if gvs.localloc.loc=LOC_INVALID then
  138. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0)
  139. else
  140. location:=gvs.localloc;
  141. {$ifdef i386}
  142. case target_info.system of
  143. system_i386_linux:
  144. location.reference.segment:=NR_GS;
  145. system_i386_win32:
  146. location.reference.segment:=NR_FS;
  147. end;
  148. {$endif i386}
  149. end
  150. else
  151. begin
  152. {
  153. Thread var loading is optimized to first check if
  154. a relocate function is available. When the function
  155. is available it is called to retrieve the address.
  156. Otherwise the address is loaded with the symbol
  157. The code needs to be in the order to first handle the
  158. call and then the address load to be sure that the
  159. register that is used for returning is the same (PFV)
  160. }
  161. current_asmdata.getjumplabel(norelocatelab);
  162. current_asmdata.getjumplabel(endrelocatelab);
  163. { make sure hregister can't allocate the register necessary for the parameter }
  164. paraloc1.init;
  165. paramanager.getintparaloc(pocall_default,1,paraloc1);
  166. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  167. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_THREADVAR_RELOCATE'),0);
  168. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  169. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,hregister,norelocatelab);
  170. { don't save the allocated register else the result will be destroyed later }
  171. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname),0);
  172. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  173. cg.a_param_ref(current_asmdata.CurrAsmList,OS_32,href,paraloc1);
  174. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  175. paraloc1.done;
  176. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  177. cg.a_call_reg(current_asmdata.CurrAsmList,hregister);
  178. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  179. cg.getcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  180. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  181. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  182. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  183. cg.a_jmp_always(current_asmdata.CurrAsmList,endrelocatelab);
  184. cg.a_label(current_asmdata.CurrAsmList,norelocatelab);
  185. { no relocation needed, load the address of the variable only, the
  186. layout of a threadvar is (4 bytes pointer):
  187. 0 - Threadvar index
  188. 4 - Threadvar value in single threading }
  189. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(tstaticvarsym(symtableentry).mangledname),sizeof(aint));
  190. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  191. cg.a_label(current_asmdata.CurrAsmList,endrelocatelab);
  192. location.reference.base:=hregister;
  193. end;
  194. end
  195. { Normal (or external) variable }
  196. else
  197. begin
  198. if gvs.localloc.loc=LOC_INVALID then
  199. reference_reset_symbol(location.reference,current_asmdata.RefAsmSymbol(gvs.mangledname),0)
  200. else
  201. location:=gvs.localloc;
  202. end;
  203. { make const a LOC_CREFERENCE }
  204. if (gvs.varspez=vs_const) and
  205. (location.loc=LOC_REFERENCE) then
  206. location.loc:=LOC_CREFERENCE;
  207. end;
  208. paravarsym,
  209. localvarsym :
  210. begin
  211. vs:=tabstractnormalvarsym(symtableentry);
  212. { Nested variable }
  213. if assigned(left) then
  214. begin
  215. secondpass(left);
  216. if left.location.loc<>LOC_REGISTER then
  217. internalerror(200309286);
  218. if vs.localloc.loc<>LOC_REFERENCE then
  219. internalerror(200409241);
  220. reference_reset_base(location.reference,left.location.register,vs.localloc.reference.offset);
  221. end
  222. else
  223. location:=vs.localloc;
  224. { handle call by reference variables when they are not
  225. alreayd copied to local copies. Also ignore the reference
  226. when we need to load the self pointer for objects }
  227. if is_addr_param_load then
  228. begin
  229. if (location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  230. hregister:=location.register
  231. else
  232. begin
  233. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  234. { we need to load only an address }
  235. location.size:=OS_ADDR;
  236. cg.a_load_loc_reg(current_asmdata.CurrAsmList,location.size,location,hregister);
  237. end;
  238. location_reset(location,LOC_REFERENCE,newsize);
  239. location.reference.base:=hregister;
  240. end;
  241. { make const a LOC_CREFERENCE }
  242. if (vs.varspez=vs_const) and
  243. (location.loc=LOC_REFERENCE) then
  244. location.loc:=LOC_CREFERENCE;
  245. end;
  246. procsym:
  247. begin
  248. if not assigned(procdef) then
  249. internalerror(200312011);
  250. if assigned(left) then
  251. begin
  252. if (sizeof(aint) = 4) then
  253. location_reset(location,LOC_CREFERENCE,OS_64)
  254. else if (sizeof(aint) = 8) then
  255. location_reset(location,LOC_CREFERENCE,OS_128)
  256. else
  257. internalerror(20020520);
  258. tg.GetTemp(current_asmdata.CurrAsmList,2*sizeof(aint),tt_normal,location.reference);
  259. secondpass(left);
  260. { load class instance/classrefdef address }
  261. if left.location.loc=LOC_CONSTANT then
  262. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_ADDR,false);
  263. case left.location.loc of
  264. LOC_CREGISTER,
  265. LOC_REGISTER:
  266. begin
  267. { this is not possible for objects }
  268. if is_object(left.resultdef) then
  269. internalerror(200304234);
  270. hregister:=left.location.register;
  271. end;
  272. LOC_CREFERENCE,
  273. LOC_REFERENCE:
  274. begin
  275. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  276. if not is_object(left.resultdef) then
  277. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,hregister)
  278. else
  279. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,hregister);
  280. location_freetemp(current_asmdata.CurrAsmList,left.location);
  281. end;
  282. else
  283. internalerror(200610311);
  284. end;
  285. { store the class instance or classredef address }
  286. href:=location.reference;
  287. inc(href.offset,sizeof(aint));
  288. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,href);
  289. { virtual method ? }
  290. if (po_virtualmethod in procdef.procoptions) and
  291. not(nf_inherited in flags) then
  292. begin
  293. { a classrefdef already points to the VMT }
  294. if (left.resultdef.typ<>classrefdef) then
  295. begin
  296. { load vmt pointer }
  297. reference_reset_base(href,hregister,0);
  298. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  299. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  300. end;
  301. { load method address }
  302. reference_reset_base(href,hregister,procdef._class.vmtmethodoffset(procdef.extnumber));
  303. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  304. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  305. { ... and store it }
  306. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  307. end
  308. else
  309. begin
  310. { load address of the function }
  311. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0);
  312. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  313. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  314. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  315. end;
  316. end
  317. else
  318. begin
  319. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  320. if (po_external in pd.procoptions) then
  321. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,pd.mangledname);
  322. {!!!!! Be aware, work on virtual methods too }
  323. if (location.reference.base = NR_NO) then
  324. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  325. end;
  326. end;
  327. labelsym :
  328. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  329. else internalerror(200510032);
  330. end;
  331. end;
  332. {*****************************************************************************
  333. SecondAssignment
  334. *****************************************************************************}
  335. procedure tcgassignmentnode.pass_generate_code;
  336. var
  337. otlabel,hlabel,oflabel : tasmlabel;
  338. href : treference;
  339. releaseright : boolean;
  340. len : aint;
  341. r:Tregister;
  342. begin
  343. location_reset(location,LOC_VOID,OS_NO);
  344. otlabel:=current_procinfo.CurrTrueLabel;
  345. oflabel:=current_procinfo.CurrFalseLabel;
  346. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  347. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  348. {
  349. in most cases we can process first the right node which contains
  350. the most complex code. Exceptions for this are:
  351. - result is in flags, loading left will then destroy the flags
  352. - result is a jump, loading left must be already done before the jump is made
  353. - result need reference count, when left points to a value used in
  354. right then decreasing the refcnt on left can possibly release
  355. the memory before right increased the refcnt, result is that an
  356. empty value is assigned
  357. - calln, call destroys most registers and is therefor 'complex'
  358. But not when the result is in the flags, then
  359. loading the left node afterwards can destroy the flags.
  360. }
  361. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  362. ((right.nodetype=calln) or
  363. (right.resultdef.needs_inittable) or
  364. (right.registersint>=left.registersint)) then
  365. begin
  366. secondpass(right);
  367. { increment source reference counter, this is
  368. useless for constants }
  369. if (right.resultdef.needs_inittable) and
  370. not is_constnode(right) then
  371. begin
  372. location_force_mem(current_asmdata.CurrAsmList,right.location);
  373. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  374. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  375. end;
  376. if codegenerror then
  377. exit;
  378. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  379. { can be false }
  380. secondpass(left);
  381. { decrement destination reference counter }
  382. if (left.resultdef.needs_inittable) then
  383. begin
  384. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  385. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  386. end;
  387. if codegenerror then
  388. exit;
  389. end
  390. else
  391. begin
  392. { calculate left sides }
  393. secondpass(left);
  394. { decrement destination reference counter }
  395. if (left.resultdef.needs_inittable) then
  396. begin
  397. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  398. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  399. end;
  400. if codegenerror then
  401. exit;
  402. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  403. { can be false }
  404. secondpass(right);
  405. { increment source reference counter, this is
  406. useless for string constants}
  407. if (right.resultdef.needs_inittable) and
  408. (right.nodetype<>stringconstn) then
  409. begin
  410. location_force_mem(current_asmdata.CurrAsmList,right.location);
  411. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  412. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  413. end;
  414. if codegenerror then
  415. exit;
  416. end;
  417. releaseright:=true;
  418. { optimize temp to temp copies }
  419. {$ifdef old_append_str}
  420. if (left.nodetype = temprefn) and
  421. { we may store certain temps in registers in the future, then this }
  422. { optimization will have to be adapted }
  423. (left.location.loc = LOC_REFERENCE) and
  424. (right.location.loc = LOC_REFERENCE) and
  425. tg.istemp(right.location.reference) and
  426. (tg.sizeoftemp(current_asmdata.CurrAsmList,right.location.reference) = tg.sizeoftemp(current_asmdata.CurrAsmList,left.location.reference)) then
  427. begin
  428. { in theory, we should also make sure the left temp type is }
  429. { already more or less of the same kind (ie. we must not }
  430. { assign an ansistring to a normaltemp). In practice, the }
  431. { assignment node will have already taken care of this for us }
  432. tcgtemprefnode(left).changelocation(right.location.reference);
  433. end
  434. { shortstring assignments are handled separately }
  435. else
  436. {$endif old_append_str}
  437. if is_shortstring(left.resultdef) then
  438. begin
  439. {
  440. we can get here only in the following situations
  441. for the right node:
  442. - empty constant string
  443. - char
  444. }
  445. { The addn is replaced by a blockn or calln that already returns
  446. a shortstring }
  447. if is_shortstring(right.resultdef) and
  448. (right.nodetype in [blockn,calln]) then
  449. begin
  450. { nothing to do }
  451. end
  452. { empty constant string }
  453. else if (right.nodetype=stringconstn) and
  454. (tstringconstnode(right).len=0) then
  455. begin
  456. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,0,left.location.reference);
  457. end
  458. { char loading }
  459. else if is_char(right.resultdef) then
  460. begin
  461. if right.nodetype=ordconstn then
  462. begin
  463. if (target_info.endian = endian_little) then
  464. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,(tordconstnode(right).value shl 8) or 1,
  465. left.location.reference)
  466. else
  467. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,tordconstnode(right).value or (1 shl 8),
  468. left.location.reference);
  469. end
  470. else
  471. begin
  472. href:=left.location.reference;
  473. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,1,href);
  474. inc(href.offset,1);
  475. case right.location.loc of
  476. LOC_REGISTER,
  477. LOC_CREGISTER :
  478. begin
  479. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  480. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,href);
  481. end;
  482. LOC_REFERENCE,
  483. LOC_CREFERENCE :
  484. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,href);
  485. else
  486. internalerror(200205111);
  487. end;
  488. end;
  489. end
  490. else
  491. internalerror(200204249);
  492. end
  493. else
  494. begin
  495. { SSA support }
  496. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  497. maybechangeloadnodereg(current_asmdata.CurrAsmList,right,true);
  498. case right.location.loc of
  499. LOC_CONSTANT :
  500. begin
  501. {$ifndef cpu64bit}
  502. if (left.location.size in [OS_64,OS_S64]) or (right.location.size in [OS_64,OS_S64]) then
  503. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  504. else
  505. {$endif cpu64bit}
  506. cg.a_load_const_loc(current_asmdata.CurrAsmList,right.location.value,left.location);
  507. end;
  508. LOC_REFERENCE,
  509. LOC_CREFERENCE :
  510. begin
  511. case left.location.loc of
  512. LOC_REGISTER,
  513. LOC_CREGISTER :
  514. begin
  515. {$ifndef cpu64bit}
  516. if left.location.size in [OS_64,OS_S64] then
  517. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  518. else
  519. {$endif cpu64bit}
  520. cg.a_load_ref_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.register);
  521. end;
  522. LOC_FPUREGISTER,
  523. LOC_CFPUREGISTER :
  524. begin
  525. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  526. right.location.size,left.location.size,
  527. right.location.reference,
  528. left.location.register);
  529. end;
  530. LOC_REFERENCE,
  531. LOC_CREFERENCE :
  532. begin
  533. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  534. { Use unaligned copy when the offset is not aligned }
  535. len:=left.resultdef.size;
  536. if (right.location.reference.offset mod sizeof(aint)<>0) or
  537. (left.location.reference.offset mod sizeof(aint)<>0) or
  538. (right.resultdef.alignment<sizeof(aint)) then
  539. cg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len)
  540. else
  541. cg.g_concatcopy(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len);
  542. end;
  543. LOC_MMREGISTER,
  544. LOC_CMMREGISTER:
  545. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  546. right.location.size,
  547. left.location.size,
  548. right.location.reference,
  549. left.location.register,mms_movescalar);
  550. LOC_SUBSETREG,
  551. LOC_CSUBSETREG:
  552. cg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sreg);
  553. LOC_SUBSETREF,
  554. LOC_CSUBSETREF:
  555. {$ifndef cpu64bit}
  556. if right.location.size in [OS_64,OS_S64] then
  557. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  558. else
  559. {$endif cpu64bit}
  560. cg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sref);
  561. else
  562. internalerror(200203284);
  563. end;
  564. end;
  565. {$ifdef SUPPORT_MMX}
  566. LOC_CMMXREGISTER,
  567. LOC_MMXREGISTER:
  568. begin
  569. if left.location.loc=LOC_CMMXREGISTER then
  570. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  571. else
  572. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  573. end;
  574. {$endif SUPPORT_MMX}
  575. LOC_MMREGISTER,
  576. LOC_CMMREGISTER:
  577. begin
  578. if left.resultdef.typ=arraydef then
  579. begin
  580. end
  581. else
  582. begin
  583. if left.location.loc=LOC_CMMREGISTER then
  584. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  585. else
  586. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  587. end;
  588. end;
  589. LOC_REGISTER,
  590. LOC_CREGISTER :
  591. begin
  592. {$ifndef cpu64bit}
  593. if left.location.size in [OS_64,OS_S64] then
  594. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  595. right.location.register64,left.location)
  596. else
  597. {$endif cpu64bit}
  598. cg.a_load_reg_loc(current_asmdata.CurrAsmList,right.location.size,right.location.register,left.location);
  599. end;
  600. LOC_FPUREGISTER,
  601. LOC_CFPUREGISTER :
  602. begin
  603. { we can't do direct moves between fpu and mm registers }
  604. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  605. begin
  606. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,false);
  607. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  608. right.location.size,left.location.size,
  609. right.location.register,left.location.register,mms_movescalar);
  610. end
  611. else
  612. cg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  613. right.location.size,
  614. right.location.register,left.location);
  615. end;
  616. LOC_SUBSETREG,
  617. LOC_CSUBSETREG:
  618. begin
  619. cg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  620. right.location.size,right.location.sreg,left.location);
  621. end;
  622. LOC_SUBSETREF,
  623. LOC_CSUBSETREF:
  624. begin
  625. {$ifndef cpu64bit}
  626. if right.location.size in [OS_64,OS_S64] then
  627. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  628. else
  629. {$endif cpu64bit}
  630. cg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  631. right.location.size,right.location.sref,left.location);
  632. end;
  633. LOC_JUMP :
  634. begin
  635. current_asmdata.getjumplabel(hlabel);
  636. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  637. cg.a_load_const_loc(current_asmdata.CurrAsmList,1,left.location);
  638. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  639. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  640. cg.a_load_const_loc(current_asmdata.CurrAsmList,0,left.location);
  641. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  642. end;
  643. {$ifdef cpuflags}
  644. LOC_FLAGS :
  645. begin
  646. {This can be a wordbool or longbool too, no?}
  647. case left.location.loc of
  648. LOC_REGISTER,LOC_CREGISTER:
  649. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.register);
  650. LOC_REFERENCE:
  651. cg.g_flags2ref(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.reference);
  652. LOC_SUBSETREG,LOC_SUBSETREF:
  653. begin
  654. r:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(left.resultdef));
  655. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,r);
  656. cg.a_load_reg_loc(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),r,left.location);
  657. end;
  658. else
  659. internalerror(200203273);
  660. end;
  661. end;
  662. {$endif cpuflags}
  663. end;
  664. end;
  665. if releaseright then
  666. location_freetemp(current_asmdata.CurrAsmList,right.location);
  667. current_procinfo.CurrTrueLabel:=otlabel;
  668. current_procinfo.CurrFalseLabel:=oflabel;
  669. end;
  670. {*****************************************************************************
  671. SecondArrayConstruct
  672. *****************************************************************************}
  673. const
  674. vtInteger = 0;
  675. vtBoolean = 1;
  676. vtChar = 2;
  677. vtExtended = 3;
  678. vtString = 4;
  679. vtPointer = 5;
  680. vtPChar = 6;
  681. vtObject = 7;
  682. vtClass = 8;
  683. vtWideChar = 9;
  684. vtPWideChar = 10;
  685. vtAnsiString32 = 11;
  686. vtCurrency = 12;
  687. vtVariant = 13;
  688. vtInterface = 14;
  689. vtWideString = 15;
  690. vtInt64 = 16;
  691. vtQWord = 17;
  692. vtAnsiString16 = 18;
  693. vtAnsiString64 = 19;
  694. procedure tcgarrayconstructornode.pass_generate_code;
  695. var
  696. hp : tarrayconstructornode;
  697. href : treference;
  698. lt : tdef;
  699. vaddr : boolean;
  700. vtype : longint;
  701. freetemp,
  702. dovariant : boolean;
  703. elesize : longint;
  704. tmpreg : tregister;
  705. paraloc : tcgparalocation;
  706. otlabel,
  707. oflabel : tasmlabel;
  708. begin
  709. if is_packed_array(resultdef) then
  710. internalerror(200608042);
  711. dovariant:=(nf_forcevaria in flags) or is_variant_array(resultdef);
  712. if dovariant then
  713. elesize:=sizeof(aint)+sizeof(aint)
  714. else
  715. elesize:=tarraydef(resultdef).elesize;
  716. location_reset(location,LOC_CREFERENCE,OS_NO);
  717. fillchar(paraloc,sizeof(paraloc),0);
  718. { Allocate always a temp, also if no elements are required, to
  719. be sure that location is valid (PFV) }
  720. if tarraydef(resultdef).highrange=-1 then
  721. tg.GetTemp(current_asmdata.CurrAsmList,elesize,tt_normal,location.reference)
  722. else
  723. tg.GetTemp(current_asmdata.CurrAsmList,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  724. href:=location.reference;
  725. { Process nodes in array constructor }
  726. hp:=self;
  727. while assigned(hp) do
  728. begin
  729. if assigned(hp.left) then
  730. begin
  731. freetemp:=true;
  732. if (hp.left.expectloc=LOC_JUMP) then
  733. begin
  734. otlabel:=current_procinfo.CurrTrueLabel;
  735. oflabel:=current_procinfo.CurrFalseLabel;
  736. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  737. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  738. end;
  739. secondpass(hp.left);
  740. { Move flags and jump in register }
  741. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  742. location_force_reg(current_asmdata.CurrAsmList,hp.left.location,def_cgsize(hp.left.resultdef),false);
  743. if (hp.left.location.loc=LOC_JUMP) then
  744. begin
  745. if (hp.left.expectloc<>LOC_JUMP) then
  746. internalerror(2007103101);
  747. current_procinfo.CurrTrueLabel:=otlabel;
  748. current_procinfo.CurrFalseLabel:=oflabel;
  749. end;
  750. if dovariant then
  751. begin
  752. { find the correct vtype value }
  753. vtype:=$ff;
  754. vaddr:=false;
  755. lt:=hp.left.resultdef;
  756. case lt.typ of
  757. enumdef,
  758. orddef :
  759. begin
  760. if is_64bit(lt) then
  761. begin
  762. case torddef(lt).ordtype of
  763. scurrency:
  764. vtype:=vtCurrency;
  765. s64bit:
  766. vtype:=vtInt64;
  767. u64bit:
  768. vtype:=vtQWord;
  769. end;
  770. freetemp:=false;
  771. vaddr:=true;
  772. end
  773. else if (lt.typ=enumdef) or
  774. is_integer(lt) then
  775. vtype:=vtInteger
  776. else
  777. if is_boolean(lt) then
  778. vtype:=vtBoolean
  779. else
  780. if (lt.typ=orddef) then
  781. begin
  782. case torddef(lt).ordtype of
  783. uchar:
  784. vtype:=vtChar;
  785. uwidechar:
  786. vtype:=vtWideChar;
  787. end;
  788. end;
  789. end;
  790. floatdef :
  791. begin
  792. if is_currency(lt) then
  793. vtype:=vtCurrency
  794. else
  795. vtype:=vtExtended;
  796. freetemp:=false;
  797. vaddr:=true;
  798. end;
  799. procvardef,
  800. pointerdef :
  801. begin
  802. if is_pchar(lt) then
  803. vtype:=vtPChar
  804. else if is_pwidechar(lt) then
  805. vtype:=vtPWideChar
  806. else
  807. vtype:=vtPointer;
  808. end;
  809. variantdef :
  810. begin
  811. vtype:=vtVariant;
  812. vaddr:=true;
  813. freetemp:=false;
  814. end;
  815. classrefdef :
  816. vtype:=vtClass;
  817. objectdef :
  818. if is_interface(lt) then
  819. vtype:=vtInterface
  820. { vtObject really means a class based on TObject }
  821. else if is_class(lt) then
  822. vtype:=vtObject
  823. else
  824. internalerror(200505171);
  825. stringdef :
  826. begin
  827. if is_shortstring(lt) then
  828. begin
  829. vtype:=vtString;
  830. vaddr:=true;
  831. freetemp:=false;
  832. end
  833. else
  834. if is_ansistring(lt) then
  835. begin
  836. vtype:=vtAnsiString;
  837. freetemp:=false;
  838. end
  839. else
  840. if is_widestring(lt) then
  841. begin
  842. vtype:=vtWideString;
  843. freetemp:=false;
  844. end;
  845. end;
  846. end;
  847. if vtype=$ff then
  848. internalerror(14357);
  849. { write changing field update href to the next element }
  850. inc(href.offset,sizeof(aint));
  851. if vaddr then
  852. begin
  853. location_force_mem(current_asmdata.CurrAsmList,hp.left.location);
  854. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  855. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.location.reference,tmpreg);
  856. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,href);
  857. end
  858. else
  859. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,hp.left.location,href);
  860. { update href to the vtype field and write it }
  861. dec(href.offset,sizeof(aint));
  862. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  863. { goto next array element }
  864. inc(href.offset,sizeof(aint)*2);
  865. end
  866. else
  867. { normal array constructor of the same type }
  868. begin
  869. if resultdef.needs_inittable then
  870. freetemp:=false;
  871. case hp.left.location.loc of
  872. LOC_MMREGISTER,
  873. LOC_CMMREGISTER:
  874. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,
  875. hp.left.location.register,href,mms_movescalar);
  876. LOC_FPUREGISTER,
  877. LOC_CFPUREGISTER :
  878. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,hp.left.location.register,href);
  879. LOC_REFERENCE,
  880. LOC_CREFERENCE :
  881. begin
  882. if is_shortstring(hp.left.resultdef) then
  883. cg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  884. Tstringdef(hp.left.resultdef).len)
  885. else
  886. cg.g_concatcopy(current_asmdata.CurrAsmList,hp.left.location.reference,href,elesize);
  887. end;
  888. else
  889. begin
  890. {$ifndef cpu64bit}
  891. if hp.left.location.size in [OS_64,OS_S64] then
  892. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  893. else
  894. {$endif cpu64bit}
  895. cg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location,href);
  896. end;
  897. end;
  898. inc(href.offset,elesize);
  899. end;
  900. if freetemp then
  901. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  902. end;
  903. { load next entry }
  904. hp:=tarrayconstructornode(hp.right);
  905. end;
  906. end;
  907. {*****************************************************************************
  908. SecondRTTI
  909. *****************************************************************************}
  910. procedure tcgrttinode.pass_generate_code;
  911. begin
  912. location_reset(location,LOC_CREFERENCE,OS_NO);
  913. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  914. end;
  915. begin
  916. cloadnode:=tcgloadnode;
  917. cassignmentnode:=tcgassignmentnode;
  918. carrayconstructornode:=tcgarrayconstructornode;
  919. crttinode:=tcgrttinode;
  920. end.