rax86.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Handles the common x86 assembler reader routines
  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. {
  18. Contains the common x86 (i386 and x86-64) assembler reader routines.
  19. }
  20. unit rax86;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. cpubase,rautils,cclasses;
  26. { Parser helpers }
  27. function is_prefix(t:tasmop):boolean;
  28. function is_override(t:tasmop):boolean;
  29. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  30. Function CheckOverride(overrideop,op:tasmop): Boolean;
  31. Procedure FWaitWarning;
  32. type
  33. Tx86Operand=class(TOperand)
  34. opsize : topsize;
  35. Procedure SetSize(_size:longint;force:boolean);override;
  36. Procedure SetCorrectSize(opcode:tasmop);override;
  37. Function CheckOperand: boolean; override;
  38. end;
  39. Tx86Instruction=class(TInstruction)
  40. OpOrder : TOperandOrder;
  41. opsize : topsize;
  42. constructor Create(optype : tcoperand);override;
  43. { Operand sizes }
  44. procedure AddReferenceSizes; virtual;
  45. procedure SetInstructionOpsize;
  46. procedure CheckOperandSizes;
  47. procedure CheckNonCommutativeOpcodes;
  48. procedure SwapOperands;
  49. { Additional actions required by specific reader }
  50. procedure FixupOpcode;virtual;
  51. { opcode adding }
  52. function ConcatInstruction(p : TAsmList) : tai;override;
  53. end;
  54. const
  55. AsmPrefixes = 6;
  56. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  57. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ
  58. );
  59. AsmOverrides = 6;
  60. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  61. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  62. );
  63. CondAsmOps=3;
  64. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  65. A_CMOVcc, A_Jcc, A_SETcc
  66. );
  67. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  68. 'CMOV','J','SET'
  69. );
  70. implementation
  71. uses
  72. globtype,globals,systems,verbose,
  73. procinfo,
  74. cpuinfo,cgbase,cgutils,
  75. itcpugas,cgx86, symsym, cutils;
  76. {*****************************************************************************
  77. Parser Helpers
  78. *****************************************************************************}
  79. function is_prefix(t:tasmop):boolean;
  80. var
  81. i : longint;
  82. Begin
  83. is_prefix:=false;
  84. for i:=1 to AsmPrefixes do
  85. if t=AsmPrefix[i-1] then
  86. begin
  87. is_prefix:=true;
  88. exit;
  89. end;
  90. end;
  91. function is_override(t:tasmop):boolean;
  92. var
  93. i : longint;
  94. Begin
  95. is_override:=false;
  96. for i:=1 to AsmOverrides do
  97. if t=AsmOverride[i-1] then
  98. begin
  99. is_override:=true;
  100. exit;
  101. end;
  102. end;
  103. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  104. { Checks if the prefix is valid with the following opcode }
  105. { return false if not, otherwise true }
  106. Begin
  107. CheckPrefix := TRUE;
  108. (* Case prefix of
  109. A_REP,A_REPNE,A_REPE:
  110. Case opcode Of
  111. A_SCASB,A_SCASW,A_SCASD,
  112. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  113. Else
  114. Begin
  115. CheckPrefix := FALSE;
  116. exit;
  117. end;
  118. end; { case }
  119. A_LOCK:
  120. Case opcode Of
  121. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  122. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  123. Else
  124. Begin
  125. CheckPrefix := FALSE;
  126. Exit;
  127. end;
  128. end; { case }
  129. A_NONE: exit; { no prefix here }
  130. else
  131. CheckPrefix := FALSE;
  132. end; { end case } *)
  133. end;
  134. Function CheckOverride(overrideop,op:tasmop): Boolean;
  135. { Check if the override is valid, and if so then }
  136. { update the instr variable accordingly. }
  137. Begin
  138. CheckOverride := true;
  139. { Case instr.getinstruction of
  140. A_MOVS,A_XLAT,A_CMPS:
  141. Begin
  142. CheckOverride := TRUE;
  143. Message(assem_e_segment_override_not_supported);
  144. end
  145. end }
  146. end;
  147. Procedure FWaitWarning;
  148. begin
  149. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in current_settings.moduleswitches) then
  150. Message(asmr_w_fwait_emu_prob);
  151. end;
  152. {*****************************************************************************
  153. TX86Operand
  154. *****************************************************************************}
  155. Procedure Tx86Operand.SetSize(_size:longint;force:boolean);
  156. begin
  157. inherited SetSize(_size,force);
  158. { OS_64 will be set to S_L and be fixed later
  159. in SetCorrectSize }
  160. // multimedia register
  161. case _size of
  162. 16: size := OS_M128;
  163. 32: size := OS_M256;
  164. end;
  165. opsize:=TCGSize2Opsize[size];
  166. end;
  167. Procedure Tx86Operand.SetCorrectSize(opcode:tasmop);
  168. begin
  169. if gas_needsuffix[opcode]=attsufFPU then
  170. begin
  171. case size of
  172. OS_32 : opsize:=S_FS;
  173. OS_64 : opsize:=S_FL;
  174. end;
  175. end
  176. else if gas_needsuffix[opcode]=attsufFPUint then
  177. begin
  178. case size of
  179. OS_16 : opsize:=S_IS;
  180. OS_32 : opsize:=S_IL;
  181. OS_64 : opsize:=S_IQ;
  182. end;
  183. end
  184. else if gas_needsuffix[opcode]=AttSufMM then
  185. begin
  186. if (opr.typ=OPR_Reference) then
  187. begin
  188. case size of
  189. OS_32 : size := OS_M32;
  190. OS_64 : size := OS_M64;
  191. end;
  192. end;
  193. end
  194. else
  195. begin
  196. if size=OS_64 then
  197. opsize:=S_Q;
  198. end;
  199. end;
  200. Function Tx86Operand.CheckOperand: boolean;
  201. begin
  202. result:=true;
  203. if (opr.typ=OPR_Reference) then
  204. begin
  205. if not hasvar then
  206. begin
  207. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  208. begin
  209. if current_procinfo.procdef.proccalloption=pocall_register then
  210. message(asmr_w_no_direct_ebp_for_parameter)
  211. else
  212. message(asmr_w_direct_ebp_for_parameter_regcall);
  213. end
  214. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  215. message(asmr_w_direct_ebp_neg_offset)
  216. else if (getsupreg(opr.ref.base)=RS_ESP) and (opr.ref.offset<0) then
  217. message(asmr_w_direct_esp_neg_offset);
  218. end;
  219. if (cs_create_pic in current_settings.moduleswitches) and
  220. assigned(opr.ref.symbol) and
  221. not assigned(opr.ref.relsymbol) then
  222. begin
  223. if not(opr.ref.refaddr in [addr_pic,addr_pic_no_got]) then
  224. begin
  225. if (opr.ref.symbol.name <> '_GLOBAL_OFFSET_TABLE_') then
  226. begin
  227. message(asmr_e_need_pic_ref);
  228. result:=false;
  229. end
  230. else
  231. opr.ref.refaddr:=addr_pic;
  232. end
  233. else
  234. begin
  235. {$ifdef x86_64}
  236. { should probably be extended to i386, but there the situation
  237. is more complex and ELF-style PIC still need to be
  238. tested/debugged }
  239. if (opr.ref.symbol.bind in [AB_LOCAL,AB_PRIVATE_EXTERN]) and
  240. (opr.ref.refaddr=addr_pic) then
  241. message(asmr_w_useless_got_for_local)
  242. else if (opr.ref.symbol.bind in [AB_GLOBAL,AB_EXTERNAL,AB_COMMON,AB_WEAK_EXTERNAL]) and
  243. (opr.ref.refaddr=addr_pic_no_got) then
  244. message(asmr_w_global_access_without_got);
  245. {$endif x86_64}
  246. end;
  247. end;
  248. end;
  249. end;
  250. {*****************************************************************************
  251. T386Instruction
  252. *****************************************************************************}
  253. constructor Tx86Instruction.Create(optype : tcoperand);
  254. begin
  255. inherited Create(optype);
  256. Opsize:=S_NO;
  257. end;
  258. procedure Tx86Instruction.SwapOperands;
  259. begin
  260. Inherited SwapOperands;
  261. { mark the correct order }
  262. if OpOrder=op_intel then
  263. OpOrder:=op_att
  264. else
  265. OpOrder:=op_intel;
  266. end;
  267. const
  268. {$ifdef x86_64}
  269. topsize2memsize: array[topsize] of integer =
  270. (0, 8,16,32,64,8,8,16,8,16,32,
  271. 16,32,64,
  272. 16,32,64,0,0,
  273. 64,
  274. 0,0,0,
  275. 80,
  276. 128,
  277. 256
  278. );
  279. {$else}
  280. topsize2memsize: array[topsize] of integer =
  281. (0, 8,16,32,64,8,8,16,
  282. 16,32,64,
  283. 16,32,64,0,0,
  284. 64,
  285. 0,0,0,
  286. 80,
  287. 128,
  288. 256
  289. );
  290. {$endif}
  291. procedure Tx86Instruction.AddReferenceSizes;
  292. { this will add the sizes for references like [esi] which do not
  293. have the size set yet, it will take only the size if the other
  294. operand is a register }
  295. var
  296. operand2,i,j : longint;
  297. s : tasmsymbol;
  298. so : aint;
  299. ExistsMemRefNoSize: boolean;
  300. ExistsMemRef: boolean;
  301. ExistsConstNoSize: boolean;
  302. ExistsLocalSymSize: boolean;
  303. memrefsize: integer;
  304. memopsize: integer;
  305. memoffset: asizeint;
  306. s1: string;
  307. begin
  308. ExistsMemRefNoSize := false;
  309. ExistsMemRef := false;
  310. ExistsConstNoSize := false;
  311. ExistsLocalSymSize := false;
  312. // EXIST A MEMORY- OR CONSTANT-OPERAND WITHOUT SIZE ?
  313. for i := 1 to ops do
  314. begin
  315. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  316. begin
  317. ExistsMemRef := true;
  318. if (tx86operand(operands[i]).opsize = S_NO) then
  319. begin
  320. ExistsMemRefNoSize := true;
  321. case operands[i].opr.Typ of
  322. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  323. OPR_REFERENCE: ExistsLocalSymSize := true;
  324. end;
  325. end;
  326. end
  327. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  328. begin
  329. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  330. end;
  331. end;
  332. // ONLY SUPPORTED OPCODES WITH SSE- OR AVX-REGISTERS
  333. if (ExistsMemRef) and
  334. (MemRefInfo(opcode).ExistsSSEAVX) then
  335. begin
  336. // 1. WE HAVE AN SSE- OR AVX-OPCODE WITH MEMORY OPERAND
  337. if (not(ExistsMemRefNoSize)) or
  338. (ExistsLocalSymSize) then
  339. begin
  340. // 2. WE KNOWN THE MEMORYSIZE OF THE MEMORY-OPERAND OR WE CAN
  341. // CALC THE MEMORYSIZE
  342. // 3. CALC THE SIZE OF THE MEMORYOPERAND BY OPCODE-DEFINITION
  343. // 4. COMPARE THE SIZE FROM OPCODE-DEFINITION AND THE REAL MEMORY-OPERAND-SIZE
  344. // - validate memory-reference-size
  345. for i := 1 to ops do
  346. begin
  347. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  348. begin
  349. memrefsize := -1;
  350. case MemRefInfo(opcode).MemRefSize of
  351. msiMem8: memrefsize := 8;
  352. msiMem16: memrefsize := 16;
  353. msiMem32: memrefsize := 32;
  354. msiMem64: memrefsize := 64;
  355. msiMem128: memrefsize := 128;
  356. msiMem256: memrefsize := 256;
  357. msiMemRegSize
  358. : for j := 1 to ops do
  359. begin
  360. if operands[j].Opr.Typ = OPR_REGISTER then
  361. begin
  362. if (tx86operand(operands[j]).opsize <> S_NO) and
  363. (tx86operand(operands[j]).size <> OS_NO) then
  364. begin
  365. case tx86operand(operands[j]).opsize of
  366. S_B : memrefsize := 8;
  367. S_W : memrefsize := 16;
  368. S_L : memrefsize := 32;
  369. S_Q : memrefsize := 64;
  370. S_XMM : memrefsize := 128;
  371. S_YMM : memrefsize := 256;
  372. else Internalerror(777200);
  373. end;
  374. break;
  375. end;
  376. end;
  377. end;
  378. end;
  379. if memrefsize > -1 then
  380. begin
  381. // CALC REAL-MEMORY-OPERAND-SIZE AND A POSSIBLE OFFSET
  382. // OFFSET:
  383. // e.g. PAND XMM0, [RAX + 16] =>> OFFSET = 16 BYTES
  384. // PAND XMM0, [RAX + a.b + 10] =>> OFFSET = 10 BYTES (a = record-variable)
  385. memopsize := 0;
  386. case operands[i].opr.typ of
  387. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  388. OPR_REFERENCE: memopsize := operands[i].opr.varsize * 8;
  389. end;
  390. if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  391. if (memopsize > 0) and
  392. (memrefsize > 0) then
  393. begin
  394. memoffset := 0;
  395. case operands[i].opr.typ of
  396. OPR_LOCAL:
  397. memoffset := operands[i].opr.localconstoffset;
  398. OPR_REFERENCE:
  399. memoffset := operands[i].opr.constoffset;
  400. end;
  401. if memoffset < 0 then
  402. begin
  403. Message2(asmr_w_check_mem_operand_negative_offset,
  404. std_op2str[opcode],
  405. ToStr(memoffset));
  406. end
  407. else if (memopsize < (memrefsize + memoffset * 8)) then
  408. begin
  409. if memoffset = 0 then
  410. begin
  411. Message3(asmr_w_check_mem_operand_size3,
  412. std_op2str[opcode],
  413. ToStr(memopsize),
  414. ToStr(memrefsize)
  415. );
  416. end
  417. else
  418. begin
  419. Message4(asmr_w_check_mem_operand_size_offset,
  420. std_op2str[opcode],
  421. ToStr(memopsize),
  422. ToStr(memrefsize),
  423. ToStr(memoffset)
  424. );
  425. end;
  426. end;
  427. end;
  428. end;
  429. end;
  430. end;
  431. end;
  432. end;
  433. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  434. (MemRefInfo(opcode).ExistsSSEAVX) then
  435. begin
  436. for i := 1 to ops do
  437. begin
  438. if (tx86operand(operands[i]).opsize = S_NO) then
  439. begin
  440. case operands[i].Opr.Typ of
  441. OPR_REFERENCE:
  442. case MemRefInfo(opcode).MemRefSize of
  443. msiMem8:
  444. begin
  445. tx86operand(operands[i]).opsize := S_B;
  446. tx86operand(operands[i]).size := OS_8;
  447. end;
  448. msiMultiple8:
  449. begin
  450. tx86operand(operands[i]).opsize := S_B;
  451. tx86operand(operands[i]).size := OS_8;
  452. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"8 bit memory operand"');
  453. end;
  454. msiMem16:
  455. begin
  456. tx86operand(operands[i]).opsize := S_W;
  457. tx86operand(operands[i]).size := OS_16;
  458. end;
  459. msiMultiple16:
  460. begin
  461. tx86operand(operands[i]).opsize := S_W;
  462. tx86operand(operands[i]).size := OS_16;
  463. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"16 bit memory operand"');
  464. end;
  465. msiMem32:
  466. begin
  467. tx86operand(operands[i]).opsize := S_L;
  468. tx86operand(operands[i]).size := OS_32;
  469. end;
  470. msiMultiple32:
  471. begin
  472. tx86operand(operands[i]).opsize := S_L;
  473. tx86operand(operands[i]).size := OS_32;
  474. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"32 bit memory operand"');
  475. end;
  476. msiMem64:
  477. begin
  478. tx86operand(operands[i]).opsize := S_Q;
  479. tx86operand(operands[i]).size := OS_M64;
  480. end;
  481. msiMultiple64:
  482. begin
  483. tx86operand(operands[i]).opsize := S_Q;
  484. tx86operand(operands[i]).size := OS_M64;
  485. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"64 bit memory operand"');
  486. end;
  487. msiMem128:
  488. begin
  489. tx86operand(operands[i]).opsize := S_XMM;
  490. tx86operand(operands[i]).size := OS_M128;
  491. end;
  492. msiMultiple128:
  493. begin
  494. tx86operand(operands[i]).opsize := S_XMM;
  495. tx86operand(operands[i]).size := OS_M128;
  496. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"128 bit memory operand"');
  497. end;
  498. msiMem256:
  499. begin
  500. tx86operand(operands[i]).opsize := S_YMM;
  501. tx86operand(operands[i]).size := OS_M256;
  502. opsize := S_YMM;
  503. end;
  504. msiMultiple256:
  505. begin
  506. tx86operand(operands[i]).opsize := S_YMM;
  507. tx86operand(operands[i]).size := OS_M256;
  508. opsize := S_YMM;
  509. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"256 bit memory operand"');
  510. end;
  511. msiMemRegSize:
  512. begin
  513. // mem-ref-size = register size
  514. for j := 1 to ops do
  515. begin
  516. if operands[j].Opr.Typ = OPR_REGISTER then
  517. begin
  518. if (tx86operand(operands[j]).opsize <> S_NO) and
  519. (tx86operand(operands[j]).size <> OS_NO) then
  520. begin
  521. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  522. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  523. break;
  524. end
  525. else Message(asmr_e_unable_to_determine_reference_size);
  526. end;
  527. end;
  528. end;
  529. msiMemRegx64y128:
  530. begin
  531. for j := 1 to ops do
  532. begin
  533. if operands[j].Opr.Typ = OPR_REGISTER then
  534. begin
  535. case getsubreg(operands[j].opr.reg) of
  536. R_SUBMMX: begin
  537. tx86operand(operands[i]).opsize := S_Q;
  538. tx86operand(operands[i]).size := OS_M64;
  539. break;
  540. end;
  541. R_SUBMMY: begin
  542. tx86operand(operands[i]).opsize := S_XMM;
  543. tx86operand(operands[i]).size := OS_M128;
  544. break;
  545. end;
  546. else Message(asmr_e_unable_to_determine_reference_size);
  547. end;
  548. end;
  549. end;
  550. end;
  551. msiMemRegx64y256:
  552. begin
  553. for j := 1 to ops do
  554. begin
  555. if operands[j].Opr.Typ = OPR_REGISTER then
  556. begin
  557. case getsubreg(operands[j].opr.reg) of
  558. R_SUBMMX: begin
  559. tx86operand(operands[i]).opsize := S_Q;
  560. tx86operand(operands[i]).size := OS_M64;
  561. break;
  562. end;
  563. R_SUBMMY: begin
  564. tx86operand(operands[i]).opsize := S_YMM;
  565. tx86operand(operands[i]).size := OS_M256;
  566. break;
  567. end;
  568. else Message(asmr_e_unable_to_determine_reference_size);
  569. end;
  570. end;
  571. end;
  572. end;
  573. msiNoSize: ; // all memory-sizes are ok
  574. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  575. end;
  576. OPR_CONSTANT:
  577. case MemRefInfo(opcode).ConstSize of
  578. csiMem8: begin
  579. tx86operand(operands[i]).opsize := S_B;
  580. tx86operand(operands[i]).size := OS_8;
  581. end;
  582. csiMem16: begin
  583. tx86operand(operands[i]).opsize := S_W;
  584. tx86operand(operands[i]).size := OS_16;
  585. end;
  586. csiMem32: begin
  587. tx86operand(operands[i]).opsize := S_L;
  588. tx86operand(operands[i]).size := OS_32;
  589. end;
  590. end;
  591. end;
  592. end;
  593. end;
  594. end;
  595. for i:=1 to ops do
  596. begin
  597. operands[i].SetCorrectSize(opcode);
  598. if tx86operand(operands[i]).opsize=S_NO then
  599. begin
  600. {$ifdef x86_64}
  601. if (opcode=A_MOVQ) and
  602. (ops=2) and
  603. (operands[1].opr.typ=OPR_CONSTANT) then
  604. opsize:=S_Q
  605. else
  606. {$endif x86_64}
  607. case operands[i].Opr.Typ of
  608. OPR_LOCAL,
  609. OPR_REFERENCE :
  610. begin
  611. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  612. don't consider it. }
  613. if i=ops then
  614. operand2:=i-1
  615. else
  616. operand2:=i+1;
  617. if operand2>0 then
  618. begin
  619. { Only allow register as operand to take the size from }
  620. if operands[operand2].opr.typ=OPR_REGISTER then
  621. begin
  622. if ((opcode<>A_MOVD) and
  623. (opcode<>A_CVTSI2SS)) then
  624. begin
  625. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  626. // torsten - 31.01.2012
  627. // old: xmm/ymm-register operands have a opsize = "S_NO"
  628. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  629. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  630. // in this case is we need the old handling ("S_NO")
  631. // =>> ignore
  632. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  633. (tx86operand(operands[operand2]).opsize <> S_YMM) then
  634. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  635. else tx86operand(operands[operand2]).opsize := S_NO;
  636. end;
  637. end
  638. else
  639. begin
  640. { if no register then take the opsize (which is available with ATT),
  641. if not availble then give an error }
  642. if opsize<>S_NO then
  643. tx86operand(operands[i]).opsize:=opsize
  644. else
  645. begin
  646. if (m_delphi in current_settings.modeswitches) then
  647. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  648. else
  649. Message(asmr_e_unable_to_determine_reference_size);
  650. { recovery }
  651. tx86operand(operands[i]).opsize:=S_L;
  652. end;
  653. end;
  654. end
  655. else
  656. begin
  657. if opsize<>S_NO then
  658. tx86operand(operands[i]).opsize:=opsize
  659. end;
  660. end;
  661. OPR_SYMBOL :
  662. begin
  663. { Fix lea which need a reference }
  664. if opcode=A_LEA then
  665. begin
  666. s:=operands[i].opr.symbol;
  667. so:=operands[i].opr.symofs;
  668. operands[i].opr.typ:=OPR_REFERENCE;
  669. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  670. operands[i].opr.ref.symbol:=s;
  671. operands[i].opr.ref.offset:=so;
  672. end;
  673. {$ifdef x86_64}
  674. tx86operand(operands[i]).opsize:=S_Q;
  675. {$else x86_64}
  676. tx86operand(operands[i]).opsize:=S_L;
  677. {$endif x86_64}
  678. end;
  679. end;
  680. end;
  681. end;
  682. end;
  683. procedure Tx86Instruction.SetInstructionOpsize;
  684. begin
  685. if opsize<>S_NO then
  686. exit;
  687. if (OpOrder=op_intel) then
  688. SwapOperands;
  689. case ops of
  690. 0 : ;
  691. 1 :
  692. begin
  693. { "push es" must be stored as a long PM }
  694. if ((opcode=A_PUSH) or
  695. (opcode=A_POP)) and
  696. (operands[1].opr.typ=OPR_REGISTER) and
  697. is_segment_reg(operands[1].opr.reg) then
  698. opsize:=S_L
  699. else
  700. opsize:=tx86operand(operands[1]).opsize;
  701. end;
  702. 2 :
  703. begin
  704. case opcode of
  705. A_MOVZX,A_MOVSX :
  706. begin
  707. if tx86operand(operands[1]).opsize=S_NO then
  708. begin
  709. tx86operand(operands[1]).opsize:=S_B;
  710. if (m_delphi in current_settings.modeswitches) then
  711. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  712. else
  713. Message(asmr_e_unable_to_determine_reference_size);
  714. end;
  715. case tx86operand(operands[1]).opsize of
  716. S_W :
  717. case tx86operand(operands[2]).opsize of
  718. S_L :
  719. opsize:=S_WL;
  720. {$ifdef x86_64}
  721. S_Q :
  722. opsize:=S_WQ;
  723. {$endif}
  724. end;
  725. S_B :
  726. begin
  727. case tx86operand(operands[2]).opsize of
  728. S_W :
  729. opsize:=S_BW;
  730. S_L :
  731. opsize:=S_BL;
  732. {$ifdef x86_64}
  733. S_Q :
  734. opsize:=S_BQ;
  735. {$endif}
  736. end;
  737. end;
  738. end;
  739. end;
  740. A_MOVD : { movd is a move from a mmx register to a
  741. 32 bit register or memory, so no opsize is correct here PM }
  742. exit;
  743. A_MOVQ :
  744. opsize:=S_IQ;
  745. A_OUT :
  746. opsize:=tx86operand(operands[1]).opsize;
  747. else
  748. opsize:=tx86operand(operands[2]).opsize;
  749. end;
  750. end;
  751. 3,4 :
  752. opsize:=tx86operand(operands[ops]).opsize;
  753. end;
  754. end;
  755. procedure Tx86Instruction.CheckOperandSizes;
  756. var
  757. sizeerr : boolean;
  758. i : longint;
  759. begin
  760. { Check only the most common opcodes here, the others are done in
  761. the assembler pass }
  762. case opcode of
  763. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  764. A_CMP,A_MOV,
  765. A_ADD,A_SUB,A_ADC,A_SBB,
  766. A_AND,A_OR,A_TEST,A_XOR: ;
  767. else
  768. exit;
  769. end;
  770. { Handle the BW,BL,WL separatly }
  771. sizeerr:=false;
  772. { special push/pop selector case }
  773. if ((opcode=A_PUSH) or
  774. (opcode=A_POP)) and
  775. (operands[1].opr.typ=OPR_REGISTER) and
  776. is_segment_reg(operands[1].opr.reg) then
  777. exit;
  778. if opsize in [S_BW,S_BL,S_WL] then
  779. begin
  780. if ops<>2 then
  781. sizeerr:=true
  782. else
  783. begin
  784. case opsize of
  785. S_BW :
  786. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  787. S_BL :
  788. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  789. S_WL :
  790. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  791. end;
  792. end;
  793. end
  794. else
  795. begin
  796. for i:=1 to ops do
  797. begin
  798. if (operands[i].opr.typ<>OPR_CONSTANT) and
  799. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  800. (tx86operand(operands[i]).opsize<>opsize) then
  801. sizeerr:=true;
  802. end;
  803. end;
  804. if sizeerr then
  805. begin
  806. { if range checks are on then generate an error }
  807. if (cs_compilesystem in current_settings.moduleswitches) or
  808. not (cs_check_range in current_settings.localswitches) then
  809. Message(asmr_w_size_suffix_and_dest_dont_match)
  810. else
  811. Message(asmr_e_size_suffix_and_dest_dont_match);
  812. end;
  813. end;
  814. { This check must be done with the operand in ATT order
  815. i.e.after swapping in the intel reader
  816. but before swapping in the NASM and TASM writers PM }
  817. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  818. begin
  819. if (OpOrder=op_intel) then
  820. SwapOperands;
  821. if (
  822. (ops=2) and
  823. (operands[1].opr.typ=OPR_REGISTER) and
  824. (operands[2].opr.typ=OPR_REGISTER) and
  825. { if the first is ST and the second is also a register
  826. it is necessarily ST1 .. ST7 }
  827. ((operands[1].opr.reg=NR_ST) or
  828. (operands[1].opr.reg=NR_ST0))
  829. ) or
  830. (ops=0) then
  831. if opcode=A_FSUBR then
  832. opcode:=A_FSUB
  833. else if opcode=A_FSUB then
  834. opcode:=A_FSUBR
  835. else if opcode=A_FDIVR then
  836. opcode:=A_FDIV
  837. else if opcode=A_FDIV then
  838. opcode:=A_FDIVR
  839. else if opcode=A_FSUBRP then
  840. opcode:=A_FSUBP
  841. else if opcode=A_FSUBP then
  842. opcode:=A_FSUBRP
  843. else if opcode=A_FDIVRP then
  844. opcode:=A_FDIVP
  845. else if opcode=A_FDIVP then
  846. opcode:=A_FDIVRP;
  847. if (
  848. (ops=1) and
  849. (operands[1].opr.typ=OPR_REGISTER) and
  850. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  851. (operands[1].opr.reg<>NR_ST) and
  852. (operands[1].opr.reg<>NR_ST0)
  853. ) then
  854. if opcode=A_FSUBRP then
  855. opcode:=A_FSUBP
  856. else if opcode=A_FSUBP then
  857. opcode:=A_FSUBRP
  858. else if opcode=A_FDIVRP then
  859. opcode:=A_FDIVP
  860. else if opcode=A_FDIVP then
  861. opcode:=A_FDIVRP;
  862. end;
  863. procedure Tx86Instruction.FixupOpcode;
  864. begin
  865. { does nothing by default }
  866. end;
  867. {*****************************************************************************
  868. opcode Adding
  869. *****************************************************************************}
  870. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  871. var
  872. siz : topsize;
  873. i,asize : longint;
  874. ai : taicpu;
  875. begin
  876. if (OpOrder=op_intel) then
  877. SwapOperands;
  878. ai:=nil;
  879. for i:=1 to Ops do
  880. if not operands[i].CheckOperand then
  881. exit;
  882. { Get Opsize }
  883. if (opsize<>S_NO) or (Ops=0) then
  884. siz:=opsize
  885. else
  886. begin
  887. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  888. siz:=tx86operand(operands[1]).opsize
  889. else
  890. siz:=tx86operand(operands[Ops]).opsize;
  891. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  892. if (ops=2) and
  893. (tx86operand(operands[1]).opsize<>S_NO) and
  894. (tx86operand(operands[2]).opsize<>S_NO) and
  895. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  896. siz:=S_NO;
  897. end;
  898. if ((opcode=A_MOVD)or
  899. (opcode=A_CVTSI2SS)) and
  900. ((tx86operand(operands[1]).opsize=S_NO) or
  901. (tx86operand(operands[2]).opsize=S_NO)) then
  902. siz:=S_NO;
  903. { NASM does not support FADD without args
  904. as alias of FADDP
  905. and GNU AS interprets FADD without operand differently
  906. for version 2.9.1 and 2.9.5 !! }
  907. if (ops=0) and
  908. ((opcode=A_FADD) or
  909. (opcode=A_FMUL) or
  910. (opcode=A_FSUB) or
  911. (opcode=A_FSUBR) or
  912. (opcode=A_FDIV) or
  913. (opcode=A_FDIVR)) then
  914. begin
  915. if opcode=A_FADD then
  916. opcode:=A_FADDP
  917. else if opcode=A_FMUL then
  918. opcode:=A_FMULP
  919. else if opcode=A_FSUB then
  920. opcode:=A_FSUBP
  921. else if opcode=A_FSUBR then
  922. opcode:=A_FSUBRP
  923. else if opcode=A_FDIV then
  924. opcode:=A_FDIVP
  925. else if opcode=A_FDIVR then
  926. opcode:=A_FDIVRP;
  927. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  928. end;
  929. {It is valid to specify some instructions without operand size.}
  930. if siz=S_NO then
  931. begin
  932. if (ops=1) and (opcode=A_INT) then
  933. siz:=S_B;
  934. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) then
  935. siz:=S_W;
  936. if (ops=1) and (opcode=A_PUSH) then
  937. begin
  938. {We are a 32 compiler, assume 32-bit by default. This is Delphi
  939. compatible but bad coding practise.}
  940. siz:=S_L;
  941. message(asmr_w_unable_to_determine_reference_size_using_dword);
  942. end;
  943. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  944. if ops=1 then
  945. siz:=S_NEAR
  946. else
  947. siz:=S_FAR;
  948. end;
  949. { GNU AS interprets FDIV without operand differently
  950. for version 2.9.1 and 2.10
  951. we add explicit args to it !! }
  952. if (ops=0) and
  953. ((opcode=A_FSUBP) or
  954. (opcode=A_FSUBRP) or
  955. (opcode=A_FDIVP) or
  956. (opcode=A_FDIVRP) or
  957. (opcode=A_FSUB) or
  958. (opcode=A_FSUBR) or
  959. (opcode=A_FADD) or
  960. (opcode=A_FADDP) or
  961. (opcode=A_FDIV) or
  962. (opcode=A_FDIVR)) then
  963. begin
  964. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  965. ops:=2;
  966. operands[1].opr.typ:=OPR_REGISTER;
  967. operands[2].opr.typ:=OPR_REGISTER;
  968. operands[1].opr.reg:=NR_ST0;
  969. operands[2].opr.reg:=NR_ST1;
  970. end;
  971. if (ops=1) and
  972. (
  973. (operands[1].opr.typ=OPR_REGISTER) and
  974. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  975. (operands[1].opr.reg<>NR_ST) and
  976. (operands[1].opr.reg<>NR_ST0)
  977. ) and
  978. (
  979. (opcode=A_FSUBP) or
  980. (opcode=A_FSUBRP) or
  981. (opcode=A_FDIVP) or
  982. (opcode=A_FDIVRP) or
  983. (opcode=A_FADDP) or
  984. (opcode=A_FMULP)
  985. ) then
  986. begin
  987. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  988. ops:=2;
  989. operands[2].opr.typ:=OPR_REGISTER;
  990. operands[2].opr.reg:=operands[1].opr.reg;
  991. operands[1].opr.reg:=NR_ST0;
  992. end;
  993. if (ops=1) and
  994. (
  995. (operands[1].opr.typ=OPR_REGISTER) and
  996. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  997. (operands[1].opr.reg<>NR_ST) and
  998. (operands[1].opr.reg<>NR_ST0)
  999. ) and
  1000. (
  1001. (opcode=A_FSUB) or
  1002. (opcode=A_FSUBR) or
  1003. (opcode=A_FDIV) or
  1004. (opcode=A_FDIVR) or
  1005. (opcode=A_FADD) or
  1006. (opcode=A_FMUL)
  1007. ) then
  1008. begin
  1009. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  1010. ops:=2;
  1011. operands[2].opr.typ:=OPR_REGISTER;
  1012. operands[2].opr.reg:=NR_ST0;
  1013. end;
  1014. { I tried to convince Linus Torvalds to add
  1015. code to support ENTER instruction
  1016. (when raising a stack page fault)
  1017. but he replied that ENTER is a bad instruction and
  1018. Linux does not need to support it
  1019. So I think its at least a good idea to add a warning
  1020. if someone uses this in assembler code
  1021. FPC itself does not use it at all PM }
  1022. if (opcode=A_ENTER) and
  1023. (target_info.system in [system_i386_linux,system_i386_FreeBSD,system_i386_android]) then
  1024. Message(asmr_w_enter_not_supported_by_linux);
  1025. ai:=taicpu.op_none(opcode,siz);
  1026. ai.fileinfo:=filepos;
  1027. ai.SetOperandOrder(OpOrder);
  1028. ai.Ops:=Ops;
  1029. ai.Allocate_oper(Ops);
  1030. for i:=1 to Ops do
  1031. case operands[i].opr.typ of
  1032. OPR_CONSTANT :
  1033. ai.loadconst(i-1,operands[i].opr.val);
  1034. OPR_REGISTER:
  1035. ai.loadreg(i-1,operands[i].opr.reg);
  1036. OPR_SYMBOL:
  1037. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1038. OPR_LOCAL :
  1039. with operands[i].opr do
  1040. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1041. localscale,localgetoffset,localforceref);
  1042. OPR_REFERENCE:
  1043. begin
  1044. ai.loadref(i-1,operands[i].opr.ref);
  1045. if operands[i].size<>OS_NO then
  1046. begin
  1047. asize:=0;
  1048. case operands[i].size of
  1049. OS_8,OS_S8 :
  1050. asize:=OT_BITS8;
  1051. OS_16,OS_S16 :
  1052. asize:=OT_BITS16;
  1053. OS_32,OS_S32,OS_F32,OS_M32 :
  1054. asize:=OT_BITS32;
  1055. OS_64,OS_S64:
  1056. begin
  1057. { Only FPU operations know about 64bit values, for all
  1058. integer operations it is seen as 32bit
  1059. this applies only to i386, see tw16622}
  1060. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  1061. asize:=OT_BITS64
  1062. {$ifdef i386}
  1063. else
  1064. asize:=OT_BITS32
  1065. {$endif i386}
  1066. ;
  1067. end;
  1068. OS_F64,OS_C64, OS_M64 :
  1069. asize:=OT_BITS64;
  1070. OS_F80 :
  1071. asize:=OT_BITS80;
  1072. OS_128,OS_M128,OS_MS128:
  1073. asize := OT_BITS128;
  1074. OS_M256,OS_MS256:
  1075. asize := OT_BITS256;
  1076. end;
  1077. if asize<>0 then
  1078. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1079. end;
  1080. end;
  1081. end;
  1082. { Condition ? }
  1083. if condition<>C_None then
  1084. ai.SetCondition(condition);
  1085. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  1086. if (opcode=A_JMP) or (opcode=A_JCC) then
  1087. ai.is_jmp := True;
  1088. { Concat the opcode or give an error }
  1089. if assigned(ai) then
  1090. p.concat(ai)
  1091. else
  1092. Message(asmr_e_invalid_opcode_and_operand);
  1093. result:=ai;
  1094. end;
  1095. end.