rax86att.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing for the x86 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 rax86att;
  18. {$i fpcdefs.inc}
  19. Interface
  20. uses
  21. cpubase,
  22. raatt,rax86;
  23. type
  24. tx86attreader = class(tattreader)
  25. ActOpsize : topsize;
  26. function is_asmopcode(const s: string):boolean;override;
  27. procedure handleopcode;override;
  28. procedure BuildReference(oper : tx86operand);
  29. procedure BuildOperand(oper : tx86operand);
  30. procedure BuildOpCode(instr : tx86instruction);
  31. procedure handlepercent;override;
  32. protected
  33. procedure MaybeGetPICModifier(var oper: tx86operand);
  34. end;
  35. Implementation
  36. uses
  37. { helpers }
  38. cutils,
  39. { global }
  40. globtype,verbose,
  41. systems,
  42. { aasm }
  43. aasmbase,aasmtai,aasmdata,aasmcpu,
  44. { symtable }
  45. symconst,
  46. { parser }
  47. scanner,
  48. procinfo,
  49. itcpugas,
  50. rabase,rautils,
  51. cgbase
  52. ;
  53. procedure tx86attreader.handlepercent;
  54. var
  55. len : longint;
  56. begin
  57. len:=1;
  58. actasmpattern[len]:='%';
  59. c:=current_scanner.asmgetchar;
  60. { to be a register there must be a letter and not a number }
  61. if c in ['0'..'9'] then
  62. begin
  63. actasmtoken:=AS_MOD;
  64. end
  65. else
  66. begin
  67. while c in ['a'..'z','A'..'Z','0'..'9'] do
  68. Begin
  69. inc(len);
  70. actasmpattern[len]:=c;
  71. c:=current_scanner.asmgetchar;
  72. end;
  73. actasmpattern[0]:=chr(len);
  74. uppervar(actasmpattern);
  75. if (actasmpattern = '%ST') and (c='(') then
  76. Begin
  77. actasmpattern:=actasmpattern+c;
  78. c:=current_scanner.asmgetchar;
  79. if c in ['0'..'9'] then
  80. actasmpattern:=actasmpattern + c
  81. else
  82. Message(asmr_e_invalid_fpu_register);
  83. c:=current_scanner.asmgetchar;
  84. if c <> ')' then
  85. Message(asmr_e_invalid_fpu_register)
  86. else
  87. Begin
  88. actasmpattern:=actasmpattern + c;
  89. c:=current_scanner.asmgetchar; { let us point to next character. }
  90. end;
  91. end;
  92. if is_register(actasmpattern) then
  93. exit;
  94. Message(asmr_e_invalid_register);
  95. actasmtoken:=raatt.AS_NONE;
  96. end;
  97. end;
  98. Procedure tx86attreader.BuildReference(oper : tx86operand);
  99. procedure Consume_RParen;
  100. begin
  101. if actasmtoken <> AS_RPAREN then
  102. Begin
  103. Message(asmr_e_invalid_reference_syntax);
  104. RecoverConsume(true);
  105. end
  106. else
  107. begin
  108. Consume(AS_RPAREN);
  109. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  110. Begin
  111. Message(asmr_e_invalid_reference_syntax);
  112. RecoverConsume(true);
  113. end;
  114. end;
  115. end;
  116. procedure Consume_Scale;
  117. var
  118. l : aint;
  119. begin
  120. { we have to process the scaling }
  121. l:=BuildConstExpression(false,true);
  122. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  123. oper.opr.ref.scalefactor:=l
  124. else
  125. Begin
  126. Message(asmr_e_wrong_scale_factor);
  127. oper.opr.ref.scalefactor:=0;
  128. end;
  129. end;
  130. begin
  131. oper.InitRef;
  132. Consume(AS_LPAREN);
  133. Case actasmtoken of
  134. AS_INTNUM,
  135. AS_MINUS,
  136. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  137. Begin
  138. { offset(offset) is invalid }
  139. If oper.opr.Ref.Offset <> 0 Then
  140. Begin
  141. Message(asmr_e_invalid_reference_syntax);
  142. RecoverConsume(true);
  143. End
  144. Else
  145. Begin
  146. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  147. Consume_RParen;
  148. end;
  149. exit;
  150. End;
  151. AS_REGISTER: { (reg ... }
  152. Begin
  153. { Check if there is already a base (mostly ebp,esp) than this is
  154. not allowed, because it will give crashing code }
  155. if ((oper.opr.typ=OPR_REFERENCE) and (oper.opr.ref.base<>NR_NO)) or
  156. ((oper.opr.typ=OPR_LOCAL) and (oper.opr.localsym.localloc.loc<>LOC_REGISTER)) then
  157. message(asmr_e_cannot_index_relative_var);
  158. oper.opr.ref.base:=actasmregister;
  159. {$ifdef x86_64}
  160. { non-GOT based RIP-relative accesses are also position-independent }
  161. if (oper.opr.ref.base=NR_RIP) and
  162. (oper.opr.ref.refaddr<>addr_pic) then
  163. oper.opr.ref.refaddr:=addr_pic_no_got;
  164. {$endif x86_64}
  165. Consume(AS_REGISTER);
  166. { can either be a register or a right parenthesis }
  167. { (reg) }
  168. if actasmtoken=AS_RPAREN then
  169. Begin
  170. Consume_RParen;
  171. exit;
  172. end;
  173. { (reg,reg .. }
  174. Consume(AS_COMMA);
  175. if actasmtoken=AS_REGISTER then
  176. Begin
  177. oper.opr.ref.index:=actasmregister;
  178. Consume(AS_REGISTER);
  179. { check for scaling ... }
  180. case actasmtoken of
  181. AS_RPAREN:
  182. Begin
  183. Consume_RParen;
  184. exit;
  185. end;
  186. AS_COMMA:
  187. Begin
  188. Consume(AS_COMMA);
  189. Consume_Scale;
  190. Consume_RParen;
  191. end;
  192. else
  193. Begin
  194. Message(asmr_e_invalid_reference_syntax);
  195. RecoverConsume(false);
  196. end;
  197. end; { end case }
  198. end
  199. else
  200. Begin
  201. Message(asmr_e_invalid_reference_syntax);
  202. RecoverConsume(false);
  203. end;
  204. end; {end case }
  205. AS_COMMA: { (, ... can either be scaling, or index }
  206. Begin
  207. Consume(AS_COMMA);
  208. { Index }
  209. if (actasmtoken=AS_REGISTER) then
  210. Begin
  211. oper.opr.ref.index:=actasmregister;
  212. Consume(AS_REGISTER);
  213. { check for scaling ... }
  214. case actasmtoken of
  215. AS_RPAREN:
  216. Begin
  217. Consume_RParen;
  218. exit;
  219. end;
  220. AS_COMMA:
  221. Begin
  222. Consume(AS_COMMA);
  223. Consume_Scale;
  224. Consume_RParen;
  225. end;
  226. else
  227. Begin
  228. Message(asmr_e_invalid_reference_syntax);
  229. RecoverConsume(false);
  230. end;
  231. end; {end case }
  232. end
  233. { Scaling }
  234. else
  235. Begin
  236. Consume_Scale;
  237. Consume_RParen;
  238. exit;
  239. end;
  240. end;
  241. else
  242. Begin
  243. Message(asmr_e_invalid_reference_syntax);
  244. RecoverConsume(false);
  245. end;
  246. end;
  247. end;
  248. Procedure tx86attreader.MaybeGetPICModifier(var oper: tx86operand);
  249. var
  250. relsym: string;
  251. asmsymtyp: tasmsymtype;
  252. l: aint;
  253. begin
  254. case actasmtoken of
  255. AS_AT:
  256. begin
  257. { darwin/i386 needs a relsym instead, and we can't }
  258. { generate this automatically }
  259. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  260. Message(asmr_e_invalid_reference_syntax);
  261. consume(AS_AT);
  262. if actasmtoken=AS_ID then
  263. begin
  264. {$ifdef x86_64}
  265. if (actasmpattern='GOTPCREL') or
  266. (actasmpattern='PLT') then
  267. {$endif x86_64}
  268. {$ifdef i386}
  269. if actasmpattern='GOT' then
  270. {$endif i386}
  271. begin
  272. oper.opr.ref.refaddr:=addr_pic;
  273. {$ifdef x86_64}
  274. { local symbols don't have to
  275. be accessed via the GOT
  276. }
  277. if (actasmpattern='GOTPCREL') and
  278. assigned(oper.opr.ref.symbol) and
  279. (oper.opr.ref.symbol.bind=AB_LOCAL) then
  280. Message(asmr_w_useless_got_for_local);
  281. {$endif x86_64}
  282. consume(AS_ID);
  283. end
  284. else
  285. Message(asmr_e_invalid_reference_syntax);
  286. end
  287. else
  288. Message(asmr_e_invalid_reference_syntax);
  289. end;
  290. AS_MINUS:
  291. begin
  292. { relsym? }
  293. Consume(AS_MINUS);
  294. BuildConstSymbolExpression(true,true,false,l,relsym,asmsymtyp);
  295. if (relsym<>'') then
  296. if not assigned(oper.opr.ref.relsymbol) then
  297. oper.opr.ref.relsymbol:=current_asmdata.RefAsmSymbol(relsym)
  298. else
  299. Message(asmr_e_invalid_reference_syntax)
  300. else
  301. dec(oper.opr.ref.offset,l);
  302. end;
  303. end;
  304. end;
  305. Procedure tx86attreader.BuildOperand(oper : tx86operand);
  306. var
  307. tempstr,
  308. expr : string;
  309. typesize,l,k : aint;
  310. procedure AddLabelOperand(hl:tasmlabel);
  311. begin
  312. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  313. is_calljmp(actopcode) then
  314. begin
  315. oper.opr.typ:=OPR_SYMBOL;
  316. oper.opr.symbol:=hl;
  317. end
  318. else
  319. begin
  320. oper.InitRef;
  321. oper.opr.ref.symbol:=hl;
  322. end;
  323. end;
  324. procedure MaybeRecordOffset;
  325. var
  326. mangledname: string;
  327. hasdot : boolean;
  328. l,
  329. toffset,
  330. tsize : aint;
  331. begin
  332. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  333. exit;
  334. l:=0;
  335. hasdot:=(actasmtoken=AS_DOT);
  336. if hasdot then
  337. begin
  338. if expr<>'' then
  339. begin
  340. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  341. if (oper.opr.typ<>OPR_CONSTANT) and
  342. (mangledname<>'') then
  343. Message(asmr_e_wrong_sym_type);
  344. inc(l,toffset);
  345. oper.SetSize(tsize,true);
  346. end;
  347. end;
  348. if actasmtoken in [AS_PLUS,AS_MINUS] then
  349. inc(l,BuildConstExpression(true,false));
  350. case oper.opr.typ of
  351. OPR_LOCAL :
  352. begin
  353. { don't allow direct access to fields of parameters, because that
  354. will generate buggy code. Allow it only for explicit typecasting }
  355. if hasdot and
  356. (not oper.hastype) and
  357. (oper.opr.localsym.owner.symtabletype=parasymtable) and
  358. (current_procinfo.procdef.proccalloption<>pocall_register) then
  359. Message(asmr_e_cannot_access_field_directly_for_parameters);
  360. inc(oper.opr.localsymofs,l)
  361. end;
  362. OPR_CONSTANT :
  363. if (mangledname<>'') then
  364. begin
  365. if (oper.opr.val<>0) then
  366. Message(asmr_e_wrong_sym_type);
  367. oper.opr.typ:=OPR_SYMBOL;
  368. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname);
  369. end
  370. else
  371. inc(oper.opr.val,l);
  372. OPR_REFERENCE :
  373. inc(oper.opr.ref.offset,l);
  374. OPR_SYMBOL:
  375. Message(asmr_e_invalid_symbol_ref);
  376. else
  377. internalerror(200309221);
  378. end;
  379. end;
  380. function MaybeBuildReference:boolean;
  381. { Try to create a reference, if not a reference is found then false
  382. is returned }
  383. var
  384. mangledname: string;
  385. begin
  386. MaybeBuildReference:=true;
  387. case actasmtoken of
  388. AS_INTNUM,
  389. AS_MINUS,
  390. AS_PLUS:
  391. Begin
  392. oper.opr.ref.offset:=BuildConstExpression(True,False);
  393. if actasmtoken<>AS_LPAREN then
  394. Message(asmr_e_invalid_reference_syntax)
  395. else
  396. BuildReference(oper);
  397. end;
  398. AS_LPAREN:
  399. BuildReference(oper);
  400. AS_ID: { only a variable is allowed ... }
  401. Begin
  402. tempstr:=actasmpattern;
  403. Consume(AS_ID);
  404. { typecasting? }
  405. if (actasmtoken=AS_LPAREN) and
  406. SearchType(tempstr,typesize) then
  407. begin
  408. oper.hastype:=true;
  409. Consume(AS_LPAREN);
  410. BuildOperand(oper);
  411. Consume(AS_RPAREN);
  412. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  413. oper.SetSize(typesize,true);
  414. end
  415. else
  416. if not oper.SetupVar(tempstr,false) then
  417. Message1(sym_e_unknown_id,tempstr);
  418. { record.field ? }
  419. if actasmtoken=AS_DOT then
  420. begin
  421. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  422. if (mangledname<>'') then
  423. Message(asmr_e_invalid_reference_syntax);
  424. inc(oper.opr.ref.offset,l);
  425. end;
  426. MaybeGetPICModifier(oper);
  427. case actasmtoken of
  428. AS_END,
  429. AS_SEPARATOR,
  430. AS_COMMA: ;
  431. AS_LPAREN:
  432. BuildReference(oper);
  433. else
  434. Begin
  435. Message(asmr_e_invalid_reference_syntax);
  436. Consume(actasmtoken);
  437. end;
  438. end; {end case }
  439. end;
  440. else
  441. MaybeBuildReference:=false;
  442. end; { end case }
  443. end;
  444. var
  445. tempreg : tregister;
  446. hl : tasmlabel;
  447. Begin
  448. expr:='';
  449. case actasmtoken of
  450. AS_LPAREN: { Memory reference or constant expression }
  451. Begin
  452. oper.InitRef;
  453. BuildReference(oper);
  454. end;
  455. AS_DOLLAR: { Constant expression }
  456. Begin
  457. Consume(AS_DOLLAR);
  458. BuildConstantOperand(oper);
  459. end;
  460. AS_INTNUM,
  461. AS_MINUS,
  462. AS_PLUS:
  463. Begin
  464. { Constant memory offset }
  465. { This must absolutely be followed by ( }
  466. oper.InitRef;
  467. oper.opr.ref.offset:=BuildConstExpression(True,False);
  468. if actasmtoken<>AS_LPAREN then
  469. Message(asmr_e_invalid_reference_syntax)
  470. else
  471. BuildReference(oper);
  472. end;
  473. AS_STAR: { Call from memory address }
  474. Begin
  475. Consume(AS_STAR);
  476. if actasmtoken=AS_REGISTER then
  477. begin
  478. oper.opr.typ:=OPR_REGISTER;
  479. oper.opr.reg:=actasmregister;
  480. oper.SetSize(tcgsize2size[reg_cgsize(actasmregister)],true);
  481. Consume(AS_REGISTER);
  482. end
  483. else
  484. begin
  485. oper.InitRef;
  486. if not MaybeBuildReference then
  487. Message(asmr_e_syn_operand);
  488. end;
  489. { this is only allowed for call's and jmp's }
  490. if not is_calljmp(actopcode) then
  491. Message(asmr_e_syn_operand);
  492. end;
  493. AS_ID: { A constant expression, or a Variable ref. }
  494. Begin
  495. { Local Label ? }
  496. if is_locallabel(actasmpattern) then
  497. begin
  498. CreateLocalLabel(actasmpattern,hl,false);
  499. Consume(AS_ID);
  500. AddLabelOperand(hl);
  501. end
  502. else
  503. { Check for label }
  504. if SearchLabel(actasmpattern,hl,false) then
  505. begin
  506. Consume(AS_ID);
  507. AddLabelOperand(hl);
  508. end
  509. else
  510. { probably a variable or normal expression }
  511. { or a procedure (such as in CALL ID) }
  512. Begin
  513. { is it a constant ? }
  514. if SearchIConstant(actasmpattern,l) then
  515. Begin
  516. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  517. Message(asmr_e_invalid_operand_type);
  518. BuildConstantOperand(oper);
  519. end
  520. else
  521. begin
  522. expr:=actasmpattern;
  523. Consume(AS_ID);
  524. { typecasting? }
  525. if (actasmtoken=AS_LPAREN) and
  526. SearchType(expr,typesize) then
  527. begin
  528. oper.hastype:=true;
  529. Consume(AS_LPAREN);
  530. BuildOperand(oper);
  531. Consume(AS_RPAREN);
  532. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  533. oper.SetSize(typesize,true);
  534. end
  535. else
  536. begin
  537. if oper.SetupVar(expr,false) then
  538. MaybeGetPICModifier(oper)
  539. else
  540. Begin
  541. { look for special symbols ... }
  542. if expr= '__HIGH' then
  543. begin
  544. consume(AS_LPAREN);
  545. if not oper.setupvar('high'+actasmpattern,false) then
  546. Message1(sym_e_unknown_id,'high'+actasmpattern);
  547. consume(AS_ID);
  548. consume(AS_RPAREN);
  549. end
  550. else
  551. if expr = '__RESULT' then
  552. oper.SetUpResult
  553. else
  554. if expr = '__SELF' then
  555. oper.SetupSelf
  556. else
  557. if expr = '__OLDEBP' then
  558. oper.SetupOldEBP
  559. else
  560. Message1(sym_e_unknown_id,expr);
  561. end;
  562. end;
  563. end;
  564. if oper.opr.typ<>OPR_NONE Then
  565. begin
  566. if (actasmtoken=AS_DOT) then
  567. MaybeRecordOffset;
  568. { add a constant expression? }
  569. if (actasmtoken=AS_PLUS) then
  570. begin
  571. l:=BuildConstExpression(true,false);
  572. case oper.opr.typ of
  573. OPR_CONSTANT :
  574. inc(oper.opr.val,l);
  575. OPR_LOCAL :
  576. inc(oper.opr.localsymofs,l);
  577. OPR_REFERENCE :
  578. inc(oper.opr.ref.offset,l);
  579. else
  580. internalerror(200309202);
  581. end;
  582. end;
  583. end;
  584. end;
  585. { Do we have a indexing reference, then parse it also }
  586. if actasmtoken=AS_LPAREN then
  587. BuildReference(oper);
  588. end;
  589. AS_REGISTER: { Register, a variable reference or a constant reference }
  590. Begin
  591. { save the type of register used. }
  592. tempreg:=actasmregister;
  593. Consume(AS_REGISTER);
  594. if actasmtoken = AS_COLON then
  595. Begin
  596. Consume(AS_COLON);
  597. oper.InitRef;
  598. oper.opr.ref.segment:=tempreg;
  599. { This must absolutely be followed by a reference }
  600. if not MaybeBuildReference then
  601. Begin
  602. Message(asmr_e_invalid_seg_override);
  603. Consume(actasmtoken);
  604. end;
  605. end
  606. { Simple register }
  607. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  608. Begin
  609. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  610. Message(asmr_e_invalid_operand_type);
  611. oper.opr.typ:=OPR_REGISTER;
  612. oper.opr.reg:=tempreg;
  613. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  614. end
  615. else
  616. Message(asmr_e_syn_operand);
  617. end;
  618. AS_END,
  619. AS_SEPARATOR,
  620. AS_COMMA: ;
  621. else
  622. Begin
  623. Message(asmr_e_syn_operand);
  624. Consume(actasmtoken);
  625. end;
  626. end; { end case }
  627. end;
  628. procedure tx86attreader.BuildOpCode(instr : tx86instruction);
  629. var
  630. operandnum : longint;
  631. PrefixOp,OverrideOp: tasmop;
  632. Begin
  633. PrefixOp:=A_None;
  634. OverrideOp:=A_None;
  635. { prefix seg opcode / prefix opcode }
  636. repeat
  637. if is_prefix(actopcode) then
  638. begin
  639. PrefixOp:=ActOpcode;
  640. with instr do
  641. begin
  642. opcode:=ActOpcode;
  643. condition:=ActCondition;
  644. opsize:=ActOpsize;
  645. ConcatInstruction(curlist);
  646. end;
  647. Consume(AS_OPCODE);
  648. end
  649. else
  650. if is_override(actopcode) then
  651. begin
  652. OverrideOp:=ActOpcode;
  653. with instr do
  654. begin
  655. opcode:=ActOpcode;
  656. condition:=ActCondition;
  657. opsize:=ActOpsize;
  658. ConcatInstruction(curlist);
  659. end;
  660. Consume(AS_OPCODE);
  661. end
  662. else
  663. break;
  664. { allow for newline as in gas styled syntax }
  665. while actasmtoken=AS_SEPARATOR do
  666. Consume(AS_SEPARATOR);
  667. until (actasmtoken<>AS_OPCODE);
  668. { opcode }
  669. if (actasmtoken<>AS_OPCODE) then
  670. Begin
  671. Message(asmr_e_invalid_or_missing_opcode);
  672. RecoverConsume(true);
  673. exit;
  674. end;
  675. { Fill the instr object with the current state }
  676. with instr do
  677. begin
  678. Opcode:=ActOpcode;
  679. condition:=ActCondition;
  680. opsize:=ActOpsize;
  681. end;
  682. { Valid combination of prefix/override and instruction ? }
  683. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  684. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  685. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  686. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  687. { We are reading operands, so opcode will be an AS_ID }
  688. operandnum:=1;
  689. Consume(AS_OPCODE);
  690. { Zero operand opcode ? }
  691. if actasmtoken in [AS_SEPARATOR,AS_END] then
  692. begin
  693. operandnum:=0;
  694. exit;
  695. end;
  696. { Read the operands }
  697. repeat
  698. case actasmtoken of
  699. AS_COMMA: { Operand delimiter }
  700. Begin
  701. if operandnum > Max_Operands then
  702. Message(asmr_e_too_many_operands)
  703. else
  704. Inc(operandnum);
  705. Consume(AS_COMMA);
  706. end;
  707. AS_SEPARATOR,
  708. AS_END : { End of asm operands for this opcode }
  709. begin
  710. break;
  711. end;
  712. else
  713. BuildOperand(instr.Operands[operandnum] as tx86operand);
  714. end; { end case }
  715. until false;
  716. instr.Ops:=operandnum;
  717. end;
  718. function tx86attreader.is_asmopcode(const s: string):boolean;
  719. const
  720. { We need first to check the long prefixes, else we get probs
  721. with things like movsbl }
  722. att_sizesuffixstr : array[0..9] of string[2] = (
  723. '','BW','BL','WL','B','W','L','S','Q','T'
  724. );
  725. att_sizesuffix : array[0..9] of topsize = (
  726. S_NO,S_BW,S_BL,S_WL,S_B,S_W,S_L,S_FS,S_IQ,S_FX
  727. );
  728. att_sizefpusuffix : array[0..9] of topsize = (
  729. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_FL,S_FS,S_IQ,S_FX
  730. );
  731. att_sizefpuintsuffix : array[0..9] of topsize = (
  732. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_IL,S_IS,S_IQ,S_NO
  733. );
  734. var
  735. cond : string[4];
  736. cnd : tasmcond;
  737. len,
  738. j,
  739. sufidx : longint;
  740. Begin
  741. is_asmopcode:=FALSE;
  742. actopcode:=A_None;
  743. actcondition:=C_None;
  744. actopsize:=S_NO;
  745. { search for all possible suffixes }
  746. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  747. begin
  748. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  749. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  750. begin
  751. { Search opcodes }
  752. if len>0 then
  753. begin
  754. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(s,1,len))));
  755. if actopcode<>A_NONE then
  756. begin
  757. if gas_needsuffix[actopcode]=attsufFPU then
  758. actopsize:=att_sizefpusuffix[sufidx]
  759. else if gas_needsuffix[actopcode]=attsufFPUint then
  760. actopsize:=att_sizefpuintsuffix[sufidx]
  761. else
  762. actopsize:=att_sizesuffix[sufidx];
  763. actasmtoken:=AS_OPCODE;
  764. is_asmopcode:=TRUE;
  765. exit;
  766. end;
  767. end;
  768. { not found, check condition opcodes }
  769. j:=0;
  770. while (j<CondAsmOps) do
  771. begin
  772. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  773. begin
  774. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  775. if cond<>'' then
  776. begin
  777. for cnd:=low(TasmCond) to high(TasmCond) do
  778. if Cond=Upper(cond2str[cnd]) then
  779. begin
  780. actopcode:=CondASmOp[j];
  781. if gas_needsuffix[actopcode]=attsufFPU then
  782. actopsize:=att_sizefpusuffix[sufidx]
  783. else if gas_needsuffix[actopcode]=attsufFPUint then
  784. actopsize:=att_sizefpuintsuffix[sufidx]
  785. else
  786. actopsize:=att_sizesuffix[sufidx];
  787. actcondition:=cnd;
  788. actasmtoken:=AS_OPCODE;
  789. is_asmopcode:=TRUE;
  790. exit;
  791. end;
  792. end;
  793. end;
  794. inc(j);
  795. end;
  796. end;
  797. end;
  798. end;
  799. procedure tx86attreader.handleopcode;
  800. var
  801. instr : Tx86Instruction;
  802. begin
  803. instr:=Tx86Instruction.Create(Tx86Operand);
  804. instr.OpOrder:=op_att;
  805. BuildOpcode(instr);
  806. instr.AddReferenceSizes;
  807. instr.SetInstructionOpsize;
  808. instr.CheckOperandSizes;
  809. instr.ConcatInstruction(curlist);
  810. instr.Free;
  811. end;
  812. end.