racpugas.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Mazen NEIFER
  4. Does the parsing for the i386 GNU AS styled inline assembler.
  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 racpugas;
  19. {$i fpcdefs.inc}
  20. Interface
  21. uses
  22. raatt,racpu;
  23. type
  24. tSparcReader = class(tattreader)
  25. function is_asmopcode(const s: string):boolean;override;
  26. procedure handleopcode;override;
  27. procedure BuildReference(oper : tSparcoperand);
  28. procedure BuildOperand(oper : tSparcoperand);
  29. procedure BuildOpCode(instr : tSparcinstruction);
  30. procedure ReadPercent(oper : tSparcoperand);
  31. procedure ReadSym(oper : tSparcoperand);
  32. procedure ConvertCalljmp(instr : tSparcinstruction);
  33. procedure handlepercent;override;
  34. end;
  35. Implementation
  36. uses
  37. { helpers }
  38. cutils,
  39. { global }
  40. globtype,globals,verbose,
  41. systems,
  42. { aasm }
  43. cpubase,cpuinfo,aasmbase,aasmtai,aasmcpu,
  44. { symtable }
  45. symconst,symbase,symtype,symsym,symtable,
  46. { parser }
  47. scanner,
  48. procinfo,
  49. itcpugas,
  50. rabase,rautils,
  51. cgbase,cgobj
  52. ;
  53. procedure tSparcReader.ReadSym(oper : tSparcoperand);
  54. var
  55. tempstr : string;
  56. typesize,l,k : longint;
  57. begin
  58. tempstr:=actasmpattern;
  59. Consume(AS_ID);
  60. { typecasting? }
  61. if (actasmtoken=AS_LPAREN) and
  62. SearchType(tempstr,typesize) then
  63. begin
  64. oper.hastype:=true;
  65. Consume(AS_LPAREN);
  66. BuildOperand(oper);
  67. Consume(AS_RPAREN);
  68. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  69. oper.SetSize(typesize,true);
  70. end
  71. else
  72. if not oper.SetupVar(tempstr,false) then
  73. Message1(sym_e_unknown_id,tempstr);
  74. { record.field ? }
  75. if actasmtoken=AS_DOT then
  76. begin
  77. BuildRecordOffsetSize(tempstr,l,k);
  78. inc(oper.opr.ref.offset,l);
  79. end;
  80. end;
  81. procedure tSparcReader.ReadPercent(oper : tSparcoperand);
  82. begin
  83. { check for ...@ }
  84. if actasmtoken=AS_AT then
  85. begin
  86. if (oper.opr.ref.symbol=nil) and
  87. (oper.opr.ref.offset = 0) then
  88. Message(asmr_e_invalid_reference_syntax);
  89. Consume(AS_AT);
  90. if actasmtoken=AS_ID then
  91. begin
  92. if upper(actasmpattern)='LO' then
  93. oper.opr.ref.symaddr:=refs_lo
  94. else if upper(actasmpattern)='HI' then
  95. oper.opr.ref.symaddr:=refs_hi
  96. else
  97. Message(asmr_e_invalid_reference_syntax);
  98. Consume(AS_ID);
  99. end
  100. else
  101. Message(asmr_e_invalid_reference_syntax);
  102. end;
  103. end;
  104. Procedure tSparcReader.BuildReference(oper : tSparcoperand);
  105. procedure Consume_RParen;
  106. begin
  107. if actasmtoken <> AS_RPAREN then
  108. Begin
  109. Message(asmr_e_invalid_reference_syntax);
  110. RecoverConsume(true);
  111. end
  112. else
  113. begin
  114. Consume(AS_RPAREN);
  115. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  116. Begin
  117. Message(asmr_e_invalid_reference_syntax);
  118. RecoverConsume(true);
  119. end;
  120. end;
  121. end;
  122. var
  123. l : longint;
  124. begin
  125. Consume(AS_LPAREN);
  126. Case actasmtoken of
  127. AS_INTNUM,
  128. AS_MINUS,
  129. AS_PLUS:
  130. Begin
  131. { offset(offset) is invalid }
  132. If oper.opr.Ref.Offset <> 0 Then
  133. Begin
  134. Message(asmr_e_invalid_reference_syntax);
  135. RecoverConsume(true);
  136. End
  137. Else
  138. Begin
  139. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  140. Consume(AS_RPAREN);
  141. if actasmtoken=AS_MOD then
  142. ReadPercent(oper);
  143. end;
  144. exit;
  145. End;
  146. AS_REGISTER: { (reg ... }
  147. Begin
  148. if ((oper.opr.typ=OPR_REFERENCE) and (oper.opr.ref.base<>NR_NO)) or
  149. ((oper.opr.typ=OPR_LOCAL) and (oper.opr.localsym.localloc.loc<>LOC_REGISTER)) then
  150. message(asmr_e_cannot_index_relative_var);
  151. oper.opr.ref.base:=actasmregister;
  152. Consume(AS_REGISTER);
  153. { can either be a register or a right parenthesis }
  154. { (reg) }
  155. if actasmtoken=AS_RPAREN then
  156. Begin
  157. Consume_RParen;
  158. exit;
  159. end;
  160. { (reg,reg .. }
  161. Consume(AS_COMMA);
  162. if (actasmtoken=AS_REGISTER) and
  163. (oper.opr.Ref.Offset = 0) then
  164. Begin
  165. oper.opr.ref.index:=actasmregister;
  166. Consume(AS_REGISTER);
  167. Consume_RParen;
  168. end
  169. else
  170. Begin
  171. Message(asmr_e_invalid_reference_syntax);
  172. RecoverConsume(false);
  173. end;
  174. end; {end case }
  175. AS_ID:
  176. Begin
  177. ReadSym(oper);
  178. { add a constant expression? }
  179. if (actasmtoken=AS_PLUS) then
  180. begin
  181. l:=BuildConstExpression(true,true);
  182. case oper.opr.typ of
  183. OPR_CONSTANT :
  184. inc(oper.opr.val,l);
  185. OPR_LOCAL :
  186. inc(oper.opr.localsymofs,l);
  187. OPR_REFERENCE :
  188. inc(oper.opr.ref.offset,l);
  189. else
  190. internalerror(200309202);
  191. end;
  192. end;
  193. Consume(AS_RPAREN);
  194. if actasmtoken=AS_MOD then
  195. ReadPercent(oper);
  196. End;
  197. AS_COMMA: { (, ... can either be scaling, or index }
  198. Begin
  199. Consume(AS_COMMA);
  200. { Index }
  201. if (actasmtoken=AS_REGISTER) then
  202. Begin
  203. oper.opr.ref.index:=actasmregister;
  204. Consume(AS_REGISTER);
  205. { check for scaling ... }
  206. Consume_RParen;
  207. end
  208. else
  209. begin
  210. Message(asmr_e_invalid_reference_syntax);
  211. RecoverConsume(false);
  212. end;
  213. end;
  214. else
  215. Begin
  216. Message(asmr_e_invalid_reference_syntax);
  217. RecoverConsume(false);
  218. end;
  219. end;
  220. end;
  221. procedure TSparcReader.handlepercent;
  222. var
  223. len : longint;
  224. begin
  225. len:=1;
  226. actasmpattern[len]:='%';
  227. c:=current_scanner.asmgetchar;
  228. { to be a register there must be a letter and not a number }
  229. while c in ['a'..'z','A'..'Z','0'..'9'] do
  230. Begin
  231. inc(len);
  232. actasmpattern[len]:=c;
  233. c:=current_scanner.asmgetchar;
  234. end;
  235. actasmpattern[0]:=chr(len);
  236. uppervar(actasmpattern);
  237. if is_register(actasmpattern) then
  238. exit;
  239. if(actasmpattern='%HI')or(actasmpattern='%LO')then
  240. actasmtoken:=AS_MOD
  241. else
  242. Message(asmr_e_invalid_register);
  243. end;
  244. Procedure tSparcReader.BuildOperand(oper : tSparcoperand);
  245. var
  246. expr : string;
  247. typesize,l : longint;
  248. procedure AddLabelOperand(hl:tasmlabel);
  249. begin
  250. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  251. is_calljmp(actopcode) then
  252. begin
  253. oper.opr.typ:=OPR_SYMBOL;
  254. oper.opr.symbol:=hl;
  255. end
  256. else
  257. begin
  258. oper.InitRef;
  259. oper.opr.ref.symbol:=hl;
  260. end;
  261. end;
  262. procedure MaybeRecordOffset;
  263. var
  264. hasdot : boolean;
  265. l,
  266. toffset,
  267. tsize : longint;
  268. begin
  269. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  270. exit;
  271. l:=0;
  272. hasdot:=(actasmtoken=AS_DOT);
  273. if hasdot then
  274. begin
  275. if expr<>'' then
  276. begin
  277. BuildRecordOffsetSize(expr,toffset,tsize);
  278. inc(l,toffset);
  279. oper.SetSize(tsize,true);
  280. end;
  281. end;
  282. if actasmtoken in [AS_PLUS,AS_MINUS] then
  283. inc(l,BuildConstExpression(true,false));
  284. case oper.opr.typ of
  285. OPR_LOCAL :
  286. begin
  287. { don't allow direct access to fields of parameters, because that
  288. will generate buggy code. Allow it only for explicit typecasting }
  289. if hasdot and
  290. (not oper.hastype) and
  291. (tvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  292. (current_procinfo.procdef.proccalloption<>pocall_register) then
  293. Message(asmr_e_cannot_access_field_directly_for_parameters);
  294. inc(oper.opr.localsymofs,l)
  295. end;
  296. OPR_CONSTANT :
  297. inc(oper.opr.val,l);
  298. OPR_REFERENCE :
  299. inc(oper.opr.ref.offset,l);
  300. else
  301. internalerror(200309221);
  302. end;
  303. end;
  304. function MaybeBuildReference:boolean;
  305. { Try to create a reference, if not a reference is found then false
  306. is returned }
  307. begin
  308. MaybeBuildReference:=true;
  309. case actasmtoken of
  310. AS_INTNUM,
  311. AS_MINUS,
  312. AS_PLUS:
  313. Begin
  314. oper.opr.ref.offset:=BuildConstExpression(True,False);
  315. if actasmtoken<>AS_LPAREN then
  316. Message(asmr_e_invalid_reference_syntax)
  317. else
  318. BuildReference(oper);
  319. end;
  320. AS_LPAREN:
  321. BuildReference(oper);
  322. AS_ID: { only a variable is allowed ... }
  323. Begin
  324. ReadSym(oper);
  325. case actasmtoken of
  326. AS_END,
  327. AS_SEPARATOR,
  328. AS_COMMA: ;
  329. AS_LPAREN:
  330. BuildReference(oper);
  331. else
  332. Begin
  333. Message(asmr_e_invalid_reference_syntax);
  334. Consume(actasmtoken);
  335. end;
  336. end; {end case }
  337. end;
  338. else
  339. MaybeBuildReference:=false;
  340. end; { end case }
  341. end;
  342. var
  343. tempreg : tregister;
  344. hl : tasmlabel;
  345. ofs : longint;
  346. Begin
  347. expr:='';
  348. case actasmtoken of
  349. AS_LPAREN: { Memory reference or constant expression }
  350. Begin
  351. oper.InitRef;
  352. BuildReference(oper);
  353. end;
  354. AS_INTNUM,
  355. AS_MINUS,
  356. AS_PLUS,
  357. AS_MOD:
  358. Begin
  359. { Constant memory offset }
  360. { This must absolutely be followed by ( }
  361. oper.InitRef;
  362. oper.opr.ref.offset:=BuildConstExpression(True,False);
  363. if actasmtoken<>AS_LPAREN then
  364. begin
  365. ofs:=oper.opr.ref.offset;
  366. BuildConstantOperand(oper);
  367. inc(oper.opr.val,ofs);
  368. end
  369. else
  370. BuildReference(oper);
  371. end;
  372. AS_ID: { A constant expression, or a Variable ref. }
  373. Begin
  374. { Local Label ? }
  375. if is_locallabel(actasmpattern) then
  376. begin
  377. CreateLocalLabel(actasmpattern,hl,false);
  378. Consume(AS_ID);
  379. AddLabelOperand(hl);
  380. end
  381. else
  382. { Check for label }
  383. if SearchLabel(actasmpattern,hl,false) then
  384. begin
  385. Consume(AS_ID);
  386. AddLabelOperand(hl);
  387. end
  388. else
  389. { probably a variable or normal expression }
  390. { or a procedure (such as in CALL ID) }
  391. Begin
  392. { is it a constant ? }
  393. if SearchIConstant(actasmpattern,l) then
  394. Begin
  395. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  396. Message(asmr_e_invalid_operand_type);
  397. BuildConstantOperand(oper);
  398. end
  399. else
  400. begin
  401. expr:=actasmpattern;
  402. Consume(AS_ID);
  403. { typecasting? }
  404. if (actasmtoken=AS_LPAREN) and
  405. SearchType(expr,typesize) then
  406. begin
  407. oper.hastype:=true;
  408. Consume(AS_LPAREN);
  409. BuildOperand(oper);
  410. Consume(AS_RPAREN);
  411. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  412. oper.SetSize(typesize,true);
  413. end
  414. else
  415. begin
  416. if oper.SetupVar(expr,false) then
  417. ReadPercent(oper)
  418. else
  419. Begin
  420. { look for special symbols ... }
  421. if expr= '__HIGH' then
  422. begin
  423. consume(AS_LPAREN);
  424. if not oper.setupvar('high'+actasmpattern,false) then
  425. Message1(sym_e_unknown_id,'high'+actasmpattern);
  426. consume(AS_ID);
  427. consume(AS_RPAREN);
  428. end
  429. else
  430. if expr = '__RESULT' then
  431. oper.SetUpResult
  432. else
  433. if expr = '__SELF' then
  434. oper.SetupSelf
  435. else
  436. if expr = '__OLDEBP' then
  437. oper.SetupOldEBP
  438. else
  439. { check for direct symbolic names }
  440. { only if compiling the system unit }
  441. if (cs_compilesystem in aktmoduleswitches) then
  442. begin
  443. if not oper.SetupDirectVar(expr) then
  444. Begin
  445. { not found, finally ... add it anyways ... }
  446. Message1(asmr_w_id_supposed_external,expr);
  447. oper.InitRef;
  448. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(expr);
  449. end;
  450. end
  451. else
  452. Message1(sym_e_unknown_id,expr);
  453. end;
  454. end;
  455. end;
  456. if actasmtoken=AS_DOT then
  457. MaybeRecordOffset;
  458. { add a constant expression? }
  459. if (actasmtoken=AS_PLUS) then
  460. begin
  461. l:=BuildConstExpression(true,false);
  462. case oper.opr.typ of
  463. OPR_CONSTANT :
  464. inc(oper.opr.val,l);
  465. OPR_LOCAL :
  466. inc(oper.opr.localsymofs,l);
  467. OPR_REFERENCE :
  468. inc(oper.opr.ref.offset,l);
  469. else
  470. internalerror(200309202);
  471. end;
  472. end
  473. end;
  474. { Do we have a indexing reference, then parse it also }
  475. if actasmtoken=AS_LPAREN then
  476. BuildReference(oper);
  477. end;
  478. AS_REGISTER: { Register, a variable reference or a constant reference }
  479. Begin
  480. { save the type of register used. }
  481. tempreg:=actasmregister;
  482. Consume(AS_REGISTER);
  483. if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  484. begin
  485. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  486. Message(asmr_e_invalid_operand_type);
  487. oper.opr.typ:=OPR_REGISTER;
  488. oper.opr.reg:=tempreg;
  489. end
  490. else
  491. Message(asmr_e_syn_operand);
  492. end;
  493. AS_END,
  494. AS_SEPARATOR,
  495. AS_COMMA: ;
  496. else
  497. Begin
  498. Message(asmr_e_syn_operand);
  499. Consume(actasmtoken);
  500. end;
  501. end; { end case }
  502. end;
  503. {*****************************************************************************
  504. tSparcReader
  505. *****************************************************************************}
  506. procedure tSparcReader.BuildOpCode(instr : tSparcinstruction);
  507. var
  508. operandnum : longint;
  509. Begin
  510. { opcode }
  511. if (actasmtoken<>AS_OPCODE) then
  512. Begin
  513. Message(asmr_e_invalid_or_missing_opcode);
  514. RecoverConsume(true);
  515. exit;
  516. end;
  517. { Fill the instr object with the current state }
  518. with instr do
  519. begin
  520. Opcode:=ActOpcode;
  521. condition:=ActCondition;
  522. end;
  523. { We are reading operands, so opcode will be an AS_ID }
  524. operandnum:=1;
  525. Consume(AS_OPCODE);
  526. { Zero operand opcode ? }
  527. if actasmtoken in [AS_SEPARATOR,AS_END] then
  528. begin
  529. operandnum:=0;
  530. exit;
  531. end;
  532. { Read the operands }
  533. repeat
  534. case actasmtoken of
  535. AS_COMMA: { Operand delimiter }
  536. Begin
  537. if operandnum>Max_Operands then
  538. Message(asmr_e_too_many_operands)
  539. else
  540. begin
  541. { condition operands doesn't set the operand but write to the
  542. condition field of the instruction
  543. }
  544. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  545. Inc(operandnum);
  546. end;
  547. Consume(AS_COMMA);
  548. end;
  549. AS_SEPARATOR,
  550. AS_END : { End of asm operands for this opcode }
  551. begin
  552. break;
  553. end;
  554. else
  555. BuildOperand(instr.Operands[operandnum] as tSparcoperand);
  556. end; { end case }
  557. until false;
  558. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  559. dec(operandnum);
  560. instr.Ops:=operandnum;
  561. end;
  562. function TSparcReader.is_asmopcode(const s: string):boolean;
  563. var
  564. str2opEntry: tstr2opEntry;
  565. cond:TAsmCond;
  566. Begin
  567. { making s a value parameter would break other assembler readers }
  568. is_asmopcode:=false;
  569. { clear op code }
  570. actopcode:=A_None;
  571. { clear condition }
  572. fillchar(actcondition,sizeof(actcondition),0);
  573. str2opentry:=tstr2opentry(iasmops.search(s));
  574. if assigned(str2opentry) then
  575. begin
  576. actopcode:=str2opentry.op;
  577. actasmtoken:=AS_OPCODE;
  578. is_asmopcode:=true;
  579. end
  580. { not found, check branch instructions }
  581. else if Upcase(s[1])='B' then
  582. begin
  583. { we can search here without an extra table which is sorted by string length
  584. because we take the whole remaining string without the leading B }
  585. actopcode := A_Bxx;
  586. for cond:=low(TAsmCond) to high(TAsmCond) do
  587. if(cond in [C_AE])and(Upper(copy(s,2,length(s)-1))=Upper(Cond2Str[cond])) then
  588. begin
  589. WriteLn('Bxx');
  590. actasmtoken:=AS_OPCODE;
  591. is_asmopcode:=true;
  592. end;
  593. end;
  594. end;
  595. procedure tSparcReader.ConvertCalljmp(instr : tSparcinstruction);
  596. var
  597. newopr : toprrec;
  598. begin
  599. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  600. with newopr do
  601. begin
  602. typ:=OPR_SYMBOL;
  603. symbol:=instr.Operands[1].opr.ref.symbol;
  604. symofs:=instr.Operands[1].opr.ref.offset;
  605. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  606. (instr.Operands[1].opr.ref.index<>NR_NO) or
  607. (instr.Operands[1].opr.ref.symaddr<>refs_full) then
  608. Message(asmr_e_syn_operand);
  609. instr.Operands[1].opr:=newopr;
  610. end;
  611. end;
  612. procedure tSparcReader.handleopcode;
  613. var
  614. instr : tSparcinstruction;
  615. begin
  616. instr:=TSparcInstruction.Create(TSparcOperand);
  617. BuildOpcode(instr);
  618. with instr do
  619. begin
  620. condition := actcondition;
  621. if is_calljmp(opcode) then
  622. ConvertCalljmp(instr);
  623. ConcatInstruction(curlist);
  624. Free;
  625. end;
  626. end;
  627. {*****************************************************************************
  628. Initialize
  629. *****************************************************************************}
  630. const
  631. asmmode_Sparc_att_info : tasmmodeinfo =
  632. (
  633. id : asmmode_Sparc_gas;
  634. idtxt : 'GAS';
  635. casmreader : tSparcReader;
  636. );
  637. asmmode_Sparc_standard_info : tasmmodeinfo =
  638. (
  639. id : asmmode_standard;
  640. idtxt : 'DIRECT';
  641. casmreader : tSparcReader;
  642. );
  643. initialization
  644. RegisterAsmMode(asmmode_Sparc_att_info);
  645. RegisterAsmMode(asmmode_Sparc_standard_info);
  646. end.
  647. {
  648. $Log$
  649. Revision 1.2 2003-12-10 13:16:36 mazen
  650. * improve hadlign %hi and %lo operators
  651. Revision 1.1 2003/12/08 13:02:21 mazen
  652. + support for native sparc assembler reader
  653. }