nld.pas 42 KB

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