racpugas.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. raatt,racpu;
  22. type
  23. tSparcReader = class(tattreader)
  24. function is_asmopcode(const s: string):boolean;override;
  25. procedure handleopcode;override;
  26. procedure BuildReference(oper : tSparcoperand);
  27. procedure BuildOperand(oper : tSparcoperand);
  28. procedure BuildOpCode(instr : tSparcinstruction);
  29. procedure ReadPercent(oper : tSparcoperand);
  30. procedure ReadSym(oper : tSparcoperand);
  31. procedure ConvertCalljmp(instr : tSparcinstruction);
  32. procedure handlepercent;override;
  33. end;
  34. Implementation
  35. uses
  36. { helpers }
  37. cutils,
  38. { global }
  39. globtype,verbose,
  40. systems,
  41. { aasm }
  42. cpubase,aasmbase,aasmtai,aasmdata,aasmcpu,
  43. { symtable }
  44. symconst,symsym,
  45. { parser }
  46. scanner,
  47. procinfo,
  48. rabase,rautils,
  49. cgbase,cgobj
  50. ;
  51. procedure TSparcReader.ReadSym(oper : tSparcoperand);
  52. var
  53. tempstr, mangledname : string;
  54. typesize,l,k : aint;
  55. begin
  56. tempstr:=actasmpattern;
  57. Consume(AS_ID);
  58. { typecasting? }
  59. if (actasmtoken=AS_LPAREN) and
  60. SearchType(tempstr,typesize) then
  61. begin
  62. oper.hastype:=true;
  63. Consume(AS_LPAREN);
  64. BuildOperand(oper);
  65. Consume(AS_RPAREN);
  66. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  67. oper.SetSize(typesize,true);
  68. end
  69. else
  70. if not oper.SetupVar(tempstr,false) then
  71. Message1(sym_e_unknown_id,tempstr);
  72. { record.field ? }
  73. if actasmtoken=AS_DOT then
  74. begin
  75. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  76. if (mangledname<>'') then
  77. Message(asmr_e_invalid_reference_syntax);
  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.refaddr:=addr_low
  94. else if upper(actasmpattern)='HI' then
  95. oper.opr.ref.refaddr:=addr_high
  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. var
  106. l : aint;
  107. regs : byte;
  108. hasimm : boolean;
  109. begin
  110. oper.initref;
  111. regs:=0;
  112. hasimm:=false;
  113. Consume(AS_LBRACKET);
  114. repeat
  115. Case actasmtoken of
  116. AS_PLUS,
  117. AS_INTNUM,
  118. AS_MINUS:
  119. Begin
  120. if hasimm or (regs>1) then
  121. Begin
  122. Message(asmr_e_invalid_reference_syntax);
  123. RecoverConsume(true);
  124. break;
  125. End;
  126. if actasmtoken=AS_PLUS then
  127. Consume(AS_PLUS);
  128. if (actasmtoken=AS_INTNUM) then
  129. begin
  130. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  131. hasimm:=true;
  132. end;
  133. End;
  134. AS_REGISTER:
  135. Begin
  136. if regs<2 then
  137. begin
  138. if regs=0 then
  139. oper.opr.ref.base:=actasmregister
  140. else
  141. oper.opr.ref.index:=actasmregister;
  142. inc(regs);
  143. end
  144. else
  145. begin
  146. Message(asmr_e_invalid_reference_syntax);
  147. RecoverConsume(true);
  148. break;
  149. end;
  150. Consume(AS_REGISTER);
  151. end;
  152. AS_ID:
  153. Begin
  154. l:=BuildConstExpression(true,true);
  155. inc(oper.opr.ref.offset,l);
  156. End;
  157. AS_RBRACKET:
  158. begin
  159. if (regs=0) and (not hasimm) then
  160. Message(asmr_e_invalid_reference_syntax);
  161. Consume(AS_RBRACKET);
  162. break;
  163. end;
  164. else
  165. Begin
  166. Message(asmr_e_invalid_reference_syntax);
  167. RecoverConsume(false);
  168. break;
  169. end;
  170. end;
  171. until false;
  172. end;
  173. procedure TSparcReader.handlepercent;
  174. var
  175. len : longint;
  176. begin
  177. len:=1;
  178. actasmpattern[len]:='%';
  179. c:=current_scanner.asmgetchar;
  180. { to be a register there must be a letter and not a number }
  181. while c in ['a'..'z','A'..'Z','0'..'9'] do
  182. Begin
  183. inc(len);
  184. actasmpattern[len]:=c;
  185. c:=current_scanner.asmgetchar;
  186. end;
  187. actasmpattern[0]:=chr(len);
  188. uppervar(actasmpattern);
  189. if is_register(actasmpattern) then
  190. exit;
  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_register);
  197. end;
  198. Procedure TSparcReader.BuildOperand(oper : tSparcoperand);
  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_LBRACKET :
  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 oper.SetupVar(expr,false) then
  395. ReadPercent(oper)
  396. else
  397. Begin
  398. { look for special symbols ... }
  399. if expr= '__HIGH' then
  400. begin
  401. consume(AS_LPAREN);
  402. if not oper.setupvar('high'+actasmpattern,false) then
  403. Message1(sym_e_unknown_id,'high'+actasmpattern);
  404. consume(AS_ID);
  405. consume(AS_RPAREN);
  406. end
  407. else
  408. if expr = '__RESULT' then
  409. oper.SetUpResult
  410. else
  411. if expr = '__SELF' then
  412. oper.SetupSelf
  413. else
  414. if expr = '__OLDEBP' then
  415. oper.SetupOldEBP
  416. else
  417. Message1(sym_e_unknown_id,expr);
  418. end;
  419. end;
  420. end;
  421. if actasmtoken=AS_DOT then
  422. MaybeRecordOffset;
  423. { add a constant expression? }
  424. if (actasmtoken=AS_PLUS) then
  425. begin
  426. l:=BuildConstExpression(true,false);
  427. case oper.opr.typ of
  428. OPR_CONSTANT :
  429. inc(oper.opr.val,l);
  430. OPR_LOCAL :
  431. inc(oper.opr.localsymofs,l);
  432. OPR_REFERENCE :
  433. inc(oper.opr.ref.offset,l);
  434. else
  435. internalerror(200309202);
  436. end;
  437. end
  438. end;
  439. gotplus:=false;
  440. end;
  441. AS_REGISTER: { Register, a variable reference or a constant reference }
  442. Begin
  443. { save the type of register used. }
  444. tempreg:=actasmregister;
  445. Consume(AS_REGISTER);
  446. if (oper.opr.typ in [OPR_REGISTER,OPR_REFERENCE]) and gotplus then
  447. begin
  448. oper.initref;
  449. oper.opr.ref.refaddr:=addr_full;
  450. if oper.opr.ref.base<>NR_NO then
  451. oper.opr.ref.base:=tempreg
  452. else
  453. if oper.opr.ref.index<>NR_NO then
  454. oper.opr.ref.index:=tempreg
  455. else
  456. Message(asmr_e_multiple_index);
  457. end
  458. else
  459. begin
  460. if (oper.opr.typ<>OPR_NONE) then
  461. Message(asmr_e_invalid_operand_type);
  462. oper.opr.typ:=OPR_REGISTER;
  463. oper.opr.reg:=tempreg;
  464. end;
  465. gotplus:=false;
  466. end;
  467. AS_END,
  468. AS_SEPARATOR,
  469. AS_COMMA:
  470. break;
  471. else
  472. Begin
  473. Message(asmr_e_syn_operand);
  474. Consume(actasmtoken);
  475. end;
  476. end; { end case }
  477. until false;
  478. end;
  479. {*****************************************************************************
  480. TSparcReader
  481. *****************************************************************************}
  482. procedure TSparcReader.BuildOpCode(instr : tSparcinstruction);
  483. var
  484. operandnum : longint;
  485. Begin
  486. { opcode }
  487. if (actasmtoken<>AS_OPCODE) then
  488. Begin
  489. Message(asmr_e_invalid_or_missing_opcode);
  490. RecoverConsume(true);
  491. exit;
  492. end;
  493. { Fill the instr object with the current state }
  494. with instr do
  495. begin
  496. Opcode:=ActOpcode;
  497. condition:=ActCondition;
  498. end;
  499. { We are reading operands, so opcode will be an AS_ID }
  500. operandnum:=1;
  501. Consume(AS_OPCODE);
  502. { Zero operand opcode ? }
  503. if actasmtoken in [AS_SEPARATOR,AS_END] then
  504. begin
  505. operandnum:=0;
  506. exit;
  507. end;
  508. { delayslot annulled? }
  509. if (actasmtoken=AS_COMMA) then
  510. begin
  511. consume(AS_COMMA);
  512. if actasmpattern='A' then
  513. instr.delayslot_annulled:=true;
  514. { force reading of AS_COMMA instead of AS_ID, otherwise
  515. a label .L0 will first read a AS_DOT instead of AS_ID }
  516. actasmtoken:=AS_COMMA;
  517. consume(AS_COMMA);
  518. end;
  519. { Read the operands }
  520. repeat
  521. case actasmtoken of
  522. AS_COMMA: { Operand delimiter }
  523. Begin
  524. if operandnum>Max_Operands then
  525. Message(asmr_e_too_many_operands)
  526. else
  527. begin
  528. { condition operands doesn't set the operand but write to the
  529. condition field of the instruction
  530. }
  531. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  532. Inc(operandnum);
  533. end;
  534. Consume(AS_COMMA);
  535. end;
  536. AS_SEPARATOR,
  537. AS_END : { End of asm operands for this opcode }
  538. begin
  539. break;
  540. end;
  541. else
  542. BuildOperand(instr.Operands[operandnum] as tSparcoperand);
  543. end; { end case }
  544. until false;
  545. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  546. dec(operandnum);
  547. instr.Ops:=operandnum;
  548. end;
  549. function TSparcReader.is_asmopcode(const s: string):boolean;
  550. var
  551. cond,condlo,condhi:TAsmCond;
  552. condstr:string[15];
  553. Begin
  554. { making s a value parameter would break other assembler readers }
  555. is_asmopcode:=false;
  556. { clear op code }
  557. actopcode:=A_None;
  558. { clear condition }
  559. fillchar(actcondition,sizeof(actcondition),0);
  560. { Search opcodes }
  561. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  562. if actopcode<>A_NONE then
  563. begin
  564. actasmtoken:=AS_OPCODE;
  565. result:=TRUE;
  566. exit;
  567. end;
  568. { not found, check branch instructions }
  569. if (Upcase(s[1])='B') then
  570. begin
  571. condstr:=lower(copy(s,2,maxint));
  572. condlo:=firstIntCond;
  573. condhi:=lastIntCond;
  574. actopcode:=A_Bxx;
  575. end
  576. else if ((Upcase(s[1])='F') and (Upcase(s[2])='B')) then
  577. begin
  578. condstr:=lower(copy(s,3,maxint));
  579. condlo:=firstFloatCond;
  580. condhi:=lastFloatCond;
  581. actopcode:=A_FBxx;
  582. end
  583. else
  584. exit;
  585. for cond:=condlo to condhi do
  586. if (condstr=Cond2Str[cond]) then
  587. begin
  588. actasmtoken:=AS_OPCODE;
  589. actcondition:=cond;
  590. is_asmopcode:=true;
  591. break;
  592. end;
  593. end;
  594. procedure TSparcReader.ConvertCalljmp(instr : tSparcinstruction);
  595. var
  596. newopr : toprrec;
  597. begin
  598. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  599. with newopr do
  600. begin
  601. typ:=OPR_SYMBOL;
  602. symbol:=instr.Operands[1].opr.ref.symbol;
  603. symofs:=instr.Operands[1].opr.ref.offset;
  604. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  605. (instr.Operands[1].opr.ref.index<>NR_NO) or
  606. (instr.Operands[1].opr.ref.refaddr<>addr_full) then
  607. Message(asmr_e_syn_operand);
  608. instr.Operands[1].opr:=newopr;
  609. end;
  610. end;
  611. procedure TSparcReader.handleopcode;
  612. var
  613. instr : tSparcinstruction;
  614. begin
  615. instr:=TSparcInstruction.Create(TSparcOperand);
  616. BuildOpcode(instr);
  617. with instr do
  618. begin
  619. condition := actcondition;
  620. if is_calljmp(opcode) then
  621. ConvertCalljmp(instr);
  622. ConcatInstruction(curlist);
  623. Free;
  624. end;
  625. end;
  626. {*****************************************************************************
  627. Initialize
  628. *****************************************************************************}
  629. const
  630. asmmode_Sparc_att_info : tasmmodeinfo =
  631. (
  632. id : asmmode_Sparc_gas;
  633. idtxt : 'GAS';
  634. casmreader : TSparcReader;
  635. );
  636. asmmode_Sparc_standard_info : tasmmodeinfo =
  637. (
  638. id : asmmode_standard;
  639. idtxt : 'STANDARD';
  640. casmreader : TSparcReader;
  641. );
  642. initialization
  643. RegisterAsmMode(asmmode_Sparc_att_info);
  644. RegisterAsmMode(asmmode_Sparc_standard_info);
  645. end.