nld.pas 41 KB

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