rax86.pas 38 KB

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