pstatmnt.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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,defutil,defcmp,
  37. paramgr,
  38. { pass 1 }
  39. pass_1,htypechk,
  40. nbas,nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  41. { parser }
  42. scanner,
  43. pbase,pexpr,
  44. { codegen }
  45. tgobj,rgobj,cgbase
  46. ,ncgutil
  47. ,radirect
  48. {$ifdef i386}
  49. {$ifndef NoRa386Int}
  50. ,ra386int
  51. {$endif NoRa386Int}
  52. {$ifndef NoRa386Att}
  53. ,ra386att
  54. {$endif NoRa386Att}
  55. {$else}
  56. ,rasm
  57. {$endif i386}
  58. ;
  59. function statement : tnode;forward;
  60. function if_statement : tnode;
  61. var
  62. ex,if_a,else_a : tnode;
  63. begin
  64. consume(_IF);
  65. ex:=comp_expr(true);
  66. consume(_THEN);
  67. if token<>_ELSE then
  68. if_a:=statement
  69. else
  70. if_a:=nil;
  71. if try_to_consume(_ELSE) then
  72. else_a:=statement
  73. else
  74. else_a:=nil;
  75. if_statement:=genloopnode(ifn,ex,if_a,else_a,false);
  76. end;
  77. { creates a block (list) of statements, til the next END token }
  78. function statements_til_end : tnode;
  79. var
  80. first,last : tstatementnode;
  81. begin
  82. first:=nil;
  83. while token<>_END do
  84. begin
  85. if first=nil then
  86. begin
  87. last:=cstatementnode.create(statement,nil);
  88. first:=last;
  89. end
  90. else
  91. begin
  92. last.right:=cstatementnode.create(statement,nil);
  93. last:=tstatementnode(last.right);
  94. end;
  95. if not try_to_consume(_SEMICOLON) then
  96. break;
  97. consume_emptystats;
  98. end;
  99. consume(_END);
  100. statements_til_end:=cblocknode.create(first);
  101. end;
  102. function case_statement : tnode;
  103. var
  104. { contains the label number of currently parsed case block }
  105. aktcaselabel : tasmlabel;
  106. firstlabel : boolean;
  107. root : pcaserecord;
  108. { the typ of the case expression }
  109. casedef : tdef;
  110. procedure newcaselabel(l,h : TConstExprInt;first:boolean);
  111. var
  112. hcaselabel : pcaserecord;
  113. procedure insertlabel(var p : pcaserecord);
  114. begin
  115. if p=nil then p:=hcaselabel
  116. else
  117. if (p^._low>hcaselabel^._low) and
  118. (p^._low>hcaselabel^._high) then
  119. if (hcaselabel^.statement = p^.statement) and
  120. (p^._low = hcaselabel^._high + 1) then
  121. begin
  122. p^._low := hcaselabel^._low;
  123. dispose(hcaselabel);
  124. end
  125. else
  126. insertlabel(p^.less)
  127. else
  128. if (p^._high<hcaselabel^._low) and
  129. (p^._high<hcaselabel^._high) then
  130. if (hcaselabel^.statement = p^.statement) and
  131. (p^._high+1 = hcaselabel^._low) then
  132. begin
  133. p^._high := hcaselabel^._high;
  134. dispose(hcaselabel);
  135. end
  136. else
  137. insertlabel(p^.greater)
  138. else Message(parser_e_double_caselabel);
  139. end;
  140. begin
  141. new(hcaselabel);
  142. hcaselabel^.less:=nil;
  143. hcaselabel^.greater:=nil;
  144. hcaselabel^.statement:=aktcaselabel;
  145. hcaselabel^.firstlabel:=first;
  146. objectlibrary.getlabel(hcaselabel^._at);
  147. hcaselabel^._low:=l;
  148. hcaselabel^._high:=h;
  149. insertlabel(root);
  150. end;
  151. var
  152. code,caseexpr,p,instruc,elseblock : tnode;
  153. hl1,hl2 : TConstExprInt;
  154. casedeferror : boolean;
  155. begin
  156. consume(_CASE);
  157. caseexpr:=comp_expr(true);
  158. { determines result type }
  159. rg.cleartempgen;
  160. do_resulttypepass(caseexpr);
  161. casedeferror:=false;
  162. casedef:=caseexpr.resulttype.def;
  163. if (not assigned(casedef)) or
  164. not(is_ordinal(casedef)) then
  165. begin
  166. CGMessage(type_e_ordinal_expr_expected);
  167. { create a correct tree }
  168. caseexpr.free;
  169. caseexpr:=cordconstnode.create(0,u32bittype,false);
  170. { set error flag so no rangechecks are done }
  171. casedeferror:=true;
  172. end;
  173. consume(_OF);
  174. inc(statement_level);
  175. root:=nil;
  176. instruc:=nil;
  177. repeat
  178. objectlibrary.getlabel(aktcaselabel);
  179. firstlabel:=true;
  180. { maybe an instruction has more case labels }
  181. repeat
  182. p:=expr;
  183. if is_widechar(casedef) then
  184. begin
  185. if (p.nodetype=rangen) then
  186. begin
  187. trangenode(p).left:=ctypeconvnode.create(trangenode(p).left,cwidechartype);
  188. trangenode(p).right:=ctypeconvnode.create(trangenode(p).right,cwidechartype);
  189. do_resulttypepass(trangenode(p).left);
  190. do_resulttypepass(trangenode(p).right);
  191. end
  192. else
  193. begin
  194. p:=ctypeconvnode.create(p,cwidechartype);
  195. do_resulttypepass(p);
  196. end;
  197. end;
  198. hl1:=0;
  199. hl2:=0;
  200. if (p.nodetype=rangen) then
  201. begin
  202. { type checking for case statements }
  203. if is_subequal(casedef, trangenode(p).left.resulttype.def) and
  204. is_subequal(casedef, trangenode(p).right.resulttype.def) then
  205. begin
  206. hl1:=get_ordinal_value(trangenode(p).left);
  207. hl2:=get_ordinal_value(trangenode(p).right);
  208. if hl1>hl2 then
  209. CGMessage(parser_e_case_lower_less_than_upper_bound);
  210. if not casedeferror then
  211. begin
  212. testrange(casedef,hl1,false);
  213. testrange(casedef,hl2,false);
  214. end;
  215. end
  216. else
  217. CGMessage(parser_e_case_mismatch);
  218. newcaselabel(hl1,hl2,firstlabel);
  219. end
  220. else
  221. begin
  222. { type checking for case statements }
  223. if not is_subequal(casedef, p.resulttype.def) then
  224. CGMessage(parser_e_case_mismatch);
  225. hl1:=get_ordinal_value(p);
  226. if not casedeferror then
  227. testrange(casedef,hl1,false);
  228. newcaselabel(hl1,hl1,firstlabel);
  229. end;
  230. p.free;
  231. if token=_COMMA then
  232. consume(_COMMA)
  233. else
  234. break;
  235. firstlabel:=false;
  236. until false;
  237. consume(_COLON);
  238. { handles instruction block }
  239. p:=clabelnode.createcase(aktcaselabel,statement);
  240. { concats instruction }
  241. instruc:=cstatementnode.create(p,instruc);
  242. if not(token in [_ELSE,_OTHERWISE,_END]) then
  243. consume(_SEMICOLON);
  244. until (token in [_ELSE,_OTHERWISE,_END]);
  245. if (token in [_ELSE,_OTHERWISE]) then
  246. begin
  247. if not try_to_consume(_ELSE) then
  248. consume(_OTHERWISE);
  249. elseblock:=statements_til_end;
  250. end
  251. else
  252. begin
  253. elseblock:=nil;
  254. consume(_END);
  255. end;
  256. dec(statement_level);
  257. code:=ccasenode.create(caseexpr,instruc,root);
  258. tcasenode(code).elseblock:=elseblock;
  259. case_statement:=code;
  260. end;
  261. function repeat_statement : tnode;
  262. var
  263. first,last,p_e : tnode;
  264. begin
  265. consume(_REPEAT);
  266. first:=nil;
  267. inc(statement_level);
  268. while token<>_UNTIL do
  269. begin
  270. if first=nil then
  271. begin
  272. last:=cstatementnode.create(statement,nil);
  273. first:=last;
  274. end
  275. else
  276. begin
  277. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  278. last:=tstatementnode(last).right;
  279. end;
  280. if not try_to_consume(_SEMICOLON) then
  281. break;
  282. consume_emptystats;
  283. end;
  284. consume(_UNTIL);
  285. dec(statement_level);
  286. first:=cblocknode.create(first);
  287. p_e:=comp_expr(true);
  288. repeat_statement:=genloopnode(whilerepeatn,p_e,first,nil,true);
  289. end;
  290. function while_statement : tnode;
  291. var
  292. p_e,p_a : tnode;
  293. begin
  294. consume(_WHILE);
  295. p_e:=comp_expr(true);
  296. consume(_DO);
  297. p_a:=statement;
  298. while_statement:=genloopnode(whilerepeatn,p_e,p_a,nil,false);
  299. end;
  300. function for_statement : tnode;
  301. var
  302. p_e,tovalue,p_a : tnode;
  303. backward : boolean;
  304. begin
  305. { parse loop header }
  306. consume(_FOR);
  307. p_e:=expr;
  308. if token=_DOWNTO then
  309. begin
  310. consume(_DOWNTO);
  311. backward:=true;
  312. end
  313. else
  314. begin
  315. consume(_TO);
  316. backward:=false;
  317. end;
  318. tovalue:=comp_expr(true);
  319. consume(_DO);
  320. { ... now the instruction }
  321. p_a:=statement;
  322. for_statement:=genloopnode(forn,p_e,tovalue,p_a,backward);
  323. end;
  324. function _with_statement : tnode;
  325. var
  326. right,p : tnode;
  327. i,levelcount : longint;
  328. withsymtable,symtab : tsymtable;
  329. obj : tobjectdef;
  330. hp : tnode;
  331. begin
  332. p:=comp_expr(true);
  333. do_resulttypepass(p);
  334. set_varstate(p,false);
  335. right:=nil;
  336. if (not codegenerror) and
  337. (p.resulttype.def.deftype in [objectdef,recorddef]) then
  338. begin
  339. case p.resulttype.def.deftype of
  340. objectdef : begin
  341. obj:=tobjectdef(p.resulttype.def);
  342. symtab:=twithsymtable.Create(obj,obj.symtable.symsearch);
  343. withsymtable:=symtab;
  344. if (p.nodetype=loadn) and
  345. (tloadnode(p).symtable=aktprocdef.localst) then
  346. twithsymtable(symtab).direct_with:=true;
  347. twithsymtable(symtab).withrefnode:=p;
  348. levelcount:=1;
  349. obj:=obj.childof;
  350. while assigned(obj) do
  351. begin
  352. symtab.next:=twithsymtable.create(obj,obj.symtable.symsearch);
  353. symtab:=symtab.next;
  354. if (p.nodetype=loadn) and
  355. (tloadnode(p).symtable=aktprocdef.localst) then
  356. twithsymtable(symtab).direct_with:=true;
  357. twithsymtable(symtab).withrefnode:=p;
  358. obj:=obj.childof;
  359. inc(levelcount);
  360. end;
  361. symtab.next:=symtablestack;
  362. symtablestack:=withsymtable;
  363. end;
  364. recorddef : begin
  365. symtab:=trecorddef(p.resulttype.def).symtable;
  366. levelcount:=1;
  367. withsymtable:=twithsymtable.create(trecorddef(p.resulttype.def),symtab.symsearch);
  368. if (p.nodetype=loadn) and
  369. (tloadnode(p).symtable=aktprocdef.localst) then
  370. twithsymtable(withsymtable).direct_with:=true;
  371. twithsymtable(withsymtable).withrefnode:=p;
  372. withsymtable.next:=symtablestack;
  373. symtablestack:=withsymtable;
  374. end;
  375. end;
  376. if token=_COMMA then
  377. begin
  378. consume(_COMMA);
  379. right:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  380. end
  381. else
  382. begin
  383. consume(_DO);
  384. if token<>_SEMICOLON then
  385. right:=statement
  386. else
  387. right:=cerrornode.create;
  388. end;
  389. for i:=1 to levelcount do
  390. symtablestack:=symtablestack.next;
  391. _with_statement:=cwithnode.create(twithsymtable(withsymtable),p,right,levelcount);
  392. end
  393. else
  394. begin
  395. Message(parser_e_false_with_expr);
  396. { try to recover from error }
  397. if token=_COMMA then
  398. begin
  399. consume(_COMMA);
  400. hp:=_with_statement{$ifdef FPCPROCVAR}(){$endif};
  401. if (hp=nil) then; { remove warning about unused }
  402. end
  403. else
  404. begin
  405. consume(_DO);
  406. { ignore all }
  407. if token<>_SEMICOLON then
  408. statement;
  409. end;
  410. _with_statement:=nil;
  411. end;
  412. end;
  413. function with_statement : tnode;
  414. begin
  415. consume(_WITH);
  416. with_statement:=_with_statement;
  417. end;
  418. function raise_statement : tnode;
  419. var
  420. p,pobj,paddr,pframe : tnode;
  421. begin
  422. pobj:=nil;
  423. paddr:=nil;
  424. pframe:=nil;
  425. consume(_RAISE);
  426. if not(token in endtokens) then
  427. begin
  428. { object }
  429. pobj:=comp_expr(true);
  430. if try_to_consume(_AT) then
  431. begin
  432. paddr:=comp_expr(true);
  433. if try_to_consume(_COMMA) then
  434. pframe:=comp_expr(true);
  435. end;
  436. end
  437. else
  438. begin
  439. if (block_type<>bt_except) then
  440. Message(parser_e_no_reraise_possible);
  441. end;
  442. p:=craisenode.create(pobj,paddr,pframe);
  443. raise_statement:=p;
  444. end;
  445. function try_statement : tnode;
  446. var
  447. p_try_block,p_finally_block,first,last,
  448. p_default,p_specific,hp : tnode;
  449. ot : ttype;
  450. sym : tvarsym;
  451. old_block_type : tblock_type;
  452. exceptsymtable : tsymtable;
  453. objname,objrealname : stringid;
  454. srsym : tsym;
  455. srsymtable : tsymtable;
  456. oldaktexceptblock: integer;
  457. begin
  458. procinfo.flags:=procinfo.flags or pi_uses_exceptions;
  459. p_default:=nil;
  460. p_specific:=nil;
  461. { read statements to try }
  462. consume(_TRY);
  463. first:=nil;
  464. inc(exceptblockcounter);
  465. oldaktexceptblock := aktexceptblock;
  466. aktexceptblock := exceptblockcounter;
  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(statement,nil);
  473. first:=last;
  474. end
  475. else
  476. begin
  477. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  478. last:=tstatementnode(last).right;
  479. end;
  480. if not try_to_consume(_SEMICOLON) then
  481. break;
  482. consume_emptystats;
  483. end;
  484. p_try_block:=cblocknode.create(first);
  485. if try_to_consume(_FINALLY) then
  486. begin
  487. inc(exceptblockcounter);
  488. aktexceptblock := exceptblockcounter;
  489. p_finally_block:=statements_til_end;
  490. try_statement:=ctryfinallynode.create(p_try_block,p_finally_block);
  491. dec(statement_level);
  492. end
  493. else
  494. begin
  495. consume(_EXCEPT);
  496. old_block_type:=block_type;
  497. block_type:=bt_except;
  498. inc(exceptblockcounter);
  499. aktexceptblock := exceptblockcounter;
  500. ot:=generrortype;
  501. p_specific:=nil;
  502. if (idtoken=_ON) then
  503. { catch specific exceptions }
  504. begin
  505. repeat
  506. consume(_ID);
  507. if token=_ID then
  508. begin
  509. objname:=pattern;
  510. objrealname:=orgpattern;
  511. { can't use consume_sym here, because we need already
  512. to check for the colon }
  513. searchsym(objname,srsym,srsymtable);
  514. consume(_ID);
  515. { is a explicit name for the exception given ? }
  516. if try_to_consume(_COLON) then
  517. begin
  518. consume_sym(srsym,srsymtable);
  519. if (srsym.typ=typesym) and
  520. is_class(ttypesym(srsym).restype.def) then
  521. begin
  522. ot:=ttypesym(srsym).restype;
  523. sym:=tvarsym.create(objrealname,ot);
  524. end
  525. else
  526. begin
  527. sym:=tvarsym.create(objrealname,generrortype);
  528. if (srsym.typ=typesym) then
  529. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  530. else
  531. Message1(type_e_class_type_expected,ot.def.typename);
  532. end;
  533. exceptsymtable:=tstt_exceptsymtable.create;
  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. identifier_not_found(objrealname);
  546. srsym:=generrorsym;
  547. end;
  548. { support unit.identifier }
  549. if srsym.typ=unitsym then
  550. begin
  551. consume(_POINT);
  552. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,pattern);
  553. if srsym=nil then
  554. begin
  555. identifier_not_found(orgpattern);
  556. srsym:=generrorsym;
  557. end;
  558. consume(_ID);
  559. end;
  560. { check if type is valid, must be done here because
  561. with "e: Exception" the e is not necessary }
  562. if (srsym.typ=typesym) and
  563. is_class(ttypesym(srsym).restype.def) then
  564. ot:=ttypesym(srsym).restype
  565. else
  566. begin
  567. ot:=generrortype;
  568. if (srsym.typ=typesym) then
  569. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename)
  570. else
  571. Message1(type_e_class_type_expected,ot.def.typename);
  572. end;
  573. exceptsymtable:=nil;
  574. end;
  575. end
  576. else
  577. consume(_ID);
  578. consume(_DO);
  579. hp:=connode.create(nil,statement);
  580. if ot.def.deftype=errordef then
  581. begin
  582. hp.free;
  583. hp:=cerrornode.create;
  584. end;
  585. if p_specific=nil then
  586. begin
  587. last:=hp;
  588. p_specific:=last;
  589. end
  590. else
  591. begin
  592. tonnode(last).left:=hp;
  593. last:=tonnode(last).left;
  594. end;
  595. { set the informations }
  596. { only if the creation of the onnode was succesful, it's possible }
  597. { that last and hp are errornodes (JM) }
  598. if last.nodetype = onn then
  599. begin
  600. tonnode(last).excepttype:=tobjectdef(ot.def);
  601. tonnode(last).exceptsymtable:=exceptsymtable;
  602. end;
  603. { remove exception symtable }
  604. if assigned(exceptsymtable) then
  605. begin
  606. dellexlevel;
  607. if last.nodetype <> onn then
  608. exceptsymtable.free;
  609. end;
  610. if not try_to_consume(_SEMICOLON) then
  611. break;
  612. consume_emptystats;
  613. until (token in [_END,_ELSE]);
  614. if try_to_consume(_ELSE) then
  615. begin
  616. { catch the other exceptions }
  617. p_default:=statements_til_end;
  618. end
  619. else
  620. consume(_END);
  621. end
  622. else
  623. begin
  624. { catch all exceptions }
  625. p_default:=statements_til_end;
  626. end;
  627. dec(statement_level);
  628. block_type:=old_block_type;
  629. try_statement:=ctryexceptnode.create(p_try_block,p_specific,p_default);
  630. end;
  631. aktexceptblock := oldaktexceptblock;
  632. end;
  633. function _asm_statement : tnode;
  634. var
  635. asmstat : tasmnode;
  636. Marker : tai;
  637. r : tregister;
  638. found : boolean;
  639. hs : string;
  640. begin
  641. Inside_asm_statement:=true;
  642. case aktasmmode of
  643. asmmode_none : ; { just be there to allow to a compile without
  644. any assembler readers }
  645. {$ifdef i386}
  646. {$ifndef NoRA386Att}
  647. asmmode_i386_att:
  648. asmstat:=tasmnode(ra386att.assemble);
  649. {$endif NoRA386Att}
  650. {$ifndef NoRA386Int}
  651. asmmode_i386_intel:
  652. asmstat:=tasmnode(ra386int.assemble);
  653. {$endif NoRA386Int}
  654. {$else not i386}
  655. asmmode_standard:
  656. asmstat:=tasmnode(rasm.assemble);
  657. {$endif i386}
  658. asmmode_direct:
  659. begin
  660. if not target_asm.allowdirect then
  661. Message(parser_f_direct_assembler_not_allowed);
  662. if (aktprocdef.proccalloption=pocall_inline) then
  663. Begin
  664. Message1(parser_w_not_supported_for_inline,'direct asm');
  665. Message(parser_w_inlining_disabled);
  666. aktprocdef.proccalloption:=pocall_fpccall;
  667. End;
  668. asmstat:=tasmnode(radirect.assemble);
  669. end;
  670. else
  671. Message(parser_f_assembler_reader_not_supported);
  672. end;
  673. { Read first the _ASM statement }
  674. consume(_ASM);
  675. { END is read }
  676. if try_to_consume(_LECKKLAMMER) then
  677. begin
  678. if token<>_RECKKLAMMER then
  679. begin
  680. repeat
  681. { it's possible to specify the modified registers }
  682. hs:=upper(pattern);
  683. found:=false;
  684. for r.enum:=firstreg to lastreg do
  685. if hs=upper(std_reg2str[r.enum]) then
  686. begin
  687. include(rg.usedinproc,r.enum);
  688. include(rg.usedbyproc,r.enum);
  689. found:=true;
  690. break;
  691. end;
  692. if not(found) then
  693. Message(asmr_e_invalid_register);
  694. consume(_CSTRING);
  695. if not try_to_consume(_COMMA) then
  696. break;
  697. until false;
  698. end;
  699. consume(_RECKKLAMMER);
  700. end
  701. else
  702. begin
  703. rg.usedbyproc := ALL_REGISTERS;
  704. rg.usedinproc := ALL_REGISTERS;
  705. end;
  706. { mark the start and the end of the assembler block
  707. this is needed for the optimizer }
  708. If Assigned(AsmStat.p_asm) Then
  709. Begin
  710. Marker := Tai_Marker.Create(AsmBlockStart);
  711. AsmStat.p_asm.Insert(Marker);
  712. Marker := Tai_Marker.Create(AsmBlockEnd);
  713. AsmStat.p_asm.Concat(Marker);
  714. End;
  715. Inside_asm_statement:=false;
  716. _asm_statement:=asmstat;
  717. end;
  718. function statement : tnode;
  719. var
  720. p : tnode;
  721. code : tnode;
  722. filepos : tfileposinfo;
  723. srsym : tsym;
  724. srsymtable : tsymtable;
  725. s : stringid;
  726. begin
  727. filepos:=akttokenpos;
  728. case token of
  729. _GOTO :
  730. begin
  731. if not(cs_support_goto in aktmoduleswitches)then
  732. Message(sym_e_goto_and_label_not_supported);
  733. consume(_GOTO);
  734. if (token<>_INTCONST) and (token<>_ID) then
  735. begin
  736. Message(sym_e_label_not_found);
  737. code:=cerrornode.create;
  738. end
  739. else
  740. begin
  741. if token=_ID then
  742. consume_sym(srsym,srsymtable)
  743. else
  744. begin
  745. searchsym(pattern,srsym,srsymtable);
  746. if srsym=nil then
  747. begin
  748. identifier_not_found(pattern);
  749. srsym:=generrorsym;
  750. srsymtable:=nil;
  751. end;
  752. consume(token);
  753. end;
  754. if srsym.typ<>labelsym then
  755. begin
  756. Message(sym_e_id_is_no_label_id);
  757. code:=cerrornode.create;
  758. end
  759. else
  760. begin
  761. code:=cgotonode.create(tlabelsym(srsym));
  762. tgotonode(code).labsym:=tlabelsym(srsym);
  763. { set flag that this label is used }
  764. tlabelsym(srsym).used:=true;
  765. end;
  766. end;
  767. end;
  768. _BEGIN :
  769. code:=statement_block(_BEGIN);
  770. _IF :
  771. code:=if_statement;
  772. _CASE :
  773. code:=case_statement;
  774. _REPEAT :
  775. code:=repeat_statement;
  776. _WHILE :
  777. code:=while_statement;
  778. _FOR :
  779. code:=for_statement;
  780. _WITH :
  781. code:=with_statement;
  782. _TRY :
  783. code:=try_statement;
  784. _RAISE :
  785. code:=raise_statement;
  786. { semicolons,else until and end are ignored }
  787. _SEMICOLON,
  788. _ELSE,
  789. _UNTIL,
  790. _END:
  791. code:=cnothingnode.create;
  792. _FAIL :
  793. begin
  794. if (aktprocdef.proctypeoption<>potype_constructor) then
  795. Message(parser_e_fail_only_in_constructor);
  796. consume(_FAIL);
  797. code:=cfailnode.create;
  798. end;
  799. _ASM :
  800. code:=_asm_statement;
  801. _EOF :
  802. Message(scan_f_end_of_file);
  803. else
  804. begin
  805. p:=expr;
  806. { When a colon follows a intconst then transform it into a label }
  807. if try_to_consume(_COLON) then
  808. begin
  809. s:=tostr(tordconstnode(p).value);
  810. p.free;
  811. searchsym(s,srsym,srsymtable);
  812. if assigned(srsym) then
  813. begin
  814. if tlabelsym(srsym).defined then
  815. Message(sym_e_label_already_defined);
  816. tlabelsym(srsym).defined:=true;
  817. p:=clabelnode.create(tlabelsym(srsym),nil);
  818. end
  819. else
  820. begin
  821. identifier_not_found(s);
  822. p:=cnothingnode.create;
  823. end;
  824. end;
  825. if p.nodetype=labeln then
  826. begin
  827. { the pointer to the following instruction }
  828. { isn't a very clean way }
  829. tlabelnode(p).left:=statement{$ifdef FPCPROCVAR}(){$endif};
  830. { be sure to have left also resulttypepass }
  831. resulttypepass(tlabelnode(p).left);
  832. end;
  833. { blockn support because a read/write is changed into a blocknode }
  834. { with a separate statement for each read/write operation (JM) }
  835. { the same is true for val() if the third parameter is not 32 bit }
  836. if not(p.nodetype in [nothingn,calln,assignn,breakn,inlinen,
  837. continuen,labeln,blockn,exitn]) then
  838. Message(cg_e_illegal_expression);
  839. { specify that we don't use the value returned by the call }
  840. { Question : can this be also improtant
  841. for inlinen ??
  842. it is used for :
  843. - dispose of temp stack space
  844. - dispose on FPU stack }
  845. if p.nodetype=calln then
  846. exclude(p.flags,nf_return_value_used);
  847. code:=p;
  848. end;
  849. end;
  850. if assigned(code) then
  851. code.set_tree_filepos(filepos);
  852. statement:=code;
  853. end;
  854. function statement_block(starttoken : ttoken) : tnode;
  855. var
  856. first,last : tnode;
  857. filepos : tfileposinfo;
  858. begin
  859. first:=nil;
  860. filepos:=akttokenpos;
  861. consume(starttoken);
  862. inc(statement_level);
  863. while not(token in [_END,_FINALIZATION]) do
  864. begin
  865. if first=nil then
  866. begin
  867. last:=cstatementnode.create(statement,nil);
  868. first:=last;
  869. end
  870. else
  871. begin
  872. tstatementnode(last).right:=cstatementnode.create(statement,nil);
  873. last:=tstatementnode(last).right;
  874. end;
  875. if (token in [_END,_FINALIZATION]) then
  876. break
  877. else
  878. begin
  879. { if no semicolon, then error and go on }
  880. if token<>_SEMICOLON then
  881. begin
  882. consume(_SEMICOLON);
  883. consume_all_until(_SEMICOLON);
  884. end;
  885. consume(_SEMICOLON);
  886. end;
  887. consume_emptystats;
  888. end;
  889. { don't consume the finalization token, it is consumed when
  890. reading the finalization block, but allow it only after
  891. an initalization ! }
  892. if (starttoken<>_INITIALIZATION) or (token<>_FINALIZATION) then
  893. consume(_END);
  894. dec(statement_level);
  895. last:=cblocknode.create(first);
  896. last.set_tree_filepos(filepos);
  897. statement_block:=last;
  898. end;
  899. function assembler_block : tnode;
  900. {# Optimize the assembler block by removing all references
  901. which are via the frame pointer by replacing them with
  902. references via the stack pointer.
  903. This is only available to certain cpu targets where
  904. the frame pointer saving must be done explicitly.
  905. }
  906. procedure OptimizeFramePointer(p:tasmnode);
  907. var
  908. hp : tai;
  909. parafixup,
  910. i : longint;
  911. begin
  912. { we don't need to allocate space for the locals }
  913. aktprocdef.localst.datasize:=0;
  914. procinfo.firsttemp_offset:=0;
  915. { replace framepointer with stackpointer }
  916. procinfo.framepointer.enum:=R_INTREGISTER;
  917. procinfo.framepointer.number:=NR_STACK_POINTER_REG;
  918. { set the right value for parameters }
  919. dec(aktprocdef.parast.address_fixup,pointer_size);
  920. dec(procinfo.para_offset,pointer_size);
  921. { replace all references to parameters in the instructions,
  922. the parameters can be identified by the parafixup option
  923. that is set. For normal user coded [ebp+4] this field is not
  924. set }
  925. parafixup:=aktprocdef.parast.address_fixup;
  926. hp:=tai(p.p_asm.first);
  927. while assigned(hp) do
  928. begin
  929. if hp.typ=ait_instruction then
  930. begin
  931. { fixup the references }
  932. for i:=1 to taicpu(hp).ops do
  933. begin
  934. with taicpu(hp).oper[i-1] do
  935. if typ=top_ref then
  936. begin
  937. case ref^.options of
  938. ref_parafixup :
  939. begin
  940. ref^.offsetfixup:=parafixup;
  941. ref^.base.enum:=R_INTREGISTER;
  942. ref^.base.number:=NR_STACK_POINTER_REG;
  943. end;
  944. end;
  945. end;
  946. end;
  947. end;
  948. hp:=tai(hp.next);
  949. end;
  950. end;
  951. {$ifdef CHECKFORPUSH}
  952. function UsesPush(p:tasmnode):boolean;
  953. var
  954. hp : tai;
  955. begin
  956. hp:=tai(p.p_asm.first);
  957. while assigned(hp) do
  958. begin
  959. if (hp.typ=ait_instruction) and
  960. (taicpu(hp).opcode=A_PUSH) then
  961. begin
  962. UsesPush:=true;
  963. exit;
  964. end;
  965. hp:=tai(hp.next);
  966. end;
  967. UsesPush:=false;
  968. end;
  969. {$endif CHECKFORPUSH}
  970. var
  971. p : tnode;
  972. begin
  973. { Rename the funcret so that recursive calls are possible }
  974. if not is_void(aktprocdef.rettype.def) then
  975. symtablestack.rename(aktprocdef.funcretsym.name,'$result');
  976. { force the asm statement }
  977. if token<>_ASM then
  978. consume(_ASM);
  979. procinfo.Flags := procinfo.Flags Or pi_is_assembler;
  980. p:=_asm_statement;
  981. { set the framepointer to esp for assembler functions when the
  982. following conditions are met:
  983. - if the are no local variables (except the allocated result)
  984. - if the are no parameters
  985. - no reference to the result variable (refcount<=1)
  986. - result is not stored as parameter
  987. - target processor has optional frame pointer save
  988. (vm, i386, vm only currently)
  989. }
  990. if (po_assembler in aktprocdef.procoptions) and
  991. (aktprocdef.parast.datasize=0) and
  992. (aktprocdef.localst.datasize=aktprocdef.rettype.def.size) and
  993. (aktprocdef.owner.symtabletype<>objectsymtable) and
  994. (not assigned(aktprocdef.funcretsym) or
  995. (tfuncretsym(aktprocdef.funcretsym).refcount<=1)) and
  996. not(paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption)) and
  997. (target_cpu in [cpu_i386,cpu_m68k,cpu_vm])
  998. {$ifdef CHECKFORPUSH}
  999. and not(UsesPush(tasmnode(p)))
  1000. {$endif CHECKFORPUSH}
  1001. then
  1002. OptimizeFramePointer(tasmnode(p));
  1003. { Flag the result as assigned when it is returned in a
  1004. register.
  1005. }
  1006. if assigned(aktprocdef.funcretsym) and
  1007. paramanager.ret_in_reg(aktprocdef.rettype.def,aktprocdef.proccalloption) then
  1008. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  1009. { because the END is already read we need to get the
  1010. last_endtoken_filepos here (PFV) }
  1011. last_endtoken_filepos:=akttokenpos;
  1012. assembler_block:=p;
  1013. end;
  1014. end.
  1015. {
  1016. $Log$
  1017. Revision 1.88 2003-03-28 19:16:57 peter
  1018. * generic constructor working for i386
  1019. * remove fixed self register
  1020. * esi added as address register for i386
  1021. Revision 1.87 2003/03/17 18:55:30 peter
  1022. * allow more tokens instead of only semicolon after inherited
  1023. Revision 1.86 2003/02/19 22:00:14 daniel
  1024. * Code generator converted to new register notation
  1025. - Horribily outdated todo.txt removed
  1026. Revision 1.85 2003/01/08 18:43:56 daniel
  1027. * Tregister changed into a record
  1028. Revision 1.84 2003/01/01 21:05:24 peter
  1029. * fixed assembler methods stackpointer optimization that was
  1030. broken after the previous change
  1031. Revision 1.83 2002/12/29 18:59:34 peter
  1032. * fixed parsing of declarations before asm statement
  1033. Revision 1.82 2002/12/27 18:18:56 peter
  1034. * check for else after empty raise statement
  1035. Revision 1.81 2002/11/27 02:37:14 peter
  1036. * case statement inlining added
  1037. * fixed inlining of write()
  1038. * switched statementnode left and right parts so the statements are
  1039. processed in the correct order when getcopy is used. This is
  1040. required for tempnodes
  1041. Revision 1.80 2002/11/25 17:43:22 peter
  1042. * splitted defbase in defutil,symutil,defcmp
  1043. * merged isconvertable and is_equal into compare_defs(_ext)
  1044. * made operator search faster by walking the list only once
  1045. Revision 1.79 2002/11/18 17:31:58 peter
  1046. * pass proccalloption to ret_in_xxx and push_xxx functions
  1047. Revision 1.78 2002/09/07 19:34:08 florian
  1048. + tcg.direction is used now
  1049. Revision 1.77 2002/09/07 15:25:07 peter
  1050. * old logs removed and tabs fixed
  1051. Revision 1.76 2002/09/07 12:16:03 carl
  1052. * second part bug report 1996 fix, testrange in cordconstnode
  1053. only called if option is set (also make parsing a tiny faster)
  1054. Revision 1.75 2002/09/02 18:40:52 peter
  1055. * fixed parsing of register names with lowercase
  1056. Revision 1.74 2002/09/01 14:43:12 peter
  1057. * fixed direct assembler for i386
  1058. Revision 1.73 2002/08/25 19:25:20 peter
  1059. * sym.insert_in_data removed
  1060. * symtable.insertvardata/insertconstdata added
  1061. * removed insert_in_data call from symtable.insert, it needs to be
  1062. called separatly. This allows to deref the address calculation
  1063. * procedures now calculate the parast addresses after the procedure
  1064. directives are parsed. This fixes the cdecl parast problem
  1065. * push_addr_param has an extra argument that specifies if cdecl is used
  1066. or not
  1067. Revision 1.72 2002/08/17 09:23:40 florian
  1068. * first part of procinfo rewrite
  1069. Revision 1.71 2002/08/16 14:24:58 carl
  1070. * issameref() to test if two references are the same (then emit no opcodes)
  1071. + ret_in_reg to replace ret_in_acc
  1072. (fix some register allocation bugs at the same time)
  1073. + save_std_register now has an extra parameter which is the
  1074. usedinproc registers
  1075. Revision 1.70 2002/08/11 14:32:27 peter
  1076. * renamed current_library to objectlibrary
  1077. Revision 1.69 2002/08/11 13:24:12 peter
  1078. * saving of asmsymbols in ppu supported
  1079. * asmsymbollist global is removed and moved into a new class
  1080. tasmlibrarydata that will hold the info of a .a file which
  1081. corresponds with a single module. Added librarydata to tmodule
  1082. to keep the library info stored for the module. In the future the
  1083. objectfiles will also be stored to the tasmlibrarydata class
  1084. * all getlabel/newasmsymbol and friends are moved to the new class
  1085. Revision 1.68 2002/08/10 14:46:30 carl
  1086. + moved target_cpu_string to cpuinfo
  1087. * renamed asmmode enum.
  1088. * assembler reader has now less ifdef's
  1089. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1090. Revision 1.67 2002/08/09 19:11:44 carl
  1091. + reading of used registers in assembler routines is now
  1092. cpu-independent
  1093. Revision 1.66 2002/08/06 20:55:22 florian
  1094. * first part of ppc calling conventions fix
  1095. Revision 1.65 2002/07/28 20:45:22 florian
  1096. + added direct assembler reader for PowerPC
  1097. Revision 1.64 2002/07/20 11:57:56 florian
  1098. * types.pas renamed to defbase.pas because D6 contains a types
  1099. unit so this would conflicts if D6 programms are compiled
  1100. + Willamette/SSE2 instructions to assembler added
  1101. Revision 1.63 2002/07/19 11:41:36 daniel
  1102. * State tracker work
  1103. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1104. allows the state tracker to change while nodes automatically into
  1105. repeat nodes.
  1106. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1107. 'not(a>b)' is optimized into 'a<=b'.
  1108. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1109. by removing the notn and later switchting the true and falselabels. The
  1110. same is done with 'repeat until not a'.
  1111. Revision 1.62 2002/07/16 15:34:20 florian
  1112. * exit is now a syssym instead of a keyword
  1113. Revision 1.61 2002/07/11 14:41:28 florian
  1114. * start of the new generic parameter handling
  1115. Revision 1.60 2002/07/04 20:43:01 florian
  1116. * first x86-64 patches
  1117. Revision 1.59 2002/07/01 18:46:25 peter
  1118. * internal linker
  1119. * reorganized aasm layer
  1120. Revision 1.58 2002/05/18 13:34:13 peter
  1121. * readded missing revisions
  1122. Revision 1.57 2002/05/16 19:46:44 carl
  1123. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1124. + try to fix temp allocation (still in ifdef)
  1125. + generic constructor calls
  1126. + start of tassembler / tmodulebase class cleanup
  1127. }