rax86att.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. Begin
  420. { allow %segmentregister:number }
  421. if oper.opr.ref.segment<>NR_NO then
  422. begin
  423. oper.InitRef;
  424. if oper.opr.Ref.Offset <> 0 Then
  425. Message(asmr_e_invalid_reference_syntax)
  426. else
  427. begin
  428. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  429. if actasmtoken=AS_LPAREN then
  430. BuildReference(oper);
  431. end;
  432. end
  433. else
  434. begin
  435. oper.opr.ref.offset:=BuildConstExpression(True,False);
  436. BuildReference(oper);
  437. end;
  438. end;
  439. AS_MINUS,
  440. AS_PLUS:
  441. Begin
  442. oper.opr.ref.offset:=BuildConstExpression(True,False);
  443. if actasmtoken<>AS_LPAREN then
  444. Message(asmr_e_invalid_reference_syntax)
  445. else
  446. BuildReference(oper);
  447. end;
  448. AS_LPAREN:
  449. BuildReference(oper);
  450. AS_ID: { only a variable is allowed ... }
  451. Begin
  452. tempstr:=actasmpattern;
  453. Consume(AS_ID);
  454. { typecasting? }
  455. if (actasmtoken=AS_LPAREN) and
  456. SearchType(tempstr,typesize) then
  457. begin
  458. oper.hastype:=true;
  459. Consume(AS_LPAREN);
  460. BuildOperand(oper);
  461. Consume(AS_RPAREN);
  462. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  463. oper.SetSize(typesize,true);
  464. end
  465. else
  466. if not oper.SetupVar(tempstr,false) then
  467. Message1(sym_e_unknown_id,tempstr);
  468. { record.field ? }
  469. if actasmtoken=AS_DOT then
  470. begin
  471. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  472. if (mangledname<>'') then
  473. Message(asmr_e_invalid_reference_syntax);
  474. inc(oper.opr.ref.offset,l);
  475. end;
  476. MaybeGetPICModifier(oper);
  477. case actasmtoken of
  478. AS_END,
  479. AS_SEPARATOR,
  480. AS_COMMA: ;
  481. AS_LPAREN:
  482. BuildReference(oper);
  483. else
  484. Begin
  485. Message(asmr_e_invalid_reference_syntax);
  486. Consume(actasmtoken);
  487. end;
  488. end; {end case }
  489. end;
  490. else
  491. MaybeBuildReference:=false;
  492. end; { end case }
  493. end;
  494. var
  495. tempreg : tregister;
  496. hl : tasmlabel;
  497. Begin
  498. expr:='';
  499. case actasmtoken of
  500. AS_LPAREN: { Memory reference or constant expression }
  501. Begin
  502. oper.InitRef;
  503. BuildReference(oper);
  504. end;
  505. AS_DOLLAR: { Constant expression }
  506. Begin
  507. Consume(AS_DOLLAR);
  508. BuildConstantOperand(oper);
  509. end;
  510. AS_INTNUM,
  511. AS_MINUS,
  512. AS_PLUS:
  513. Begin
  514. { Constant memory offset }
  515. { This must absolutely be followed by ( }
  516. oper.InitRef;
  517. oper.opr.ref.offset:=BuildConstExpression(True,False);
  518. if actasmtoken<>AS_LPAREN then
  519. Message(asmr_e_invalid_reference_syntax)
  520. else
  521. BuildReference(oper);
  522. end;
  523. AS_STAR: { Call from memory address }
  524. Begin
  525. Consume(AS_STAR);
  526. if actasmtoken=AS_REGISTER then
  527. begin
  528. oper.opr.typ:=OPR_REGISTER;
  529. oper.opr.reg:=actasmregister;
  530. oper.SetSize(tcgsize2size[reg_cgsize(actasmregister)],true);
  531. Consume(AS_REGISTER);
  532. end
  533. else
  534. begin
  535. oper.InitRef;
  536. if not MaybeBuildReference then
  537. Message(asmr_e_syn_operand);
  538. end;
  539. { this is only allowed for call's and jmp's }
  540. if not is_calljmp(actopcode) then
  541. Message(asmr_e_syn_operand);
  542. end;
  543. AS_ID: { A constant expression, or a Variable ref. }
  544. Begin
  545. { Local Label ? }
  546. if is_locallabel(actasmpattern) then
  547. begin
  548. CreateLocalLabel(actasmpattern,hl,false);
  549. Consume(AS_ID);
  550. AddLabelOperand(hl);
  551. end
  552. else
  553. { Check for label }
  554. if SearchLabel(actasmpattern,hl,false) then
  555. begin
  556. Consume(AS_ID);
  557. AddLabelOperand(hl);
  558. end
  559. else
  560. { probably a variable or normal expression }
  561. { or a procedure (such as in CALL ID) }
  562. Begin
  563. { is it a constant ? }
  564. if SearchIConstant(actasmpattern,l) then
  565. Begin
  566. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  567. Message(asmr_e_invalid_operand_type);
  568. BuildConstantOperand(oper);
  569. end
  570. else
  571. begin
  572. expr:=actasmpattern;
  573. Consume(AS_ID);
  574. { typecasting? }
  575. if (actasmtoken=AS_LPAREN) and
  576. SearchType(expr,typesize) then
  577. begin
  578. oper.hastype:=true;
  579. Consume(AS_LPAREN);
  580. BuildOperand(oper);
  581. Consume(AS_RPAREN);
  582. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  583. oper.SetSize(typesize,true);
  584. end
  585. else
  586. begin
  587. if oper.SetupVar(expr,false) then
  588. MaybeGetPICModifier(oper)
  589. else
  590. Begin
  591. { look for special symbols ... }
  592. if expr= '__HIGH' then
  593. begin
  594. consume(AS_LPAREN);
  595. if not oper.setupvar('high'+actasmpattern,false) then
  596. Message1(sym_e_unknown_id,'high'+actasmpattern);
  597. consume(AS_ID);
  598. consume(AS_RPAREN);
  599. end
  600. else
  601. if expr = '__RESULT' then
  602. oper.SetUpResult
  603. else
  604. if expr = '__SELF' then
  605. oper.SetupSelf
  606. else
  607. if expr = '__OLDEBP' then
  608. oper.SetupOldEBP
  609. else
  610. Message1(sym_e_unknown_id,expr);
  611. end;
  612. end;
  613. end;
  614. if oper.opr.typ<>OPR_NONE Then
  615. begin
  616. if (actasmtoken=AS_DOT) then
  617. MaybeRecordOffset;
  618. { add a constant expression? }
  619. if (actasmtoken=AS_PLUS) then
  620. begin
  621. l:=BuildConstExpression(true,false);
  622. case oper.opr.typ of
  623. OPR_CONSTANT :
  624. inc(oper.opr.val,l);
  625. OPR_LOCAL :
  626. inc(oper.opr.localsymofs,l);
  627. OPR_REFERENCE :
  628. inc(oper.opr.ref.offset,l);
  629. else
  630. internalerror(200309202);
  631. end;
  632. end;
  633. end;
  634. end;
  635. { Do we have a indexing reference, then parse it also }
  636. if actasmtoken=AS_LPAREN then
  637. BuildReference(oper);
  638. end;
  639. AS_REGISTER: { Register, a variable reference or a constant reference }
  640. Begin
  641. { save the type of register used. }
  642. tempreg:=actasmregister;
  643. Consume(AS_REGISTER);
  644. if actasmtoken = AS_COLON then
  645. Begin
  646. Consume(AS_COLON);
  647. oper.InitRef;
  648. oper.opr.ref.segment:=tempreg;
  649. { This must absolutely be followed by a reference }
  650. if not MaybeBuildReference then
  651. Begin
  652. Message(asmr_e_invalid_seg_override);
  653. Consume(actasmtoken);
  654. end;
  655. end
  656. { Simple register }
  657. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  658. Begin
  659. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  660. Message(asmr_e_invalid_operand_type);
  661. oper.opr.typ:=OPR_REGISTER;
  662. oper.opr.reg:=tempreg;
  663. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  664. end
  665. else
  666. Message(asmr_e_syn_operand);
  667. end;
  668. AS_END,
  669. AS_SEPARATOR,
  670. AS_COMMA: ;
  671. else
  672. Begin
  673. Message(asmr_e_syn_operand);
  674. Consume(actasmtoken);
  675. end;
  676. end; { end case }
  677. end;
  678. procedure tx86attreader.BuildOpCode(instr : tx86instruction);
  679. var
  680. operandnum : longint;
  681. PrefixOp,OverrideOp: tasmop;
  682. Begin
  683. PrefixOp:=A_None;
  684. OverrideOp:=A_None;
  685. { prefix seg opcode / prefix opcode }
  686. repeat
  687. if is_prefix(actopcode) then
  688. begin
  689. PrefixOp:=ActOpcode;
  690. with instr do
  691. begin
  692. opcode:=ActOpcode;
  693. condition:=ActCondition;
  694. opsize:=ActOpsize;
  695. ConcatInstruction(curlist);
  696. end;
  697. Consume(AS_OPCODE);
  698. end
  699. else
  700. if is_override(actopcode) then
  701. begin
  702. OverrideOp:=ActOpcode;
  703. with instr do
  704. begin
  705. opcode:=ActOpcode;
  706. condition:=ActCondition;
  707. opsize:=ActOpsize;
  708. ConcatInstruction(curlist);
  709. end;
  710. Consume(AS_OPCODE);
  711. end
  712. else
  713. break;
  714. { allow for newline as in gas styled syntax }
  715. while actasmtoken=AS_SEPARATOR do
  716. Consume(AS_SEPARATOR);
  717. until (actasmtoken<>AS_OPCODE);
  718. { opcode }
  719. if (actasmtoken<>AS_OPCODE) then
  720. Begin
  721. Message(asmr_e_invalid_or_missing_opcode);
  722. RecoverConsume(true);
  723. exit;
  724. end;
  725. { Fill the instr object with the current state }
  726. with instr do
  727. begin
  728. Opcode:=ActOpcode;
  729. condition:=ActCondition;
  730. opsize:=ActOpsize;
  731. end;
  732. { Valid combination of prefix/override and instruction ? }
  733. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  734. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  735. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  736. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  737. { We are reading operands, so opcode will be an AS_ID }
  738. operandnum:=1;
  739. Consume(AS_OPCODE);
  740. { Zero operand opcode ? }
  741. if actasmtoken in [AS_SEPARATOR,AS_END] then
  742. begin
  743. operandnum:=0;
  744. exit;
  745. end;
  746. { Read the operands }
  747. repeat
  748. case actasmtoken of
  749. AS_COMMA: { Operand delimiter }
  750. Begin
  751. if operandnum > Max_Operands then
  752. Message(asmr_e_too_many_operands)
  753. else
  754. Inc(operandnum);
  755. Consume(AS_COMMA);
  756. end;
  757. AS_SEPARATOR,
  758. AS_END : { End of asm operands for this opcode }
  759. begin
  760. break;
  761. end;
  762. else
  763. BuildOperand(instr.Operands[operandnum] as tx86operand);
  764. end; { end case }
  765. until false;
  766. instr.Ops:=operandnum;
  767. end;
  768. function tx86attreader.is_asmopcode(const s: string):boolean;
  769. var
  770. cond : string[4];
  771. cnd : tasmcond;
  772. len,
  773. j,
  774. sufidx,
  775. suflen : longint;
  776. Begin
  777. is_asmopcode:=FALSE;
  778. actopcode:=A_None;
  779. actcondition:=C_None;
  780. actopsize:=S_NO;
  781. { search for all possible suffixes }
  782. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  783. begin
  784. suflen:=length(att_sizesuffixstr[sufidx]);
  785. len:=length(s)-suflen;
  786. if copy(s,len+1,suflen)=att_sizesuffixstr[sufidx] then
  787. begin
  788. { Search opcodes }
  789. if len>0 then
  790. begin
  791. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(s,1,len))));
  792. { two-letter suffix is allowed by just a few instructions (movsx,movzx),
  793. and it is always required whenever allowed }
  794. if (gas_needsuffix[actopcode]=attsufINTdual) xor (suflen=2) then
  795. continue;
  796. if actopcode<>A_NONE then
  797. begin
  798. if gas_needsuffix[actopcode]=attsufFPU then
  799. actopsize:=att_sizefpusuffix[sufidx]
  800. else if gas_needsuffix[actopcode]=attsufFPUint then
  801. actopsize:=att_sizefpuintsuffix[sufidx]
  802. else
  803. actopsize:=att_sizesuffix[sufidx];
  804. { only accept suffix from the same category that the opcode belongs to }
  805. if (actopsize<>S_NO) or (suflen=0) then
  806. begin
  807. actasmtoken:=AS_OPCODE;
  808. is_asmopcode:=TRUE;
  809. exit;
  810. end;
  811. end;
  812. end;
  813. { not found, check condition opcodes }
  814. j:=0;
  815. while (j<CondAsmOps) do
  816. begin
  817. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  818. begin
  819. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  820. if cond<>'' then
  821. begin
  822. for cnd:=low(TasmCond) to high(TasmCond) do
  823. if Cond=Upper(cond2str[cnd]) then
  824. begin
  825. actopcode:=CondASmOp[j];
  826. if gas_needsuffix[actopcode]=attsufFPU then
  827. actopsize:=att_sizefpusuffix[sufidx]
  828. else if gas_needsuffix[actopcode]=attsufFPUint then
  829. actopsize:=att_sizefpuintsuffix[sufidx]
  830. else
  831. actopsize:=att_sizesuffix[sufidx];
  832. { only accept suffix from the same category that the opcode belongs to }
  833. if (actopsize<>S_NO) or (suflen=0) then
  834. begin
  835. actcondition:=cnd;
  836. actasmtoken:=AS_OPCODE;
  837. is_asmopcode:=TRUE;
  838. exit;
  839. end;
  840. end;
  841. end;
  842. end;
  843. inc(j);
  844. end;
  845. end;
  846. end;
  847. end;
  848. procedure tx86attreader.handleopcode;
  849. var
  850. instr : Tx86Instruction;
  851. begin
  852. instr:=Tx86attInstruction.Create(Tx86Operand);
  853. instr.OpOrder:=op_att;
  854. BuildOpcode(instr);
  855. instr.AddReferenceSizes;
  856. instr.SetInstructionOpsize;
  857. instr.CheckOperandSizes;
  858. instr.FixupOpcode;
  859. instr.ConcatInstruction(curlist);
  860. instr.Free;
  861. end;
  862. end.