rax86att.pas 31 KB

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