rax86att.pas 28 KB

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