racpugas.pas 22 KB

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