rax86att.pas 28 KB

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