racpugas.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Mazen NEIFER
  4. Does the parsing for the i386 GNU AS styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit racpugas;
  19. {$i fpcdefs.inc}
  20. Interface
  21. uses
  22. raatt,racpu;
  23. type
  24. tSparcReader = class(tattreader)
  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 ReadPercent(oper : tSparcoperand);
  31. procedure ReadSym(oper : tSparcoperand);
  32. procedure ConvertCalljmp(instr : tSparcinstruction);
  33. procedure handlepercent;override;
  34. end;
  35. Implementation
  36. uses
  37. { helpers }
  38. cutils,
  39. { global }
  40. globtype,globals,verbose,
  41. systems,
  42. { aasm }
  43. cpubase,cpuinfo,aasmbase,aasmtai,aasmcpu,
  44. { symtable }
  45. symconst,symbase,symtype,symsym,symtable,
  46. { parser }
  47. scanner,
  48. procinfo,
  49. itcpugas,
  50. rabase,rautils,
  51. cgbase,cgobj,cgutils
  52. ;
  53. procedure tSparcReader.ReadSym(oper : tSparcoperand);
  54. var
  55. tempstr : string;
  56. typesize,l,k : longint;
  57. begin
  58. tempstr:=actasmpattern;
  59. Consume(AS_ID);
  60. { typecasting? }
  61. if (actasmtoken=AS_LPAREN) and
  62. SearchType(tempstr,typesize) then
  63. begin
  64. oper.hastype:=true;
  65. Consume(AS_LPAREN);
  66. BuildOperand(oper);
  67. Consume(AS_RPAREN);
  68. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  69. oper.SetSize(typesize,true);
  70. end
  71. else
  72. if not oper.SetupVar(tempstr,false) then
  73. Message1(sym_e_unknown_id,tempstr);
  74. { record.field ? }
  75. if actasmtoken=AS_DOT then
  76. begin
  77. BuildRecordOffsetSize(tempstr,l,k);
  78. inc(oper.opr.ref.offset,l);
  79. end;
  80. end;
  81. procedure tSparcReader.ReadPercent(oper : tSparcoperand);
  82. begin
  83. { check for ...@ }
  84. if actasmtoken=AS_AT then
  85. begin
  86. if (oper.opr.ref.symbol=nil) and
  87. (oper.opr.ref.offset = 0) then
  88. Message(asmr_e_invalid_reference_syntax);
  89. Consume(AS_AT);
  90. if actasmtoken=AS_ID then
  91. begin
  92. if upper(actasmpattern)='LO' then
  93. oper.opr.ref.refaddr:=addr_lo
  94. else if upper(actasmpattern)='HI' then
  95. oper.opr.ref.refaddr:=addr_hi
  96. else
  97. Message(asmr_e_invalid_reference_syntax);
  98. Consume(AS_ID);
  99. end
  100. else
  101. Message(asmr_e_invalid_reference_syntax);
  102. end;
  103. end;
  104. Procedure tSparcReader.BuildReference(oper : tSparcoperand);
  105. var
  106. l : longint;
  107. regs : byte;
  108. hasimm : boolean;
  109. begin
  110. oper.initref;
  111. regs:=0;
  112. hasimm:=false;
  113. Consume(AS_LBRACKET);
  114. repeat
  115. Case actasmtoken of
  116. AS_INTNUM,
  117. AS_MINUS,
  118. AS_PLUS:
  119. Begin
  120. if hasimm or (regs>1) then
  121. Begin
  122. Message(asmr_e_invalid_reference_syntax);
  123. RecoverConsume(true);
  124. break;
  125. End;
  126. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  127. hasimm:=true;
  128. End;
  129. AS_REGISTER:
  130. Begin
  131. if regs<2 then
  132. begin
  133. if regs=0 then
  134. oper.opr.ref.base:=actasmregister
  135. else
  136. oper.opr.ref.index:=actasmregister;
  137. inc(regs);
  138. end
  139. else
  140. begin
  141. Message(asmr_e_invalid_reference_syntax);
  142. RecoverConsume(true);
  143. break;
  144. end;
  145. Consume(AS_REGISTER);
  146. end;
  147. AS_ID:
  148. Begin
  149. l:=BuildConstExpression(true,true);
  150. inc(oper.opr.ref.offset,l);
  151. End;
  152. AS_RBRACKET:
  153. begin
  154. if (regs=0) and (not hasimm) then
  155. Message(asmr_e_invalid_reference_syntax);
  156. Consume(AS_RBRACKET);
  157. break;
  158. end;
  159. else
  160. Begin
  161. Message(asmr_e_invalid_reference_syntax);
  162. RecoverConsume(false);
  163. break;
  164. end;
  165. end;
  166. until false;
  167. end;
  168. procedure TSparcReader.handlepercent;
  169. var
  170. len : longint;
  171. begin
  172. len:=1;
  173. actasmpattern[len]:='%';
  174. c:=current_scanner.asmgetchar;
  175. { to be a register there must be a letter and not a number }
  176. while c in ['a'..'z','A'..'Z','0'..'9'] do
  177. Begin
  178. inc(len);
  179. actasmpattern[len]:=c;
  180. c:=current_scanner.asmgetchar;
  181. end;
  182. actasmpattern[0]:=chr(len);
  183. uppervar(actasmpattern);
  184. if is_register(actasmpattern) then
  185. exit;
  186. if (actasmpattern='%HI') then
  187. actasmtoken:=AS_HI
  188. else if (actasmpattern='%LO')then
  189. actasmtoken:=AS_LO
  190. else
  191. Message(asmr_e_invalid_register);
  192. end;
  193. Procedure tSparcReader.BuildOperand(oper : tSparcoperand);
  194. var
  195. expr : string;
  196. typesize,l : longint;
  197. procedure AddLabelOperand(hl:tasmlabel);
  198. begin
  199. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  200. is_calljmp(actopcode) then
  201. begin
  202. oper.opr.typ:=OPR_SYMBOL;
  203. oper.opr.symbol:=hl;
  204. end
  205. else
  206. begin
  207. oper.InitRef;
  208. oper.opr.ref.symbol:=hl;
  209. end;
  210. end;
  211. procedure MaybeRecordOffset;
  212. var
  213. hasdot : boolean;
  214. l,
  215. toffset,
  216. tsize : longint;
  217. begin
  218. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  219. exit;
  220. l:=0;
  221. hasdot:=(actasmtoken=AS_DOT);
  222. if hasdot then
  223. begin
  224. if expr<>'' then
  225. begin
  226. BuildRecordOffsetSize(expr,toffset,tsize);
  227. inc(l,toffset);
  228. oper.SetSize(tsize,true);
  229. end;
  230. end;
  231. if actasmtoken in [AS_PLUS,AS_MINUS] then
  232. inc(l,BuildConstExpression(true,false));
  233. case oper.opr.typ of
  234. OPR_LOCAL :
  235. begin
  236. { don't allow direct access to fields of parameters, because that
  237. will generate buggy code. Allow it only for explicit typecasting }
  238. if hasdot and
  239. (not oper.hastype) and
  240. (tvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  241. (current_procinfo.procdef.proccalloption<>pocall_register) then
  242. Message(asmr_e_cannot_access_field_directly_for_parameters);
  243. inc(oper.opr.localsymofs,l)
  244. end;
  245. OPR_CONSTANT :
  246. inc(oper.opr.val,l);
  247. OPR_REFERENCE :
  248. inc(oper.opr.ref.offset,l);
  249. else
  250. internalerror(200309221);
  251. end;
  252. end;
  253. var
  254. tempreg : tregister;
  255. tempstr : string;
  256. hl : tasmlabel;
  257. Begin
  258. expr:='';
  259. case actasmtoken of
  260. AS_INTNUM,
  261. AS_MINUS,
  262. AS_PLUS,
  263. AS_MOD:
  264. Begin
  265. { Constant memory offset }
  266. { This must absolutely be followed by ( }
  267. oper.InitRef;
  268. oper.opr.ref.offset:=BuildConstExpression(True,False);
  269. end;
  270. AS_LBRACKET :
  271. begin
  272. { memory reference }
  273. BuildReference(oper);
  274. end;
  275. AS_HI,
  276. AS_LO:
  277. begin
  278. { Low or High part of a constant (or constant
  279. memory location) }
  280. oper.InitRef;
  281. if actasmtoken=AS_LO then
  282. oper.opr.ref.refaddr:=addr_lo
  283. else
  284. oper.opr.ref.refaddr:=addr_hi;
  285. Consume(actasmtoken);
  286. Consume(AS_LPAREN);
  287. BuildConstSymbolExpression(false, true,false,l,tempstr);
  288. if not assigned(oper.opr.ref.symbol) then
  289. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(tempstr)
  290. else
  291. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  292. case oper.opr.typ of
  293. OPR_CONSTANT :
  294. inc(oper.opr.val,l);
  295. OPR_LOCAL :
  296. inc(oper.opr.localsymofs,l);
  297. OPR_REFERENCE :
  298. inc(oper.opr.ref.offset,l);
  299. else
  300. internalerror(200309202);
  301. end;
  302. Consume(AS_RPAREN);
  303. end;
  304. AS_ID: { A constant expression, or a Variable ref. }
  305. Begin
  306. { Local Label ? }
  307. if is_locallabel(actasmpattern) then
  308. begin
  309. CreateLocalLabel(actasmpattern,hl,false);
  310. Consume(AS_ID);
  311. AddLabelOperand(hl);
  312. end
  313. else
  314. { Check for label }
  315. if SearchLabel(actasmpattern,hl,false) then
  316. begin
  317. Consume(AS_ID);
  318. AddLabelOperand(hl);
  319. end
  320. else
  321. { probably a variable or normal expression }
  322. { or a procedure (such as in CALL ID) }
  323. Begin
  324. { is it a constant ? }
  325. if SearchIConstant(actasmpattern,l) then
  326. Begin
  327. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  328. Message(asmr_e_invalid_operand_type);
  329. BuildConstantOperand(oper);
  330. end
  331. else
  332. begin
  333. expr:=actasmpattern;
  334. Consume(AS_ID);
  335. { typecasting? }
  336. if (actasmtoken=AS_LPAREN) and
  337. SearchType(expr,typesize) then
  338. begin
  339. oper.hastype:=true;
  340. Consume(AS_LPAREN);
  341. BuildOperand(oper);
  342. Consume(AS_RPAREN);
  343. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  344. oper.SetSize(typesize,true);
  345. end
  346. else
  347. begin
  348. if oper.SetupVar(expr,false) then
  349. ReadPercent(oper)
  350. else
  351. Begin
  352. { look for special symbols ... }
  353. if expr= '__HIGH' then
  354. begin
  355. consume(AS_LPAREN);
  356. if not oper.setupvar('high'+actasmpattern,false) then
  357. Message1(sym_e_unknown_id,'high'+actasmpattern);
  358. consume(AS_ID);
  359. consume(AS_RPAREN);
  360. end
  361. else
  362. if expr = '__RESULT' then
  363. oper.SetUpResult
  364. else
  365. if expr = '__SELF' then
  366. oper.SetupSelf
  367. else
  368. if expr = '__OLDEBP' then
  369. oper.SetupOldEBP
  370. else
  371. { check for direct symbolic names }
  372. { only if compiling the system unit }
  373. if (cs_compilesystem in aktmoduleswitches) then
  374. begin
  375. if not oper.SetupDirectVar(expr) then
  376. Begin
  377. { not found, finally ... add it anyways ... }
  378. Message1(asmr_w_id_supposed_external,expr);
  379. oper.InitRef;
  380. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(expr);
  381. end;
  382. end
  383. else
  384. Message1(sym_e_unknown_id,expr);
  385. end;
  386. end;
  387. end;
  388. if actasmtoken=AS_DOT then
  389. MaybeRecordOffset;
  390. { add a constant expression? }
  391. if (actasmtoken=AS_PLUS) then
  392. begin
  393. l:=BuildConstExpression(true,false);
  394. case oper.opr.typ of
  395. OPR_CONSTANT :
  396. inc(oper.opr.val,l);
  397. OPR_LOCAL :
  398. inc(oper.opr.localsymofs,l);
  399. OPR_REFERENCE :
  400. inc(oper.opr.ref.offset,l);
  401. else
  402. internalerror(200309202);
  403. end;
  404. end
  405. end;
  406. end;
  407. AS_REGISTER: { Register, a variable reference or a constant reference }
  408. Begin
  409. { save the type of register used. }
  410. tempreg:=actasmregister;
  411. Consume(AS_REGISTER);
  412. if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  413. begin
  414. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  415. Message(asmr_e_invalid_operand_type);
  416. oper.opr.typ:=OPR_REGISTER;
  417. oper.opr.reg:=tempreg;
  418. end
  419. else
  420. Message(asmr_e_syn_operand);
  421. end;
  422. AS_END,
  423. AS_SEPARATOR,
  424. AS_COMMA: ;
  425. else
  426. Begin
  427. Message(asmr_e_syn_operand);
  428. Consume(actasmtoken);
  429. end;
  430. end; { end case }
  431. end;
  432. {*****************************************************************************
  433. tSparcReader
  434. *****************************************************************************}
  435. procedure tSparcReader.BuildOpCode(instr : tSparcinstruction);
  436. var
  437. operandnum : longint;
  438. Begin
  439. { opcode }
  440. if (actasmtoken<>AS_OPCODE) then
  441. Begin
  442. Message(asmr_e_invalid_or_missing_opcode);
  443. RecoverConsume(true);
  444. exit;
  445. end;
  446. { Fill the instr object with the current state }
  447. with instr do
  448. begin
  449. Opcode:=ActOpcode;
  450. condition:=ActCondition;
  451. end;
  452. { We are reading operands, so opcode will be an AS_ID }
  453. operandnum:=1;
  454. Consume(AS_OPCODE);
  455. { Zero operand opcode ? }
  456. if actasmtoken in [AS_SEPARATOR,AS_END] then
  457. begin
  458. operandnum:=0;
  459. exit;
  460. end;
  461. { Read the operands }
  462. repeat
  463. case actasmtoken of
  464. AS_COMMA: { Operand delimiter }
  465. Begin
  466. if operandnum>Max_Operands then
  467. Message(asmr_e_too_many_operands)
  468. else
  469. begin
  470. { condition operands doesn't set the operand but write to the
  471. condition field of the instruction
  472. }
  473. if instr.Operands[operandnum].opr.typ<>OPR_NONE then
  474. Inc(operandnum);
  475. end;
  476. Consume(AS_COMMA);
  477. end;
  478. AS_SEPARATOR,
  479. AS_END : { End of asm operands for this opcode }
  480. begin
  481. break;
  482. end;
  483. else
  484. BuildOperand(instr.Operands[operandnum] as tSparcoperand);
  485. end; { end case }
  486. until false;
  487. if (operandnum=1) and (instr.Operands[operandnum].opr.typ=OPR_NONE) then
  488. dec(operandnum);
  489. instr.Ops:=operandnum;
  490. end;
  491. function TSparcReader.is_asmopcode(const s: string):boolean;
  492. var
  493. str2opEntry: tstr2opEntry;
  494. cond:TAsmCond;
  495. Begin
  496. { making s a value parameter would break other assembler readers }
  497. is_asmopcode:=false;
  498. { clear op code }
  499. actopcode:=A_None;
  500. { clear condition }
  501. fillchar(actcondition,sizeof(actcondition),0);
  502. str2opentry:=tstr2opentry(iasmops.search(s));
  503. if assigned(str2opentry) then
  504. begin
  505. actopcode:=str2opentry.op;
  506. actasmtoken:=AS_OPCODE;
  507. is_asmopcode:=true;
  508. end
  509. { not found, check branch instructions }
  510. else if (Upcase(s[1])='B') or
  511. ((Upcase(s[1])='F') and (Upcase(s[2])='B')) then
  512. begin
  513. { we can search here without an extra table which is sorted by string length
  514. because we take the whole remaining string without the leading B }
  515. if (Upcase(s[1])='F') then
  516. actopcode := A_FBxx
  517. else
  518. actopcode := A_Bxx;
  519. for cond:=low(TAsmCond) to high(TAsmCond) do
  520. if (Upper(copy(s,2,length(s)-1))=Upper(Cond2Str[cond])) then
  521. begin
  522. actasmtoken:=AS_OPCODE;
  523. is_asmopcode:=true;
  524. end;
  525. end;
  526. end;
  527. procedure tSparcReader.ConvertCalljmp(instr : tSparcinstruction);
  528. var
  529. newopr : toprrec;
  530. begin
  531. if instr.Operands[1].opr.typ=OPR_REFERENCE then
  532. with newopr do
  533. begin
  534. typ:=OPR_SYMBOL;
  535. symbol:=instr.Operands[1].opr.ref.symbol;
  536. symofs:=instr.Operands[1].opr.ref.offset;
  537. if (instr.Operands[1].opr.ref.base<>NR_NO) or
  538. (instr.Operands[1].opr.ref.index<>NR_NO) or
  539. (instr.Operands[1].opr.ref.refaddr<>addr_full) then
  540. Message(asmr_e_syn_operand);
  541. instr.Operands[1].opr:=newopr;
  542. end;
  543. end;
  544. procedure tSparcReader.handleopcode;
  545. var
  546. instr : tSparcinstruction;
  547. begin
  548. instr:=TSparcInstruction.Create(TSparcOperand);
  549. BuildOpcode(instr);
  550. with instr do
  551. begin
  552. condition := actcondition;
  553. if is_calljmp(opcode) then
  554. ConvertCalljmp(instr);
  555. ConcatInstruction(curlist);
  556. Free;
  557. end;
  558. end;
  559. {*****************************************************************************
  560. Initialize
  561. *****************************************************************************}
  562. const
  563. asmmode_Sparc_att_info : tasmmodeinfo =
  564. (
  565. id : asmmode_Sparc_gas;
  566. idtxt : 'GAS';
  567. casmreader : tSparcReader;
  568. );
  569. asmmode_Sparc_standard_info : tasmmodeinfo =
  570. (
  571. id : asmmode_standard;
  572. idtxt : 'STANDARD';
  573. casmreader : tSparcReader;
  574. );
  575. initialization
  576. RegisterAsmMode(asmmode_Sparc_att_info);
  577. RegisterAsmMode(asmmode_Sparc_standard_info);
  578. end.
  579. {
  580. $Log$
  581. Revision 1.7 2004-02-27 13:27:28 mazen
  582. * symaddr ==> refaddr to follow the rest of compiler changes
  583. Revision 1.6 2004/01/12 22:11:39 peter
  584. * use localalign info for alignment for locals and temps
  585. * sparc fpu flags branching added
  586. * moved powerpc copy_valye_openarray to generic
  587. Revision 1.5 2004/01/12 16:39:41 peter
  588. * sparc updates, mostly float related
  589. Revision 1.4 2003/12/26 14:02:30 peter
  590. * sparc updates
  591. * use registertype in spill_register
  592. Revision 1.3 2003/12/25 01:25:43 peter
  593. * sparc assembler reader updates
  594. Revision 1.2 2003/12/10 13:16:36 mazen
  595. * improve hadlign %hi and %lo operators
  596. Revision 1.1 2003/12/08 13:02:21 mazen
  597. + support for native sparc assembler reader
  598. }