nld.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for load/assignment nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nld;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. {$ifdef state_tracking}
  24. nstate,
  25. {$endif}
  26. symconst,symbase,symtype,symsym,symdef;
  27. type
  28. tloadnode = class(tunarynode)
  29. symtableentry : tsym;
  30. symtableentryderef : tderef;
  31. symtable : tsymtable;
  32. procdef : tprocdef;
  33. procdefderef : tderef;
  34. constructor create(v : tsym;st : tsymtable);virtual;
  35. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure buildderefimpl;override;
  39. procedure derefimpl;override;
  40. procedure set_mp(p:tnode);
  41. function is_addr_param_load:boolean;
  42. function getcopy : tnode;override;
  43. function pass_1 : tnode;override;
  44. function det_resulttype:tnode;override;
  45. procedure mark_write;override;
  46. function docompare(p: tnode): boolean; override;
  47. procedure printnodedata(var t:text);override;
  48. end;
  49. tloadnodeclass = class of tloadnode;
  50. { different assignment types }
  51. tassigntype = (at_normal,at_plus,at_minus,at_star,at_slash);
  52. tassignmentnode = class(tbinarynode)
  53. assigntype : tassigntype;
  54. constructor create(l,r : tnode);virtual;
  55. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  56. procedure ppuwrite(ppufile:tcompilerppufile);override;
  57. function getcopy : tnode;override;
  58. function pass_1 : tnode;override;
  59. function det_resulttype:tnode;override;
  60. {$ifdef state_tracking}
  61. function track_state_pass(exec_known:boolean):boolean;override;
  62. {$endif state_tracking}
  63. function docompare(p: tnode): boolean; override;
  64. end;
  65. tassignmentnodeclass = class of tassignmentnode;
  66. tarrayconstructorrangenode = class(tbinarynode)
  67. constructor create(l,r : tnode);virtual;
  68. function pass_1 : tnode;override;
  69. function det_resulttype:tnode;override;
  70. end;
  71. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  72. tarrayconstructornode = class(tbinarynode)
  73. constructor create(l,r : tnode);virtual;
  74. function getcopy : tnode;override;
  75. function pass_1 : tnode;override;
  76. function det_resulttype:tnode;override;
  77. function docompare(p: tnode): boolean; override;
  78. procedure force_type(tt:ttype);
  79. procedure insert_typeconvs;
  80. end;
  81. tarrayconstructornodeclass = class of tarrayconstructornode;
  82. ttypenode = class(tnode)
  83. allowed : boolean;
  84. restype : ttype;
  85. constructor create(t : ttype);virtual;
  86. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderefimpl;override;
  89. procedure derefimpl;override;
  90. function pass_1 : tnode;override;
  91. function det_resulttype:tnode;override;
  92. function docompare(p: tnode): boolean; override;
  93. end;
  94. ttypenodeclass = class of ttypenode;
  95. trttinode = class(tnode)
  96. l1,l2 : longint;
  97. rttitype : trttitype;
  98. rttidef : tstoreddef;
  99. rttidefderef : tderef;
  100. constructor create(def:tstoreddef;rt:trttitype);virtual;
  101. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  102. procedure ppuwrite(ppufile:tcompilerppufile);override;
  103. procedure buildderefimpl;override;
  104. procedure derefimpl;override;
  105. function getcopy : tnode;override;
  106. function pass_1 : tnode;override;
  107. function det_resulttype:tnode;override;
  108. function docompare(p: tnode): boolean; override;
  109. end;
  110. trttinodeclass = class of trttinode;
  111. var
  112. cloadnode : tloadnodeclass;
  113. cassignmentnode : tassignmentnodeclass;
  114. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  115. carrayconstructornode : tarrayconstructornodeclass;
  116. ctypenode : ttypenodeclass;
  117. crttinode : trttinodeclass;
  118. implementation
  119. uses
  120. cutils,verbose,globtype,globals,systems,
  121. symnot,
  122. defutil,defcmp,
  123. htypechk,pass_1,procinfo,paramgr,
  124. ncon,ninl,ncnv,nmem,ncal,nutils,
  125. cgobj,cgbase
  126. ;
  127. {*****************************************************************************
  128. TLOADNODE
  129. *****************************************************************************}
  130. constructor tloadnode.create(v : tsym;st : tsymtable);
  131. begin
  132. inherited create(loadn,nil);
  133. if not assigned(v) then
  134. internalerror(200108121);
  135. symtableentry:=v;
  136. symtable:=st;
  137. procdef:=nil;
  138. end;
  139. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  140. begin
  141. inherited create(loadn,nil);
  142. if not assigned(v) then
  143. internalerror(200108121);
  144. symtableentry:=v;
  145. symtable:=st;
  146. procdef:=d;
  147. end;
  148. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  149. begin
  150. inherited ppuload(t,ppufile);
  151. ppufile.getderef(symtableentryderef);
  152. symtable:=nil;
  153. ppufile.getderef(procdefderef);
  154. end;
  155. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  156. begin
  157. inherited ppuwrite(ppufile);
  158. ppufile.putderef(symtableentryderef);
  159. ppufile.putderef(procdefderef);
  160. end;
  161. procedure tloadnode.buildderefimpl;
  162. begin
  163. inherited buildderefimpl;
  164. symtableentryderef.build(symtableentry);
  165. procdefderef.build(procdef);
  166. end;
  167. procedure tloadnode.derefimpl;
  168. begin
  169. inherited derefimpl;
  170. symtableentry:=tsym(symtableentryderef.resolve);
  171. symtable:=symtableentry.owner;
  172. procdef:=tprocdef(procdefderef.resolve);
  173. end;
  174. procedure tloadnode.set_mp(p:tnode);
  175. begin
  176. { typen nodes should not be set }
  177. if p.nodetype=typen then
  178. internalerror(200301042);
  179. left:=p;
  180. end;
  181. function tloadnode.getcopy : tnode;
  182. var
  183. n : tloadnode;
  184. begin
  185. n:=tloadnode(inherited getcopy);
  186. n.symtable:=symtable;
  187. n.symtableentry:=symtableentry;
  188. n.procdef:=procdef;
  189. result:=n;
  190. end;
  191. function tloadnode.is_addr_param_load:boolean;
  192. begin
  193. result:=(symtable.symtabletype=parasymtable) and
  194. (symtableentry.typ=paravarsym) and
  195. not(vo_has_local_copy in tparavarsym(symtableentry).varoptions) and
  196. not(nf_load_self_pointer in flags) and
  197. paramanager.push_addr_param(tparavarsym(symtableentry).varspez,tparavarsym(symtableentry).vartype.def,tprocdef(symtable.defowner).proccalloption);
  198. end;
  199. function tloadnode.det_resulttype:tnode;
  200. begin
  201. result:=nil;
  202. case symtableentry.typ of
  203. absolutevarsym :
  204. resulttype:=tabsolutevarsym(symtableentry).vartype;
  205. constsym:
  206. begin
  207. if tconstsym(symtableentry).consttyp=constresourcestring then
  208. begin
  209. {$ifdef ansistring_bits}
  210. case aktansistring_bits of
  211. sb_16:
  212. resulttype:=cansistringtype16;
  213. sb_32:
  214. resulttype:=cansistringtype32;
  215. sb_64:
  216. resulttype:=cansistringtype64;
  217. end;
  218. {$else}
  219. resulttype:=cansistringtype
  220. {$endif}
  221. end
  222. else
  223. internalerror(22799);
  224. end;
  225. globalvarsym,
  226. paravarsym,
  227. localvarsym :
  228. begin
  229. inc(tabstractvarsym(symtableentry).refs);
  230. { Nested variable? The we need to load the framepointer of
  231. the parent procedure }
  232. if assigned(current_procinfo) then
  233. begin
  234. if (symtable.symtabletype in [localsymtable,parasymtable]) and
  235. (symtable.symtablelevel<>current_procinfo.procdef.parast.symtablelevel) then
  236. begin
  237. if assigned(left) then
  238. internalerror(200309289);
  239. left:=cloadparentfpnode.create(tprocdef(symtable.defowner));
  240. { reference in nested procedures, variable needs to be in memory }
  241. make_not_regable(self);
  242. end;
  243. { static variables referenced in procedures or from finalization,
  244. variable needs to be in memory.
  245. It is too hard and the benefit is too small to detect whether a
  246. variable is only used in the finalization to add support for it (PFV) }
  247. if (symtable.symtabletype=staticsymtable) and
  248. (
  249. (symtable.symtablelevel<>current_procinfo.procdef.localst.symtablelevel) or
  250. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)
  251. ) then
  252. make_not_regable(self);
  253. end;
  254. { fix self type which is declared as voidpointer in the
  255. definition }
  256. if vo_is_self in tabstractvarsym(symtableentry).varoptions then
  257. begin
  258. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  259. if (po_classmethod in tprocdef(symtableentry.owner.defowner).procoptions) or
  260. (po_staticmethod in tprocdef(symtableentry.owner.defowner).procoptions) then
  261. resulttype.setdef(tclassrefdef.create(resulttype))
  262. else if is_object(resulttype.def) and
  263. (nf_load_self_pointer in flags) then
  264. resulttype.setdef(tpointerdef.create(resulttype));
  265. end
  266. else if vo_is_vmt in tabstractvarsym(symtableentry).varoptions then
  267. begin
  268. resulttype.setdef(tprocdef(symtableentry.owner.defowner)._class);
  269. resulttype.setdef(tclassrefdef.create(resulttype));
  270. end
  271. else
  272. resulttype:=tabstractvarsym(symtableentry).vartype;
  273. end;
  274. typedconstsym :
  275. resulttype:=ttypedconstsym(symtableentry).typedconsttype;
  276. procsym :
  277. begin
  278. if not assigned(procdef) then
  279. begin
  280. if Tprocsym(symtableentry).procdef_count>1 then
  281. CGMessage(parser_e_no_overloaded_procvars);
  282. procdef:=tprocsym(symtableentry).first_procdef;
  283. end;
  284. { the result is a procdef, addrn and proc_to_procvar
  285. typeconvn need this as resulttype so they know
  286. that the address needs to be returned }
  287. resulttype.setdef(procdef);
  288. { process methodpointer }
  289. if assigned(left) then
  290. resulttypepass(left);
  291. end;
  292. labelsym:
  293. resulttype:=voidtype;
  294. else
  295. internalerror(200104141);
  296. end;
  297. end;
  298. procedure Tloadnode.mark_write;
  299. begin
  300. include(flags,nf_write);
  301. end;
  302. function tloadnode.pass_1 : tnode;
  303. begin
  304. result:=nil;
  305. expectloc:=LOC_REFERENCE;
  306. registersint:=0;
  307. registersfpu:=0;
  308. {$ifdef SUPPORT_MMX}
  309. registersmmx:=0;
  310. {$endif SUPPORT_MMX}
  311. case symtableentry.typ of
  312. absolutevarsym :
  313. ;
  314. constsym:
  315. begin
  316. if tconstsym(symtableentry).consttyp=constresourcestring then
  317. begin
  318. include(current_procinfo.flags,pi_needs_implicit_finally);
  319. expectloc:=LOC_CREFERENCE;
  320. end;
  321. end;
  322. globalvarsym,
  323. localvarsym,
  324. paravarsym :
  325. begin
  326. if assigned(left) then
  327. firstpass(left);
  328. if not is_addr_param_load and
  329. tabstractvarsym(symtableentry).is_regvar then
  330. begin
  331. case tabstractvarsym(symtableentry).varregable of
  332. vr_intreg :
  333. expectloc:=LOC_CREGISTER;
  334. vr_fpureg :
  335. expectloc:=LOC_CFPUREGISTER;
  336. vr_mmreg :
  337. expectloc:=LOC_CMMREGISTER;
  338. end
  339. end
  340. else
  341. if (tabstractvarsym(symtableentry).varspez=vs_const) then
  342. expectloc:=LOC_CREFERENCE;
  343. { we need a register for call by reference parameters }
  344. if paramanager.push_addr_param(tabstractvarsym(symtableentry).varspez,tabstractvarsym(symtableentry).vartype.def,pocall_default) then
  345. registersint:=1;
  346. if ([vo_is_thread_var,vo_is_dll_var]*tabstractvarsym(symtableentry).varoptions)<>[] then
  347. registersint:=1;
  348. if (target_info.system=system_powerpc_darwin) and (vo_is_dll_var in tabstractvarsym(symtableentry).varoptions) then
  349. include(current_procinfo.flags,pi_needs_got);
  350. { call to get address of threadvar }
  351. if (vo_is_thread_var in tabstractvarsym(symtableentry).varoptions) then
  352. include(current_procinfo.flags,pi_do_call);
  353. if nf_write in flags then
  354. Tabstractvarsym(symtableentry).trigger_notifications(vn_onwrite)
  355. else
  356. Tabstractvarsym(symtableentry).trigger_notifications(vn_onread);
  357. { count variable references }
  358. if cg.t_times>1 then
  359. inc(tabstractvarsym(symtableentry).refs,cg.t_times-1);
  360. end;
  361. typedconstsym :
  362. ;
  363. procsym :
  364. begin
  365. { method pointer ? }
  366. if assigned(left) then
  367. begin
  368. expectloc:=LOC_CREFERENCE;
  369. firstpass(left);
  370. registersint:=max(registersint,left.registersint);
  371. registersfpu:=max(registersfpu,left.registersfpu);
  372. {$ifdef SUPPORT_MMX}
  373. registersmmx:=max(registersmmx,left.registersmmx);
  374. {$endif SUPPORT_MMX}
  375. end;
  376. end;
  377. labelsym :
  378. ;
  379. else
  380. internalerror(200104143);
  381. end;
  382. end;
  383. function tloadnode.docompare(p: tnode): boolean;
  384. begin
  385. docompare :=
  386. inherited docompare(p) and
  387. (symtableentry = tloadnode(p).symtableentry) and
  388. (procdef = tloadnode(p).procdef) and
  389. (symtable = tloadnode(p).symtable);
  390. end;
  391. procedure Tloadnode.printnodedata(var t:text);
  392. begin
  393. inherited printnodedata(t);
  394. write(t,printnodeindention,'symbol = ',symtableentry.name);
  395. if symtableentry.typ=procsym then
  396. write(t,printnodeindention,'procdef = ',procdef.mangledname);
  397. writeln(t,'');
  398. end;
  399. {*****************************************************************************
  400. TASSIGNMENTNODE
  401. *****************************************************************************}
  402. constructor tassignmentnode.create(l,r : tnode);
  403. begin
  404. inherited create(assignn,l,r);
  405. l.mark_write;
  406. assigntype:=at_normal;
  407. end;
  408. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  409. begin
  410. inherited ppuload(t,ppufile);
  411. assigntype:=tassigntype(ppufile.getbyte);
  412. end;
  413. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  414. begin
  415. inherited ppuwrite(ppufile);
  416. ppufile.putbyte(byte(assigntype));
  417. end;
  418. function tassignmentnode.getcopy : tnode;
  419. var
  420. n : tassignmentnode;
  421. begin
  422. n:=tassignmentnode(inherited getcopy);
  423. n.assigntype:=assigntype;
  424. getcopy:=n;
  425. end;
  426. function tassignmentnode.det_resulttype:tnode;
  427. var
  428. hp : tnode;
  429. useshelper : boolean;
  430. original_size : longint;
  431. begin
  432. result:=nil;
  433. resulttype:=voidtype;
  434. original_size := 0;
  435. { must be made unique }
  436. set_unique(left);
  437. resulttypepass(left);
  438. if is_ansistring(left.resulttype.def) then
  439. begin
  440. { fold <ansistring>:=<ansistring>+<char|shortstring|ansistring> }
  441. if (right.nodetype=addn) and
  442. left.isequal(tbinarynode(right).left) and
  443. { don't fold multiple concatenations else we could get trouble
  444. with multiple uses of s
  445. }
  446. (tbinarynode(right).left.nodetype<>addn) and
  447. (tbinarynode(right).right.nodetype<>addn) then
  448. begin
  449. { don't do a resulttypepass(right), since then the addnode }
  450. { may insert typeconversions that make this optimization }
  451. { opportunity quite difficult to detect (JM) }
  452. resulttypepass(tbinarynode(right).left);
  453. resulttypepass(tbinarynode(right).right);
  454. if (is_char(tbinarynode(right).right.resulttype.def) or
  455. is_shortstring(tbinarynode(right).right.resulttype.def) or
  456. is_ansistring(tbinarynode(right).right.resulttype.def)) then
  457. begin
  458. { remove property flag so it'll not trigger an error }
  459. exclude(left.flags,nf_isproperty);
  460. { generate call to helper }
  461. hp:=ccallparanode.create(tbinarynode(right).right,
  462. ccallparanode.create(left,nil));
  463. if is_char(tbinarynode(right).right.resulttype.def) then
  464. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_char',hp)
  465. else if is_shortstring(tbinarynode(right).right.resulttype.def) then
  466. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_shortstring',hp)
  467. else
  468. result:=ccallnode.createintern('fpc_'+Tstringdef(left.resulttype.def).stringtypname+'_append_ansistring',hp);
  469. tbinarynode(right).right:=nil;
  470. left:=nil;
  471. exit;
  472. end;
  473. end;
  474. end
  475. else
  476. if is_shortstring(left.resulttype.def) then
  477. begin
  478. { fold <shortstring>:=<shortstring>+<shortstring>,
  479. <shortstring>+<char> is handled by an optimized node }
  480. if (right.nodetype=addn) and
  481. left.isequal(tbinarynode(right).left) and
  482. { don't fold multiple concatenations else we could get trouble
  483. with multiple uses of s }
  484. (tbinarynode(right).left.nodetype<>addn) and
  485. (tbinarynode(right).right.nodetype<>addn) then
  486. begin
  487. { don't do a resulttypepass(right), since then the addnode }
  488. { may insert typeconversions that make this optimization }
  489. { opportunity quite difficult to detect (JM) }
  490. resulttypepass(tbinarynode(right).left);
  491. resulttypepass(tbinarynode(right).right);
  492. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  493. begin
  494. { remove property flag so it'll not trigger an error }
  495. exclude(left.flags,nf_isproperty);
  496. { generate call to helper }
  497. hp:=ccallparanode.create(tbinarynode(right).right,
  498. ccallparanode.create(left,nil));
  499. if is_shortstring(tbinarynode(right).right.resulttype.def) then
  500. result:=ccallnode.createintern('fpc_shortstr_append_shortstr',hp);
  501. tbinarynode(right).right:=nil;
  502. left:=nil;
  503. exit;
  504. end;
  505. end;
  506. end;
  507. resulttypepass(right);
  508. set_varstate(left,vs_assigned,false);
  509. set_varstate(right,vs_used,true);
  510. if codegenerror then
  511. exit;
  512. { tp procvar support, when we don't expect a procvar
  513. then we need to call the procvar }
  514. if (left.resulttype.def.deftype<>procvardef) then
  515. maybe_call_procvar(right,true);
  516. { assignments to formaldefs and open arrays aren't allowed }
  517. if (left.resulttype.def.deftype=formaldef) or
  518. is_open_array(left.resulttype.def) then
  519. CGMessage(type_e_operator_not_allowed);
  520. { test if node can be assigned, properties are allowed }
  521. valid_for_assignment(left);
  522. { assigning nil to a dynamic array clears the array }
  523. if is_dynamic_array(left.resulttype.def) and
  524. (right.nodetype=niln) then
  525. begin
  526. hp:=ccallparanode.create(caddrnode.create
  527. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  528. ccallparanode.create(ctypeconvnode.create_internal(left,voidpointertype),nil));
  529. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  530. left:=nil;
  531. exit;
  532. end;
  533. { shortstring helpers can do the conversion directly,
  534. so treat them separatly }
  535. if (is_shortstring(left.resulttype.def)) then
  536. begin
  537. { insert typeconv, except for chars that are handled in
  538. secondpass and except for ansi/wide string that can
  539. be converted immediatly }
  540. if not(is_char(right.resulttype.def) or
  541. (right.resulttype.def.deftype=stringdef)) then
  542. inserttypeconv(right,left.resulttype);
  543. if right.resulttype.def.deftype=stringdef then
  544. begin
  545. useshelper:=true;
  546. { convert constant strings to shortstrings. But
  547. skip empty constant strings, that will be handled
  548. in secondpass }
  549. if (right.nodetype=stringconstn) then
  550. begin
  551. { verify if range fits within shortstring }
  552. { just emit a warning, delphi gives an }
  553. { error, only if the type definition of }
  554. { of the string is less < 255 characters }
  555. if not is_open_string(left.resulttype.def) and
  556. (tstringconstnode(right).len > tstringdef(left.resulttype.def).len) then
  557. cgmessage(type_w_string_too_long);
  558. inserttypeconv(right,left.resulttype);
  559. if (tstringconstnode(right).len=0) then
  560. useshelper:=false;
  561. end;
  562. { rest is done in pass 1 (JM) }
  563. if useshelper then
  564. exit;
  565. end
  566. end
  567. else
  568. begin
  569. { get the size before the type conversion - check for all nodes }
  570. if assigned(right.resulttype.def) and
  571. (right.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  572. (right.nodetype in [loadn,vecn,calln]) then
  573. original_size := right.resulttype.def.size;
  574. inserttypeconv(right,left.resulttype);
  575. end;
  576. { check if the assignment may cause a range check error }
  577. { if its not explicit, and only if the values are }
  578. { ordinals, enumdef and floatdef }
  579. if (right.nodetype = typeconvn) and
  580. not (nf_explicit in ttypeconvnode(right).flags) then
  581. begin
  582. if assigned(left.resulttype.def) and
  583. (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) then
  584. begin
  585. if (original_size <> 0) and (left.resulttype.def.size < original_size) then
  586. begin
  587. if (cs_check_range in aktlocalswitches) then
  588. Message(type_w_smaller_possible_range_check)
  589. else
  590. Message(type_h_smaller_possible_range_check);
  591. end;
  592. end;
  593. end;
  594. { call helpers for interface }
  595. if is_interfacecom(left.resulttype.def) then
  596. begin
  597. hp:=ccallparanode.create(ctypeconvnode.create_internal
  598. (right,voidpointertype),
  599. ccallparanode.create(ctypeconvnode.create_internal
  600. (left,voidpointertype),nil));
  601. result:=ccallnode.createintern('fpc_intf_assign',hp);
  602. left:=nil;
  603. right:=nil;
  604. exit;
  605. end;
  606. { check if local proc/func is assigned to procvar }
  607. if right.resulttype.def.deftype=procvardef then
  608. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  609. end;
  610. function tassignmentnode.pass_1 : tnode;
  611. var
  612. hp: tnode;
  613. begin
  614. result:=nil;
  615. expectloc:=LOC_VOID;
  616. firstpass(left);
  617. firstpass(right);
  618. { assignment to refcounted variable -> inc/decref }
  619. if (not is_class(left.resulttype.def) and
  620. left.resulttype.def.needs_inittable) then
  621. include(current_procinfo.flags,pi_do_call);
  622. if codegenerror then
  623. exit;
  624. if (is_shortstring(left.resulttype.def)) then
  625. begin
  626. if right.resulttype.def.deftype=stringdef then
  627. begin
  628. if (right.nodetype<>stringconstn) or
  629. (tstringconstnode(right).len<>0) then
  630. begin
  631. if (cs_optimize in aktglobalswitches) and
  632. (right.nodetype in [calln,blockn]) and
  633. (left.nodetype = temprefn) and
  634. is_shortstring(right.resulttype.def) and
  635. not is_open_string(left.resulttype.def) and
  636. (tstringdef(left.resulttype.def).len = 255) then
  637. begin
  638. { the blocknode case is handled in pass_2 at the temp }
  639. { reference level (mainly for callparatemp) (JM) }
  640. if (right.nodetype = calln) then
  641. begin
  642. tcallnode(right).funcretnode := left;
  643. result := right;
  644. end
  645. else
  646. exit;
  647. end
  648. else
  649. begin
  650. hp:=ccallparanode.create
  651. (right,
  652. ccallparanode.create(cinlinenode.create
  653. (in_high_x,false,left.getcopy),nil));
  654. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  655. firstpass(result);
  656. end;
  657. left:=nil;
  658. right:=nil;
  659. exit;
  660. end;
  661. end;
  662. end;
  663. if (cs_optimize in aktglobalswitches) and
  664. (right.nodetype = calln) and
  665. { left must be a temp, since otherwise as soon as you modify the }
  666. { result, the current left node is modified and that one may }
  667. { still be an argument to the function or even accessed in the }
  668. { function }
  669. (((left.nodetype = temprefn) and
  670. paramanager.ret_in_param(right.resulttype.def,
  671. tcallnode(right).procdefinition.proccalloption)) or
  672. { there's special support for ansi/widestrings in the callnode }
  673. is_ansistring(right.resulttype.def) or
  674. is_widestring(right.resulttype.def)) then
  675. begin
  676. tcallnode(right).funcretnode := left;
  677. result := right;
  678. left := nil;
  679. right := nil;
  680. exit;
  681. end;
  682. registersint:=left.registersint+right.registersint;
  683. registersfpu:=max(left.registersfpu,right.registersfpu);
  684. {$ifdef SUPPORT_MMX}
  685. registersmmx:=max(left.registersmmx,right.registersmmx);
  686. {$endif SUPPORT_MMX}
  687. end;
  688. function tassignmentnode.docompare(p: tnode): boolean;
  689. begin
  690. docompare :=
  691. inherited docompare(p) and
  692. (assigntype = tassignmentnode(p).assigntype);
  693. end;
  694. {$ifdef state_tracking}
  695. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  696. var se:Tstate_entry;
  697. begin
  698. track_state_pass:=false;
  699. if exec_known then
  700. begin
  701. track_state_pass:=right.track_state_pass(exec_known);
  702. {Force a new resulttype pass.}
  703. right.resulttype.def:=nil;
  704. do_resulttypepass(right);
  705. resulttypepass(right);
  706. aktstate.store_fact(left.getcopy,right.getcopy);
  707. end
  708. else
  709. aktstate.delete_fact(left);
  710. end;
  711. {$endif}
  712. {*****************************************************************************
  713. TARRAYCONSTRUCTORRANGENODE
  714. *****************************************************************************}
  715. constructor tarrayconstructorrangenode.create(l,r : tnode);
  716. begin
  717. inherited create(arrayconstructorrangen,l,r);
  718. end;
  719. function tarrayconstructorrangenode.det_resulttype:tnode;
  720. begin
  721. result:=nil;
  722. resulttypepass(left);
  723. resulttypepass(right);
  724. set_varstate(left,vs_used,true);
  725. set_varstate(right,vs_used,true);
  726. if codegenerror then
  727. exit;
  728. resulttype:=left.resulttype;
  729. end;
  730. function tarrayconstructorrangenode.pass_1 : tnode;
  731. begin
  732. firstpass(left);
  733. firstpass(right);
  734. expectloc:=LOC_CREFERENCE;
  735. calcregisters(self,0,0,0);
  736. result:=nil;
  737. end;
  738. {****************************************************************************
  739. TARRAYCONSTRUCTORNODE
  740. *****************************************************************************}
  741. constructor tarrayconstructornode.create(l,r : tnode);
  742. begin
  743. inherited create(arrayconstructorn,l,r);
  744. end;
  745. function tarrayconstructornode.getcopy : tnode;
  746. var
  747. n : tarrayconstructornode;
  748. begin
  749. n:=tarrayconstructornode(inherited getcopy);
  750. result:=n;
  751. end;
  752. function tarrayconstructornode.det_resulttype:tnode;
  753. var
  754. htype : ttype;
  755. hp : tarrayconstructornode;
  756. len : longint;
  757. varia : boolean;
  758. begin
  759. result:=nil;
  760. { are we allowing array constructor? Then convert it to a set }
  761. if not allow_array_constructor then
  762. begin
  763. hp:=tarrayconstructornode(getcopy);
  764. arrayconstructor_to_set(tnode(hp));
  765. result:=hp;
  766. exit;
  767. end;
  768. { only pass left tree, right tree contains next construct if any }
  769. htype.reset;
  770. len:=0;
  771. varia:=false;
  772. if assigned(left) then
  773. begin
  774. hp:=self;
  775. while assigned(hp) do
  776. begin
  777. resulttypepass(hp.left);
  778. set_varstate(hp.left,vs_used,true);
  779. if (htype.def=nil) then
  780. htype:=hp.left.resulttype
  781. else
  782. begin
  783. if ((nf_novariaallowed in flags) or (not varia)) and
  784. (not equal_defs(htype.def,hp.left.resulttype.def)) then
  785. begin
  786. varia:=true;
  787. end;
  788. end;
  789. inc(len);
  790. hp:=tarrayconstructornode(hp.right);
  791. end;
  792. end;
  793. if not assigned(htype.def) then
  794. htype:=voidtype;
  795. resulttype.setdef(tarraydef.create(0,len-1,s32inttype));
  796. tarraydef(resulttype.def).setelementtype(htype);
  797. tarraydef(resulttype.def).IsConstructor:=true;
  798. tarraydef(resulttype.def).IsVariant:=varia;
  799. end;
  800. procedure tarrayconstructornode.force_type(tt:ttype);
  801. var
  802. hp : tarrayconstructornode;
  803. begin
  804. tarraydef(resulttype.def).setelementtype(tt);
  805. tarraydef(resulttype.def).IsConstructor:=true;
  806. tarraydef(resulttype.def).IsVariant:=false;
  807. if assigned(left) then
  808. begin
  809. hp:=self;
  810. while assigned(hp) do
  811. begin
  812. inserttypeconv(hp.left,tt);
  813. hp:=tarrayconstructornode(hp.right);
  814. end;
  815. end;
  816. end;
  817. procedure tarrayconstructornode.insert_typeconvs;
  818. var
  819. hp : tarrayconstructornode;
  820. dovariant : boolean;
  821. begin
  822. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  823. { only pass left tree, right tree contains next construct if any }
  824. if assigned(left) then
  825. begin
  826. hp:=self;
  827. while assigned(hp) do
  828. begin
  829. resulttypepass(hp.left);
  830. { Insert typeconvs for array of const }
  831. if dovariant then
  832. begin
  833. case hp.left.resulttype.def.deftype of
  834. enumdef :
  835. hp.left:=ctypeconvnode.create_internal(hp.left,s32inttype);
  836. arraydef :
  837. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  838. orddef :
  839. begin
  840. if is_integer(hp.left.resulttype.def) and
  841. not(is_64bitint(hp.left.resulttype.def)) then
  842. hp.left:=ctypeconvnode.create(hp.left,s32inttype);
  843. end;
  844. floatdef :
  845. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  846. procvardef :
  847. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  848. stringdef,
  849. variantdef,
  850. pointerdef,
  851. classrefdef,
  852. objectdef : ;
  853. else
  854. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  855. end;
  856. end;
  857. resulttypepass(hp.left);
  858. hp:=tarrayconstructornode(hp.right);
  859. end;
  860. end;
  861. end;
  862. function tarrayconstructornode.pass_1 : tnode;
  863. var
  864. hp : tarrayconstructornode;
  865. do_variant:boolean;
  866. begin
  867. do_variant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  868. result:=nil;
  869. { Insert required type convs, this must be
  870. done in pass 1, because the call must be
  871. resulttypepassed already }
  872. if assigned(left) then
  873. begin
  874. insert_typeconvs;
  875. { call firstpass for all nodes }
  876. hp:=self;
  877. while assigned(hp) do
  878. begin
  879. if hp.left<>nil then
  880. begin
  881. {This check is pessimistic; a call will happen depending
  882. on the location in which the elements will be found in
  883. pass 2.}
  884. if not do_variant then
  885. include(current_procinfo.flags,pi_do_call);
  886. firstpass(hp.left);
  887. end;
  888. hp:=tarrayconstructornode(hp.right);
  889. end;
  890. end;
  891. expectloc:=LOC_CREFERENCE;
  892. calcregisters(self,0,0,0);
  893. end;
  894. function tarrayconstructornode.docompare(p: tnode): boolean;
  895. begin
  896. docompare:=inherited docompare(p);
  897. end;
  898. {*****************************************************************************
  899. TTYPENODE
  900. *****************************************************************************}
  901. constructor ttypenode.create(t : ttype);
  902. begin
  903. inherited create(typen);
  904. restype:=t;
  905. allowed:=false;
  906. end;
  907. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  908. begin
  909. inherited ppuload(t,ppufile);
  910. ppufile.gettype(restype);
  911. allowed:=boolean(ppufile.getbyte);
  912. end;
  913. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  914. begin
  915. inherited ppuwrite(ppufile);
  916. ppufile.puttype(restype);
  917. ppufile.putbyte(byte(allowed));
  918. end;
  919. procedure ttypenode.buildderefimpl;
  920. begin
  921. inherited buildderefimpl;
  922. restype.buildderef;
  923. end;
  924. procedure ttypenode.derefimpl;
  925. begin
  926. inherited derefimpl;
  927. restype.resolve;
  928. end;
  929. function ttypenode.det_resulttype:tnode;
  930. begin
  931. result:=nil;
  932. resulttype:=restype;
  933. { check if it's valid }
  934. if restype.def.deftype = errordef then
  935. CGMessage(parser_e_illegal_expression);
  936. end;
  937. function ttypenode.pass_1 : tnode;
  938. begin
  939. result:=nil;
  940. expectloc:=LOC_VOID;
  941. { a typenode can't generate code, so we give here
  942. an error. Else it'll be an abstract error in pass_2.
  943. Only when the allowed flag is set we don't generate
  944. an error }
  945. if not allowed then
  946. Message(parser_e_no_type_not_allowed_here);
  947. end;
  948. function ttypenode.docompare(p: tnode): boolean;
  949. begin
  950. docompare :=
  951. inherited docompare(p);
  952. end;
  953. {*****************************************************************************
  954. TRTTINODE
  955. *****************************************************************************}
  956. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  957. begin
  958. inherited create(rttin);
  959. rttidef:=def;
  960. rttitype:=rt;
  961. end;
  962. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  963. begin
  964. inherited ppuload(t,ppufile);
  965. ppufile.getderef(rttidefderef);
  966. rttitype:=trttitype(ppufile.getbyte);
  967. end;
  968. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  969. begin
  970. inherited ppuwrite(ppufile);
  971. ppufile.putderef(rttidefderef);
  972. ppufile.putbyte(byte(rttitype));
  973. end;
  974. procedure trttinode.buildderefimpl;
  975. begin
  976. inherited buildderefimpl;
  977. rttidefderef.build(rttidef);
  978. end;
  979. procedure trttinode.derefimpl;
  980. begin
  981. inherited derefimpl;
  982. rttidef:=tstoreddef(rttidefderef.resolve);
  983. end;
  984. function trttinode.getcopy : tnode;
  985. var
  986. n : trttinode;
  987. begin
  988. n:=trttinode(inherited getcopy);
  989. n.rttidef:=rttidef;
  990. n.rttitype:=rttitype;
  991. result:=n;
  992. end;
  993. function trttinode.det_resulttype:tnode;
  994. begin
  995. { rtti information will be returned as a void pointer }
  996. result:=nil;
  997. resulttype:=voidpointertype;
  998. end;
  999. function trttinode.pass_1 : tnode;
  1000. begin
  1001. result:=nil;
  1002. expectloc:=LOC_CREFERENCE;
  1003. end;
  1004. function trttinode.docompare(p: tnode): boolean;
  1005. begin
  1006. docompare :=
  1007. inherited docompare(p) and
  1008. (rttidef = trttinode(p).rttidef) and
  1009. (rttitype = trttinode(p).rttitype);
  1010. end;
  1011. begin
  1012. cloadnode:=tloadnode;
  1013. cassignmentnode:=tassignmentnode;
  1014. carrayconstructorrangenode:=tarrayconstructorrangenode;
  1015. carrayconstructornode:=tarrayconstructornode;
  1016. ctypenode:=ttypenode;
  1017. crttinode:=trttinode;
  1018. end.
  1019. {
  1020. $Log$
  1021. Revision 1.139 2004-11-08 22:09:59 peter
  1022. * tvarsym splitted
  1023. Revision 1.138 2004/11/02 12:55:16 peter
  1024. * nf_internal flag for internal inserted typeconvs. This will
  1025. supress the generation of warning/hints
  1026. Revision 1.137 2004/11/01 15:32:12 peter
  1027. * support @labelsym
  1028. Revision 1.136 2004/10/31 21:45:03 peter
  1029. * generic tlocation
  1030. * move tlocation to cgutils
  1031. Revision 1.135 2004/10/24 11:44:28 peter
  1032. * small regvar fixes
  1033. * loadref parameter removed from concatcopy,incrrefcount,etc
  1034. Revision 1.134 2004/10/12 14:35:14 peter
  1035. * fixed crash when current_procinfo was not yet available
  1036. Revision 1.133 2004/10/11 15:48:15 peter
  1037. * small regvar for para fixes
  1038. * function tvarsym.is_regvar added
  1039. * tvarsym.getvaluesize removed, use getsize instead
  1040. Revision 1.132 2004/10/10 20:22:53 peter
  1041. * symtable allocation rewritten
  1042. * loading of parameters to local temps/regs cleanup
  1043. * regvar support for parameters
  1044. * regvar support for staticsymtable (main body)
  1045. Revision 1.131 2004/10/08 17:09:43 peter
  1046. * tvarsym.varregable added, split vo_regable from varoptions
  1047. Revision 1.130 2004/10/06 19:26:50 jonas
  1048. * regvar fixes from Peter
  1049. Revision 1.129 2004/09/26 17:45:30 peter
  1050. * simple regvar support, not yet finished
  1051. Revision 1.128 2004/06/20 08:55:29 florian
  1052. * logs truncated
  1053. Revision 1.127 2004/06/16 20:07:08 florian
  1054. * dwarf branch merged
  1055. Revision 1.126 2004/04/29 19:56:37 daniel
  1056. * Prepare compiler infrastructure for multiple ansistring types
  1057. Revision 1.125.2.1 2004/04/28 19:55:51 peter
  1058. * new warning for ordinal-pointer when size is different
  1059. * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
  1060. Revision 1.125 2004/03/02 17:32:12 florian
  1061. * make cycle fixed
  1062. + pic support for darwin
  1063. + support of importing vars from shared libs on darwin implemented
  1064. Revision 1.124 2004/02/20 22:15:26 peter
  1065. * fixed compiler err
  1066. }