rax86att.pas 28 KB

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