racpugas.pas 20 KB

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