rax86att.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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, rautils;
  23. type
  24. { tx86attreader }
  25. tx86attreader = class(tattreader)
  26. ActOpsize : topsize;
  27. function is_asmopcode(const s: string):boolean;override;
  28. procedure handleopcode;override;
  29. procedure BuildReference(oper : tx86operand);
  30. procedure BuildOperand(oper : tx86operand);
  31. procedure BuildOpCode(instr : tx86instruction);
  32. procedure handlepercent;override;
  33. protected
  34. procedure MaybeGetPICModifier(var oper: tx86operand);
  35. end;
  36. { Tx86attInstruction }
  37. Tx86attInstruction = class(Tx86Instruction)
  38. procedure AddReferenceSizes; override;
  39. procedure FixupOpcode;override;
  40. end;
  41. Implementation
  42. uses
  43. { helpers }
  44. cutils,
  45. { global }
  46. globtype,verbose,
  47. systems,
  48. { aasm }
  49. aasmbase,aasmdata,aasmcpu,
  50. { symtable }
  51. symconst,symsym,symdef,
  52. { parser }
  53. scanner,
  54. procinfo,
  55. itcpugas,
  56. paramgr,
  57. cgbase
  58. ;
  59. { Tx86attInstruction }
  60. procedure Tx86attInstruction.AddReferenceSizes;
  61. var
  62. i: integer;
  63. begin
  64. if (Opsize <> S_NO) and
  65. (MemRefInfo(opcode).ExistsSSEAVX) and
  66. (MemRefInfo(opcode).MemRefSize in MemRefMultiples) then
  67. begin
  68. for i := 1 to ops do
  69. begin
  70. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  71. begin
  72. if (tx86operand(operands[i]).opsize = S_NO) then
  73. tx86operand(operands[i]).opsize := Opsize;
  74. end;
  75. end;
  76. end;
  77. inherited AddReferenceSizes;
  78. end;
  79. procedure Tx86attInstruction.FixupOpcode;
  80. begin
  81. case opcode of
  82. A_MOVQ:
  83. begin
  84. { May be either real 'movq' or a generic 'mov' with 'q' suffix. Convert to mov
  85. if source is a constant, or if neither operand is an mmx/xmm register }
  86. {$ifdef x86_64}
  87. if (ops=2) and
  88. (
  89. (operands[1].opr.typ=OPR_CONSTANT) or not
  90. (
  91. ((operands[1].opr.typ=OPR_REGISTER) and
  92. (getregtype(operands[1].opr.reg) in [R_MMXREGISTER,R_MMREGISTER])) or
  93. ((operands[2].opr.typ=OPR_REGISTER) and
  94. (getregtype(operands[2].opr.reg) in [R_MMXREGISTER,R_MMREGISTER]))
  95. )
  96. ) then
  97. opcode:=A_MOV;
  98. {$endif x86_64}
  99. end;
  100. end;
  101. end;
  102. { Tx86attReader }
  103. procedure tx86attreader.handlepercent;
  104. var
  105. len : longint;
  106. begin
  107. len:=1;
  108. actasmpattern[len]:='%';
  109. c:=current_scanner.asmgetchar;
  110. { to be a register there must be a letter and not a number }
  111. if c in ['0'..'9'] then
  112. begin
  113. actasmtoken:=AS_MOD;
  114. end
  115. else
  116. begin
  117. while c in ['a'..'z','A'..'Z','0'..'9'] do
  118. Begin
  119. inc(len);
  120. actasmpattern[len]:=c;
  121. c:=current_scanner.asmgetchar;
  122. end;
  123. actasmpattern[0]:=chr(len);
  124. uppervar(actasmpattern);
  125. if (actasmpattern = '%ST') and (c='(') then
  126. Begin
  127. actasmpattern:=actasmpattern+c;
  128. c:=current_scanner.asmgetchar;
  129. if c in ['0'..'9'] then
  130. actasmpattern:=actasmpattern + c
  131. else
  132. Message(asmr_e_invalid_fpu_register);
  133. c:=current_scanner.asmgetchar;
  134. if c <> ')' then
  135. Message(asmr_e_invalid_fpu_register)
  136. else
  137. Begin
  138. actasmpattern:=actasmpattern + c;
  139. c:=current_scanner.asmgetchar; { let us point to next character. }
  140. end;
  141. end;
  142. if is_register(actasmpattern) then
  143. exit;
  144. Message(asmr_e_invalid_register);
  145. actasmtoken:=raatt.AS_NONE;
  146. end;
  147. end;
  148. Procedure tx86attreader.BuildReference(oper : tx86operand);
  149. procedure Consume_RParen;
  150. begin
  151. if actasmtoken <> AS_RPAREN then
  152. Begin
  153. Message(asmr_e_invalid_reference_syntax);
  154. RecoverConsume(true);
  155. end
  156. else
  157. begin
  158. Consume(AS_RPAREN);
  159. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  160. Begin
  161. Message(asmr_e_invalid_reference_syntax);
  162. RecoverConsume(true);
  163. end;
  164. end;
  165. end;
  166. procedure Consume_Scale;
  167. var
  168. l : aint;
  169. begin
  170. { we have to process the scaling }
  171. l:=BuildConstExpression(false,true);
  172. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  173. oper.opr.ref.scalefactor:=l
  174. else
  175. Begin
  176. Message(asmr_e_wrong_scale_factor);
  177. oper.opr.ref.scalefactor:=0;
  178. end;
  179. end;
  180. begin
  181. oper.InitRef;
  182. Consume(AS_LPAREN);
  183. Case actasmtoken of
  184. AS_INTNUM,
  185. AS_MINUS,
  186. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  187. Begin
  188. { offset(offset) is invalid }
  189. If oper.opr.Ref.Offset <> 0 Then
  190. Begin
  191. Message(asmr_e_invalid_reference_syntax);
  192. RecoverConsume(true);
  193. End
  194. Else
  195. Begin
  196. oper.opr.Ref.Offset:=BuildConstExpression(false,true);
  197. Consume_RParen;
  198. end;
  199. exit;
  200. End;
  201. AS_REGISTER: { (reg ... }
  202. Begin
  203. { Check if there is already a base (mostly ebp,esp) than this is
  204. not allowed, because it will give crashing code }
  205. if ((oper.opr.typ=OPR_REFERENCE) and (oper.opr.ref.base<>NR_NO)) or
  206. ((oper.opr.typ=OPR_LOCAL) and (oper.opr.localsym.localloc.loc<>LOC_REGISTER)) then
  207. message(asmr_e_cannot_index_relative_var);
  208. oper.opr.ref.base:=actasmregister;
  209. {$ifdef x86_64}
  210. { non-GOT based RIP-relative accesses are also position-independent }
  211. if (oper.opr.ref.base=NR_RIP) and
  212. (oper.opr.ref.refaddr<>addr_pic) then
  213. oper.opr.ref.refaddr:=addr_pic_no_got;
  214. {$endif x86_64}
  215. Consume(AS_REGISTER);
  216. { can either be a register or a right parenthesis }
  217. { (reg) }
  218. if actasmtoken=AS_RPAREN then
  219. Begin
  220. Consume_RParen;
  221. exit;
  222. end;
  223. { (reg,reg .. }
  224. Consume(AS_COMMA);
  225. if actasmtoken=AS_REGISTER then
  226. Begin
  227. oper.opr.ref.index:=actasmregister;
  228. Consume(AS_REGISTER);
  229. { check for scaling ... }
  230. case actasmtoken of
  231. AS_RPAREN:
  232. Begin
  233. Consume_RParen;
  234. exit;
  235. end;
  236. AS_COMMA:
  237. Begin
  238. Consume(AS_COMMA);
  239. Consume_Scale;
  240. Consume_RParen;
  241. end;
  242. else
  243. Begin
  244. Message(asmr_e_invalid_reference_syntax);
  245. RecoverConsume(false);
  246. end;
  247. end; { end case }
  248. end
  249. else
  250. Begin
  251. Message(asmr_e_invalid_reference_syntax);
  252. RecoverConsume(false);
  253. end;
  254. end; {end case }
  255. AS_COMMA: { (, ... can either be scaling, or index }
  256. Begin
  257. Consume(AS_COMMA);
  258. { Index }
  259. if (actasmtoken=AS_REGISTER) then
  260. Begin
  261. oper.opr.ref.index:=actasmregister;
  262. Consume(AS_REGISTER);
  263. { check for scaling ... }
  264. case actasmtoken of
  265. AS_RPAREN:
  266. Begin
  267. Consume_RParen;
  268. exit;
  269. end;
  270. AS_COMMA:
  271. Begin
  272. Consume(AS_COMMA);
  273. Consume_Scale;
  274. Consume_RParen;
  275. end;
  276. else
  277. Begin
  278. Message(asmr_e_invalid_reference_syntax);
  279. RecoverConsume(false);
  280. end;
  281. end; {end case }
  282. end
  283. { Scaling }
  284. else
  285. Begin
  286. Consume_Scale;
  287. Consume_RParen;
  288. exit;
  289. end;
  290. end;
  291. else
  292. Begin
  293. Message(asmr_e_invalid_reference_syntax);
  294. RecoverConsume(false);
  295. end;
  296. end;
  297. end;
  298. Procedure tx86attreader.MaybeGetPICModifier(var oper: tx86operand);
  299. var
  300. relsym: string;
  301. asmsymtyp: tasmsymtype;
  302. l: tcgint;
  303. sym: tasmsymbol;
  304. begin
  305. case actasmtoken of
  306. AS_AT:
  307. begin
  308. { darwin/i386 needs a relsym instead, and we can't }
  309. { generate this automatically }
  310. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  311. Message(asmr_e_invalid_reference_syntax);
  312. consume(AS_AT);
  313. if actasmtoken=AS_ID then
  314. begin
  315. {$ifdef x86_64}
  316. if (actasmpattern='GOTPCREL') or
  317. (actasmpattern='PLT') then
  318. {$endif x86_64}
  319. {$ifdef i386}
  320. if actasmpattern='GOT' then
  321. {$endif i386}
  322. {$ifdef i8086}
  323. if actasmpattern='GOT' then
  324. {$endif i8086}
  325. begin
  326. case oper.opr.typ of
  327. OPR_SYMBOL:
  328. begin
  329. sym:=oper.opr.symbol;
  330. if oper.opr.symofs<>0 then
  331. Message(asmr_e_invalid_reference_syntax);
  332. oper.opr.typ:=OPR_REFERENCE;
  333. fillchar(oper.opr.ref,sizeof(oper.opr.ref),0);
  334. oper.opr.ref.symbol:=sym;
  335. end;
  336. OPR_REFERENCE:
  337. begin
  338. { ok }
  339. end;
  340. else
  341. Message(asmr_e_invalid_reference_syntax)
  342. end;
  343. oper.opr.ref.refaddr:=addr_pic;
  344. consume(AS_ID);
  345. end
  346. else
  347. Message(asmr_e_invalid_reference_syntax);
  348. end
  349. else
  350. Message(asmr_e_invalid_reference_syntax);
  351. end;
  352. AS_MINUS:
  353. begin
  354. { relsym? }
  355. Consume(AS_MINUS);
  356. BuildConstSymbolExpression(true,true,false,l,relsym,asmsymtyp);
  357. if (relsym<>'') then
  358. if not assigned(oper.opr.ref.relsymbol) then
  359. oper.opr.ref.relsymbol:=current_asmdata.RefAsmSymbol(relsym,asmsymtyp)
  360. else
  361. Message(asmr_e_invalid_reference_syntax)
  362. else
  363. dec(oper.opr.ref.offset,l);
  364. end;
  365. end;
  366. end;
  367. Procedure tx86attreader.BuildOperand(oper : tx86operand);
  368. var
  369. tempstr,
  370. expr : string;
  371. typesize,l,k : tcgint;
  372. procedure AddLabelOperand(hl:tasmlabel);
  373. begin
  374. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  375. is_calljmp(actopcode) then
  376. begin
  377. oper.opr.typ:=OPR_SYMBOL;
  378. oper.opr.symbol:=hl;
  379. end
  380. else
  381. begin
  382. oper.InitRef;
  383. oper.opr.ref.symbol:=hl;
  384. end;
  385. end;
  386. procedure MaybeRecordOffset;
  387. var
  388. mangledname: string;
  389. hasdot : boolean;
  390. l,
  391. toffset,
  392. tsize : tcgint;
  393. begin
  394. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  395. exit;
  396. l:=0;
  397. mangledname:='';
  398. hasdot:=(actasmtoken=AS_DOT);
  399. if hasdot then
  400. begin
  401. if expr<>'' then
  402. begin
  403. BuildRecordOffsetSize(expr,toffset,tsize,mangledname,false);
  404. if (oper.opr.typ<>OPR_CONSTANT) and
  405. (mangledname<>'') then
  406. Message(asmr_e_wrong_sym_type);
  407. inc(l,toffset);
  408. oper.SetSize(tsize,true);
  409. case oper.opr.typ of
  410. OPR_REFERENCE: oper.opr.varsize := tsize;
  411. OPR_LOCAL: oper.opr.localvarsize := tsize;
  412. end;
  413. end;
  414. end;
  415. if actasmtoken in [AS_PLUS,AS_MINUS] then
  416. inc(l,BuildConstExpression(true,false));
  417. case oper.opr.typ of
  418. OPR_LOCAL :
  419. begin
  420. { don't allow direct access to fields of parameters, because that
  421. will generate buggy code. Allow it only for explicit typecasting }
  422. if hasdot and
  423. (not oper.hastype) and
  424. (oper.opr.localsym.typ=paravarsym) and
  425. (not(po_assembler in current_procinfo.procdef.procoptions) or
  426. (tparavarsym(oper.opr.localsym).paraloc[calleeside].location^.loc<>LOC_REGISTER) or
  427. (not is_implicit_pointer_object_type(oper.opr.localsym.vardef) and
  428. not paramanager.push_addr_param(oper.opr.localsym.varspez,oper.opr.localsym.vardef,current_procinfo.procdef.proccalloption))) then
  429. Message(asmr_e_cannot_access_field_directly_for_parameters);
  430. inc(oper.opr.localsymofs,l);
  431. inc(oper.opr.localconstoffset,l);
  432. end;
  433. OPR_CONSTANT :
  434. if (mangledname<>'') then
  435. begin
  436. if (oper.opr.val<>0) then
  437. Message(asmr_e_wrong_sym_type);
  438. oper.opr.typ:=OPR_SYMBOL;
  439. oper.opr.symbol:=current_asmdata.RefAsmSymbol(mangledname,AT_FUNCTION);
  440. end
  441. else
  442. inc(oper.opr.val,l);
  443. OPR_REFERENCE :
  444. begin
  445. inc(oper.opr.ref.offset,l);
  446. inc(oper.opr.constoffset,l);
  447. end;
  448. OPR_SYMBOL:
  449. Message(asmr_e_invalid_symbol_ref);
  450. else
  451. internalerror(200309221);
  452. end;
  453. end;
  454. function MaybeBuildReference:boolean;
  455. { Try to create a reference, if not a reference is found then false
  456. is returned }
  457. var
  458. mangledname: string;
  459. begin
  460. MaybeBuildReference:=true;
  461. case actasmtoken of
  462. AS_INTNUM:
  463. Begin
  464. { allow %segmentregister:number }
  465. if oper.opr.ref.segment<>NR_NO then
  466. begin
  467. // already done before calling oper.InitRef;
  468. if oper.opr.Ref.Offset <> 0 Then
  469. Message(asmr_e_invalid_reference_syntax)
  470. else
  471. begin
  472. oper.opr.Ref.Offset:=BuildConstExpression(true,false);
  473. if actasmtoken=AS_LPAREN then
  474. BuildReference(oper)
  475. else if (oper.opr.ref.segment <> NR_FS) and
  476. (oper.opr.ref.segment <> NR_GS) then
  477. Message(asmr_w_general_segment_with_constant);
  478. end;
  479. end
  480. else
  481. begin
  482. oper.opr.ref.offset:=BuildConstExpression(True,False);
  483. BuildReference(oper);
  484. end;
  485. end;
  486. AS_MINUS,
  487. AS_PLUS:
  488. Begin
  489. oper.opr.ref.offset:=BuildConstExpression(True,False);
  490. if actasmtoken<>AS_LPAREN then
  491. Message(asmr_e_invalid_reference_syntax)
  492. else
  493. BuildReference(oper);
  494. end;
  495. AS_LPAREN:
  496. BuildReference(oper);
  497. AS_ID: { only a variable is allowed ... }
  498. Begin
  499. tempstr:=actasmpattern;
  500. Consume(AS_ID);
  501. { typecasting? }
  502. if (actasmtoken=AS_LPAREN) and
  503. SearchType(tempstr,typesize) then
  504. begin
  505. oper.hastype:=true;
  506. Consume(AS_LPAREN);
  507. BuildOperand(oper);
  508. Consume(AS_RPAREN);
  509. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  510. oper.SetSize(typesize,true);
  511. end
  512. else
  513. if not oper.SetupVar(tempstr,false) then
  514. Message1(sym_e_unknown_id,tempstr);
  515. { record.field ? }
  516. if actasmtoken=AS_DOT then
  517. begin
  518. BuildRecordOffsetSize(tempstr,l,k,mangledname,false);
  519. if (mangledname<>'') then
  520. Message(asmr_e_invalid_reference_syntax);
  521. inc(oper.opr.ref.offset,l);
  522. case oper.opr.typ of
  523. OPR_REFERENCE: oper.opr.varsize := k;
  524. OPR_LOCAL: oper.opr.localvarsize := k;
  525. end;
  526. end;
  527. MaybeGetPICModifier(oper);
  528. case actasmtoken of
  529. AS_END,
  530. AS_SEPARATOR,
  531. AS_COMMA: ;
  532. AS_LPAREN:
  533. BuildReference(oper);
  534. else
  535. Begin
  536. Message(asmr_e_invalid_reference_syntax);
  537. Consume(actasmtoken);
  538. end;
  539. end; {end case }
  540. end;
  541. else
  542. MaybeBuildReference:=false;
  543. end; { end case }
  544. end;
  545. var
  546. tempreg : tregister;
  547. hl : tasmlabel;
  548. Begin
  549. expr:='';
  550. case actasmtoken of
  551. AS_LPAREN: { Memory reference or constant expression }
  552. Begin
  553. oper.InitRef;
  554. BuildReference(oper);
  555. end;
  556. AS_DOLLAR: { Constant expression }
  557. Begin
  558. Consume(AS_DOLLAR);
  559. BuildConstantOperand(oper);
  560. end;
  561. AS_INTNUM,
  562. AS_MINUS,
  563. AS_PLUS:
  564. Begin
  565. { Constant memory offset }
  566. { This must absolutely be followed by ( }
  567. oper.InitRef;
  568. oper.opr.ref.offset:=BuildConstExpression(True,False);
  569. if actasmtoken<>AS_LPAREN then
  570. Message(asmr_e_invalid_reference_syntax)
  571. else
  572. BuildReference(oper);
  573. end;
  574. AS_STAR: { Call from memory address }
  575. Begin
  576. Consume(AS_STAR);
  577. if actasmtoken=AS_REGISTER then
  578. begin
  579. oper.opr.typ:=OPR_REGISTER;
  580. oper.opr.reg:=actasmregister;
  581. oper.SetSize(tcgsize2size[reg_cgsize(actasmregister)],true);
  582. Consume(AS_REGISTER);
  583. end
  584. else
  585. begin
  586. oper.InitRef;
  587. if not MaybeBuildReference then
  588. Message(asmr_e_syn_operand);
  589. end;
  590. { this is only allowed for call's and jmp's }
  591. if not is_calljmp(actopcode) then
  592. Message(asmr_e_syn_operand);
  593. end;
  594. AS_ID: { A constant expression, or a Variable ref. }
  595. Begin
  596. { Local Label ? }
  597. if is_locallabel(actasmpattern) then
  598. begin
  599. CreateLocalLabel(actasmpattern,hl,false);
  600. Consume(AS_ID);
  601. AddLabelOperand(hl);
  602. end
  603. else
  604. { Check for label }
  605. if SearchLabel(actasmpattern,hl,false) then
  606. begin
  607. Consume(AS_ID);
  608. AddLabelOperand(hl);
  609. end
  610. else
  611. { probably a variable or normal expression }
  612. { or a procedure (such as in CALL ID) }
  613. Begin
  614. { is it a constant ? }
  615. if SearchIConstant(actasmpattern,l) then
  616. Begin
  617. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  618. Message(asmr_e_invalid_operand_type);
  619. BuildConstantOperand(oper);
  620. end
  621. else
  622. begin
  623. expr:=actasmpattern;
  624. Consume(AS_ID);
  625. { typecasting? }
  626. if (actasmtoken=AS_LPAREN) and
  627. SearchType(expr,typesize) then
  628. begin
  629. oper.hastype:=true;
  630. Consume(AS_LPAREN);
  631. BuildOperand(oper);
  632. Consume(AS_RPAREN);
  633. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  634. oper.SetSize(typesize,true);
  635. end
  636. else
  637. begin
  638. if oper.SetupVar(expr,false) then
  639. MaybeGetPICModifier(oper)
  640. else
  641. Begin
  642. { look for special symbols ... }
  643. if expr= '__HIGH' then
  644. begin
  645. consume(AS_LPAREN);
  646. if not oper.setupvar('high'+actasmpattern,false) then
  647. Message1(sym_e_unknown_id,'high'+actasmpattern);
  648. consume(AS_ID);
  649. consume(AS_RPAREN);
  650. end
  651. else
  652. if expr = '__RESULT' then
  653. oper.SetUpResult
  654. else
  655. if expr = '__SELF' then
  656. oper.SetupSelf
  657. else
  658. if expr = '__OLDEBP' then
  659. oper.SetupOldEBP
  660. else
  661. Message1(sym_e_unknown_id,expr);
  662. end;
  663. end;
  664. end;
  665. if oper.opr.typ<>OPR_NONE Then
  666. begin
  667. if (actasmtoken=AS_DOT) then
  668. MaybeRecordOffset;
  669. { add a constant expression? }
  670. if (actasmtoken=AS_PLUS) then
  671. begin
  672. l:=BuildConstExpression(true,false);
  673. case oper.opr.typ of
  674. OPR_CONSTANT :
  675. inc(oper.opr.val,l);
  676. OPR_LOCAL :
  677. begin
  678. inc(oper.opr.localsymofs,l);
  679. inc(oper.opr.localconstoffset, l);
  680. end;
  681. OPR_REFERENCE :
  682. begin
  683. inc(oper.opr.ref.offset,l);
  684. inc(oper.opr.constoffset, l);
  685. end;
  686. else
  687. internalerror(200309202);
  688. end;
  689. end;
  690. end;
  691. end;
  692. { Do we have a indexing reference, then parse it also }
  693. if actasmtoken=AS_LPAREN then
  694. BuildReference(oper);
  695. end;
  696. AS_REGISTER: { Register, a variable reference or a constant reference }
  697. Begin
  698. { save the type of register used. }
  699. tempreg:=actasmregister;
  700. Consume(AS_REGISTER);
  701. if actasmtoken = AS_COLON then
  702. Begin
  703. Consume(AS_COLON);
  704. oper.InitRef;
  705. oper.opr.ref.segment:=tempreg;
  706. { This must absolutely be followed by a reference }
  707. if not MaybeBuildReference then
  708. Begin
  709. Message(asmr_e_invalid_seg_override);
  710. Consume(actasmtoken);
  711. end;
  712. end
  713. { Simple register }
  714. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  715. Begin
  716. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  717. Message(asmr_e_invalid_operand_type);
  718. oper.opr.typ:=OPR_REGISTER;
  719. oper.opr.reg:=tempreg;
  720. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  721. end
  722. else
  723. Message(asmr_e_syn_operand);
  724. end;
  725. AS_END,
  726. AS_SEPARATOR,
  727. AS_COMMA: ;
  728. else
  729. Begin
  730. Message(asmr_e_syn_operand);
  731. Consume(actasmtoken);
  732. end;
  733. end; { end case }
  734. end;
  735. procedure tx86attreader.BuildOpCode(instr : tx86instruction);
  736. var
  737. operandnum : longint;
  738. PrefixOp,OverrideOp: tasmop;
  739. di_param, si_param: ShortInt;
  740. Begin
  741. PrefixOp:=A_None;
  742. OverrideOp:=A_None;
  743. { prefix seg opcode / prefix opcode }
  744. repeat
  745. if is_prefix(actopcode) then
  746. begin
  747. PrefixOp:=ActOpcode;
  748. with instr do
  749. begin
  750. opcode:=ActOpcode;
  751. condition:=ActCondition;
  752. opsize:=ActOpsize;
  753. ConcatInstruction(curlist);
  754. end;
  755. Consume(AS_OPCODE);
  756. end
  757. else
  758. if is_override(actopcode) then
  759. begin
  760. OverrideOp:=ActOpcode;
  761. with instr do
  762. begin
  763. opcode:=ActOpcode;
  764. condition:=ActCondition;
  765. opsize:=ActOpsize;
  766. ConcatInstruction(curlist);
  767. end;
  768. Consume(AS_OPCODE);
  769. end
  770. else
  771. break;
  772. { allow for newline as in gas styled syntax }
  773. while actasmtoken=AS_SEPARATOR do
  774. Consume(AS_SEPARATOR);
  775. until (actasmtoken<>AS_OPCODE);
  776. { opcode }
  777. if (actasmtoken<>AS_OPCODE) then
  778. Begin
  779. Message(asmr_e_invalid_or_missing_opcode);
  780. RecoverConsume(true);
  781. exit;
  782. end;
  783. { Fill the instr object with the current state }
  784. with instr do
  785. begin
  786. Opcode:=ActOpcode;
  787. condition:=ActCondition;
  788. opsize:=ActOpsize;
  789. end;
  790. { Valid combination of prefix/override and instruction ? }
  791. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  792. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  793. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  794. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  795. { We are reading operands, so opcode will be an AS_ID }
  796. operandnum:=1;
  797. Consume(AS_OPCODE);
  798. { Zero operand opcode ? }
  799. if actasmtoken in [AS_SEPARATOR,AS_END] then
  800. begin
  801. operandnum:=0;
  802. exit;
  803. end;
  804. { Read the operands }
  805. repeat
  806. case actasmtoken of
  807. AS_COMMA: { Operand delimiter }
  808. Begin
  809. if operandnum > Max_Operands then
  810. Message(asmr_e_too_many_operands)
  811. else
  812. Inc(operandnum);
  813. Consume(AS_COMMA);
  814. end;
  815. AS_SEPARATOR,
  816. AS_END : { End of asm operands for this opcode }
  817. begin
  818. break;
  819. end;
  820. else
  821. BuildOperand(instr.Operands[operandnum] as tx86operand);
  822. end; { end case }
  823. until false;
  824. instr.Ops:=operandnum;
  825. { handle string instructions with parameters }
  826. with instr do
  827. if is_x86_parameterless_string_op(opcode) and
  828. (Ops>=1) and (Ops<=2) then
  829. begin
  830. if opcode=A_MOVSD then
  831. begin
  832. { distinguish between MOVS and the SSE MOVSD instruction:
  833. MOVS must have memory 2 reference operands (there's no need
  834. to distinguish from SSE CMPSD, because the SSE version has 3
  835. arguments and we've already checked that above) }
  836. if (Ops=2) and (operands[1].opr.typ=OPR_REFERENCE) and (operands[2].opr.typ=OPR_REFERENCE) then
  837. begin
  838. opcode:=A_MOVS;
  839. opsize:=S_L;
  840. end;
  841. end
  842. else
  843. begin
  844. opsize:=get_x86_string_op_size(opcode);
  845. opcode:=x86_param2paramless_string_op(opcode);
  846. end;
  847. end;
  848. { Check for invalid ES: overrides }
  849. if is_x86_parameterized_string_op(instr.opcode) then
  850. begin
  851. si_param:=get_x86_string_op_si_param(instr.opcode);
  852. if si_param<>-1 then
  853. begin
  854. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  855. if si_param<=operandnum then
  856. with instr.operands[si_param] do
  857. if (opr.typ=OPR_REFERENCE) then
  858. begin
  859. if not((((opr.ref.index<>NR_NO) and
  860. (opr.ref.base=NR_NO) and
  861. (getregtype(opr.ref.index)=R_INTREGISTER) and
  862. (getsupreg(opr.ref.index)=RS_ESI)) or
  863. ((opr.ref.index=NR_NO) and
  864. (opr.ref.base<>NR_NO) and
  865. (getregtype(opr.ref.base)=R_INTREGISTER) and
  866. (getsupreg(opr.ref.base)=RS_ESI))) and
  867. (opr.ref.offset=0) and
  868. (opr.ref.scalefactor<=1) and
  869. (opr.ref.refaddr=addr_no) and
  870. (opr.ref.symbol=nil) and
  871. (opr.ref.relsymbol=nil)) then
  872. {$if defined(i8086)}
  873. Message1(asmr_w_invalid_reference,'(%si)');
  874. {$elseif defined(i386)}
  875. Message1(asmr_w_invalid_reference,'(%esi)');
  876. {$elseif defined(x86_64)}
  877. Message1(asmr_w_invalid_reference,'(%rsi)');
  878. {$endif}
  879. end;
  880. end;
  881. di_param:=get_x86_string_op_di_param(instr.opcode);
  882. if di_param<>-1 then
  883. begin
  884. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  885. if di_param<=operandnum then
  886. with instr.operands[di_param] do
  887. if (opr.typ=OPR_REFERENCE) then
  888. begin
  889. if (opr.ref.segment<>NR_NO) and
  890. (opr.ref.segment<>NR_ES) then
  891. Message(asmr_e_cannot_override_es_segment);
  892. if not((((opr.ref.index<>NR_NO) and
  893. (opr.ref.base=NR_NO) and
  894. (getregtype(opr.ref.index)=R_INTREGISTER) and
  895. (getsupreg(opr.ref.index)=RS_EDI)) or
  896. ((opr.ref.index=NR_NO) and
  897. (opr.ref.base<>NR_NO) and
  898. (getregtype(opr.ref.base)=R_INTREGISTER) and
  899. (getsupreg(opr.ref.base)=RS_EDI))) and
  900. (opr.ref.offset=0) and
  901. (opr.ref.scalefactor<=1) and
  902. (opr.ref.refaddr=addr_no) and
  903. (opr.ref.symbol=nil) and
  904. (opr.ref.relsymbol=nil)) then
  905. {$if defined(i8086)}
  906. Message1(asmr_w_invalid_reference,'(%di)');
  907. {$elseif defined(i386)}
  908. Message1(asmr_w_invalid_reference,'(%edi)');
  909. {$elseif defined(x86_64)}
  910. Message1(asmr_w_invalid_reference,'(%edi)');
  911. {$endif}
  912. end;
  913. end;
  914. { if two memory parameters, check whether their address sizes are equal }
  915. if (si_param<>-1) and (di_param<>-1) and
  916. (si_param<=operandnum) and (di_param<=operandnum) and
  917. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  918. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  919. begin
  920. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  921. get_ref_address_size(instr.operands[di_param].opr.ref) then
  922. Message(asmr_e_address_sizes_do_not_match);
  923. end;
  924. end;
  925. end;
  926. function tx86attreader.is_asmopcode(const s: string):boolean;
  927. var
  928. cond : string[4];
  929. cnd : tasmcond;
  930. len,
  931. j,
  932. sufidx,
  933. suflen : longint;
  934. Begin
  935. is_asmopcode:=FALSE;
  936. actopcode:=A_None;
  937. actcondition:=C_None;
  938. actopsize:=S_NO;
  939. { search for all possible suffixes }
  940. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  941. begin
  942. suflen:=length(att_sizesuffixstr[sufidx]);
  943. len:=length(s)-suflen;
  944. if copy(s,len+1,suflen)=att_sizesuffixstr[sufidx] then
  945. begin
  946. { Search opcodes }
  947. if len>0 then
  948. begin
  949. actopcode:=tasmop(PtrUInt(iasmops.Find(copy(s,1,len))));
  950. { movsd needs special handling because it has two namings in at&t syntax (movsl for string handling and
  951. movsd for the sse instruction) while only one in intel syntax (movsd, both string and sse)
  952. this cannot be expressed by the instruction table format so we have to hack around this here }
  953. if (actopcode = A_NONE) and (upper(s) = 'MOVSD') then
  954. actopcode := A_MOVSD;
  955. { cmpsd also needs special handling for pretty much the same reasons as movsd }
  956. if (actopcode = A_NONE) and (upper(s) = 'CMPSD') then
  957. actopcode := A_CMPSD;
  958. { disambiguation between A_MOVS (movsb/movsw/movsl/movsq) and
  959. A_MOVSX (movsbw/movsbl/movswl/movsbq/movswq) }
  960. if (actopcode = A_MOVS) and (suflen=2) then
  961. actopcode := A_MOVSX;
  962. { two-letter suffix is allowed by just a few instructions (movsx,movzx),
  963. and it is always required whenever allowed }
  964. if (gas_needsuffix[actopcode]=attsufINTdual) xor (suflen=2) then
  965. continue;
  966. if actopcode<>A_NONE then
  967. begin
  968. if gas_needsuffix[actopcode]=attsufFPU then
  969. actopsize:=att_sizefpusuffix[sufidx]
  970. else if gas_needsuffix[actopcode]=attsufFPUint then
  971. actopsize:=att_sizefpuintsuffix[sufidx]
  972. else if gas_needsuffix[actopcode]=attsufMM then
  973. actopsize:=att_sizemmsuffix[sufidx]
  974. else
  975. actopsize:=att_sizesuffix[sufidx];
  976. { only accept suffix from the same category that the opcode belongs to }
  977. if (actopsize<>S_NO) or (suflen=0) then
  978. begin
  979. actasmtoken:=AS_OPCODE;
  980. is_asmopcode:=TRUE;
  981. exit;
  982. end;
  983. end;
  984. end;
  985. { not found, check condition opcodes }
  986. j:=0;
  987. while (j<CondAsmOps) do
  988. begin
  989. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  990. begin
  991. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  992. if cond<>'' then
  993. begin
  994. for cnd:=low(TasmCond) to high(TasmCond) do
  995. if Cond=Upper(cond2str[cnd]) then
  996. begin
  997. actopcode:=CondASmOp[j];
  998. { conditional instructions (cmovcc, setcc) use only INT suffixes;
  999. other stuff like fcmovcc is represented as group of individual instructions }
  1000. if gas_needsuffix[actopcode]=attsufINT then
  1001. actopsize:=att_sizesuffix[sufidx];
  1002. { only accept suffix from the same category that the opcode belongs to }
  1003. if (actopsize<>S_NO) or (suflen=0) then
  1004. begin
  1005. actcondition:=cnd;
  1006. actasmtoken:=AS_OPCODE;
  1007. is_asmopcode:=TRUE;
  1008. exit;
  1009. end;
  1010. end;
  1011. end;
  1012. end;
  1013. inc(j);
  1014. end;
  1015. end;
  1016. end;
  1017. end;
  1018. procedure tx86attreader.handleopcode;
  1019. var
  1020. instr : Tx86Instruction;
  1021. begin
  1022. instr:=Tx86attInstruction.Create(Tx86Operand);
  1023. BuildOpcode(instr);
  1024. instr.AddReferenceSizes;
  1025. instr.SetInstructionOpsize;
  1026. instr.CheckOperandSizes;
  1027. instr.FixupOpcode;
  1028. instr.ConcatInstruction(curlist);
  1029. instr.Free;
  1030. end;
  1031. end.