rax86.pas 41 KB

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