racpugas.pas 21 KB

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