rappcgas.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  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 rappcgas;
  19. {$i fpcdefs.inc}
  20. Interface
  21. uses
  22. raatt,rappc;
  23. type
  24. tppcattreader = class(tattreader)
  25. function is_asmopcode(const s: string):boolean;override;
  26. procedure handleopcode;override;
  27. procedure BuildReference(oper : tppcoperand);
  28. procedure BuildOperand(oper : tppcoperand);
  29. procedure BuildOpCode(instr : tppcinstruction);
  30. end;
  31. Implementation
  32. uses
  33. { helpers }
  34. cutils,
  35. { global }
  36. globtype,globals,verbose,
  37. systems,
  38. { aasm }
  39. cpubase,cpuinfo,aasmbase,aasmtai,aasmcpu,
  40. { symtable }
  41. symconst,symbase,symtype,symsym,symtable,
  42. { parser }
  43. scanner,
  44. procinfo,
  45. itcpugas,
  46. rabase,rautils,
  47. cgbase,cgobj
  48. ;
  49. Procedure tppcattreader.BuildReference(oper : tppcoperand);
  50. procedure Consume_RParen;
  51. begin
  52. if actasmtoken <> AS_RPAREN then
  53. Begin
  54. Message(asmr_e_invalid_reference_syntax);
  55. RecoverConsume(true);
  56. end
  57. else
  58. begin
  59. Consume(AS_RPAREN);
  60. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  61. Begin
  62. Message(asmr_e_invalid_reference_syntax);
  63. RecoverConsume(true);
  64. end;
  65. end;
  66. end;
  67. begin
  68. oper.InitRef;
  69. Consume(AS_LPAREN);
  70. Case actasmtoken of
  71. AS_INTNUM,
  72. AS_MINUS,
  73. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  74. Begin
  75. { offset(offset) is invalid }
  76. If oper.opr.Ref.Offset <> 0 Then
  77. Begin
  78. Message(asmr_e_invalid_reference_syntax);
  79. RecoverConsume(true);
  80. End
  81. Else
  82. Begin
  83. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  84. Consume_RParen;
  85. end;
  86. exit;
  87. End;
  88. AS_REGISTER: { (reg ... }
  89. Begin
  90. { Check if there is already a base (mostly ebp,esp) than this is
  91. not allowed, because it will give crashing code }
  92. if ((oper.opr.typ=OPR_REFERENCE) and (oper.opr.ref.base<>NR_NO)) or
  93. ((oper.opr.typ=OPR_LOCAL) and (oper.opr.localsym.localloc.loc<>LOC_REGISTER)) then
  94. message(asmr_e_cannot_index_relative_var);
  95. oper.opr.ref.base:=actasmregister;
  96. Consume(AS_REGISTER);
  97. { can either be a register or a right parenthesis }
  98. { (reg) }
  99. if actasmtoken=AS_RPAREN then
  100. Begin
  101. Consume_RParen;
  102. exit;
  103. end;
  104. { (reg,reg .. }
  105. Consume(AS_COMMA);
  106. if actasmtoken=AS_REGISTER then
  107. Begin
  108. oper.opr.ref.index:=actasmregister;
  109. Consume(AS_REGISTER);
  110. Consume_RParen;
  111. end
  112. else
  113. Begin
  114. Message(asmr_e_invalid_reference_syntax);
  115. RecoverConsume(false);
  116. end;
  117. end; {end case }
  118. AS_COMMA: { (, ... can either be scaling, or index }
  119. Begin
  120. Consume(AS_COMMA);
  121. { Index }
  122. if (actasmtoken=AS_REGISTER) then
  123. Begin
  124. oper.opr.ref.index:=actasmregister;
  125. Consume(AS_REGISTER);
  126. { check for scaling ... }
  127. Consume_RParen;
  128. end
  129. else
  130. begin
  131. Message(asmr_e_invalid_reference_syntax);
  132. RecoverConsume(false);
  133. end;
  134. end;
  135. else
  136. Begin
  137. Message(asmr_e_invalid_reference_syntax);
  138. RecoverConsume(false);
  139. end;
  140. end;
  141. end;
  142. Procedure tppcattreader.BuildOperand(oper : tppcoperand);
  143. var
  144. tempstr,
  145. expr : string;
  146. typesize,
  147. l,k : longint;
  148. procedure AddLabelOperand(hl:tasmlabel);
  149. begin
  150. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  151. is_calljmp(actopcode) then
  152. begin
  153. oper.opr.typ:=OPR_SYMBOL;
  154. oper.opr.symbol:=hl;
  155. end
  156. else
  157. begin
  158. oper.InitRef;
  159. oper.opr.ref.symbol:=hl;
  160. end;
  161. end;
  162. procedure MaybeRecordOffset;
  163. var
  164. hasdot : boolean;
  165. l,
  166. toffset,
  167. tsize : longint;
  168. begin
  169. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  170. exit;
  171. l:=0;
  172. hasdot:=(actasmtoken=AS_DOT);
  173. if hasdot then
  174. begin
  175. if expr<>'' then
  176. begin
  177. BuildRecordOffsetSize(expr,toffset,tsize);
  178. inc(l,toffset);
  179. oper.SetSize(tsize,true);
  180. end;
  181. end;
  182. if actasmtoken in [AS_PLUS,AS_MINUS] then
  183. inc(l,BuildConstExpression(true,false));
  184. case oper.opr.typ of
  185. OPR_LOCAL :
  186. begin
  187. { don't allow direct access to fields of parameters, because that
  188. will generate buggy code. Allow it only for explicit typecasting }
  189. if hasdot and
  190. (not oper.hastype) and
  191. (tvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  192. (current_procinfo.procdef.proccalloption<>pocall_register) then
  193. Message(asmr_e_cannot_access_field_directly_for_parameters);
  194. inc(oper.opr.localsymofs,l)
  195. end;
  196. OPR_CONSTANT :
  197. inc(oper.opr.val,l);
  198. OPR_REFERENCE :
  199. inc(oper.opr.ref.offset,l);
  200. else
  201. internalerror(200309221);
  202. end;
  203. end;
  204. function MaybeBuildReference:boolean;
  205. { Try to create a reference, if not a reference is found then false
  206. is returned }
  207. begin
  208. MaybeBuildReference:=true;
  209. case actasmtoken of
  210. AS_INTNUM,
  211. AS_MINUS,
  212. AS_PLUS:
  213. Begin
  214. oper.opr.ref.offset:=BuildConstExpression(True,False);
  215. if actasmtoken<>AS_LPAREN then
  216. Message(asmr_e_invalid_reference_syntax)
  217. else
  218. BuildReference(oper);
  219. end;
  220. AS_LPAREN:
  221. BuildReference(oper);
  222. AS_ID: { only a variable is allowed ... }
  223. Begin
  224. tempstr:=actasmpattern;
  225. Consume(AS_ID);
  226. { typecasting? }
  227. if (actasmtoken=AS_LPAREN) and
  228. SearchType(tempstr,typesize) then
  229. begin
  230. oper.hastype:=true;
  231. Consume(AS_LPAREN);
  232. BuildOperand(oper);
  233. Consume(AS_RPAREN);
  234. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  235. oper.SetSize(typesize,true);
  236. end
  237. else
  238. if not oper.SetupVar(tempstr,false) then
  239. Message1(sym_e_unknown_id,tempstr);
  240. { record.field ? }
  241. if actasmtoken=AS_DOT then
  242. begin
  243. BuildRecordOffsetSize(tempstr,l,k);
  244. inc(oper.opr.ref.offset,l);
  245. end;
  246. case actasmtoken of
  247. AS_END,
  248. AS_SEPARATOR,
  249. AS_COMMA: ;
  250. AS_LPAREN:
  251. BuildReference(oper);
  252. else
  253. Begin
  254. Message(asmr_e_invalid_reference_syntax);
  255. Consume(actasmtoken);
  256. end;
  257. end; {end case }
  258. end;
  259. else
  260. MaybeBuildReference:=false;
  261. end; { end case }
  262. end;
  263. var
  264. tempreg : tregister;
  265. hl : tasmlabel;
  266. Begin
  267. expr:='';
  268. case actasmtoken of
  269. AS_LPAREN: { Memory reference or constant expression }
  270. Begin
  271. oper.InitRef;
  272. BuildReference(oper);
  273. end;
  274. AS_DOLLAR: { Constant expression }
  275. Begin
  276. Consume(AS_DOLLAR);
  277. BuildConstantOperand(oper);
  278. end;
  279. AS_INTNUM,
  280. AS_MINUS,
  281. AS_PLUS:
  282. Begin
  283. { Constant memory offset }
  284. { This must absolutely be followed by ( }
  285. oper.InitRef;
  286. oper.opr.ref.offset:=BuildConstExpression(True,False);
  287. if actasmtoken<>AS_LPAREN then
  288. BuildConstantOperand(oper)
  289. else
  290. BuildReference(oper);
  291. end;
  292. AS_ID: { A constant expression, or a Variable ref. }
  293. Begin
  294. { Local Label ? }
  295. if is_locallabel(actasmpattern) then
  296. begin
  297. CreateLocalLabel(actasmpattern,hl,false);
  298. Consume(AS_ID);
  299. AddLabelOperand(hl);
  300. end
  301. else
  302. { Check for label }
  303. if SearchLabel(actasmpattern,hl,false) then
  304. begin
  305. Consume(AS_ID);
  306. AddLabelOperand(hl);
  307. end
  308. else
  309. { probably a variable or normal expression }
  310. { or a procedure (such as in CALL ID) }
  311. Begin
  312. { is it a constant ? }
  313. if SearchIConstant(actasmpattern,l) then
  314. Begin
  315. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  316. Message(asmr_e_invalid_operand_type);
  317. BuildConstantOperand(oper);
  318. end
  319. else
  320. begin
  321. expr:=actasmpattern;
  322. Consume(AS_ID);
  323. { typecasting? }
  324. if (actasmtoken=AS_LPAREN) and
  325. SearchType(expr,typesize) then
  326. begin
  327. oper.hastype:=true;
  328. Consume(AS_LPAREN);
  329. BuildOperand(oper);
  330. Consume(AS_RPAREN);
  331. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  332. oper.SetSize(typesize,true);
  333. end
  334. else
  335. begin
  336. if oper.SetupVar(expr,false) then
  337. begin
  338. { check for ...@ }
  339. if actasmtoken=AS_AT then
  340. begin
  341. if oper.opr.ref.symbol=nil then
  342. Message(asmr_e_invalid_reference_syntax);
  343. Consume(AS_AT);
  344. if actasmtoken=AS_ID then
  345. begin
  346. if upper(actasmpattern)='L' then
  347. oper.opr.ref.symaddr:=refs_l
  348. else if upper(actasmpattern)='HA' then
  349. oper.opr.ref.symaddr:=refs_ha
  350. else
  351. Message(asmr_e_invalid_reference_syntax);
  352. Consume(AS_ID);
  353. end
  354. else
  355. Message(asmr_e_invalid_reference_syntax);
  356. end;
  357. end
  358. else
  359. Begin
  360. { look for special symbols ... }
  361. if expr= '__HIGH' then
  362. begin
  363. consume(AS_LPAREN);
  364. if not oper.setupvar('high'+actasmpattern,false) then
  365. Message1(sym_e_unknown_id,'high'+actasmpattern);
  366. consume(AS_ID);
  367. consume(AS_RPAREN);
  368. end
  369. else
  370. if expr = '__RESULT' then
  371. oper.SetUpResult
  372. else
  373. if expr = '__SELF' then
  374. oper.SetupSelf
  375. else
  376. if expr = '__OLDEBP' then
  377. oper.SetupOldEBP
  378. else
  379. { check for direct symbolic names }
  380. { only if compiling the system unit }
  381. if (cs_compilesystem in aktmoduleswitches) then
  382. begin
  383. if not oper.SetupDirectVar(expr) then
  384. Begin
  385. { not found, finally ... add it anyways ... }
  386. Message1(asmr_w_id_supposed_external,expr);
  387. oper.InitRef;
  388. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(expr);
  389. end;
  390. end
  391. else
  392. Message1(sym_e_unknown_id,expr);
  393. end;
  394. end;
  395. end;
  396. if actasmtoken=AS_DOT then
  397. MaybeRecordOffset;
  398. { add a constant expression? }
  399. if (actasmtoken=AS_PLUS) then
  400. begin
  401. l:=BuildConstExpression(true,false);
  402. case oper.opr.typ of
  403. OPR_CONSTANT :
  404. inc(oper.opr.val,l);
  405. OPR_LOCAL :
  406. inc(oper.opr.localsymofs,l);
  407. OPR_REFERENCE :
  408. inc(oper.opr.ref.offset,l);
  409. else
  410. internalerror(200309202);
  411. end;
  412. end
  413. end;
  414. { Do we have a indexing reference, then parse it also }
  415. if actasmtoken=AS_LPAREN then
  416. BuildReference(oper);
  417. end;
  418. AS_REGISTER: { Register, a variable reference or a constant reference }
  419. Begin
  420. { save the type of register used. }
  421. tempreg:=actasmregister;
  422. Consume(AS_REGISTER);
  423. if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  424. Begin
  425. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  426. Message(asmr_e_invalid_operand_type);
  427. oper.opr.typ:=OPR_REGISTER;
  428. oper.opr.reg:=tempreg;
  429. end
  430. else if is_condreg(tempreg) then
  431. begin
  432. if actasmtoken=AS_STAR then
  433. begin
  434. consume(AS_STAR);
  435. if (actasmtoken=AS_INTNUM) and (actasmpattern='4') then
  436. begin
  437. end
  438. else
  439. Message(asmr_e_syn_operand);
  440. end
  441. else
  442. Message(asmr_e_syn_operand);
  443. end
  444. else
  445. Message(asmr_e_syn_operand);
  446. end;
  447. AS_END,
  448. AS_SEPARATOR,
  449. AS_COMMA: ;
  450. else
  451. Begin
  452. Message(asmr_e_syn_operand);
  453. Consume(actasmtoken);
  454. end;
  455. end; { end case }
  456. end;
  457. {*****************************************************************************
  458. tppcattreader
  459. *****************************************************************************}
  460. procedure tppcattreader.BuildOpCode(instr : tppcinstruction);
  461. var
  462. operandnum : longint;
  463. Begin
  464. { opcode }
  465. if (actasmtoken<>AS_OPCODE) then
  466. Begin
  467. Message(asmr_e_invalid_or_missing_opcode);
  468. RecoverConsume(true);
  469. exit;
  470. end;
  471. { Fill the instr object with the current state }
  472. with instr do
  473. begin
  474. Opcode:=ActOpcode;
  475. condition:=ActCondition;
  476. end;
  477. { We are reading operands, so opcode will be an AS_ID }
  478. operandnum:=1;
  479. Consume(AS_OPCODE);
  480. { Zero operand opcode ? }
  481. if actasmtoken in [AS_SEPARATOR,AS_END] then
  482. begin
  483. operandnum:=0;
  484. exit;
  485. end;
  486. { Read the operands }
  487. repeat
  488. case actasmtoken of
  489. AS_COMMA: { Operand delimiter }
  490. Begin
  491. if operandnum > Max_Operands then
  492. Message(asmr_e_too_many_operands)
  493. else
  494. Inc(operandnum);
  495. Consume(AS_COMMA);
  496. end;
  497. AS_SEPARATOR,
  498. AS_END : { End of asm operands for this opcode }
  499. begin
  500. break;
  501. end;
  502. else
  503. BuildOperand(instr.Operands[operandnum] as tppcoperand);
  504. end; { end case }
  505. until false;
  506. instr.Ops:=operandnum;
  507. end;
  508. function tppcattreader.is_asmopcode(const s: string):boolean;
  509. var
  510. str2opentry: tstr2opentry;
  511. cond : tasmcondflag;
  512. len,
  513. j,
  514. sufidx : longint;
  515. hs : string;
  516. Begin
  517. { making s a value parameter would break other assembler readers }
  518. hs:=s;
  519. is_asmopcode:=false;
  520. actopcode:=A_None;
  521. actcondition.cond:=C_None;
  522. { check for direction hint }
  523. if hs[length(s)]='-' then
  524. begin
  525. dec(ord(hs[0]));
  526. actcondition.dirhint:=DH_Minus;
  527. end;
  528. str2opentry:=tstr2opentry(iasmops.search(copy(hs,1,len)));
  529. if assigned(str2opentry) then
  530. begin
  531. if actcondition.dirhint<>DH_None then
  532. message1(asmr_e_unknown_opcode,actasmpattern);
  533. actopcode:=str2opentry.op;
  534. actasmtoken:=AS_OPCODE;
  535. is_asmopcode:=true;
  536. exit;
  537. end;
  538. { not found, check branch instructions }
  539. if hs[1]='B' then
  540. begin
  541. { we can search here without an extra table which is sorted by string length
  542. because we take the whole remaining string without the leading B }
  543. for cond:=low(TAsmCondFlag) to high(TAsmCondFlag) do
  544. if copy(hs,2,length(s)-1)=UpperAsmCondFlag2Str[cond] then
  545. begin
  546. actopcode:=A_B;
  547. actcondition.simple:=true;
  548. actcondition.cond:=cond;
  549. actasmtoken:=AS_OPCODE;
  550. is_asmopcode:=true;
  551. exit;
  552. end;
  553. end;
  554. end;
  555. procedure tppcattreader.handleopcode;
  556. var
  557. instr : tppcinstruction;
  558. begin
  559. instr:=TPPCInstruction.Create(TPPCOperand);
  560. BuildOpcode(instr);
  561. {
  562. instr.AddReferenceSizes;
  563. instr.SetInstructionOpsize;
  564. instr.CheckOperandSizes;
  565. }
  566. instr.ConcatInstruction(curlist);
  567. instr.Free;
  568. end;
  569. {*****************************************************************************
  570. Initialize
  571. *****************************************************************************}
  572. const
  573. asmmode_ppc_att_info : tasmmodeinfo =
  574. (
  575. id : asmmode_ppc_gas;
  576. idtxt : 'GAS';
  577. casmreader : tppcattreader;
  578. );
  579. asmmode_ppc_standard_info : tasmmodeinfo =
  580. (
  581. id : asmmode_standard;
  582. idtxt : 'STANDARD';
  583. casmreader : tppcattreader;
  584. );
  585. initialization
  586. RegisterAsmMode(asmmode_ppc_att_info);
  587. RegisterAsmMode(asmmode_ppc_standard_info);
  588. end.
  589. {
  590. $Log$
  591. Revision 1.2 2003-11-12 16:05:40 florian
  592. * assembler readers OOPed
  593. + typed currency constants
  594. + typed 128 bit float constants if the CPU supports it
  595. Revision 1.1 2003/11/06 20:48:02 florian
  596. * initial revision
  597. }