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. 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. old_allow_multi_pass2,
  359. releaseright : boolean;
  360. len : aint;
  361. r:Tregister;
  362. begin
  363. location_reset(location,LOC_VOID,OS_NO);
  364. otlabel:=truelabel;
  365. oflabel:=falselabel;
  366. objectlibrary.getlabel(truelabel);
  367. objectlibrary.getlabel(falselabel);
  368. {
  369. in most cases we can process first the right node which contains
  370. the most complex code. Exceptions for this are:
  371. - result is in flags, loading left will then destroy the flags
  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. when the right node returns as LOC_JUMP then we will generate
  380. the following code:
  381. rightnode
  382. true:
  383. leftnode
  384. assign 1
  385. false:
  386. leftnode
  387. assign 0
  388. }
  389. if (right.expectloc<>LOC_FLAGS) and
  390. ((right.expectloc=LOC_JUMP) or
  391. (right.nodetype=calln) or
  392. (right.resulttype.def.needs_inittable) or
  393. (right.registersint>=left.registersint)) then
  394. begin
  395. secondpass(right);
  396. { increment source reference counter, this is
  397. useless for string constants}
  398. if (right.resulttype.def.needs_inittable) and
  399. (right.nodetype<>stringconstn) then
  400. begin
  401. location_force_mem(exprasmlist,right.location);
  402. location_get_data_ref(exprasmlist,right.location,href,false);
  403. cg.g_incrrefcount(exprasmlist,right.resulttype.def,href);
  404. end;
  405. if codegenerror then
  406. exit;
  407. { We skip the generation of the left node when it's a jump, see
  408. explanation above }
  409. if (right.location.loc<>LOC_JUMP) and
  410. not(nf_concat_string in flags) then
  411. begin
  412. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  413. { can be false }
  414. secondpass(left);
  415. { decrement destination reference counter }
  416. if (left.resulttype.def.needs_inittable) then
  417. begin
  418. location_get_data_ref(exprasmlist,left.location,href,false);
  419. cg.g_decrrefcount(exprasmlist,left.resulttype.def,href);
  420. end;
  421. if codegenerror then
  422. exit;
  423. end;
  424. end
  425. else
  426. begin
  427. { calculate left sides }
  428. { don't do it yet if it's a crgister (JM) }
  429. if not(nf_concat_string in flags) then
  430. begin
  431. secondpass(left);
  432. { decrement destination reference counter }
  433. if (left.resulttype.def.needs_inittable) then
  434. begin
  435. location_get_data_ref(exprasmlist,left.location,href,false);
  436. cg.g_decrrefcount(exprasmlist,left.resulttype.def,href);
  437. end;
  438. if codegenerror then
  439. exit;
  440. end;
  441. { left can't be never a 64 bit LOC_REGISTER, so the 3. arg }
  442. { can be false }
  443. secondpass(right);
  444. { increment source reference counter, this is
  445. useless for string constants}
  446. if (right.resulttype.def.needs_inittable) and
  447. (right.nodetype<>stringconstn) then
  448. begin
  449. location_force_mem(exprasmlist,right.location);
  450. location_get_data_ref(exprasmlist,right.location,href,false);
  451. cg.g_incrrefcount(exprasmlist,right.resulttype.def,href);
  452. end;
  453. if codegenerror then
  454. exit;
  455. end;
  456. releaseright:=true;
  457. { optimize temp to temp copies }
  458. (* if (left.nodetype = temprefn) and
  459. { we may store certain temps in registers in the future, then this }
  460. { optimization will have to be adapted }
  461. (left.location.loc = LOC_REFERENCE) and
  462. (right.location.loc = LOC_REFERENCE) and
  463. tg.istemp(right.location.reference) and
  464. (tg.sizeoftemp(exprasmlist,right.location.reference) = tg.sizeoftemp(exprasmlist,left.location.reference)) then
  465. begin
  466. { in theory, we should also make sure the left temp type is }
  467. { already more or less of the same kind (ie. we must not }
  468. { assign an ansistring to a normaltemp). In practice, the }
  469. { assignment node will have already taken care of this for us }
  470. tcgtemprefnode(left).changelocation(right.location.reference);
  471. end
  472. { shortstring assignments are handled separately }
  473. else *) if is_shortstring(left.resulttype.def) then
  474. begin
  475. {
  476. we can get here only in the following situations
  477. for the right node:
  478. - empty constant string
  479. - char
  480. }
  481. { empty constant string }
  482. if (right.nodetype=stringconstn) and
  483. (tstringconstnode(right).len=0) then
  484. begin
  485. cg.a_load_const_ref(exprasmlist,OS_8,0,left.location.reference);
  486. end
  487. { char loading }
  488. else if is_char(right.resulttype.def) then
  489. begin
  490. if right.nodetype=ordconstn then
  491. begin
  492. if (target_info.endian = endian_little) then
  493. cg.a_load_const_ref(exprasmlist,OS_16,(tordconstnode(right).value shl 8) or 1,
  494. left.location.reference)
  495. else
  496. cg.a_load_const_ref(exprasmlist,OS_16,tordconstnode(right).value or (1 shl 8),
  497. left.location.reference);
  498. end
  499. else
  500. begin
  501. href:=left.location.reference;
  502. cg.a_load_const_ref(exprasmlist,OS_8,1,href);
  503. inc(href.offset,1);
  504. case right.location.loc of
  505. LOC_REGISTER,
  506. LOC_CREGISTER :
  507. begin
  508. r:=cg.makeregsize(exprasmlist,right.location.register,OS_8);
  509. cg.a_load_reg_ref(exprasmlist,OS_8,OS_8,r,href);
  510. end;
  511. LOC_REFERENCE,
  512. LOC_CREFERENCE :
  513. cg.a_load_ref_ref(exprasmlist,OS_8,OS_8,right.location.reference,href);
  514. else
  515. internalerror(200205111);
  516. end;
  517. end;
  518. end
  519. else
  520. internalerror(200204249);
  521. end
  522. else
  523. begin
  524. case right.location.loc of
  525. LOC_CONSTANT :
  526. begin
  527. {$ifndef cpu64bit}
  528. if right.location.size in [OS_64,OS_S64] then
  529. cg64.a_load64_const_loc(exprasmlist,right.location.value64,left.location)
  530. else
  531. {$endif cpu64bit}
  532. cg.a_load_const_loc(exprasmlist,right.location.value,left.location);
  533. end;
  534. LOC_REFERENCE,
  535. LOC_CREFERENCE :
  536. begin
  537. case left.location.loc of
  538. LOC_REGISTER,
  539. LOC_CREGISTER :
  540. begin
  541. {$ifndef cpu64bit}
  542. if left.location.size in [OS_64,OS_S64] then
  543. cg64.a_load64_ref_reg(exprasmlist,right.location.reference,left.location.register64)
  544. else
  545. {$endif cpu64bit}
  546. cg.a_load_ref_reg(exprasmlist,right.location.size,left.location.size,right.location.reference,left.location.register);
  547. end;
  548. LOC_FPUREGISTER,
  549. LOC_CFPUREGISTER :
  550. begin
  551. cg.a_loadfpu_ref_reg(exprasmlist,
  552. right.location.size,
  553. right.location.reference,
  554. left.location.register);
  555. end;
  556. LOC_REFERENCE,
  557. LOC_CREFERENCE :
  558. begin
  559. {$warning HACK: unaligned test, maybe remove all unaligned locations (array of char) from the compiler}
  560. { Use unaligned copy when the offset is not aligned }
  561. len:=left.resulttype.def.size;
  562. if (right.location.reference.offset mod sizeof(aint)<>0) or
  563. (left.location.reference.offset mod sizeof(aint)<>0) or
  564. (right.resulttype.def.alignment<sizeof(aint)) then
  565. cg.g_concatcopy_unaligned(exprasmlist,right.location.reference,left.location.reference,len)
  566. else
  567. cg.g_concatcopy(exprasmlist,right.location.reference,left.location.reference,len);
  568. end;
  569. else
  570. internalerror(200203284);
  571. end;
  572. end;
  573. {$ifdef SUPPORT_MMX}
  574. LOC_CMMXREGISTER,
  575. LOC_MMXREGISTER:
  576. begin
  577. if left.location.loc=LOC_CMMXREGISTER then
  578. cg.a_loadmm_reg_reg(exprasmlist,right.location.register,left.location.register)
  579. else
  580. cg.a_loadmm_reg_ref(exprasmlist,right.location.register,left.location.reference);
  581. end;
  582. {$endif SUPPORT_MMX}
  583. LOC_MMREGISTER,
  584. LOC_CMMREGISTER:
  585. begin
  586. if left.resulttype.def.deftype=arraydef then
  587. begin
  588. end
  589. else
  590. begin
  591. if left.location.loc=LOC_CMMREGISTER then
  592. cg.a_loadmm_reg_reg(exprasmlist,right.location.size,left.location.size,right.location.register,left.location.register,mms_movescalar)
  593. else
  594. cg.a_loadmm_reg_ref(exprasmlist,right.location.size,left.location.size,right.location.register,left.location.reference,mms_movescalar);
  595. end;
  596. end;
  597. LOC_REGISTER,
  598. LOC_CREGISTER :
  599. begin
  600. {$ifndef cpu64bit}
  601. if left.location.size in [OS_64,OS_S64] then
  602. cg64.a_load64_reg_loc(exprasmlist,
  603. right.location.register64,left.location)
  604. else
  605. {$endif cpu64bit}
  606. cg.a_load_reg_loc(exprasmlist,right.location.size,right.location.register,left.location);
  607. end;
  608. LOC_FPUREGISTER,
  609. LOC_CFPUREGISTER :
  610. begin
  611. if (left.resulttype.def.deftype=floatdef) then
  612. fputyp:=tfloatdef(left.resulttype.def).typ
  613. else
  614. if (right.resulttype.def.deftype=floatdef) then
  615. fputyp:=tfloatdef(right.resulttype.def).typ
  616. else
  617. if (right.nodetype=typeconvn) and
  618. (ttypeconvnode(right).left.resulttype.def.deftype=floatdef) then
  619. fputyp:=tfloatdef(ttypeconvnode(right).left.resulttype.def).typ
  620. else
  621. fputyp:=s32real;
  622. cg.a_loadfpu_reg_loc(exprasmlist,
  623. tfloat2tcgsize[fputyp],
  624. right.location.register,left.location);
  625. end;
  626. LOC_JUMP :
  627. begin
  628. objectlibrary.getlabel(hlabel);
  629. { generate the leftnode for the true case, and
  630. release the location }
  631. cg.a_label(exprasmlist,truelabel);
  632. secondpass(left);
  633. if codegenerror then
  634. exit;
  635. cg.a_load_const_loc(exprasmlist,1,left.location);
  636. cg.a_jmp_always(exprasmlist,hlabel);
  637. { generate the leftnode for the false case }
  638. cg.a_label(exprasmlist,falselabel);
  639. old_allow_multi_pass2:=allow_multi_pass2;
  640. allow_multi_pass2:=true;
  641. secondpass(left);
  642. allow_multi_pass2:=old_allow_multi_pass2;
  643. if codegenerror then
  644. exit;
  645. cg.a_load_const_loc(exprasmlist,0,left.location);
  646. cg.a_label(exprasmlist,hlabel);
  647. end;
  648. {$ifdef cpuflags}
  649. LOC_FLAGS :
  650. begin
  651. {This can be a wordbool or longbool too, no?}
  652. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  653. cg.g_flags2reg(exprasmlist,def_cgsize(left.resulttype.def),right.location.resflags,left.location.register)
  654. else
  655. begin
  656. if not(left.location.loc = LOC_REFERENCE) then
  657. internalerror(200203273);
  658. cg.g_flags2ref(exprasmlist,def_cgsize(left.resulttype.def),right.location.resflags,left.location.reference);
  659. end;
  660. end;
  661. {$endif cpuflags}
  662. end;
  663. end;
  664. if releaseright then
  665. location_freetemp(exprasmlist,right.location);
  666. truelabel:=otlabel;
  667. falselabel:=oflabel;
  668. end;
  669. {*****************************************************************************
  670. SecondArrayConstruct
  671. *****************************************************************************}
  672. const
  673. vtInteger = 0;
  674. vtBoolean = 1;
  675. vtChar = 2;
  676. vtExtended = 3;
  677. vtString = 4;
  678. vtPointer = 5;
  679. vtPChar = 6;
  680. vtObject = 7;
  681. vtClass = 8;
  682. vtWideChar = 9;
  683. vtPWideChar = 10;
  684. vtAnsiString32 = 11;
  685. vtCurrency = 12;
  686. vtVariant = 13;
  687. vtInterface = 14;
  688. vtWideString = 15;
  689. vtInt64 = 16;
  690. vtQWord = 17;
  691. vtAnsiString16 = 18;
  692. vtAnsiString64 = 19;
  693. procedure tcgarrayconstructornode.pass_2;
  694. var
  695. hp : tarrayconstructornode;
  696. href : treference;
  697. lt : tdef;
  698. vaddr : boolean;
  699. vtype : longint;
  700. freetemp,
  701. dovariant : boolean;
  702. elesize : longint;
  703. tmpreg : tregister;
  704. paraloc : tcgparalocation;
  705. begin
  706. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  707. if dovariant then
  708. elesize:=sizeof(aint)+sizeof(aint)
  709. else
  710. elesize:=tarraydef(resulttype.def).elesize;
  711. location_reset(location,LOC_CREFERENCE,OS_NO);
  712. fillchar(paraloc,sizeof(paraloc),0);
  713. { Allocate always a temp, also if no elements are required, to
  714. be sure that location is valid (PFV) }
  715. if tarraydef(resulttype.def).highrange=-1 then
  716. tg.GetTemp(exprasmlist,elesize,tt_normal,location.reference)
  717. else
  718. tg.GetTemp(exprasmlist,(tarraydef(resulttype.def).highrange+1)*elesize,tt_normal,location.reference);
  719. href:=location.reference;
  720. { Process nodes in array constructor }
  721. hp:=self;
  722. while assigned(hp) do
  723. begin
  724. if assigned(hp.left) then
  725. begin
  726. freetemp:=true;
  727. secondpass(hp.left);
  728. if codegenerror then
  729. exit;
  730. { Move flags and jump in register }
  731. if hp.left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  732. location_force_reg(exprasmlist,hp.left.location,def_cgsize(hp.left.resulttype.def),false);
  733. if dovariant then
  734. begin
  735. { find the correct vtype value }
  736. vtype:=$ff;
  737. vaddr:=false;
  738. lt:=hp.left.resulttype.def;
  739. case lt.deftype of
  740. enumdef,
  741. orddef :
  742. begin
  743. if is_64bit(lt) then
  744. begin
  745. case torddef(lt).typ of
  746. s64bit:
  747. vtype:=vtInt64;
  748. u64bit:
  749. vtype:=vtQWord;
  750. end;
  751. freetemp:=false;
  752. vaddr:=true;
  753. end
  754. else if (lt.deftype=enumdef) or
  755. is_integer(lt) then
  756. vtype:=vtInteger
  757. else
  758. if is_boolean(lt) then
  759. vtype:=vtBoolean
  760. else
  761. if (lt.deftype=orddef) then
  762. begin
  763. case torddef(lt).typ of
  764. uchar:
  765. vtype:=vtChar;
  766. uwidechar:
  767. vtype:=vtWideChar;
  768. end;
  769. end;
  770. end;
  771. floatdef :
  772. begin
  773. vtype:=vtExtended;
  774. freetemp:=false;
  775. vaddr:=true;
  776. end;
  777. procvardef,
  778. pointerdef :
  779. begin
  780. if is_pchar(lt) then
  781. vtype:=vtPChar
  782. else if is_pwidechar(lt) then
  783. vtype:=vtPWideChar
  784. else
  785. vtype:=vtPointer;
  786. end;
  787. variantdef :
  788. begin
  789. vtype:=vtVariant;
  790. vaddr:=true;
  791. freetemp:=false;
  792. end;
  793. classrefdef :
  794. vtype:=vtClass;
  795. objectdef :
  796. vtype:=vtObject;
  797. stringdef :
  798. begin
  799. if is_shortstring(lt) then
  800. begin
  801. vtype:=vtString;
  802. vaddr:=true;
  803. freetemp:=false;
  804. end
  805. else
  806. if is_ansistring(lt) then
  807. {$ifdef ansistring_bits}
  808. begin
  809. case Tstringdef(lt).string_typ of
  810. st_ansistring16:
  811. vtype:=vtAnsiString16;
  812. st_ansistring32:
  813. vtype:=vtAnsiString32;
  814. st_ansistring64:
  815. vtype:=vtAnsiString64;
  816. end;
  817. freetemp:=false;
  818. end
  819. {$else}
  820. begin
  821. vtype:=vtAnsiString;
  822. freetemp:=false;
  823. end
  824. {$endif}
  825. else
  826. if is_widestring(lt) then
  827. begin
  828. vtype:=vtWideString;
  829. freetemp:=false;
  830. end;
  831. end;
  832. end;
  833. if vtype=$ff then
  834. internalerror(14357);
  835. { write changing field update href to the next element }
  836. inc(href.offset,sizeof(aint));
  837. if vaddr then
  838. begin
  839. location_force_mem(exprasmlist,hp.left.location);
  840. tmpreg:=cg.getaddressregister(exprasmlist);
  841. cg.a_loadaddr_ref_reg(exprasmlist,hp.left.location.reference,tmpreg);
  842. cg.a_load_reg_ref(exprasmlist,OS_ADDR,OS_ADDR,tmpreg,href);
  843. end
  844. else
  845. cg.a_load_loc_ref(exprasmlist,OS_ADDR,hp.left.location,href);
  846. { update href to the vtype field and write it }
  847. dec(href.offset,sizeof(aint));
  848. cg.a_load_const_ref(exprasmlist, OS_INT,vtype,href);
  849. { goto next array element }
  850. inc(href.offset,sizeof(aint)*2);
  851. end
  852. else
  853. { normal array constructor of the same type }
  854. begin
  855. if (is_ansistring(left.resulttype.def) or
  856. is_widestring(left.resulttype.def) or
  857. (left.resulttype.def.deftype=variantdef)) then
  858. freetemp:=false;
  859. case hp.left.location.loc of
  860. LOC_FPUREGISTER,
  861. LOC_CFPUREGISTER :
  862. cg.a_loadfpu_reg_ref(exprasmlist,hp.left.location.size,hp.left.location.register,href);
  863. LOC_REFERENCE,
  864. LOC_CREFERENCE :
  865. begin
  866. if is_shortstring(hp.left.resulttype.def) then
  867. cg.g_copyshortstring(exprasmlist,hp.left.location.reference,href,
  868. Tstringdef(hp.left.resulttype.def).len)
  869. else
  870. cg.g_concatcopy(exprasmlist,hp.left.location.reference,href,elesize);
  871. end;
  872. else
  873. begin
  874. {$ifndef cpu64bit}
  875. if hp.left.location.size in [OS_64,OS_S64] then
  876. cg64.a_load64_loc_ref(exprasmlist,hp.left.location,href)
  877. else
  878. {$endif cpu64bit}
  879. cg.a_load_loc_ref(exprasmlist,hp.left.location.size,hp.left.location,href);
  880. end;
  881. end;
  882. inc(href.offset,elesize);
  883. end;
  884. if freetemp then
  885. location_freetemp(exprasmlist,hp.left.location);
  886. end;
  887. { load next entry }
  888. hp:=tarrayconstructornode(hp.right);
  889. end;
  890. end;
  891. {*****************************************************************************
  892. SecondRTTI
  893. *****************************************************************************}
  894. procedure tcgrttinode.pass_2;
  895. begin
  896. location_reset(location,LOC_CREFERENCE,OS_NO);
  897. location.reference.symbol:=rttidef.get_rtti_label(rttitype);
  898. end;
  899. begin
  900. cloadnode:=tcgloadnode;
  901. cassignmentnode:=tcgassignmentnode;
  902. carrayconstructornode:=tcgarrayconstructornode;
  903. crttinode:=tcgrttinode;
  904. end.
  905. {
  906. $Log$
  907. Revision 1.139 2005-02-14 17:13:06 peter
  908. * truncate log
  909. Revision 1.138 2005/02/13 19:57:15 florian
  910. * better alignment checking
  911. Revision 1.137 2005/02/10 21:54:36 peter
  912. * data with inittables need to have a memory location assigned
  913. for incrref
  914. Revision 1.136 2005/01/23 17:14:21 florian
  915. + optimized code generation on sparc
  916. + some stuff for pic code on sparc added
  917. Revision 1.135 2005/01/04 16:37:09 peter
  918. * don't release temps for array of ansistring
  919. }