node.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. {
  2. $Id$
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. The implementation of the abstract 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. {****************************************************************************
  19. TNODE
  20. ****************************************************************************}
  21. constructor tnode.create(tt : tnodetype);
  22. begin
  23. inherited create;
  24. nodetype:=tt;
  25. { this allows easier error tracing }
  26. location.loc:=LOC_INVALID;
  27. { save local info }
  28. fileinfo:=aktfilepos;
  29. localswitches:=aktlocalswitches;
  30. resulttype:=nil;
  31. registers32:=0;
  32. registersfpu:=0;
  33. {$ifdef SUPPORT_MMX}
  34. registersmmx:=0;
  35. {$endif SUPPORT_MMX}
  36. flags:=[];
  37. end;
  38. constructor tnode.createforcopy;
  39. begin
  40. end;
  41. destructor tnode.destroy;
  42. begin
  43. { reference info }
  44. if (location.loc in [LOC_MEM,LOC_REFERENCE]) and
  45. assigned(location.reference.symbol) then
  46. dispose(location.reference.symbol,done);
  47. {$ifdef EXTDEBUG}
  48. if firstpasscount>maxfirstpasscount then
  49. maxfirstpasscount:=firstpasscount;
  50. {$endif EXTDEBUG}
  51. end;
  52. function tnode.pass_1 : tnode;
  53. begin
  54. if not(assigned(resulttype)) then
  55. det_resulttype;
  56. det_temp;
  57. end;
  58. procedure tnode.concattolist(l : plinkedlist);
  59. begin
  60. {$ifdef newcg}
  61. l^.concat(self);
  62. {$endif newcg}
  63. end;
  64. function tnode.ischild(p : tnode) : boolean;
  65. begin
  66. ischild:=false;
  67. end;
  68. {$ifdef EXTDEBUG}
  69. procedure tnode.dowrite;
  70. const treetype2str : array[tnodetype] of string[20] = (
  71. 'addn',
  72. 'muln',
  73. 'subn',
  74. 'divn',
  75. 'symdifn',
  76. 'modn',
  77. 'assignn',
  78. 'loadn',
  79. 'rangen',
  80. 'ltn',
  81. 'lten',
  82. 'gtn',
  83. 'gten',
  84. 'equaln',
  85. 'unequaln',
  86. 'inn',
  87. 'orn',
  88. 'xorn',
  89. 'shrn',
  90. 'shln',
  91. 'slashn',
  92. 'andn',
  93. 'subscriptn',
  94. 'derefn',
  95. 'addrn',
  96. 'doubleaddrn',
  97. 'ordconstn',
  98. 'typeconvn',
  99. 'calln',
  100. 'callparan',
  101. 'realconstn',
  102. 'fixconstn',
  103. 'umminusn',
  104. 'asmn',
  105. 'vecn',
  106. 'stringconstn',
  107. 'funcretn',
  108. 'selfn',
  109. 'notn',
  110. 'inlinen',
  111. 'niln',
  112. 'errorn',
  113. 'typen',
  114. 'hnewn',
  115. 'hdisposen',
  116. 'newn',
  117. 'simpledisposen',
  118. 'setelementn',
  119. 'setconstn',
  120. 'blockn',
  121. 'statementn',
  122. 'loopn',
  123. 'ifn',
  124. 'breakn',
  125. 'continuen',
  126. 'repeatn',
  127. 'whilen',
  128. 'forn',
  129. 'exitn',
  130. 'withn',
  131. 'casen',
  132. 'labeln',
  133. 'goton',
  134. 'simplenewn',
  135. 'tryexceptn',
  136. 'raisen',
  137. 'switchesn',
  138. 'tryfinallyn',
  139. 'onn',
  140. 'isn',
  141. 'asn',
  142. 'caretn',
  143. 'failn',
  144. 'starstarn',
  145. 'procinlinen',
  146. 'arrayconstructn',
  147. 'arrayconstructrangen',
  148. 'nothingn',
  149. 'loadvmtn',
  150. 'pointerconstn');
  151. begin
  152. write(indention,'(',treetype2str[nodetype]);
  153. end;
  154. {$endif EXTDEBUG}
  155. function tnode.isequal(p : tnode) : boolean;
  156. begin
  157. isequal:=assigned(p) and (p.nodetype=nodetype) and
  158. (flags*flagsequal=p.flags*flagsequal) and
  159. docompare(p);
  160. end;
  161. function tnode.docompare(p : tnode) : boolean;
  162. begin
  163. docompare:=true;
  164. end;
  165. function tnode.getcopy : tnode;
  166. var
  167. p : tnode;
  168. begin
  169. { this is quite tricky because we need a node of the current }
  170. { node type and not one of tnode! }
  171. p:=tnode(classtype).createforcopy;
  172. p.nodetype:=nodetype;
  173. p.location:=location;
  174. p.parent:=parent;
  175. p.flags:=flags;
  176. p.registers32:=registers32;
  177. p.registersfpu:=registersfpu;
  178. {$ifdef SUPPORT_MMX}
  179. p.registersmmx:=registersmmx;
  180. p.registerskni:=registerskni;
  181. {$endif SUPPORT_MMX}
  182. p.resulttype:=resulttype;
  183. p.fileinfo:=fileinfo;
  184. p.localswitches:=localswitches;
  185. {$ifdef extdebug}
  186. p.firstpasscount:=firstpasscount;
  187. {$endif extdebug}
  188. p.list:=list;
  189. getcopy:=p;
  190. end;
  191. procedure tnode.set_file_line(from : tnode);
  192. begin
  193. if assigned(from) then
  194. fileinfo:=from.fileinfo;
  195. end;
  196. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  197. begin
  198. fileinfo:=filepos;
  199. end;
  200. procedure tnode.unset_varstate;
  201. begin
  202. internalerror(220920002);
  203. end;
  204. procedure tnode.set_varstate(must_be_valid : boolean);
  205. begin
  206. internalerror(220920001);
  207. end;
  208. procedure tnode.set_unique;
  209. begin
  210. case nodetype of
  211. vecn:
  212. include(flags,nf_callunique);
  213. typeconvn,subscriptn,derefn:
  214. if assigned(tunarynode(self).left) then
  215. tunarynode(self).left.set_unique;
  216. end;
  217. end;
  218. procedure tnode.set_funcret_is_valid;
  219. begin
  220. case nodetype of
  221. funcretn:
  222. if nf_is_first_funcret in flags then
  223. pprocinfo(tfuncretnode(self).funcretprocinfo)^.funcret_state:=vs_assigned;
  224. vecn,typeconvn,subscriptn{,derefn}:
  225. if assigned(tunarynode(self).left) then
  226. tunarynode(self).left.set_funcret_is_valid;
  227. end;
  228. end;
  229. {$warning FIX ME !!!!!}
  230. {$ifdef dummy}
  231. procedure unset_varstate(p : ptree);
  232. begin
  233. while assigned(p) do
  234. begin
  235. p^.varstateset:=false;
  236. case p^.treetype of
  237. typeconvn,
  238. subscriptn,
  239. vecn :
  240. p:=p^.left;
  241. else
  242. break;
  243. end;
  244. end;
  245. end;
  246. procedure set_varstate(p : ptree;must_be_valid : boolean);
  247. begin
  248. if not assigned(p) then
  249. exit
  250. else
  251. begin
  252. if p^.varstateset then
  253. exit;
  254. case p^.treetype of
  255. typeconvn :
  256. if p^.convtyp in
  257. [
  258. tc_cchar_2_pchar,
  259. tc_cstring_2_pchar,
  260. tc_array_2_pointer
  261. ] then
  262. set_varstate(p^.left,false)
  263. else if p^.convtyp in
  264. [
  265. tc_pchar_2_string,
  266. tc_pointer_2_array
  267. ] then
  268. set_varstate(p^.left,true)
  269. else
  270. set_varstate(p^.left,must_be_valid);
  271. subscriptn :
  272. set_varstate(p^.left,must_be_valid);
  273. vecn:
  274. begin
  275. if (p^.left^.resulttype^.deftype in [stringdef,arraydef]) then
  276. set_varstate(p^.left,must_be_valid)
  277. else
  278. set_varstate(p^.left,true);
  279. set_varstate(p^.right,true);
  280. end;
  281. { do not parse calln }
  282. calln : ;
  283. callparan:
  284. begin
  285. set_varstate(p^.left,must_be_valid);
  286. set_varstate(p^.right,must_be_valid);
  287. end;
  288. loadn :
  289. if (p^.symtableentry^.typ=varsym) then
  290. begin
  291. if must_be_valid and p^.is_first then
  292. begin
  293. if (pvarsym(p^.symtableentry)^.varstate=vs_declared_and_first_found) or
  294. (pvarsym(p^.symtableentry)^.varstate=vs_set_but_first_not_passed) then
  295. if (assigned(pvarsym(p^.symtableentry)^.owner) and
  296. assigned(aktprocsym) and
  297. (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  298. begin
  299. if p^.symtable^.symtabletype=localsymtable then
  300. CGMessage1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name)
  301. else
  302. CGMessage1(sym_n_uninitialized_variable,pvarsym(p^.symtableentry)^.name);
  303. end;
  304. end;
  305. if (p^.is_first) then
  306. begin
  307. if pvarsym(p^.symtableentry)^.varstate=vs_declared_and_first_found then
  308. { this can only happen at left of an assignment, no ? PM }
  309. if (parsing_para_level=0) and not must_be_valid then
  310. pvarsym(p^.symtableentry)^.varstate:=vs_assigned
  311. else
  312. pvarsym(p^.symtableentry)^.varstate:=vs_used;
  313. if pvarsym(p^.symtableentry)^.varstate=vs_set_but_first_not_passed then
  314. pvarsym(p^.symtableentry)^.varstate:=vs_used;
  315. p^.is_first:=false;
  316. end
  317. else
  318. begin
  319. if (pvarsym(p^.symtableentry)^.varstate=vs_assigned) and
  320. (must_be_valid or (parsing_para_level>0) or
  321. (p^.resulttype^.deftype=procvardef)) then
  322. pvarsym(p^.symtableentry)^.varstate:=vs_used;
  323. if (pvarsym(p^.symtableentry)^.varstate=vs_declared_and_first_found) and
  324. (must_be_valid or (parsing_para_level>0) or
  325. (p^.resulttype^.deftype=procvardef)) then
  326. pvarsym(p^.symtableentry)^.varstate:=vs_set_but_first_not_passed;
  327. end;
  328. end;
  329. funcretn:
  330. begin
  331. { no claim if setting higher return value_str }
  332. if must_be_valid and
  333. (procinfo=pprocinfo(p^.funcretprocinfo)) and
  334. ((procinfo^.funcret_state=vs_declared) or
  335. ((p^.is_first_funcret) and
  336. (procinfo^.funcret_state=vs_declared_and_first_found))) then
  337. begin
  338. CGMessage(sym_w_function_result_not_set);
  339. { avoid multiple warnings }
  340. procinfo^.funcret_state:=vs_assigned;
  341. end;
  342. if p^.is_first_funcret and not must_be_valid then
  343. pprocinfo(p^.funcretprocinfo)^.funcret_state:=vs_assigned;
  344. end;
  345. else
  346. begin
  347. {internalerror(565656);}
  348. end;
  349. end;{case }
  350. p^.varstateset:=true;
  351. end;
  352. end;
  353. {$endif}
  354. {****************************************************************************
  355. TUNARYNODE
  356. ****************************************************************************}
  357. constructor tunarynode.create(tt : tnodetype;l : tnode);
  358. begin
  359. inherited create(tt);
  360. left:=l;
  361. end;
  362. function tunarynode.docompare(p : tnode) : boolean;
  363. begin
  364. docompare:=(inherited docompare(p)) and
  365. left.isequal(tunarynode(p).left);
  366. end;
  367. function tunarynode.getcopy : tnode;
  368. var
  369. p : tunarynode;
  370. begin
  371. p:=tunarynode(inherited getcopy);
  372. p.left:=left.getcopy;
  373. getcopy:=p;
  374. end;
  375. {$ifdef extdebug}
  376. procedure tunarynode.dowrite;
  377. begin
  378. inherited dowrite;
  379. writeln(',');
  380. writenode(left);
  381. writeln(')');
  382. dec(byte(indention[0]),2);
  383. end;
  384. {$endif}
  385. procedure tunarynode.left_max;
  386. begin
  387. registers32:=left.registers32;
  388. registersfpu:=left.registersfpu;
  389. {$ifdef SUPPORT_MMX}
  390. registersmmx:=left.registersmmx;
  391. {$endif SUPPORT_MMX}
  392. end;
  393. procedure tunarynode.concattolist(l : plinkedlist);
  394. begin
  395. left.parent:=self;
  396. left.concattolist(l);
  397. inherited concattolist(l);
  398. end;
  399. function tunarynode.ischild(p : tnode) : boolean;
  400. begin
  401. ischild:=p=left;
  402. end;
  403. procedure tunarynode.det_resulttype;
  404. begin
  405. left.det_resulttype;
  406. end;
  407. procedure tunarynode.det_temp;
  408. begin
  409. left.det_temp;
  410. end;
  411. {****************************************************************************
  412. TBINARYNODE
  413. ****************************************************************************}
  414. constructor tbinarynode.create(tt : tnodetype;l,r : tnode);
  415. begin
  416. inherited create(tt,l);
  417. right:=r
  418. end;
  419. procedure tbinarynode.concattolist(l : plinkedlist);
  420. begin
  421. { we could change that depending on the number of }
  422. { required registers }
  423. left.parent:=self;
  424. left.concattolist(l);
  425. left.parent:=self;
  426. left.concattolist(l);
  427. inherited concattolist(l);
  428. end;
  429. function tbinarynode.ischild(p : tnode) : boolean;
  430. begin
  431. ischild:=(p=right) or (p=right);
  432. end;
  433. procedure tbinarynode.det_resulttype;
  434. begin
  435. left.det_resulttype;
  436. right.det_resulttype;
  437. end;
  438. procedure tbinarynode.det_temp;
  439. begin
  440. left.det_temp;
  441. right.det_temp;
  442. end;
  443. function tbinarynode.docompare(p : tnode) : boolean;
  444. begin
  445. docompare:=left.isequal(tbinarynode(p).left) and
  446. right.isequal(tbinarynode(p).right);
  447. end;
  448. function tbinarynode.getcopy : tnode;
  449. var
  450. p : tbinarynode;
  451. begin
  452. p:=tbinarynode(inherited getcopy);
  453. p.right:=right.getcopy;
  454. getcopy:=p;
  455. end;
  456. function tbinarynode.isbinaryoverloaded(var t : tnode) : boolean;
  457. var
  458. rd,ld : pdef;
  459. optoken : ttoken;
  460. begin
  461. t:=nil;
  462. isbinaryoverloaded:=false;
  463. { overloaded operator ? }
  464. { load easier access variables }
  465. rd:=right.resulttype;
  466. ld:=left.resulttype;
  467. if isbinaryoperatoroverloadable(ld,rd,voiddef,nodetype) then
  468. begin
  469. isbinaryoverloaded:=true;
  470. {!!!!!!!!! handle paras }
  471. case nodetype of
  472. { the nil as symtable signs firstcalln that this is
  473. an overloaded operator }
  474. addn:
  475. optoken:=_PLUS;
  476. subn:
  477. optoken:=_MINUS;
  478. muln:
  479. optoken:=_STAR;
  480. starstarn:
  481. optoken:=_STARSTAR;
  482. slashn:
  483. optoken:=_SLASH;
  484. ltn:
  485. optoken:=tokens._lt;
  486. gtn:
  487. optoken:=tokens._gt;
  488. lten:
  489. optoken:=_lte;
  490. gten:
  491. optoken:=_gte;
  492. equaln,unequaln :
  493. optoken:=_EQUAL;
  494. symdifn :
  495. optoken:=_SYMDIF;
  496. modn :
  497. optoken:=_OP_MOD;
  498. orn :
  499. optoken:=_OP_OR;
  500. xorn :
  501. optoken:=_OP_XOR;
  502. andn :
  503. optoken:=_OP_AND;
  504. divn :
  505. optoken:=_OP_DIV;
  506. shln :
  507. optoken:=_OP_SHL;
  508. shrn :
  509. optoken:=_OP_SHR;
  510. else
  511. exit;
  512. end;
  513. t:=gencallnode(overloaded_operators[optoken],nil);
  514. { we have to convert p^.left and p^.right into
  515. callparanodes }
  516. if tcallnode(t).symtableprocentry=nil then
  517. begin
  518. CGMessage(parser_e_operator_not_overloaded);
  519. t.free;
  520. t:=nil;
  521. end
  522. else
  523. begin
  524. inc(tcallnode(t).symtableprocentry^.refs);
  525. tcallnode(t).left:=gencallparanode(left,nil);
  526. tcallnode(t).left:=gencallparanode(right,tcallnode(t).left);
  527. if nodetype=unequaln then
  528. t:=cnotnode.create(t);
  529. firstpass(t);
  530. end;
  531. end;
  532. end;
  533. procedure tbinarynode.swapleftright;
  534. var
  535. swapp : tnode;
  536. begin
  537. swapp:=right;
  538. right:=left;
  539. left:=swapp;
  540. if nf_swaped in flags then
  541. exclude(flags,nf_swaped)
  542. else
  543. include(flags,nf_swaped);
  544. end;
  545. procedure tbinarynode.left_right_max;
  546. begin
  547. if assigned(left) then
  548. begin
  549. if assigned(right) then
  550. begin
  551. registers32:=max(left.registers32,right.registers32);
  552. registersfpu:=max(left.registersfpu,right.registersfpu);
  553. {$ifdef SUPPORT_MMX}
  554. registersmmx:=max(left.registersmmx,right.registersmmx);
  555. {$endif SUPPORT_MMX}
  556. end
  557. else
  558. begin
  559. registers32:=left.registers32;
  560. registersfpu:=left.registersfpu;
  561. {$ifdef SUPPORT_MMX}
  562. registersmmx:=left.registersmmx;
  563. {$endif SUPPORT_MMX}
  564. end;
  565. end;
  566. end;
  567. {****************************************************************************
  568. TBINOPYNODE
  569. ****************************************************************************}
  570. constructor tbinopnode.create(tt : tnodetype;l,r : tnode);
  571. begin
  572. inherited create(tt,l,r);
  573. end;
  574. function tbinopnode.docompare(p : tnode) : boolean;
  575. begin
  576. docompare:=(inherited docompare(p)) or
  577. ((nf_swapable in flags) and
  578. left.isequal(tbinopnode(p).right) and
  579. right.isequal(tbinopnode(p).left));
  580. end;
  581. {
  582. $Log$
  583. Revision 1.7 2000-09-29 15:45:23 florian
  584. * make cycle fixed
  585. Revision 1.6 2000/09/28 19:49:52 florian
  586. *** empty log message ***
  587. Revision 1.5 2000/09/27 18:14:31 florian
  588. * fixed a lot of syntax errors in the n*.pas stuff
  589. Revision 1.4 2000/09/26 20:06:13 florian
  590. * hmm, still a lot of work to get things compilable
  591. Revision 1.3 2000/09/22 21:45:36 florian
  592. * some updates e.g. getcopy added
  593. Revision 1.2 2000/09/20 21:52:38 florian
  594. * removed a lot of errors
  595. Revision 1.1 2000/08/26 12:27:17 florian
  596. * createial release
  597. }