ncgld.pas 43 KB

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