rax86.pas 42 KB

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