ncgld.pas 42 KB

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