racpugas.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 ReadSym(oper : TOperand);
  30. procedure ConvertCalljmp(instr : TInstruction);
  31. procedure handlepercent;override;
  32. procedure handledollar;override;
  33. procedure handleopcode;override;
  34. end;
  35. Implementation
  36. uses
  37. { helpers }
  38. cutils,
  39. { global }
  40. globtype,verbose,
  41. systems,
  42. { aasm }
  43. cpubase,aasmbase,aasmtai,aasmdata,aasmcpu,
  44. { symtable }
  45. symconst,symsym,
  46. { parser }
  47. scanner,
  48. procinfo,
  49. rabase,
  50. rgbase,
  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.BuildReference(oper : TOperand);
  113. var
  114. l : aint;
  115. regs : byte;
  116. hasimm : boolean;
  117. begin
  118. oper.initref;
  119. regs:=0;
  120. hasimm:=false;
  121. Consume(ActAsmToken);
  122. repeat
  123. Case actasmtoken of
  124. AS_INTNUM,
  125. AS_MINUS,
  126. AS_PLUS:
  127. Begin
  128. if hasimm or (regs>1) then
  129. Begin
  130. Message(asmr_e_invalid_reference_syntax);
  131. RecoverConsume(true);
  132. break;
  133. End;
  134. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  135. hasimm:=true;
  136. End;
  137. AS_REGISTER:
  138. Begin
  139. if regs<2 then
  140. begin
  141. if regs=0 then
  142. oper.opr.ref.base:=actasmregister
  143. else
  144. oper.opr.ref.index:=actasmregister;
  145. inc(regs);
  146. end
  147. else
  148. begin
  149. Message(asmr_e_invalid_reference_syntax);
  150. RecoverConsume(true);
  151. break;
  152. end;
  153. Consume(AS_REGISTER);
  154. end;
  155. AS_ID:
  156. Begin
  157. l:=BuildConstExpression(true,true);
  158. inc(oper.opr.ref.offset,l);
  159. End;
  160. AS_RPAREN:
  161. begin
  162. if (regs=0) and (not hasimm) then
  163. Message(asmr_e_invalid_reference_syntax);
  164. Consume(AS_RPAREN);
  165. break;
  166. end;
  167. else
  168. Begin
  169. Message(asmr_e_invalid_reference_syntax);
  170. RecoverConsume(false);
  171. break;
  172. end;
  173. end;
  174. until false;
  175. end;
  176. procedure TMipsReader.handlepercent;
  177. var
  178. len : longint;
  179. begin
  180. len:=1;
  181. actasmpattern[len]:='%';
  182. c:=current_scanner.asmgetchar;
  183. { to be a register there must be a letter and not a number }
  184. while c in ['a'..'z','A'..'Z','0'..'9'] do
  185. Begin
  186. inc(len);
  187. actasmpattern[len]:=c;
  188. c:=current_scanner.asmgetchar;
  189. end;
  190. actasmpattern[0]:=chr(len);
  191. uppervar(actasmpattern);
  192. if (actasmpattern='%HI') then
  193. actasmtoken:=AS_HI
  194. else if (actasmpattern='%LO')then
  195. actasmtoken:=AS_LO
  196. else
  197. Message(asmr_e_invalid_reference_syntax);
  198. end;
  199. Procedure TMipsReader.BuildOperand(oper : TOperand);
  200. var
  201. expr : string;
  202. typesize,l : aint;
  203. procedure AddLabelOperand(hl:tasmlabel);
  204. begin
  205. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  206. is_calljmp(actopcode) then
  207. begin
  208. oper.opr.typ:=OPR_SYMBOL;
  209. oper.opr.symbol:=hl;
  210. end
  211. else
  212. begin
  213. oper.InitRef;
  214. oper.opr.ref.symbol:=hl;
  215. end;
  216. end;
  217. procedure MaybeRecordOffset;
  218. var
  219. mangledname: string;
  220. hasdot : boolean;
  221. l,
  222. toffset,
  223. tsize : aint;
  224. begin
  225. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  226. exit;
  227. l:=0;
  228. hasdot:=(actasmtoken=AS_DOT);
  229. if hasdot then
  230. begin
  231. if expr<>'' then
  232. begin
  233. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  234. if (oper.opr.typ<>OPR_CONSTANT) and
  235. (mangledname<>'') then
  236. Message(asmr_e_wrong_sym_type);
  237. inc(l,toffset);
  238. oper.SetSize(tsize,true);
  239. end;
  240. end;
  241. if actasmtoken in [AS_PLUS,AS_MINUS] then
  242. inc(l,BuildConstExpression(true,false));
  243. case oper.opr.typ of
  244. OPR_LOCAL :
  245. begin
  246. { don't allow direct access to fields of parameters, because that
  247. will generate buggy code. Allow it only for explicit typecasting }
  248. if hasdot and
  249. (not oper.hastype) and
  250. (tabstractnormalvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  251. (current_procinfo.procdef.proccalloption<>pocall_register) then
  252. Message(asmr_e_cannot_access_field_directly_for_parameters);
  253. inc(oper.opr.localsymofs,l)
  254. end;
  255. OPR_CONSTANT :
  256. if (mangledname<>'') then
  257. begin
  258. if (oper.opr.val<>0) then
  259. Message(asmr_e_wrong_sym_type);
  260. oper.opr.typ:=OPR_SYMBOL;
  261. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  262. end
  263. else
  264. inc(oper.opr.val,l);
  265. OPR_REFERENCE :
  266. inc(oper.opr.ref.offset,l);
  267. OPR_SYMBOL:
  268. Message(asmr_e_invalid_symbol_ref);
  269. else
  270. internalerror(200309221);
  271. end;
  272. end;
  273. var
  274. tempreg : tregister;
  275. tempstr : string;
  276. tempsymtyp : TAsmSymType;
  277. hl : tasmlabel;
  278. gotplus,
  279. negative : boolean;
  280. Begin
  281. expr:='';
  282. gotplus:=true;
  283. negative:=false;
  284. repeat
  285. case actasmtoken of
  286. AS_MINUS :
  287. begin
  288. consume(AS_MINUS);
  289. negative:=true;
  290. gotplus:=true;
  291. end;
  292. AS_PLUS :
  293. begin
  294. consume(AS_PLUS);
  295. negative:=false;
  296. gotplus:=true;
  297. end;
  298. AS_INTNUM,
  299. AS_MOD:
  300. Begin
  301. if not gotplus then
  302. Message(asmr_e_invalid_reference_syntax);
  303. l:=BuildConstExpression(True,False);
  304. if negative then
  305. l:=-l;
  306. { Constant memory offset }
  307. oper.InitRef;
  308. oper.opr.ref.refaddr:=addr_full;
  309. oper.opr.ref.offset:=l;
  310. GotPlus:=(prevasmtoken=AS_PLUS) or
  311. (prevasmtoken=AS_MINUS);
  312. if GotPlus then
  313. negative:=(prevasmtoken=AS_MINUS);
  314. end;
  315. AS_LPAREN :
  316. begin
  317. { memory reference }
  318. BuildReference(oper);
  319. gotplus:=false;
  320. end;
  321. AS_HI,
  322. AS_LO:
  323. begin
  324. { Low or High part of a constant (or constant
  325. memory location) }
  326. oper.InitRef;
  327. if actasmtoken=AS_LO then
  328. oper.opr.ref.refaddr:=addr_low
  329. else
  330. oper.opr.ref.refaddr:=addr_high;
  331. Consume(actasmtoken);
  332. Consume(AS_LPAREN);
  333. BuildConstSymbolExpression(false, true,false,l,tempstr,tempsymtyp);
  334. if not assigned(oper.opr.ref.symbol) then
  335. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr)
  336. else
  337. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  338. case oper.opr.typ of
  339. OPR_CONSTANT :
  340. inc(oper.opr.val,l);
  341. OPR_LOCAL :
  342. inc(oper.opr.localsymofs,l);
  343. OPR_REFERENCE :
  344. inc(oper.opr.ref.offset,l);
  345. else
  346. internalerror(200309202);
  347. end;
  348. Consume(AS_RPAREN);
  349. gotplus:=false;
  350. end;
  351. AS_ID: { A constant expression, or a Variable ref. }
  352. Begin
  353. { Local Label ? }
  354. if is_locallabel(actasmpattern) then
  355. begin
  356. CreateLocalLabel(actasmpattern,hl,false);
  357. Consume(AS_ID);
  358. AddLabelOperand(hl);
  359. end
  360. else
  361. { Check for label }
  362. if SearchLabel(actasmpattern,hl,false) then
  363. begin
  364. Consume(AS_ID);
  365. AddLabelOperand(hl);
  366. end
  367. else
  368. { probably a variable or normal expression }
  369. { or a procedure (such as in CALL ID) }
  370. Begin
  371. { is it a constant ? }
  372. if SearchIConstant(actasmpattern,l) then
  373. Begin
  374. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  375. Message(asmr_e_invalid_operand_type);
  376. BuildConstantOperand(oper);
  377. end
  378. else
  379. begin
  380. expr:=actasmpattern;
  381. Consume(AS_ID);
  382. { typecasting? }
  383. if (actasmtoken=AS_LPAREN) and
  384. SearchType(expr,typesize) then
  385. begin
  386. oper.hastype:=true;
  387. Consume(AS_LPAREN);
  388. BuildOperand(oper);
  389. Consume(AS_RPAREN);
  390. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  391. oper.SetSize(typesize,true);
  392. end
  393. else
  394. begin
  395. if not oper.SetupVar(expr,false) then
  396. Begin
  397. { look for special symbols ... }
  398. if expr= '__HIGH' then
  399. begin
  400. consume(AS_LPAREN);
  401. if not oper.setupvar('high'+actasmpattern,false) then
  402. Message1(sym_e_unknown_id,'high'+actasmpattern);
  403. consume(AS_ID);
  404. consume(AS_RPAREN);
  405. end
  406. else
  407. if expr = '__RESULT' then
  408. oper.SetUpResult
  409. else
  410. if expr = '__SELF' then
  411. oper.SetupSelf
  412. else
  413. if expr = '__OLDEBP' then
  414. oper.SetupOldEBP
  415. else
  416. Message1(sym_e_unknown_id,expr);
  417. end;
  418. end;
  419. end;
  420. if actasmtoken=AS_DOT then
  421. MaybeRecordOffset;
  422. { add a constant expression? }
  423. if (actasmtoken=AS_PLUS) then
  424. begin
  425. l:=BuildConstExpression(true,false);
  426. case oper.opr.typ of
  427. OPR_CONSTANT :
  428. inc(oper.opr.val,l);
  429. OPR_LOCAL :
  430. inc(oper.opr.localsymofs,l);
  431. OPR_REFERENCE :
  432. inc(oper.opr.ref.offset,l);
  433. else
  434. internalerror(200309202);
  435. end;
  436. end
  437. end;
  438. gotplus:=false;
  439. end;
  440. AS_REGISTER: { Register, a variable reference or a constant reference }
  441. Begin
  442. { save the type of register used. }
  443. tempreg:=actasmregister;
  444. Consume(AS_REGISTER);
  445. if (oper.opr.typ in [OPR_REGISTER,OPR_REFERENCE]) and gotplus then
  446. begin
  447. oper.initref;
  448. oper.opr.ref.refaddr:=addr_full;
  449. if oper.opr.ref.base<>NR_NO then
  450. oper.opr.ref.base:=tempreg
  451. else
  452. if oper.opr.ref.index<>NR_NO then
  453. oper.opr.ref.index:=tempreg
  454. else
  455. Message(asmr_e_multiple_index);
  456. end
  457. else
  458. begin
  459. if (oper.opr.typ<>OPR_NONE) then
  460. Message(asmr_e_invalid_operand_type);
  461. oper.opr.typ:=OPR_REGISTER;
  462. oper.opr.reg:=tempreg;
  463. end;
  464. gotplus:=false;
  465. end;
  466. AS_END,
  467. AS_SEPARATOR,
  468. AS_COMMA:
  469. break;
  470. else
  471. Begin
  472. Message(asmr_e_syn_operand);
  473. Consume(actasmtoken);
  474. end;
  475. end; { end case }
  476. until false;
  477. end;
  478. {*****************************************************************************
  479. TMipsReader
  480. *****************************************************************************}
  481. procedure TMipsReader.BuildOpCode(instr : TInstruction);
  482. var
  483. operandnum : longint;
  484. Begin
  485. { opcode }
  486. if (actasmtoken<>AS_OPCODE) then
  487. Begin
  488. Message(asmr_e_invalid_or_missing_opcode);
  489. RecoverConsume(true);
  490. exit;
  491. end;
  492. { Fill the instr object with the current state }
  493. with instr do
  494. begin
  495. Opcode:=ActOpcode;
  496. condition:=ActCondition;
  497. end;
  498. { We are reading operands, so opcode will be an AS_ID }
  499. operandnum:=1;
  500. Consume(AS_OPCODE);
  501. { Zero operand opcode ? }
  502. if actasmtoken in [AS_SEPARATOR,AS_END] then
  503. begin
  504. operandnum:=0;
  505. exit;
  506. end;
  507. { Read the operands }
  508. repeat
  509. case actasmtoken of
  510. AS_COMMA: { Operand delimiter }
  511. Begin
  512. if operandnum>Max_Operands then
  513. Message(asmr_e_too_many_operands)
  514. else
  515. begin
  516. { condition operands doesn't set the operand but write to the
  517. condition field of the instruction
  518. }
  519. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  520. Inc(operandnum);
  521. end;
  522. Consume(AS_COMMA);
  523. end;
  524. AS_SEPARATOR,
  525. AS_END : { End of asm operands for this opcode }
  526. begin
  527. break;
  528. end;
  529. else
  530. BuildOperand(instr.Operands[operandnum] as TOperand);
  531. end; { end case }
  532. until false;
  533. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  534. dec(operandnum);
  535. instr.Ops:=operandnum;
  536. end;
  537. function TMipsReader.is_asmopcode(const s: string):boolean;
  538. var
  539. cond:TAsmCond;
  540. Begin
  541. { making s a value parameter would break other assembler readers }
  542. is_asmopcode:=false;
  543. { clear op code }
  544. actopcode:=A_None;
  545. { clear condition }
  546. fillchar(actcondition,sizeof(actcondition),0);
  547. { Search opcodes }
  548. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  549. if actopcode<>A_NONE then
  550. begin
  551. actasmtoken:=AS_OPCODE;
  552. result:=TRUE;
  553. exit;
  554. end;
  555. { not found, check branch instructions }
  556. if (Upcase(s[1])='B') then
  557. begin
  558. { we can search here without an extra table which is sorted by string length
  559. because we take the whole remaining string without the leading B }
  560. actopcode := A_BC;
  561. for cond:=low(TAsmCond) to high(TAsmCond) do
  562. if (Upper(copy(s,2,length(s)-1))=Upper(Cond2Str[cond])) then
  563. begin
  564. actasmtoken:=AS_OPCODE;
  565. actcondition:=cond;
  566. is_asmopcode:=true;
  567. end;
  568. end;
  569. end;
  570. procedure TMipsReader.ConvertCalljmp(instr : TInstruction);
  571. var
  572. newopr : toprrec;
  573. begin
  574. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  575. with newopr do
  576. begin
  577. typ:=OPR_SYMBOL;
  578. symbol:=instr.Operands[1].opr.ref.symbol;
  579. symofs:=instr.Operands[1].opr.ref.offset;
  580. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  581. (instr.Operands[1].opr.ref.index<>NR_NO) or
  582. (instr.Operands[1].opr.ref.refaddr<>addr_full) then
  583. Message(asmr_e_syn_operand);
  584. instr.Operands[1].opr:=newopr;
  585. end;
  586. end;
  587. procedure TMipsReader.handleopcode;
  588. var
  589. instr : TInstruction;
  590. begin
  591. instr:=TInstruction.Create(TOperand);
  592. BuildOpcode(instr);
  593. with instr do
  594. begin
  595. condition := actcondition;
  596. if is_calljmp(opcode) then
  597. ConvertCalljmp(instr);
  598. if (opcode in [A_MTC0,A_MFC0]) then
  599. begin
  600. if (ops<2) or (operands[2].opr.typ<>OPR_REGISTER) then
  601. message(asmr_e_syn_operand);
  602. operands[2].opr.reg:=newreg(R_SPECIALREGISTER,getsupreg(operands[2].opr.reg),R_SUBNONE);
  603. end;
  604. ConcatInstruction(curlist);
  605. Free;
  606. end;
  607. end;
  608. {*****************************************************************************
  609. Initialize
  610. *****************************************************************************}
  611. const
  612. asmmode_mips_att_info : tasmmodeinfo =
  613. (
  614. id : asmmode_standard;
  615. idtxt : 'GAS';
  616. casmreader : TMipsReader;
  617. );
  618. initialization
  619. RegisterAsmMode(asmmode_mips_att_info);
  620. end.