nld.pas 41 KB

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