ncgld.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 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 is_class_or_interface(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 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. { load vmt pointer }
  294. reference_reset_base(href,hregister,0);
  295. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  296. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  297. { load method address }
  298. reference_reset_base(href,hregister,procdef._class.vmtmethodoffset(procdef.extnumber));
  299. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  300. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,hregister);
  301. { ... and store it }
  302. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  303. end
  304. else
  305. begin
  306. { load address of the function }
  307. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0);
  308. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  309. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,hregister);
  310. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  311. end;
  312. end
  313. else
  314. begin
  315. pd:=tprocdef(tprocsym(symtableentry).ProcdefList[0]);
  316. if (po_external in pd.procoptions) then
  317. location.reference.base := cg.g_indirect_sym_load(current_asmdata.CurrAsmList,pd.mangledname);
  318. {!!!!! Be aware, work on virtual methods too }
  319. if (location.reference.base = NR_NO) then
  320. location.reference.symbol:=current_asmdata.RefAsmSymbol(procdef.mangledname);
  321. end;
  322. end;
  323. labelsym :
  324. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  325. else internalerror(200510032);
  326. end;
  327. end;
  328. {*****************************************************************************
  329. SecondAssignment
  330. *****************************************************************************}
  331. procedure tcgassignmentnode.pass_generate_code;
  332. var
  333. otlabel,hlabel,oflabel : tasmlabel;
  334. href : treference;
  335. releaseright : boolean;
  336. len : aint;
  337. r:Tregister;
  338. begin
  339. location_reset(location,LOC_VOID,OS_NO);
  340. otlabel:=current_procinfo.CurrTrueLabel;
  341. oflabel:=current_procinfo.CurrFalseLabel;
  342. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  343. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  344. {
  345. in most cases we can process first the right node which contains
  346. the most complex code. Exceptions for this are:
  347. - result is in flags, loading left will then destroy the flags
  348. - result is a jump, loading left must be already done before the jump is made
  349. - result need reference count, when left points to a value used in
  350. right then decreasing the refcnt on left can possibly release
  351. the memory before right increased the refcnt, result is that an
  352. empty value is assigned
  353. - calln, call destroys most registers and is therefor 'complex'
  354. But not when the result is in the flags, then
  355. loading the left node afterwards can destroy the flags.
  356. }
  357. if not(right.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  358. ((right.nodetype=calln) or
  359. (right.resultdef.needs_inittable) or
  360. (right.registersint>=left.registersint)) then
  361. begin
  362. secondpass(right);
  363. { increment source reference counter, this is
  364. useless for string constants}
  365. if (right.resultdef.needs_inittable) and
  366. (right.nodetype<>stringconstn) then
  367. begin
  368. location_force_mem(current_asmdata.CurrAsmList,right.location);
  369. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  370. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  371. end;
  372. if codegenerror then
  373. exit;
  374. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  375. { can be false }
  376. secondpass(left);
  377. { decrement destination reference counter }
  378. if (left.resultdef.needs_inittable) then
  379. begin
  380. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  381. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  382. end;
  383. if codegenerror then
  384. exit;
  385. end
  386. else
  387. begin
  388. { calculate left sides }
  389. secondpass(left);
  390. { decrement destination reference counter }
  391. if (left.resultdef.needs_inittable) then
  392. begin
  393. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  394. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  395. end;
  396. if codegenerror then
  397. exit;
  398. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  399. { can be false }
  400. secondpass(right);
  401. { increment source reference counter, this is
  402. useless for string constants}
  403. if (right.resultdef.needs_inittable) and
  404. (right.nodetype<>stringconstn) then
  405. begin
  406. location_force_mem(current_asmdata.CurrAsmList,right.location);
  407. location_get_data_ref(current_asmdata.CurrAsmList,right.location,href,false);
  408. cg.g_incrrefcount(current_asmdata.CurrAsmList,right.resultdef,href);
  409. end;
  410. if codegenerror then
  411. exit;
  412. end;
  413. releaseright:=true;
  414. { optimize temp to temp copies }
  415. {$ifdef old_append_str}
  416. if (left.nodetype = temprefn) and
  417. { we may store certain temps in registers in the future, then this }
  418. { optimization will have to be adapted }
  419. (left.location.loc = LOC_REFERENCE) and
  420. (right.location.loc = LOC_REFERENCE) and
  421. tg.istemp(right.location.reference) and
  422. (tg.sizeoftemp(current_asmdata.CurrAsmList,right.location.reference) = tg.sizeoftemp(current_asmdata.CurrAsmList,left.location.reference)) then
  423. begin
  424. { in theory, we should also make sure the left temp type is }
  425. { already more or less of the same kind (ie. we must not }
  426. { assign an ansistring to a normaltemp). In practice, the }
  427. { assignment node will have already taken care of this for us }
  428. tcgtemprefnode(left).changelocation(right.location.reference);
  429. end
  430. { shortstring assignments are handled separately }
  431. else
  432. {$endif old_append_str}
  433. if is_shortstring(left.resultdef) then
  434. begin
  435. {
  436. we can get here only in the following situations
  437. for the right node:
  438. - empty constant string
  439. - char
  440. }
  441. { The addn is replaced by a blockn or calln }
  442. if right.nodetype in [blockn,calln] then
  443. begin
  444. { nothing to do }
  445. end
  446. { empty constant string }
  447. else if (right.nodetype=stringconstn) and
  448. (tstringconstnode(right).len=0) then
  449. begin
  450. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,0,left.location.reference);
  451. end
  452. { char loading }
  453. else if is_char(right.resultdef) then
  454. begin
  455. if right.nodetype=ordconstn then
  456. begin
  457. if (target_info.endian = endian_little) then
  458. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,(tordconstnode(right).value shl 8) or 1,
  459. left.location.reference)
  460. else
  461. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_16,tordconstnode(right).value or (1 shl 8),
  462. left.location.reference);
  463. end
  464. else
  465. begin
  466. href:=left.location.reference;
  467. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,1,href);
  468. inc(href.offset,1);
  469. case right.location.loc of
  470. LOC_REGISTER,
  471. LOC_CREGISTER :
  472. begin
  473. r:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_8);
  474. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,r,href);
  475. end;
  476. LOC_REFERENCE,
  477. LOC_CREFERENCE :
  478. cg.a_load_ref_ref(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,href);
  479. else
  480. internalerror(200205111);
  481. end;
  482. end;
  483. end
  484. else
  485. internalerror(200204249);
  486. end
  487. else
  488. begin
  489. { SSA support }
  490. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,false);
  491. maybechangeloadnodereg(current_asmdata.CurrAsmList,right,true);
  492. case right.location.loc of
  493. LOC_CONSTANT :
  494. begin
  495. {$ifndef cpu64bit}
  496. if right.location.size in [OS_64,OS_S64] then
  497. cg64.a_load64_const_loc(current_asmdata.CurrAsmList,right.location.value64,left.location)
  498. else
  499. {$endif cpu64bit}
  500. cg.a_load_const_loc(current_asmdata.CurrAsmList,right.location.value,left.location);
  501. end;
  502. LOC_REFERENCE,
  503. LOC_CREFERENCE :
  504. begin
  505. case left.location.loc of
  506. LOC_REGISTER,
  507. LOC_CREGISTER :
  508. begin
  509. {$ifndef cpu64bit}
  510. if left.location.size in [OS_64,OS_S64] then
  511. cg64.a_load64_ref_reg(current_asmdata.CurrAsmList,right.location.reference,left.location.register64)
  512. else
  513. {$endif cpu64bit}
  514. cg.a_load_ref_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.register);
  515. end;
  516. LOC_FPUREGISTER,
  517. LOC_CFPUREGISTER :
  518. begin
  519. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  520. right.location.size,left.location.size,
  521. right.location.reference,
  522. left.location.register);
  523. end;
  524. LOC_REFERENCE,
  525. LOC_CREFERENCE :
  526. begin
  527. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  528. { Use unaligned copy when the offset is not aligned }
  529. len:=left.resultdef.size;
  530. if (right.location.reference.offset mod sizeof(aint)<>0) or
  531. (left.location.reference.offset mod sizeof(aint)<>0) or
  532. (right.resultdef.alignment<sizeof(aint)) then
  533. cg.g_concatcopy_unaligned(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len)
  534. else
  535. cg.g_concatcopy(current_asmdata.CurrAsmList,right.location.reference,left.location.reference,len);
  536. end;
  537. LOC_MMREGISTER,
  538. LOC_CMMREGISTER:
  539. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,
  540. right.location.size,
  541. left.location.size,
  542. right.location.reference,
  543. left.location.register,mms_movescalar);
  544. LOC_SUBSETREG,
  545. LOC_CSUBSETREG:
  546. cg.a_load_ref_subsetreg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sreg);
  547. LOC_SUBSETREF,
  548. LOC_CSUBSETREF:
  549. {$ifndef cpu64bit}
  550. if right.location.size in [OS_64,OS_S64] then
  551. cg64.a_load64_ref_subsetref(current_asmdata.CurrAsmList,right.location.reference,left.location.sref)
  552. else
  553. {$endif cpu64bit}
  554. cg.a_load_ref_subsetref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.reference,left.location.sref);
  555. else
  556. internalerror(200203284);
  557. end;
  558. end;
  559. {$ifdef SUPPORT_MMX}
  560. LOC_CMMXREGISTER,
  561. LOC_MMXREGISTER:
  562. begin
  563. if left.location.loc=LOC_CMMXREGISTER then
  564. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  565. else
  566. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  567. end;
  568. {$endif SUPPORT_MMX}
  569. LOC_MMREGISTER,
  570. LOC_CMMREGISTER:
  571. begin
  572. if left.resultdef.typ=arraydef then
  573. begin
  574. end
  575. else
  576. begin
  577. if left.location.loc=LOC_CMMREGISTER then
  578. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  579. else
  580. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  581. end;
  582. end;
  583. LOC_REGISTER,
  584. LOC_CREGISTER :
  585. begin
  586. {$ifndef cpu64bit}
  587. if left.location.size in [OS_64,OS_S64] then
  588. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,
  589. right.location.register64,left.location)
  590. else
  591. {$endif cpu64bit}
  592. cg.a_load_reg_loc(current_asmdata.CurrAsmList,right.location.size,right.location.register,left.location);
  593. end;
  594. LOC_FPUREGISTER,
  595. LOC_CFPUREGISTER :
  596. begin
  597. { we can't do direct moves between fpu and mm registers }
  598. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  599. begin
  600. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,false);
  601. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,
  602. right.location.size,left.location.size,
  603. right.location.register,left.location.register,mms_movescalar);
  604. end
  605. else
  606. cg.a_loadfpu_reg_loc(current_asmdata.CurrAsmList,
  607. right.location.size,
  608. right.location.register,left.location);
  609. end;
  610. LOC_SUBSETREG,
  611. LOC_CSUBSETREG:
  612. begin
  613. cg.a_load_subsetreg_loc(current_asmdata.CurrAsmList,
  614. right.location.size,right.location.sreg,left.location);
  615. end;
  616. LOC_SUBSETREF,
  617. LOC_CSUBSETREF:
  618. begin
  619. {$ifndef cpu64bit}
  620. if right.location.size in [OS_64,OS_S64] then
  621. cg64.a_load64_subsetref_loc(current_asmdata.CurrAsmList,right.location.sref,left.location)
  622. else
  623. {$endif cpu64bit}
  624. cg.a_load_subsetref_loc(current_asmdata.CurrAsmList,
  625. right.location.size,right.location.sref,left.location);
  626. end;
  627. LOC_JUMP :
  628. begin
  629. current_asmdata.getjumplabel(hlabel);
  630. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  631. cg.a_load_const_loc(current_asmdata.CurrAsmList,1,left.location);
  632. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  633. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  634. cg.a_load_const_loc(current_asmdata.CurrAsmList,0,left.location);
  635. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  636. end;
  637. {$ifdef cpuflags}
  638. LOC_FLAGS :
  639. begin
  640. {This can be a wordbool or longbool too, no?}
  641. case left.location.loc of
  642. LOC_REGISTER,LOC_CREGISTER:
  643. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.register);
  644. LOC_REFERENCE:
  645. cg.g_flags2ref(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,left.location.reference);
  646. LOC_SUBSETREG,LOC_SUBSETREF:
  647. begin
  648. r:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(left.resultdef));
  649. cg.g_flags2reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),right.location.resflags,r);
  650. cg.a_load_reg_loc(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),r,left.location);
  651. end;
  652. else
  653. internalerror(200203273);
  654. end;
  655. end;
  656. {$endif cpuflags}
  657. end;
  658. end;
  659. if releaseright then
  660. location_freetemp(current_asmdata.CurrAsmList,right.location);
  661. current_procinfo.CurrTrueLabel:=otlabel;
  662. current_procinfo.CurrFalseLabel:=oflabel;
  663. end;
  664. {*****************************************************************************
  665. SecondArrayConstruct
  666. *****************************************************************************}
  667. const
  668. vtInteger = 0;
  669. vtBoolean = 1;
  670. vtChar = 2;
  671. vtExtended = 3;
  672. vtString = 4;
  673. vtPointer = 5;
  674. vtPChar = 6;
  675. vtObject = 7;
  676. vtClass = 8;
  677. vtWideChar = 9;
  678. vtPWideChar = 10;
  679. vtAnsiString32 = 11;
  680. vtCurrency = 12;
  681. vtVariant = 13;
  682. vtInterface = 14;
  683. vtWideString = 15;
  684. vtInt64 = 16;
  685. vtQWord = 17;
  686. vtAnsiString16 = 18;
  687. vtAnsiString64 = 19;
  688. procedure tcgarrayconstructornode.pass_generate_code;
  689. var
  690. hp : tarrayconstructornode;
  691. href : treference;
  692. lt : tdef;
  693. vaddr : boolean;
  694. vtype : longint;
  695. freetemp,
  696. dovariant : boolean;
  697. elesize : longint;
  698. tmpreg : tregister;
  699. paraloc : tcgparalocation;
  700. begin
  701. if is_packed_array(resultdef) then
  702. internalerror(200608042);
  703. dovariant:=(nf_forcevaria in flags) or is_variant_array(resultdef);
  704. if dovariant then
  705. elesize:=sizeof(aint)+sizeof(aint)
  706. else
  707. elesize:=tarraydef(resultdef).elesize;
  708. location_reset(location,LOC_CREFERENCE,OS_NO);
  709. fillchar(paraloc,sizeof(paraloc),0);
  710. { Allocate always a temp, also if no elements are required, to
  711. be sure that location is valid (PFV) }
  712. if tarraydef(resultdef).highrange=-1 then
  713. tg.GetTemp(current_asmdata.CurrAsmList,elesize,tt_normal,location.reference)
  714. else
  715. tg.GetTemp(current_asmdata.CurrAsmList,(tarraydef(resultdef).highrange+1)*elesize,tt_normal,location.reference);
  716. href:=location.reference;
  717. { Process nodes in array constructor }
  718. hp:=self;
  719. while assigned(hp) do
  720. begin
  721. if assigned(hp.left) then
  722. begin
  723. freetemp:=true;
  724. secondpass(hp.left);
  725. if codegenerror then
  726. exit;
  727. { Move flags and jump in register }
  728. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  729. location_force_reg(current_asmdata.CurrAsmList,hp.left.location,def_cgsize(hp.left.resultdef),false);
  730. if dovariant then
  731. begin
  732. { find the correct vtype value }
  733. vtype:=$ff;
  734. vaddr:=false;
  735. lt:=hp.left.resultdef;
  736. case lt.typ of
  737. enumdef,
  738. orddef :
  739. begin
  740. if is_64bit(lt) then
  741. begin
  742. case torddef(lt).ordtype of
  743. scurrency:
  744. vtype:=vtCurrency;
  745. s64bit:
  746. vtype:=vtInt64;
  747. u64bit:
  748. vtype:=vtQWord;
  749. end;
  750. freetemp:=false;
  751. vaddr:=true;
  752. end
  753. else if (lt.typ=enumdef) or
  754. is_integer(lt) then
  755. vtype:=vtInteger
  756. else
  757. if is_boolean(lt) then
  758. vtype:=vtBoolean
  759. else
  760. if (lt.typ=orddef) then
  761. begin
  762. case torddef(lt).ordtype of
  763. uchar:
  764. vtype:=vtChar;
  765. uwidechar:
  766. vtype:=vtWideChar;
  767. end;
  768. end;
  769. end;
  770. floatdef :
  771. begin
  772. if is_currency(lt) then
  773. vtype:=vtCurrency
  774. else
  775. vtype:=vtExtended;
  776. freetemp:=false;
  777. vaddr:=true;
  778. end;
  779. procvardef,
  780. pointerdef :
  781. begin
  782. if is_pchar(lt) then
  783. vtype:=vtPChar
  784. else if is_pwidechar(lt) then
  785. vtype:=vtPWideChar
  786. else
  787. vtype:=vtPointer;
  788. end;
  789. variantdef :
  790. begin
  791. vtype:=vtVariant;
  792. vaddr:=true;
  793. freetemp:=false;
  794. end;
  795. classrefdef :
  796. vtype:=vtClass;
  797. objectdef :
  798. if is_interface(lt) then
  799. vtype:=vtInterface
  800. { vtObject really means a class based on TObject }
  801. else if is_class(lt) then
  802. vtype:=vtObject
  803. else
  804. internalerror(200505171);
  805. stringdef :
  806. begin
  807. if is_shortstring(lt) then
  808. begin
  809. vtype:=vtString;
  810. vaddr:=true;
  811. freetemp:=false;
  812. end
  813. else
  814. if is_ansistring(lt) then
  815. begin
  816. vtype:=vtAnsiString;
  817. freetemp:=false;
  818. end
  819. else
  820. if is_widestring(lt) then
  821. begin
  822. vtype:=vtWideString;
  823. freetemp:=false;
  824. end;
  825. end;
  826. end;
  827. if vtype=$ff then
  828. internalerror(14357);
  829. { write changing field update href to the next element }
  830. inc(href.offset,sizeof(aint));
  831. if vaddr then
  832. begin
  833. location_force_mem(current_asmdata.CurrAsmList,hp.left.location);
  834. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  835. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hp.left.location.reference,tmpreg);
  836. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,href);
  837. end
  838. else
  839. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,hp.left.location,href);
  840. { update href to the vtype field and write it }
  841. dec(href.offset,sizeof(aint));
  842. cg.a_load_const_ref(current_asmdata.CurrAsmList, OS_INT,vtype,href);
  843. { goto next array element }
  844. inc(href.offset,sizeof(aint)*2);
  845. end
  846. else
  847. { normal array constructor of the same type }
  848. begin
  849. if resultdef.needs_inittable then
  850. freetemp:=false;
  851. case hp.left.location.loc of
  852. LOC_MMREGISTER,
  853. LOC_CMMREGISTER:
  854. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,
  855. hp.left.location.register,href,mms_movescalar);
  856. LOC_FPUREGISTER,
  857. LOC_CFPUREGISTER :
  858. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location.size,hp.left.location.register,href);
  859. LOC_REFERENCE,
  860. LOC_CREFERENCE :
  861. begin
  862. if is_shortstring(hp.left.resultdef) then
  863. cg.g_copyshortstring(current_asmdata.CurrAsmList,hp.left.location.reference,href,
  864. Tstringdef(hp.left.resultdef).len)
  865. else
  866. cg.g_concatcopy(current_asmdata.CurrAsmList,hp.left.location.reference,href,elesize);
  867. end;
  868. else
  869. begin
  870. {$ifndef cpu64bit}
  871. if hp.left.location.size in [OS_64,OS_S64] then
  872. cg64.a_load64_loc_ref(current_asmdata.CurrAsmList,hp.left.location,href)
  873. else
  874. {$endif cpu64bit}
  875. cg.a_load_loc_ref(current_asmdata.CurrAsmList,hp.left.location.size,hp.left.location,href);
  876. end;
  877. end;
  878. inc(href.offset,elesize);
  879. end;
  880. if freetemp then
  881. location_freetemp(current_asmdata.CurrAsmList,hp.left.location);
  882. end;
  883. { load next entry }
  884. hp:=tarrayconstructornode(hp.right);
  885. end;
  886. end;
  887. {*****************************************************************************
  888. SecondRTTI
  889. *****************************************************************************}
  890. procedure tcgrttinode.pass_generate_code;
  891. begin
  892. location_reset(location,LOC_CREFERENCE,OS_NO);
  893. location.reference.symbol:=RTTIWriter.get_rtti_label(rttidef,rttitype);
  894. end;
  895. begin
  896. cloadnode:=tcgloadnode;
  897. cassignmentnode:=tcgassignmentnode;
  898. carrayconstructornode:=tcgarrayconstructornode;
  899. crttinode:=tcgrttinode;
  900. end.