ncgld.pas 40 KB

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