rappcgas.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 rappcatt;
  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. end
  339. else
  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. { check for direct symbolic names }
  361. { only if compiling the system unit }
  362. if (cs_compilesystem in aktmoduleswitches) then
  363. begin
  364. if not oper.SetupDirectVar(expr) then
  365. Begin
  366. { not found, finally ... add it anyways ... }
  367. Message1(asmr_w_id_supposed_external,expr);
  368. oper.InitRef;
  369. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(expr);
  370. end;
  371. end
  372. else
  373. Message1(sym_e_unknown_id,expr);
  374. end;
  375. end;
  376. end;
  377. if actasmtoken=AS_DOT then
  378. MaybeRecordOffset;
  379. { add a constant expression? }
  380. if (actasmtoken=AS_PLUS) then
  381. begin
  382. l:=BuildConstExpression(true,false);
  383. case oper.opr.typ of
  384. OPR_CONSTANT :
  385. inc(oper.opr.val,l);
  386. OPR_LOCAL :
  387. inc(oper.opr.localsymofs,l);
  388. OPR_REFERENCE :
  389. inc(oper.opr.ref.offset,l);
  390. else
  391. internalerror(200309202);
  392. end;
  393. end
  394. end;
  395. { Do we have a indexing reference, then parse it also }
  396. if actasmtoken=AS_LPAREN then
  397. BuildReference(oper);
  398. end;
  399. AS_REGISTER: { Register, a variable reference or a constant reference }
  400. Begin
  401. { save the type of register used. }
  402. tempreg:=actasmregister;
  403. Consume(AS_REGISTER);
  404. if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  405. Begin
  406. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  407. Message(asmr_e_invalid_operand_type);
  408. oper.opr.typ:=OPR_REGISTER;
  409. oper.opr.reg:=tempreg;
  410. end
  411. else
  412. Message(asmr_e_syn_operand);
  413. end;
  414. AS_END,
  415. AS_SEPARATOR,
  416. AS_COMMA: ;
  417. else
  418. Begin
  419. Message(asmr_e_syn_operand);
  420. Consume(actasmtoken);
  421. end;
  422. end; { end case }
  423. end;
  424. {*****************************************************************************
  425. tppcattreader
  426. *****************************************************************************}
  427. procedure tppcattreader.BuildOpCode(instr : tppcinstruction);
  428. var
  429. operandnum : longint;
  430. Begin
  431. { opcode }
  432. if (actasmtoken<>AS_OPCODE) then
  433. Begin
  434. Message(asmr_e_invalid_or_missing_opcode);
  435. RecoverConsume(true);
  436. exit;
  437. end;
  438. { Fill the instr object with the current state }
  439. with instr do
  440. begin
  441. Opcode:=ActOpcode;
  442. condition:=ActCondition;
  443. end;
  444. { We are reading operands, so opcode will be an AS_ID }
  445. operandnum:=1;
  446. Consume(AS_OPCODE);
  447. { Zero operand opcode ? }
  448. if actasmtoken in [AS_SEPARATOR,AS_END] then
  449. begin
  450. operandnum:=0;
  451. exit;
  452. end;
  453. { Read the operands }
  454. repeat
  455. case actasmtoken of
  456. AS_COMMA: { Operand delimiter }
  457. Begin
  458. if operandnum > Max_Operands then
  459. Message(asmr_e_too_many_operands)
  460. else
  461. Inc(operandnum);
  462. Consume(AS_COMMA);
  463. end;
  464. AS_SEPARATOR,
  465. AS_END : { End of asm operands for this opcode }
  466. begin
  467. break;
  468. end;
  469. else
  470. BuildOperand(instr.Operands[operandnum] as tppcoperand);
  471. end; { end case }
  472. until false;
  473. instr.Ops:=operandnum;
  474. end;
  475. function tppcattreader.is_asmopcode(const s: string):boolean;
  476. var
  477. str2opentry: tstr2opentry;
  478. cond : tasmcondflag;
  479. len,
  480. j,
  481. sufidx : longint;
  482. hs : string;
  483. Begin
  484. { making s a value parameter would break other assembler readers }
  485. hs:=s;
  486. is_asmopcode:=false;
  487. actopcode:=A_None;
  488. actcondition.cond:=C_None;
  489. { check for direction hint }
  490. if hs[length(s)]='-' then
  491. begin
  492. dec(ord(hs[0]));
  493. actcondition.dirhint:=DH_Minus;
  494. end;
  495. str2opentry:=tstr2opentry(iasmops.search(copy(hs,1,len)));
  496. if assigned(str2opentry) then
  497. begin
  498. if actcondition.dirhint<>DH_None then
  499. message1(asmr_e_unknown_opcode,actasmpattern);
  500. actopcode:=str2opentry.op;
  501. actasmtoken:=AS_OPCODE;
  502. is_asmopcode:=true;
  503. exit;
  504. end;
  505. { not found, check branch instructions }
  506. if hs[1]='B' then
  507. begin
  508. { we can search here without an extra table which is sorted by string length
  509. because we take the whole remaining string without the leading B }
  510. for cond:=low(TAsmCondFlag) to high(TAsmCondFlag) do
  511. if copy(hs,2,length(s)-1)=UpperAsmCondFlag2Str[cond] then
  512. begin
  513. actopcode:=A_B;
  514. actcondition.simple:=true;
  515. actcondition.cond:=cond;
  516. actasmtoken:=AS_OPCODE;
  517. is_asmopcode:=true;
  518. exit;
  519. end;
  520. end;
  521. end;
  522. procedure tppcattreader.handleopcode;
  523. var
  524. instr : tppcinstruction;
  525. begin
  526. instr:=TPPCInstruction.Create(TPPCOperand);
  527. BuildOpcode(instr);
  528. {
  529. instr.AddReferenceSizes;
  530. instr.SetInstructionOpsize;
  531. instr.CheckOperandSizes;
  532. }
  533. instr.ConcatInstruction(curlist);
  534. instr.Free;
  535. end;
  536. {*****************************************************************************
  537. Initialize
  538. *****************************************************************************}
  539. const
  540. asmmode_ppc_att_info : tasmmodeinfo =
  541. (
  542. id : asmmode_ppc_gas;
  543. idtxt : 'GAS';
  544. casmreader : tppcattreader;
  545. );
  546. asmmode_ppc_standard_info : tasmmodeinfo =
  547. (
  548. id : asmmode_standard;
  549. idtxt : 'STANDARD';
  550. casmreader : tppcattreader;
  551. );
  552. initialization
  553. RegisterAsmMode(asmmode_ppc_att_info);
  554. RegisterAsmMode(asmmode_ppc_standard_info);
  555. end.
  556. {
  557. $Log$
  558. Revision 1.1 2003-11-06 20:48:02 florian
  559. * initial revision
  560. }