2
0

node.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. registersint:=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:=classtype.createforcopy;
  172. p.nodetype:=nodetype;
  173. p.location:=location;
  174. p.varstateset:=varstateset;
  175. p.parent:=parent;
  176. p.flags:=flags;
  177. p.registers32:=registers32
  178. p.registersfpu:=registersfpu;
  179. {$ifdef SUPPORT_MMX}
  180. p.registersmmx:=registersmmx;
  181. p.registerskni:=registerskni
  182. {$endif SUPPORT_MMX}
  183. p.resulttype:=resulttype;
  184. p.fileinfo:=fileinfo;
  185. p.localswitches:=localswitches;
  186. {$ifdef extdebug}
  187. p.firstpasscount:=firstpasscount;
  188. {$endif extdebug}
  189. p.list:=list;
  190. getcopy:=p;
  191. end;
  192. procedure tnode.set_file_line(from : tnode);
  193. begin
  194. if assigned(from) then
  195. fileinfo:=from.fileinfo;
  196. end;
  197. procedure tnode.set_tree_filepos(const filepos : tfileposinfo);
  198. begin
  199. fileinfo:=filepos;
  200. end;
  201. procedure tnode.unset_varstate;
  202. begin
  203. internalerror(220920002);
  204. end;
  205. procedure tnode.set_varstate(must_be_valid : boolean);
  206. begin
  207. internalerror(220920001);
  208. end;
  209. {$warning FIX ME !!!!!}
  210. {$ifdef dummy}
  211. procedure unset_varstate(p : ptree);
  212. begin
  213. while assigned(p) do
  214. begin
  215. p^.varstateset:=false;
  216. case p^.treetype of
  217. typeconvn,
  218. subscriptn,
  219. vecn :
  220. p:=p^.left;
  221. else
  222. break;
  223. end;
  224. end;
  225. end;
  226. procedure set_varstate(p : ptree;must_be_valid : boolean);
  227. begin
  228. if not assigned(p) then
  229. exit
  230. else
  231. begin
  232. if p^.varstateset then
  233. exit;
  234. case p^.treetype of
  235. typeconvn :
  236. if p^.convtyp in
  237. [
  238. tc_cchar_2_pchar,
  239. tc_cstring_2_pchar,
  240. tc_array_2_pointer
  241. ] then
  242. set_varstate(p^.left,false)
  243. else if p^.convtyp in
  244. [
  245. tc_pchar_2_string,
  246. tc_pointer_2_array
  247. ] then
  248. set_varstate(p^.left,true)
  249. else
  250. set_varstate(p^.left,must_be_valid);
  251. subscriptn :
  252. set_varstate(p^.left,must_be_valid);
  253. vecn:
  254. begin
  255. if (p^.left^.resulttype^.deftype in [stringdef,arraydef]) then
  256. set_varstate(p^.left,must_be_valid)
  257. else
  258. set_varstate(p^.left,true);
  259. set_varstate(p^.right,true);
  260. end;
  261. { do not parse calln }
  262. calln : ;
  263. callparan:
  264. begin
  265. set_varstate(p^.left,must_be_valid);
  266. set_varstate(p^.right,must_be_valid);
  267. end;
  268. loadn :
  269. if (p^.symtableentry^.typ=varsym) then
  270. begin
  271. if must_be_valid and p^.is_first then
  272. begin
  273. if (pvarsym(p^.symtableentry)^.varstate=vs_declared_and_first_found) or
  274. (pvarsym(p^.symtableentry)^.varstate=vs_set_but_first_not_passed) then
  275. if (assigned(pvarsym(p^.symtableentry)^.owner) and
  276. assigned(aktprocsym) and
  277. (pvarsym(p^.symtableentry)^.owner = aktprocsym^.definition^.localst)) then
  278. begin
  279. if p^.symtable^.symtabletype=localsymtable then
  280. CGMessage1(sym_n_uninitialized_local_variable,pvarsym(p^.symtableentry)^.name)
  281. else
  282. CGMessage1(sym_n_uninitialized_variable,pvarsym(p^.symtableentry)^.name);
  283. end;
  284. end;
  285. if (p^.is_first) then
  286. begin
  287. if pvarsym(p^.symtableentry)^.varstate=vs_declared_and_first_found then
  288. { this can only happen at left of an assignment, no ? PM }
  289. if (parsing_para_level=0) and not must_be_valid then
  290. pvarsym(p^.symtableentry)^.varstate:=vs_assigned
  291. else
  292. pvarsym(p^.symtableentry)^.varstate:=vs_used;
  293. if pvarsym(p^.symtableentry)^.varstate=vs_set_but_first_not_passed then
  294. pvarsym(p^.symtableentry)^.varstate:=vs_used;
  295. p^.is_first:=false;
  296. end
  297. else
  298. begin
  299. if (pvarsym(p^.symtableentry)^.varstate=vs_assigned) and
  300. (must_be_valid or (parsing_para_level>0) or
  301. (p^.resulttype^.deftype=procvardef)) then
  302. pvarsym(p^.symtableentry)^.varstate:=vs_used;
  303. if (pvarsym(p^.symtableentry)^.varstate=vs_declared_and_first_found) and
  304. (must_be_valid or (parsing_para_level>0) or
  305. (p^.resulttype^.deftype=procvardef)) then
  306. pvarsym(p^.symtableentry)^.varstate:=vs_set_but_first_not_passed;
  307. end;
  308. end;
  309. funcretn:
  310. begin
  311. { no claim if setting higher return value_str }
  312. if must_be_valid and
  313. (procinfo=pprocinfo(p^.funcretprocinfo)) and
  314. ((procinfo^.funcret_state=vs_declared) or
  315. ((p^.is_first_funcret) and
  316. (procinfo^.funcret_state=vs_declared_and_first_found))) then
  317. begin
  318. CGMessage(sym_w_function_result_not_set);
  319. { avoid multiple warnings }
  320. procinfo^.funcret_state:=vs_assigned;
  321. end;
  322. if p^.is_first_funcret and not must_be_valid then
  323. pprocinfo(p^.funcretprocinfo)^.funcret_state:=vs_assigned;
  324. end;
  325. else
  326. begin
  327. {internalerror(565656);}
  328. end;
  329. end;{case }
  330. p^.varstateset:=true;
  331. end;
  332. end;
  333. {$endif}
  334. {****************************************************************************
  335. TUNARYNODE
  336. ****************************************************************************}
  337. constructor tunarynode.create(tt : tnodetype;l : tnode);
  338. begin
  339. inherited create(tt);
  340. left:=l;
  341. end;
  342. function tunarynode.docompare(p : tnode) : boolean;
  343. begin
  344. docompare:=(inherited docompare(p)) and
  345. left.isequal(tunarynode(p).left);
  346. end;
  347. function.tunarynode.getcopy : tnode;
  348. var
  349. p : tunarynode;
  350. begin
  351. p:=tunarynode(inherited getcopy);
  352. p.left:=left.getcopy;
  353. end;
  354. {$ifdef extdebug}
  355. procedure tunarynode.dowrite;
  356. begin
  357. inherited dowrite;
  358. writeln(',');
  359. writenode(left);
  360. writeln(')');
  361. dec(byte(indention[0]),2);
  362. end;
  363. {$endif}
  364. procedure tunarynode.concattolist(l : plinkedlist);
  365. begin
  366. left.parent:=self;
  367. left.concattolist(l);
  368. inherited concattolist(l);
  369. end;
  370. function tunarynode.ischild(p : tnode) : boolean;
  371. begin
  372. ischild:=p=left;
  373. end;
  374. procedure tunarynode.det_resulttype;
  375. begin
  376. left.det_resulttype;
  377. end;
  378. procedure tunarynode.det_temp;
  379. begin
  380. left.det_temp;
  381. end;
  382. {****************************************************************************
  383. TBINARYNODE
  384. ****************************************************************************}
  385. constructor tbinarynode.create(tt : tnodetype;l,r : tnode);
  386. begin
  387. inherited create(tt,l);
  388. right:=r
  389. end;
  390. procedure tbinarynode.concattolist(l : plinkedlist);
  391. begin
  392. { we could change that depending on the number of }
  393. { required registers }
  394. left.parent:=self;
  395. left.concattolist(l);
  396. left.parent:=self;
  397. left.concattolist(l);
  398. inherited concattolist(l);
  399. end;
  400. function tbinarynode.ischild(p : tnode) : boolean;
  401. begin
  402. ischild:=(p=right) or (p=right);
  403. end;
  404. procedure tbinarynode.det_resulttype;
  405. begin
  406. left.det_resulttype;
  407. right.det_resulttype;
  408. end;
  409. procedure tbinarynode.det_temp;
  410. begin
  411. left.det_temp;
  412. right.det_temp;
  413. end;
  414. function tbinarynode.docompare(p : tnode) : boolean;
  415. begin
  416. docompare:=left.isequal(tbinarynode(p).left) and
  417. right.isequal(tbinarynode(p).right);
  418. end;
  419. function.tbinarynode.getcopy : tnode;
  420. var
  421. p : tbinarynode;
  422. begin
  423. p:=tbinarynode(inherited getcopy);
  424. p.right:=right.getcopy;
  425. end;
  426. function tbinarynode.isbinaryoverloaded(var t : tnode) : boolean;
  427. var
  428. rd,ld : pdef;
  429. optoken : ttoken;
  430. begin
  431. t:=nil;
  432. isbinaryoverloaded:=false;
  433. { overloaded operator ? }
  434. { load easier access variables }
  435. rd:=right.resulttype;
  436. ld:=left.resulttype;
  437. if isbinaryoperatoroverloadable(ld,rd,voiddef,nodetype) then
  438. begin
  439. isbinaryoverloaded:=true;
  440. {!!!!!!!!! handle paras }
  441. case nodetype of
  442. { the nil as symtable signs firstcalln that this is
  443. an overloaded operator }
  444. addn:
  445. optoken:=_PLUS;
  446. subn:
  447. optoken:=_MINUS;
  448. muln:
  449. optoken:=_STAR;
  450. starstarn:
  451. optoken:=_STARSTAR;
  452. slashn:
  453. optoken:=_SLASH;
  454. ltn:
  455. optoken:=tokens._lt;
  456. gtn:
  457. optoken:=tokens._gt;
  458. lten:
  459. optoken:=_lte;
  460. gten:
  461. optoken:=_gte;
  462. equaln,unequaln :
  463. optoken:=_EQUAL;
  464. symdifn :
  465. optoken:=_SYMDIF;
  466. modn :
  467. optoken:=_OP_MOD;
  468. orn :
  469. optoken:=_OP_OR;
  470. xorn :
  471. optoken:=_OP_XOR;
  472. andn :
  473. optoken:=_OP_AND;
  474. divn :
  475. optoken:=_OP_DIV;
  476. shln :
  477. optoken:=_OP_SHL;
  478. shrn :
  479. optoken:=_OP_SHR;
  480. else
  481. exit;
  482. end;
  483. t:=gencallnode(overloaded_operators[optoken],nil);
  484. { we have to convert p^.left and p^.right into
  485. callparanodes }
  486. if tcallnode(t).symtableprocentry=nil then
  487. begin
  488. CGMessage(parser_e_operator_not_overloaded);
  489. t.free;
  490. end
  491. else
  492. begin
  493. inc(tcallnode(t).symtableprocentry^.refs);
  494. tcallnode(t).left:=gencallparanode(left,nil);
  495. tcallnode(t).left:=gencallparanode(right,tcallnode(t).left);
  496. if nodetype=unequaln then
  497. t:=cnotnode.create(t);
  498. firstpass(t);
  499. putnode(p);
  500. p:=t;
  501. end;
  502. end;
  503. end;
  504. procedure tbinarynode.swapleftright;
  505. var
  506. swapp : tnode;
  507. begin
  508. swapp:=right;
  509. right:=left;
  510. left:=swapp;
  511. if nf_swaped in flags then
  512. exclude(flags,nf_swaped)
  513. else
  514. include(flags,nf_swaped);
  515. end;
  516. procedure tbinarynode.left_right_max;
  517. begin
  518. if assigned(left) then
  519. begin
  520. if assigned(right) then
  521. begin
  522. registers32:=max(left.registers32,right.registers32);
  523. registersfpu:=max(left.registersfpu,right.registersfpu);
  524. {$ifdef SUPPORT_MMX}
  525. registersmmx:=max(left.registersmmx,right.registersmmx);
  526. {$endif SUPPORT_MMX}
  527. end
  528. else
  529. begin
  530. registers32:=left.registers32;
  531. registersfpu:=left.registersfpu;
  532. {$ifdef SUPPORT_MMX}
  533. registersmmx:=left.registersmmx;
  534. {$endif SUPPORT_MMX}
  535. end;
  536. end;
  537. end;
  538. {****************************************************************************
  539. TBINOPYNODE
  540. ****************************************************************************}
  541. constructor tbinopnode.create(tt : tnodetype;l,r : tnode);
  542. begin
  543. inherited create(tt,l,r);
  544. end;
  545. function tbinopnode.docompare(p : tnode) : boolean;
  546. begin
  547. docompare:=(inherited docompare(p)) or
  548. ((nf_swapable in flags) and
  549. left.isequal(tbinopnode(p).right) and
  550. right.isequal(tbinopnode(p).left));
  551. end;
  552. {
  553. $Log$
  554. Revision 1.4 2000-09-26 20:06:13 florian
  555. * hmm, still a lot of work to get things compilable
  556. Revision 1.3 2000/09/22 21:45:36 florian
  557. * some updates e.g. getcopy added
  558. Revision 1.2 2000/09/20 21:52:38 florian
  559. * removed a lot of errors
  560. Revision 1.1 2000/08/26 12:27:17 florian
  561. * createial release
  562. }