pstatmnt.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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,aasmbase,aasmtai,aasmcpu,
  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. rgobj,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 x86_64}
  57. {$ifndef NoRax86Dir}
  58. ,rax86dir
  59. {$endif NoRax86Dir}
  60. {$endif i386}
  61. {$ifdef m68k}
  62. {$ifndef NoRa68kMot}
  63. ,ra68kmot
  64. {$endif NoRa68kMot}
  65. {$endif m68k}
  66. { codegen }
  67. {$ifdef newcg}
  68. ,cgbase
  69. {$endif newcg}
  70. ;
  71. function statement : tnode;forward;
  72. function if_statement : tnode;
  73. var
  74. ex,if_a,else_a : tnode;
  75. begin
  76. consume(_IF);
  77. ex:=comp_expr(true);
  78. consume(_THEN);
  79. if token<>_ELSE then
  80. if_a:=statement
  81. else
  82. if_a:=nil;
  83. if try_to_consume(_ELSE) then
  84. else_a:=statement
  85. else
  86. else_a:=nil;
  87. if_statement:=genloopnode(ifn,ex,if_a,else_a,false);
  88. end;
  89. { creates a block (list) of statements, til the next END token }
  90. function statements_til_end : tnode;
  91. var
  92. first,last : tstatementnode;
  93. begin
  94. first:=nil;
  95. while token<>_END do
  96. begin
  97. if first=nil then
  98. begin
  99. last:=cstatementnode.create(nil,statement);
  100. first:=last;
  101. end
  102. else
  103. begin
  104. last.left:=cstatementnode.create(nil,statement);
  105. last:=tstatementnode(last.left);
  106. end;
  107. if not try_to_consume(_SEMICOLON) then
  108. break;
  109. consume_emptystats;
  110. end;
  111. consume(_END);
  112. statements_til_end:=cblocknode.create(first);
  113. end;
  114. function case_statement : tnode;
  115. var
  116. { contains the label number of currently parsed case block }
  117. aktcaselabel : tasmlabel;
  118. firstlabel : boolean;
  119. root : pcaserecord;
  120. { the typ of the case expression }
  121. casedef : tdef;
  122. procedure newcaselabel(l,h : TConstExprInt;first:boolean);
  123. var
  124. hcaselabel : pcaserecord;
  125. procedure insertlabel(var p : pcaserecord);
  126. begin
  127. if p=nil then p:=hcaselabel
  128. else
  129. if (p^._low>hcaselabel^._low) and
  130. (p^._low>hcaselabel^._high) then
  131. if (hcaselabel^.statement = p^.statement) and
  132. (p^._low = hcaselabel^._high + 1) then
  133. begin
  134. p^._low := hcaselabel^._low;
  135. dispose(hcaselabel);
  136. end
  137. else
  138. insertlabel(p^.less)
  139. else
  140. if (p^._high<hcaselabel^._low) and
  141. (p^._high<hcaselabel^._high) then
  142. if (hcaselabel^.statement = p^.statement) and
  143. (p^._high+1 = hcaselabel^._low) then
  144. begin
  145. p^._high := hcaselabel^._high;
  146. dispose(hcaselabel);
  147. end
  148. else
  149. insertlabel(p^.greater)
  150. else Message(parser_e_double_caselabel);
  151. end;
  152. begin
  153. new(hcaselabel);
  154. hcaselabel^.less:=nil;
  155. hcaselabel^.greater:=nil;
  156. hcaselabel^.statement:=aktcaselabel;
  157. hcaselabel^.firstlabel:=first;
  158. getlabel(hcaselabel^._at);
  159. hcaselabel^._low:=l;
  160. hcaselabel^._high:=h;
  161. insertlabel(root);
  162. end;
  163. var
  164. code,caseexpr,p,instruc,elseblock : tnode;
  165. hl1,hl2 : TConstExprInt;
  166. casedeferror : boolean;
  167. begin
  168. consume(_CASE);
  169. caseexpr:=comp_expr(true);
  170. { determines result type }
  171. rg.cleartempgen;
  172. do_resulttypepass(caseexpr);
  173. casedeferror:=false;
  174. casedef:=caseexpr.resulttype.def;
  175. if (not assigned(casedef)) or
  176. not(is_ordinal(casedef)) then
  177. begin
  178. CGMessage(type_e_ordinal_expr_expected);
  179. { create a correct tree }
  180. caseexpr.free;
  181. caseexpr:=cordconstnode.create(0,u32bittype);
  182. { set error flag so no rangechecks are done }
  183. casedeferror:=true;
  184. end;
  185. consume(_OF);
  186. inc(statement_level);
  187. root:=nil;
  188. instruc:=nil;
  189. repeat
  190. getlabel(aktcaselabel);
  191. firstlabel:=true;
  192. { maybe an instruction has more case labels }
  193. repeat
  194. p:=expr;
  195. if is_widechar(casedef) then
  196. begin
  197. if (p.nodetype=rangen) then
  198. begin
  199. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  200. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  201. do_resulttypepass(trangenode(p).left);
  202. do_resulttypepass(trangenode(p).right);
  203. end
  204. else
  205. begin
  206. p:=ctypeconvnode.create(p,cwidechartype);
  207. do_resulttypepass(p);
  208. end;
  209. end;
  210. hl1:=0;
  211. hl2:=0;
  212. if (p.nodetype=rangen) then
  213. begin
  214. { type checking for case statements }
  215. if is_subequal(casedef, trangenode(p).left.resulttype.def) and
  216. is_subequal(casedef, trangenode(p).right.resulttype.def) then
  217. begin
  218. hl1:=get_ordinal_value(trangenode(p).left);
  219. hl2:=get_ordinal_value(trangenode(p).right);
  220. if hl1>hl2 then
  221. CGMessage(parser_e_case_lower_less_than_upper_bound);
  222. if not casedeferror then
  223. begin
  224. testrange(casedef,hl1,false);
  225. testrange(casedef,hl2,false);
  226. end;
  227. end
  228. else
  229. CGMessage(parser_e_case_mismatch);
  230. newcaselabel(hl1,hl2,firstlabel);
  231. end
  232. else
  233. begin
  234. { type checking for case statements }
  235. if not is_subequal(casedef, p.resulttype.def) then
  236. CGMessage(parser_e_case_mismatch);
  237. hl1:=get_ordinal_value(p);
  238. if not casedeferror then
  239. testrange(casedef,hl1,false);
  240. newcaselabel(hl1,hl1,firstlabel);
  241. end;
  242. p.free;
  243. if token=_COMMA then
  244. consume(_COMMA)
  245. else
  246. break;
  247. firstlabel:=false;
  248. until false;
  249. consume(_COLON);
  250. { handles instruction block }
  251. p:=clabelnode.createcase(aktcaselabel,statement);
  252. { concats instruction }
  253. instruc:=cstatementnode.create(instruc,p);
  254. if not((token=_ELSE) or (token=_OTHERWISE) or (token=_END)) then
  255. consume(_SEMICOLON);
  256. until (token=_ELSE) or (token=_OTHERWISE) or (token=_END);
  257. if (token=_ELSE) or (token=_OTHERWISE) then
  258. begin
  259. if not try_to_consume(_ELSE) then
  260. consume(_OTHERWISE);
  261. elseblock:=statements_til_end;
  262. end
  263. else
  264. begin
  265. elseblock:=nil;
  266. consume(_END);
  267. end;
  268. dec(statement_level);
  269. code:=ccasenode.create(caseexpr,instruc,root);
  270. tcasenode(code).elseblock:=elseblock;
  271. case_statement:=code;
  272. end;
  273. function repeat_statement : tnode;
  274. var
  275. first,last,p_e : tnode;
  276. begin
  277. consume(_REPEAT);
  278. first:=nil;
  279. inc(statement_level);
  280. while token<>_UNTIL do
  281. begin
  282. if first=nil then
  283. begin
  284. last:=cstatementnode.create(nil,statement);
  285. first:=last;
  286. end
  287. else
  288. begin
  289. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  290. last:=tstatementnode(last).left;
  291. end;
  292. if not try_to_consume(_SEMICOLON) then
  293. break;
  294. consume_emptystats;
  295. end;
  296. consume(_UNTIL);
  297. dec(statement_level);
  298. first:=cblocknode.create(first);
  299. p_e:=comp_expr(true);
  300. repeat_statement:=genloopnode(repeatn,p_e,first,nil,false);
  301. end;
  302. function while_statement : tnode;
  303. var
  304. p_e,p_a : tnode;
  305. begin
  306. consume(_WHILE);
  307. p_e:=comp_expr(true);
  308. consume(_DO);
  309. p_a:=statement;
  310. while_statement:=genloopnode(whilen,p_e,p_a,nil,false);
  311. end;
  312. function for_statement : tnode;
  313. var
  314. p_e,tovalue,p_a : tnode;
  315. backward : boolean;
  316. begin
  317. { parse loop header }
  318. consume(_FOR);
  319. p_e:=expr;
  320. if token=_DOWNTO then
  321. begin
  322. consume(_DOWNTO);
  323. backward:=true;
  324. end
  325. else
  326. begin
  327. consume(_TO);
  328. backward:=false;
  329. end;
  330. tovalue:=comp_expr(true);
  331. consume(_DO);
  332. { ... now the instruction }
  333. p_a:=statement;
  334. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  335. end;
  336. function _with_statement : tnode;
  337. var
  338. right,p : tnode;
  339. i,levelcount : longint;
  340. withsymtable,symtab : tsymtable;
  341. obj : tobjectdef;
  342. hp : tnode;
  343. begin
  344. p:=comp_expr(true);
  345. do_resulttypepass(p);
  346. set_varstate(p,false);
  347. right:=nil;
  348. if (not codegenerror) and
  349. (p.resulttype.def.deftype in [objectdef,recorddef]) then
  350. begin
  351. case p.resulttype.def.deftype of
  352. objectdef : begin
  353. obj:=tobjectdef(p.resulttype.def);
  354. symtab:=twithsymtable.Create(obj,obj.symtable.symsearch);
  355. withsymtable:=symtab;
  356. if (p.nodetype=loadn) and
  357. (tloadnode(p).symtable=aktprocdef.localst) then
  358. twithsymtable(symtab).direct_with:=true;
  359. twithsymtable(symtab).withrefnode:=p;
  360. levelcount:=1;
  361. obj:=obj.childof;
  362. while assigned(obj) do
  363. begin
  364. symtab.next:=twithsymtable.create(obj,obj.symtable.symsearch);
  365. symtab:=symtab.next;
  366. if (p.nodetype=loadn) and
  367. (tloadnode(p).symtable=aktprocdef.localst) then
  368. twithsymtable(symtab).direct_with:=true;
  369. twithsymtable(symtab).withrefnode:=p;
  370. obj:=obj.childof;
  371. inc(levelcount);
  372. end;
  373. symtab.next:=symtablestack;
  374. symtablestack:=withsymtable;
  375. end;
  376. recorddef : begin
  377. symtab:=trecorddef(p.resulttype.def).symtable;
  378. levelcount:=1;
  379. withsymtable:=twithsymtable.create(trecorddef(p.resulttype.def),symtab.symsearch);
  380. if (p.nodetype=loadn) and
  381. (tloadnode(p).symtable=aktprocdef.localst) then
  382. twithsymtable(withsymtable).direct_with:=true;
  383. twithsymtable(withsymtable).withrefnode:=p;
  384. withsymtable.next:=symtablestack;
  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:=cerrornode.create;
  400. end;
  401. for i:=1 to levelcount do
  402. symtablestack:=symtablestack.next;
  403. _with_statement:=cwithnode.create(twithsymtable(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 : ttype;
  462. sym : tvarsym;
  463. old_block_type : tblock_type;
  464. exceptsymtable : tsymtable;
  465. objname,objrealname : stringid;
  466. srsym : tsym;
  467. srsymtable : tsymtable;
  468. oldaktexceptblock: integer;
  469. begin
  470. procinfo^.flags:=procinfo^.flags or pi_uses_exceptions;
  471. p_default:=nil;
  472. p_specific:=nil;
  473. { read statements to try }
  474. consume(_TRY);
  475. first:=nil;
  476. inc(exceptblockcounter);
  477. oldaktexceptblock := aktexceptblock;
  478. aktexceptblock := exceptblockcounter;
  479. inc(statement_level);
  480. while (token<>_FINALLY) and (token<>_EXCEPT) do
  481. begin
  482. if first=nil then
  483. begin
  484. last:=cstatementnode.create(nil,statement);
  485. first:=last;
  486. end
  487. else
  488. begin
  489. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  490. last:=tstatementnode(last).left;
  491. end;
  492. if not try_to_consume(_SEMICOLON) then
  493. break;
  494. consume_emptystats;
  495. end;
  496. p_try_block:=cblocknode.create(first);
  497. if try_to_consume(_FINALLY) then
  498. begin
  499. inc(exceptblockcounter);
  500. aktexceptblock := exceptblockcounter;
  501. p_finally_block:=statements_til_end;
  502. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  503. dec(statement_level);
  504. end
  505. else
  506. begin
  507. consume(_EXCEPT);
  508. old_block_type:=block_type;
  509. block_type:=bt_except;
  510. inc(exceptblockcounter);
  511. aktexceptblock := exceptblockcounter;
  512. ot:=generrortype;
  513. p_specific:=nil;
  514. if (idtoken=_ON) then
  515. { catch specific exceptions }
  516. begin
  517. repeat
  518. consume(_ID);
  519. if token=_ID then
  520. begin
  521. objname:=pattern;
  522. objrealname:=orgpattern;
  523. { can't use consume_sym here, because we need already
  524. to check for the colon }
  525. searchsym(objname,srsym,srsymtable);
  526. consume(_ID);
  527. { is a explicit name for the exception given ? }
  528. if try_to_consume(_COLON) then
  529. begin
  530. consume_sym(srsym,srsymtable);
  531. if (srsym.typ=typesym) and
  532. is_class(ttypesym(srsym).restype.def) then
  533. begin
  534. ot:=ttypesym(srsym).restype;
  535. sym:=tvarsym.create(objrealname,ot);
  536. end
  537. else
  538. begin
  539. sym:=tvarsym.create(objrealname,generrortype);
  540. if (srsym.typ=typesym) then
  541. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  542. else
  543. Message1(type_e_class_type_expected,ot.def.typename);
  544. end;
  545. exceptsymtable:=tstt_exceptsymtable.create;
  546. exceptsymtable.insert(sym);
  547. { insert the exception symtable stack }
  548. exceptsymtable.next:=symtablestack;
  549. symtablestack:=exceptsymtable;
  550. end
  551. else
  552. begin
  553. { check if type is valid, must be done here because
  554. with "e: Exception" the e is not necessary }
  555. if srsym=nil then
  556. begin
  557. identifier_not_found(objrealname);
  558. srsym:=generrorsym;
  559. end;
  560. { support unit.identifier }
  561. if srsym.typ=unitsym then
  562. begin
  563. consume(_POINT);
  564. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,pattern);
  565. if srsym=nil then
  566. begin
  567. identifier_not_found(orgpattern);
  568. srsym:=generrorsym;
  569. end;
  570. consume(_ID);
  571. end;
  572. { check if type is valid, must be done here because
  573. with "e: Exception" the e is not necessary }
  574. if (srsym.typ=typesym) and
  575. is_class(ttypesym(srsym).restype.def) then
  576. ot:=ttypesym(srsym).restype
  577. else
  578. begin
  579. ot:=generrortype;
  580. if (srsym.typ=typesym) then
  581. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  582. else
  583. Message1(type_e_class_type_expected,ot.def.typename);
  584. end;
  585. exceptsymtable:=nil;
  586. end;
  587. end
  588. else
  589. consume(_ID);
  590. consume(_DO);
  591. hp:=connode.create(nil,statement);
  592. if ot.def.deftype=errordef then
  593. begin
  594. hp.free;
  595. hp:=cerrornode.create;
  596. end;
  597. if p_specific=nil then
  598. begin
  599. last:=hp;
  600. p_specific:=last;
  601. end
  602. else
  603. begin
  604. tonnode(last).left:=hp;
  605. last:=tonnode(last).left;
  606. end;
  607. { set the informations }
  608. { only if the creation of the onnode was succesful, it's possible }
  609. { that last and hp are errornodes (JM) }
  610. if last.nodetype = onn then
  611. begin
  612. tonnode(last).excepttype:=tobjectdef(ot.def);
  613. tonnode(last).exceptsymtable:=exceptsymtable;
  614. end;
  615. { remove exception symtable }
  616. if assigned(exceptsymtable) then
  617. begin
  618. dellexlevel;
  619. if last.nodetype <> onn then
  620. exceptsymtable.free;
  621. end;
  622. if not try_to_consume(_SEMICOLON) then
  623. break;
  624. consume_emptystats;
  625. until (token=_END) or (token=_ELSE);
  626. if token=_ELSE then
  627. { catch the other exceptions }
  628. begin
  629. consume(_ELSE);
  630. p_default:=statements_til_end;
  631. end
  632. else
  633. consume(_END);
  634. end
  635. else
  636. { catch all exceptions }
  637. begin
  638. p_default:=statements_til_end;
  639. end;
  640. dec(statement_level);
  641. block_type:=old_block_type;
  642. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  643. end;
  644. aktexceptblock := oldaktexceptblock;
  645. end;
  646. function exit_statement : tnode;
  647. var
  648. p : tnode;
  649. begin
  650. consume(_EXIT);
  651. if try_to_consume(_LKLAMMER) then
  652. begin
  653. p:=comp_expr(true);
  654. consume(_RKLAMMER);
  655. if (block_type=bt_except) then
  656. Message(parser_e_exit_with_argument_not__possible);
  657. if is_void(aktprocdef.rettype.def) then
  658. Message(parser_e_void_function);
  659. end
  660. else
  661. p:=nil;
  662. p:=cexitnode.create(p);
  663. do_resulttypepass(p);
  664. exit_statement:=p;
  665. end;
  666. function _asm_statement : tnode;
  667. var
  668. asmstat : tasmnode;
  669. Marker : tai;
  670. begin
  671. Inside_asm_statement:=true;
  672. case aktasmmode of
  673. asmmode_none : ; { just be there to allow to a compile without
  674. any assembler readers }
  675. {$ifdef i386}
  676. {$ifndef NoRA386Att}
  677. asmmode_i386_att:
  678. asmstat:=tasmnode(ra386att.assemble);
  679. {$endif NoRA386Att}
  680. {$ifndef NoRA386Int}
  681. asmmode_i386_intel:
  682. asmstat:=tasmnode(ra386int.assemble);
  683. {$endif NoRA386Int}
  684. {$ifndef NoRA386Dir}
  685. asmmode_i386_direct:
  686. begin
  687. if not target_asm.allowdirect then
  688. Message(parser_f_direct_assembler_not_allowed);
  689. if (aktprocdef.proccalloption=pocall_inline) then
  690. Begin
  691. Message1(parser_w_not_supported_for_inline,'direct asm');
  692. Message(parser_w_inlining_disabled);
  693. aktprocdef.proccalloption:=pocall_fpccall;
  694. End;
  695. asmstat:=tasmnode(ra386dir.assemble);
  696. end;
  697. {$endif NoRA386Dir}
  698. {$endif}
  699. {$ifdef x86_64}
  700. {$ifndef NoRA386Dir}
  701. asmmode_i386_direct:
  702. begin
  703. if not target_asm.allowdirect then
  704. Message(parser_f_direct_assembler_not_allowed);
  705. if (aktprocdef.proccalloption=pocall_inline) then
  706. Begin
  707. Message1(parser_w_not_supported_for_inline,'direct asm');
  708. Message(parser_w_inlining_disabled);
  709. aktprocdef.proccalloption:=pocall_fpccall;
  710. End;
  711. asmstat:=tasmnode(rax86dir.assemble);
  712. end;
  713. {$endif NoRA386Dir}
  714. {$endif x86_64}
  715. {$ifdef m68k}
  716. {$ifndef NoRA68kMot}
  717. asmmode_m68k_mot:
  718. asmstat:=tasmnode(ra68kmot.assemble);
  719. {$endif NoRA68kMot}
  720. {$endif}
  721. else
  722. Message(parser_f_assembler_reader_not_supported);
  723. end;
  724. { Read first the _ASM statement }
  725. consume(_ASM);
  726. { END is read }
  727. if try_to_consume(_LECKKLAMMER) then
  728. begin
  729. { it's possible to specify the modified registers }
  730. include(asmstat.flags,nf_object_preserved);
  731. if token<>_RECKKLAMMER then
  732. repeat
  733. { uppercase, because it's a CSTRING }
  734. uppervar(pattern);
  735. {$ifdef i386}
  736. if pattern='EAX' then
  737. include(rg.usedinproc,R_EAX)
  738. else if pattern='EBX' then
  739. include(rg.usedinproc,R_EBX)
  740. else if pattern='ECX' then
  741. include(rg.usedinproc,R_ECX)
  742. else if pattern='EDX' then
  743. include(rg.usedinproc,R_EDX)
  744. else if pattern='ESI' then
  745. begin
  746. include(rg.usedinproc,R_ESI);
  747. exclude(asmstat.flags,nf_object_preserved);
  748. end
  749. else if pattern='EDI' then
  750. include(rg.usedinproc,R_EDI)
  751. {$endif i386}
  752. {$ifdef x86_64}
  753. if pattern='RAX' then
  754. include(usedinproc,R_RAX)
  755. else if pattern='RBX' then
  756. include(usedinproc,R_RBX)
  757. else if pattern='RCX' then
  758. include(usedinproc,R_RCX)
  759. else if pattern='RDX' then
  760. include(usedinproc,R_RDX)
  761. else if pattern='RSI' then
  762. begin
  763. include(usedinproc,R_RSI);
  764. exclude(asmstat.flags,nf_object_preserved);
  765. end
  766. else if pattern='RDI' then
  767. include(usedinproc,R_RDI)
  768. {$endif x86_64}
  769. {$ifdef m68k}
  770. if pattern='D0' then
  771. include(rg.usedinproc,R_D0)
  772. else if pattern='D1' then
  773. include(rg.usedinproc,R_D1)
  774. else if pattern='D2' then
  775. include(rg.usedinproc,R_D2)
  776. else if pattern='D3' then
  777. include(rg.usedinproc,R_D3)
  778. else if pattern='D4' then
  779. include(rg.usedinproc,R_D4)
  780. else if pattern='D5' then
  781. include(rg.usedinproc,R_D5)
  782. else if pattern='D6' then
  783. include(rg.usedinproc,R_D6)
  784. else if pattern='D7' then
  785. include(rg.usedinproc,R_D7)
  786. else if pattern='A0' then
  787. include(rg.usedinproc,R_A0)
  788. else if pattern='A1' then
  789. include(rg.usedinproc,R_A1)
  790. else if pattern='A2' then
  791. include(rg.usedinproc,R_A2)
  792. else if pattern='A3' then
  793. include(rg.usedinproc,R_A3)
  794. else if pattern='A4' then
  795. include(rg.usedinproc,R_A4)
  796. else if pattern='A5' then
  797. include(rg.usedinproc,R_A5)
  798. {$endif m68k}
  799. {$ifdef powerpc}
  800. if pattern<>'' then
  801. internalerror(200108251)
  802. {$endif powerpc}
  803. {$IFDEF SPARC}
  804. if pattern<>'' then
  805. internalerror(200108251)
  806. {$ENDIF SPARC}
  807. else consume(_RECKKLAMMER);
  808. consume(_CSTRING);
  809. if not try_to_consume(_COMMA) then
  810. break;
  811. until false;
  812. consume(_RECKKLAMMER);
  813. end
  814. else rg.usedinproc := ALL_REGISTERS;
  815. { mark the start and the end of the assembler block
  816. this is needed for the optimizer }
  817. If Assigned(AsmStat.p_asm) Then
  818. Begin
  819. Marker := Tai_Marker.Create(AsmBlockStart);
  820. AsmStat.p_asm.Insert(Marker);
  821. Marker := Tai_Marker.Create(AsmBlockEnd);
  822. AsmStat.p_asm.Concat(Marker);
  823. End;
  824. Inside_asm_statement:=false;
  825. _asm_statement:=asmstat;
  826. end;
  827. function statement : tnode;
  828. var
  829. p : tnode;
  830. code : tnode;
  831. filepos : tfileposinfo;
  832. srsym : tsym;
  833. srsymtable : tsymtable;
  834. s : stringid;
  835. begin
  836. filepos:=akttokenpos;
  837. case token of
  838. _GOTO :
  839. begin
  840. if not(cs_support_goto in aktmoduleswitches)then
  841. Message(sym_e_goto_and_label_not_supported);
  842. consume(_GOTO);
  843. if (token<>_INTCONST) and (token<>_ID) then
  844. begin
  845. Message(sym_e_label_not_found);
  846. code:=cerrornode.create;
  847. end
  848. else
  849. begin
  850. if token=_ID then
  851. consume_sym(srsym,srsymtable)
  852. else
  853. begin
  854. searchsym(pattern,srsym,srsymtable);
  855. if srsym=nil then
  856. begin
  857. identifier_not_found(pattern);
  858. srsym:=generrorsym;
  859. srsymtable:=nil;
  860. end;
  861. consume(token);
  862. end;
  863. if srsym.typ<>labelsym then
  864. begin
  865. Message(sym_e_id_is_no_label_id);
  866. code:=cerrornode.create;
  867. end
  868. else
  869. begin
  870. code:=cgotonode.create(tlabelsym(srsym));
  871. tgotonode(code).labsym:=tlabelsym(srsym);
  872. { set flag that this label is used }
  873. tlabelsym(srsym).used:=true;
  874. end;
  875. end;
  876. end;
  877. _BEGIN :
  878. code:=statement_block(_BEGIN);
  879. _IF :
  880. code:=if_statement;
  881. _CASE :
  882. code:=case_statement;
  883. _REPEAT :
  884. code:=repeat_statement;
  885. _WHILE :
  886. code:=while_statement;
  887. _FOR :
  888. code:=for_statement;
  889. _WITH :
  890. code:=with_statement;
  891. _TRY :
  892. code:=try_statement;
  893. _RAISE :
  894. code:=raise_statement;
  895. { semicolons,else until and end are ignored }
  896. _SEMICOLON,
  897. _ELSE,
  898. _UNTIL,
  899. _END:
  900. code:=cnothingnode.create;
  901. _FAIL :
  902. begin
  903. if (aktprocdef.proctypeoption<>potype_constructor) then
  904. Message(parser_e_fail_only_in_constructor);
  905. consume(_FAIL);
  906. code:=cfailnode.create;
  907. end;
  908. _EXIT :
  909. code:=exit_statement;
  910. _ASM :
  911. code:=_asm_statement;
  912. _EOF :
  913. Message(scan_f_end_of_file);
  914. else
  915. begin
  916. p:=expr;
  917. { When a colon follows a intconst then transform it into a label }
  918. if try_to_consume(_COLON) then
  919. begin
  920. s:=tostr(tordconstnode(p).value);
  921. p.free;
  922. searchsym(s,srsym,srsymtable);
  923. if assigned(srsym) then
  924. begin
  925. if tlabelsym(srsym).defined then
  926. Message(sym_e_label_already_defined);
  927. tlabelsym(srsym).defined:=true;
  928. p:=clabelnode.create(tlabelsym(srsym),nil);
  929. end
  930. else
  931. begin
  932. identifier_not_found(s);
  933. p:=cnothingnode.create;
  934. end;
  935. end;
  936. if p.nodetype=labeln then
  937. begin
  938. { the pointer to the following instruction }
  939. { isn't a very clean way }
  940. tlabelnode(p).left:=statement{$ifdef FPCPROCVAR}(){$endif};
  941. { be sure to have left also resulttypepass }
  942. resulttypepass(tlabelnode(p).left);
  943. end;
  944. { blockn support because a read/write is changed into a blocknode }
  945. { with a separate statement for each read/write operation (JM) }
  946. { the same is true for val() if the third parameter is not 32 bit }
  947. if not(p.nodetype in [nothingn,calln,assignn,breakn,inlinen,
  948. continuen,labeln,blockn]) then
  949. Message(cg_e_illegal_expression);
  950. { specify that we don't use the value returned by the call }
  951. { Question : can this be also improtant
  952. for inlinen ??
  953. it is used for :
  954. - dispose of temp stack space
  955. - dispose on FPU stack }
  956. if p.nodetype=calln then
  957. exclude(p.flags,nf_return_value_used);
  958. code:=p;
  959. end;
  960. end;
  961. if assigned(code) then
  962. code.set_tree_filepos(filepos);
  963. statement:=code;
  964. end;
  965. function statement_block(starttoken : ttoken) : tnode;
  966. var
  967. first,last : tnode;
  968. filepos : tfileposinfo;
  969. begin
  970. first:=nil;
  971. filepos:=akttokenpos;
  972. consume(starttoken);
  973. inc(statement_level);
  974. while not(token in [_END,_FINALIZATION]) do
  975. begin
  976. if first=nil then
  977. begin
  978. last:=cstatementnode.create(nil,statement);
  979. first:=last;
  980. end
  981. else
  982. begin
  983. tstatementnode(last).left:=cstatementnode.create(nil,statement);
  984. last:=tstatementnode(last).left;
  985. end;
  986. if (token in [_END,_FINALIZATION]) then
  987. break
  988. else
  989. begin
  990. { if no semicolon, then error and go on }
  991. if token<>_SEMICOLON then
  992. begin
  993. consume(_SEMICOLON);
  994. consume_all_until(_SEMICOLON);
  995. end;
  996. consume(_SEMICOLON);
  997. end;
  998. consume_emptystats;
  999. end;
  1000. { don't consume the finalization token, it is consumed when
  1001. reading the finalization block, but allow it only after
  1002. an initalization ! }
  1003. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  1004. consume(_END);
  1005. dec(statement_level);
  1006. last:=cblocknode.create(first);
  1007. last.set_tree_filepos(filepos);
  1008. statement_block:=last;
  1009. end;
  1010. function assembler_block : tnode;
  1011. {# Optimize the assembler block by removing all references
  1012. which are via the frame pointer by replacing them with
  1013. references via the stack pointer.
  1014. This is only available to certain cpu targets where
  1015. the frame pointer saving must be done explicitly.
  1016. }
  1017. procedure OptimizeFramePointer(p:tasmnode);
  1018. var
  1019. hp : tai;
  1020. parafixup,
  1021. i : longint;
  1022. begin
  1023. { replace framepointer with stackpointer }
  1024. procinfo^.framepointer:=STACK_POINTER_REG;
  1025. { set the right value for parameters }
  1026. dec(aktprocdef.parast.address_fixup,pointer_size);
  1027. dec(procinfo^.para_offset,pointer_size);
  1028. { replace all references to parameters in the instructions,
  1029. the parameters can be identified by the parafixup option
  1030. that is set. For normal user coded [ebp+4] this field is not
  1031. set }
  1032. parafixup:=aktprocdef.parast.address_fixup;
  1033. hp:=tai(p.p_asm.first);
  1034. while assigned(hp) do
  1035. begin
  1036. if hp.typ=ait_instruction then
  1037. begin
  1038. { fixup the references }
  1039. for i:=1 to taicpu(hp).ops do
  1040. begin
  1041. with taicpu(hp).oper[i-1] do
  1042. if typ=top_ref then
  1043. begin
  1044. case ref^.options of
  1045. ref_parafixup :
  1046. begin
  1047. ref^.offsetfixup:=parafixup;
  1048. ref^.base:=STACK_POINTER_REG;
  1049. end;
  1050. end;
  1051. end;
  1052. end;
  1053. end;
  1054. hp:=tai(hp.next);
  1055. end;
  1056. end;
  1057. {$ifdef CHECKFORPUSH}
  1058. function UsesPush(p:tasmnode):boolean;
  1059. var
  1060. hp : tai;
  1061. begin
  1062. hp:=tai(p.p_asm.first);
  1063. while assigned(hp) do
  1064. begin
  1065. if (hp.typ=ait_instruction) and
  1066. (taicpu(hp).opcode=A_PUSH) then
  1067. begin
  1068. UsesPush:=true;
  1069. exit;
  1070. end;
  1071. hp:=tai(hp.next);
  1072. end;
  1073. UsesPush:=false;
  1074. end;
  1075. {$endif CHECKFORPUSH}
  1076. var
  1077. p : tnode;
  1078. haslocals,hasparas : boolean;
  1079. begin
  1080. { retrieve info about locals and paras before a result
  1081. is inserted in the symtable }
  1082. haslocals:=(aktprocdef.localst.datasize>0);
  1083. hasparas:=(aktprocdef.parast.datasize>0);
  1084. { temporary space is set, while the BEGIN of the procedure }
  1085. if symtablestack.symtabletype=localsymtable then
  1086. procinfo^.firsttemp_offset := -symtablestack.datasize
  1087. else
  1088. procinfo^.firsttemp_offset := 0;
  1089. { assembler code does not allocate }
  1090. { space for the return value }
  1091. if not is_void(aktprocdef.rettype.def) then
  1092. begin
  1093. aktprocdef.funcretsym:=tfuncretsym.create(aktprocsym.name,aktprocdef.rettype);
  1094. { insert in local symtable }
  1095. { but with another name, so that recursive calls are possible }
  1096. symtablestack.insert(aktprocdef.funcretsym);
  1097. symtablestack.rename(aktprocdef.funcretsym.name,'$result');
  1098. { update the symtablesize back to 0 if there were no locals }
  1099. if not haslocals then
  1100. symtablestack.datasize:=0;
  1101. { set the used flag for the return }
  1102. if ret_in_acc(aktprocdef.rettype.def) then
  1103. include(rg.usedinproc,accumulator);
  1104. end;
  1105. { force the asm statement }
  1106. if token<>_ASM then
  1107. consume(_ASM);
  1108. procinfo^.Flags := procinfo^.Flags Or pi_is_assembler;
  1109. p:=_asm_statement;
  1110. { set the framepointer to esp for assembler functions when the
  1111. following conditions are met:
  1112. - if the are no local variables
  1113. - no reference to the result variable (refcount<=1)
  1114. - result is not stored as parameter
  1115. - target processor has optional frame pointer save
  1116. (vm, i386, vm only currently)
  1117. }
  1118. if (po_assembler in aktprocdef.procoptions) and
  1119. (not haslocals) and
  1120. (not hasparas) and
  1121. (aktprocdef.owner.symtabletype<>objectsymtable) and
  1122. (not assigned(aktprocdef.funcretsym) or
  1123. (tfuncretsym(aktprocdef.funcretsym).refcount<=1)) and
  1124. not(ret_in_param(aktprocdef.rettype.def)) and
  1125. (target_cpu in [cpu_i386,cpu_m68k,cpu_vm])
  1126. {$ifdef CHECKFORPUSH}
  1127. and not(UsesPush(tasmnode(p)))
  1128. {$endif CHECKFORPUSH}
  1129. then
  1130. OptimizeFramePointer(tasmnode(p));
  1131. { Flag the result as assigned when it is returned in the
  1132. accumulator or on the fpu stack }
  1133. if assigned(aktprocdef.funcretsym) and
  1134. (is_fpu(aktprocdef.rettype.def) or
  1135. ret_in_acc(aktprocdef.rettype.def)) then
  1136. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  1137. { because the END is already read we need to get the
  1138. last_endtoken_filepos here (PFV) }
  1139. last_endtoken_filepos:=akttokenpos;
  1140. assembler_block:=p;
  1141. end;
  1142. end.
  1143. {
  1144. $Log$
  1145. Revision 1.60 2002-07-04 20:43:01 florian
  1146. * first x86-64 patches
  1147. Revision 1.59 2002/07/01 18:46:25 peter
  1148. * internal linker
  1149. * reorganized aasm layer
  1150. Revision 1.58 2002/05/18 13:34:13 peter
  1151. * readded missing revisions
  1152. Revision 1.57 2002/05/16 19:46:44 carl
  1153. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1154. + try to fix temp allocation (still in ifdef)
  1155. + generic constructor calls
  1156. + start of tassembler / tmodulebase class cleanup
  1157. Revision 1.55 2002/05/06 19:56:42 carl
  1158. + added more patches from Mazen for SPARC port
  1159. Revision 1.54 2002/04/21 19:02:05 peter
  1160. * removed newn and disposen nodes, the code is now directly
  1161. inlined from pexpr
  1162. * -an option that will write the secondpass nodes to the .s file, this
  1163. requires EXTDEBUG define to actually write the info
  1164. * fixed various internal errors and crashes due recent code changes
  1165. Revision 1.53 2002/04/20 21:32:24 carl
  1166. + generic FPC_CHECKPOINTER
  1167. + first parameter offset in stack now portable
  1168. * rename some constants
  1169. + move some cpu stuff to other units
  1170. - remove unused constents
  1171. * fix stacksize for some targets
  1172. * fix generic size problems which depend now on EXTEND_SIZE constant
  1173. Revision 1.52 2002/04/16 16:11:17 peter
  1174. * using inherited; without a parent having the same function
  1175. will do nothing like delphi
  1176. Revision 1.51 2002/04/15 19:01:28 carl
  1177. + target_info.size_of_pointer -> pointer_Size
  1178. Revision 1.50 2002/04/14 16:53:54 carl
  1179. + asm statement uses ALL_REGISTERS
  1180. Revision 1.49 2002/03/31 20:26:36 jonas
  1181. + a_loadfpu_* and a_loadmm_* methods in tcg
  1182. * register allocation is now handled by a class and is mostly processor
  1183. independent (+rgobj.pas and i386/rgcpu.pas)
  1184. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1185. * some small improvements and fixes to the optimizer
  1186. * some register allocation fixes
  1187. * some fpuvaroffset fixes in the unary minus node
  1188. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1189. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1190. also better optimizable)
  1191. * fixed and optimized register saving/restoring for new/dispose nodes
  1192. * LOC_FPU locations now also require their "register" field to be set to
  1193. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1194. - list field removed of the tnode class because it's not used currently
  1195. and can cause hard-to-find bugs
  1196. Revision 1.48 2002/03/11 19:10:28 peter
  1197. * Regenerated with updated fpcmake
  1198. Revision 1.47 2002/03/04 17:54:59 peter
  1199. * allow oridinal labels again
  1200. Revision 1.46 2002/01/29 21:32:03 peter
  1201. * allow accessing locals in other lexlevel when the current assembler
  1202. routine doesn't have locals.
  1203. Revision 1.45 2002/01/24 18:25:49 peter
  1204. * implicit result variable generation for assembler routines
  1205. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1206. }