racpugas.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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 : tcgint;
  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 : tcgint;
  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) then
  205. checklocalsubscript(oper.opr.localsym);
  206. inc(oper.opr.localsymofs,l)
  207. end;
  208. OPR_CONSTANT :
  209. if (mangledname<>'') then
  210. begin
  211. if (oper.opr.val<>0) then
  212. Message(asmr_e_wrong_sym_type);
  213. oper.opr.typ:=OPR_SYMBOL;
  214. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
  215. end
  216. else
  217. inc(oper.opr.val,l);
  218. OPR_REFERENCE :
  219. inc(oper.opr.ref.offset,l);
  220. OPR_SYMBOL:
  221. Message(asmr_e_invalid_symbol_ref);
  222. else
  223. internalerror(200309221);
  224. end;
  225. end;
  226. var
  227. tempreg : tregister;
  228. tempstr : string;
  229. tempsymtyp : TAsmSymType;
  230. hl : tasmlabel;
  231. gotplus,
  232. negative : boolean;
  233. Begin
  234. expr:='';
  235. gotplus:=true;
  236. negative:=false;
  237. repeat
  238. case actasmtoken of
  239. AS_MINUS :
  240. begin
  241. consume(AS_MINUS);
  242. negative:=true;
  243. gotplus:=true;
  244. end;
  245. AS_PLUS :
  246. begin
  247. consume(AS_PLUS);
  248. negative:=false;
  249. gotplus:=true;
  250. end;
  251. AS_INTNUM,
  252. AS_MOD:
  253. Begin
  254. if not gotplus then
  255. Message(asmr_e_invalid_reference_syntax);
  256. l:=BuildConstExpression(True,False);
  257. if negative then
  258. l:=-l;
  259. { Constant memory offset }
  260. oper.InitRef;
  261. oper.opr.ref.refaddr:=addr_full;
  262. oper.opr.ref.offset:=l;
  263. GotPlus:=(prevasmtoken=AS_PLUS) or
  264. (prevasmtoken=AS_MINUS);
  265. if GotPlus then
  266. negative:=(prevasmtoken=AS_MINUS);
  267. end;
  268. AS_LBRACKET :
  269. begin
  270. { memory reference }
  271. BuildReference(oper);
  272. gotplus:=false;
  273. end;
  274. AS_RELTYPE:
  275. begin
  276. { Low or High part of a constant (or constant
  277. memory location) }
  278. oper.InitRef;
  279. oper.opr.ref.refaddr:=actrel;
  280. Consume(actasmtoken);
  281. Consume(AS_LPAREN);
  282. BuildConstSymbolExpression(false, true,false,l,tempstr,tempsymtyp);
  283. if not assigned(oper.opr.ref.symbol) then
  284. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp)
  285. else
  286. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  287. case oper.opr.typ of
  288. OPR_CONSTANT :
  289. inc(oper.opr.val,l);
  290. OPR_LOCAL :
  291. inc(oper.opr.localsymofs,l);
  292. OPR_REFERENCE :
  293. inc(oper.opr.ref.offset,l);
  294. else
  295. internalerror(200309202);
  296. end;
  297. Consume(AS_RPAREN);
  298. gotplus:=false;
  299. end;
  300. AS_ID: { A constant expression, or a Variable ref. }
  301. Begin
  302. { Local Label ? }
  303. if is_locallabel(actasmpattern) then
  304. begin
  305. CreateLocalLabel(actasmpattern,hl,false);
  306. Consume(AS_ID);
  307. AddLabelOperand(hl);
  308. end
  309. else
  310. { Check for label }
  311. if SearchLabel(actasmpattern,hl,false) then
  312. begin
  313. Consume(AS_ID);
  314. AddLabelOperand(hl);
  315. end
  316. else
  317. { probably a variable or normal expression }
  318. { or a procedure (such as in CALL ID) }
  319. Begin
  320. { is it a constant ? }
  321. if SearchIConstant(actasmpattern,l) then
  322. Begin
  323. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  324. Message(asmr_e_invalid_operand_type);
  325. BuildConstantOperand(oper);
  326. end
  327. else
  328. begin
  329. expr:=actasmpattern;
  330. Consume(AS_ID);
  331. { typecasting? }
  332. if (actasmtoken=AS_LPAREN) and
  333. SearchType(expr,typesize) then
  334. begin
  335. oper.hastype:=true;
  336. Consume(AS_LPAREN);
  337. BuildOperand(oper);
  338. Consume(AS_RPAREN);
  339. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  340. oper.SetSize(typesize,true);
  341. end
  342. else
  343. begin
  344. if not oper.SetupVar(expr,false) then
  345. Begin
  346. { look for special symbols ... }
  347. if expr= '__HIGH' then
  348. begin
  349. consume(AS_LPAREN);
  350. if not oper.setupvar('high'+actasmpattern,false) then
  351. Message1(sym_e_unknown_id,'high'+actasmpattern);
  352. consume(AS_ID);
  353. consume(AS_RPAREN);
  354. end
  355. else
  356. if expr = '__RESULT' then
  357. oper.SetUpResult
  358. else
  359. if expr = '__SELF' then
  360. oper.SetupSelf
  361. else
  362. if expr = '__OLDEBP' then
  363. oper.SetupOldEBP
  364. else
  365. Message1(sym_e_unknown_id,expr);
  366. end;
  367. end;
  368. end;
  369. if actasmtoken=AS_DOT then
  370. MaybeRecordOffset;
  371. { add a constant expression? }
  372. if (actasmtoken=AS_PLUS) then
  373. begin
  374. l:=BuildConstExpression(true,false);
  375. case oper.opr.typ of
  376. OPR_CONSTANT :
  377. inc(oper.opr.val,l);
  378. OPR_LOCAL :
  379. inc(oper.opr.localsymofs,l);
  380. OPR_REFERENCE :
  381. inc(oper.opr.ref.offset,l);
  382. else
  383. internalerror(200309202);
  384. end;
  385. end
  386. end;
  387. gotplus:=false;
  388. end;
  389. AS_REGISTER: { Register, a variable reference or a constant reference }
  390. Begin
  391. { save the type of register used. }
  392. tempreg:=actasmregister;
  393. Consume(AS_REGISTER);
  394. if (oper.opr.typ in [OPR_REGISTER,OPR_REFERENCE]) and gotplus then
  395. begin
  396. oper.initref;
  397. oper.opr.ref.refaddr:=addr_full;
  398. if oper.opr.ref.base<>NR_NO then
  399. oper.opr.ref.base:=tempreg
  400. else
  401. if oper.opr.ref.index<>NR_NO then
  402. oper.opr.ref.index:=tempreg
  403. else
  404. Message(asmr_e_multiple_index);
  405. end
  406. else
  407. begin
  408. if (oper.opr.typ<>OPR_NONE) then
  409. Message(asmr_e_invalid_operand_type);
  410. oper.opr.typ:=OPR_REGISTER;
  411. oper.opr.reg:=tempreg;
  412. end;
  413. gotplus:=false;
  414. end;
  415. AS_END,
  416. AS_SEPARATOR,
  417. AS_COMMA:
  418. break;
  419. else
  420. Begin
  421. Message(asmr_e_syn_operand);
  422. Consume(actasmtoken);
  423. end;
  424. end; { end case }
  425. until false;
  426. end;
  427. {*****************************************************************************
  428. TSparcReader
  429. *****************************************************************************}
  430. procedure TSparcReader.BuildOpCode(instr : tSparcinstruction);
  431. var
  432. operandnum : longint;
  433. Begin
  434. { opcode }
  435. if (actasmtoken<>AS_OPCODE) then
  436. Begin
  437. Message(asmr_e_invalid_or_missing_opcode);
  438. RecoverConsume(true);
  439. exit;
  440. end;
  441. { Fill the instr object with the current state }
  442. with instr do
  443. begin
  444. Opcode:=ActOpcode;
  445. condition:=ActCondition;
  446. end;
  447. { We are reading operands, so opcode will be an AS_ID }
  448. operandnum:=1;
  449. Consume(AS_OPCODE);
  450. { Zero operand opcode ? }
  451. if actasmtoken in [AS_SEPARATOR,AS_END] then
  452. begin
  453. operandnum:=0;
  454. exit;
  455. end;
  456. { delayslot annulled? }
  457. if (actasmtoken=AS_COMMA) then
  458. begin
  459. consume(AS_COMMA);
  460. if actasmpattern='A' then
  461. instr.delayslot_annulled:=true;
  462. { force reading of AS_COMMA instead of AS_ID, otherwise
  463. a label .L0 will first read a AS_DOT instead of AS_ID }
  464. actasmtoken:=AS_COMMA;
  465. consume(AS_COMMA);
  466. end;
  467. { Read the operands }
  468. repeat
  469. case actasmtoken of
  470. AS_COMMA: { Operand delimiter }
  471. Begin
  472. if operandnum>Max_Operands then
  473. Message(asmr_e_too_many_operands)
  474. else
  475. begin
  476. { condition operands doesn't set the operand but write to the
  477. condition field of the instruction
  478. }
  479. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  480. Inc(operandnum);
  481. end;
  482. Consume(AS_COMMA);
  483. end;
  484. AS_SEPARATOR,
  485. AS_END : { End of asm operands for this opcode }
  486. begin
  487. break;
  488. end;
  489. else
  490. BuildOperand(instr.Operands[operandnum] as tSparcoperand);
  491. end; { end case }
  492. until false;
  493. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  494. dec(operandnum);
  495. instr.Ops:=operandnum;
  496. end;
  497. function TSparcReader.is_asmopcode(const s: string):boolean;
  498. var
  499. cond,condlo,condhi:TAsmCond;
  500. condstr:string[15];
  501. Begin
  502. { making s a value parameter would break other assembler readers }
  503. is_asmopcode:=false;
  504. { clear op code }
  505. actopcode:=A_None;
  506. { clear condition }
  507. fillchar(actcondition,sizeof(actcondition),0);
  508. { Search opcodes }
  509. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  510. if actopcode<>A_NONE then
  511. begin
  512. actasmtoken:=AS_OPCODE;
  513. result:=TRUE;
  514. exit;
  515. end;
  516. { not found, check branch instructions }
  517. if (Upcase(s[1])='B') then
  518. begin
  519. condstr:=lower(copy(s,2,maxint));
  520. condlo:=firstIntCond;
  521. condhi:=lastIntCond;
  522. actopcode:=A_Bxx;
  523. end
  524. else if ((Upcase(s[1])='F') and (Upcase(s[2])='B')) then
  525. begin
  526. condstr:=lower(copy(s,3,maxint));
  527. condlo:=firstFloatCond;
  528. condhi:=lastFloatCond;
  529. actopcode:=A_FBxx;
  530. end
  531. else
  532. exit;
  533. for cond:=condlo to condhi do
  534. if (condstr=Cond2Str[cond]) then
  535. begin
  536. actasmtoken:=AS_OPCODE;
  537. actcondition:=cond;
  538. is_asmopcode:=true;
  539. break;
  540. end;
  541. end;
  542. procedure TSparcReader.ConvertCalljmp(instr : tSparcinstruction);
  543. var
  544. newopr : toprrec;
  545. begin
  546. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  547. with newopr do
  548. begin
  549. typ:=OPR_SYMBOL;
  550. symbol:=instr.Operands[1].opr.ref.symbol;
  551. symofs:=instr.Operands[1].opr.ref.offset;
  552. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  553. (instr.Operands[1].opr.ref.index<>NR_NO) or
  554. (instr.Operands[1].opr.ref.refaddr<>addr_full) then
  555. Message(asmr_e_syn_operand);
  556. instr.Operands[1].opr:=newopr;
  557. end;
  558. end;
  559. procedure TSparcReader.handleopcode;
  560. var
  561. instr : tSparcinstruction;
  562. begin
  563. instr:=TSparcInstruction.Create(TSparcOperand);
  564. BuildOpcode(instr);
  565. with instr do
  566. begin
  567. condition := actcondition;
  568. if is_calljmp(opcode) then
  569. ConvertCalljmp(instr);
  570. ConcatInstruction(curlist);
  571. Free;
  572. end;
  573. end;
  574. {*****************************************************************************
  575. Initialize
  576. *****************************************************************************}
  577. const
  578. asmmode_Sparc_att_info : tasmmodeinfo =
  579. (
  580. id : asmmode_Sparc_gas;
  581. idtxt : 'GAS';
  582. casmreader : TSparcReader;
  583. );
  584. asmmode_Sparc_standard_info : tasmmodeinfo =
  585. (
  586. id : asmmode_standard;
  587. idtxt : 'STANDARD';
  588. casmreader : TSparcReader;
  589. );
  590. initialization
  591. RegisterAsmMode(asmmode_Sparc_att_info);
  592. RegisterAsmMode(asmmode_Sparc_standard_info);
  593. end.