rax86.pas 41 KB

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