racpugas.pas 20 KB

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