rax86.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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. begin
  307. ExistsMemRefNoSize := false;
  308. ExistsMemRef := false;
  309. ExistsConstNoSize := false;
  310. ExistsLocalSymSize := false;
  311. // EXIST A MEMORY- OR CONSTANT-OPERAND WITHOUT SIZE ?
  312. for i := 1 to ops do
  313. begin
  314. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  315. begin
  316. ExistsMemRef := true;
  317. if (tx86operand(operands[i]).opsize = S_NO) then
  318. begin
  319. ExistsMemRefNoSize := true;
  320. case operands[i].opr.Typ of
  321. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  322. OPR_REFERENCE: ExistsLocalSymSize := true;
  323. end;
  324. end;
  325. end
  326. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  327. begin
  328. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  329. end;
  330. end;
  331. // ONLY SUPPORTED OPCODES WITH SSE- OR AVX-REGISTERS
  332. if (ExistsMemRef) and
  333. (MemRefInfo(opcode).ExistsSSEAVX) then
  334. begin
  335. // 1. WE HAVE AN SSE- OR AVX-OPCODE WITH MEMORY OPERAND
  336. if (not(ExistsMemRefNoSize)) or
  337. (ExistsLocalSymSize) then
  338. begin
  339. // 2. WE KNOWN THE MEMORYSIZE OF THE MEMORY-OPERAND OR WE CAN
  340. // CALC THE MEMORYSIZE
  341. // 3. CALC THE SIZE OF THE MEMORYOPERAND BY OPCODE-DEFINITION
  342. // 4. COMPARE THE SIZE FROM OPCODE-DEFINITION AND THE REAL MEMORY-OPERAND-SIZE
  343. // - validate memory-reference-size
  344. for i := 1 to ops do
  345. begin
  346. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  347. begin
  348. memrefsize := -1;
  349. case MemRefInfo(opcode).MemRefSize of
  350. msiMem8: memrefsize := 8;
  351. msiMem16: memrefsize := 16;
  352. msiMem32: memrefsize := 32;
  353. msiMem64: memrefsize := 64;
  354. msiMem128: memrefsize := 128;
  355. msiMem256: memrefsize := 256;
  356. msiMemRegSize
  357. : for j := 1 to ops do
  358. begin
  359. if operands[j].Opr.Typ = OPR_REGISTER then
  360. begin
  361. if (tx86operand(operands[j]).opsize <> S_NO) and
  362. (tx86operand(operands[j]).size <> OS_NO) then
  363. begin
  364. case tx86operand(operands[j]).opsize of
  365. S_B : memrefsize := 8;
  366. S_W : memrefsize := 16;
  367. S_L : memrefsize := 32;
  368. S_Q : memrefsize := 64;
  369. S_XMM : memrefsize := 128;
  370. S_YMM : memrefsize := 256;
  371. else Internalerror(777200);
  372. end;
  373. break;
  374. end;
  375. end;
  376. end;
  377. end;
  378. if memrefsize > -1 then
  379. begin
  380. // CALC REAL-MEMORY-OPERAND-SIZE AND A POSSIBLE OFFSET
  381. // OFFSET:
  382. // e.g. PAND XMM0, [RAX + 16] =>> OFFSET = 16 BYTES
  383. // PAND XMM0, [RAX + a.b + 10] =>> OFFSET = 10 BYTES (a = record-variable)
  384. memopsize := 0;
  385. case operands[i].opr.typ of
  386. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  387. OPR_REFERENCE: memopsize := operands[i].opr.varsize * 8;
  388. end;
  389. if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  390. if (memopsize > 0) and
  391. (memrefsize > 0) then
  392. begin
  393. memoffset := 0;
  394. case operands[i].opr.typ of
  395. OPR_LOCAL:
  396. memoffset := operands[i].opr.localconstoffset;
  397. OPR_REFERENCE:
  398. memoffset := operands[i].opr.constoffset;
  399. end;
  400. if memoffset < 0 then
  401. begin
  402. Message2(asmr_w_check_mem_operand_negative_offset,
  403. std_op2str[opcode],
  404. ToStr(memoffset));
  405. end
  406. else if (memopsize < (memrefsize + memoffset * 8)) then
  407. begin
  408. if memoffset = 0 then
  409. begin
  410. Message3(asmr_w_check_mem_operand_size3,
  411. std_op2str[opcode],
  412. ToStr(memopsize),
  413. ToStr(memrefsize)
  414. );
  415. end
  416. else
  417. begin
  418. Message4(asmr_w_check_mem_operand_size_offset,
  419. std_op2str[opcode],
  420. ToStr(memopsize),
  421. ToStr(memrefsize),
  422. ToStr(memoffset)
  423. );
  424. end;
  425. end;
  426. end;
  427. end;
  428. end;
  429. end;
  430. end;
  431. end;
  432. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  433. (MemRefInfo(opcode).ExistsSSEAVX) then
  434. begin
  435. for i := 1 to ops do
  436. begin
  437. if (tx86operand(operands[i]).opsize = S_NO) then
  438. begin
  439. case operands[i].Opr.Typ of
  440. OPR_REFERENCE:
  441. case MemRefInfo(opcode).MemRefSize of
  442. msiMem8:
  443. begin
  444. tx86operand(operands[i]).opsize := S_B;
  445. tx86operand(operands[i]).size := OS_8;
  446. end;
  447. msiMultiple8:
  448. begin
  449. tx86operand(operands[i]).opsize := S_B;
  450. tx86operand(operands[i]).size := OS_8;
  451. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"8 bit memory operand"');
  452. end;
  453. msiMem16:
  454. begin
  455. tx86operand(operands[i]).opsize := S_W;
  456. tx86operand(operands[i]).size := OS_16;
  457. end;
  458. msiMultiple16:
  459. begin
  460. tx86operand(operands[i]).opsize := S_W;
  461. tx86operand(operands[i]).size := OS_16;
  462. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"16 bit memory operand"');
  463. end;
  464. msiMem32:
  465. begin
  466. tx86operand(operands[i]).opsize := S_L;
  467. tx86operand(operands[i]).size := OS_32;
  468. end;
  469. msiMultiple32:
  470. begin
  471. tx86operand(operands[i]).opsize := S_L;
  472. tx86operand(operands[i]).size := OS_32;
  473. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"32 bit memory operand"');
  474. end;
  475. msiMem64:
  476. begin
  477. tx86operand(operands[i]).opsize := S_Q;
  478. tx86operand(operands[i]).size := OS_M64;
  479. end;
  480. msiMultiple64:
  481. begin
  482. tx86operand(operands[i]).opsize := S_Q;
  483. tx86operand(operands[i]).size := OS_M64;
  484. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"64 bit memory operand"');
  485. end;
  486. msiMem128:
  487. begin
  488. tx86operand(operands[i]).opsize := S_XMM;
  489. tx86operand(operands[i]).size := OS_M128;
  490. end;
  491. msiMultiple128:
  492. begin
  493. tx86operand(operands[i]).opsize := S_XMM;
  494. tx86operand(operands[i]).size := OS_M128;
  495. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"128 bit memory operand"');
  496. end;
  497. msiMem256:
  498. begin
  499. tx86operand(operands[i]).opsize := S_YMM;
  500. tx86operand(operands[i]).size := OS_M256;
  501. opsize := S_YMM;
  502. end;
  503. msiMultiple256:
  504. begin
  505. tx86operand(operands[i]).opsize := S_YMM;
  506. tx86operand(operands[i]).size := OS_M256;
  507. opsize := S_YMM;
  508. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"256 bit memory operand"');
  509. end;
  510. msiMemRegSize:
  511. begin
  512. // mem-ref-size = register size
  513. for j := 1 to ops do
  514. begin
  515. if operands[j].Opr.Typ = OPR_REGISTER then
  516. begin
  517. if (tx86operand(operands[j]).opsize <> S_NO) and
  518. (tx86operand(operands[j]).size <> OS_NO) then
  519. begin
  520. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  521. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  522. break;
  523. end
  524. else Message(asmr_e_unable_to_determine_reference_size);
  525. end;
  526. end;
  527. end;
  528. msiMemRegx16y32:
  529. begin
  530. for j := 1 to ops do
  531. begin
  532. if operands[j].Opr.Typ = OPR_REGISTER then
  533. begin
  534. case getsubreg(operands[j].opr.reg) of
  535. R_SUBMMX: begin
  536. tx86operand(operands[i]).opsize := S_L;
  537. tx86operand(operands[i]).size := OS_M16;
  538. break;
  539. end;
  540. R_SUBMMY: begin
  541. tx86operand(operands[i]).opsize := S_Q;
  542. tx86operand(operands[i]).size := OS_M32;
  543. break;
  544. end;
  545. else Message(asmr_e_unable_to_determine_reference_size);
  546. end;
  547. end;
  548. end;
  549. end;
  550. msiMemRegx32y64:
  551. begin
  552. for j := 1 to ops do
  553. begin
  554. if operands[j].Opr.Typ = OPR_REGISTER then
  555. begin
  556. case getsubreg(operands[j].opr.reg) of
  557. R_SUBMMX: begin
  558. tx86operand(operands[i]).opsize := S_L;
  559. tx86operand(operands[i]).size := OS_M32;
  560. break;
  561. end;
  562. R_SUBMMY: begin
  563. tx86operand(operands[i]).opsize := S_Q;
  564. tx86operand(operands[i]).size := OS_M64;
  565. break;
  566. end;
  567. else Message(asmr_e_unable_to_determine_reference_size);
  568. end;
  569. end;
  570. end;
  571. end;
  572. msiMemRegx64y128:
  573. begin
  574. for j := 1 to ops do
  575. begin
  576. if operands[j].Opr.Typ = OPR_REGISTER then
  577. begin
  578. case getsubreg(operands[j].opr.reg) of
  579. R_SUBMMX: begin
  580. tx86operand(operands[i]).opsize := S_Q;
  581. tx86operand(operands[i]).size := OS_M64;
  582. break;
  583. end;
  584. R_SUBMMY: begin
  585. tx86operand(operands[i]).opsize := S_XMM;
  586. tx86operand(operands[i]).size := OS_M128;
  587. break;
  588. end;
  589. else Message(asmr_e_unable_to_determine_reference_size);
  590. end;
  591. end;
  592. end;
  593. end;
  594. msiMemRegx64y256:
  595. begin
  596. for j := 1 to ops do
  597. begin
  598. if operands[j].Opr.Typ = OPR_REGISTER then
  599. begin
  600. case getsubreg(operands[j].opr.reg) of
  601. R_SUBMMX: begin
  602. tx86operand(operands[i]).opsize := S_Q;
  603. tx86operand(operands[i]).size := OS_M64;
  604. break;
  605. end;
  606. R_SUBMMY: begin
  607. tx86operand(operands[i]).opsize := S_YMM;
  608. tx86operand(operands[i]).size := OS_M256;
  609. break;
  610. end;
  611. else Message(asmr_e_unable_to_determine_reference_size);
  612. end;
  613. end;
  614. end;
  615. end;
  616. msiNoSize: ; // all memory-sizes are ok
  617. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  618. end;
  619. OPR_CONSTANT:
  620. case MemRefInfo(opcode).ConstSize of
  621. csiMem8: begin
  622. tx86operand(operands[i]).opsize := S_B;
  623. tx86operand(operands[i]).size := OS_8;
  624. end;
  625. csiMem16: begin
  626. tx86operand(operands[i]).opsize := S_W;
  627. tx86operand(operands[i]).size := OS_16;
  628. end;
  629. csiMem32: begin
  630. tx86operand(operands[i]).opsize := S_L;
  631. tx86operand(operands[i]).size := OS_32;
  632. end;
  633. end;
  634. end;
  635. end;
  636. end;
  637. end;
  638. for i:=1 to ops do
  639. begin
  640. operands[i].SetCorrectSize(opcode);
  641. if tx86operand(operands[i]).opsize=S_NO then
  642. begin
  643. {$ifdef x86_64}
  644. if (opcode=A_MOVQ) and
  645. (ops=2) and
  646. (operands[1].opr.typ=OPR_CONSTANT) then
  647. opsize:=S_Q
  648. else
  649. {$endif x86_64}
  650. case operands[i].Opr.Typ of
  651. OPR_LOCAL,
  652. OPR_REFERENCE :
  653. begin
  654. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  655. don't consider it. }
  656. if i=ops then
  657. operand2:=i-1
  658. else
  659. operand2:=i+1;
  660. if operand2>0 then
  661. begin
  662. { Only allow register as operand to take the size from }
  663. if operands[operand2].opr.typ=OPR_REGISTER then
  664. begin
  665. if ((opcode<>A_MOVD) and
  666. (opcode<>A_CVTSI2SS)) then
  667. begin
  668. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  669. // torsten - 31.01.2012
  670. // old: xmm/ymm-register operands have a opsize = "S_NO"
  671. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  672. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  673. // in this case is we need the old handling ("S_NO")
  674. // =>> ignore
  675. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  676. (tx86operand(operands[operand2]).opsize <> S_YMM) then
  677. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  678. else tx86operand(operands[operand2]).opsize := S_NO;
  679. end;
  680. end
  681. else
  682. begin
  683. { if no register then take the opsize (which is available with ATT),
  684. if not availble then give an error }
  685. if opsize<>S_NO then
  686. tx86operand(operands[i]).opsize:=opsize
  687. else
  688. begin
  689. if (m_delphi in current_settings.modeswitches) then
  690. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  691. else
  692. Message(asmr_e_unable_to_determine_reference_size);
  693. { recovery }
  694. tx86operand(operands[i]).opsize:=S_L;
  695. end;
  696. end;
  697. end
  698. else
  699. begin
  700. if opsize<>S_NO then
  701. tx86operand(operands[i]).opsize:=opsize
  702. end;
  703. end;
  704. OPR_SYMBOL :
  705. begin
  706. { Fix lea which need a reference }
  707. if opcode=A_LEA then
  708. begin
  709. s:=operands[i].opr.symbol;
  710. so:=operands[i].opr.symofs;
  711. operands[i].opr.typ:=OPR_REFERENCE;
  712. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  713. operands[i].opr.ref.symbol:=s;
  714. operands[i].opr.ref.offset:=so;
  715. end;
  716. {$ifdef x86_64}
  717. tx86operand(operands[i]).opsize:=S_Q;
  718. {$else x86_64}
  719. tx86operand(operands[i]).opsize:=S_L;
  720. {$endif x86_64}
  721. end;
  722. end;
  723. end;
  724. end;
  725. end;
  726. procedure Tx86Instruction.SetInstructionOpsize;
  727. begin
  728. if opsize<>S_NO then
  729. exit;
  730. if (OpOrder=op_intel) then
  731. SwapOperands;
  732. case ops of
  733. 0 : ;
  734. 1 :
  735. begin
  736. { "push es" must be stored as a long PM }
  737. if ((opcode=A_PUSH) or
  738. (opcode=A_POP)) and
  739. (operands[1].opr.typ=OPR_REGISTER) and
  740. is_segment_reg(operands[1].opr.reg) then
  741. opsize:=S_L
  742. else
  743. opsize:=tx86operand(operands[1]).opsize;
  744. end;
  745. 2 :
  746. begin
  747. case opcode of
  748. A_MOVZX,A_MOVSX :
  749. begin
  750. if tx86operand(operands[1]).opsize=S_NO then
  751. begin
  752. tx86operand(operands[1]).opsize:=S_B;
  753. if (m_delphi in current_settings.modeswitches) then
  754. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  755. else
  756. Message(asmr_e_unable_to_determine_reference_size);
  757. end;
  758. case tx86operand(operands[1]).opsize of
  759. S_W :
  760. case tx86operand(operands[2]).opsize of
  761. S_L :
  762. opsize:=S_WL;
  763. {$ifdef x86_64}
  764. S_Q :
  765. opsize:=S_WQ;
  766. {$endif}
  767. end;
  768. S_B :
  769. begin
  770. case tx86operand(operands[2]).opsize of
  771. S_W :
  772. opsize:=S_BW;
  773. S_L :
  774. opsize:=S_BL;
  775. {$ifdef x86_64}
  776. S_Q :
  777. opsize:=S_BQ;
  778. {$endif}
  779. end;
  780. end;
  781. end;
  782. end;
  783. A_MOVD : { movd is a move from a mmx register to a
  784. 32 bit register or memory, so no opsize is correct here PM }
  785. exit;
  786. A_MOVQ :
  787. opsize:=S_IQ;
  788. A_OUT :
  789. opsize:=tx86operand(operands[1]).opsize;
  790. else
  791. opsize:=tx86operand(operands[2]).opsize;
  792. end;
  793. end;
  794. 3,4 :
  795. opsize:=tx86operand(operands[ops]).opsize;
  796. end;
  797. end;
  798. procedure Tx86Instruction.CheckOperandSizes;
  799. var
  800. sizeerr : boolean;
  801. i : longint;
  802. begin
  803. { Check only the most common opcodes here, the others are done in
  804. the assembler pass }
  805. case opcode of
  806. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  807. A_CMP,A_MOV,
  808. A_ADD,A_SUB,A_ADC,A_SBB,
  809. A_AND,A_OR,A_TEST,A_XOR: ;
  810. else
  811. exit;
  812. end;
  813. { Handle the BW,BL,WL separatly }
  814. sizeerr:=false;
  815. { special push/pop selector case }
  816. if ((opcode=A_PUSH) or
  817. (opcode=A_POP)) and
  818. (operands[1].opr.typ=OPR_REGISTER) and
  819. is_segment_reg(operands[1].opr.reg) then
  820. exit;
  821. if opsize in [S_BW,S_BL,S_WL] then
  822. begin
  823. if ops<>2 then
  824. sizeerr:=true
  825. else
  826. begin
  827. case opsize of
  828. S_BW :
  829. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  830. S_BL :
  831. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  832. S_WL :
  833. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  834. end;
  835. end;
  836. end
  837. else
  838. begin
  839. for i:=1 to ops do
  840. begin
  841. if (operands[i].opr.typ<>OPR_CONSTANT) and
  842. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  843. (tx86operand(operands[i]).opsize<>opsize) then
  844. sizeerr:=true;
  845. end;
  846. end;
  847. if sizeerr then
  848. begin
  849. { if range checks are on then generate an error }
  850. if (cs_compilesystem in current_settings.moduleswitches) or
  851. not (cs_check_range in current_settings.localswitches) then
  852. Message(asmr_w_size_suffix_and_dest_dont_match)
  853. else
  854. Message(asmr_e_size_suffix_and_dest_dont_match);
  855. end;
  856. end;
  857. { This check must be done with the operand in ATT order
  858. i.e.after swapping in the intel reader
  859. but before swapping in the NASM and TASM writers PM }
  860. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  861. begin
  862. if (OpOrder=op_intel) then
  863. SwapOperands;
  864. if (
  865. (ops=2) and
  866. (operands[1].opr.typ=OPR_REGISTER) and
  867. (operands[2].opr.typ=OPR_REGISTER) and
  868. { if the first is ST and the second is also a register
  869. it is necessarily ST1 .. ST7 }
  870. ((operands[1].opr.reg=NR_ST) or
  871. (operands[1].opr.reg=NR_ST0))
  872. ) or
  873. (ops=0) then
  874. if opcode=A_FSUBR then
  875. opcode:=A_FSUB
  876. else if opcode=A_FSUB then
  877. opcode:=A_FSUBR
  878. else if opcode=A_FDIVR then
  879. opcode:=A_FDIV
  880. else if opcode=A_FDIV then
  881. opcode:=A_FDIVR
  882. else if opcode=A_FSUBRP then
  883. opcode:=A_FSUBP
  884. else if opcode=A_FSUBP then
  885. opcode:=A_FSUBRP
  886. else if opcode=A_FDIVRP then
  887. opcode:=A_FDIVP
  888. else if opcode=A_FDIVP then
  889. opcode:=A_FDIVRP;
  890. if (
  891. (ops=1) and
  892. (operands[1].opr.typ=OPR_REGISTER) and
  893. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  894. (operands[1].opr.reg<>NR_ST) and
  895. (operands[1].opr.reg<>NR_ST0)
  896. ) then
  897. if opcode=A_FSUBRP then
  898. opcode:=A_FSUBP
  899. else if opcode=A_FSUBP then
  900. opcode:=A_FSUBRP
  901. else if opcode=A_FDIVRP then
  902. opcode:=A_FDIVP
  903. else if opcode=A_FDIVP then
  904. opcode:=A_FDIVRP;
  905. end;
  906. procedure Tx86Instruction.FixupOpcode;
  907. begin
  908. { does nothing by default }
  909. end;
  910. {*****************************************************************************
  911. opcode Adding
  912. *****************************************************************************}
  913. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  914. var
  915. siz : topsize;
  916. i,asize : longint;
  917. ai : taicpu;
  918. begin
  919. ConcatInstruction:=nil;
  920. if (OpOrder=op_intel) then
  921. SwapOperands;
  922. ai:=nil;
  923. for i:=1 to Ops do
  924. if not operands[i].CheckOperand then
  925. exit;
  926. { Get Opsize }
  927. if (opsize<>S_NO) or (Ops=0) then
  928. siz:=opsize
  929. else
  930. begin
  931. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  932. siz:=tx86operand(operands[1]).opsize
  933. else
  934. siz:=tx86operand(operands[Ops]).opsize;
  935. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  936. if (ops=2) and
  937. (tx86operand(operands[1]).opsize<>S_NO) and
  938. (tx86operand(operands[2]).opsize<>S_NO) and
  939. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  940. siz:=S_NO;
  941. end;
  942. if ((opcode=A_MOVD)or
  943. (opcode=A_CVTSI2SS)) and
  944. ((tx86operand(operands[1]).opsize=S_NO) or
  945. (tx86operand(operands[2]).opsize=S_NO)) then
  946. siz:=S_NO;
  947. { NASM does not support FADD without args
  948. as alias of FADDP
  949. and GNU AS interprets FADD without operand differently
  950. for version 2.9.1 and 2.9.5 !! }
  951. if (ops=0) and
  952. ((opcode=A_FADD) or
  953. (opcode=A_FMUL) or
  954. (opcode=A_FSUB) or
  955. (opcode=A_FSUBR) or
  956. (opcode=A_FDIV) or
  957. (opcode=A_FDIVR)) then
  958. begin
  959. if opcode=A_FADD then
  960. opcode:=A_FADDP
  961. else if opcode=A_FMUL then
  962. opcode:=A_FMULP
  963. else if opcode=A_FSUB then
  964. opcode:=A_FSUBP
  965. else if opcode=A_FSUBR then
  966. opcode:=A_FSUBRP
  967. else if opcode=A_FDIV then
  968. opcode:=A_FDIVP
  969. else if opcode=A_FDIVR then
  970. opcode:=A_FDIVRP;
  971. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  972. end;
  973. {It is valid to specify some instructions without operand size.}
  974. if siz=S_NO then
  975. begin
  976. if (ops=1) and (opcode=A_INT) then
  977. siz:=S_B;
  978. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) then
  979. siz:=S_W;
  980. if (ops=1) and (opcode=A_PUSH) then
  981. begin
  982. {We are a 32 compiler, assume 32-bit by default. This is Delphi
  983. compatible but bad coding practise.}
  984. siz:=S_L;
  985. message(asmr_w_unable_to_determine_reference_size_using_dword);
  986. end;
  987. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  988. if ops=1 then
  989. siz:=S_NEAR
  990. else
  991. siz:=S_FAR;
  992. end;
  993. { GNU AS interprets FDIV without operand differently
  994. for version 2.9.1 and 2.10
  995. we add explicit args to it !! }
  996. if (ops=0) and
  997. ((opcode=A_FSUBP) or
  998. (opcode=A_FSUBRP) or
  999. (opcode=A_FDIVP) or
  1000. (opcode=A_FDIVRP) or
  1001. (opcode=A_FSUB) or
  1002. (opcode=A_FSUBR) or
  1003. (opcode=A_FADD) or
  1004. (opcode=A_FADDP) or
  1005. (opcode=A_FDIV) or
  1006. (opcode=A_FDIVR)) then
  1007. begin
  1008. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  1009. ops:=2;
  1010. operands[1].opr.typ:=OPR_REGISTER;
  1011. operands[2].opr.typ:=OPR_REGISTER;
  1012. operands[1].opr.reg:=NR_ST0;
  1013. operands[2].opr.reg:=NR_ST1;
  1014. end;
  1015. if (ops=1) and
  1016. (
  1017. (operands[1].opr.typ=OPR_REGISTER) and
  1018. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1019. (operands[1].opr.reg<>NR_ST) and
  1020. (operands[1].opr.reg<>NR_ST0)
  1021. ) and
  1022. (
  1023. (opcode=A_FSUBP) or
  1024. (opcode=A_FSUBRP) or
  1025. (opcode=A_FDIVP) or
  1026. (opcode=A_FDIVRP) or
  1027. (opcode=A_FADDP) or
  1028. (opcode=A_FMULP)
  1029. ) then
  1030. begin
  1031. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  1032. ops:=2;
  1033. operands[2].opr.typ:=OPR_REGISTER;
  1034. operands[2].opr.reg:=operands[1].opr.reg;
  1035. operands[1].opr.reg:=NR_ST0;
  1036. end;
  1037. if (ops=1) and
  1038. (
  1039. (operands[1].opr.typ=OPR_REGISTER) and
  1040. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1041. (operands[1].opr.reg<>NR_ST) and
  1042. (operands[1].opr.reg<>NR_ST0)
  1043. ) and
  1044. (
  1045. (opcode=A_FSUB) or
  1046. (opcode=A_FSUBR) or
  1047. (opcode=A_FDIV) or
  1048. (opcode=A_FDIVR) or
  1049. (opcode=A_FADD) or
  1050. (opcode=A_FMUL)
  1051. ) then
  1052. begin
  1053. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  1054. ops:=2;
  1055. operands[2].opr.typ:=OPR_REGISTER;
  1056. operands[2].opr.reg:=NR_ST0;
  1057. end;
  1058. { I tried to convince Linus Torvalds to add
  1059. code to support ENTER instruction
  1060. (when raising a stack page fault)
  1061. but he replied that ENTER is a bad instruction and
  1062. Linux does not need to support it
  1063. So I think its at least a good idea to add a warning
  1064. if someone uses this in assembler code
  1065. FPC itself does not use it at all PM }
  1066. if (opcode=A_ENTER) and
  1067. (target_info.system in [system_i386_linux,system_i386_FreeBSD,system_i386_android]) then
  1068. Message(asmr_w_enter_not_supported_by_linux);
  1069. ai:=taicpu.op_none(opcode,siz);
  1070. ai.fileinfo:=filepos;
  1071. ai.SetOperandOrder(OpOrder);
  1072. ai.Ops:=Ops;
  1073. ai.Allocate_oper(Ops);
  1074. for i:=1 to Ops do
  1075. case operands[i].opr.typ of
  1076. OPR_CONSTANT :
  1077. ai.loadconst(i-1,operands[i].opr.val);
  1078. OPR_REGISTER:
  1079. ai.loadreg(i-1,operands[i].opr.reg);
  1080. OPR_SYMBOL:
  1081. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1082. OPR_LOCAL :
  1083. with operands[i].opr do
  1084. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1085. localscale,localgetoffset,localforceref);
  1086. OPR_REFERENCE:
  1087. begin
  1088. ai.loadref(i-1,operands[i].opr.ref);
  1089. if operands[i].size<>OS_NO then
  1090. begin
  1091. asize:=0;
  1092. case operands[i].size of
  1093. OS_8,OS_S8 :
  1094. asize:=OT_BITS8;
  1095. OS_16,OS_S16, OS_M16:
  1096. asize:=OT_BITS16;
  1097. OS_32,OS_S32,OS_F32,OS_M32 :
  1098. asize:=OT_BITS32;
  1099. OS_64,OS_S64:
  1100. begin
  1101. { Only FPU operations know about 64bit values, for all
  1102. integer operations it is seen as 32bit
  1103. this applies only to i386, see tw16622}
  1104. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  1105. asize:=OT_BITS64
  1106. {$ifdef i386}
  1107. else
  1108. asize:=OT_BITS32
  1109. {$endif i386}
  1110. ;
  1111. end;
  1112. OS_F64,OS_C64, OS_M64 :
  1113. asize:=OT_BITS64;
  1114. OS_F80 :
  1115. asize:=OT_BITS80;
  1116. OS_128,OS_M128,OS_MS128:
  1117. asize := OT_BITS128;
  1118. OS_M256,OS_MS256:
  1119. asize := OT_BITS256;
  1120. end;
  1121. if asize<>0 then
  1122. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1123. end;
  1124. end;
  1125. end;
  1126. { Condition ? }
  1127. if condition<>C_None then
  1128. ai.SetCondition(condition);
  1129. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  1130. if (opcode=A_JMP) or (opcode=A_JCC) then
  1131. ai.is_jmp := True;
  1132. { Concat the opcode or give an error }
  1133. if assigned(ai) then
  1134. p.concat(ai)
  1135. else
  1136. Message(asmr_e_invalid_opcode_and_operand);
  1137. result:=ai;
  1138. end;
  1139. end.