pstatmnt.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Does the parsing of the statements
  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 pstatmnt;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. tokens,node;
  23. function statement_block(starttoken : ttoken) : tnode;
  24. { reads an assembler block }
  25. function assembler_block : tnode;
  26. implementation
  27. uses
  28. { common }
  29. cutils,
  30. { global }
  31. globtype,globals,verbose,
  32. systems,cpuinfo,
  33. { aasm }
  34. cpubase,aasm,
  35. { symtable }
  36. symconst,symbase,symtype,symdef,symsym,symtable,types,
  37. { pass 1 }
  38. pass_1,htypechk,
  39. nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser }
  41. scanner,
  42. pbase,pexpr,
  43. { codegen }
  44. tgcpu,hcodegen
  45. {$ifdef i386}
  46. {$ifndef NoRa386Int}
  47. ,ra386int
  48. {$endif NoRa386Int}
  49. {$ifndef NoRa386Att}
  50. ,ra386att
  51. {$endif NoRa386Att}
  52. {$ifndef NoRa386Dir}
  53. ,ra386dir
  54. {$endif NoRa386Dir}
  55. {$endif i386}
  56. {$ifdef m68k}
  57. {$ifndef NoRa68kMot}
  58. ,ra68kmot
  59. {$endif NoRa68kMot}
  60. {$endif m68k}
  61. { codegen }
  62. {$ifdef newcg}
  63. ,cgbase
  64. {$endif newcg}
  65. ;
  66. const
  67. statement_level : longint = 0;
  68. function statement : tnode;forward;
  69. function if_statement : tnode;
  70. var
  71. ex,if_a,else_a : tnode;
  72. begin
  73. consume(_IF);
  74. ex:=comp_expr(true);
  75. consume(_THEN);
  76. if token<>_ELSE then
  77. if_a:=statement
  78. else
  79. if_a:=nil;
  80. if try_to_consume(_ELSE) then
  81. else_a:=statement
  82. else
  83. else_a:=nil;
  84. if_statement:=genloopnode(ifn,ex,if_a,else_a,false);
  85. end;
  86. { creates a block (list) of statements, til the next END token }
  87. function statements_til_end : tnode;
  88. var
  89. first,last : tstatementnode;
  90. begin
  91. first:=nil;
  92. while token<>_END do
  93. begin
  94. if first=nil then
  95. begin
  96. last:=cstatementnode.create(nil,statement);
  97. first:=last;
  98. end
  99. else
  100. begin
  101. last.left:=cstatementnode.create(nil,statement);
  102. last:=tstatementnode(last.left);
  103. end;
  104. if not try_to_consume(_SEMICOLON) then
  105. break;
  106. emptystats;
  107. end;
  108. consume(_END);
  109. statements_til_end:=cblocknode.create(first);
  110. end;
  111. function case_statement : tnode;
  112. var
  113. { contains the label number of currently parsed case block }
  114. aktcaselabel : pasmlabel;
  115. firstlabel : boolean;
  116. root : pcaserecord;
  117. { the typ of the case expression }
  118. casedef : pdef;
  119. procedure newcaselabel(l,h : TConstExprInt;first:boolean);
  120. var
  121. hcaselabel : pcaserecord;
  122. procedure insertlabel(var p : pcaserecord);
  123. begin
  124. if p=nil then p:=hcaselabel
  125. else
  126. if (p^._low>hcaselabel^._low) and
  127. (p^._low>hcaselabel^._high) then
  128. if (hcaselabel^.statement = p^.statement) and
  129. (p^._low = hcaselabel^._high + 1) then
  130. begin
  131. p^._low := hcaselabel^._low;
  132. dispose(hcaselabel);
  133. end
  134. else
  135. insertlabel(p^.less)
  136. else
  137. if (p^._high<hcaselabel^._low) and
  138. (p^._high<hcaselabel^._high) then
  139. if (hcaselabel^.statement = p^.statement) and
  140. (p^._high+1 = hcaselabel^._low) then
  141. begin
  142. p^._high := hcaselabel^._high;
  143. dispose(hcaselabel);
  144. end
  145. else
  146. insertlabel(p^.greater)
  147. else Message(parser_e_double_caselabel);
  148. end;
  149. begin
  150. new(hcaselabel);
  151. hcaselabel^.less:=nil;
  152. hcaselabel^.greater:=nil;
  153. hcaselabel^.statement:=aktcaselabel;
  154. hcaselabel^.firstlabel:=first;
  155. getlabel(hcaselabel^._at);
  156. hcaselabel^._low:=l;
  157. hcaselabel^._high:=h;
  158. insertlabel(root);
  159. end;
  160. var
  161. code,caseexpr,p,instruc,elseblock : tnode;
  162. hl1,hl2 : TConstExprInt;
  163. casedeferror : boolean;
  164. begin
  165. consume(_CASE);
  166. caseexpr:=comp_expr(true);
  167. { determines result type }
  168. cleartempgen;
  169. do_resulttypepass(caseexpr);
  170. casedeferror:=false;
  171. casedef:=caseexpr.resulttype.def;
  172. if (not assigned(casedef)) or
  173. not(is_ordinal(casedef)) then
  174. begin
  175. CGMessage(type_e_ordinal_expr_expected);
  176. { create a correct tree }
  177. caseexpr.free;
  178. caseexpr:=cordconstnode.create(0,u32bittype);
  179. { set error flag so no rangechecks are done }
  180. casedeferror:=true;
  181. end;
  182. consume(_OF);
  183. inc(statement_level);
  184. root:=nil;
  185. instruc:=nil;
  186. repeat
  187. getlabel(aktcaselabel);
  188. firstlabel:=true;
  189. { may be an instruction has more case labels }
  190. repeat
  191. p:=expr;
  192. hl1:=0;
  193. hl2:=0;
  194. if (p.nodetype=rangen) then
  195. begin
  196. { type checking for case statements }
  197. if is_subequal(casedef, trangenode(p).left.resulttype.def) and
  198. is_subequal(casedef, trangenode(p).right.resulttype.def) then
  199. begin
  200. hl1:=get_ordinal_value(trangenode(p).left);
  201. hl2:=get_ordinal_value(trangenode(p).right);
  202. if hl1>hl2 then
  203. CGMessage(parser_e_case_lower_less_than_upper_bound);
  204. if not casedeferror then
  205. begin
  206. testrange(casedef,hl1);
  207. testrange(casedef,hl2);
  208. end;
  209. end
  210. else
  211. CGMessage(parser_e_case_mismatch);
  212. newcaselabel(hl1,hl2,firstlabel);
  213. end
  214. else
  215. begin
  216. { type checking for case statements }
  217. if not is_subequal(casedef, p.resulttype.def) then
  218. CGMessage(parser_e_case_mismatch);
  219. hl1:=get_ordinal_value(p);
  220. if not casedeferror then
  221. testrange(casedef,hl1);
  222. newcaselabel(hl1,hl1,firstlabel);
  223. end;
  224. p.free;
  225. if token=_COMMA then
  226. consume(_COMMA)
  227. else
  228. break;
  229. firstlabel:=false;
  230. until false;
  231. consume(_COLON);
  232. { handles instruction block }
  233. p:=clabelnode.create(aktcaselabel,statement);
  234. { concats instruction }
  235. instruc:=cstatementnode.create(instruc,p);
  236. if not((token=_ELSE) or (token=_OTHERWISE) or (token=_END)) then
  237. consume(_SEMICOLON);
  238. until (token=_ELSE) or (token=_OTHERWISE) or (token=_END);
  239. if (token=_ELSE) or (token=_OTHERWISE) then
  240. begin
  241. if not try_to_consume(_ELSE) then
  242. consume(_OTHERWISE);
  243. elseblock:=statements_til_end;
  244. end
  245. else
  246. begin
  247. elseblock:=nil;
  248. consume(_END);
  249. end;
  250. dec(statement_level);
  251. code:=ccasenode.create(caseexpr,instruc,root);
  252. tcasenode(code).elseblock:=elseblock;
  253. case_statement:=code;
  254. end;
  255. function repeat_statement : tnode;
  256. var
  257. first,last,p_e : tnode;
  258. begin
  259. consume(_REPEAT);
  260. first:=nil;
  261. inc(statement_level);
  262. while token<>_UNTIL do
  263. begin
  264. if first=nil then
  265. begin
  266. last:=cstatementnode.create(nil,statement);
  267. first:=last;
  268. end
  269. else
  270. begin
  271. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  272. last:=tstatementnode(last).left;
  273. end;
  274. if not try_to_consume(_SEMICOLON) then
  275. break;
  276. emptystats;
  277. end;
  278. consume(_UNTIL);
  279. dec(statement_level);
  280. first:=cblocknode.create(first);
  281. p_e:=comp_expr(true);
  282. repeat_statement:=genloopnode(repeatn,p_e,first,nil,false);
  283. end;
  284. function while_statement : tnode;
  285. var
  286. p_e,p_a : tnode;
  287. begin
  288. consume(_WHILE);
  289. p_e:=comp_expr(true);
  290. consume(_DO);
  291. p_a:=statement;
  292. while_statement:=genloopnode(whilen,p_e,p_a,nil,false);
  293. end;
  294. function for_statement : tnode;
  295. var
  296. p_e,tovalue,p_a : tnode;
  297. backward : boolean;
  298. begin
  299. { parse loop header }
  300. consume(_FOR);
  301. p_e:=expr;
  302. if token=_DOWNTO then
  303. begin
  304. consume(_DOWNTO);
  305. backward:=true;
  306. end
  307. else
  308. begin
  309. consume(_TO);
  310. backward:=false;
  311. end;
  312. tovalue:=comp_expr(true);
  313. consume(_DO);
  314. { ... now the instruction }
  315. p_a:=statement;
  316. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  317. end;
  318. function _with_statement : tnode;
  319. var
  320. right,p : tnode;
  321. i,levelcount : longint;
  322. withsymtable,symtab : psymtable;
  323. obj : pobjectdef;
  324. hp : tnode;
  325. begin
  326. p:=comp_expr(true);
  327. do_resulttypepass(p);
  328. set_varstate(p,false);
  329. right:=nil;
  330. if (not codegenerror) and
  331. (p.resulttype.def^.deftype in [objectdef,recorddef]) then
  332. begin
  333. case p.resulttype.def^.deftype of
  334. objectdef : begin
  335. obj:=pobjectdef(p.resulttype.def);
  336. withsymtable:=new(pwithsymtable,init);
  337. withsymtable^.symsearch:=obj^.symtable^.symsearch;
  338. withsymtable^.defowner:=obj;
  339. symtab:=withsymtable;
  340. if (p.nodetype=loadn) and
  341. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  342. pwithsymtable(symtab)^.direct_with:=true;
  343. {symtab^.withnode:=p; not yet allocated !! }
  344. pwithsymtable(symtab)^.withrefnode:=p;
  345. levelcount:=1;
  346. obj:=obj^.childof;
  347. while assigned(obj) do
  348. begin
  349. symtab^.next:=new(pwithsymtable,init);
  350. symtab:=symtab^.next;
  351. symtab^.symsearch:=obj^.symtable^.symsearch;
  352. if (p.nodetype=loadn) and
  353. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  354. pwithsymtable(symtab)^.direct_with:=true;
  355. {symtab^.withnode:=p; not yet allocated !! }
  356. pwithsymtable(symtab)^.withrefnode:=p;
  357. symtab^.defowner:=obj;
  358. obj:=obj^.childof;
  359. inc(levelcount);
  360. end;
  361. symtab^.next:=symtablestack;
  362. symtablestack:=withsymtable;
  363. end;
  364. recorddef : begin
  365. symtab:=precorddef(p.resulttype.def)^.symtable;
  366. levelcount:=1;
  367. withsymtable:=new(pwithsymtable,init);
  368. withsymtable^.symsearch:=symtab^.symsearch;
  369. if (p.nodetype=loadn) and
  370. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  371. pwithsymtable(withsymtable)^.direct_with:=true;
  372. {symtab^.withnode:=p; not yet allocated !! }
  373. pwithsymtable(withsymtable)^.withrefnode:=p;
  374. withsymtable^.defowner:=precorddef(p.resulttype.def);
  375. withsymtable^.next:=symtablestack;
  376. symtablestack:=withsymtable;
  377. end;
  378. end;
  379. if token=_COMMA then
  380. begin
  381. consume(_COMMA);
  382. right:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  383. end
  384. else
  385. begin
  386. consume(_DO);
  387. if token<>_SEMICOLON then
  388. right:=statement
  389. else
  390. right:=nil;
  391. end;
  392. for i:=1 to levelcount do
  393. symtablestack:=symtablestack^.next;
  394. _with_statement:=cwithnode.create(pwithsymtable(withsymtable),p,right,levelcount);
  395. end
  396. else
  397. begin
  398. Message(parser_e_false_with_expr);
  399. { try to recover from error }
  400. if token=_COMMA then
  401. begin
  402. consume(_COMMA);
  403. hp:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  404. if (hp=nil) then; { remove warning about unused }
  405. end
  406. else
  407. begin
  408. consume(_DO);
  409. { ignore all }
  410. if token<>_SEMICOLON then
  411. statement;
  412. end;
  413. _with_statement:=nil;
  414. end;
  415. end;
  416. function with_statement : tnode;
  417. begin
  418. consume(_WITH);
  419. with_statement:=_with_statement;
  420. end;
  421. function raise_statement : tnode;
  422. var
  423. p,pobj,paddr,pframe : tnode;
  424. begin
  425. pobj:=nil;
  426. paddr:=nil;
  427. pframe:=nil;
  428. consume(_RAISE);
  429. if not(token in [_SEMICOLON,_END]) then
  430. begin
  431. { object }
  432. pobj:=comp_expr(true);
  433. if try_to_consume(_AT) then
  434. begin
  435. paddr:=comp_expr(true);
  436. if try_to_consume(_COMMA) then
  437. pframe:=comp_expr(true);
  438. end;
  439. end
  440. else
  441. begin
  442. if (block_type<>bt_except) then
  443. Message(parser_e_no_reraise_possible);
  444. end;
  445. p:=craisenode.create(pobj,paddr,pframe);
  446. raise_statement:=p;
  447. end;
  448. function try_statement : tnode;
  449. var
  450. p_try_block,p_finally_block,first,last,
  451. p_default,p_specific,hp : tnode;
  452. ot : ttype;
  453. sym : pvarsym;
  454. old_block_type : tblock_type;
  455. exceptsymtable : psymtable;
  456. objname,objrealname : stringid;
  457. srsym : psym;
  458. srsymtable : psymtable;
  459. begin
  460. procinfo^.flags:=procinfo^.flags or pi_uses_exceptions;
  461. p_default:=nil;
  462. p_specific:=nil;
  463. { read statements to try }
  464. consume(_TRY);
  465. first:=nil;
  466. inc(statement_level);
  467. while (token<>_FINALLY) and (token<>_EXCEPT) do
  468. begin
  469. if first=nil then
  470. begin
  471. last:=cstatementnode.create(nil,statement);
  472. first:=last;
  473. end
  474. else
  475. begin
  476. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  477. last:=tstatementnode(last).left;
  478. end;
  479. if not try_to_consume(_SEMICOLON) then
  480. break;
  481. emptystats;
  482. end;
  483. p_try_block:=cblocknode.create(first);
  484. if try_to_consume(_FINALLY) then
  485. begin
  486. p_finally_block:=statements_til_end;
  487. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  488. dec(statement_level);
  489. end
  490. else
  491. begin
  492. consume(_EXCEPT);
  493. old_block_type:=block_type;
  494. block_type:=bt_except;
  495. ot:=generrortype;
  496. p_specific:=nil;
  497. if (idtoken=_ON) then
  498. { catch specific exceptions }
  499. begin
  500. repeat
  501. consume(_ID);
  502. if token=_ID then
  503. begin
  504. objname:=pattern;
  505. objrealname:=orgpattern;
  506. { can't use consume_sym here, because we need already
  507. to check for the colon }
  508. searchsym(objname,srsym,srsymtable);
  509. consume(_ID);
  510. { is a explicit name for the exception given ? }
  511. if try_to_consume(_COLON) then
  512. begin
  513. consume_sym(srsym,srsymtable);
  514. if (srsym^.typ=typesym) and
  515. is_class(ptypesym(srsym)^.restype.def) then
  516. begin
  517. ot:=ptypesym(srsym)^.restype;
  518. sym:=new(pvarsym,init(objrealname,ot));
  519. end
  520. else
  521. begin
  522. sym:=new(pvarsym,init(objrealname,generrortype));
  523. if (srsym^.typ=typesym) then
  524. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  525. else
  526. Message1(type_e_class_type_expected,ot.def^.typename);
  527. end;
  528. exceptsymtable:=new(pstoredsymtable,init(stt_exceptsymtable));
  529. exceptsymtable^.insert(sym);
  530. { insert the exception symtable stack }
  531. exceptsymtable^.next:=symtablestack;
  532. symtablestack:=exceptsymtable;
  533. end
  534. else
  535. begin
  536. { check if type is valid, must be done here because
  537. with "e: Exception" the e is not necessary }
  538. if srsym=nil then
  539. begin
  540. identifier_not_found(objrealname);
  541. srsym:=generrorsym;
  542. end;
  543. { support unit.identifier }
  544. if srsym^.typ=unitsym then
  545. begin
  546. consume(_POINT);
  547. srsym:=searchsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  548. if srsym=nil then
  549. begin
  550. identifier_not_found(orgpattern);
  551. srsym:=generrorsym;
  552. end;
  553. consume(_ID);
  554. end;
  555. { check if type is valid, must be done here because
  556. with "e: Exception" the e is not necessary }
  557. if (srsym^.typ=typesym) and
  558. is_class(ptypesym(srsym)^.restype.def) then
  559. ot:=ptypesym(srsym)^.restype
  560. else
  561. begin
  562. ot:=generrortype;
  563. if (srsym^.typ=typesym) then
  564. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  565. else
  566. Message1(type_e_class_type_expected,ot.def^.typename);
  567. end;
  568. exceptsymtable:=nil;
  569. end;
  570. end
  571. else
  572. consume(_ID);
  573. consume(_DO);
  574. hp:=connode.create(nil,statement);
  575. if ot.def^.deftype=errordef then
  576. begin
  577. hp.free;
  578. hp:=cerrornode.create;
  579. end;
  580. if p_specific=nil then
  581. begin
  582. last:=hp;
  583. p_specific:=last;
  584. end
  585. else
  586. begin
  587. tonnode(last).left:=hp;
  588. last:=tonnode(last).left;
  589. end;
  590. { set the informations }
  591. { only if the creation of the onnode was succesful, it's possible }
  592. { that last and hp are errornodes (JM) }
  593. if last.nodetype = onn then
  594. begin
  595. tonnode(last).excepttype:=pobjectdef(ot.def);
  596. tonnode(last).exceptsymtable:=exceptsymtable;
  597. end;
  598. { remove exception symtable }
  599. if assigned(exceptsymtable) then
  600. begin
  601. dellexlevel;
  602. if last.nodetype <> onn then
  603. dispose(exceptsymtable,done);
  604. end;
  605. if not try_to_consume(_SEMICOLON) then
  606. break;
  607. emptystats;
  608. until (token=_END) or (token=_ELSE);
  609. if token=_ELSE then
  610. { catch the other exceptions }
  611. begin
  612. consume(_ELSE);
  613. p_default:=statements_til_end;
  614. end
  615. else
  616. consume(_END);
  617. end
  618. else
  619. { catch all exceptions }
  620. begin
  621. p_default:=statements_til_end;
  622. end;
  623. dec(statement_level);
  624. block_type:=old_block_type;
  625. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  626. end;
  627. end;
  628. function exit_statement : tnode;
  629. var
  630. p : tnode;
  631. begin
  632. consume(_EXIT);
  633. if try_to_consume(_LKLAMMER) then
  634. begin
  635. p:=comp_expr(true);
  636. consume(_RKLAMMER);
  637. if (block_type=bt_except) then
  638. Message(parser_e_exit_with_argument_not__possible);
  639. if is_void(procinfo^.returntype.def) then
  640. Message(parser_e_void_function);
  641. end
  642. else
  643. p:=nil;
  644. p:=cexitnode.create(p);
  645. p.resulttype:=voidtype;
  646. exit_statement:=p;
  647. end;
  648. function _asm_statement : tnode;
  649. var
  650. asmstat : tasmnode;
  651. Marker : tai;
  652. begin
  653. Inside_asm_statement:=true;
  654. case aktasmmode of
  655. asmmode_none : ; { just be there to allow to a compile without
  656. any assembler readers }
  657. {$ifdef i386}
  658. {$ifndef NoRA386Att}
  659. asmmode_i386_att:
  660. asmstat:=tasmnode(ra386att.assemble);
  661. {$endif NoRA386Att}
  662. {$ifndef NoRA386Int}
  663. asmmode_i386_intel:
  664. asmstat:=tasmnode(ra386int.assemble);
  665. {$endif NoRA386Int}
  666. {$ifndef NoRA386Dir}
  667. asmmode_i386_direct:
  668. begin
  669. if not target_asm.allowdirect then
  670. Message(parser_f_direct_assembler_not_allowed);
  671. if (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  672. Begin
  673. Message1(parser_w_not_supported_for_inline,'direct asm');
  674. Message(parser_w_inlining_disabled);
  675. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  676. End;
  677. asmstat:=tasmnode(ra386dir.assemble);
  678. end;
  679. {$endif NoRA386Dir}
  680. {$endif}
  681. {$ifdef m68k}
  682. {$ifndef NoRA68kMot}
  683. asmmode_m68k_mot:
  684. asmstat:=tasmnode(ra68kmot.assemble);
  685. {$endif NoRA68kMot}
  686. {$endif}
  687. else
  688. Message(parser_f_assembler_reader_not_supported);
  689. end;
  690. { Read first the _ASM statement }
  691. consume(_ASM);
  692. {$ifndef newcg}
  693. { END is read }
  694. if try_to_consume(_LECKKLAMMER) then
  695. begin
  696. { it's possible to specify the modified registers }
  697. include(asmstat.flags,nf_object_preserved);
  698. if token<>_RECKKLAMMER then
  699. repeat
  700. { uppercase, because it's a CSTRING }
  701. uppervar(pattern);
  702. {$ifdef i386}
  703. if pattern='EAX' then
  704. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  705. else if pattern='EBX' then
  706. usedinproc:=usedinproc or ($80 shr byte(R_EBX))
  707. else if pattern='ECX' then
  708. usedinproc:=usedinproc or ($80 shr byte(R_ECX))
  709. else if pattern='EDX' then
  710. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  711. else if pattern='ESI' then
  712. begin
  713. usedinproc:=usedinproc or ($80 shr byte(R_ESI));
  714. exclude(asmstat.flags,nf_object_preserved);
  715. end
  716. else if pattern='EDI' then
  717. usedinproc:=usedinproc or ($80 shr byte(R_EDI))
  718. {$endif i386}
  719. {$ifdef m68k}
  720. if pattern='D0' then
  721. usedinproc:=usedinproc or ($800 shr word(R_D0))
  722. else if pattern='D1' then
  723. usedinproc:=usedinproc or ($800 shr word(R_D1))
  724. else if pattern='D6' then
  725. usedinproc:=usedinproc or ($800 shr word(R_D6))
  726. else if pattern='A0' then
  727. usedinproc:=usedinproc or ($800 shr word(R_A0))
  728. else if pattern='A1' then
  729. usedinproc:=usedinproc or ($800 shr word(R_A1))
  730. {$endif m68k}
  731. else consume(_RECKKLAMMER);
  732. consume(_CSTRING);
  733. if not try_to_consume(_COMMA) then
  734. break;
  735. until false;
  736. consume(_RECKKLAMMER);
  737. end
  738. else usedinproc:=$ff;
  739. {$endif newcg}
  740. { mark the start and the end of the assembler block
  741. this is needed for the optimizer }
  742. If Assigned(AsmStat.p_asm) Then
  743. Begin
  744. Marker := Tai_Marker.Create(AsmBlockStart);
  745. AsmStat.p_asm.Insert(Marker);
  746. Marker := Tai_Marker.Create(AsmBlockEnd);
  747. AsmStat.p_asm.Concat(Marker);
  748. End;
  749. Inside_asm_statement:=false;
  750. _asm_statement:=asmstat;
  751. end;
  752. function new_dispose_statement : tnode;
  753. var
  754. p,p2 : tnode;
  755. again : boolean; { dummy for do_proc_call }
  756. destructorname : stringid;
  757. sym : psym;
  758. classh : pobjectdef;
  759. destructorpos,
  760. storepos : tfileposinfo;
  761. is_new : boolean;
  762. begin
  763. if try_to_consume(_NEW) then
  764. is_new:=true
  765. else
  766. begin
  767. consume(_DISPOSE);
  768. is_new:=false;
  769. end;
  770. consume(_LKLAMMER);
  771. p:=comp_expr(true);
  772. { calc return type }
  773. cleartempgen;
  774. set_varstate(p,(not is_new));
  775. { constructor,destructor specified }
  776. if try_to_consume(_COMMA) then
  777. begin
  778. { extended syntax of new and dispose }
  779. { function styled new is handled in factor }
  780. { destructors have no parameters }
  781. destructorname:=pattern;
  782. destructorpos:=akttokenpos;
  783. consume(_ID);
  784. if (p.resulttype.def^.deftype<>pointerdef) then
  785. begin
  786. Message1(type_e_pointer_type_expected,p.resulttype.def^.typename);
  787. p.free;
  788. p:=factor(false);
  789. p.free;
  790. consume(_RKLAMMER);
  791. new_dispose_statement:=cerrornode.create;
  792. exit;
  793. end;
  794. { first parameter must be an object or class }
  795. if ppointerdef(p.resulttype.def)^.pointertype.def^.deftype<>objectdef then
  796. begin
  797. Message(parser_e_pointer_to_class_expected);
  798. p.free;
  799. new_dispose_statement:=factor(false);
  800. consume_all_until(_RKLAMMER);
  801. consume(_RKLAMMER);
  802. exit;
  803. end;
  804. { check, if the first parameter is a pointer to a _class_ }
  805. classh:=pobjectdef(ppointerdef(p.resulttype.def)^.pointertype.def);
  806. if is_class(classh) then
  807. begin
  808. Message(parser_e_no_new_or_dispose_for_classes);
  809. new_dispose_statement:=factor(false);
  810. consume_all_until(_RKLAMMER);
  811. consume(_RKLAMMER);
  812. exit;
  813. end;
  814. { search cons-/destructor, also in parent classes }
  815. storepos:=akttokenpos;
  816. akttokenpos:=destructorpos;
  817. sym:=search_class_member(classh,destructorname);
  818. akttokenpos:=storepos;
  819. { the second parameter of new/dispose must be a call }
  820. { to a cons-/destructor }
  821. if (not assigned(sym)) or (sym^.typ<>procsym) then
  822. begin
  823. if is_new then
  824. Message(parser_e_expr_have_to_be_constructor_call)
  825. else
  826. Message(parser_e_expr_have_to_be_destructor_call);
  827. p.free;
  828. new_dispose_statement:=cerrornode.create;
  829. end
  830. else
  831. begin
  832. if is_new then
  833. p2:=chnewnode.create
  834. else
  835. p2:=chdisposenode.create(p);
  836. do_resulttypepass(p2);
  837. p2.resulttype:=ppointerdef(p.resulttype.def)^.pointertype;
  838. if is_new then
  839. do_member_read(false,sym,p2,again)
  840. else
  841. begin
  842. if (m_tp in aktmodeswitches) then
  843. do_member_read(false,sym,p2,again)
  844. else
  845. begin
  846. p2:=ccallnode.create(nil,pprocsym(sym),sym^.owner,p2);
  847. { support dispose(p,done()); }
  848. if try_to_consume(_LKLAMMER) then
  849. begin
  850. if not try_to_consume(_RKLAMMER) then
  851. begin
  852. Message(parser_e_no_paras_for_destructor);
  853. consume_all_until(_RKLAMMER);
  854. consume(_RKLAMMER);
  855. end;
  856. end;
  857. end;
  858. end;
  859. { we need the real called method }
  860. cleartempgen;
  861. do_resulttypepass(p2);
  862. if not codegenerror then
  863. begin
  864. if is_new then
  865. begin
  866. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_constructor) then
  867. Message(parser_e_expr_have_to_be_constructor_call);
  868. p2:=cnewnode.create(p2);
  869. do_resulttypepass(p2);
  870. p2.resulttype:=p.resulttype;
  871. p2:=cassignmentnode.create(p,p2);
  872. end
  873. else
  874. begin
  875. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_destructor) then
  876. Message(parser_e_expr_have_to_be_destructor_call);
  877. end;
  878. end;
  879. new_dispose_statement:=p2;
  880. end;
  881. end
  882. else
  883. begin
  884. if (p.resulttype.def^.deftype<>pointerdef) then
  885. Begin
  886. Message1(type_e_pointer_type_expected,p.resulttype.def^.typename);
  887. new_dispose_statement:=cerrornode.create;
  888. end
  889. else
  890. begin
  891. if (ppointerdef(p.resulttype.def)^.pointertype.def^.deftype=objectdef) and
  892. (oo_has_vmt in pobjectdef(ppointerdef(p.resulttype.def)^.pointertype.def)^.objectoptions) then
  893. Message(parser_w_use_extended_syntax_for_objects);
  894. if (ppointerdef(p.resulttype.def)^.pointertype.def^.deftype=orddef) and
  895. (porddef(ppointerdef(p.resulttype.def)^.pointertype.def)^.typ=uvoid) then
  896. begin
  897. if (m_tp in aktmodeswitches) or
  898. (m_delphi in aktmodeswitches) then
  899. Message(parser_w_no_new_dispose_on_void_pointers)
  900. else
  901. Message(parser_e_no_new_dispose_on_void_pointers);
  902. end;
  903. if is_new then
  904. new_dispose_statement:=csimplenewdisposenode.create(simplenewn,p)
  905. else
  906. new_dispose_statement:=csimplenewdisposenode.create(simpledisposen,p);
  907. end;
  908. end;
  909. consume(_RKLAMMER);
  910. end;
  911. function statement : tnode;
  912. var
  913. p : tnode;
  914. code : tnode;
  915. filepos : tfileposinfo;
  916. srsym : psym;
  917. srsymtable : psymtable;
  918. begin
  919. filepos:=akttokenpos;
  920. case token of
  921. _GOTO :
  922. begin
  923. if not(cs_support_goto in aktmoduleswitches)then
  924. Message(sym_e_goto_and_label_not_supported);
  925. consume(_GOTO);
  926. if (token<>_INTCONST) and (token<>_ID) then
  927. begin
  928. Message(sym_e_label_not_found);
  929. code:=cerrornode.create;
  930. end
  931. else
  932. begin
  933. consume_sym(srsym,srsymtable);
  934. if srsym^.typ<>labelsym then
  935. begin
  936. Message(sym_e_id_is_no_label_id);
  937. code:=cerrornode.create;
  938. end
  939. else
  940. begin
  941. code:=cgotonode.create(plabelsym(srsym)^.lab);
  942. tgotonode(code).labsym:=plabelsym(srsym);
  943. { set flag that this label is used }
  944. plabelsym(srsym)^.used:=true;
  945. end;
  946. end;
  947. end;
  948. _BEGIN :
  949. code:=statement_block(_BEGIN);
  950. _IF :
  951. code:=if_statement;
  952. _CASE :
  953. code:=case_statement;
  954. _REPEAT :
  955. code:=repeat_statement;
  956. _WHILE :
  957. code:=while_statement;
  958. _FOR :
  959. code:=for_statement;
  960. _NEW,
  961. _DISPOSE :
  962. code:=new_dispose_statement;
  963. _WITH :
  964. code:=with_statement;
  965. _TRY :
  966. code:=try_statement;
  967. _RAISE :
  968. code:=raise_statement;
  969. { semicolons,else until and end are ignored }
  970. _SEMICOLON,
  971. _ELSE,
  972. _UNTIL,
  973. _END:
  974. code:=cnothingnode.create;
  975. _FAIL :
  976. begin
  977. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  978. Message(parser_e_fail_only_in_constructor);
  979. consume(_FAIL);
  980. code:=cfailnode.create;
  981. end;
  982. _EXIT :
  983. code:=exit_statement;
  984. _ASM :
  985. code:=_asm_statement;
  986. _EOF :
  987. Message(scan_f_end_of_file);
  988. else
  989. begin
  990. p:=expr;
  991. if p.nodetype=labeln then
  992. begin
  993. { the pointer to the following instruction }
  994. { isn't a very clean way }
  995. tlabelnode(p).left:=statement{$ifdef FPCPROCVAR}(){$endif};
  996. end;
  997. if not(p.nodetype in [calln,assignn,breakn,inlinen,continuen,labeln]) then
  998. Message(cg_e_illegal_expression);
  999. { specify that we don't use the value returned by the call }
  1000. { Question : can this be also improtant
  1001. for inlinen ??
  1002. it is used for :
  1003. - dispose of temp stack space
  1004. - dispose on FPU stack }
  1005. if p.nodetype=calln then
  1006. exclude(p.flags,nf_return_value_used);
  1007. code:=p;
  1008. end;
  1009. end;
  1010. if assigned(code) then
  1011. code.set_tree_filepos(filepos);
  1012. statement:=code;
  1013. end;
  1014. function statement_block(starttoken : ttoken) : tnode;
  1015. var
  1016. first,last : tnode;
  1017. filepos : tfileposinfo;
  1018. begin
  1019. first:=nil;
  1020. filepos:=akttokenpos;
  1021. consume(starttoken);
  1022. inc(statement_level);
  1023. while not(token in [_END,_FINALIZATION]) do
  1024. begin
  1025. if first=nil then
  1026. begin
  1027. last:=cstatementnode.create(nil,statement);
  1028. first:=last;
  1029. end
  1030. else
  1031. begin
  1032. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  1033. last:=tstatementnode(last).left;
  1034. end;
  1035. if (token in [_END,_FINALIZATION]) then
  1036. break
  1037. else
  1038. begin
  1039. { if no semicolon, then error and go on }
  1040. if token<>_SEMICOLON then
  1041. begin
  1042. consume(_SEMICOLON);
  1043. consume_all_until(_SEMICOLON);
  1044. end;
  1045. consume(_SEMICOLON);
  1046. end;
  1047. emptystats;
  1048. end;
  1049. { don't consume the finalization token, it is consumed when
  1050. reading the finalization block, but allow it only after
  1051. an initalization ! }
  1052. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1053. consume(_END);
  1054. dec(statement_level);
  1055. last:=cblocknode.create(first);
  1056. last.set_tree_filepos(filepos);
  1057. statement_block:=last;
  1058. end;
  1059. function assembler_block : tnode;
  1060. begin
  1061. { temporary space is set, while the BEGIN of the procedure }
  1062. if symtablestack^.symtabletype=localsymtable then
  1063. procinfo^.firsttemp_offset := -symtablestack^.datasize
  1064. else
  1065. procinfo^.firsttemp_offset := 0;
  1066. { assembler code does not allocate }
  1067. { space for the return value }
  1068. if not is_void(procinfo^.returntype.def) then
  1069. begin
  1070. if ret_in_acc(procinfo^.returntype.def) then
  1071. begin
  1072. { in assembler code the result should be directly in %eax
  1073. procinfo^.retoffset:=procinfo^.firsttemp-procinfo^.retdef^.size;
  1074. procinfo^.firsttemp:=procinfo^.retoffset; }
  1075. {$ifndef newcg}
  1076. {$ifdef i386}
  1077. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  1078. {$endif}
  1079. {$ifdef m68k}
  1080. usedinproc:=usedinproc or ($800 shr word(R_D0))
  1081. {$endif}
  1082. {$endif newcg}
  1083. end
  1084. {
  1085. else if not is_fpu(procinfo^.retdef) then
  1086. should we allow assembler functions of big elements ?
  1087. YES (FK)!!
  1088. Message(parser_e_asm_incomp_with_function_return);
  1089. }
  1090. end;
  1091. { set the framepointer to esp for assembler functions }
  1092. { but only if the are no local variables }
  1093. { added no parameter also (PM) }
  1094. { disable for methods, because self pointer is expected }
  1095. { at -8(%ebp) (JM) }
  1096. { why if se use %esp then self is still at the correct address PM }
  1097. if {not(assigned(procinfo^._class)) and}
  1098. (po_assembler in aktprocsym^.definition^.procoptions) and
  1099. (aktprocsym^.definition^.localst^.datasize=0) and
  1100. (aktprocsym^.definition^.parast^.datasize=0) and
  1101. not(ret_in_param(aktprocsym^.definition^.rettype.def)) then
  1102. begin
  1103. procinfo^.framepointer:=stack_pointer;
  1104. { set the right value for parameters }
  1105. dec(aktprocsym^.definition^.parast^.address_fixup,target_os.size_of_pointer);
  1106. dec(procinfo^.para_offset,target_os.size_of_pointer);
  1107. end;
  1108. { force the asm statement }
  1109. if token<>_ASM then
  1110. consume(_ASM);
  1111. procinfo^.Flags := procinfo^.Flags Or pi_is_assembler;
  1112. assembler_block:=_asm_statement;
  1113. { becuase the END is already read we need to get the
  1114. last_endtoken_filepos here (PFV) }
  1115. last_endtoken_filepos:=akttokenpos;
  1116. end;
  1117. end.
  1118. {
  1119. $Log$
  1120. Revision 1.23 2001-04-04 22:43:52 peter
  1121. * remove unnecessary calls to firstpass
  1122. Revision 1.22 2001/04/02 21:20:34 peter
  1123. * resulttype rewrite
  1124. Revision 1.21 2001/03/22 22:35:42 florian
  1125. + support for type a = (a=1); in Delphi mode added
  1126. + procedure p(); in Delphi mode supported
  1127. + on isn't keyword anymore, it can be used as
  1128. id etc. now
  1129. Revision 1.20 2001/03/11 22:58:50 peter
  1130. * getsym redesign, removed the globals srsym,srsymtable
  1131. Revision 1.19 2000/12/25 00:07:27 peter
  1132. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1133. tlinkedlist objects)
  1134. Revision 1.18 2000/12/23 19:59:35 peter
  1135. * object to class for ow/og objects
  1136. * split objectdata from objectoutput
  1137. Revision 1.17 2000/12/16 22:45:55 jonas
  1138. * fixed case statements with int64 values
  1139. Revision 1.16 2000/11/29 00:30:37 florian
  1140. * unused units removed from uses clause
  1141. * some changes for widestrings
  1142. Revision 1.15 2000/11/27 15:47:19 jonas
  1143. * fix for web bug 1251 (example 1)
  1144. Revision 1.14 2000/11/22 22:43:34 peter
  1145. * fixed crash with exception without sysutils (merged)
  1146. Revision 1.13 2000/11/04 14:25:21 florian
  1147. + merged Attila's changes for interfaces, not tested yet
  1148. Revision 1.12 2000/10/31 22:02:50 peter
  1149. * symtable splitted, no real code changes
  1150. Revision 1.11 2000/10/14 21:52:56 peter
  1151. * fixed memory leaks
  1152. Revision 1.10 2000/10/14 10:14:52 peter
  1153. * moehrendorf oct 2000 rewrite
  1154. Revision 1.9 2000/10/01 19:48:25 peter
  1155. * lot of compile updates for cg11
  1156. Revision 1.8 2000/09/24 21:19:50 peter
  1157. * delphi compile fixes
  1158. Revision 1.7 2000/09/24 15:06:24 peter
  1159. * use defines.inc
  1160. Revision 1.6 2000/08/27 16:11:52 peter
  1161. * moved some util functions from globals,cobjects to cutils
  1162. * splitted files into finput,fmodule
  1163. Revision 1.5 2000/08/12 15:41:15 peter
  1164. * fixed bug 1096 (merged)
  1165. Revision 1.4 2000/08/12 06:46:06 florian
  1166. + case statement for int64/qword implemented
  1167. Revision 1.3 2000/07/13 12:08:27 michael
  1168. + patched to 1.1.0 with former 1.09patch from peter
  1169. Revision 1.2 2000/07/13 11:32:45 michael
  1170. + removed logs
  1171. }