rax86.pas 42 KB

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