rax86att.pas 31 KB

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