nld.pas 42 KB

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