nld.pas 40 KB

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