pstatmnt.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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,cobjects,
  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_firstpass(caseexpr);
  170. casedeferror:=false;
  171. casedef:=caseexpr.resulttype;
  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:=genordinalconstnode(0,u32bitdef);
  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) and
  200. is_subequal(casedef, trangenode(p).right.resulttype) 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) 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_firstpass(p);
  330. set_varstate(p,false);
  331. right:=nil;
  332. if (not codegenerror) and
  333. (p.resulttype^.deftype in [objectdef,recorddef]) then
  334. begin
  335. case p.resulttype^.deftype of
  336. objectdef : begin
  337. obj:=pobjectdef(p.resulttype);
  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)^.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);
  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:=genwithnode(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 : pobjectdef;
  455. sym : pvarsym;
  456. old_block_type : tblock_type;
  457. exceptsymtable : psymtable;
  458. objname : stringid;
  459. begin
  460. procinfo^.flags:=procinfo^.flags or
  461. pi_uses_exceptions;
  462. p_default:=nil;
  463. p_specific:=nil;
  464. { read statements to try }
  465. consume(_TRY);
  466. first:=nil;
  467. inc(statement_level);
  468. while (token<>_FINALLY) and (token<>_EXCEPT) do
  469. begin
  470. if first=nil then
  471. begin
  472. last:=cstatementnode.create(nil,statement);
  473. first:=last;
  474. end
  475. else
  476. begin
  477. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  478. last:=tstatementnode(last).left;
  479. end;
  480. if not try_to_consume(_SEMICOLON) then
  481. break;
  482. emptystats;
  483. end;
  484. p_try_block:=cblocknode.create(first);
  485. if try_to_consume(_FINALLY) then
  486. begin
  487. p_finally_block:=statements_til_end;
  488. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  489. dec(statement_level);
  490. end
  491. else
  492. begin
  493. consume(_EXCEPT);
  494. old_block_type:=block_type;
  495. block_type:=bt_except;
  496. ot:=pobjectdef(generrordef);
  497. p_specific:=nil;
  498. if token=_ON then
  499. { catch specific exceptions }
  500. begin
  501. repeat
  502. consume(_ON);
  503. if token=_ID then
  504. begin
  505. objname:=pattern;
  506. getsym(objname,false);
  507. consume(_ID);
  508. { is a explicit name for the exception given ? }
  509. if try_to_consume(_COLON) then
  510. begin
  511. getsym(pattern,true);
  512. consume(_ID);
  513. if srsym^.typ=unitsym then
  514. begin
  515. consume(_POINT);
  516. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  517. consume(_ID);
  518. end;
  519. if (srsym^.typ=typesym) and
  520. is_class(ptypesym(srsym)^.restype.def) then
  521. begin
  522. ot:=pobjectdef(ptypesym(srsym)^.restype.def);
  523. sym:=new(pvarsym,initdef(objname,ot));
  524. end
  525. else
  526. begin
  527. sym:=new(pvarsym,initdef(objname,new(perrordef,init)));
  528. if (srsym^.typ=typesym) then
  529. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  530. else
  531. Message1(type_e_class_type_expected,ot^.typename);
  532. end;
  533. exceptsymtable:=new(pstoredsymtable,init(stt_exceptsymtable));
  534. exceptsymtable^.insert(sym);
  535. { insert the exception symtable stack }
  536. exceptsymtable^.next:=symtablestack;
  537. symtablestack:=exceptsymtable;
  538. end
  539. else
  540. begin
  541. { check if type is valid, must be done here because
  542. with "e: Exception" the e is not necessary }
  543. if srsym=nil then
  544. begin
  545. Message1(sym_e_id_not_found,objname);
  546. srsym:=generrorsym;
  547. end;
  548. { only exception type }
  549. if srsym^.typ=unitsym then
  550. begin
  551. consume(_POINT);
  552. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  553. consume(_ID);
  554. end;
  555. if (srsym^.typ=typesym) and
  556. is_class(ptypesym(srsym)^.restype.def) then
  557. ot:=pobjectdef(ptypesym(srsym)^.restype.def)
  558. else
  559. begin
  560. ot:=pobjectdef(generrordef);
  561. if (srsym^.typ=typesym) then
  562. Message1(type_e_class_type_expected,ptypesym(srsym)^.restype.def^.typename)
  563. else
  564. Message1(type_e_class_type_expected,ot^.typename);
  565. end;
  566. exceptsymtable:=nil;
  567. end;
  568. end
  569. else
  570. consume(_ID);
  571. consume(_DO);
  572. hp:=connode.create(nil,statement);
  573. if ot^.deftype=errordef then
  574. begin
  575. hp.free;
  576. hp:=cerrornode.create;
  577. end;
  578. if p_specific=nil then
  579. begin
  580. last:=hp;
  581. p_specific:=last;
  582. end
  583. else
  584. begin
  585. tonnode(last).left:=hp;
  586. last:=tonnode(last).left;
  587. end;
  588. { set the informations }
  589. { only if the creation of the onnode was succesful, it's possible }
  590. { that last and hp are errornodes (JM) }
  591. if last.nodetype = onn then
  592. begin
  593. tonnode(last).excepttype:=ot;
  594. tonnode(last).exceptsymtable:=exceptsymtable;
  595. end;
  596. { remove exception symtable }
  597. if assigned(exceptsymtable) then
  598. begin
  599. dellexlevel;
  600. if last.nodetype <> onn then
  601. dispose(exceptsymtable,done);
  602. end;
  603. if not try_to_consume(_SEMICOLON) then
  604. break;
  605. emptystats;
  606. until (token=_END) or (token=_ELSE);
  607. if token=_ELSE then
  608. { catch the other exceptions }
  609. begin
  610. consume(_ELSE);
  611. p_default:=statements_til_end;
  612. end
  613. else
  614. consume(_END);
  615. end
  616. else
  617. { catch all exceptions }
  618. begin
  619. p_default:=statements_til_end;
  620. end;
  621. dec(statement_level);
  622. block_type:=old_block_type;
  623. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  624. end;
  625. end;
  626. function exit_statement : tnode;
  627. var
  628. p : tnode;
  629. begin
  630. consume(_EXIT);
  631. if try_to_consume(_LKLAMMER) then
  632. begin
  633. p:=comp_expr(true);
  634. consume(_RKLAMMER);
  635. if (block_type=bt_except) then
  636. Message(parser_e_exit_with_argument_not__possible);
  637. if procinfo^.returntype.def=pdef(voiddef) then
  638. Message(parser_e_void_function);
  639. end
  640. else
  641. p:=nil;
  642. p:=cexitnode.create(p);
  643. // p.resulttype:=procinfo^.returntype.def;
  644. p.resulttype:=voiddef;
  645. exit_statement:=p;
  646. end;
  647. function _asm_statement : tnode;
  648. var
  649. asmstat : tasmnode;
  650. Marker : tai;
  651. begin
  652. Inside_asm_statement:=true;
  653. case aktasmmode of
  654. asmmode_none : ; { just be there to allow to a compile without
  655. any assembler readers }
  656. {$ifdef i386}
  657. {$ifndef NoRA386Att}
  658. asmmode_i386_att:
  659. asmstat:=tasmnode(ra386att.assemble);
  660. {$endif NoRA386Att}
  661. {$ifndef NoRA386Int}
  662. asmmode_i386_intel:
  663. asmstat:=tasmnode(ra386int.assemble);
  664. {$endif NoRA386Int}
  665. {$ifndef NoRA386Dir}
  666. asmmode_i386_direct:
  667. begin
  668. if not target_asm.allowdirect then
  669. Message(parser_f_direct_assembler_not_allowed);
  670. if (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  671. Begin
  672. Message1(parser_w_not_supported_for_inline,'direct asm');
  673. Message(parser_w_inlining_disabled);
  674. exclude(aktprocsym^.definition^.proccalloptions,pocall_inline);
  675. End;
  676. asmstat:=tasmnode(ra386dir.assemble);
  677. end;
  678. {$endif NoRA386Dir}
  679. {$endif}
  680. {$ifdef m68k}
  681. {$ifndef NoRA68kMot}
  682. asmmode_m68k_mot:
  683. asmstat:=tasmnode(ra68kmot.assemble);
  684. {$endif NoRA68kMot}
  685. {$endif}
  686. else
  687. Message(parser_f_assembler_reader_not_supported);
  688. end;
  689. { Read first the _ASM statement }
  690. consume(_ASM);
  691. {$ifndef newcg}
  692. { END is read }
  693. if try_to_consume(_LECKKLAMMER) then
  694. begin
  695. { it's possible to specify the modified registers }
  696. include(asmstat.flags,nf_object_preserved);
  697. if token<>_RECKKLAMMER then
  698. repeat
  699. { uppercase, because it's a CSTRING }
  700. uppervar(pattern);
  701. {$ifdef i386}
  702. if pattern='EAX' then
  703. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  704. else if pattern='EBX' then
  705. usedinproc:=usedinproc or ($80 shr byte(R_EBX))
  706. else if pattern='ECX' then
  707. usedinproc:=usedinproc or ($80 shr byte(R_ECX))
  708. else if pattern='EDX' then
  709. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  710. else if pattern='ESI' then
  711. begin
  712. usedinproc:=usedinproc or ($80 shr byte(R_ESI));
  713. exclude(asmstat.flags,nf_object_preserved);
  714. end
  715. else if pattern='EDI' then
  716. usedinproc:=usedinproc or ($80 shr byte(R_EDI))
  717. {$endif i386}
  718. {$ifdef m68k}
  719. if pattern='D0' then
  720. usedinproc:=usedinproc or ($800 shr word(R_D0))
  721. else if pattern='D1' then
  722. usedinproc:=usedinproc or ($800 shr word(R_D1))
  723. else if pattern='D6' then
  724. usedinproc:=usedinproc or ($800 shr word(R_D6))
  725. else if pattern='A0' then
  726. usedinproc:=usedinproc or ($800 shr word(R_A0))
  727. else if pattern='A1' then
  728. usedinproc:=usedinproc or ($800 shr word(R_A1))
  729. {$endif m68k}
  730. else consume(_RECKKLAMMER);
  731. consume(_CSTRING);
  732. if not try_to_consume(_COMMA) then
  733. break;
  734. until false;
  735. consume(_RECKKLAMMER);
  736. end
  737. else usedinproc:=$ff;
  738. {$endif newcg}
  739. { mark the start and the end of the assembler block
  740. this is needed for the optimizer }
  741. If Assigned(AsmStat.p_asm) Then
  742. Begin
  743. Marker := Tai_Marker.Create(AsmBlockStart);
  744. AsmStat.p_asm.Insert(Marker);
  745. Marker := Tai_Marker.Create(AsmBlockEnd);
  746. AsmStat.p_asm.Concat(Marker);
  747. End;
  748. Inside_asm_statement:=false;
  749. _asm_statement:=asmstat;
  750. end;
  751. function new_dispose_statement : tnode;
  752. var
  753. p,p2 : tnode;
  754. again : boolean; { dummy for do_proc_call }
  755. destructorname : stringid;
  756. sym : psym;
  757. classh : pobjectdef;
  758. pd,pd2 : pdef;
  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. do_firstpass(p);
  775. set_varstate(p,(not is_new));
  776. { constructor,destructor specified }
  777. if try_to_consume(_COMMA) then
  778. begin
  779. { extended syntax of new and dispose }
  780. { function styled new is handled in factor }
  781. { destructors have no parameters }
  782. destructorname:=pattern;
  783. destructorpos:=akttokenpos;
  784. consume(_ID);
  785. pd:=p.resulttype;
  786. if pd=nil then
  787. pd:=generrordef;
  788. pd2:=pd;
  789. if (pd^.deftype<>pointerdef) then
  790. begin
  791. Message1(type_e_pointer_type_expected,pd^.typename);
  792. p.free;
  793. p:=factor(false);
  794. p.free;
  795. consume(_RKLAMMER);
  796. new_dispose_statement:=cerrornode.create;
  797. exit;
  798. end;
  799. { first parameter must be an object or class }
  800. if ppointerdef(pd)^.pointertype.def^.deftype<>objectdef then
  801. begin
  802. Message(parser_e_pointer_to_class_expected);
  803. p.free;
  804. new_dispose_statement:=factor(false);
  805. consume_all_until(_RKLAMMER);
  806. consume(_RKLAMMER);
  807. exit;
  808. end;
  809. { check, if the first parameter is a pointer to a _class_ }
  810. classh:=pobjectdef(ppointerdef(pd)^.pointertype.def);
  811. if is_class(classh) then
  812. begin
  813. Message(parser_e_no_new_or_dispose_for_classes);
  814. new_dispose_statement:=factor(false);
  815. consume_all_until(_RKLAMMER);
  816. consume(_RKLAMMER);
  817. exit;
  818. end;
  819. { search cons-/destructor, also in parent classes }
  820. storepos:=akttokenpos;
  821. akttokenpos:=destructorpos;
  822. sym:=search_class_member(classh,destructorname);
  823. akttokenpos:=storepos;
  824. { the second parameter of new/dispose must be a call }
  825. { to a cons-/destructor }
  826. if (not assigned(sym)) or (sym^.typ<>procsym) then
  827. begin
  828. if is_new then
  829. Message(parser_e_expr_have_to_be_constructor_call)
  830. else
  831. Message(parser_e_expr_have_to_be_destructor_call);
  832. p.free;
  833. new_dispose_statement:=cerrornode.create;
  834. end
  835. else
  836. begin
  837. if is_new then
  838. p2:=chnewnode.create
  839. else
  840. p2:=chdisposenode.create(p);
  841. if is_new then
  842. begin
  843. { Constructors can take parameters.}
  844. p2.resulttype:=ppointerdef(pd)^.pointertype.def;
  845. do_member_read(false,sym,p2,pd,again);
  846. end
  847. else
  848. begin
  849. if (m_tp in aktmodeswitches) then
  850. begin
  851. { Constructors can take parameters.}
  852. p2.resulttype:=ppointerdef(pd)^.pointertype.def;
  853. do_member_read(false,sym,p2,pd,again);
  854. end
  855. else
  856. begin
  857. p2:=ccallnode.create(pprocsym(sym),srsymtable,p2);
  858. { support dispose(p,done()); }
  859. if try_to_consume(_LKLAMMER) then
  860. begin
  861. if not try_to_consume(_RKLAMMER) then
  862. begin
  863. Message(parser_e_no_paras_for_destructor);
  864. consume_all_until(_RKLAMMER);
  865. consume(_RKLAMMER);
  866. end;
  867. end;
  868. end;
  869. end;
  870. { we need the real called method }
  871. cleartempgen;
  872. do_firstpass(p2);
  873. if not codegenerror then
  874. begin
  875. if is_new then
  876. begin
  877. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_constructor) then
  878. Message(parser_e_expr_have_to_be_constructor_call);
  879. p2:=cassignmentnode.create(p,cnewnode.create(p2));
  880. tassignmentnode(p2).right.resulttype:=pd2;
  881. end
  882. else
  883. begin
  884. if (tcallnode(p2).procdefinition^.proctypeoption<>potype_destructor) then
  885. Message(parser_e_expr_have_to_be_destructor_call);
  886. end;
  887. end;
  888. new_dispose_statement:=p2;
  889. end;
  890. end
  891. else
  892. begin
  893. if p.resulttype=nil then
  894. p.resulttype:=generrordef;
  895. if (p.resulttype^.deftype<>pointerdef) then
  896. Begin
  897. Message1(type_e_pointer_type_expected,p.resulttype^.typename);
  898. new_dispose_statement:=cerrornode.create;
  899. end
  900. else
  901. begin
  902. if (ppointerdef(p.resulttype)^.pointertype.def^.deftype=objectdef) and
  903. (oo_has_vmt in pobjectdef(ppointerdef(p.resulttype)^.pointertype.def)^.objectoptions) then
  904. Message(parser_w_use_extended_syntax_for_objects);
  905. if (ppointerdef(p.resulttype)^.pointertype.def^.deftype=orddef) and
  906. (porddef(ppointerdef(p.resulttype)^.pointertype.def)^.typ=uvoid) then
  907. begin
  908. if (m_tp in aktmodeswitches) or
  909. (m_delphi in aktmodeswitches) then
  910. Message(parser_w_no_new_dispose_on_void_pointers)
  911. else
  912. Message(parser_e_no_new_dispose_on_void_pointers);
  913. end;
  914. if is_new then
  915. new_dispose_statement:=csimplenewdisposenode.create(simplenewn,p)
  916. else
  917. new_dispose_statement:=csimplenewdisposenode.create(simpledisposen,p);
  918. end;
  919. end;
  920. consume(_RKLAMMER);
  921. end;
  922. function statement : tnode;
  923. var
  924. p : tnode;
  925. code : tnode;
  926. filepos : tfileposinfo;
  927. sr : plabelsym;
  928. label
  929. ready;
  930. begin
  931. filepos:=akttokenpos;
  932. case token of
  933. _GOTO :
  934. begin
  935. if not(cs_support_goto in aktmoduleswitches)then
  936. Message(sym_e_goto_and_label_not_supported);
  937. consume(_GOTO);
  938. if (token<>_INTCONST) and (token<>_ID) then
  939. begin
  940. Message(sym_e_label_not_found);
  941. code:=cerrornode.create;
  942. end
  943. else
  944. begin
  945. getsym(pattern,true);
  946. consume(token);
  947. if srsym^.typ<>labelsym then
  948. begin
  949. Message(sym_e_id_is_no_label_id);
  950. code:=cerrornode.create;
  951. end
  952. else
  953. begin
  954. code:=cgotonode.create(plabelsym(srsym)^.lab);
  955. tgotonode(code).labsym:=plabelsym(srsym);
  956. { set flag that this label is used }
  957. plabelsym(srsym)^.used:=true;
  958. end;
  959. end;
  960. end;
  961. _BEGIN :
  962. code:=statement_block(_BEGIN);
  963. _IF :
  964. code:=if_statement;
  965. _CASE :
  966. code:=case_statement;
  967. _REPEAT :
  968. code:=repeat_statement;
  969. _WHILE :
  970. code:=while_statement;
  971. _FOR :
  972. code:=for_statement;
  973. _NEW,
  974. _DISPOSE :
  975. code:=new_dispose_statement;
  976. _WITH :
  977. code:=with_statement;
  978. _TRY :
  979. code:=try_statement;
  980. _RAISE :
  981. code:=raise_statement;
  982. { semicolons,else until and end are ignored }
  983. _SEMICOLON,
  984. _ELSE,
  985. _UNTIL,
  986. _END:
  987. code:=cnothingnode.create;
  988. _FAIL :
  989. begin
  990. if (aktprocsym^.definition^.proctypeoption<>potype_constructor) then
  991. Message(parser_e_fail_only_in_constructor);
  992. consume(_FAIL);
  993. code:=cfailnode.create;
  994. end;
  995. _EXIT :
  996. code:=exit_statement;
  997. _ASM :
  998. code:=_asm_statement;
  999. _EOF :
  1000. Message(scan_f_end_of_file);
  1001. else
  1002. begin
  1003. if (token in [_INTCONST,_ID]) then
  1004. begin
  1005. getsym(pattern,true);
  1006. lastsymknown:=true;
  1007. lastsrsym:=srsym;
  1008. { it is NOT necessarily the owner
  1009. it can be a withsymtable !!! }
  1010. lastsrsymtable:=srsymtable;
  1011. if assigned(srsym) and (srsym^.typ=labelsym) then
  1012. begin
  1013. consume(token);
  1014. consume(_COLON);
  1015. { we must preserve srsym to set code later }
  1016. sr:=plabelsym(srsym);
  1017. if sr^.defined then
  1018. Message(sym_e_label_already_defined);
  1019. sr^.defined:=true;
  1020. { statement modifies srsym }
  1021. lastsymknown:=false;
  1022. { the pointer to the following instruction }
  1023. { isn't a very clean way }
  1024. code:=clabelnode.create(sr^.lab,statement{$ifdef FPCPROCVAR}(){$endif});
  1025. sr^.code:=code;
  1026. { sorry, but here is a jump the easiest way }
  1027. goto ready;
  1028. end;
  1029. end;
  1030. p:=expr;
  1031. if not(p.nodetype in [calln,assignn,breakn,inlinen,continuen]) then
  1032. Message(cg_e_illegal_expression);
  1033. { specify that we don't use the value returned by the call }
  1034. { Question : can this be also improtant
  1035. for inlinen ??
  1036. it is used for :
  1037. - dispose of temp stack space
  1038. - dispose on FPU stack }
  1039. if p.nodetype=calln then
  1040. exclude(p.flags,nf_return_value_used);
  1041. code:=p;
  1042. end;
  1043. end;
  1044. ready:
  1045. if assigned(code) then
  1046. code.set_tree_filepos(filepos);
  1047. statement:=code;
  1048. end;
  1049. function statement_block(starttoken : ttoken) : tnode;
  1050. var
  1051. first,last : tnode;
  1052. filepos : tfileposinfo;
  1053. begin
  1054. first:=nil;
  1055. filepos:=akttokenpos;
  1056. consume(starttoken);
  1057. inc(statement_level);
  1058. while not(token in [_END,_FINALIZATION]) do
  1059. begin
  1060. if first=nil then
  1061. begin
  1062. last:=cstatementnode.create(nil,statement);
  1063. first:=last;
  1064. end
  1065. else
  1066. begin
  1067. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  1068. last:=tstatementnode(last).left;
  1069. end;
  1070. if (token in [_END,_FINALIZATION]) then
  1071. break
  1072. else
  1073. begin
  1074. { if no semicolon, then error and go on }
  1075. if token<>_SEMICOLON then
  1076. begin
  1077. consume(_SEMICOLON);
  1078. consume_all_until(_SEMICOLON);
  1079. end;
  1080. consume(_SEMICOLON);
  1081. end;
  1082. emptystats;
  1083. end;
  1084. { don't consume the finalization token, it is consumed when
  1085. reading the finalization block, but allow it only after
  1086. an initalization ! }
  1087. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1088. consume(_END);
  1089. dec(statement_level);
  1090. last:=cblocknode.create(first);
  1091. last.set_tree_filepos(filepos);
  1092. statement_block:=last;
  1093. end;
  1094. function assembler_block : tnode;
  1095. begin
  1096. { temporary space is set, while the BEGIN of the procedure }
  1097. if symtablestack^.symtabletype=localsymtable then
  1098. procinfo^.firsttemp_offset := -symtablestack^.datasize
  1099. else
  1100. procinfo^.firsttemp_offset := 0;
  1101. { assembler code does not allocate }
  1102. { space for the return value }
  1103. if procinfo^.returntype.def<>pdef(voiddef) then
  1104. begin
  1105. if ret_in_acc(procinfo^.returntype.def) then
  1106. begin
  1107. { in assembler code the result should be directly in %eax
  1108. procinfo^.retoffset:=procinfo^.firsttemp-procinfo^.retdef^.size;
  1109. procinfo^.firsttemp:=procinfo^.retoffset; }
  1110. {$ifndef newcg}
  1111. {$ifdef i386}
  1112. usedinproc:=usedinproc or ($80 shr byte(R_EAX))
  1113. {$endif}
  1114. {$ifdef m68k}
  1115. usedinproc:=usedinproc or ($800 shr word(R_D0))
  1116. {$endif}
  1117. {$endif newcg}
  1118. end
  1119. {
  1120. else if not is_fpu(procinfo^.retdef) then
  1121. should we allow assembler functions of big elements ?
  1122. YES (FK)!!
  1123. Message(parser_e_asm_incomp_with_function_return);
  1124. }
  1125. end;
  1126. { set the framepointer to esp for assembler functions }
  1127. { but only if the are no local variables }
  1128. { added no parameter also (PM) }
  1129. { disable for methods, because self pointer is expected }
  1130. { at -8(%ebp) (JM) }
  1131. { why if se use %esp then self is still at the correct address PM }
  1132. if {not(assigned(procinfo^._class)) and}
  1133. (po_assembler in aktprocsym^.definition^.procoptions) and
  1134. (aktprocsym^.definition^.localst^.datasize=0) and
  1135. (aktprocsym^.definition^.parast^.datasize=0) and
  1136. not(ret_in_param(aktprocsym^.definition^.rettype.def)) then
  1137. begin
  1138. procinfo^.framepointer:=stack_pointer;
  1139. { set the right value for parameters }
  1140. dec(aktprocsym^.definition^.parast^.address_fixup,target_os.size_of_pointer);
  1141. dec(procinfo^.para_offset,target_os.size_of_pointer);
  1142. end;
  1143. { force the asm statement }
  1144. if token<>_ASM then
  1145. consume(_ASM);
  1146. procinfo^.Flags := procinfo^.Flags Or pi_is_assembler;
  1147. assembler_block:=_asm_statement;
  1148. { becuase the END is already read we need to get the
  1149. last_endtoken_filepos here (PFV) }
  1150. last_endtoken_filepos:=akttokenpos;
  1151. end;
  1152. end.
  1153. {
  1154. $Log$
  1155. Revision 1.19 2000-12-25 00:07:27 peter
  1156. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1157. tlinkedlist objects)
  1158. Revision 1.18 2000/12/23 19:59:35 peter
  1159. * object to class for ow/og objects
  1160. * split objectdata from objectoutput
  1161. Revision 1.17 2000/12/16 22:45:55 jonas
  1162. * fixed case statements with int64 values
  1163. Revision 1.16 2000/11/29 00:30:37 florian
  1164. * unused units removed from uses clause
  1165. * some changes for widestrings
  1166. Revision 1.15 2000/11/27 15:47:19 jonas
  1167. * fix for web bug 1251 (example 1)
  1168. Revision 1.14 2000/11/22 22:43:34 peter
  1169. * fixed crash with exception without sysutils (merged)
  1170. Revision 1.13 2000/11/04 14:25:21 florian
  1171. + merged Attila's changes for interfaces, not tested yet
  1172. Revision 1.12 2000/10/31 22:02:50 peter
  1173. * symtable splitted, no real code changes
  1174. Revision 1.11 2000/10/14 21:52:56 peter
  1175. * fixed memory leaks
  1176. Revision 1.10 2000/10/14 10:14:52 peter
  1177. * moehrendorf oct 2000 rewrite
  1178. Revision 1.9 2000/10/01 19:48:25 peter
  1179. * lot of compile updates for cg11
  1180. Revision 1.8 2000/09/24 21:19:50 peter
  1181. * delphi compile fixes
  1182. Revision 1.7 2000/09/24 15:06:24 peter
  1183. * use defines.inc
  1184. Revision 1.6 2000/08/27 16:11:52 peter
  1185. * moved some util functions from globals,cobjects to cutils
  1186. * splitted files into finput,fmodule
  1187. Revision 1.5 2000/08/12 15:41:15 peter
  1188. * fixed bug 1096 (merged)
  1189. Revision 1.4 2000/08/12 06:46:06 florian
  1190. + case statement for int64/qword implemented
  1191. Revision 1.3 2000/07/13 12:08:27 michael
  1192. + patched to 1.1.0 with former 1.09patch from peter
  1193. Revision 1.2 2000/07/13 11:32:45 michael
  1194. + removed logs
  1195. }