rax86.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  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. vopext : smallint; // bitmask: vector-operand extention AVX512 (e.g. vaddps xmm0 {k1} {z})
  36. vbcst : byte;
  37. Procedure SetSize(_size:longint;force:boolean);override;
  38. Procedure SetCorrectSize(opcode:tasmop);override;
  39. Function CheckOperand: boolean; override;
  40. { handles the @Code symbol }
  41. Procedure SetupCode;
  42. { handles the @Data symbol }
  43. Procedure SetupData;
  44. constructor create; override;
  45. end;
  46. { Operands are always in AT&T order.
  47. Intel reader attaches them right-to-left, then shifts to start with 1 }
  48. { Tx86Instruction }
  49. Tx86Instruction=class(TInstruction)
  50. opsize : topsize;
  51. constructor Create(optype : tcoperand);override;
  52. { Operand sizes }
  53. procedure AddReferenceSizes; virtual;
  54. procedure SetInstructionOpsize;
  55. procedure CheckOperandSizes;
  56. procedure CheckNonCommutativeOpcodes;
  57. { Additional actions required by specific reader }
  58. procedure FixupOpcode;virtual;
  59. { opcode adding }
  60. function ConcatInstruction(p : TAsmList) : tai;override;
  61. function getstring: string;
  62. end;
  63. const
  64. AsmPrefixes = 8{$ifdef i8086}+2{$endif i8086};
  65. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  66. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ,A_XACQUIRE,A_XRELEASE{$ifdef i8086},A_REPC,A_REPNC{$endif i8086}
  67. );
  68. AsmOverrides = 6;
  69. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  70. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  71. );
  72. CondAsmOps=3;
  73. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  74. A_CMOVcc, A_Jcc, A_SETcc
  75. );
  76. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  77. 'CMOV','J','SET'
  78. );
  79. implementation
  80. uses
  81. globtype,globals,systems,verbose,
  82. procinfo,
  83. cgbase,cgutils,
  84. itcpugas,cgx86, cutils;
  85. {*****************************************************************************
  86. Parser Helpers
  87. *****************************************************************************}
  88. function is_prefix(t:tasmop):boolean;
  89. var
  90. i : longint;
  91. Begin
  92. is_prefix:=false;
  93. for i:=1 to AsmPrefixes do
  94. if t=AsmPrefix[i-1] then
  95. begin
  96. is_prefix:=true;
  97. exit;
  98. end;
  99. end;
  100. function is_override(t:tasmop):boolean;
  101. var
  102. i : longint;
  103. Begin
  104. is_override:=false;
  105. for i:=1 to AsmOverrides do
  106. if t=AsmOverride[i-1] then
  107. begin
  108. is_override:=true;
  109. exit;
  110. end;
  111. end;
  112. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  113. { Checks if the prefix is valid with the following opcode }
  114. { return false if not, otherwise true }
  115. Begin
  116. CheckPrefix := TRUE;
  117. (* Case prefix of
  118. A_REP,A_REPNE,A_REPE:
  119. Case opcode Of
  120. A_SCASB,A_SCASW,A_SCASD,
  121. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  122. Else
  123. Begin
  124. CheckPrefix := FALSE;
  125. exit;
  126. end;
  127. end; { case }
  128. A_LOCK:
  129. Case opcode Of
  130. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  131. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  132. Else
  133. Begin
  134. CheckPrefix := FALSE;
  135. Exit;
  136. end;
  137. end; { case }
  138. A_NONE: exit; { no prefix here }
  139. else
  140. CheckPrefix := FALSE;
  141. end; { end case } *)
  142. end;
  143. Function CheckOverride(overrideop,op:tasmop): Boolean;
  144. { Check if the override is valid, and if so then }
  145. { update the instr variable accordingly. }
  146. Begin
  147. CheckOverride := true;
  148. { Case instr.getinstruction of
  149. A_MOVS,A_XLAT,A_CMPS:
  150. Begin
  151. CheckOverride := TRUE;
  152. Message(assem_e_segment_override_not_supported);
  153. end
  154. end }
  155. end;
  156. Procedure FWaitWarning;
  157. begin
  158. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in current_settings.moduleswitches) then
  159. Message(asmr_w_fwait_emu_prob);
  160. end;
  161. {*****************************************************************************
  162. TX86Operand
  163. *****************************************************************************}
  164. Procedure Tx86Operand.SetSize(_size:longint;force:boolean);
  165. begin
  166. inherited SetSize(_size,force);
  167. { OS_64 will be set to S_L and be fixed later
  168. in SetCorrectSize }
  169. // multimedia register
  170. case _size of
  171. 16: size := OS_M128;
  172. 32: size := OS_M256;
  173. 64: size := OS_M512;
  174. end;
  175. {$ifdef i8086}
  176. { allows e.g. using 32-bit registers in i8086 inline asm }
  177. if size in [OS_32,OS_S32] then
  178. opsize:=S_L
  179. else
  180. {$endif i8086}
  181. opsize:=TCGSize2Opsize[size];
  182. end;
  183. Procedure Tx86Operand.SetCorrectSize(opcode:tasmop);
  184. begin
  185. if gas_needsuffix[opcode]=attsufFPU then
  186. begin
  187. case size of
  188. OS_32 : opsize:=S_FS;
  189. OS_64 : opsize:=S_FL;
  190. end;
  191. end
  192. else if gas_needsuffix[opcode]=attsufFPUint then
  193. begin
  194. case size of
  195. OS_16 : opsize:=S_IS;
  196. OS_32 : opsize:=S_IL;
  197. OS_64 : opsize:=S_IQ;
  198. end;
  199. end
  200. else if gas_needsuffix[opcode]=AttSufMM then
  201. begin
  202. if (opr.typ=OPR_Reference) then
  203. begin
  204. case size of
  205. OS_32 : size := OS_M32;
  206. OS_64 : size := OS_M64;
  207. end;
  208. end;
  209. end
  210. else
  211. begin
  212. if size=OS_64 then
  213. opsize:=S_Q;
  214. end;
  215. end;
  216. Function Tx86Operand.CheckOperand: boolean;
  217. begin
  218. result:=true;
  219. if (opr.typ=OPR_Reference) then
  220. begin
  221. if not hasvar then
  222. begin
  223. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  224. begin
  225. if current_procinfo.procdef.proccalloption=pocall_register then
  226. message(asmr_w_no_direct_ebp_for_parameter)
  227. else
  228. message(asmr_w_direct_ebp_for_parameter_regcall);
  229. end
  230. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  231. message(asmr_w_direct_ebp_neg_offset)
  232. else if (getsupreg(opr.ref.base)=RS_ESP) and (opr.ref.offset<0) then
  233. message(asmr_w_direct_esp_neg_offset);
  234. end;
  235. if (cs_create_pic in current_settings.moduleswitches) and
  236. assigned(opr.ref.symbol) and
  237. not assigned(opr.ref.relsymbol) then
  238. begin
  239. if not(opr.ref.refaddr in [addr_pic,addr_pic_no_got]) then
  240. begin
  241. if (opr.ref.symbol.name <> '_GLOBAL_OFFSET_TABLE_') then
  242. begin
  243. message(asmr_e_need_pic_ref);
  244. result:=false;
  245. end
  246. else
  247. opr.ref.refaddr:=addr_pic;
  248. end
  249. else
  250. begin
  251. {$ifdef x86_64}
  252. { should probably be extended to i386, but there the situation
  253. is more complex and ELF-style PIC still need to be
  254. tested/debugged }
  255. if (opr.ref.symbol.bind in [AB_LOCAL,AB_PRIVATE_EXTERN]) and
  256. (opr.ref.refaddr=addr_pic) then
  257. message(asmr_w_useless_got_for_local)
  258. else if (opr.ref.symbol.bind in [AB_GLOBAL,AB_EXTERNAL,AB_COMMON,AB_WEAK_EXTERNAL]) and
  259. (opr.ref.refaddr=addr_pic_no_got) then
  260. message(asmr_w_global_access_without_got);
  261. {$endif x86_64}
  262. end;
  263. end;
  264. end;
  265. end;
  266. procedure Tx86Operand.SetupCode;
  267. begin
  268. {$ifdef i8086}
  269. opr.typ:=OPR_SYMBOL;
  270. opr.symofs:=0;
  271. opr.symbol:=current_asmdata.RefAsmSymbol(current_procinfo.procdef.mangledname,AT_FUNCTION);
  272. opr.symseg:=true;
  273. opr.sym_farproc_entry:=false;
  274. {$else i8086}
  275. Message(asmr_w_CODE_and_DATA_not_supported);
  276. {$endif i8086}
  277. end;
  278. procedure Tx86Operand.SetupData;
  279. begin
  280. {$ifdef i8086}
  281. InitRef;
  282. if current_settings.x86memorymodel=mm_huge then
  283. opr.ref.refaddr:=addr_fardataseg
  284. else
  285. opr.ref.refaddr:=addr_dgroup;
  286. {$else i8086}
  287. Message(asmr_w_CODE_and_DATA_not_supported);
  288. {$endif i8086}
  289. end;
  290. constructor Tx86Operand.create;
  291. begin
  292. inherited;
  293. vopext := 0;
  294. vbcst := 0;
  295. end;
  296. {*****************************************************************************
  297. T386Instruction
  298. *****************************************************************************}
  299. constructor Tx86Instruction.Create(optype : tcoperand);
  300. begin
  301. inherited Create(optype);
  302. Opsize:=S_NO;
  303. end;
  304. procedure Tx86Instruction.AddReferenceSizes;
  305. { this will add the sizes for references like [esi] which do not
  306. have the size set yet, it will take only the size if the other
  307. operand is a register }
  308. var
  309. operand2,i,j,k : longint;
  310. s : tasmsymbol;
  311. so : aint;
  312. ExistsMemRefNoSize: boolean;
  313. ExistsMemRef: boolean;
  314. ExistsConstNoSize: boolean;
  315. ExistConst: boolean;
  316. ExistsLocalSymSize: boolean;
  317. ExistsBCST: boolean;
  318. memrefsize: integer;
  319. memopsize: integer;
  320. memoffset: asizeint;
  321. vbcst: byte;
  322. mmregs: Set of TSubregister;
  323. multiplicator: integer;
  324. bcst1,bcst2: string;
  325. begin
  326. ExistsMemRefNoSize := false;
  327. ExistsMemRef := false;
  328. ExistsConstNoSize := false;
  329. ExistsLocalSymSize := false;
  330. ExistsBCST := false;
  331. // EXIST A MEMORY- OR CONSTANT-OPERAND WITHOUT SIZE ?
  332. for i := 1 to ops do
  333. begin
  334. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  335. begin
  336. ExistsMemRef := true;
  337. ExistsBCST := (MemRefInfo(opcode).ExistsSSEAVX) and
  338. (tx86operand(operands[i]).vbcst <> 0);
  339. if (tx86operand(operands[i]).opsize = S_NO) then
  340. begin
  341. ExistsMemRefNoSize := true;
  342. case operands[i].opr.Typ of
  343. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  344. OPR_REFERENCE: ExistsLocalSymSize := true;
  345. end;
  346. end;
  347. end
  348. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  349. begin
  350. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  351. end;
  352. end;
  353. // ONLY SUPPORTED OPCODES WITH SSE- OR AVX-REGISTERS
  354. if (ExistsMemRef) and
  355. (MemRefInfo(opcode).ExistsSSEAVX) then
  356. begin
  357. // 1. WE HAVE AN SSE- OR AVX-OPCODE WITH MEMORY OPERAND
  358. if (not(ExistsMemRefNoSize)) or
  359. (ExistsLocalSymSize) then
  360. begin
  361. // 2. WE KNOWN THE MEMORYSIZE OF THE MEMORY-OPERAND OR WE CAN
  362. // CALC THE MEMORYSIZE
  363. // 3. CALC THE SIZE OF THE MEMORYOPERAND BY OPCODE-DEFINITION
  364. // 4. COMPARE THE SIZE FROM OPCODE-DEFINITION AND THE REAL MEMORY-OPERAND-SIZE
  365. // - validate memory-reference-size
  366. for i := 1 to ops do
  367. begin
  368. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  369. begin
  370. memrefsize := -1;
  371. if ExistsBCST then
  372. begin
  373. case MemRefInfo(opcode).MemRefSizeBCST of
  374. msbBCST32: memrefsize := 32;
  375. msbBCST64: memrefsize := 64;
  376. end;
  377. end
  378. else
  379. case MemRefInfo(opcode).MemRefSize of
  380. msiMem8: memrefsize := 8;
  381. msiMem16: memrefsize := 16;
  382. msiMem32: memrefsize := 32;
  383. msiMem64: memrefsize := 64;
  384. msiMem128: memrefsize := 128;
  385. msiMem256: memrefsize := 256;
  386. msiMemRegSize:
  387. for j := 1 to ops do
  388. begin
  389. if operands[j].Opr.Typ = OPR_REGISTER then
  390. begin
  391. if (tx86operand(operands[j]).opsize <> S_NO) and
  392. (tx86operand(operands[j]).size <> OS_NO) then
  393. begin
  394. case tx86operand(operands[j]).opsize of
  395. S_B : memrefsize := 8;
  396. S_W : memrefsize := 16;
  397. S_L : memrefsize := 32;
  398. S_Q : memrefsize := 64;
  399. S_XMM : memrefsize := 128;
  400. S_YMM : memrefsize := 256;
  401. S_ZMM : memrefsize := 512;
  402. else Internalerror(777200);
  403. end;
  404. break;
  405. end;
  406. end;
  407. end;
  408. msiMemRegConst128,
  409. msiMemRegConst256,
  410. msiMemRegConst512:
  411. begin
  412. for j := 1 to ops do
  413. begin
  414. if operands[j].Opr.Typ = OPR_CONSTANT then
  415. begin
  416. for k := 1 to ops do
  417. begin
  418. if operands[k].Opr.Typ = OPR_REGISTER then
  419. begin
  420. if (tx86operand(operands[k]).opsize <> S_NO) and
  421. (tx86operand(operands[k]).size <> OS_NO) then
  422. begin
  423. case tx86operand(operands[k]).opsize of
  424. S_B : memrefsize := 8;
  425. S_W : memrefsize := 16;
  426. S_L : memrefsize := 32;
  427. S_Q : memrefsize := 64;
  428. S_XMM : memrefsize := 128;
  429. S_YMM : memrefsize := 256;
  430. S_ZMM : memrefsize := 512;
  431. else Internalerror(777200);
  432. end;
  433. break;
  434. end;
  435. end;
  436. end;
  437. break;
  438. end;
  439. end;
  440. // no exists const-operand
  441. if memrefsize = -1 then
  442. begin
  443. case MemRefInfo(opcode).MemRefSize of
  444. msiMemRegConst128: memrefsize := 128;
  445. msiMemRegConst256: memrefsize := 256;
  446. msiMemRegConst512: memrefsize := 512;
  447. else Internalerror(777200);
  448. end;
  449. end;
  450. end;
  451. end;
  452. if memrefsize > -1 then
  453. begin
  454. // CALC REAL-MEMORY-OPERAND-SIZE AND A POSSIBLE OFFSET
  455. // OFFSET:
  456. // e.g. PAND XMM0, [RAX + 16] =>> OFFSET = 16 BYTES
  457. // PAND XMM0, [RAX + a.b + 10] =>> OFFSET = 10 BYTES (a = record-variable)
  458. memopsize := 0;
  459. case operands[i].opr.typ of
  460. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  461. OPR_REFERENCE:
  462. if operands[i].opr.ref.refaddr = addr_pic then
  463. memopsize := sizeof(pint) * 8
  464. else
  465. memopsize := operands[i].opr.varsize * 8;
  466. end;
  467. if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  468. if (memopsize > 0) and
  469. (memrefsize > 0) then
  470. begin
  471. memoffset := 0;
  472. case operands[i].opr.typ of
  473. OPR_LOCAL:
  474. memoffset := operands[i].opr.localconstoffset;
  475. OPR_REFERENCE:
  476. memoffset := operands[i].opr.constoffset;
  477. end;
  478. if memoffset < 0 then
  479. begin
  480. Message2(asmr_w_check_mem_operand_negative_offset,
  481. std_op2str[opcode],
  482. ToStr(memoffset));
  483. end
  484. else if (memopsize < (memrefsize + memoffset * 8)) then
  485. begin
  486. if memoffset = 0 then
  487. begin
  488. Message3(asmr_w_check_mem_operand_size3,
  489. std_op2str[opcode],
  490. ToStr(memopsize),
  491. ToStr(memrefsize)
  492. );
  493. end
  494. else
  495. begin
  496. Message4(asmr_w_check_mem_operand_size_offset,
  497. std_op2str[opcode],
  498. ToStr(memopsize),
  499. ToStr(memrefsize),
  500. ToStr(memoffset)
  501. );
  502. end;
  503. end;
  504. end;
  505. end;
  506. end;
  507. end;
  508. end;
  509. end;
  510. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  511. (MemRefInfo(opcode).ExistsSSEAVX) then
  512. begin
  513. for i := 1 to ops do
  514. begin
  515. if (tx86operand(operands[i]).opsize = S_NO) then
  516. begin
  517. case operands[i].Opr.Typ of
  518. OPR_REFERENCE:
  519. begin
  520. if ExistsBCST then
  521. begin
  522. case MemRefInfo(opcode).MemRefSizeBCST of
  523. msbBCST32: begin
  524. tx86operand(operands[i]).opsize := S_L;
  525. tx86operand(operands[i]).size := OS_32;
  526. end;
  527. msbBCST64: begin
  528. tx86operand(operands[i]).opsize := S_Q;
  529. tx86operand(operands[i]).size := OS_M64;
  530. end;
  531. end;
  532. end
  533. else
  534. case MemRefInfo(opcode).MemRefSize of
  535. msiMem8:
  536. begin
  537. tx86operand(operands[i]).opsize := S_B;
  538. tx86operand(operands[i]).size := OS_8;
  539. end;
  540. msiMultiple8:
  541. begin
  542. tx86operand(operands[i]).opsize := S_B;
  543. tx86operand(operands[i]).size := OS_8;
  544. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"8 bit memory operand"');
  545. end;
  546. msiMem16:
  547. begin
  548. tx86operand(operands[i]).opsize := S_W;
  549. tx86operand(operands[i]).size := OS_16;
  550. end;
  551. msiMultiple16:
  552. begin
  553. tx86operand(operands[i]).opsize := S_W;
  554. tx86operand(operands[i]).size := OS_16;
  555. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"16 bit memory operand"');
  556. end;
  557. msiMem32:
  558. begin
  559. tx86operand(operands[i]).opsize := S_L;
  560. tx86operand(operands[i]).size := OS_32;
  561. end;
  562. msiMultiple32:
  563. begin
  564. tx86operand(operands[i]).opsize := S_L;
  565. tx86operand(operands[i]).size := OS_32;
  566. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"32 bit memory operand"');
  567. end;
  568. msiMem64:
  569. begin
  570. tx86operand(operands[i]).opsize := S_Q;
  571. tx86operand(operands[i]).size := OS_M64;
  572. end;
  573. msiMultiple64:
  574. begin
  575. tx86operand(operands[i]).opsize := S_Q;
  576. tx86operand(operands[i]).size := OS_M64;
  577. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"64 bit memory operand"');
  578. end;
  579. msiMem128:
  580. begin
  581. tx86operand(operands[i]).opsize := S_XMM;
  582. tx86operand(operands[i]).size := OS_M128;
  583. end;
  584. msiMultiple128:
  585. begin
  586. tx86operand(operands[i]).opsize := S_XMM;
  587. tx86operand(operands[i]).size := OS_M128;
  588. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"128 bit memory operand"');
  589. end;
  590. msiMem256:
  591. begin
  592. tx86operand(operands[i]).opsize := S_YMM;
  593. tx86operand(operands[i]).size := OS_M256;
  594. opsize := S_YMM;
  595. end;
  596. msiMultiple256:
  597. begin
  598. tx86operand(operands[i]).opsize := S_YMM;
  599. tx86operand(operands[i]).size := OS_M256;
  600. opsize := S_YMM;
  601. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"256 bit memory operand"');
  602. end;
  603. msiMem512:
  604. begin
  605. tx86operand(operands[i]).opsize := S_ZMM;
  606. tx86operand(operands[i]).size := OS_M512;
  607. opsize := S_ZMM;
  608. end;
  609. msiMultiple512:
  610. begin
  611. tx86operand(operands[i]).opsize := S_ZMM;
  612. tx86operand(operands[i]).size := OS_M512;
  613. opsize := S_ZMM;
  614. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"512 bit memory operand"');
  615. end;
  616. msiMemRegSize:
  617. begin
  618. // mem-ref-size = register size
  619. for j := 1 to ops do
  620. begin
  621. if operands[j].Opr.Typ = OPR_REGISTER then
  622. begin
  623. if (tx86operand(operands[j]).opsize <> S_NO) and
  624. (tx86operand(operands[j]).size <> OS_NO) then
  625. begin
  626. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  627. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  628. break;
  629. end
  630. else Message(asmr_e_unable_to_determine_reference_size);
  631. end;
  632. end;
  633. end;
  634. msiMemRegx16y32:
  635. begin
  636. for j := 1 to ops do
  637. begin
  638. if operands[j].Opr.Typ = OPR_REGISTER then
  639. begin
  640. case getsubreg(operands[j].opr.reg) of
  641. R_SUBMMX: begin
  642. tx86operand(operands[i]).opsize := S_W;
  643. tx86operand(operands[i]).size := OS_M16;
  644. break;
  645. end;
  646. R_SUBMMY: begin
  647. tx86operand(operands[i]).opsize := S_L;
  648. tx86operand(operands[i]).size := OS_M32;
  649. break;
  650. end;
  651. else Message(asmr_e_unable_to_determine_reference_size);
  652. end;
  653. end;
  654. end;
  655. end;
  656. msiMemRegx16y32z64:
  657. begin
  658. for j := 1 to ops do
  659. begin
  660. if operands[j].Opr.Typ = OPR_REGISTER then
  661. begin
  662. case getsubreg(operands[j].opr.reg) of
  663. R_SUBMMX: begin
  664. tx86operand(operands[i]).opsize := S_W;
  665. tx86operand(operands[i]).size := OS_M16;
  666. break;
  667. end;
  668. R_SUBMMY: begin
  669. tx86operand(operands[i]).opsize := S_L;
  670. tx86operand(operands[i]).size := OS_M32;
  671. break;
  672. end;
  673. R_SUBMMZ: begin
  674. tx86operand(operands[i]).opsize := S_Q;
  675. tx86operand(operands[i]).size := OS_M64;
  676. break;
  677. end;
  678. else Message(asmr_e_unable_to_determine_reference_size);
  679. end;
  680. end;
  681. end;
  682. end;
  683. msiMemRegx32y64:
  684. begin
  685. for j := 1 to ops do
  686. begin
  687. if operands[j].Opr.Typ = OPR_REGISTER then
  688. begin
  689. case getsubreg(operands[j].opr.reg) of
  690. R_SUBMMX: begin
  691. tx86operand(operands[i]).opsize := S_L;
  692. tx86operand(operands[i]).size := OS_M32;
  693. break;
  694. end;
  695. R_SUBMMY: begin
  696. tx86operand(operands[i]).opsize := S_Q;
  697. tx86operand(operands[i]).size := OS_M64;
  698. break;
  699. end;
  700. else Message(asmr_e_unable_to_determine_reference_size);
  701. end;
  702. end;
  703. end;
  704. end;
  705. msiMemRegx32y64z128:
  706. for j := 1 to ops do
  707. begin
  708. if operands[j].Opr.Typ = OPR_REGISTER then
  709. begin
  710. case getsubreg(operands[j].opr.reg) of
  711. R_SUBMMX: begin
  712. tx86operand(operands[i]).opsize := S_L;
  713. tx86operand(operands[i]).size := OS_M32;
  714. break;
  715. end;
  716. R_SUBMMY: begin
  717. tx86operand(operands[i]).opsize := S_Q;
  718. tx86operand(operands[i]).size := OS_M64;
  719. break;
  720. end;
  721. R_SUBMMZ: begin
  722. tx86operand(operands[i]).opsize := S_XMM;
  723. tx86operand(operands[i]).size := OS_M128;
  724. break;
  725. end;
  726. else Message(asmr_e_unable_to_determine_reference_size);
  727. end;
  728. end;
  729. end;
  730. msiMemRegx64y128:
  731. begin
  732. for j := 1 to ops do
  733. begin
  734. if operands[j].Opr.Typ = OPR_REGISTER then
  735. begin
  736. case getsubreg(operands[j].opr.reg) of
  737. R_SUBMMX: begin
  738. tx86operand(operands[i]).opsize := S_Q;
  739. tx86operand(operands[i]).size := OS_M64;
  740. break;
  741. end;
  742. R_SUBMMY: begin
  743. tx86operand(operands[i]).opsize := S_XMM;
  744. tx86operand(operands[i]).size := OS_M128;
  745. break;
  746. end;
  747. else Message(asmr_e_unable_to_determine_reference_size);
  748. end;
  749. end;
  750. end;
  751. end;
  752. msiMemRegx64y128z256:
  753. begin
  754. for j := 1 to ops do
  755. begin
  756. if operands[j].Opr.Typ = OPR_REGISTER then
  757. begin
  758. case getsubreg(operands[j].opr.reg) of
  759. R_SUBMMX: begin
  760. tx86operand(operands[i]).opsize := S_Q;
  761. tx86operand(operands[i]).size := OS_M64;
  762. break;
  763. end;
  764. R_SUBMMY: begin
  765. tx86operand(operands[i]).opsize := S_XMM;
  766. tx86operand(operands[i]).size := OS_M128;
  767. break;
  768. end;
  769. R_SUBMMZ: begin
  770. tx86operand(operands[i]).opsize := S_YMM;
  771. tx86operand(operands[i]).size := OS_M256;
  772. break;
  773. end;
  774. else Message(asmr_e_unable_to_determine_reference_size);
  775. end;
  776. end;
  777. end;
  778. end;
  779. msiMemRegx64y256:
  780. begin
  781. for j := 1 to ops do
  782. begin
  783. if operands[j].Opr.Typ = OPR_REGISTER then
  784. begin
  785. case getsubreg(operands[j].opr.reg) of
  786. R_SUBMMX: begin
  787. tx86operand(operands[i]).opsize := S_Q;
  788. tx86operand(operands[i]).size := OS_M64;
  789. break;
  790. end;
  791. R_SUBMMY: begin
  792. tx86operand(operands[i]).opsize := S_YMM;
  793. tx86operand(operands[i]).size := OS_M256;
  794. break;
  795. end;
  796. else Message(asmr_e_unable_to_determine_reference_size);
  797. end;
  798. end;
  799. end;
  800. end;
  801. msiMemRegx64y256z512:
  802. begin
  803. for j := 1 to ops do
  804. begin
  805. if operands[j].Opr.Typ = OPR_REGISTER then
  806. begin
  807. case getsubreg(operands[j].opr.reg) of
  808. R_SUBMMX: begin
  809. tx86operand(operands[i]).opsize := S_Q;
  810. tx86operand(operands[i]).size := OS_M64;
  811. break;
  812. end;
  813. R_SUBMMY: begin
  814. tx86operand(operands[i]).opsize := S_YMM;
  815. tx86operand(operands[i]).size := OS_M256;
  816. break;
  817. end;
  818. R_SUBMMZ: begin
  819. tx86operand(operands[i]).opsize := S_ZMM;
  820. tx86operand(operands[i]).size := OS_M512;
  821. break;
  822. end;
  823. else Message(asmr_e_unable_to_determine_reference_size);
  824. end;
  825. end;
  826. end;
  827. end;
  828. msiMemRegConst128,
  829. msiMemRegConst256,
  830. msiMemRegConst512:
  831. begin
  832. ExistConst := false;
  833. for j := 1 to ops do
  834. begin
  835. if operands[j].Opr.Typ = OPR_CONSTANT then
  836. begin
  837. ExistConst := true;
  838. break;
  839. end;
  840. end;
  841. if ExistConst then
  842. begin
  843. for j := 1 to ops do
  844. begin
  845. if operands[j].Opr.Typ = OPR_REGISTER then
  846. begin
  847. if (tx86operand(operands[j]).opsize <> S_NO) and
  848. (tx86operand(operands[j]).size <> OS_NO) then
  849. begin
  850. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  851. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  852. break;
  853. end
  854. else Message(asmr_e_unable_to_determine_reference_size);
  855. end;
  856. end;
  857. end
  858. else
  859. begin
  860. case MemRefInfo(opcode).MemRefSize of
  861. msiMemRegConst128: begin
  862. tx86operand(operands[i]).opsize := S_XMM;
  863. tx86operand(operands[i]).size := OS_M128;
  864. break;
  865. end;
  866. msiMemRegConst256: begin
  867. tx86operand(operands[i]).opsize := S_YMM;
  868. tx86operand(operands[i]).size := OS_M256;
  869. break;
  870. end;
  871. msiMemRegConst512: begin
  872. tx86operand(operands[i]).opsize := S_ZMM;
  873. tx86operand(operands[i]).size := OS_M512;
  874. break;
  875. end;
  876. end;
  877. end;
  878. end;
  879. msiNoSize: ; // all memory-sizes are ok
  880. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  881. end;
  882. end;
  883. OPR_CONSTANT:
  884. case MemRefInfo(opcode).ConstSize of
  885. csiMem8: begin
  886. tx86operand(operands[i]).opsize := S_B;
  887. tx86operand(operands[i]).size := OS_8;
  888. end;
  889. csiMem16: begin
  890. tx86operand(operands[i]).opsize := S_W;
  891. tx86operand(operands[i]).size := OS_16;
  892. end;
  893. csiMem32: begin
  894. tx86operand(operands[i]).opsize := S_L;
  895. tx86operand(operands[i]).size := OS_32;
  896. end;
  897. end;
  898. end;
  899. end;
  900. end;
  901. end;
  902. for i:=1 to ops do
  903. begin
  904. operands[i].SetCorrectSize(opcode);
  905. if tx86operand(operands[i]).opsize=S_NO then
  906. begin
  907. {$ifdef x86_64}
  908. if (opcode=A_MOVQ) and
  909. (ops=2) and
  910. (operands[1].opr.typ=OPR_CONSTANT) then
  911. opsize:=S_Q
  912. else
  913. {$endif x86_64}
  914. case operands[i].Opr.Typ of
  915. OPR_LOCAL,
  916. OPR_REFERENCE :
  917. begin
  918. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  919. don't consider it. }
  920. if i=ops then
  921. operand2:=i-1
  922. else
  923. operand2:=i+1;
  924. if operand2>0 then
  925. begin
  926. { Only allow register as operand to take the size from }
  927. if operands[operand2].opr.typ=OPR_REGISTER then
  928. begin
  929. if ((opcode<>A_MOVD) and
  930. (opcode<>A_CVTSI2SS)) then
  931. begin
  932. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  933. // torsten - 31.01.2012
  934. // old: xmm/ymm-register operands have a opsize = "S_NO"
  935. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  936. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  937. // in this case is we need the old handling ("S_NO")
  938. // =>> ignore
  939. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  940. (tx86operand(operands[operand2]).opsize <> S_YMM) and
  941. (tx86operand(operands[operand2]).opsize <> S_ZMM) then
  942. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  943. else tx86operand(operands[operand2]).opsize := S_NO;
  944. end;
  945. end
  946. else
  947. begin
  948. { if no register then take the opsize (which is available with ATT),
  949. if not availble then give an error }
  950. if opsize<>S_NO then
  951. tx86operand(operands[i]).opsize:=opsize
  952. else
  953. begin
  954. if (m_delphi in current_settings.modeswitches) then
  955. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  956. else
  957. Message(asmr_e_unable_to_determine_reference_size);
  958. { recovery }
  959. tx86operand(operands[i]).opsize:=S_L;
  960. end;
  961. end;
  962. end
  963. else
  964. begin
  965. if opsize<>S_NO then
  966. tx86operand(operands[i]).opsize:=opsize
  967. end;
  968. end;
  969. OPR_SYMBOL :
  970. begin
  971. { Fix lea which need a reference }
  972. if opcode=A_LEA then
  973. begin
  974. s:=operands[i].opr.symbol;
  975. so:=operands[i].opr.symofs;
  976. operands[i].opr.typ:=OPR_REFERENCE;
  977. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  978. operands[i].opr.ref.symbol:=s;
  979. operands[i].opr.ref.offset:=so;
  980. end;
  981. {$if defined(x86_64)}
  982. tx86operand(operands[i]).opsize:=S_Q;
  983. {$elseif defined(i386)}
  984. tx86operand(operands[i]).opsize:=S_L;
  985. {$elseif defined(i8086)}
  986. tx86operand(operands[i]).opsize:=S_W;
  987. {$endif}
  988. end;
  989. end;
  990. end;
  991. end;
  992. if MemRefInfo(opcode).ExistsSSEAVX then
  993. begin
  994. // validate broadcast-memory-operands
  995. vbcst := 0;
  996. mmregs := [];
  997. for i := 1 to ops do
  998. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then vbcst := tx86operand(operands[i]).vbcst
  999. else if operands[i].Opr.Typ = OPR_REGISTER then
  1000. begin
  1001. if getsubreg(operands[i].opr.reg) in [R_SUBMMX, R_SUBMMY, R_SUBMMZ] then
  1002. begin
  1003. include(mmregs, getsubreg(operands[i].opr.reg));
  1004. end;
  1005. end;
  1006. if vbcst <> 0 then
  1007. begin
  1008. // found broadcast-memory-operand (e.g. "{1to8}")
  1009. // check is correct
  1010. multiplicator := 0;
  1011. if mmregs = [R_SUBMMX] then multiplicator := 1
  1012. else if mmregs = [R_SUBMMY] then multiplicator := 2
  1013. else if mmregs = [R_SUBMMZ] then multiplicator := 4
  1014. else
  1015. begin
  1016. //TG TODO
  1017. end;
  1018. if MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator <> vbcst then
  1019. begin
  1020. str(MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator, bcst1);
  1021. str(vbcst, bcst2);
  1022. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  1023. end;
  1024. end;
  1025. end;
  1026. end;
  1027. procedure Tx86Instruction.SetInstructionOpsize;
  1028. begin
  1029. if opsize<>S_NO then
  1030. exit;
  1031. case ops of
  1032. 0 : ;
  1033. 1 :
  1034. begin
  1035. { "push es" must be stored as a long PM }
  1036. if ((opcode=A_PUSH) or
  1037. (opcode=A_POP)) and
  1038. (operands[1].opr.typ=OPR_REGISTER) and
  1039. is_segment_reg(operands[1].opr.reg) then
  1040. {$ifdef i8086}
  1041. opsize:=S_W
  1042. {$else i8086}
  1043. opsize:=S_L
  1044. {$endif i8086}
  1045. else
  1046. opsize:=tx86operand(operands[1]).opsize;
  1047. end;
  1048. 2 :
  1049. begin
  1050. case opcode of
  1051. A_MOVZX,A_MOVSX :
  1052. begin
  1053. if tx86operand(operands[1]).opsize=S_NO then
  1054. begin
  1055. tx86operand(operands[1]).opsize:=S_B;
  1056. if (m_delphi in current_settings.modeswitches) then
  1057. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  1058. else
  1059. Message(asmr_e_unable_to_determine_reference_size);
  1060. end;
  1061. case tx86operand(operands[1]).opsize of
  1062. S_W :
  1063. case tx86operand(operands[2]).opsize of
  1064. S_L :
  1065. opsize:=S_WL;
  1066. {$ifdef x86_64}
  1067. S_Q :
  1068. opsize:=S_WQ;
  1069. {$endif}
  1070. end;
  1071. S_B :
  1072. begin
  1073. case tx86operand(operands[2]).opsize of
  1074. S_W :
  1075. opsize:=S_BW;
  1076. S_L :
  1077. opsize:=S_BL;
  1078. {$ifdef x86_64}
  1079. S_Q :
  1080. opsize:=S_BQ;
  1081. {$endif}
  1082. end;
  1083. end;
  1084. end;
  1085. end;
  1086. A_MOVSS,
  1087. A_VMOVSS,
  1088. A_MOVD : { movd is a move from a mmx register to a
  1089. 32 bit register or memory, so no opsize is correct here PM }
  1090. exit;
  1091. A_MOVQ :
  1092. opsize:=S_IQ;
  1093. A_CVTSI2SS,
  1094. A_CVTSI2SD,
  1095. A_OUT :
  1096. opsize:=tx86operand(operands[1]).opsize;
  1097. else
  1098. opsize:=tx86operand(operands[2]).opsize;
  1099. end;
  1100. end;
  1101. 3 :
  1102. begin
  1103. case opcode of
  1104. A_VCVTSI2SS,
  1105. A_VCVTSI2SD:
  1106. opsize:=tx86operand(operands[1]).opsize;
  1107. else
  1108. opsize:=tx86operand(operands[ops]).opsize;
  1109. end;
  1110. end;
  1111. 4 :
  1112. opsize:=tx86operand(operands[ops]).opsize;
  1113. end;
  1114. end;
  1115. procedure Tx86Instruction.CheckOperandSizes;
  1116. var
  1117. sizeerr : boolean;
  1118. i : longint;
  1119. begin
  1120. { Check only the most common opcodes here, the others are done in
  1121. the assembler pass }
  1122. case opcode of
  1123. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  1124. A_CMP,A_MOV,
  1125. A_ADD,A_SUB,A_ADC,A_SBB,
  1126. A_AND,A_OR,A_TEST,A_XOR: ;
  1127. else
  1128. exit;
  1129. end;
  1130. { Handle the BW,BL,WL separatly }
  1131. sizeerr:=false;
  1132. { special push/pop selector case }
  1133. if ((opcode=A_PUSH) or
  1134. (opcode=A_POP)) and
  1135. (operands[1].opr.typ=OPR_REGISTER) and
  1136. is_segment_reg(operands[1].opr.reg) then
  1137. exit;
  1138. if opsize in [S_BW,S_BL,S_WL] then
  1139. begin
  1140. if ops<>2 then
  1141. sizeerr:=true
  1142. else
  1143. begin
  1144. case opsize of
  1145. S_BW :
  1146. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  1147. S_BL :
  1148. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  1149. S_WL :
  1150. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  1151. end;
  1152. end;
  1153. end
  1154. else
  1155. begin
  1156. for i:=1 to ops do
  1157. begin
  1158. if (operands[i].opr.typ<>OPR_CONSTANT) and
  1159. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  1160. (tx86operand(operands[i]).opsize<>opsize) then
  1161. sizeerr:=true;
  1162. end;
  1163. end;
  1164. if sizeerr then
  1165. begin
  1166. { if range checks are on then generate an error }
  1167. if (cs_compilesystem in current_settings.moduleswitches) or
  1168. not (cs_check_range in current_settings.localswitches) then
  1169. Message(asmr_w_size_suffix_and_dest_dont_match)
  1170. else
  1171. Message(asmr_e_size_suffix_and_dest_dont_match);
  1172. end;
  1173. end;
  1174. { This check must be done with the operand in ATT order
  1175. i.e.after swapping in the intel reader
  1176. but before swapping in the NASM and TASM writers PM }
  1177. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  1178. begin
  1179. if (
  1180. (ops=2) and
  1181. (operands[1].opr.typ=OPR_REGISTER) and
  1182. (operands[2].opr.typ=OPR_REGISTER) and
  1183. { if the first is ST and the second is also a register
  1184. it is necessarily ST1 .. ST7 }
  1185. ((operands[1].opr.reg=NR_ST) or
  1186. (operands[1].opr.reg=NR_ST0))
  1187. ) or
  1188. (ops=0) then
  1189. if opcode=A_FSUBR then
  1190. opcode:=A_FSUB
  1191. else if opcode=A_FSUB then
  1192. opcode:=A_FSUBR
  1193. else if opcode=A_FDIVR then
  1194. opcode:=A_FDIV
  1195. else if opcode=A_FDIV then
  1196. opcode:=A_FDIVR
  1197. else if opcode=A_FSUBRP then
  1198. opcode:=A_FSUBP
  1199. else if opcode=A_FSUBP then
  1200. opcode:=A_FSUBRP
  1201. else if opcode=A_FDIVRP then
  1202. opcode:=A_FDIVP
  1203. else if opcode=A_FDIVP then
  1204. opcode:=A_FDIVRP;
  1205. if (
  1206. (ops=1) and
  1207. (operands[1].opr.typ=OPR_REGISTER) and
  1208. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1209. (operands[1].opr.reg<>NR_ST) and
  1210. (operands[1].opr.reg<>NR_ST0)
  1211. ) then
  1212. if opcode=A_FSUBRP then
  1213. opcode:=A_FSUBP
  1214. else if opcode=A_FSUBP then
  1215. opcode:=A_FSUBRP
  1216. else if opcode=A_FDIVRP then
  1217. opcode:=A_FDIVP
  1218. else if opcode=A_FDIVP then
  1219. opcode:=A_FDIVRP;
  1220. end;
  1221. procedure Tx86Instruction.FixupOpcode;
  1222. begin
  1223. { does nothing by default }
  1224. end;
  1225. {*****************************************************************************
  1226. opcode Adding
  1227. *****************************************************************************}
  1228. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  1229. var
  1230. siz : topsize;
  1231. i : longint;
  1232. asize : int64;
  1233. ai : taicpu;
  1234. //TG TODO delete
  1235. ocode: tasmop;
  1236. begin
  1237. ConcatInstruction:=nil;
  1238. ai:=nil;
  1239. for i:=1 to Ops do
  1240. if not operands[i].CheckOperand then
  1241. exit;
  1242. { Get Opsize }
  1243. if (opsize<>S_NO) or (Ops=0) then
  1244. siz:=opsize
  1245. else
  1246. begin
  1247. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  1248. siz:=tx86operand(operands[1]).opsize
  1249. else
  1250. siz:=tx86operand(operands[Ops]).opsize;
  1251. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  1252. if (ops=2) and
  1253. (tx86operand(operands[1]).opsize<>S_NO) and
  1254. (tx86operand(operands[2]).opsize<>S_NO) and
  1255. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  1256. siz:=S_NO;
  1257. end;
  1258. if ((opcode=A_MOVD)or
  1259. (opcode=A_CVTSI2SS)) and
  1260. ((tx86operand(operands[1]).opsize=S_NO) or
  1261. (tx86operand(operands[2]).opsize=S_NO)) then
  1262. siz:=S_NO;
  1263. { NASM does not support FADD without args
  1264. as alias of FADDP
  1265. and GNU AS interprets FADD without operand differently
  1266. for version 2.9.1 and 2.9.5 !! }
  1267. if (ops=0) and
  1268. ((opcode=A_FADD) or
  1269. (opcode=A_FMUL) or
  1270. (opcode=A_FSUB) or
  1271. (opcode=A_FSUBR) or
  1272. (opcode=A_FDIV) or
  1273. (opcode=A_FDIVR)) then
  1274. begin
  1275. if opcode=A_FADD then
  1276. opcode:=A_FADDP
  1277. else if opcode=A_FMUL then
  1278. opcode:=A_FMULP
  1279. else if opcode=A_FSUB then
  1280. opcode:=A_FSUBP
  1281. else if opcode=A_FSUBR then
  1282. opcode:=A_FSUBRP
  1283. else if opcode=A_FDIV then
  1284. opcode:=A_FDIVP
  1285. else if opcode=A_FDIVR then
  1286. opcode:=A_FDIVRP;
  1287. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  1288. end;
  1289. {It is valid to specify some instructions without operand size.}
  1290. if siz=S_NO then
  1291. begin
  1292. if (ops=1) and (opcode=A_INT) then
  1293. siz:=S_B;
  1294. if (ops=1) and (opcode=A_XABORT) then
  1295. siz:=S_B;
  1296. {$ifdef i8086}
  1297. if (ops=1) and (opcode=A_BRKEM) then
  1298. siz:=S_B;
  1299. {$endif i8086}
  1300. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) or
  1301. (opcode=A_RETW) or (opcode=A_RETNW) or (opcode=A_RETFW) or
  1302. {$ifndef x86_64}
  1303. (opcode=A_RETD) or (opcode=A_RETND) or
  1304. {$endif x86_64}
  1305. (opcode=A_RETFD)
  1306. {$ifdef x86_64}
  1307. or (opcode=A_RETQ) or (opcode=A_RETNQ) or (opcode=A_RETFQ)
  1308. {$endif x86_64}
  1309. then
  1310. siz:=S_W;
  1311. if (ops=1) and (opcode=A_PUSH) then
  1312. begin
  1313. {$ifdef i8086}
  1314. if (tx86operand(operands[1]).opr.val>=-128) and (tx86operand(operands[1]).opr.val<=127) then
  1315. begin
  1316. siz:=S_B;
  1317. message(asmr_w_unable_to_determine_constant_size_using_byte);
  1318. end
  1319. else
  1320. begin
  1321. siz:=S_W;
  1322. message(asmr_w_unable_to_determine_constant_size_using_word);
  1323. end;
  1324. {$else i8086}
  1325. { We are a 32 compiler, assume 32-bit by default. This is Delphi
  1326. compatible but bad coding practise.}
  1327. siz:=S_L;
  1328. message(asmr_w_unable_to_determine_reference_size_using_dword);
  1329. {$endif i8086}
  1330. end;
  1331. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  1332. if ops=1 then
  1333. siz:=S_NEAR
  1334. else
  1335. siz:=S_FAR;
  1336. end;
  1337. { GNU AS interprets FDIV without operand differently
  1338. for version 2.9.1 and 2.10
  1339. we add explicit args to it !! }
  1340. if (ops=0) and
  1341. ((opcode=A_FSUBP) or
  1342. (opcode=A_FSUBRP) or
  1343. (opcode=A_FDIVP) or
  1344. (opcode=A_FDIVRP) or
  1345. (opcode=A_FSUB) or
  1346. (opcode=A_FSUBR) or
  1347. (opcode=A_FADD) or
  1348. (opcode=A_FADDP) or
  1349. (opcode=A_FDIV) or
  1350. (opcode=A_FDIVR)) then
  1351. begin
  1352. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  1353. ops:=2;
  1354. operands[1].opr.typ:=OPR_REGISTER;
  1355. operands[2].opr.typ:=OPR_REGISTER;
  1356. operands[1].opr.reg:=NR_ST0;
  1357. operands[2].opr.reg:=NR_ST1;
  1358. end;
  1359. if (ops=1) and
  1360. (
  1361. (operands[1].opr.typ=OPR_REGISTER) and
  1362. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1363. (operands[1].opr.reg<>NR_ST) and
  1364. (operands[1].opr.reg<>NR_ST0)
  1365. ) and
  1366. (
  1367. (opcode=A_FSUBP) or
  1368. (opcode=A_FSUBRP) or
  1369. (opcode=A_FDIVP) or
  1370. (opcode=A_FDIVRP) or
  1371. (opcode=A_FADDP) or
  1372. (opcode=A_FMULP)
  1373. ) then
  1374. begin
  1375. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  1376. ops:=2;
  1377. operands[2].opr.typ:=OPR_REGISTER;
  1378. operands[2].opr.reg:=operands[1].opr.reg;
  1379. operands[1].opr.reg:=NR_ST0;
  1380. end;
  1381. if (ops=1) and
  1382. (
  1383. (operands[1].opr.typ=OPR_REGISTER) and
  1384. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1385. (operands[1].opr.reg<>NR_ST) and
  1386. (operands[1].opr.reg<>NR_ST0)
  1387. ) and
  1388. (
  1389. (opcode=A_FSUB) or
  1390. (opcode=A_FSUBR) or
  1391. (opcode=A_FDIV) or
  1392. (opcode=A_FDIVR) or
  1393. (opcode=A_FADD) or
  1394. (opcode=A_FMUL)
  1395. ) then
  1396. begin
  1397. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  1398. ops:=2;
  1399. operands[2].opr.typ:=OPR_REGISTER;
  1400. operands[2].opr.reg:=NR_ST0;
  1401. end;
  1402. { Check for 'POP CS' }
  1403. if (opcode=A_POP) and (ops=1) and (operands[1].opr.typ=OPR_REGISTER) and
  1404. (operands[1].opr.reg=NR_CS) then
  1405. {$ifdef i8086}
  1406. { On i8086 we print only a warning, because 'POP CS' works on 8086 and 8088
  1407. CPUs, but isn't supported on any later CPU }
  1408. Message(asmr_w_pop_cs_not_portable);
  1409. {$else i8086}
  1410. { On the i386 and x86_64 targets, we print out an error, because no CPU,
  1411. supported by these targets support 'POP CS' }
  1412. Message(asmr_e_pop_cs_not_valid);
  1413. {$endif i8086}
  1414. { I tried to convince Linus Torvalds to add
  1415. code to support ENTER instruction
  1416. (when raising a stack page fault)
  1417. but he replied that ENTER is a bad instruction and
  1418. Linux does not need to support it
  1419. So I think its at least a good idea to add a warning
  1420. if someone uses this in assembler code
  1421. FPC itself does not use it at all PM }
  1422. if (opcode=A_ENTER) and
  1423. (target_info.system in [system_i386_linux,system_i386_FreeBSD,system_i386_android]) then
  1424. Message(asmr_w_enter_not_supported_by_linux);
  1425. //TG TODO delete
  1426. oCode := opcode;
  1427. ai:=taicpu.op_none(opcode,siz);
  1428. ai.fileinfo:=filepos;
  1429. ai.SetOperandOrder(op_att);
  1430. ai.Ops:=Ops;
  1431. ai.Allocate_oper(Ops);
  1432. for i:=1 to Ops do
  1433. begin
  1434. ai.oper[i-1]^.vopext := (operands[i] as tx86operand).vopext;
  1435. case operands[i].opr.typ of
  1436. OPR_CONSTANT :
  1437. ai.loadconst(i-1,operands[i].opr.val);
  1438. OPR_REGISTER:
  1439. ai.loadreg(i-1,operands[i].opr.reg);
  1440. OPR_SYMBOL:
  1441. {$ifdef i8086}
  1442. if operands[i].opr.symseg then
  1443. taicpu(ai).loadsegsymbol(i-1,operands[i].opr.symbol)
  1444. else
  1445. {$endif i8086}
  1446. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1447. OPR_LOCAL :
  1448. with operands[i].opr do
  1449. begin
  1450. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1451. localscale,localgetoffset,localforceref);
  1452. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1453. end;
  1454. OPR_REFERENCE:
  1455. begin
  1456. if current_settings.optimizerswitches <> [] then
  1457. if (not(MemRefInfo(opcode).MemRefSize in MemRefSizeInfoVMems)) and (opcode<>A_XLAT) and not is_x86_string_op(opcode) then
  1458. optimize_ref(operands[i].opr.ref,true);
  1459. ai.loadref(i-1,operands[i].opr.ref);
  1460. if operands[i].size<>OS_NO then
  1461. begin
  1462. asize:=0;
  1463. case operands[i].size of
  1464. OS_8,OS_S8 :
  1465. asize:=OT_BITS8;
  1466. OS_16,OS_S16, OS_M16:
  1467. asize:=OT_BITS16;
  1468. OS_32,OS_S32 :
  1469. {$ifdef i8086}
  1470. if siz=S_FAR then
  1471. asize:=OT_FAR
  1472. else
  1473. asize:=OT_BITS32;
  1474. {$else i8086}
  1475. asize:=OT_BITS32;
  1476. {$endif i8086}
  1477. OS_F32,OS_M32 :
  1478. asize:=OT_BITS32;
  1479. OS_64,OS_S64:
  1480. begin
  1481. { Only FPU operations know about 64bit values, for all
  1482. integer operations it is seen as 32bit
  1483. this applies only to i386, see tw16622}
  1484. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  1485. asize:=OT_BITS64
  1486. else if MemRefInfo(opcode).ExistsSSEAVX then asize:=OT_BITS64
  1487. {$ifdef i386}
  1488. else
  1489. asize:=OT_BITS32
  1490. {$endif i386}
  1491. ;
  1492. end;
  1493. OS_F64,OS_C64, OS_M64 :
  1494. asize:=OT_BITS64;
  1495. OS_F80 :
  1496. asize:=OT_BITS80;
  1497. OS_128,OS_M128,OS_MS128:
  1498. asize := OT_BITS128;
  1499. OS_M256,OS_MS256:
  1500. asize := OT_BITS256;
  1501. OS_M512,OS_MS512:
  1502. asize := OT_BITS512;
  1503. end;
  1504. if asize<>0 then
  1505. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1506. end;
  1507. end;
  1508. end;
  1509. end;
  1510. { Condition ? }
  1511. if condition<>C_None then
  1512. ai.SetCondition(condition);
  1513. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  1514. if (opcode=A_JMP) or (opcode=A_JCC) then
  1515. ai.is_jmp := True;
  1516. { Concat the opcode or give an error }
  1517. if assigned(ai) then
  1518. p.concat(ai)
  1519. else
  1520. Message(asmr_e_invalid_opcode_and_operand);
  1521. result:=ai;
  1522. end;
  1523. function Tx86Instruction.getstring: string;
  1524. var
  1525. i : longint;
  1526. s, sval : string;
  1527. regnr: string;
  1528. addsize : boolean;
  1529. begin
  1530. s:='['+std_op2str[opcode];
  1531. for i:=1 to ops do
  1532. begin
  1533. with operands[i] as Tx86Operand do
  1534. begin
  1535. if i=0 then
  1536. s:=s+' '
  1537. else
  1538. s:=s+',';
  1539. { type }
  1540. addsize:=false;
  1541. case operands[i].opr.typ of
  1542. OPR_CONSTANT : begin
  1543. str(operands[i].opr.val, sval);
  1544. s:=s+ sval;
  1545. end;
  1546. OPR_REGISTER : begin
  1547. regnr := '';
  1548. str(getsupreg(opr.reg),regnr);
  1549. if getsubreg(opr.reg)= R_SUBMMX then
  1550. s:=s+'xmmreg' + regnr
  1551. else
  1552. if getsubreg(opr.reg)= R_SUBMMY then
  1553. s:=s+'ymmreg' + regnr
  1554. else
  1555. if getsubreg(opr.reg)= R_SUBMMZ then
  1556. s:=s+'zmmreg' + regnr
  1557. else
  1558. if getregtype(opr.reg)= R_MMXREGISTER then
  1559. s:=s+'mmxreg'
  1560. else
  1561. if getregtype(opr.reg)= R_FPUREGISTER then
  1562. s:=s+'fpureg'
  1563. else
  1564. if getregtype(opr.reg)=R_INTREGISTER then
  1565. begin
  1566. s:=s+'reg';
  1567. addsize:=true;
  1568. end;
  1569. end;
  1570. OPR_LOCAL,
  1571. OPR_REFERENCE: begin
  1572. s:=s + 'mem';
  1573. addsize:=true;
  1574. end;
  1575. else s:=s + '???';
  1576. end;
  1577. if addsize then
  1578. begin
  1579. sval := '';
  1580. str(tcgsize2size[size], sval);
  1581. s := s + sval;
  1582. end;
  1583. if vopext <> 0 then
  1584. begin
  1585. str(vopext and $07, regnr);
  1586. if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  1587. s := s + ' {k' + regnr + '}';
  1588. if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  1589. s := s + ' {z}';
  1590. if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  1591. s := s + ' {sae}';
  1592. if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  1593. case vopext and OTVE_VECTOR_BCST_MASK of
  1594. OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  1595. OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  1596. OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  1597. OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  1598. end;
  1599. if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  1600. case vopext and OTVE_VECTOR_ER_MASK of
  1601. OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  1602. OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  1603. OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  1604. OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  1605. end;
  1606. end;
  1607. end;
  1608. end;
  1609. GetString:=s+']';
  1610. end;
  1611. end.