pstatmnt.pas 44 KB

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