pstatmnt.pas 45 KB

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