pstatmnt.pas 43 KB

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