ncgld.pas 41 KB

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