rax86att.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 actasmtoken=AS_DOT then
  542. MaybeRecordOffset;
  543. { add a constant expression? }
  544. if (actasmtoken=AS_PLUS) then
  545. begin
  546. l:=BuildConstExpression(true,false);
  547. case oper.opr.typ of
  548. OPR_CONSTANT :
  549. inc(oper.opr.val,l);
  550. OPR_LOCAL :
  551. inc(oper.opr.localsymofs,l);
  552. OPR_REFERENCE :
  553. inc(oper.opr.ref.offset,l);
  554. else
  555. internalerror(200309202);
  556. end;
  557. end
  558. end;
  559. { Do we have a indexing reference, then parse it also }
  560. if actasmtoken=AS_LPAREN then
  561. BuildReference(oper);
  562. end;
  563. AS_REGISTER: { Register, a variable reference or a constant reference }
  564. Begin
  565. { save the type of register used. }
  566. tempreg:=actasmregister;
  567. Consume(AS_REGISTER);
  568. if actasmtoken = AS_COLON then
  569. Begin
  570. Consume(AS_COLON);
  571. oper.InitRef;
  572. oper.opr.ref.segment:=tempreg;
  573. { This must absolutely be followed by a reference }
  574. if not MaybeBuildReference then
  575. Begin
  576. Message(asmr_e_invalid_seg_override);
  577. Consume(actasmtoken);
  578. end;
  579. end
  580. { Simple register }
  581. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  582. Begin
  583. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  584. Message(asmr_e_invalid_operand_type);
  585. oper.opr.typ:=OPR_REGISTER;
  586. oper.opr.reg:=tempreg;
  587. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  588. end
  589. else
  590. Message(asmr_e_syn_operand);
  591. end;
  592. AS_END,
  593. AS_SEPARATOR,
  594. AS_COMMA: ;
  595. else
  596. Begin
  597. Message(asmr_e_syn_operand);
  598. Consume(actasmtoken);
  599. end;
  600. end; { end case }
  601. end;
  602. procedure tx86attreader.BuildOpCode(instr : tx86instruction);
  603. var
  604. operandnum : longint;
  605. PrefixOp,OverrideOp: tasmop;
  606. Begin
  607. PrefixOp:=A_None;
  608. OverrideOp:=A_None;
  609. { prefix seg opcode / prefix opcode }
  610. repeat
  611. if is_prefix(actopcode) then
  612. begin
  613. PrefixOp:=ActOpcode;
  614. with instr do
  615. begin
  616. opcode:=ActOpcode;
  617. condition:=ActCondition;
  618. opsize:=ActOpsize;
  619. ConcatInstruction(curlist);
  620. end;
  621. Consume(AS_OPCODE);
  622. end
  623. else
  624. if is_override(actopcode) then
  625. begin
  626. OverrideOp:=ActOpcode;
  627. with instr do
  628. begin
  629. opcode:=ActOpcode;
  630. condition:=ActCondition;
  631. opsize:=ActOpsize;
  632. ConcatInstruction(curlist);
  633. end;
  634. Consume(AS_OPCODE);
  635. end
  636. else
  637. break;
  638. { allow for newline as in gas styled syntax }
  639. while actasmtoken=AS_SEPARATOR do
  640. Consume(AS_SEPARATOR);
  641. until (actasmtoken<>AS_OPCODE);
  642. { opcode }
  643. if (actasmtoken<>AS_OPCODE) then
  644. Begin
  645. Message(asmr_e_invalid_or_missing_opcode);
  646. RecoverConsume(true);
  647. exit;
  648. end;
  649. { Fill the instr object with the current state }
  650. with instr do
  651. begin
  652. Opcode:=ActOpcode;
  653. condition:=ActCondition;
  654. opsize:=ActOpsize;
  655. end;
  656. { Valid combination of prefix/override and instruction ? }
  657. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  658. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  659. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  660. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  661. { We are reading operands, so opcode will be an AS_ID }
  662. operandnum:=1;
  663. Consume(AS_OPCODE);
  664. { Zero operand opcode ? }
  665. if actasmtoken in [AS_SEPARATOR,AS_END] then
  666. begin
  667. operandnum:=0;
  668. exit;
  669. end;
  670. { Read the operands }
  671. repeat
  672. case actasmtoken of
  673. AS_COMMA: { Operand delimiter }
  674. Begin
  675. if operandnum > Max_Operands then
  676. Message(asmr_e_too_many_operands)
  677. else
  678. Inc(operandnum);
  679. Consume(AS_COMMA);
  680. end;
  681. AS_SEPARATOR,
  682. AS_END : { End of asm operands for this opcode }
  683. begin
  684. break;
  685. end;
  686. else
  687. BuildOperand(instr.Operands[operandnum] as tx86operand);
  688. end; { end case }
  689. until false;
  690. instr.Ops:=operandnum;
  691. end;
  692. function tx86attreader.is_asmopcode(const s: string):boolean;
  693. const
  694. { We need first to check the long prefixes, else we get probs
  695. with things like movsbl }
  696. att_sizesuffixstr : array[0..9] of string[2] = (
  697. '','BW','BL','WL','B','W','L','S','Q','T'
  698. );
  699. att_sizesuffix : array[0..9] of topsize = (
  700. S_NO,S_BW,S_BL,S_WL,S_B,S_W,S_L,S_FS,S_IQ,S_FX
  701. );
  702. att_sizefpusuffix : array[0..9] of topsize = (
  703. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_FL,S_FS,S_IQ,S_FX
  704. );
  705. att_sizefpuintsuffix : array[0..9] of topsize = (
  706. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_IL,S_IS,S_IQ,S_NO
  707. );
  708. var
  709. cond : string[4];
  710. cnd : tasmcond;
  711. len,
  712. j,
  713. sufidx : longint;
  714. Begin
  715. is_asmopcode:=FALSE;
  716. actopcode:=A_None;
  717. actcondition:=C_None;
  718. actopsize:=S_NO;
  719. { search for all possible suffixes }
  720. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  721. begin
  722. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  723. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  724. begin
  725. { Search opcodes }
  726. if len>0 then
  727. begin
  728. actopcode:=tasmop(PtrInt(iasmops.Find(copy(s,1,len))));
  729. if actopcode<>A_NONE then
  730. begin
  731. if gas_needsuffix[actopcode]=attsufFPU then
  732. actopsize:=att_sizefpusuffix[sufidx]
  733. else if gas_needsuffix[actopcode]=attsufFPUint then
  734. actopsize:=att_sizefpuintsuffix[sufidx]
  735. else
  736. actopsize:=att_sizesuffix[sufidx];
  737. actasmtoken:=AS_OPCODE;
  738. is_asmopcode:=TRUE;
  739. exit;
  740. end;
  741. end;
  742. { not found, check condition opcodes }
  743. j:=0;
  744. while (j<CondAsmOps) do
  745. begin
  746. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  747. begin
  748. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  749. if cond<>'' then
  750. begin
  751. for cnd:=low(TasmCond) to high(TasmCond) do
  752. if Cond=Upper(cond2str[cnd]) then
  753. begin
  754. actopcode:=CondASmOp[j];
  755. if gas_needsuffix[actopcode]=attsufFPU then
  756. actopsize:=att_sizefpusuffix[sufidx]
  757. else if gas_needsuffix[actopcode]=attsufFPUint then
  758. actopsize:=att_sizefpuintsuffix[sufidx]
  759. else
  760. actopsize:=att_sizesuffix[sufidx];
  761. actcondition:=cnd;
  762. actasmtoken:=AS_OPCODE;
  763. is_asmopcode:=TRUE;
  764. exit;
  765. end;
  766. end;
  767. end;
  768. inc(j);
  769. end;
  770. end;
  771. end;
  772. end;
  773. procedure tx86attreader.handleopcode;
  774. var
  775. instr : Tx86Instruction;
  776. begin
  777. instr:=Tx86Instruction.Create(Tx86Operand);
  778. instr.OpOrder:=op_att;
  779. BuildOpcode(instr);
  780. instr.AddReferenceSizes;
  781. instr.SetInstructionOpsize;
  782. instr.CheckOperandSizes;
  783. instr.ConcatInstruction(curlist);
  784. instr.Free;
  785. end;
  786. end.