racpugas.pas 20 KB

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