rax86att.pas 30 KB

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