2
0

rax86.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 : longint;
  310. s : tasmsymbol;
  311. so : aint;
  312. ExistsMemRefNoSize: boolean;
  313. ExistsMemRef: boolean;
  314. ExistsConstNoSize: boolean;
  315. ExistsLocalSymSize: boolean;
  316. ExistsBCST: boolean;
  317. memrefsize: integer;
  318. memopsize: integer;
  319. memoffset: asizeint;
  320. vbcst: byte;
  321. mmregs: Set of TSubregister;
  322. multiplicator: integer;
  323. bcst1,bcst2: string;
  324. begin
  325. ExistsMemRefNoSize := false;
  326. ExistsMemRef := false;
  327. ExistsConstNoSize := false;
  328. ExistsLocalSymSize := false;
  329. ExistsBCST := 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. ExistsBCST := (MemRefInfo(opcode).ExistsSSEAVX) and
  337. (tx86operand(operands[i]).vbcst <> 0);
  338. if (tx86operand(operands[i]).opsize = S_NO) then
  339. begin
  340. ExistsMemRefNoSize := true;
  341. case operands[i].opr.Typ of
  342. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  343. OPR_REFERENCE: ExistsLocalSymSize := true;
  344. end;
  345. end;
  346. end
  347. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  348. begin
  349. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  350. end;
  351. end;
  352. // ONLY SUPPORTED OPCODES WITH SSE- OR AVX-REGISTERS
  353. if (ExistsMemRef) and
  354. (MemRefInfo(opcode).ExistsSSEAVX) then
  355. begin
  356. // 1. WE HAVE AN SSE- OR AVX-OPCODE WITH MEMORY OPERAND
  357. if (not(ExistsMemRefNoSize)) or
  358. (ExistsLocalSymSize) then
  359. begin
  360. // 2. WE KNOWN THE MEMORYSIZE OF THE MEMORY-OPERAND OR WE CAN
  361. // CALC THE MEMORYSIZE
  362. // 3. CALC THE SIZE OF THE MEMORYOPERAND BY OPCODE-DEFINITION
  363. // 4. COMPARE THE SIZE FROM OPCODE-DEFINITION AND THE REAL MEMORY-OPERAND-SIZE
  364. // - validate memory-reference-size
  365. for i := 1 to ops do
  366. begin
  367. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  368. begin
  369. memrefsize := -1;
  370. if ExistsBCST then
  371. begin
  372. case MemRefInfo(opcode).MemRefSizeBCST of
  373. msbBCST32: memrefsize := 32;
  374. msbBCST64: memrefsize := 64;
  375. end;
  376. end
  377. else
  378. case MemRefInfo(opcode).MemRefSize of
  379. msiMem8: memrefsize := 8;
  380. msiMem16: memrefsize := 16;
  381. msiMem32: memrefsize := 32;
  382. msiMem64: memrefsize := 64;
  383. msiMem128: memrefsize := 128;
  384. msiMem256: memrefsize := 256;
  385. msiMemRegSize:
  386. for j := 1 to ops do
  387. begin
  388. if operands[j].Opr.Typ = OPR_REGISTER then
  389. begin
  390. if (tx86operand(operands[j]).opsize <> S_NO) and
  391. (tx86operand(operands[j]).size <> OS_NO) then
  392. begin
  393. case tx86operand(operands[j]).opsize of
  394. S_B : memrefsize := 8;
  395. S_W : memrefsize := 16;
  396. S_L : memrefsize := 32;
  397. S_Q : memrefsize := 64;
  398. S_XMM : memrefsize := 128;
  399. S_YMM : memrefsize := 256;
  400. S_ZMM : memrefsize := 512;
  401. else Internalerror(777200);
  402. end;
  403. break;
  404. end;
  405. end;
  406. end;
  407. end;
  408. if memrefsize > -1 then
  409. begin
  410. // CALC REAL-MEMORY-OPERAND-SIZE AND A POSSIBLE OFFSET
  411. // OFFSET:
  412. // e.g. PAND XMM0, [RAX + 16] =>> OFFSET = 16 BYTES
  413. // PAND XMM0, [RAX + a.b + 10] =>> OFFSET = 10 BYTES (a = record-variable)
  414. memopsize := 0;
  415. case operands[i].opr.typ of
  416. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  417. OPR_REFERENCE:
  418. if operands[i].opr.ref.refaddr = addr_pic then
  419. memopsize := sizeof(pint) * 8
  420. else
  421. memopsize := operands[i].opr.varsize * 8;
  422. end;
  423. if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  424. if (memopsize > 0) and
  425. (memrefsize > 0) then
  426. begin
  427. memoffset := 0;
  428. case operands[i].opr.typ of
  429. OPR_LOCAL:
  430. memoffset := operands[i].opr.localconstoffset;
  431. OPR_REFERENCE:
  432. memoffset := operands[i].opr.constoffset;
  433. end;
  434. if memoffset < 0 then
  435. begin
  436. Message2(asmr_w_check_mem_operand_negative_offset,
  437. std_op2str[opcode],
  438. ToStr(memoffset));
  439. end
  440. else if (memopsize < (memrefsize + memoffset * 8)) then
  441. begin
  442. if memoffset = 0 then
  443. begin
  444. Message3(asmr_w_check_mem_operand_size3,
  445. std_op2str[opcode],
  446. ToStr(memopsize),
  447. ToStr(memrefsize)
  448. );
  449. end
  450. else
  451. begin
  452. Message4(asmr_w_check_mem_operand_size_offset,
  453. std_op2str[opcode],
  454. ToStr(memopsize),
  455. ToStr(memrefsize),
  456. ToStr(memoffset)
  457. );
  458. end;
  459. end;
  460. end;
  461. end;
  462. end;
  463. end;
  464. end;
  465. end;
  466. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  467. (MemRefInfo(opcode).ExistsSSEAVX) then
  468. begin
  469. for i := 1 to ops do
  470. begin
  471. if (tx86operand(operands[i]).opsize = S_NO) then
  472. begin
  473. case operands[i].Opr.Typ of
  474. OPR_REFERENCE:
  475. begin
  476. if ExistsBCST then
  477. begin
  478. case MemRefInfo(opcode).MemRefSizeBCST of
  479. msbBCST32: begin
  480. tx86operand(operands[i]).opsize := S_L;
  481. tx86operand(operands[i]).size := OS_32;
  482. end;
  483. msbBCST64: begin
  484. tx86operand(operands[i]).opsize := S_Q;
  485. tx86operand(operands[i]).size := OS_M64;
  486. end;
  487. end;
  488. end
  489. else
  490. case MemRefInfo(opcode).MemRefSize of
  491. msiMem8:
  492. begin
  493. tx86operand(operands[i]).opsize := S_B;
  494. tx86operand(operands[i]).size := OS_8;
  495. end;
  496. msiMultiple8:
  497. begin
  498. tx86operand(operands[i]).opsize := S_B;
  499. tx86operand(operands[i]).size := OS_8;
  500. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"8 bit memory operand"');
  501. end;
  502. msiMem16:
  503. begin
  504. tx86operand(operands[i]).opsize := S_W;
  505. tx86operand(operands[i]).size := OS_16;
  506. end;
  507. msiMultiple16:
  508. begin
  509. tx86operand(operands[i]).opsize := S_W;
  510. tx86operand(operands[i]).size := OS_16;
  511. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"16 bit memory operand"');
  512. end;
  513. msiMem32:
  514. begin
  515. tx86operand(operands[i]).opsize := S_L;
  516. tx86operand(operands[i]).size := OS_32;
  517. end;
  518. msiMultiple32:
  519. begin
  520. tx86operand(operands[i]).opsize := S_L;
  521. tx86operand(operands[i]).size := OS_32;
  522. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"32 bit memory operand"');
  523. end;
  524. msiMem64:
  525. begin
  526. tx86operand(operands[i]).opsize := S_Q;
  527. tx86operand(operands[i]).size := OS_M64;
  528. end;
  529. msiMultiple64:
  530. begin
  531. tx86operand(operands[i]).opsize := S_Q;
  532. tx86operand(operands[i]).size := OS_M64;
  533. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"64 bit memory operand"');
  534. end;
  535. msiMem128:
  536. begin
  537. tx86operand(operands[i]).opsize := S_XMM;
  538. tx86operand(operands[i]).size := OS_M128;
  539. end;
  540. msiMultiple128:
  541. begin
  542. tx86operand(operands[i]).opsize := S_XMM;
  543. tx86operand(operands[i]).size := OS_M128;
  544. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"128 bit memory operand"');
  545. end;
  546. msiMem256:
  547. begin
  548. tx86operand(operands[i]).opsize := S_YMM;
  549. tx86operand(operands[i]).size := OS_M256;
  550. opsize := S_YMM;
  551. end;
  552. msiMultiple256:
  553. begin
  554. tx86operand(operands[i]).opsize := S_YMM;
  555. tx86operand(operands[i]).size := OS_M256;
  556. opsize := S_YMM;
  557. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"256 bit memory operand"');
  558. end;
  559. msiMem512:
  560. begin
  561. tx86operand(operands[i]).opsize := S_ZMM;
  562. tx86operand(operands[i]).size := OS_M512;
  563. opsize := S_ZMM;
  564. end;
  565. msiMultiple512:
  566. begin
  567. tx86operand(operands[i]).opsize := S_ZMM;
  568. tx86operand(operands[i]).size := OS_M512;
  569. opsize := S_ZMM;
  570. Message2(asmr_w_check_mem_operand_automap_multiple_size, std_op2str[opcode], '"512 bit memory operand"');
  571. end;
  572. msiMemRegSize:
  573. begin
  574. // mem-ref-size = register size
  575. for j := 1 to ops do
  576. begin
  577. if operands[j].Opr.Typ = OPR_REGISTER then
  578. begin
  579. if (tx86operand(operands[j]).opsize <> S_NO) and
  580. (tx86operand(operands[j]).size <> OS_NO) then
  581. begin
  582. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  583. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  584. break;
  585. end
  586. else Message(asmr_e_unable_to_determine_reference_size);
  587. end;
  588. end;
  589. end;
  590. msiMemRegx16y32:
  591. begin
  592. for j := 1 to ops do
  593. begin
  594. if operands[j].Opr.Typ = OPR_REGISTER then
  595. begin
  596. case getsubreg(operands[j].opr.reg) of
  597. R_SUBMMX: begin
  598. tx86operand(operands[i]).opsize := S_L;
  599. tx86operand(operands[i]).size := OS_M16;
  600. break;
  601. end;
  602. R_SUBMMY: begin
  603. tx86operand(operands[i]).opsize := S_Q;
  604. tx86operand(operands[i]).size := OS_M32;
  605. break;
  606. end;
  607. else Message(asmr_e_unable_to_determine_reference_size);
  608. end;
  609. end;
  610. end;
  611. end;
  612. msiMemRegx32y64:
  613. begin
  614. for j := 1 to ops do
  615. begin
  616. if operands[j].Opr.Typ = OPR_REGISTER then
  617. begin
  618. case getsubreg(operands[j].opr.reg) of
  619. R_SUBMMX: begin
  620. tx86operand(operands[i]).opsize := S_L;
  621. tx86operand(operands[i]).size := OS_M32;
  622. break;
  623. end;
  624. R_SUBMMY: begin
  625. tx86operand(operands[i]).opsize := S_Q;
  626. tx86operand(operands[i]).size := OS_M64;
  627. break;
  628. end;
  629. else Message(asmr_e_unable_to_determine_reference_size);
  630. end;
  631. end;
  632. end;
  633. end;
  634. msiMemRegx64y128:
  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_Q;
  643. tx86operand(operands[i]).size := OS_M64;
  644. break;
  645. end;
  646. R_SUBMMY: begin
  647. tx86operand(operands[i]).opsize := S_XMM;
  648. tx86operand(operands[i]).size := OS_M128;
  649. break;
  650. end;
  651. else Message(asmr_e_unable_to_determine_reference_size);
  652. end;
  653. end;
  654. end;
  655. end;
  656. msiMemRegx64y256:
  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_Q;
  665. tx86operand(operands[i]).size := OS_M64;
  666. break;
  667. end;
  668. R_SUBMMY: begin
  669. tx86operand(operands[i]).opsize := S_YMM;
  670. tx86operand(operands[i]).size := OS_M256;
  671. break;
  672. end;
  673. else Message(asmr_e_unable_to_determine_reference_size);
  674. end;
  675. end;
  676. end;
  677. end;
  678. msiNoSize: ; // all memory-sizes are ok
  679. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  680. end;
  681. end;
  682. OPR_CONSTANT:
  683. case MemRefInfo(opcode).ConstSize of
  684. csiMem8: begin
  685. tx86operand(operands[i]).opsize := S_B;
  686. tx86operand(operands[i]).size := OS_8;
  687. end;
  688. csiMem16: begin
  689. tx86operand(operands[i]).opsize := S_W;
  690. tx86operand(operands[i]).size := OS_16;
  691. end;
  692. csiMem32: begin
  693. tx86operand(operands[i]).opsize := S_L;
  694. tx86operand(operands[i]).size := OS_32;
  695. end;
  696. end;
  697. end;
  698. end;
  699. end;
  700. end;
  701. for i:=1 to ops do
  702. begin
  703. operands[i].SetCorrectSize(opcode);
  704. if tx86operand(operands[i]).opsize=S_NO then
  705. begin
  706. {$ifdef x86_64}
  707. if (opcode=A_MOVQ) and
  708. (ops=2) and
  709. (operands[1].opr.typ=OPR_CONSTANT) then
  710. opsize:=S_Q
  711. else
  712. {$endif x86_64}
  713. case operands[i].Opr.Typ of
  714. OPR_LOCAL,
  715. OPR_REFERENCE :
  716. begin
  717. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  718. don't consider it. }
  719. if i=ops then
  720. operand2:=i-1
  721. else
  722. operand2:=i+1;
  723. if operand2>0 then
  724. begin
  725. { Only allow register as operand to take the size from }
  726. if operands[operand2].opr.typ=OPR_REGISTER then
  727. begin
  728. if ((opcode<>A_MOVD) and
  729. (opcode<>A_CVTSI2SS)) then
  730. begin
  731. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  732. // torsten - 31.01.2012
  733. // old: xmm/ymm-register operands have a opsize = "S_NO"
  734. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  735. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  736. // in this case is we need the old handling ("S_NO")
  737. // =>> ignore
  738. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  739. (tx86operand(operands[operand2]).opsize <> S_YMM) and
  740. (tx86operand(operands[operand2]).opsize <> S_ZMM) then
  741. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  742. else tx86operand(operands[operand2]).opsize := S_NO;
  743. end;
  744. end
  745. else
  746. begin
  747. { if no register then take the opsize (which is available with ATT),
  748. if not availble then give an error }
  749. if opsize<>S_NO then
  750. tx86operand(operands[i]).opsize:=opsize
  751. else
  752. begin
  753. if (m_delphi in current_settings.modeswitches) then
  754. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  755. else
  756. Message(asmr_e_unable_to_determine_reference_size);
  757. { recovery }
  758. tx86operand(operands[i]).opsize:=S_L;
  759. end;
  760. end;
  761. end
  762. else
  763. begin
  764. if opsize<>S_NO then
  765. tx86operand(operands[i]).opsize:=opsize
  766. end;
  767. end;
  768. OPR_SYMBOL :
  769. begin
  770. { Fix lea which need a reference }
  771. if opcode=A_LEA then
  772. begin
  773. s:=operands[i].opr.symbol;
  774. so:=operands[i].opr.symofs;
  775. operands[i].opr.typ:=OPR_REFERENCE;
  776. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  777. operands[i].opr.ref.symbol:=s;
  778. operands[i].opr.ref.offset:=so;
  779. end;
  780. {$if defined(x86_64)}
  781. tx86operand(operands[i]).opsize:=S_Q;
  782. {$elseif defined(i386)}
  783. tx86operand(operands[i]).opsize:=S_L;
  784. {$elseif defined(i8086)}
  785. tx86operand(operands[i]).opsize:=S_W;
  786. {$endif}
  787. end;
  788. end;
  789. end;
  790. end;
  791. if MemRefInfo(opcode).ExistsSSEAVX then
  792. begin
  793. // validate broadcast-memory-operands
  794. vbcst := 0;
  795. mmregs := [];
  796. for i := 1 to ops do
  797. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then vbcst := tx86operand(operands[i]).vbcst
  798. else if operands[i].Opr.Typ = OPR_REGISTER then
  799. begin
  800. if getsubreg(operands[i].opr.reg) in [R_SUBMMX, R_SUBMMY, R_SUBMMZ] then
  801. begin
  802. include(mmregs, getsubreg(operands[i].opr.reg));
  803. end;
  804. end;
  805. if vbcst <> 0 then
  806. begin
  807. // found broadcast-memory-operand (e.g. "{1to8}")
  808. // check is correct
  809. multiplicator := 0;
  810. if mmregs = [R_SUBMMX] then multiplicator := 1
  811. else if mmregs = [R_SUBMMY] then multiplicator := 2
  812. else if mmregs = [R_SUBMMZ] then multiplicator := 3
  813. else
  814. begin
  815. //TG TODO
  816. end;
  817. if MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator <> vbcst then
  818. begin
  819. str(MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator, bcst1);
  820. str(vbcst, bcst2);
  821. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  822. end;
  823. end;
  824. end;
  825. end;
  826. procedure Tx86Instruction.SetInstructionOpsize;
  827. begin
  828. if opsize<>S_NO then
  829. exit;
  830. case ops of
  831. 0 : ;
  832. 1 :
  833. begin
  834. { "push es" must be stored as a long PM }
  835. if ((opcode=A_PUSH) or
  836. (opcode=A_POP)) and
  837. (operands[1].opr.typ=OPR_REGISTER) and
  838. is_segment_reg(operands[1].opr.reg) then
  839. {$ifdef i8086}
  840. opsize:=S_W
  841. {$else i8086}
  842. opsize:=S_L
  843. {$endif i8086}
  844. else
  845. opsize:=tx86operand(operands[1]).opsize;
  846. end;
  847. 2 :
  848. begin
  849. case opcode of
  850. A_MOVZX,A_MOVSX :
  851. begin
  852. if tx86operand(operands[1]).opsize=S_NO then
  853. begin
  854. tx86operand(operands[1]).opsize:=S_B;
  855. if (m_delphi in current_settings.modeswitches) then
  856. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  857. else
  858. Message(asmr_e_unable_to_determine_reference_size);
  859. end;
  860. case tx86operand(operands[1]).opsize of
  861. S_W :
  862. case tx86operand(operands[2]).opsize of
  863. S_L :
  864. opsize:=S_WL;
  865. {$ifdef x86_64}
  866. S_Q :
  867. opsize:=S_WQ;
  868. {$endif}
  869. end;
  870. S_B :
  871. begin
  872. case tx86operand(operands[2]).opsize of
  873. S_W :
  874. opsize:=S_BW;
  875. S_L :
  876. opsize:=S_BL;
  877. {$ifdef x86_64}
  878. S_Q :
  879. opsize:=S_BQ;
  880. {$endif}
  881. end;
  882. end;
  883. end;
  884. end;
  885. A_MOVSS,
  886. A_VMOVSS,
  887. A_MOVD : { movd is a move from a mmx register to a
  888. 32 bit register or memory, so no opsize is correct here PM }
  889. exit;
  890. A_MOVQ :
  891. opsize:=S_IQ;
  892. A_CVTSI2SS,
  893. A_CVTSI2SD,
  894. A_OUT :
  895. opsize:=tx86operand(operands[1]).opsize;
  896. else
  897. opsize:=tx86operand(operands[2]).opsize;
  898. end;
  899. end;
  900. 3 :
  901. begin
  902. case opcode of
  903. A_VCVTSI2SS,
  904. A_VCVTSI2SD:
  905. opsize:=tx86operand(operands[1]).opsize;
  906. else
  907. opsize:=tx86operand(operands[ops]).opsize;
  908. end;
  909. end;
  910. 4 :
  911. opsize:=tx86operand(operands[ops]).opsize;
  912. end;
  913. end;
  914. procedure Tx86Instruction.CheckOperandSizes;
  915. var
  916. sizeerr : boolean;
  917. i : longint;
  918. begin
  919. { Check only the most common opcodes here, the others are done in
  920. the assembler pass }
  921. case opcode of
  922. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  923. A_CMP,A_MOV,
  924. A_ADD,A_SUB,A_ADC,A_SBB,
  925. A_AND,A_OR,A_TEST,A_XOR: ;
  926. else
  927. exit;
  928. end;
  929. { Handle the BW,BL,WL separatly }
  930. sizeerr:=false;
  931. { special push/pop selector case }
  932. if ((opcode=A_PUSH) or
  933. (opcode=A_POP)) and
  934. (operands[1].opr.typ=OPR_REGISTER) and
  935. is_segment_reg(operands[1].opr.reg) then
  936. exit;
  937. if opsize in [S_BW,S_BL,S_WL] then
  938. begin
  939. if ops<>2 then
  940. sizeerr:=true
  941. else
  942. begin
  943. case opsize of
  944. S_BW :
  945. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  946. S_BL :
  947. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  948. S_WL :
  949. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  950. end;
  951. end;
  952. end
  953. else
  954. begin
  955. for i:=1 to ops do
  956. begin
  957. if (operands[i].opr.typ<>OPR_CONSTANT) and
  958. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  959. (tx86operand(operands[i]).opsize<>opsize) then
  960. sizeerr:=true;
  961. end;
  962. end;
  963. if sizeerr then
  964. begin
  965. { if range checks are on then generate an error }
  966. if (cs_compilesystem in current_settings.moduleswitches) or
  967. not (cs_check_range in current_settings.localswitches) then
  968. Message(asmr_w_size_suffix_and_dest_dont_match)
  969. else
  970. Message(asmr_e_size_suffix_and_dest_dont_match);
  971. end;
  972. end;
  973. { This check must be done with the operand in ATT order
  974. i.e.after swapping in the intel reader
  975. but before swapping in the NASM and TASM writers PM }
  976. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  977. begin
  978. if (
  979. (ops=2) and
  980. (operands[1].opr.typ=OPR_REGISTER) and
  981. (operands[2].opr.typ=OPR_REGISTER) and
  982. { if the first is ST and the second is also a register
  983. it is necessarily ST1 .. ST7 }
  984. ((operands[1].opr.reg=NR_ST) or
  985. (operands[1].opr.reg=NR_ST0))
  986. ) or
  987. (ops=0) then
  988. if opcode=A_FSUBR then
  989. opcode:=A_FSUB
  990. else if opcode=A_FSUB then
  991. opcode:=A_FSUBR
  992. else if opcode=A_FDIVR then
  993. opcode:=A_FDIV
  994. else if opcode=A_FDIV then
  995. opcode:=A_FDIVR
  996. else if opcode=A_FSUBRP then
  997. opcode:=A_FSUBP
  998. else if opcode=A_FSUBP then
  999. opcode:=A_FSUBRP
  1000. else if opcode=A_FDIVRP then
  1001. opcode:=A_FDIVP
  1002. else if opcode=A_FDIVP then
  1003. opcode:=A_FDIVRP;
  1004. if (
  1005. (ops=1) and
  1006. (operands[1].opr.typ=OPR_REGISTER) and
  1007. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1008. (operands[1].opr.reg<>NR_ST) and
  1009. (operands[1].opr.reg<>NR_ST0)
  1010. ) then
  1011. if opcode=A_FSUBRP then
  1012. opcode:=A_FSUBP
  1013. else if opcode=A_FSUBP then
  1014. opcode:=A_FSUBRP
  1015. else if opcode=A_FDIVRP then
  1016. opcode:=A_FDIVP
  1017. else if opcode=A_FDIVP then
  1018. opcode:=A_FDIVRP;
  1019. end;
  1020. procedure Tx86Instruction.FixupOpcode;
  1021. begin
  1022. { does nothing by default }
  1023. end;
  1024. {*****************************************************************************
  1025. opcode Adding
  1026. *****************************************************************************}
  1027. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  1028. var
  1029. siz : topsize;
  1030. i : longint;
  1031. asize : int64;
  1032. ai : taicpu;
  1033. //TG TODO delete
  1034. ocode: tasmop;
  1035. begin
  1036. ConcatInstruction:=nil;
  1037. ai:=nil;
  1038. for i:=1 to Ops do
  1039. if not operands[i].CheckOperand then
  1040. exit;
  1041. { Get Opsize }
  1042. if (opsize<>S_NO) or (Ops=0) then
  1043. siz:=opsize
  1044. else
  1045. begin
  1046. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  1047. siz:=tx86operand(operands[1]).opsize
  1048. else
  1049. siz:=tx86operand(operands[Ops]).opsize;
  1050. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  1051. if (ops=2) and
  1052. (tx86operand(operands[1]).opsize<>S_NO) and
  1053. (tx86operand(operands[2]).opsize<>S_NO) and
  1054. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  1055. siz:=S_NO;
  1056. end;
  1057. if ((opcode=A_MOVD)or
  1058. (opcode=A_CVTSI2SS)) and
  1059. ((tx86operand(operands[1]).opsize=S_NO) or
  1060. (tx86operand(operands[2]).opsize=S_NO)) then
  1061. siz:=S_NO;
  1062. { NASM does not support FADD without args
  1063. as alias of FADDP
  1064. and GNU AS interprets FADD without operand differently
  1065. for version 2.9.1 and 2.9.5 !! }
  1066. if (ops=0) and
  1067. ((opcode=A_FADD) or
  1068. (opcode=A_FMUL) or
  1069. (opcode=A_FSUB) or
  1070. (opcode=A_FSUBR) or
  1071. (opcode=A_FDIV) or
  1072. (opcode=A_FDIVR)) then
  1073. begin
  1074. if opcode=A_FADD then
  1075. opcode:=A_FADDP
  1076. else if opcode=A_FMUL then
  1077. opcode:=A_FMULP
  1078. else if opcode=A_FSUB then
  1079. opcode:=A_FSUBP
  1080. else if opcode=A_FSUBR then
  1081. opcode:=A_FSUBRP
  1082. else if opcode=A_FDIV then
  1083. opcode:=A_FDIVP
  1084. else if opcode=A_FDIVR then
  1085. opcode:=A_FDIVRP;
  1086. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  1087. end;
  1088. {It is valid to specify some instructions without operand size.}
  1089. if siz=S_NO then
  1090. begin
  1091. if (ops=1) and (opcode=A_INT) then
  1092. siz:=S_B;
  1093. if (ops=1) and (opcode=A_XABORT) then
  1094. siz:=S_B;
  1095. {$ifdef i8086}
  1096. if (ops=1) and (opcode=A_BRKEM) then
  1097. siz:=S_B;
  1098. {$endif i8086}
  1099. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) or
  1100. (opcode=A_RETW) or (opcode=A_RETNW) or (opcode=A_RETFW) or
  1101. {$ifndef x86_64}
  1102. (opcode=A_RETD) or (opcode=A_RETND) or
  1103. {$endif x86_64}
  1104. (opcode=A_RETFD)
  1105. {$ifdef x86_64}
  1106. or (opcode=A_RETQ) or (opcode=A_RETNQ) or (opcode=A_RETFQ)
  1107. {$endif x86_64}
  1108. then
  1109. siz:=S_W;
  1110. if (ops=1) and (opcode=A_PUSH) then
  1111. begin
  1112. {$ifdef i8086}
  1113. if (tx86operand(operands[1]).opr.val>=-128) and (tx86operand(operands[1]).opr.val<=127) then
  1114. begin
  1115. siz:=S_B;
  1116. message(asmr_w_unable_to_determine_constant_size_using_byte);
  1117. end
  1118. else
  1119. begin
  1120. siz:=S_W;
  1121. message(asmr_w_unable_to_determine_constant_size_using_word);
  1122. end;
  1123. {$else i8086}
  1124. { We are a 32 compiler, assume 32-bit by default. This is Delphi
  1125. compatible but bad coding practise.}
  1126. siz:=S_L;
  1127. message(asmr_w_unable_to_determine_reference_size_using_dword);
  1128. {$endif i8086}
  1129. end;
  1130. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  1131. if ops=1 then
  1132. siz:=S_NEAR
  1133. else
  1134. siz:=S_FAR;
  1135. end;
  1136. { GNU AS interprets FDIV without operand differently
  1137. for version 2.9.1 and 2.10
  1138. we add explicit args to it !! }
  1139. if (ops=0) and
  1140. ((opcode=A_FSUBP) or
  1141. (opcode=A_FSUBRP) or
  1142. (opcode=A_FDIVP) or
  1143. (opcode=A_FDIVRP) or
  1144. (opcode=A_FSUB) or
  1145. (opcode=A_FSUBR) or
  1146. (opcode=A_FADD) or
  1147. (opcode=A_FADDP) or
  1148. (opcode=A_FDIV) or
  1149. (opcode=A_FDIVR)) then
  1150. begin
  1151. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  1152. ops:=2;
  1153. operands[1].opr.typ:=OPR_REGISTER;
  1154. operands[2].opr.typ:=OPR_REGISTER;
  1155. operands[1].opr.reg:=NR_ST0;
  1156. operands[2].opr.reg:=NR_ST1;
  1157. end;
  1158. if (ops=1) and
  1159. (
  1160. (operands[1].opr.typ=OPR_REGISTER) and
  1161. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1162. (operands[1].opr.reg<>NR_ST) and
  1163. (operands[1].opr.reg<>NR_ST0)
  1164. ) and
  1165. (
  1166. (opcode=A_FSUBP) or
  1167. (opcode=A_FSUBRP) or
  1168. (opcode=A_FDIVP) or
  1169. (opcode=A_FDIVRP) or
  1170. (opcode=A_FADDP) or
  1171. (opcode=A_FMULP)
  1172. ) then
  1173. begin
  1174. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  1175. ops:=2;
  1176. operands[2].opr.typ:=OPR_REGISTER;
  1177. operands[2].opr.reg:=operands[1].opr.reg;
  1178. operands[1].opr.reg:=NR_ST0;
  1179. end;
  1180. if (ops=1) and
  1181. (
  1182. (operands[1].opr.typ=OPR_REGISTER) and
  1183. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1184. (operands[1].opr.reg<>NR_ST) and
  1185. (operands[1].opr.reg<>NR_ST0)
  1186. ) and
  1187. (
  1188. (opcode=A_FSUB) or
  1189. (opcode=A_FSUBR) or
  1190. (opcode=A_FDIV) or
  1191. (opcode=A_FDIVR) or
  1192. (opcode=A_FADD) or
  1193. (opcode=A_FMUL)
  1194. ) then
  1195. begin
  1196. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  1197. ops:=2;
  1198. operands[2].opr.typ:=OPR_REGISTER;
  1199. operands[2].opr.reg:=NR_ST0;
  1200. end;
  1201. { Check for 'POP CS' }
  1202. if (opcode=A_POP) and (ops=1) and (operands[1].opr.typ=OPR_REGISTER) and
  1203. (operands[1].opr.reg=NR_CS) then
  1204. {$ifdef i8086}
  1205. { On i8086 we print only a warning, because 'POP CS' works on 8086 and 8088
  1206. CPUs, but isn't supported on any later CPU }
  1207. Message(asmr_w_pop_cs_not_portable);
  1208. {$else i8086}
  1209. { On the i386 and x86_64 targets, we print out an error, because no CPU,
  1210. supported by these targets support 'POP CS' }
  1211. Message(asmr_e_pop_cs_not_valid);
  1212. {$endif i8086}
  1213. { I tried to convince Linus Torvalds to add
  1214. code to support ENTER instruction
  1215. (when raising a stack page fault)
  1216. but he replied that ENTER is a bad instruction and
  1217. Linux does not need to support it
  1218. So I think its at least a good idea to add a warning
  1219. if someone uses this in assembler code
  1220. FPC itself does not use it at all PM }
  1221. if (opcode=A_ENTER) and
  1222. (target_info.system in [system_i386_linux,system_i386_FreeBSD,system_i386_android]) then
  1223. Message(asmr_w_enter_not_supported_by_linux);
  1224. //TG TODO delete
  1225. oCode := opcode;
  1226. ai:=taicpu.op_none(opcode,siz);
  1227. ai.fileinfo:=filepos;
  1228. ai.SetOperandOrder(op_att);
  1229. ai.Ops:=Ops;
  1230. ai.Allocate_oper(Ops);
  1231. for i:=1 to Ops do
  1232. begin
  1233. ai.oper[i-1]^.vopext := (operands[i] as tx86operand).vopext;
  1234. case operands[i].opr.typ of
  1235. OPR_CONSTANT :
  1236. ai.loadconst(i-1,operands[i].opr.val);
  1237. OPR_REGISTER:
  1238. ai.loadreg(i-1,operands[i].opr.reg);
  1239. OPR_SYMBOL:
  1240. {$ifdef i8086}
  1241. if operands[i].opr.symseg then
  1242. taicpu(ai).loadsegsymbol(i-1,operands[i].opr.symbol)
  1243. else
  1244. {$endif i8086}
  1245. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1246. OPR_LOCAL :
  1247. with operands[i].opr do
  1248. begin
  1249. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1250. localscale,localgetoffset,localforceref);
  1251. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1252. end;
  1253. OPR_REFERENCE:
  1254. begin
  1255. if (opcode<>A_XLAT) and not is_x86_string_op(opcode) then
  1256. optimize_ref(operands[i].opr.ref,true);
  1257. ai.loadref(i-1,operands[i].opr.ref);
  1258. if operands[i].size<>OS_NO then
  1259. begin
  1260. asize:=0;
  1261. case operands[i].size of
  1262. OS_8,OS_S8 :
  1263. asize:=OT_BITS8;
  1264. OS_16,OS_S16, OS_M16:
  1265. asize:=OT_BITS16;
  1266. OS_32,OS_S32 :
  1267. {$ifdef i8086}
  1268. if siz=S_FAR then
  1269. asize:=OT_FAR
  1270. else
  1271. asize:=OT_BITS32;
  1272. {$else i8086}
  1273. asize:=OT_BITS32;
  1274. {$endif i8086}
  1275. OS_F32,OS_M32 :
  1276. asize:=OT_BITS32;
  1277. OS_64,OS_S64:
  1278. begin
  1279. { Only FPU operations know about 64bit values, for all
  1280. integer operations it is seen as 32bit
  1281. this applies only to i386, see tw16622}
  1282. if gas_needsuffix[opcode] in [attsufFPU,attsufFPUint] then
  1283. asize:=OT_BITS64
  1284. {$ifdef i386}
  1285. else
  1286. asize:=OT_BITS32
  1287. {$endif i386}
  1288. ;
  1289. end;
  1290. OS_F64,OS_C64, OS_M64 :
  1291. asize:=OT_BITS64;
  1292. OS_F80 :
  1293. asize:=OT_BITS80;
  1294. OS_128,OS_M128,OS_MS128:
  1295. asize := OT_BITS128;
  1296. OS_M256,OS_MS256:
  1297. asize := OT_BITS256;
  1298. OS_M512,OS_MS512:
  1299. asize := OT_BITS512;
  1300. end;
  1301. if asize<>0 then
  1302. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1303. end;
  1304. end;
  1305. end;
  1306. end;
  1307. { Condition ? }
  1308. if condition<>C_None then
  1309. ai.SetCondition(condition);
  1310. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  1311. if (opcode=A_JMP) or (opcode=A_JCC) then
  1312. ai.is_jmp := True;
  1313. { Concat the opcode or give an error }
  1314. if assigned(ai) then
  1315. p.concat(ai)
  1316. else
  1317. Message(asmr_e_invalid_opcode_and_operand);
  1318. result:=ai;
  1319. end;
  1320. function Tx86Instruction.getstring: string;
  1321. var
  1322. i : longint;
  1323. s, sval : string;
  1324. regnr: string;
  1325. addsize : boolean;
  1326. begin
  1327. s:='['+std_op2str[opcode];
  1328. for i:=1 to ops do
  1329. begin
  1330. with operands[i] as Tx86Operand do
  1331. begin
  1332. if i=0 then
  1333. s:=s+' '
  1334. else
  1335. s:=s+',';
  1336. { type }
  1337. addsize:=false;
  1338. case operands[i].opr.typ of
  1339. OPR_CONSTANT : begin
  1340. str(operands[i].opr.val, sval);
  1341. s:=s+ sval;
  1342. end;
  1343. OPR_REGISTER : begin
  1344. regnr := '';
  1345. str(getsupreg(opr.reg),regnr);
  1346. if getsubreg(opr.reg)= R_SUBMMX then
  1347. s:=s+'xmmreg' + regnr
  1348. else
  1349. if getsubreg(opr.reg)= R_SUBMMY then
  1350. s:=s+'ymmreg' + regnr
  1351. else
  1352. if getsubreg(opr.reg)= R_SUBMMZ then
  1353. s:=s+'zmmreg' + regnr
  1354. else
  1355. if getregtype(opr.reg)= R_MMXREGISTER then
  1356. s:=s+'mmxreg'
  1357. else
  1358. if getregtype(opr.reg)= R_FPUREGISTER then
  1359. s:=s+'fpureg'
  1360. else
  1361. if getregtype(opr.reg)=R_INTREGISTER then
  1362. begin
  1363. s:=s+'reg';
  1364. addsize:=true;
  1365. end;
  1366. end;
  1367. OPR_LOCAL,
  1368. OPR_REFERENCE: begin
  1369. s:=s + 'mem';
  1370. addsize:=true;
  1371. end;
  1372. else s:=s + '???';
  1373. end;
  1374. if addsize then
  1375. begin
  1376. sval := '';
  1377. str(tcgsize2size[size], sval);
  1378. s := s + sval;
  1379. end;
  1380. if vopext <> 0 then
  1381. begin
  1382. str(vopext and $07, regnr);
  1383. if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  1384. s := s + ' {k' + regnr + '}';
  1385. if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  1386. s := s + ' {z}';
  1387. if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  1388. s := s + ' {sae}';
  1389. if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  1390. case vopext and OTVE_VECTOR_BCST_MASK of
  1391. OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  1392. OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  1393. OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  1394. OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  1395. end;
  1396. if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  1397. case vopext and OTVE_VECTOR_ER_MASK of
  1398. OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  1399. OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  1400. OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  1401. OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  1402. end;
  1403. end;
  1404. end;
  1405. end;
  1406. GetString:=s+']';
  1407. end;
  1408. end.