rax86att.pas 31 KB

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