racpugas.pas 21 KB

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