2
0

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 (vo_is_dll_var in tabstractvarsym(symtableentry).varoptions) then
  111. { DLL variable }
  112. begin
  113. hregister:=cg.getaddressregister(exprasmlist);
  114. location.reference.symbol:=objectlibrary.newasmsymbol(tglobalvarsym(symtableentry).mangledname,AB_EXTERNAL,AT_DATA);
  115. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,location.reference,hregister);
  116. reference_reset_base(location.reference,hregister,0);
  117. end
  118. { Thread variable }
  119. else if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  120. begin
  121. {
  122. Thread var loading is optimized to first check if
  123. a relocate function is available. When the function
  124. is available it is called to retrieve the address.
  125. Otherwise the address is loaded with the symbol
  126. The code needs to be in the order to first handle the
  127. call and then the address load to be sure that the
  128. register that is used for returning is the same (PFV)
  129. }
  130. objectlibrary.getlabel(norelocatelab);
  131. objectlibrary.getlabel(endrelocatelab);
  132. { make sure hregister can't allocate the register necessary for the parameter }
  133. paraloc1.init;
  134. paramanager.getintparaloc(pocall_default,1,paraloc1);
  135. hregister:=cg.getaddressregister(exprasmlist);
  136. reference_reset_symbol(href,objectlibrary.newasmsymbol('FPC_THREADVAR_RELOCATE',AB_EXTERNAL,AT_DATA),0);
  137. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,hregister);
  138. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,hregister,norelocatelab);
  139. { don't save the allocated register else the result will be destroyed later }
  140. reference_reset_symbol(href,objectlibrary.newasmsymbol(tglobalvarsym(symtableentry).mangledname,AB_EXTERNAL,AT_DATA),0);
  141. paramanager.allocparaloc(exprasmlist,paraloc1);
  142. cg.a_param_ref(exprasmlist,OS_32,href,paraloc1);
  143. paramanager.freeparaloc(exprasmlist,paraloc1);
  144. paraloc1.done;
  145. cg.alloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  146. cg.a_call_reg(exprasmlist,hregister);
  147. cg.dealloccpuregisters(exprasmlist,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  148. cg.getcpuregister(exprasmlist,NR_FUNCTION_RESULT_REG);
  149. cg.ungetcpuregister(exprasmlist,NR_FUNCTION_RESULT_REG);
  150. hregister:=cg.getaddressregister(exprasmlist);
  151. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_ADDR,NR_FUNCTION_RESULT_REG,hregister);
  152. cg.a_jmp_always(exprasmlist,endrelocatelab);
  153. cg.a_label(exprasmlist,norelocatelab);
  154. { no relocation needed, load the address of the variable only, the
  155. layout of a threadvar is (4 bytes pointer):
  156. 0 - Threadvar index
  157. 4 - Threadvar value in single threading }
  158. reference_reset_symbol(href,objectlibrary.newasmsymbol(tglobalvarsym(symtableentry).mangledname,AB_EXTERNAL,AT_DATA),sizeof(aint));
  159. cg.a_loadaddr_ref_reg(exprasmlist,href,hregister);
  160. cg.a_label(exprasmlist,endrelocatelab);
  161. location.reference.base:=hregister;
  162. end
  163. { Nested variable }
  164. else if assigned(left) then
  165. begin
  166. if not(symtabletype in [localsymtable,parasymtable]) then
  167. internalerror(200309285);
  168. secondpass(left);
  169. if left.location.loc<>LOC_REGISTER then
  170. internalerror(200309286);
  171. if tabstractnormalvarsym(symtableentry).localloc.loc<>LOC_REFERENCE then
  172. internalerror(200409241);
  173. hregister:=left.location.register;
  174. reference_reset_base(location.reference,hregister,tabstractnormalvarsym(symtableentry).localloc.reference.offset);
  175. end
  176. { Normal (or external) variable }
  177. else
  178. begin
  179. {$ifdef OLDREGVARS}
  180. { in case it is a register variable: }
  181. if tvarsym(symtableentry).localloc.loc in [LOC_REGISTER,LOC_FPUREGISTER] then
  182. begin
  183. case getregtype(tvarsym(symtableentry).localloc.register) of
  184. R_FPUREGISTER :
  185. begin
  186. location_reset(location,LOC_CFPUREGISTER,def_cgsize(resulttype.def));
  187. location.register:=tvarsym(symtableentry).localloc.register;
  188. end;
  189. R_INTREGISTER :
  190. begin
  191. location_reset(location,LOC_CREGISTER,def_cgsize(resulttype.def));
  192. location.register:=tvarsym(symtableentry).localloc.register;
  193. hregister := location.register;
  194. end;
  195. else
  196. internalerror(200301172);
  197. end;
  198. end
  199. else
  200. {$endif OLDREGVARS}
  201. begin
  202. case symtabletype of
  203. stt_exceptsymtable,
  204. localsymtable,
  205. parasymtable :
  206. location:=tabstractnormalvarsym(symtableentry).localloc;
  207. globalsymtable,
  208. staticsymtable :
  209. begin
  210. if tabstractnormalvarsym(symtableentry).localloc.loc=LOC_INVALID then
  211. reference_reset_symbol(location.reference,objectlibrary.newasmsymbol(tglobalvarsym(symtableentry).mangledname,AB_EXTERNAL,AT_DATA),0)
  212. else
  213. location:=tglobalvarsym(symtableentry).localloc;
  214. end;
  215. else
  216. internalerror(200305102);
  217. end;
  218. end;
  219. end;
  220. { handle call by reference variables when they are not
  221. alreayd copied to local copies. Also ignore the reference
  222. when we need to load the self pointer for objects }
  223. if is_addr_param_load then
  224. begin
  225. if (location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  226. hregister:=location.register
  227. else
  228. begin
  229. hregister:=cg.getaddressregister(exprasmlist);
  230. { we need to load only an address }
  231. location.size:=OS_ADDR;
  232. cg.a_load_loc_reg(exprasmlist,location.size,location,hregister);
  233. end;
  234. location_reset(location,LOC_REFERENCE,newsize);
  235. location.reference.base:=hregister;
  236. end;
  237. { make const a LOC_CREFERENCE }
  238. if (tabstractvarsym(symtableentry).varspez=vs_const) and
  239. (location.loc=LOC_REFERENCE) then
  240. location.loc:=LOC_CREFERENCE;
  241. end;
  242. procsym:
  243. begin
  244. if not assigned(procdef) then
  245. internalerror(200312011);
  246. if assigned(left) then
  247. begin
  248. {
  249. THIS IS A TERRIBLE HACK!!!!!! WHICH WILL NOT WORK
  250. ON 64-BIT SYSTEMS: SINCE PROCSYM FOR METHODS
  251. CONSISTS OF TWO OS_ADDR, so you cannot set it
  252. to OS_64 - how to solve?? Carl
  253. Solved. Florian
  254. }
  255. if (sizeof(aint) = 4) then
  256. location_reset(location,LOC_CREFERENCE,OS_64)
  257. else if (sizeof(aint) = 8) then
  258. location_reset(location,LOC_CREFERENCE,OS_128)
  259. else
  260. internalerror(20020520);
  261. tg.GetTemp(exprasmlist,2*sizeof(aint),tt_normal,location.reference);
  262. secondpass(left);
  263. { load class instance address }
  264. case left.location.loc of
  265. LOC_CREGISTER,
  266. LOC_REGISTER:
  267. begin
  268. { this is not possible for objects }
  269. if is_object(left.resulttype.def) then
  270. internalerror(200304234);
  271. hregister:=left.location.register;
  272. end;
  273. LOC_CREFERENCE,
  274. LOC_REFERENCE:
  275. begin
  276. hregister:=cg.getaddressregister(exprasmlist);
  277. if is_class_or_interface(left.resulttype.def) then
  278. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,hregister)
  279. else
  280. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,hregister);
  281. location_freetemp(exprasmlist,left.location);
  282. end;
  283. else
  284. internalerror(26019);
  285. end;
  286. { store the class instance address }
  287. href:=location.reference;
  288. inc(href.offset,sizeof(aint));
  289. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,hregister,href);
  290. { virtual method ? }
  291. if (po_virtualmethod in procdef.procoptions) and
  292. not(nf_inherited in flags) then
  293. begin
  294. { load vmt pointer }
  295. reference_reset_base(href,hregister,0);
  296. hregister:=cg.getaddressregister(exprasmlist);
  297. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,hregister);
  298. { load method address }
  299. reference_reset_base(href,hregister,procdef._class.vmtmethodoffset(procdef.extnumber));
  300. hregister:=cg.getaddressregister(exprasmlist);
  301. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,hregister);
  302. { ... and store it }
  303. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,hregister,location.reference);
  304. end
  305. else
  306. begin
  307. { load address of the function }
  308. reference_reset_symbol(href,objectlibrary.newasmsymbol(procdef.mangledname,AB_EXTERNAL,AT_FUNCTION),0);
  309. hregister:=cg.getaddressregister(exprasmlist);
  310. cg.a_loadaddr_ref_reg(exprasmlist,href,hregister);
  311. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,hregister,location.reference);
  312. end;
  313. end
  314. else
  315. begin
  316. {!!!!! Be aware, work on virtual methods too }
  317. location.reference.symbol:=objectlibrary.newasmsymbol(procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  318. end;
  319. end;
  320. typedconstsym :
  321. location.reference.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(symtableentry).mangledname,AB_EXTERNAL,AT_DATA);
  322. labelsym :
  323. location.reference.symbol:=tcglabelnode((tlabelsym(symtableentry).code)).getasmlabel;
  324. else internalerror(200510032);
  325. end;
  326. end;
  327. {*****************************************************************************
  328. SecondAssignment
  329. *****************************************************************************}
  330. procedure tcgassignmentnode.pass_2;
  331. var
  332. otlabel,hlabel,oflabel : tasmlabel;
  333. fputyp : tfloattype;
  334. href : treference;
  335. releaseright : boolean;
  336. len : aint;
  337. r:Tregister;
  338. begin
  339. location_reset(location,LOC_VOID,OS_NO);
  340. otlabel:=truelabel;
  341. oflabel:=falselabel;
  342. objectlibrary.getlabel(truelabel);
  343. objectlibrary.getlabel(falselabel);
  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.resulttype.def.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.resulttype.def.needs_inittable) and
  366. (right.nodetype<>stringconstn) then
  367. begin
  368. location_force_mem(exprasmlist,right.location);
  369. location_get_data_ref(exprasmlist,right.location,href,false);
  370. cg.g_incrrefcount(exprasmlist,right.resulttype.def,href);
  371. end;
  372. if codegenerror then
  373. exit;
  374. if not(nf_concat_string in flags) then
  375. begin
  376. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  377. { can be false }
  378. secondpass(left);
  379. { decrement destination reference counter }
  380. if (left.resulttype.def.needs_inittable) then
  381. begin
  382. location_get_data_ref(exprasmlist,left.location,href,false);
  383. cg.g_decrrefcount(exprasmlist,left.resulttype.def,href);
  384. end;
  385. if codegenerror then
  386. exit;
  387. end;
  388. end
  389. else
  390. begin
  391. { calculate left sides }
  392. { don't do it yet if it's a crgister (JM) }
  393. if not(nf_concat_string in flags) then
  394. begin
  395. secondpass(left);
  396. { decrement destination reference counter }
  397. if (left.resulttype.def.needs_inittable) then
  398. begin
  399. location_get_data_ref(exprasmlist,left.location,href,false);
  400. cg.g_decrrefcount(exprasmlist,left.resulttype.def,href);
  401. end;
  402. if codegenerror then
  403. exit;
  404. end;
  405. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  406. { can be false }
  407. secondpass(right);
  408. { increment source reference counter, this is
  409. useless for string constants}
  410. if (right.resulttype.def.needs_inittable) and
  411. (right.nodetype<>stringconstn) then
  412. begin
  413. location_force_mem(exprasmlist,right.location);
  414. location_get_data_ref(exprasmlist,right.location,href,false);
  415. cg.g_incrrefcount(exprasmlist,right.resulttype.def,href);
  416. end;
  417. if codegenerror then
  418. exit;
  419. end;
  420. releaseright:=true;
  421. { optimize temp to temp copies }
  422. (* if (left.nodetype = temprefn) and
  423. { we may store certain temps in registers in the future, then this }
  424. { optimization will have to be adapted }
  425. (left.location.loc = LOC_REFERENCE) and
  426. (right.location.loc = LOC_REFERENCE) and
  427. tg.istemp(right.location.reference) and
  428. (tg.sizeoftemp(exprasmlist,right.location.reference) = tg.sizeoftemp(exprasmlist,left.location.reference)) then
  429. begin
  430. { in theory, we should also make sure the left temp type is }
  431. { already more or less of the same kind (ie. we must not }
  432. { assign an ansistring to a normaltemp). In practice, the }
  433. { assignment node will have already taken care of this for us }
  434. tcgtemprefnode(left).changelocation(right.location.reference);
  435. end
  436. { shortstring assignments are handled separately }
  437. else *)
  438. if is_shortstring(left.resulttype.def) then
  439. begin
  440. {
  441. we can get here only in the following situations
  442. for the right node:
  443. - empty constant string
  444. - char
  445. }
  446. { empty constant string }
  447. if (right.nodetype=stringconstn) and
  448. (tstringconstnode(right).len=0) then
  449. begin
  450. cg.a_load_const_ref(exprasmlist,OS_8,0,left.location.reference);
  451. end
  452. { char loading }
  453. else if is_char(right.resulttype.def) 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(exprasmlist,OS_16,(tordconstnode(right).value shl 8) or 1,
  459. left.location.reference)
  460. else
  461. cg.a_load_const_ref(exprasmlist,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(exprasmlist,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(exprasmlist,right.location.register,OS_8);
  474. cg.a_load_reg_ref(exprasmlist,OS_8,OS_8,r,href);
  475. end;
  476. LOC_REFERENCE,
  477. LOC_CREFERENCE :
  478. cg.a_load_ref_ref(exprasmlist,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. case right.location.loc of
  490. LOC_CONSTANT :
  491. begin
  492. {$ifndef cpu64bit}
  493. if right.location.size in [OS_64,OS_S64] then
  494. cg64.a_load64_const_loc(exprasmlist,right.location.value64,left.location)
  495. else
  496. {$endif cpu64bit}
  497. cg.a_load_const_loc(exprasmlist,right.location.value,left.location);
  498. end;
  499. LOC_REFERENCE,
  500. LOC_CREFERENCE :
  501. begin
  502. case left.location.loc of
  503. LOC_REGISTER,
  504. LOC_CREGISTER :
  505. begin
  506. {$ifndef cpu64bit}
  507. if left.location.size in [OS_64,OS_S64] then
  508. cg64.a_load64_ref_reg(exprasmlist,right.location.reference,left.location.register64)
  509. else
  510. {$endif cpu64bit}
  511. cg.a_load_ref_reg(exprasmlist,right.location.size,left.location.size,right.location.reference,left.location.register);
  512. end;
  513. LOC_FPUREGISTER,
  514. LOC_CFPUREGISTER :
  515. begin
  516. cg.a_loadfpu_ref_reg(exprasmlist,
  517. right.location.size,
  518. right.location.reference,
  519. left.location.register);
  520. end;
  521. LOC_REFERENCE,
  522. LOC_CREFERENCE :
  523. begin
  524. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  525. { Use unaligned copy when the offset is not aligned }
  526. len:=left.resulttype.def.size;
  527. if (right.location.reference.offset mod sizeof(aint)<>0) or
  528. (left.location.reference.offset mod sizeof(aint)<>0) or
  529. (right.resulttype.def.alignment<sizeof(aint)) then
  530. cg.g_concatcopy_unaligned(exprasmlist,right.location.reference,left.location.reference,len)
  531. else
  532. cg.g_concatcopy(exprasmlist,right.location.reference,left.location.reference,len);
  533. end;
  534. LOC_MMREGISTER,
  535. LOC_CMMREGISTER:
  536. cg.a_loadmm_ref_reg(exprasmlist,
  537. right.location.size,
  538. left.location.size,
  539. right.location.reference,
  540. left.location.register,mms_movescalar);
  541. else
  542. internalerror(200203284);
  543. end;
  544. end;
  545. {$ifdef SUPPORT_MMX}
  546. LOC_CMMXREGISTER,
  547. LOC_MMXREGISTER:
  548. begin
  549. if left.location.loc=LOC_CMMXREGISTER then
  550. cg.a_loadmm_reg_reg(exprasmlist,OS_M64,OS_M64,right.location.register,left.location.register,nil)
  551. else
  552. cg.a_loadmm_reg_ref(exprasmlist,OS_M64,OS_M64,right.location.register,left.location.reference,nil);
  553. end;
  554. {$endif SUPPORT_MMX}
  555. LOC_MMREGISTER,
  556. LOC_CMMREGISTER:
  557. begin
  558. if left.resulttype.def.deftype=arraydef then
  559. begin
  560. end
  561. else
  562. begin
  563. if left.location.loc=LOC_CMMREGISTER then
  564. cg.a_loadmm_reg_reg(exprasmlist,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  565. else
  566. cg.a_loadmm_reg_ref(exprasmlist,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  567. end;
  568. end;
  569. LOC_REGISTER,
  570. LOC_CREGISTER :
  571. begin
  572. {$ifndef cpu64bit}
  573. if left.location.size in [OS_64,OS_S64] then
  574. cg64.a_load64_reg_loc(exprasmlist,
  575. right.location.register64,left.location)
  576. else
  577. {$endif cpu64bit}
  578. cg.a_load_reg_loc(exprasmlist,right.location.size,right.location.register,left.location);
  579. end;
  580. LOC_FPUREGISTER,
  581. LOC_CFPUREGISTER :
  582. begin
  583. if (left.resulttype.def.deftype=floatdef) then
  584. fputyp:=tfloatdef(left.resulttype.def).typ
  585. else
  586. if (right.resulttype.def.deftype=floatdef) then
  587. fputyp:=tfloatdef(right.resulttype.def).typ
  588. else
  589. if (right.nodetype=typeconvn) and
  590. (ttypeconvnode(right).left.resulttype.def.deftype=floatdef) then
  591. fputyp:=tfloatdef(ttypeconvnode(right).left.resulttype.def).typ
  592. else
  593. fputyp:=s32real;
  594. { we can't do direct moves between fpu and mm registers }
  595. if left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER] then
  596. begin
  597. location_force_mmregscalar(exprasmlist,right.location,false);
  598. cg.a_loadmm_reg_reg(exprasmlist,
  599. tfloat2tcgsize[fputyp],tfloat2tcgsize[fputyp],
  600. right.location.register,left.location.register,mms_movescalar);
  601. end
  602. else
  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.