rax86att.pas 28 KB

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