nld.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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,symppu,symbase,symtype,symsym,symdef;
  27. type
  28. tloadnode = class(tunarynode)
  29. symtableentry : tsym;
  30. symtable : tsymtable;
  31. procdef : tprocdef;
  32. constructor create(v : tsym;st : tsymtable);virtual;
  33. constructor create_procvar(v : tsym;d:tprocdef;st : tsymtable);virtual;
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure derefimpl;override;
  37. procedure set_mp(p:tnode);
  38. function getcopy : tnode;override;
  39. function pass_1 : tnode;override;
  40. function det_resulttype:tnode;override;
  41. {$ifdef var_notification}
  42. procedure mark_write;override;
  43. {$endif}
  44. function docompare(p: tnode): boolean; override;
  45. {$ifdef extdebug}
  46. procedure _dowrite;override;
  47. {$endif}
  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. tfuncretnode = class(tnode)
  67. funcretsym : tfuncretsym;
  68. constructor create(v:tsym);virtual;
  69. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  70. procedure ppuwrite(ppufile:tcompilerppufile);override;
  71. procedure derefimpl;override;
  72. function getcopy : tnode;override;
  73. function pass_1 : tnode;override;
  74. function det_resulttype:tnode;override;
  75. {$ifdef var_notification}
  76. procedure mark_write;override;
  77. {$endif}
  78. function docompare(p: tnode): boolean; override;
  79. end;
  80. tfuncretnodeclass = class of tfuncretnode;
  81. tarrayconstructorrangenode = class(tbinarynode)
  82. constructor create(l,r : tnode);virtual;
  83. function pass_1 : tnode;override;
  84. function det_resulttype:tnode;override;
  85. end;
  86. tarrayconstructorrangenodeclass = class of tarrayconstructorrangenode;
  87. tarrayconstructornode = class(tbinarynode)
  88. constructor create(l,r : tnode);virtual;
  89. function getcopy : tnode;override;
  90. function pass_1 : tnode;override;
  91. function det_resulttype:tnode;override;
  92. function docompare(p: tnode): boolean; override;
  93. procedure force_type(tt:ttype);
  94. end;
  95. tarrayconstructornodeclass = class of tarrayconstructornode;
  96. ttypenode = class(tnode)
  97. allowed : boolean;
  98. restype : ttype;
  99. constructor create(t : ttype);virtual;
  100. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure derefimpl;override;
  103. function pass_1 : tnode;override;
  104. function det_resulttype:tnode;override;
  105. function docompare(p: tnode): boolean; override;
  106. end;
  107. ttypenodeclass = class of ttypenode;
  108. trttinode = class(tnode)
  109. l1,l2 : longint;
  110. rttitype : trttitype;
  111. rttidef : tstoreddef;
  112. constructor create(def:tstoreddef;rt:trttitype);virtual;
  113. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  114. procedure ppuwrite(ppufile:tcompilerppufile);override;
  115. procedure derefimpl;override;
  116. function getcopy : tnode;override;
  117. function pass_1 : tnode;override;
  118. procedure pass_2;override;
  119. function det_resulttype:tnode;override;
  120. function docompare(p: tnode): boolean; override;
  121. end;
  122. trttinodeclass = class of trttinode;
  123. var
  124. cloadnode : tloadnodeclass;
  125. cassignmentnode : tassignmentnodeclass;
  126. cfuncretnode : tfuncretnodeclass;
  127. carrayconstructorrangenode : tarrayconstructorrangenodeclass;
  128. carrayconstructornode : tarrayconstructornodeclass;
  129. ctypenode : ttypenodeclass;
  130. crttinode : trttinodeclass;
  131. implementation
  132. uses
  133. cutils,verbose,globtype,globals,systems,
  134. symtable,paramgr,defbase,
  135. htypechk,pass_1,
  136. ncon,ninl,ncnv,nmem,ncal,cpubase,rgobj,cginfo,cgbase
  137. ;
  138. {*****************************************************************************
  139. TLOADNODE
  140. *****************************************************************************}
  141. constructor tloadnode.create(v : tsym;st : tsymtable);
  142. begin
  143. inherited create(loadn,nil);
  144. if not assigned(v) then
  145. internalerror(200108121);
  146. symtableentry:=v;
  147. symtable:=st;
  148. procdef:=nil;
  149. end;
  150. constructor tloadnode.create_procvar(v : tsym;d:tprocdef;st : tsymtable);
  151. begin
  152. inherited create(loadn,nil);
  153. if not assigned(v) then
  154. internalerror(200108121);
  155. symtableentry:=v;
  156. symtable:=st;
  157. procdef:=d;
  158. end;
  159. constructor tloadnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  160. begin
  161. inherited ppuload(t,ppufile);
  162. symtableentry:=tsym(ppufile.getderef);
  163. {$warning FIXME: No withsymtable support}
  164. symtable:=nil;
  165. procdef:=tprocdef(ppufile.getderef);
  166. end;
  167. procedure tloadnode.ppuwrite(ppufile:tcompilerppufile);
  168. begin
  169. inherited ppuwrite(ppufile);
  170. ppufile.putderef(symtableentry);
  171. ppufile.putderef(procdef);
  172. end;
  173. procedure tloadnode.derefimpl;
  174. begin
  175. inherited derefimpl;
  176. resolvesym(pointer(symtableentry));
  177. symtable:=symtableentry.owner;
  178. resolvedef(pointer(procdef));
  179. end;
  180. procedure tloadnode.set_mp(p:tnode);
  181. begin
  182. left:=p;
  183. end;
  184. function tloadnode.getcopy : tnode;
  185. var
  186. n : tloadnode;
  187. begin
  188. n:=tloadnode(inherited getcopy);
  189. n.symtable:=symtable;
  190. n.symtableentry:=symtableentry;
  191. result:=n;
  192. end;
  193. function tloadnode.det_resulttype:tnode;
  194. var
  195. p1 : tnode;
  196. p : tprocinfo;
  197. begin
  198. result:=nil;
  199. { optimize simple with loadings }
  200. if (symtable.symtabletype=withsymtable) and
  201. (twithsymtable(symtable).direct_with) and
  202. (symtableentry.typ=varsym) then
  203. begin
  204. p1:=tnode(twithsymtable(symtable).withrefnode).getcopy;
  205. p1:=csubscriptnode.create(tvarsym(symtableentry),p1);
  206. left:=nil;
  207. result:=p1;
  208. exit;
  209. end;
  210. { handle first absolute as it will replace the symtableentry }
  211. if symtableentry.typ=absolutesym then
  212. begin
  213. { force the resulttype to the type of the absolute }
  214. resulttype:=tabsolutesym(symtableentry).vartype;
  215. { replace the symtableentry when it points to a var, else
  216. we are finished }
  217. if tabsolutesym(symtableentry).abstyp=tovar then
  218. begin
  219. symtableentry:=tabsolutesym(symtableentry).ref;
  220. symtable:=symtableentry.owner;
  221. include(flags,nf_absolute);
  222. end
  223. else
  224. exit;
  225. end;
  226. case symtableentry.typ of
  227. funcretsym :
  228. begin
  229. { find the main funcret for the function }
  230. p:=procinfo;
  231. while assigned(p) do
  232. begin
  233. if assigned(p.procdef.funcretsym) and
  234. ((tfuncretsym(symtableentry)=p.procdef.resultfuncretsym) or
  235. (tfuncretsym(symtableentry)=p.procdef.funcretsym)) then
  236. begin
  237. symtableentry:=p.procdef.funcretsym;
  238. break;
  239. end;
  240. p:=p.parent;
  241. end;
  242. { generate funcretnode }
  243. p1:=cfuncretnode.create(symtableentry);
  244. resulttypepass(p1);
  245. { if it's refered as absolute then we need to have the
  246. type of the absolute instead of the function return,
  247. the function return is then also assigned }
  248. if nf_absolute in flags then
  249. begin
  250. tfuncretsym(symtableentry).funcretstate:=vs_assigned;
  251. p1.resulttype:=resulttype;
  252. end;
  253. left:=nil;
  254. result:=p1;
  255. end;
  256. constsym:
  257. begin
  258. if tconstsym(symtableentry).consttyp=constresourcestring then
  259. resulttype:=cansistringtype
  260. else
  261. internalerror(22799);
  262. end;
  263. varsym :
  264. begin
  265. { if it's refered by absolute then it's used }
  266. if nf_absolute in flags then
  267. tvarsym(symtableentry).varstate:=vs_used
  268. else
  269. resulttype:=tvarsym(symtableentry).vartype;
  270. end;
  271. typedconstsym :
  272. if not(nf_absolute in flags) then
  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. resulttype.setdef(tprocsym(symtableentry).first_procdef);
  281. end
  282. else
  283. resulttype.setdef(procdef);
  284. if (m_tp_procvar in aktmodeswitches) then
  285. begin
  286. if assigned(left) then
  287. begin
  288. if left.nodetype=typen then
  289. begin
  290. { we need to return only a voidpointer,
  291. so no need to keep the typen }
  292. left.free;
  293. left:=nil;
  294. end;
  295. end
  296. else
  297. begin
  298. { if the owner of the procsym is a object, }
  299. { left must be set, if left isn't set }
  300. { it can be only self }
  301. if (tprocsym(symtableentry).owner.symtabletype=objectsymtable) then
  302. left:=cselfnode.create(tobjectdef(symtableentry.owner.defowner));
  303. end;
  304. end;
  305. { process methodpointer }
  306. if assigned(left) then
  307. begin
  308. resulttypepass(left);
  309. { turn on the allowed flag, the secondpass
  310. will handle the typen itself }
  311. if left.nodetype=typen then
  312. ttypenode(left).allowed:=true;
  313. end;
  314. end;
  315. else
  316. internalerror(200104141);
  317. end;
  318. end;
  319. {$ifdef var_notification}
  320. procedure Tloadnode.mark_write;
  321. begin
  322. include(flags,nf_write);
  323. end;
  324. {$endif}
  325. function tloadnode.pass_1 : tnode;
  326. begin
  327. result:=nil;
  328. location.loc:=LOC_REFERENCE;
  329. registers32:=0;
  330. registersfpu:=0;
  331. {$ifdef SUPPORT_MMX}
  332. registersmmx:=0;
  333. {$endif SUPPORT_MMX}
  334. case symtableentry.typ of
  335. absolutesym :
  336. ;
  337. funcretsym :
  338. internalerror(200104142);
  339. constsym:
  340. begin
  341. if tconstsym(symtableentry).consttyp=constresourcestring then
  342. begin
  343. { we use ansistrings so no fast exit here }
  344. if assigned(procinfo) then
  345. procinfo.no_fast_exit:=true;
  346. location.loc:=LOC_CREFERENCE;
  347. end;
  348. end;
  349. varsym :
  350. begin
  351. if (symtable.symtabletype in [parasymtable,localsymtable]) and
  352. (lexlevel>symtable.symtablelevel) then
  353. begin
  354. { if the variable is in an other stackframe then we need
  355. a register to dereference }
  356. if (symtable.symtablelevel)>0 then
  357. begin
  358. registers32:=1;
  359. { further, the variable can't be put into a register }
  360. tvarsym(symtableentry).varoptions:=
  361. tvarsym(symtableentry).varoptions-[vo_fpuregable,vo_regable];
  362. end;
  363. end;
  364. if (tvarsym(symtableentry).varspez=vs_const) then
  365. location.loc:=LOC_CREFERENCE;
  366. { we need a register for call by reference parameters }
  367. if (tvarsym(symtableentry).varspez in [vs_var,vs_out]) or
  368. ((tvarsym(symtableentry).varspez=vs_const) and
  369. paramanager.push_addr_param(tvarsym(symtableentry).vartype.def,false)) or
  370. { call by value open arrays are also indirect addressed }
  371. is_open_array(tvarsym(symtableentry).vartype.def) then
  372. registers32:=1;
  373. if symtable.symtabletype=withsymtable then
  374. inc(registers32);
  375. if ([vo_is_thread_var,vo_is_dll_var]*tvarsym(symtableentry).varoptions)<>[] then
  376. registers32:=1;
  377. { count variable references }
  378. { this will create problem with local var set by
  379. under_procedures
  380. if (assigned(tvarsym(symtableentry).owner) and assigned(aktprocsym)
  381. and ((tvarsym(symtableentry).owner = aktprocdef.localst)
  382. or (tvarsym(symtableentry).owner = aktprocdef.localst))) then }
  383. if rg.t_times<1 then
  384. inc(tvarsym(symtableentry).refs)
  385. else
  386. inc(tvarsym(symtableentry).refs,rg.t_times);
  387. end;
  388. typedconstsym :
  389. ;
  390. procsym :
  391. begin
  392. { method pointer ? }
  393. if assigned(left) then
  394. begin
  395. firstpass(left);
  396. registers32:=max(registers32,left.registers32);
  397. registersfpu:=max(registersfpu,left.registersfpu);
  398. {$ifdef SUPPORT_MMX}
  399. registersmmx:=max(registersmmx,left.registersmmx);
  400. {$endif SUPPORT_MMX}
  401. end;
  402. end;
  403. else
  404. internalerror(200104143);
  405. end;
  406. end;
  407. function tloadnode.docompare(p: tnode): boolean;
  408. begin
  409. docompare :=
  410. inherited docompare(p) and
  411. (symtableentry = tloadnode(p).symtableentry) and
  412. (symtable = tloadnode(p).symtable);
  413. end;
  414. {$ifdef extdebug}
  415. procedure Tloadnode._dowrite;
  416. begin
  417. inherited _dowrite;
  418. writeln(',');
  419. system.write(writenodeindention,'symbol = ',symtableentry.name);
  420. end;
  421. {$endif}
  422. {*****************************************************************************
  423. TASSIGNMENTNODE
  424. *****************************************************************************}
  425. constructor tassignmentnode.create(l,r : tnode);
  426. begin
  427. inherited create(assignn,l,r);
  428. {$ifdef var_notification}
  429. l.mark_write;
  430. {$endif}
  431. assigntype:=at_normal;
  432. end;
  433. constructor tassignmentnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  434. begin
  435. inherited ppuload(t,ppufile);
  436. assigntype:=tassigntype(ppufile.getbyte);
  437. end;
  438. procedure tassignmentnode.ppuwrite(ppufile:tcompilerppufile);
  439. begin
  440. inherited ppuwrite(ppufile);
  441. ppufile.putbyte(byte(assigntype));
  442. end;
  443. function tassignmentnode.getcopy : tnode;
  444. var
  445. n : tassignmentnode;
  446. begin
  447. n:=tassignmentnode(inherited getcopy);
  448. n.assigntype:=assigntype;
  449. getcopy:=n;
  450. end;
  451. function tassignmentnode.det_resulttype:tnode;
  452. var
  453. hp : tnode;
  454. useshelper : boolean;
  455. begin
  456. result:=nil;
  457. resulttype:=voidtype;
  458. { must be made unique }
  459. if assigned(left) then
  460. begin
  461. set_unique(left);
  462. { set we the function result? }
  463. set_funcret_is_valid(left);
  464. end;
  465. resulttypepass(left);
  466. resulttypepass(right);
  467. set_varstate(left,false);
  468. set_varstate(right,true);
  469. if codegenerror then
  470. exit;
  471. { assignments to open arrays aren't allowed }
  472. if is_open_array(left.resulttype.def) then
  473. CGMessage(type_e_mismatch);
  474. { test if node can be assigned, properties are allowed }
  475. valid_for_assignment(left);
  476. { assigning nil to a dynamic array clears the array }
  477. if is_dynamic_array(left.resulttype.def) and
  478. (right.nodetype=niln) then
  479. begin
  480. hp:=ccallparanode.create(caddrnode.create
  481. (crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  482. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil));
  483. result := ccallnode.createintern('fpc_dynarray_clear',hp);
  484. left:=nil;
  485. exit;
  486. end;
  487. { shortstring helpers can do the conversion directly,
  488. so treat them separatly }
  489. if (is_shortstring(left.resulttype.def)) then
  490. begin
  491. { test for s:=s+anything ... }
  492. { the problem is for
  493. s:=s+s+s;
  494. this is broken here !! }
  495. {$ifdef newoptimizations2}
  496. { the above is fixed now, but still problem with s := s + f(); if }
  497. { f modifies s (bad programming, so only enable if uncertain }
  498. { optimizations are on) (JM) }
  499. if (cs_UncertainOpts in aktglobalswitches) then
  500. begin
  501. hp := right;
  502. while hp.treetype=addn do
  503. hp:=hp.left;
  504. if equal_trees(left,hp) and
  505. not multiple_uses(left,right) then
  506. begin
  507. concat_string:=true;
  508. hp:=right;
  509. while hp.treetype=addn do
  510. begin
  511. hp.use_strconcat:=true;
  512. hp:=hp.left;
  513. end;
  514. end;
  515. end;
  516. {$endif newoptimizations2}
  517. { insert typeconv, except for chars that are handled in
  518. secondpass and except for ansi/wide string that can
  519. be converted immediatly }
  520. if not(is_char(right.resulttype.def) or
  521. (right.resulttype.def.deftype=stringdef)) then
  522. inserttypeconv(right,left.resulttype);
  523. if right.resulttype.def.deftype=stringdef then
  524. begin
  525. useshelper:=true;
  526. { convert constant strings to shortstrings. But
  527. skip empty constant strings, that will be handled
  528. in secondpass }
  529. if (right.nodetype=stringconstn) then
  530. begin
  531. inserttypeconv(right,left.resulttype);
  532. if (tstringconstnode(right).len=0) then
  533. useshelper:=false;
  534. end;
  535. if useshelper then
  536. begin
  537. hp:=ccallparanode.create
  538. (right,
  539. ccallparanode.create(cinlinenode.create
  540. (in_high_x,false,left.getcopy),nil));
  541. result:=ccallnode.createinternreturn('fpc_'+tstringdef(right.resulttype.def).stringtypname+'_to_shortstr',hp,left);
  542. left:=nil;
  543. right:=nil;
  544. exit;
  545. end;
  546. end;
  547. end
  548. else
  549. inserttypeconv(right,left.resulttype);
  550. { call helpers for interface }
  551. if is_interfacecom(left.resulttype.def) then
  552. begin
  553. hp:=ccallparanode.create(ctypeconvnode.create_explicit
  554. (right,voidpointertype),
  555. ccallparanode.create(ctypeconvnode.create_explicit
  556. (left,voidpointertype),nil));
  557. result:=ccallnode.createintern('fpc_intf_assign',hp);
  558. left:=nil;
  559. right:=nil;
  560. exit;
  561. end;
  562. { check if local proc/func is assigned to procvar }
  563. if right.resulttype.def.deftype=procvardef then
  564. test_local_to_procvar(tprocvardef(right.resulttype.def),left.resulttype.def);
  565. end;
  566. function tassignmentnode.pass_1 : tnode;
  567. begin
  568. result:=nil;
  569. firstpass(left);
  570. firstpass(right);
  571. if codegenerror then
  572. exit;
  573. registers32:=left.registers32+right.registers32;
  574. registersfpu:=max(left.registersfpu,right.registersfpu);
  575. {$ifdef SUPPORT_MMX}
  576. registersmmx:=max(left.registersmmx,right.registersmmx);
  577. {$endif SUPPORT_MMX}
  578. end;
  579. function tassignmentnode.docompare(p: tnode): boolean;
  580. begin
  581. docompare :=
  582. inherited docompare(p) and
  583. (assigntype = tassignmentnode(p).assigntype);
  584. end;
  585. {$ifdef state_tracking}
  586. function Tassignmentnode.track_state_pass(exec_known:boolean):boolean;
  587. var se:Tstate_entry;
  588. begin
  589. track_state_pass:=false;
  590. if exec_known then
  591. begin
  592. track_state_pass:=right.track_state_pass(exec_known);
  593. {Force a new resulttype pass.}
  594. right.resulttype.def:=nil;
  595. do_resulttypepass(right);
  596. resulttypepass(right);
  597. aktstate.store_fact(left.getcopy,right.getcopy);
  598. end
  599. else
  600. aktstate.delete_fact(left);
  601. end;
  602. {$endif}
  603. {*****************************************************************************
  604. TFUNCRETNODE
  605. *****************************************************************************}
  606. constructor tfuncretnode.create(v:tsym);
  607. begin
  608. inherited create(funcretn);
  609. funcretsym:=tfuncretsym(v);
  610. end;
  611. constructor tfuncretnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  612. begin
  613. inherited ppuload(t,ppufile);
  614. funcretsym:=tfuncretsym(ppufile.getderef);
  615. end;
  616. procedure tfuncretnode.ppuwrite(ppufile:tcompilerppufile);
  617. begin
  618. inherited ppuwrite(ppufile);
  619. ppufile.putderef(funcretsym);
  620. end;
  621. procedure tfuncretnode.derefimpl;
  622. begin
  623. inherited derefimpl;
  624. resolvesym(pointer(funcretsym));
  625. end;
  626. function tfuncretnode.getcopy : tnode;
  627. var
  628. n : tfuncretnode;
  629. begin
  630. n:=tfuncretnode(inherited getcopy);
  631. n.funcretsym:=funcretsym;
  632. getcopy:=n;
  633. end;
  634. function tfuncretnode.det_resulttype:tnode;
  635. begin
  636. result:=nil;
  637. resulttype:=funcretsym.returntype;
  638. end;
  639. {$ifdef var_notification}
  640. procedure Tfuncretnode.mark_write;
  641. begin
  642. include(flags,nf_write);
  643. end;
  644. {$endif}
  645. function tfuncretnode.pass_1 : tnode;
  646. begin
  647. result:=nil;
  648. location.loc:=LOC_REFERENCE;
  649. if paramanager.ret_in_param(resulttype.def) or
  650. (lexlevel<>funcretsym.owner.symtablelevel) then
  651. registers32:=1;
  652. end;
  653. function tfuncretnode.docompare(p: tnode): boolean;
  654. begin
  655. docompare :=
  656. inherited docompare(p) and
  657. (funcretsym = tfuncretnode(p).funcretsym);
  658. end;
  659. {*****************************************************************************
  660. TARRAYCONSTRUCTORRANGENODE
  661. *****************************************************************************}
  662. constructor tarrayconstructorrangenode.create(l,r : tnode);
  663. begin
  664. inherited create(arrayconstructorrangen,l,r);
  665. end;
  666. function tarrayconstructorrangenode.det_resulttype:tnode;
  667. begin
  668. result:=nil;
  669. resulttypepass(left);
  670. resulttypepass(right);
  671. set_varstate(left,true);
  672. set_varstate(right,true);
  673. if codegenerror then
  674. exit;
  675. resulttype:=left.resulttype;
  676. end;
  677. function tarrayconstructorrangenode.pass_1 : tnode;
  678. begin
  679. firstpass(left);
  680. firstpass(right);
  681. location.loc := LOC_CREFERENCE;
  682. calcregisters(self,0,0,0);
  683. result:=nil;
  684. end;
  685. {****************************************************************************
  686. TARRAYCONSTRUCTORNODE
  687. *****************************************************************************}
  688. constructor tarrayconstructornode.create(l,r : tnode);
  689. begin
  690. inherited create(arrayconstructorn,l,r);
  691. end;
  692. function tarrayconstructornode.getcopy : tnode;
  693. var
  694. n : tarrayconstructornode;
  695. begin
  696. n:=tarrayconstructornode(inherited getcopy);
  697. result:=n;
  698. end;
  699. function tarrayconstructornode.det_resulttype:tnode;
  700. var
  701. htype : ttype;
  702. hp : tarrayconstructornode;
  703. len : longint;
  704. varia : boolean;
  705. begin
  706. result:=nil;
  707. { are we allowing array constructor? Then convert it to a set }
  708. if not allow_array_constructor then
  709. begin
  710. hp:=tarrayconstructornode(getcopy);
  711. arrayconstructor_to_set(hp);
  712. result:=hp;
  713. exit;
  714. end;
  715. { only pass left tree, right tree contains next construct if any }
  716. htype.reset;
  717. len:=0;
  718. varia:=false;
  719. if assigned(left) then
  720. begin
  721. hp:=self;
  722. while assigned(hp) do
  723. begin
  724. resulttypepass(hp.left);
  725. set_varstate(hp.left,true);
  726. if (htype.def=nil) then
  727. htype:=hp.left.resulttype
  728. else
  729. begin
  730. if ((nf_novariaallowed in flags) or (not varia)) and
  731. (not is_equal(htype.def,hp.left.resulttype.def)) then
  732. begin
  733. varia:=true;
  734. end;
  735. end;
  736. inc(len);
  737. hp:=tarrayconstructornode(hp.right);
  738. end;
  739. end;
  740. if not assigned(htype.def) then
  741. htype:=voidtype;
  742. resulttype.setdef(tarraydef.create(0,len-1,s32bittype));
  743. tarraydef(resulttype.def).setelementtype(htype);
  744. tarraydef(resulttype.def).IsConstructor:=true;
  745. tarraydef(resulttype.def).IsVariant:=varia;
  746. end;
  747. procedure tarrayconstructornode.force_type(tt:ttype);
  748. var
  749. hp : tarrayconstructornode;
  750. begin
  751. tarraydef(resulttype.def).setelementtype(tt);
  752. tarraydef(resulttype.def).IsConstructor:=true;
  753. tarraydef(resulttype.def).IsVariant:=false;
  754. if assigned(left) then
  755. begin
  756. hp:=self;
  757. while assigned(hp) do
  758. begin
  759. inserttypeconv(hp.left,tt);
  760. hp:=tarrayconstructornode(hp.right);
  761. end;
  762. end;
  763. end;
  764. function tarrayconstructornode.pass_1 : tnode;
  765. var
  766. thp,
  767. chp,
  768. hp : tarrayconstructornode;
  769. dovariant : boolean;
  770. htype : ttype;
  771. orgflags : tnodeflagset;
  772. begin
  773. dovariant:=(nf_forcevaria in flags) or tarraydef(resulttype.def).isvariant;
  774. result:=nil;
  775. { only pass left tree, right tree contains next construct if any }
  776. if assigned(left) then
  777. begin
  778. hp:=self;
  779. while assigned(hp) do
  780. begin
  781. firstpass(hp.left);
  782. { Insert typeconvs for array of const }
  783. if dovariant then
  784. begin
  785. case hp.left.resulttype.def.deftype of
  786. enumdef :
  787. begin
  788. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  789. firstpass(hp.left);
  790. end;
  791. orddef :
  792. begin
  793. if is_integer(hp.left.resulttype.def) and
  794. not(is_64bitint(hp.left.resulttype.def)) then
  795. begin
  796. hp.left:=ctypeconvnode.create(hp.left,s32bittype);
  797. firstpass(hp.left);
  798. end;
  799. end;
  800. floatdef :
  801. begin
  802. hp.left:=ctypeconvnode.create(hp.left,pbestrealtype^);
  803. firstpass(hp.left);
  804. end;
  805. stringdef :
  806. begin
  807. if nf_cargs in flags then
  808. begin
  809. hp.left:=ctypeconvnode.create(hp.left,charpointertype);
  810. firstpass(hp.left);
  811. end;
  812. end;
  813. procvardef :
  814. begin
  815. hp.left:=ctypeconvnode.create(hp.left,voidpointertype);
  816. firstpass(hp.left);
  817. end;
  818. variantdef,
  819. pointerdef,
  820. classrefdef,
  821. objectdef : ;
  822. else
  823. CGMessagePos1(hp.left.fileinfo,type_e_wrong_type_in_array_constructor,hp.left.resulttype.def.typename);
  824. end;
  825. end;
  826. hp:=tarrayconstructornode(hp.right);
  827. end;
  828. { swap the tree for cargs }
  829. if (nf_cargs in flags) and (not(nf_cargswap in flags)) then
  830. begin
  831. chp:=nil;
  832. { save resulttype }
  833. htype:=resulttype;
  834. { we need a copy here, because self is destroyed }
  835. { by firstpass later }
  836. hp:=tarrayconstructornode(getcopy);
  837. { we also need a copy of the nf_ forcevaria flag to restore }
  838. { later) (JM) }
  839. orgflags := flags * [nf_forcevaria];
  840. while assigned(hp) do
  841. begin
  842. thp:=tarrayconstructornode(hp.right);
  843. hp.right:=chp;
  844. chp:=hp;
  845. hp:=thp;
  846. end;
  847. chp.flags := chp.flags+orgflags;
  848. include(chp.flags,nf_cargswap);
  849. chp.location.loc:=LOC_CREFERENCE;
  850. calcregisters(chp,0,0,0);
  851. chp.resulttype:=htype;
  852. result:=chp;
  853. exit;
  854. end;
  855. end;
  856. { C Arguments are pushed on the stack and
  857. are not accesible after the push }
  858. if not(nf_cargs in flags) then
  859. location.loc:=LOC_CREFERENCE
  860. else
  861. location.loc:=LOC_INVALID;
  862. calcregisters(self,0,0,0);
  863. end;
  864. function tarrayconstructornode.docompare(p: tnode): boolean;
  865. begin
  866. docompare :=
  867. inherited docompare(p);
  868. end;
  869. {*****************************************************************************
  870. TTYPENODE
  871. *****************************************************************************}
  872. constructor ttypenode.create(t : ttype);
  873. begin
  874. inherited create(typen);
  875. restype:=t;
  876. allowed:=false;
  877. end;
  878. constructor ttypenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  879. begin
  880. inherited ppuload(t,ppufile);
  881. ppufile.gettype(restype);
  882. allowed:=boolean(ppufile.getbyte);
  883. end;
  884. procedure ttypenode.ppuwrite(ppufile:tcompilerppufile);
  885. begin
  886. inherited ppuwrite(ppufile);
  887. ppufile.puttype(restype);
  888. ppufile.putbyte(byte(allowed));
  889. end;
  890. procedure ttypenode.derefimpl;
  891. begin
  892. inherited derefimpl;
  893. restype.resolve;
  894. end;
  895. function ttypenode.det_resulttype:tnode;
  896. begin
  897. result:=nil;
  898. resulttype:=restype;
  899. { check if it's valid }
  900. if restype.def.deftype = errordef then
  901. CGMessage(cg_e_illegal_expression);
  902. end;
  903. function ttypenode.pass_1 : tnode;
  904. begin
  905. result:=nil;
  906. { a typenode can't generate code, so we give here
  907. an error. Else it'll be an abstract error in pass_2.
  908. Only when the allowed flag is set we don't generate
  909. an error }
  910. if not allowed then
  911. Message(parser_e_no_type_not_allowed_here);
  912. end;
  913. function ttypenode.docompare(p: tnode): boolean;
  914. begin
  915. docompare :=
  916. inherited docompare(p);
  917. end;
  918. {*****************************************************************************
  919. TRTTINODE
  920. *****************************************************************************}
  921. constructor trttinode.create(def:tstoreddef;rt:trttitype);
  922. begin
  923. inherited create(rttin);
  924. rttidef:=def;
  925. rttitype:=rt;
  926. end;
  927. constructor trttinode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  928. begin
  929. inherited ppuload(t,ppufile);
  930. rttidef:=tstoreddef(ppufile.getderef);
  931. rttitype:=trttitype(ppufile.getbyte);
  932. end;
  933. procedure trttinode.ppuwrite(ppufile:tcompilerppufile);
  934. begin
  935. inherited ppuwrite(ppufile);
  936. ppufile.putderef(rttidef);
  937. ppufile.putbyte(byte(rttitype));
  938. end;
  939. procedure trttinode.derefimpl;
  940. begin
  941. inherited derefimpl;
  942. resolvedef(pointer(rttidef));
  943. end;
  944. function trttinode.getcopy : tnode;
  945. var
  946. n : trttinode;
  947. begin
  948. n:=trttinode(inherited getcopy);
  949. n.rttidef:=rttidef;
  950. n.rttitype:=rttitype;
  951. result:=n;
  952. end;
  953. function trttinode.det_resulttype:tnode;
  954. begin
  955. { rtti information will be returned as a void pointer }
  956. result:=nil;
  957. resulttype:=voidpointertype;
  958. end;
  959. function trttinode.pass_1 : tnode;
  960. begin
  961. result:=nil;
  962. location.loc:=LOC_CREFERENCE;
  963. end;
  964. function trttinode.docompare(p: tnode): boolean;
  965. begin
  966. docompare :=
  967. inherited docompare(p) and
  968. (rttidef = trttinode(p).rttidef) and
  969. (rttitype = trttinode(p).rttitype);
  970. end;
  971. procedure trttinode.pass_2;
  972. begin
  973. location_reset(location,LOC_CREFERENCE,OS_NO);
  974. location.reference.symbol:=rttidef.get_rtti_label(rttitype);
  975. end;
  976. begin
  977. cloadnode:=tloadnode;
  978. cassignmentnode:=tassignmentnode;
  979. cfuncretnode:=tfuncretnode;
  980. carrayconstructorrangenode:=tarrayconstructorrangenode;
  981. carrayconstructornode:=tarrayconstructornode;
  982. ctypenode:=ttypenode;
  983. crttinode:=trttinode;
  984. end.
  985. {
  986. $Log$
  987. Revision 1.60 2002-09-27 21:13:28 carl
  988. * low-highval always checked if limit ober 2GB is reached (to avoid overflow)
  989. Revision 1.59 2002/09/26 15:02:05 florian
  990. + support of passing variants to "array of const"
  991. Revision 1.58 2002/09/07 15:25:03 peter
  992. * old logs removed and tabs fixed
  993. Revision 1.57 2002/09/03 16:26:26 daniel
  994. * Make Tprocdef.defs protected
  995. Revision 1.56 2002/09/01 13:28:37 daniel
  996. - write_access fields removed in favor of a flag
  997. Revision 1.55 2002/09/01 08:01:16 daniel
  998. * Removed sets from Tcallnode.det_resulttype
  999. + Added read/write notifications of variables. These will be usefull
  1000. for providing information for several optimizations. For example
  1001. the value of the loop variable of a for loop does matter is the
  1002. variable is read after the for loop, but if it's no longer used
  1003. or written, it doesn't matter and this can be used to optimize
  1004. the loop code generation.
  1005. Revision 1.54 2002/08/25 19:25:19 peter
  1006. * sym.insert_in_data removed
  1007. * symtable.insertvardata/insertconstdata added
  1008. * removed insert_in_data call from symtable.insert, it needs to be
  1009. called separatly. This allows to deref the address calculation
  1010. * procedures now calculate the parast addresses after the procedure
  1011. directives are parsed. This fixes the cdecl parast problem
  1012. * push_addr_param has an extra argument that specifies if cdecl is used
  1013. or not
  1014. Revision 1.53 2002/08/19 19:36:43 peter
  1015. * More fixes for cross unit inlining, all tnodes are now implemented
  1016. * Moved pocall_internconst to po_internconst because it is not a
  1017. calling type at all and it conflicted when inlining of these small
  1018. functions was requested
  1019. Revision 1.52 2002/08/18 20:06:23 peter
  1020. * inlining is now also allowed in interface
  1021. * renamed write/load to ppuwrite/ppuload
  1022. * tnode storing in ppu
  1023. * nld,ncon,nbas are already updated for storing in ppu
  1024. Revision 1.51 2002/08/17 22:09:46 florian
  1025. * result type handling in tcgcal.pass_2 overhauled
  1026. * better tnode.dowrite
  1027. * some ppc stuff fixed
  1028. Revision 1.50 2002/08/17 09:23:37 florian
  1029. * first part of procinfo rewrite
  1030. Revision 1.49 2002/07/20 11:57:54 florian
  1031. * types.pas renamed to defbase.pas because D6 contains a types
  1032. unit so this would conflicts if D6 programms are compiled
  1033. + Willamette/SSE2 instructions to assembler added
  1034. Revision 1.48 2002/07/20 07:44:37 daniel
  1035. * Forgot to add a $ifdef extdebug
  1036. Revision 1.47 2002/07/19 12:55:27 daniel
  1037. * Further developed state tracking in whilerepeatn
  1038. Revision 1.46 2002/07/19 11:41:36 daniel
  1039. * State tracker work
  1040. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1041. allows the state tracker to change while nodes automatically into
  1042. repeat nodes.
  1043. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1044. 'not(a>b)' is optimized into 'a<=b'.
  1045. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1046. by removing the notn and later switchting the true and falselabels. The
  1047. same is done with 'repeat until not a'.
  1048. Revision 1.45 2002/07/15 18:03:15 florian
  1049. * readded removed changes
  1050. Revision 1.43 2002/07/11 14:41:28 florian
  1051. * start of the new generic parameter handling
  1052. Revision 1.44 2002/07/14 18:00:44 daniel
  1053. + Added the beginning of a state tracker. This will track the values of
  1054. variables through procedures and optimize things away.
  1055. Revision 1.42 2002/05/18 13:34:10 peter
  1056. * readded missing revisions
  1057. Revision 1.41 2002/05/16 19:46:38 carl
  1058. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1059. + try to fix temp allocation (still in ifdef)
  1060. + generic constructor calls
  1061. + start of tassembler / tmodulebase class cleanup
  1062. Revision 1.39 2002/05/12 16:53:07 peter
  1063. * moved entry and exitcode to ncgutil and cgobj
  1064. * foreach gets extra argument for passing local data to the
  1065. iterator function
  1066. * -CR checks also class typecasts at runtime by changing them
  1067. into as
  1068. * fixed compiler to cycle with the -CR option
  1069. * fixed stabs with elf writer, finally the global variables can
  1070. be watched
  1071. * removed a lot of routines from cga unit and replaced them by
  1072. calls to cgobj
  1073. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1074. u32bit then the other is typecasted also to u32bit without giving
  1075. a rangecheck warning/error.
  1076. * fixed pascal calling method with reversing also the high tree in
  1077. the parast, detected by tcalcst3 test
  1078. Revision 1.38 2002/04/25 20:16:39 peter
  1079. * moved more routines from cga/n386util
  1080. Revision 1.37 2002/04/23 19:16:34 peter
  1081. * add pinline unit that inserts compiler supported functions using
  1082. one or more statements
  1083. * moved finalize and setlength from ninl to pinline
  1084. }