racpugas.pas 21 KB

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