pstatmnt.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. cleartempgen;
  193. do_firstpass(p);
  194. hl1:=0;
  195. hl2:=0;
  196. if (p.nodetype=rangen) then
  197. begin
  198. { type checking for case statements }
  199. if is_subequal(casedef, trangenode(p).left.resulttype.def) and
  200. is_subequal(casedef, trangenode(p).right.resulttype.def) then
  201. begin
  202. hl1:=get_ordinal_value(trangenode(p).left);
  203. hl2:=get_ordinal_value(trangenode(p).right);
  204. if hl1>hl2 then
  205. CGMessage(parser_e_case_lower_less_than_upper_bound);
  206. if not casedeferror then
  207. begin
  208. testrange(casedef,hl1);
  209. testrange(casedef,hl2);
  210. end;
  211. end
  212. else
  213. CGMessage(parser_e_case_mismatch);
  214. newcaselabel(hl1,hl2,firstlabel);
  215. end
  216. else
  217. begin
  218. { type checking for case statements }
  219. if not is_subequal(casedef, p.resulttype.def) then
  220. CGMessage(parser_e_case_mismatch);
  221. hl1:=get_ordinal_value(p);
  222. if not casedeferror then
  223. testrange(casedef,hl1);
  224. newcaselabel(hl1,hl1,firstlabel);
  225. end;
  226. p.free;
  227. if token=_COMMA then
  228. consume(_COMMA)
  229. else
  230. break;
  231. firstlabel:=false;
  232. until false;
  233. consume(_COLON);
  234. { handles instruction block }
  235. p:=clabelnode.create(aktcaselabel,statement);
  236. { concats instruction }
  237. instruc:=cstatementnode.create(instruc,p);
  238. if not((token=_ELSE) or (token=_OTHERWISE) or (token=_END)) then
  239. consume(_SEMICOLON);
  240. until (token=_ELSE) or (token=_OTHERWISE) or (token=_END);
  241. if (token=_ELSE) or (token=_OTHERWISE) then
  242. begin
  243. if not try_to_consume(_ELSE) then
  244. consume(_OTHERWISE);
  245. elseblock:=statements_til_end;
  246. end
  247. else
  248. begin
  249. elseblock:=nil;
  250. consume(_END);
  251. end;
  252. dec(statement_level);
  253. code:=ccasenode.create(caseexpr,instruc,root);
  254. tcasenode(code).elseblock:=elseblock;
  255. case_statement:=code;
  256. end;
  257. function repeat_statement : tnode;
  258. var
  259. first,last,p_e : tnode;
  260. begin
  261. consume(_REPEAT);
  262. first:=nil;
  263. inc(statement_level);
  264. while token<>_UNTIL do
  265. begin
  266. if first=nil then
  267. begin
  268. last:=cstatementnode.create(nil,statement);
  269. first:=last;
  270. end
  271. else
  272. begin
  273. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  274. last:=tstatementnode(last).left;
  275. end;
  276. if not try_to_consume(_SEMICOLON) then
  277. break;
  278. emptystats;
  279. end;
  280. consume(_UNTIL);
  281. dec(statement_level);
  282. first:=cblocknode.create(first);
  283. p_e:=comp_expr(true);
  284. repeat_statement:=genloopnode(repeatn,p_e,first,nil,false);
  285. end;
  286. function while_statement : tnode;
  287. var
  288. p_e,p_a : tnode;
  289. begin
  290. consume(_WHILE);
  291. p_e:=comp_expr(true);
  292. consume(_DO);
  293. p_a:=statement;
  294. while_statement:=genloopnode(whilen,p_e,p_a,nil,false);
  295. end;
  296. function for_statement : tnode;
  297. var
  298. p_e,tovalue,p_a : tnode;
  299. backward : boolean;
  300. begin
  301. { parse loop header }
  302. consume(_FOR);
  303. p_e:=expr;
  304. if token=_DOWNTO then
  305. begin
  306. consume(_DOWNTO);
  307. backward:=true;
  308. end
  309. else
  310. begin
  311. consume(_TO);
  312. backward:=false;
  313. end;
  314. tovalue:=comp_expr(true);
  315. consume(_DO);
  316. { ... now the instruction }
  317. p_a:=statement;
  318. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  319. end;
  320. function _with_statement : tnode;
  321. var
  322. right,p : tnode;
  323. i,levelcount : longint;
  324. withsymtable,symtab : psymtable;
  325. obj : pobjectdef;
  326. hp : tnode;
  327. begin
  328. p:=comp_expr(true);
  329. do_resulttypepass(p);
  330. set_varstate(p,false);
  331. right:=nil;
  332. if (not codegenerror) and
  333. (p.resulttype.def^.deftype in [objectdef,recorddef]) then
  334. begin
  335. case p.resulttype.def^.deftype of
  336. objectdef : begin
  337. obj:=pobjectdef(p.resulttype.def);
  338. withsymtable:=new(pwithsymtable,init);
  339. withsymtable^.symsearch:=obj^.symtable^.symsearch;
  340. withsymtable^.defowner:=obj;
  341. symtab:=withsymtable;
  342. if (p.nodetype=loadn) and
  343. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  344. pwithsymtable(symtab)^.direct_with:=true;
  345. {symtab^.withnode:=p; not yet allocated !! }
  346. pwithsymtable(symtab)^.withrefnode:=p;
  347. levelcount:=1;
  348. obj:=obj^.childof;
  349. while assigned(obj) do
  350. begin
  351. symtab^.next:=new(pwithsymtable,init);
  352. symtab:=symtab^.next;
  353. symtab^.symsearch:=obj^.symtable^.symsearch;
  354. if (p.nodetype=loadn) and
  355. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  356. pwithsymtable(symtab)^.direct_with:=true;
  357. {symtab^.withnode:=p; not yet allocated !! }
  358. pwithsymtable(symtab)^.withrefnode:=p;
  359. symtab^.defowner:=obj;
  360. obj:=obj^.childof;
  361. inc(levelcount);
  362. end;
  363. symtab^.next:=symtablestack;
  364. symtablestack:=withsymtable;
  365. end;
  366. recorddef : begin
  367. symtab:=precorddef(p.resulttype.def)^.symtable;
  368. levelcount:=1;
  369. withsymtable:=new(pwithsymtable,init);
  370. withsymtable^.symsearch:=symtab^.symsearch;
  371. if (p.nodetype=loadn) and
  372. (tloadnode(p).symtable=aktprocsym^.definition^.localst) then
  373. pwithsymtable(withsymtable)^.direct_with:=true;
  374. {symtab^.withnode:=p; not yet allocated !! }
  375. pwithsymtable(withsymtable)^.withrefnode:=p;
  376. withsymtable^.defowner:=precorddef(p.resulttype.def);
  377. withsymtable^.next:=symtablestack;
  378. symtablestack:=withsymtable;
  379. end;
  380. end;
  381. if token=_COMMA then
  382. begin
  383. consume(_COMMA);
  384. right:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  385. end
  386. else
  387. begin
  388. consume(_DO);
  389. if token<>_SEMICOLON then
  390. right:=statement
  391. else
  392. right:=nil;
  393. end;
  394. for i:=1 to levelcount do
  395. symtablestack:=symtablestack^.next;
  396. _with_statement:=cwithnode.create(pwithsymtable(withsymtable),p,right,levelcount);
  397. end
  398. else
  399. begin
  400. Message(parser_e_false_with_expr);
  401. { try to recover from error }
  402. if token=_COMMA then
  403. begin
  404. consume(_COMMA);
  405. hp:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  406. if (hp=nil) then; { remove warning about unused }
  407. end
  408. else
  409. begin
  410. consume(_DO);
  411. { ignore all }
  412. if token<>_SEMICOLON then
  413. statement;
  414. end;
  415. _with_statement:=nil;
  416. end;
  417. end;
  418. function with_statement : tnode;
  419. begin
  420. consume(_WITH);
  421. with_statement:=_with_statement;
  422. end;
  423. function raise_statement : tnode;
  424. var
  425. p,pobj,paddr,pframe : tnode;
  426. begin
  427. pobj:=nil;
  428. paddr:=nil;
  429. pframe:=nil;
  430. consume(_RAISE);
  431. if not(token in [_SEMICOLON,_END]) then
  432. begin
  433. { object }
  434. pobj:=comp_expr(true);
  435. if try_to_consume(_AT) then
  436. begin
  437. paddr:=comp_expr(true);
  438. if try_to_consume(_COMMA) then
  439. pframe:=comp_expr(true);
  440. end;
  441. end
  442. else
  443. begin
  444. if (block_type<>bt_except) then
  445. Message(parser_e_no_reraise_possible);
  446. end;
  447. p:=craisenode.create(pobj,paddr,pframe);
  448. raise_statement:=p;
  449. end;
  450. function try_statement : tnode;
  451. var
  452. p_try_block,p_finally_block,first,last,
  453. p_default,p_specific,hp : tnode;
  454. ot : ttype;
  455. sym : pvarsym;
  456. old_block_type : tblock_type;
  457. exceptsymtable : psymtable;
  458. objname,objrealname : stringid;
  459. srsym : psym;
  460. srsymtable : psymtable;
  461. begin
  462. procinfo^.flags:=procinfo^.flags or pi_uses_exceptions;
  463. p_default:=nil;
  464. p_specific:=nil;
  465. { read statements to try }
  466. consume(_TRY);
  467. first:=nil;
  468. inc(statement_level);
  469. while (token<>_FINALLY) and (token<>_EXCEPT) do
  470. begin
  471. if first=nil then
  472. begin
  473. last:=cstatementnode.create(nil,statement);
  474. first:=last;
  475. end
  476. else
  477. begin
  478. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  479. last:=tstatementnode(last).left;
  480. end;
  481. if not try_to_consume(_SEMICOLON) then
  482. break;
  483. emptystats;
  484. end;
  485. p_try_block:=cblocknode.create(first);
  486. if try_to_consume(_FINALLY) then
  487. begin
  488. p_finally_block:=statements_til_end;
  489. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  490. dec(statement_level);
  491. end
  492. else
  493. begin
  494. consume(_EXCEPT);
  495. old_block_type:=block_type;
  496. block_type:=bt_except;
  497. ot:=generrortype;
  498. p_specific:=nil;
  499. if (idtoken=_ON) then
  500. { catch specific exceptions }
  501. begin
  502. repeat
  503. consume(_ID);
  504. if token=_ID then
  505. begin
  506. objname:=pattern;
  507. objrealname:=orgpattern;
  508. { can't use consume_sym here, because we need already
  509. to check for the colon }
  510. searchsym(objname,srsym,srsymtable);
  511. consume(_ID);
  512. { is a explicit name for the exception given ? }
  513. if try_to_consume(_COLON) then
  514. begin
  515. consume_sym(srsym,srsymtable);
  516. if (srsym^.typ=typesym) and
  517. is_class(ptypesym(srsym)^.restype.def) then
  518. begin
  519. ot:=ptypesym(srsym)^.restype;
  520. sym:=new(pvarsym,init(objrealname,ot));
  521. end
  522. else
  523. begin
  524. sym:=new(pvarsym,init(objrealname,generrortype));
  525. if (srsym^.typ=typesym) then
  526. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  527. else
  528. Message1(type_e_class_type_expected,ot.def^.typename);
  529. end;
  530. exceptsymtable:=new(pstoredsymtable,init(stt_exceptsymtable));
  531. exceptsymtable^.insert(sym);
  532. { insert the exception symtable stack }
  533. exceptsymtable^.next:=symtablestack;
  534. symtablestack:=exceptsymtable;
  535. end
  536. else
  537. begin
  538. { check if type is valid, must be done here because
  539. with "e: Exception" the e is not necessary }
  540. if srsym=nil then
  541. begin
  542. identifier_not_found(objrealname);
  543. srsym:=generrorsym;
  544. end;
  545. { support unit.identifier }
  546. if srsym^.typ=unitsym then
  547. begin
  548. consume(_POINT);
  549. srsym:=searchsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  550. if srsym=nil then
  551. begin
  552. identifier_not_found(orgpattern);
  553. srsym:=generrorsym;
  554. end;
  555. consume(_ID);
  556. end;
  557. { check if type is valid, must be done here because
  558. with "e: Exception" the e is not necessary }
  559. if (srsym^.typ=typesym) and
  560. is_class(ptypesym(srsym)^.restype.def) then
  561. ot:=ptypesym(srsym)^.restype
  562. else
  563. begin
  564. ot:=generrortype;
  565. if (srsym^.typ=typesym) then
  566. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  567. else
  568. Message1(type_e_class_type_expected,ot.def^.typename);
  569. end;
  570. exceptsymtable:=nil;
  571. end;
  572. end
  573. else
  574. consume(_ID);
  575. consume(_DO);
  576. hp:=connode.create(nil,statement);
  577. if ot.def^.deftype=errordef then
  578. begin
  579. hp.free;
  580. hp:=cerrornode.create;
  581. end;
  582. if p_specific=nil then
  583. begin
  584. last:=hp;
  585. p_specific:=last;
  586. end
  587. else
  588. begin
  589. tonnode(last).left:=hp;
  590. last:=tonnode(last).left;
  591. end;
  592. { set the informations }
  593. { only if the creation of the onnode was succesful, it's possible }
  594. { that last and hp are errornodes (JM) }
  595. if last.nodetype = onn then
  596. begin
  597. tonnode(last).excepttype:=pobjectdef(ot.def);
  598. tonnode(last).exceptsymtable:=exceptsymtable;
  599. end;
  600. { remove exception symtable }
  601. if assigned(exceptsymtable) then
  602. begin
  603. dellexlevel;
  604. if last.nodetype <> onn then
  605. dispose(exceptsymtable,done);
  606. end;
  607. if not try_to_consume(_SEMICOLON) then
  608. break;
  609. emptystats;
  610. until (token=_END) or (token=_ELSE);
  611. if token=_ELSE then
  612. { catch the other exceptions }
  613. begin
  614. consume(_ELSE);
  615. p_default:=statements_til_end;
  616. end
  617. else
  618. consume(_END);
  619. end
  620. else
  621. { catch all exceptions }
  622. begin
  623. p_default:=statements_til_end;
  624. end;
  625. dec(statement_level);
  626. block_type:=old_block_type;
  627. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  628. end;
  629. end;
  630. function exit_statement : tnode;
  631. var
  632. p : tnode;
  633. begin
  634. consume(_EXIT);
  635. if try_to_consume(_LKLAMMER) then
  636. begin
  637. p:=comp_expr(true);
  638. consume(_RKLAMMER);
  639. if (block_type=bt_except) then
  640. Message(parser_e_exit_with_argument_not__possible);
  641. if is_void(procinfo^.returntype.def) then
  642. Message(parser_e_void_function);
  643. end
  644. else
  645. p:=nil;
  646. p:=cexitnode.create(p);
  647. p.resulttype:=voidtype;
  648. exit_statement:=p;
  649. end;
  650. function _asm_statement : tnode;
  651. var
  652. asmstat : tasmnode;
  653. Marker : tai;
  654. begin
  655. Inside_asm_statement:=true;
  656. case aktasmmode of
  657. asmmode_none : ; { just be there to allow to a compile without
  658. any assembler readers }
  659. {$ifdef i386}
  660. {$ifndef NoRA386Att}
  661. asmmode_i386_att:
  662. asmstat:=tasmnode(ra386att.assemble);
  663. {$endif NoRA386Att}
  664. {$ifndef NoRA386Int}
  665. asmmode_i386_intel:
  666. asmstat:=tasmnode(ra386int.assemble);
  667. {$endif NoRA386Int}
  668. {$ifndef NoRA386Dir}
  669. asmmode_i386_direct:
  670. begin
  671. if not target_asm.allowdirect then
  672. Message(parser_f_direct_assembler_not_allowed);
  673. if (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  674. Begin
  675. Message1(parser_w_not_supported_for_inline,'direct asm');
  676. Message(parser_w_inlining_disabled);
  677. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  678. End;
  679. asmstat:=tasmnode(ra386dir.assemble);
  680. end;
  681. {$endif NoRA386Dir}
  682. {$endif}
  683. {$ifdef m68k}
  684. {$ifndef NoRA68kMot}
  685. asmmode_m68k_mot:
  686. asmstat:=tasmnode(ra68kmot.assemble);
  687. {$endif NoRA68kMot}
  688. {$endif}
  689. else
  690. Message(parser_f_assembler_reader_not_supported);
  691. end;
  692. { Read first the _ASM statement }
  693. consume(_ASM);
  694. {$ifndef newcg}
  695. { END is read }
  696. if try_to_consume(_LECKKLAMMER) then
  697. begin
  698. { it's possible to specify the modified registers }
  699. include(asmstat.flags,nf_object_preserved);
  700. if token<>_RECKKLAMMER then
  701. repeat
  702. { uppercase, because it's a CSTRING }
  703. uppervar(pattern);
  704. {$ifdef i386}
  705. if pattern='EAX' then
  706. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  707. else if pattern='EBX' then
  708. usedinproc:=usedinproc or ($80 shr byte(R_EBX))
  709. else if pattern='ECX' then
  710. usedinproc:=usedinproc or ($80 shr byte(R_ECX))
  711. else if pattern='EDX' then
  712. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  713. else if pattern='ESI' then
  714. begin
  715. usedinproc:=usedinproc or ($80 shr byte(R_ESI));
  716. exclude(asmstat.flags,nf_object_preserved);
  717. end
  718. else if pattern='EDI' then
  719. usedinproc:=usedinproc or ($80 shr byte(R_EDI))
  720. {$endif i386}
  721. {$ifdef m68k}
  722. if pattern='D0' then
  723. usedinproc:=usedinproc or ($800 shr word(R_D0))
  724. else if pattern='D1' then
  725. usedinproc:=usedinproc or ($800 shr word(R_D1))
  726. else if pattern='D6' then
  727. usedinproc:=usedinproc or ($800 shr word(R_D6))
  728. else if pattern='A0' then
  729. usedinproc:=usedinproc or ($800 shr word(R_A0))
  730. else if pattern='A1' then
  731. usedinproc:=usedinproc or ($800 shr word(R_A1))
  732. {$endif m68k}
  733. else consume(_RECKKLAMMER);
  734. consume(_CSTRING);
  735. if not try_to_consume(_COMMA) then
  736. break;
  737. until false;
  738. consume(_RECKKLAMMER);
  739. end
  740. else usedinproc:=$ff;
  741. {$endif newcg}
  742. { mark the start and the end of the assembler block
  743. this is needed for the optimizer }
  744. If Assigned(AsmStat.p_asm) Then
  745. Begin
  746. Marker := Tai_Marker.Create(AsmBlockStart);
  747. AsmStat.p_asm.Insert(Marker);
  748. Marker := Tai_Marker.Create(AsmBlockEnd);
  749. AsmStat.p_asm.Concat(Marker);
  750. End;
  751. Inside_asm_statement:=false;
  752. _asm_statement:=asmstat;
  753. end;
  754. function new_dispose_statement : tnode;
  755. var
  756. p,p2 : tnode;
  757. again : boolean; { dummy for do_proc_call }
  758. destructorname : stringid;
  759. sym : psym;
  760. classh : pobjectdef;
  761. destructorpos,
  762. storepos : tfileposinfo;
  763. is_new : boolean;
  764. begin
  765. if try_to_consume(_NEW) then
  766. is_new:=true
  767. else
  768. begin
  769. consume(_DISPOSE);
  770. is_new:=false;
  771. end;
  772. consume(_LKLAMMER);
  773. p:=comp_expr(true);
  774. { calc return type }
  775. cleartempgen;
  776. set_varstate(p,(not is_new));
  777. { constructor,destructor specified }
  778. if try_to_consume(_COMMA) then
  779. begin
  780. { extended syntax of new and dispose }
  781. { function styled new is handled in factor }
  782. { destructors have no parameters }
  783. destructorname:=pattern;
  784. destructorpos:=akttokenpos;
  785. consume(_ID);
  786. if (p.resulttype.def^.deftype<>pointerdef) then
  787. begin
  788. Message1(type_e_pointer_type_expected,p.resulttype.def^.typename);
  789. p.free;
  790. p:=factor(false);
  791. p.free;
  792. consume(_RKLAMMER);
  793. new_dispose_statement:=cerrornode.create;
  794. exit;
  795. end;
  796. { first parameter must be an object or class }
  797. if ppointerdef(p.resulttype.def)^.pointertype.def^.deftype<>objectdef then
  798. begin
  799. Message(parser_e_pointer_to_class_expected);
  800. p.free;
  801. new_dispose_statement:=factor(false);
  802. consume_all_until(_RKLAMMER);
  803. consume(_RKLAMMER);
  804. exit;
  805. end;
  806. { check, if the first parameter is a pointer to a _class_ }
  807. classh:=pobjectdef(ppointerdef(p.resulttype.def)^.pointertype.def);
  808. if is_class(classh) then
  809. begin
  810. Message(parser_e_no_new_or_dispose_for_classes);
  811. new_dispose_statement:=factor(false);
  812. consume_all_until(_RKLAMMER);
  813. consume(_RKLAMMER);
  814. exit;
  815. end;
  816. { search cons-/destructor, also in parent classes }
  817. storepos:=akttokenpos;
  818. akttokenpos:=destructorpos;
  819. sym:=search_class_member(classh,destructorname);
  820. akttokenpos:=storepos;
  821. { the second parameter of new/dispose must be a call }
  822. { to a cons-/destructor }
  823. if (not assigned(sym)) or (sym^.typ<>procsym) then
  824. begin
  825. if is_new then
  826. Message(parser_e_expr_have_to_be_constructor_call)
  827. else
  828. Message(parser_e_expr_have_to_be_destructor_call);
  829. p.free;
  830. new_dispose_statement:=cerrornode.create;
  831. end
  832. else
  833. begin
  834. if is_new then
  835. p2:=chnewnode.create
  836. else
  837. p2:=chdisposenode.create(p);
  838. do_resulttypepass(p2);
  839. p2.resulttype:=ppointerdef(p.resulttype.def)^.pointertype;
  840. if is_new then
  841. do_member_read(false,sym,p2,again)
  842. else
  843. begin
  844. if (m_tp in aktmodeswitches) then
  845. do_member_read(false,sym,p2,again)
  846. else
  847. begin
  848. p2:=ccallnode.create(nil,pprocsym(sym),sym^.owner,p2);
  849. { support dispose(p,done()); }
  850. if try_to_consume(_LKLAMMER) then
  851. begin
  852. if not try_to_consume(_RKLAMMER) then
  853. begin
  854. Message(parser_e_no_paras_for_destructor);
  855. consume_all_until(_RKLAMMER);
  856. consume(_RKLAMMER);
  857. end;
  858. end;
  859. end;
  860. end;
  861. { we need the real called method }
  862. cleartempgen;
  863. do_resulttypepass(p2);
  864. if not codegenerror then
  865. begin
  866. if is_new then
  867. begin
  868. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_constructor) then
  869. Message(parser_e_expr_have_to_be_constructor_call);
  870. p2:=cnewnode.create(p2);
  871. do_resulttypepass(p2);
  872. p2.resulttype:=p.resulttype;
  873. p2:=cassignmentnode.create(p,p2);
  874. end
  875. else
  876. begin
  877. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_destructor) then
  878. Message(parser_e_expr_have_to_be_destructor_call);
  879. end;
  880. end;
  881. new_dispose_statement:=p2;
  882. end;
  883. end
  884. else
  885. begin
  886. if (p.resulttype.def^.deftype<>pointerdef) then
  887. Begin
  888. Message1(type_e_pointer_type_expected,p.resulttype.def^.typename);
  889. new_dispose_statement:=cerrornode.create;
  890. end
  891. else
  892. begin
  893. if (ppointerdef(p.resulttype.def)^.pointertype.def^.deftype=objectdef) and
  894. (oo_has_vmt in pobjectdef(ppointerdef(p.resulttype.def)^.pointertype.def)^.objectoptions) then
  895. Message(parser_w_use_extended_syntax_for_objects);
  896. if (ppointerdef(p.resulttype.def)^.pointertype.def^.deftype=orddef) and
  897. (porddef(ppointerdef(p.resulttype.def)^.pointertype.def)^.typ=uvoid) then
  898. begin
  899. if (m_tp in aktmodeswitches) or
  900. (m_delphi in aktmodeswitches) then
  901. Message(parser_w_no_new_dispose_on_void_pointers)
  902. else
  903. Message(parser_e_no_new_dispose_on_void_pointers);
  904. end;
  905. if is_new then
  906. new_dispose_statement:=csimplenewdisposenode.create(simplenewn,p)
  907. else
  908. new_dispose_statement:=csimplenewdisposenode.create(simpledisposen,p);
  909. end;
  910. end;
  911. consume(_RKLAMMER);
  912. end;
  913. function statement : tnode;
  914. var
  915. p : tnode;
  916. code : tnode;
  917. filepos : tfileposinfo;
  918. srsym : psym;
  919. srsymtable : psymtable;
  920. begin
  921. filepos:=akttokenpos;
  922. case token of
  923. _GOTO :
  924. begin
  925. if not(cs_support_goto in aktmoduleswitches)then
  926. Message(sym_e_goto_and_label_not_supported);
  927. consume(_GOTO);
  928. if (token<>_INTCONST) and (token<>_ID) then
  929. begin
  930. Message(sym_e_label_not_found);
  931. code:=cerrornode.create;
  932. end
  933. else
  934. begin
  935. consume_sym(srsym,srsymtable);
  936. if srsym^.typ<>labelsym then
  937. begin
  938. Message(sym_e_id_is_no_label_id);
  939. code:=cerrornode.create;
  940. end
  941. else
  942. begin
  943. code:=cgotonode.create(plabelsym(srsym)^.lab);
  944. tgotonode(code).labsym:=plabelsym(srsym);
  945. { set flag that this label is used }
  946. plabelsym(srsym)^.used:=true;
  947. end;
  948. end;
  949. end;
  950. _BEGIN :
  951. code:=statement_block(_BEGIN);
  952. _IF :
  953. code:=if_statement;
  954. _CASE :
  955. code:=case_statement;
  956. _REPEAT :
  957. code:=repeat_statement;
  958. _WHILE :
  959. code:=while_statement;
  960. _FOR :
  961. code:=for_statement;
  962. _NEW,
  963. _DISPOSE :
  964. code:=new_dispose_statement;
  965. _WITH :
  966. code:=with_statement;
  967. _TRY :
  968. code:=try_statement;
  969. _RAISE :
  970. code:=raise_statement;
  971. { semicolons,else until and end are ignored }
  972. _SEMICOLON,
  973. _ELSE,
  974. _UNTIL,
  975. _END:
  976. code:=cnothingnode.create;
  977. _FAIL :
  978. begin
  979. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  980. Message(parser_e_fail_only_in_constructor);
  981. consume(_FAIL);
  982. code:=cfailnode.create;
  983. end;
  984. _EXIT :
  985. code:=exit_statement;
  986. _ASM :
  987. code:=_asm_statement;
  988. _EOF :
  989. Message(scan_f_end_of_file);
  990. else
  991. begin
  992. p:=expr;
  993. if p.nodetype=labeln then
  994. begin
  995. { the pointer to the following instruction }
  996. { isn't a very clean way }
  997. tlabelnode(p).left:=statement{$ifdef FPCPROCVAR}(){$endif};
  998. end;
  999. if not(p.nodetype in [calln,assignn,breakn,inlinen,continuen,labeln]) then
  1000. Message(cg_e_illegal_expression);
  1001. { specify that we don't use the value returned by the call }
  1002. { Question : can this be also improtant
  1003. for inlinen ??
  1004. it is used for :
  1005. - dispose of temp stack space
  1006. - dispose on FPU stack }
  1007. if p.nodetype=calln then
  1008. exclude(p.flags,nf_return_value_used);
  1009. code:=p;
  1010. end;
  1011. end;
  1012. if assigned(code) then
  1013. code.set_tree_filepos(filepos);
  1014. statement:=code;
  1015. end;
  1016. function statement_block(starttoken : ttoken) : tnode;
  1017. var
  1018. first,last : tnode;
  1019. filepos : tfileposinfo;
  1020. begin
  1021. first:=nil;
  1022. filepos:=akttokenpos;
  1023. consume(starttoken);
  1024. inc(statement_level);
  1025. while not(token in [_END,_FINALIZATION]) do
  1026. begin
  1027. if first=nil then
  1028. begin
  1029. last:=cstatementnode.create(nil,statement);
  1030. first:=last;
  1031. end
  1032. else
  1033. begin
  1034. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  1035. last:=tstatementnode(last).left;
  1036. end;
  1037. if (token in [_END,_FINALIZATION]) then
  1038. break
  1039. else
  1040. begin
  1041. { if no semicolon, then error and go on }
  1042. if token<>_SEMICOLON then
  1043. begin
  1044. consume(_SEMICOLON);
  1045. consume_all_until(_SEMICOLON);
  1046. end;
  1047. consume(_SEMICOLON);
  1048. end;
  1049. emptystats;
  1050. end;
  1051. { don't consume the finalization token, it is consumed when
  1052. reading the finalization block, but allow it only after
  1053. an initalization ! }
  1054. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1055. consume(_END);
  1056. dec(statement_level);
  1057. last:=cblocknode.create(first);
  1058. last.set_tree_filepos(filepos);
  1059. statement_block:=last;
  1060. end;
  1061. function assembler_block : tnode;
  1062. begin
  1063. { temporary space is set, while the BEGIN of the procedure }
  1064. if symtablestack^.symtabletype=localsymtable then
  1065. procinfo^.firsttemp_offset := -symtablestack^.datasize
  1066. else
  1067. procinfo^.firsttemp_offset := 0;
  1068. { assembler code does not allocate }
  1069. { space for the return value }
  1070. if not is_void(procinfo^.returntype.def) then
  1071. begin
  1072. if ret_in_acc(procinfo^.returntype.def) then
  1073. begin
  1074. { in assembler code the result should be directly in %eax
  1075. procinfo^.retoffset:=procinfo^.firsttemp-procinfo^.retdef^.size;
  1076. procinfo^.firsttemp:=procinfo^.retoffset; }
  1077. {$ifndef newcg}
  1078. {$ifdef i386}
  1079. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  1080. {$endif}
  1081. {$ifdef m68k}
  1082. usedinproc:=usedinproc or ($800 shr word(R_D0))
  1083. {$endif}
  1084. {$endif newcg}
  1085. end
  1086. {
  1087. else if not is_fpu(procinfo^.retdef) then
  1088. should we allow assembler functions of big elements ?
  1089. YES (FK)!!
  1090. Message(parser_e_asm_incomp_with_function_return);
  1091. }
  1092. end;
  1093. { set the framepointer to esp for assembler functions }
  1094. { but only if the are no local variables }
  1095. { added no parameter also (PM) }
  1096. { disable for methods, because self pointer is expected }
  1097. { at -8(%ebp) (JM) }
  1098. { why if se use %esp then self is still at the correct address PM }
  1099. if {not(assigned(procinfo^._class)) and}
  1100. (po_assembler in aktprocsym^.definition^.procoptions) and
  1101. (aktprocsym^.definition^.localst^.datasize=0) and
  1102. (aktprocsym^.definition^.parast^.datasize=0) and
  1103. not(ret_in_param(aktprocsym^.definition^.rettype.def)) then
  1104. begin
  1105. procinfo^.framepointer:=stack_pointer;
  1106. { set the right value for parameters }
  1107. dec(aktprocsym^.definition^.parast^.address_fixup,target_os.size_of_pointer);
  1108. dec(procinfo^.para_offset,target_os.size_of_pointer);
  1109. end;
  1110. { force the asm statement }
  1111. if token<>_ASM then
  1112. consume(_ASM);
  1113. procinfo^.Flags := procinfo^.Flags Or pi_is_assembler;
  1114. assembler_block:=_asm_statement;
  1115. { becuase the END is already read we need to get the
  1116. last_endtoken_filepos here (PFV) }
  1117. last_endtoken_filepos:=akttokenpos;
  1118. end;
  1119. end.
  1120. {
  1121. $Log$
  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. }