rax86.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279
  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(ins : TInstruction): 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(aAddMemRefSize: boolean = true): string;
  62. { returns true, if the opcode might have an extension as used by AVX512 }
  63. function MightHaveExtension : boolean;
  64. end;
  65. const
  66. AsmPrefixes = 8{$ifdef i8086}+2{$endif i8086};
  67. AsmPrefix : array[0..AsmPrefixes-1] of TasmOP =(
  68. A_LOCK,A_REP,A_REPE,A_REPNE,A_REPNZ,A_REPZ,A_XACQUIRE,A_XRELEASE{$ifdef i8086},A_REPC,A_REPNC{$endif i8086}
  69. );
  70. AsmOverrides = 6;
  71. AsmOverride : array[0..AsmOverrides-1] of TasmOP =(
  72. A_SEGCS,A_SEGES,A_SEGDS,A_SEGFS,A_SEGGS,A_SEGSS
  73. );
  74. CondAsmOps=3;
  75. CondAsmOp:array[0..CondAsmOps-1] of TasmOp=(
  76. A_CMOVcc, A_Jcc, A_SETcc
  77. );
  78. CondAsmOpStr:array[0..CondAsmOps-1] of string[4]=(
  79. 'CMOV','J','SET'
  80. );
  81. implementation
  82. uses
  83. globtype,globals,systems,verbose,
  84. procinfo,
  85. cgbase,cgutils,
  86. itcpugas,cgx86, cutils;
  87. {*****************************************************************************
  88. Parser Helpers
  89. *****************************************************************************}
  90. function is_prefix(t:tasmop):boolean;
  91. var
  92. i : longint;
  93. Begin
  94. is_prefix:=false;
  95. for i:=1 to AsmPrefixes do
  96. if t=AsmPrefix[i-1] then
  97. begin
  98. is_prefix:=true;
  99. exit;
  100. end;
  101. end;
  102. function is_override(t:tasmop):boolean;
  103. var
  104. i : longint;
  105. Begin
  106. is_override:=false;
  107. for i:=1 to AsmOverrides do
  108. if t=AsmOverride[i-1] then
  109. begin
  110. is_override:=true;
  111. exit;
  112. end;
  113. end;
  114. Function CheckPrefix(prefixop,op:tasmop): Boolean;
  115. { Checks if the prefix is valid with the following opcode }
  116. { return false if not, otherwise true }
  117. Begin
  118. CheckPrefix := TRUE;
  119. (* Case prefix of
  120. A_REP,A_REPNE,A_REPE:
  121. Case opcode Of
  122. A_SCASB,A_SCASW,A_SCASD,
  123. A_INS,A_OUTS,A_MOVS,A_CMPS,A_LODS,A_STOS:;
  124. Else
  125. Begin
  126. CheckPrefix := FALSE;
  127. exit;
  128. end;
  129. end; { case }
  130. A_LOCK:
  131. Case opcode Of
  132. A_BT,A_BTS,A_BTR,A_BTC,A_XCHG,A_ADD,A_OR,A_ADC,A_SBB,A_AND,A_SUB,
  133. A_XOR,A_NOT,A_NEG,A_INC,A_DEC:;
  134. Else
  135. Begin
  136. CheckPrefix := FALSE;
  137. Exit;
  138. end;
  139. end; { case }
  140. A_NONE: exit; { no prefix here }
  141. else
  142. CheckPrefix := FALSE;
  143. end; { end case } *)
  144. end;
  145. Function CheckOverride(overrideop,op:tasmop): Boolean;
  146. { Check if the override is valid, and if so then }
  147. { update the instr variable accordingly. }
  148. Begin
  149. CheckOverride := true;
  150. { Case instr.getinstruction of
  151. A_MOVS,A_XLAT,A_CMPS:
  152. Begin
  153. CheckOverride := TRUE;
  154. Message(assem_e_segment_override_not_supported);
  155. end
  156. end }
  157. end;
  158. Procedure FWaitWarning;
  159. begin
  160. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in current_settings.moduleswitches) then
  161. Message(asmr_w_fwait_emu_prob);
  162. end;
  163. {*****************************************************************************
  164. TX86Operand
  165. *****************************************************************************}
  166. Procedure Tx86Operand.SetSize(_size:longint;force:boolean);
  167. begin
  168. inherited SetSize(_size,force);
  169. { OS_64 will be set to S_L and be fixed later
  170. in SetCorrectSize }
  171. // multimedia register
  172. case _size of
  173. 16: size := OS_M128;
  174. 32: size := OS_M256;
  175. 64: size := OS_M512;
  176. end;
  177. {$ifdef i8086}
  178. { allows e.g. using 32-bit registers in i8086 inline asm }
  179. if size in [OS_32,OS_S32] then
  180. opsize:=S_L
  181. else
  182. {$endif i8086}
  183. opsize:=TCGSize2Opsize[size];
  184. end;
  185. Procedure Tx86Operand.SetCorrectSize(opcode:tasmop);
  186. begin
  187. if gas_needsuffix[opcode]=attsufFPU then
  188. begin
  189. case size of
  190. OS_32 : opsize:=S_FS;
  191. OS_64 : opsize:=S_FL;
  192. else
  193. ;
  194. end;
  195. end
  196. else if gas_needsuffix[opcode]=attsufFPUint then
  197. begin
  198. case size of
  199. OS_16 : opsize:=S_IS;
  200. OS_32 : opsize:=S_IL;
  201. OS_64 : opsize:=S_IQ;
  202. else
  203. ;
  204. end;
  205. end
  206. else if gas_needsuffix[opcode] in [AttSufMM, AttSufMMX, AttSufMMS] then
  207. begin
  208. if (opr.typ=OPR_Reference) then
  209. begin
  210. case size of
  211. OS_32 : size := OS_M32;
  212. OS_64 : size := OS_M64;
  213. else
  214. ;
  215. end;
  216. end;
  217. end
  218. else
  219. begin
  220. if size=OS_64 then
  221. opsize:=S_Q;
  222. end;
  223. end;
  224. Function Tx86Operand.CheckOperand(ins : TInstruction): boolean;
  225. var
  226. ErrorRefStr: string;
  227. begin
  228. result:=true;
  229. if (opr.typ=OPR_Reference) then
  230. begin
  231. if not hasvar then
  232. begin
  233. if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset>0) then
  234. begin
  235. if current_settings.asmmode in asmmodes_x86_intel then
  236. begin
  237. case getsubreg(opr.ref.base) of
  238. R_SUBW:
  239. ErrorRefStr:='[BP+offset]';
  240. R_SUBD:
  241. ErrorRefStr:='[EBP+offset]';
  242. R_SUBQ:
  243. ErrorRefStr:='[RBP+offset]';
  244. else
  245. internalerror(2019061001);
  246. end;
  247. end
  248. else
  249. begin
  250. case getsubreg(opr.ref.base) of
  251. R_SUBW:
  252. ErrorRefStr:='+offset(%bp)';
  253. R_SUBD:
  254. ErrorRefStr:='+offset(%ebp)';
  255. R_SUBQ:
  256. ErrorRefStr:='+offset(%rbp)';
  257. else
  258. internalerror(2019061002);
  259. end;
  260. end;
  261. if current_procinfo.procdef.proccalloption=pocall_register then
  262. message1(asmr_w_no_direct_ebp_for_parameter,ErrorRefStr)
  263. else
  264. message1(asmr_w_direct_ebp_for_parameter_regcall,ErrorRefStr);
  265. end
  266. else if (getsupreg(opr.ref.base)=RS_EBP) and (opr.ref.offset<0) then
  267. begin
  268. if current_settings.asmmode in asmmodes_x86_intel then
  269. begin
  270. case getsubreg(opr.ref.base) of
  271. R_SUBW:
  272. ErrorRefStr:='[BP-offset]';
  273. R_SUBD:
  274. ErrorRefStr:='[EBP-offset]';
  275. R_SUBQ:
  276. ErrorRefStr:='[RBP-offset]';
  277. else
  278. internalerror(2019061003);
  279. end;
  280. end
  281. else
  282. begin
  283. case getsubreg(opr.ref.base) of
  284. R_SUBW:
  285. ErrorRefStr:='-offset(%bp)';
  286. R_SUBD:
  287. ErrorRefStr:='-offset(%ebp)';
  288. R_SUBQ:
  289. ErrorRefStr:='-offset(%rbp)';
  290. else
  291. internalerror(2019061004);
  292. end;
  293. end;
  294. message1(asmr_w_direct_ebp_neg_offset,ErrorRefStr);
  295. end
  296. else if ((ins.opcode<>A_LEA) and (getsupreg(opr.ref.base)=RS_ESP) and (getsubreg(opr.ref.base)<>R_SUBW) and (opr.ref.offset<0)) or
  297. ((ins.opcode=A_LEA) and (getsupreg(ins.operands[2].opr.reg)<>RS_ESP) and (getsupreg(opr.ref.base)=RS_ESP) and (getsubreg(opr.ref.base)<>R_SUBW) and (opr.ref.offset<0)) then
  298. begin
  299. if current_settings.asmmode in asmmodes_x86_intel then
  300. begin
  301. case getsubreg(opr.ref.base) of
  302. R_SUBD:
  303. ErrorRefStr:='[ESP-offset]';
  304. R_SUBQ:
  305. ErrorRefStr:='[RSP-offset]';
  306. else
  307. internalerror(2019061005);
  308. end;
  309. end
  310. else
  311. begin
  312. case getsubreg(opr.ref.base) of
  313. R_SUBD:
  314. ErrorRefStr:='-offset(%esp)';
  315. R_SUBQ:
  316. ErrorRefStr:='-offset(%rsp)';
  317. else
  318. internalerror(2019061006);
  319. end;
  320. end;
  321. message1(asmr_w_direct_esp_neg_offset,ErrorRefStr);
  322. end;
  323. end;
  324. if (cs_create_pic in current_settings.moduleswitches) and
  325. assigned(opr.ref.symbol) and
  326. not assigned(opr.ref.relsymbol) then
  327. begin
  328. if not(opr.ref.refaddr in [addr_pic,addr_pic_no_got]) then
  329. begin
  330. if (opr.ref.symbol.name <> '_GLOBAL_OFFSET_TABLE_') then
  331. begin
  332. message(asmr_e_need_pic_ref);
  333. result:=false;
  334. end
  335. else
  336. opr.ref.refaddr:=addr_pic;
  337. end
  338. else
  339. begin
  340. {$ifdef x86_64}
  341. { should probably be extended to i386, but there the situation
  342. is more complex and ELF-style PIC still need to be
  343. tested/debugged }
  344. if (opr.ref.symbol.bind in [AB_LOCAL,AB_PRIVATE_EXTERN]) and
  345. (opr.ref.refaddr=addr_pic) then
  346. message(asmr_w_useless_got_for_local)
  347. else if (opr.ref.symbol.bind in [AB_GLOBAL,AB_EXTERNAL,AB_COMMON,AB_WEAK_EXTERNAL]) and
  348. (opr.ref.refaddr=addr_pic_no_got) then
  349. message(asmr_w_global_access_without_got);
  350. {$endif x86_64}
  351. end;
  352. end;
  353. end;
  354. end;
  355. procedure Tx86Operand.SetupCode;
  356. begin
  357. {$ifdef i8086}
  358. opr.typ:=OPR_SYMBOL;
  359. opr.symofs:=0;
  360. opr.symbol:=current_asmdata.RefAsmSymbol(current_procinfo.procdef.mangledname,AT_FUNCTION);
  361. opr.symseg:=true;
  362. opr.sym_farproc_entry:=false;
  363. {$else i8086}
  364. Message(asmr_w_CODE_and_DATA_not_supported);
  365. {$endif i8086}
  366. end;
  367. procedure Tx86Operand.SetupData;
  368. begin
  369. {$ifdef i8086}
  370. InitRef;
  371. if current_settings.x86memorymodel=mm_huge then
  372. opr.ref.refaddr:=addr_fardataseg
  373. else
  374. opr.ref.refaddr:=addr_dgroup;
  375. {$else i8086}
  376. Message(asmr_w_CODE_and_DATA_not_supported);
  377. {$endif i8086}
  378. end;
  379. constructor Tx86Operand.create;
  380. begin
  381. inherited;
  382. vopext := 0;
  383. vbcst := 0;
  384. end;
  385. {*****************************************************************************
  386. T386Instruction
  387. *****************************************************************************}
  388. constructor Tx86Instruction.Create(optype : tcoperand);
  389. begin
  390. inherited Create(optype);
  391. Opsize:=S_NO;
  392. end;
  393. procedure Tx86Instruction.AddReferenceSizes;
  394. { this will add the sizes for references like [esi] which do not
  395. have the size set yet, it will take only the size if the other
  396. operand is a register }
  397. var
  398. operand2,i,j,k : longint;
  399. s : tasmsymbol;
  400. so : aint;
  401. ExistsMemRefNoSize: boolean;
  402. ExistsMemRef: boolean;
  403. ExistsConstNoSize: boolean;
  404. ExistConst: boolean;
  405. ExistsLocalSymSize: boolean;
  406. ExistsBCST: boolean;
  407. memrefsize: integer;
  408. memopsize: integer;
  409. memoffset: asizeint;
  410. vbcst: byte;
  411. mmregs: Set of TSubregister;
  412. multiplicator: integer;
  413. bcst1,bcst2: string;
  414. function ScanLowestOpsize(aValue: int64): int64;
  415. var
  416. i: integer;
  417. begin
  418. result := 0;
  419. if aValue and OT_BITS8 = OT_BITS8 then result := 8
  420. else if aValue and OT_BITS16 = OT_BITS16 then result := 16
  421. else if aValue and OT_BITS32 = OT_BITS32 then result := 32
  422. else if aValue and OT_BITS64 = OT_BITS64 then result := 64
  423. else if aValue and OT_BITS128 = OT_BITS128 then result := 128
  424. else if aValue and OT_BITS256 = OT_BITS256 then result := 256
  425. else if aValue and OT_BITS512 = OT_BITS512 then result := 512;
  426. end;
  427. begin
  428. ExistsMemRefNoSize := false;
  429. ExistsMemRef := false;
  430. ExistsConstNoSize := false;
  431. ExistsLocalSymSize := false;
  432. ExistsBCST := false;
  433. // EXIST A MEMORY- OR CONSTANT-OPERAND WITHOUT SIZE ?
  434. for i := 1 to ops do
  435. begin
  436. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then
  437. begin
  438. ExistsMemRef := true;
  439. ExistsBCST := (MemRefInfo(opcode).ExistsSSEAVX) and
  440. (tx86operand(operands[i]).vbcst <> 0);
  441. if (tx86operand(operands[i]).opsize = S_NO) then
  442. begin
  443. ExistsMemRefNoSize := true;
  444. case operands[i].opr.Typ of
  445. OPR_LOCAL: ExistsLocalSymSize := tx86operand(operands[i]).opr.localsym.getsize > 0;
  446. OPR_REFERENCE: ExistsLocalSymSize := true;
  447. else
  448. ;
  449. end;
  450. end;
  451. end
  452. else if operands[i].Opr.Typ in [OPR_CONSTANT] then
  453. begin
  454. ExistsConstNoSize := tx86operand(operands[i]).opsize = S_NO;
  455. end;
  456. end;
  457. // ONLY SUPPORTED OPCODES WITH SSE- OR AVX-REGISTERS
  458. if (ExistsMemRef) and
  459. (MemRefInfo(opcode).ExistsSSEAVX) then
  460. begin
  461. // 1. WE HAVE AN SSE- OR AVX-OPCODE WITH MEMORY OPERAND
  462. if (not(ExistsMemRefNoSize)) or
  463. (ExistsLocalSymSize) then
  464. begin
  465. // 2. WE KNOWN THE MEMORYSIZE OF THE MEMORY-OPERAND OR WE CAN
  466. // CALC THE MEMORYSIZE
  467. // 3. CALC THE SIZE OF THE MEMORYOPERAND BY OPCODE-DEFINITION
  468. // 4. COMPARE THE SIZE FROM OPCODE-DEFINITION AND THE REAL MEMORY-OPERAND-SIZE
  469. // - validate memory-reference-size
  470. for i := 1 to ops do
  471. begin
  472. if (operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL]) then
  473. begin
  474. memrefsize := -1;
  475. if ExistsBCST then
  476. begin
  477. case MemRefInfo(opcode).MemRefSizeBCST of
  478. msbBCST32: memrefsize := 32;
  479. msbBCST64: memrefsize := 64;
  480. else
  481. Internalerror(2019081015);
  482. end;
  483. end
  484. else
  485. case MemRefInfo(opcode).MemRefSize of
  486. msiMem8: memrefsize := 8;
  487. msiMem16: memrefsize := 16;
  488. msiMem32: memrefsize := 32;
  489. msiMem64: memrefsize := 64;
  490. msiMem128: memrefsize := 128;
  491. msiMem256: memrefsize := 256;
  492. msiMem512: memrefsize := 512;
  493. msiMemRegx16y32:
  494. begin
  495. for j := 1 to ops do
  496. begin
  497. if operands[j].Opr.Typ = OPR_REGISTER then
  498. begin
  499. case getsubreg(operands[j].opr.reg) of
  500. R_SUBMMX: memrefsize := 16;
  501. R_SUBMMY: memrefsize := 32;
  502. else Message(asmr_e_unable_to_determine_reference_size);
  503. end;
  504. end;
  505. end;
  506. end;
  507. msiMemRegx16y32z64:
  508. begin
  509. for j := 1 to ops do
  510. begin
  511. if operands[j].Opr.Typ = OPR_REGISTER then
  512. begin
  513. case getsubreg(operands[j].opr.reg) of
  514. R_SUBMMX: memrefsize := 16;
  515. R_SUBMMY: memrefsize := 32;
  516. R_SUBMMZ: memrefsize := 64;
  517. else Message(asmr_e_unable_to_determine_reference_size);
  518. end;
  519. end;
  520. end;
  521. end;
  522. msiMemRegx32y64:
  523. begin
  524. for j := 1 to ops do
  525. begin
  526. if operands[j].Opr.Typ = OPR_REGISTER then
  527. begin
  528. case getsubreg(operands[j].opr.reg) of
  529. R_SUBMMX: memrefsize := 32;
  530. R_SUBMMY: memrefsize := 64;
  531. else Message(asmr_e_unable_to_determine_reference_size);
  532. end;
  533. end;
  534. end;
  535. end;
  536. msiMemRegx32y64z128:
  537. begin
  538. for j := 1 to ops do
  539. begin
  540. if operands[j].Opr.Typ = OPR_REGISTER then
  541. begin
  542. case getsubreg(operands[j].opr.reg) of
  543. R_SUBMMX: memrefsize := 32;
  544. R_SUBMMY: memrefsize := 64;
  545. R_SUBMMZ: memrefsize := 128;
  546. else Message(asmr_e_unable_to_determine_reference_size);
  547. end;
  548. end;
  549. end;
  550. end;
  551. msiMemRegx64y128:
  552. begin
  553. for j := 1 to ops do
  554. begin
  555. if operands[j].Opr.Typ = OPR_REGISTER then
  556. begin
  557. case getsubreg(operands[j].opr.reg) of
  558. R_SUBMMX: memrefsize := 64;
  559. R_SUBMMY: memrefsize := 128;
  560. else Message(asmr_e_unable_to_determine_reference_size);
  561. end;
  562. end;
  563. end;
  564. end;
  565. msiMemRegx64y256:
  566. begin
  567. for j := 1 to ops do
  568. begin
  569. if operands[j].Opr.Typ = OPR_REGISTER then
  570. begin
  571. case getsubreg(operands[j].opr.reg) of
  572. R_SUBMMX: memrefsize := 64;
  573. R_SUBMMY: memrefsize := 256;
  574. else Message(asmr_e_unable_to_determine_reference_size);
  575. end;
  576. end;
  577. end;
  578. end;
  579. msiMemRegx64y128z256:
  580. begin
  581. begin
  582. for j := 1 to ops do
  583. begin
  584. if operands[j].Opr.Typ = OPR_REGISTER then
  585. begin
  586. case getsubreg(operands[j].opr.reg) of
  587. R_SUBMMX: memrefsize := 64;
  588. R_SUBMMY: memrefsize := 128;
  589. R_SUBMMZ: memrefsize := 256;
  590. else Message(asmr_e_unable_to_determine_reference_size);
  591. end;
  592. end;
  593. end;
  594. end;
  595. end;
  596. msiMemRegx64y256z512:
  597. begin
  598. begin
  599. for j := 1 to ops do
  600. begin
  601. if operands[j].Opr.Typ = OPR_REGISTER then
  602. begin
  603. case getsubreg(operands[j].opr.reg) of
  604. R_SUBMMX: memrefsize := 64;
  605. R_SUBMMY: memrefsize := 256;
  606. R_SUBMMZ: memrefsize := 512;
  607. else Message(asmr_e_unable_to_determine_reference_size);
  608. end;
  609. end;
  610. end;
  611. end;
  612. end;
  613. msiMemRegSize:
  614. begin
  615. for j := 1 to ops do
  616. begin
  617. if operands[j].Opr.Typ = OPR_REGISTER then
  618. begin
  619. if (tx86operand(operands[j]).opsize <> S_NO) and
  620. (tx86operand(operands[j]).size <> OS_NO) then
  621. begin
  622. case tx86operand(operands[j]).opsize of
  623. S_B : memrefsize := 8;
  624. S_W : memrefsize := 16;
  625. S_L : memrefsize := 32;
  626. S_Q : memrefsize := 64;
  627. S_XMM : memrefsize := 128;
  628. S_YMM : memrefsize := 256;
  629. S_ZMM : memrefsize := 512;
  630. else Internalerror(2019081010);
  631. end;
  632. break;
  633. end;
  634. end;
  635. end;
  636. end;
  637. msiMemRegConst128,
  638. msiMemRegConst256,
  639. msiMemRegConst512:
  640. begin
  641. for j := 1 to ops do
  642. begin
  643. if operands[j].Opr.Typ = OPR_CONSTANT then
  644. begin
  645. for k := 1 to ops do
  646. begin
  647. if operands[k].Opr.Typ = OPR_REGISTER then
  648. begin
  649. if (tx86operand(operands[k]).opsize <> S_NO) and
  650. (tx86operand(operands[k]).size <> OS_NO) then
  651. begin
  652. case tx86operand(operands[k]).opsize of
  653. S_B : memrefsize := 8;
  654. S_W : memrefsize := 16;
  655. S_L : memrefsize := 32;
  656. S_Q : memrefsize := 64;
  657. S_XMM : memrefsize := 128;
  658. S_YMM : memrefsize := 256;
  659. S_ZMM : memrefsize := 512;
  660. else Internalerror(777200);
  661. end;
  662. break;
  663. end;
  664. end;
  665. end;
  666. break;
  667. end;
  668. end;
  669. // no exists const-operand
  670. if memrefsize = -1 then
  671. begin
  672. case MemRefInfo(opcode).MemRefSize of
  673. msiMemRegConst128: memrefsize := 128;
  674. msiMemRegConst256: memrefsize := 256;
  675. msiMemRegConst512: memrefsize := 512;
  676. else Internalerror(2019081012);
  677. end;
  678. end;
  679. end;
  680. msiXMem32,
  681. msiYMem32,
  682. msiZMem32,
  683. msiXMem64,
  684. msiYMem64,
  685. msiZMem64: ; // ignore; gather/scatter opcodes haven a fixed element-size, not a fixed memory-size
  686. // the vector-register have indices with base of the memory-address in the memory-operand
  687. msiMultipleMinSize8,
  688. msiMultipleMinSize16,
  689. msiMultipleMinSize32,
  690. msiMultipleMinSize64,
  691. msiMultipleMinSize128,
  692. msiMultipleMinSize256,
  693. msiMultipleMinSize512:
  694. begin
  695. for j := 1 to ops do
  696. begin
  697. if operands[j].Opr.Typ = OPR_REGISTER then
  698. begin
  699. case getsubreg(operands[j].opr.reg) of
  700. R_SUBMMX: begin
  701. memrefsize := ScanLowestOpsize(MemRefInfo(opcode).RegXMMSizeMask);
  702. break;
  703. end;
  704. R_SUBMMY: begin
  705. memrefsize := ScanLowestOpsize(MemRefInfo(opcode).RegYMMSizeMask);
  706. break;
  707. end;
  708. R_SUBMMZ: begin
  709. memrefsize := ScanLowestOpsize(MemRefInfo(opcode).RegZMMSizeMask);
  710. break;
  711. end;
  712. else;
  713. end;
  714. end;
  715. end;
  716. if memrefsize = -1 then
  717. begin
  718. case MemRefInfo(opcode).MemRefSize of
  719. msiMultipleMinSize8: memrefsize := 8;
  720. msiMultipleMinSize16: memrefsize := 16;
  721. msiMultipleMinSize32: memrefsize := 32;
  722. msiMultipleMinSize64: memrefsize := 64;
  723. msiMultipleMinSize128: memrefsize := 128;
  724. msiMultipleMinSize256: memrefsize := 256;
  725. msiMultipleMinSize512: memrefsize := 512;
  726. else;
  727. end;
  728. end;
  729. end;
  730. msiNoSize,
  731. msiNoMemRef,
  732. msiUnknown,
  733. msiUnsupported,
  734. msiVMemMultiple,
  735. msiVMemRegSize,
  736. msiMultiple:
  737. ;
  738. else
  739. Internalerror(2020111001);
  740. end;
  741. if memrefsize > -1 then
  742. begin
  743. // CALC REAL-MEMORY-OPERAND-SIZE AND A POSSIBLE OFFSET
  744. // OFFSET:
  745. // e.g. PAND XMM0, [RAX + 16] =>> OFFSET = 16 BYTES
  746. // PAND XMM0, [RAX + a.b + 10] =>> OFFSET = 10 BYTES (a = record-variable)
  747. memopsize := 0;
  748. case operands[i].opr.typ of
  749. OPR_LOCAL: memopsize := operands[i].opr.localvarsize * 8;
  750. OPR_REFERENCE:
  751. if operands[i].opr.ref.refaddr = addr_pic then
  752. memopsize := sizeof(pint) * 8
  753. else
  754. memopsize := operands[i].opr.varsize * 8;
  755. else
  756. ;
  757. end;
  758. //if memopsize = 0 then memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  759. if memopsize = 0 then
  760. begin
  761. {$ifdef i386}
  762. { 64-bit operands are allowed for SSE and AVX instructions, so
  763. go by the byte size instead for these families of opcodes }
  764. if (MemRefInfo(opcode).ExistsSSEAVX) then
  765. begin
  766. memopsize := tx86operand(operands[i]).typesize * 8;
  767. if tx86operand(operands[i]).typesize = 8 then
  768. { Will be S_L otherwise and won't be corrected in time }
  769. tx86operand(operands[i]).opsize := S_Q;
  770. end
  771. else
  772. {$endif i386}
  773. memopsize := topsize2memsize[tx86operand(operands[i]).opsize];
  774. end;
  775. if (memopsize > 0) and
  776. (memrefsize > 0) then
  777. begin
  778. memoffset := 0;
  779. case operands[i].opr.typ of
  780. OPR_LOCAL:
  781. memoffset := operands[i].opr.localconstoffset;
  782. OPR_REFERENCE:
  783. memoffset := operands[i].opr.constoffset;
  784. else
  785. ;
  786. end;
  787. if ((operands[i].opr.ref.base=NR_ESP) or (operands[i].opr.ref.index=NR_ESP)) and (memoffset < 0) then
  788. begin
  789. Message2(asmr_w_check_mem_operand_negative_offset,
  790. //std_op2str[opcode],
  791. getstring(false),
  792. ToStr(memoffset));
  793. end
  794. else if ((tx86operand(operands[i]).hastype) and (memopsize < memrefsize)) or
  795. (memopsize < (memrefsize + memoffset * 8)) then
  796. begin
  797. if memopsize < memrefsize then
  798. begin
  799. if memoffset = 0 then
  800. begin
  801. Message3(asmr_w_check_mem_operand_size3,
  802. getstring(false),
  803. ToStr(memopsize),
  804. ToStr(memrefsize)
  805. );
  806. end
  807. else
  808. begin
  809. Message4(asmr_w_check_mem_operand_size_offset,
  810. getstring(false),
  811. ToStr(memopsize),
  812. ToStr(memrefsize),
  813. ToStr(memoffset)
  814. );
  815. end;
  816. end;
  817. end;
  818. end;
  819. end;
  820. end;
  821. end;
  822. end;
  823. end;
  824. if (ExistsMemRefNoSize or ExistsConstNoSize) and
  825. (MemRefInfo(opcode).ExistsSSEAVX) then
  826. begin
  827. for i := 1 to ops do
  828. begin
  829. if (tx86operand(operands[i]).opsize = S_NO) then
  830. begin
  831. case operands[i].Opr.Typ of
  832. OPR_REFERENCE,
  833. OPR_LOCAL:
  834. begin
  835. if ExistsBCST then
  836. begin
  837. case MemRefInfo(opcode).MemRefSizeBCST of
  838. msbBCST32: begin
  839. tx86operand(operands[i]).opsize := S_L;
  840. tx86operand(operands[i]).size := OS_32;
  841. end;
  842. msbBCST64: begin
  843. tx86operand(operands[i]).opsize := S_Q;
  844. tx86operand(operands[i]).size := OS_M64;
  845. end;
  846. else
  847. Internalerror(2019081017);
  848. end;
  849. end
  850. else
  851. case MemRefInfo(opcode).MemRefSize of
  852. msiMem8:
  853. begin
  854. tx86operand(operands[i]).opsize := S_B;
  855. tx86operand(operands[i]).size := OS_8;
  856. end;
  857. msiMultipleMinSize8:
  858. begin
  859. tx86operand(operands[i]).opsize := S_B;
  860. tx86operand(operands[i]).size := OS_8;
  861. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"8 bit memory operand"');
  862. end;
  863. msiMem16:
  864. begin
  865. tx86operand(operands[i]).opsize := S_W;
  866. tx86operand(operands[i]).size := OS_16;
  867. end;
  868. msiMultipleMinSize16:
  869. begin
  870. tx86operand(operands[i]).opsize := S_W;
  871. tx86operand(operands[i]).size := OS_16;
  872. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"16 bit memory operand"');
  873. end;
  874. msiMem32:
  875. begin
  876. tx86operand(operands[i]).opsize := S_L;
  877. tx86operand(operands[i]).size := OS_32;
  878. end;
  879. msiMultipleMinSize32:
  880. begin
  881. tx86operand(operands[i]).opsize := S_L;
  882. tx86operand(operands[i]).size := OS_32;
  883. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"32 bit memory operand"');
  884. end;
  885. msiMem64:
  886. begin
  887. tx86operand(operands[i]).opsize := S_Q;
  888. tx86operand(operands[i]).size := OS_M64;
  889. end;
  890. msiMultipleMinSize64:
  891. begin
  892. tx86operand(operands[i]).opsize := S_Q;
  893. tx86operand(operands[i]).size := OS_M64;
  894. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"64 bit memory operand"');
  895. end;
  896. msiMem128:
  897. begin
  898. tx86operand(operands[i]).opsize := S_XMM;
  899. tx86operand(operands[i]).size := OS_M128;
  900. end;
  901. msiMultipleMinSize128:
  902. begin
  903. tx86operand(operands[i]).opsize := S_XMM;
  904. tx86operand(operands[i]).size := OS_M128;
  905. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"128 bit memory operand"');
  906. end;
  907. msiMem256:
  908. begin
  909. tx86operand(operands[i]).opsize := S_YMM;
  910. tx86operand(operands[i]).size := OS_M256;
  911. opsize := S_YMM;
  912. end;
  913. msiMultipleMinSize256:
  914. begin
  915. tx86operand(operands[i]).opsize := S_YMM;
  916. tx86operand(operands[i]).size := OS_M256;
  917. opsize := S_YMM;
  918. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"256 bit memory operand"');
  919. end;
  920. msiMem512:
  921. begin
  922. tx86operand(operands[i]).opsize := S_ZMM;
  923. tx86operand(operands[i]).size := OS_M512;
  924. opsize := S_ZMM;
  925. end;
  926. msiMultipleMinSize512:
  927. begin
  928. tx86operand(operands[i]).opsize := S_ZMM;
  929. tx86operand(operands[i]).size := OS_M512;
  930. opsize := S_ZMM;
  931. Message2(asmr_w_check_mem_operand_automap_multiple_size, GetString(false), '"512 bit memory operand"');
  932. end;
  933. msiMemRegSize:
  934. begin
  935. // mem-ref-size = register size
  936. for j := 1 to ops do
  937. begin
  938. if operands[j].Opr.Typ = OPR_REGISTER then
  939. begin
  940. if (tx86operand(operands[j]).opsize <> S_NO) and
  941. (tx86operand(operands[j]).size <> OS_NO) then
  942. begin
  943. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  944. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  945. break;
  946. end
  947. else Message(asmr_e_unable_to_determine_reference_size);
  948. end;
  949. end;
  950. end;
  951. msiMemRegx16y32:
  952. begin
  953. for j := 1 to ops do
  954. begin
  955. if operands[j].Opr.Typ = OPR_REGISTER then
  956. begin
  957. case getsubreg(operands[j].opr.reg) of
  958. R_SUBMMX: begin
  959. tx86operand(operands[i]).opsize := S_W;
  960. tx86operand(operands[i]).size := OS_M16;
  961. break;
  962. end;
  963. R_SUBMMY: begin
  964. tx86operand(operands[i]).opsize := S_L;
  965. tx86operand(operands[i]).size := OS_M32;
  966. break;
  967. end;
  968. else Message(asmr_e_unable_to_determine_reference_size);
  969. end;
  970. end;
  971. end;
  972. end;
  973. msiMemRegx16y32z64:
  974. begin
  975. for j := 1 to ops do
  976. begin
  977. if operands[j].Opr.Typ = OPR_REGISTER then
  978. begin
  979. case getsubreg(operands[j].opr.reg) of
  980. R_SUBMMX: begin
  981. tx86operand(operands[i]).opsize := S_W;
  982. tx86operand(operands[i]).size := OS_M16;
  983. break;
  984. end;
  985. R_SUBMMY: begin
  986. tx86operand(operands[i]).opsize := S_L;
  987. tx86operand(operands[i]).size := OS_M32;
  988. break;
  989. end;
  990. R_SUBMMZ: begin
  991. tx86operand(operands[i]).opsize := S_Q;
  992. tx86operand(operands[i]).size := OS_M64;
  993. break;
  994. end;
  995. else Message(asmr_e_unable_to_determine_reference_size);
  996. end;
  997. end;
  998. end;
  999. end;
  1000. msiMemRegx32y64:
  1001. begin
  1002. for j := 1 to ops do
  1003. begin
  1004. if operands[j].Opr.Typ = OPR_REGISTER then
  1005. begin
  1006. case getsubreg(operands[j].opr.reg) of
  1007. R_SUBMMX: begin
  1008. tx86operand(operands[i]).opsize := S_L;
  1009. tx86operand(operands[i]).size := OS_M32;
  1010. break;
  1011. end;
  1012. R_SUBMMY: begin
  1013. tx86operand(operands[i]).opsize := S_Q;
  1014. tx86operand(operands[i]).size := OS_M64;
  1015. break;
  1016. end;
  1017. else Message(asmr_e_unable_to_determine_reference_size);
  1018. end;
  1019. end;
  1020. end;
  1021. end;
  1022. msiMemRegx32y64z128:
  1023. for j := 1 to ops do
  1024. begin
  1025. if operands[j].Opr.Typ = OPR_REGISTER then
  1026. begin
  1027. case getsubreg(operands[j].opr.reg) of
  1028. R_SUBMMX: begin
  1029. tx86operand(operands[i]).opsize := S_L;
  1030. tx86operand(operands[i]).size := OS_M32;
  1031. break;
  1032. end;
  1033. R_SUBMMY: begin
  1034. tx86operand(operands[i]).opsize := S_Q;
  1035. tx86operand(operands[i]).size := OS_M64;
  1036. break;
  1037. end;
  1038. R_SUBMMZ: begin
  1039. tx86operand(operands[i]).opsize := S_XMM;
  1040. tx86operand(operands[i]).size := OS_M128;
  1041. break;
  1042. end;
  1043. else Message(asmr_e_unable_to_determine_reference_size);
  1044. end;
  1045. end;
  1046. end;
  1047. msiMemRegx64y128:
  1048. begin
  1049. for j := 1 to ops do
  1050. begin
  1051. if operands[j].Opr.Typ = OPR_REGISTER then
  1052. begin
  1053. case getsubreg(operands[j].opr.reg) of
  1054. R_SUBMMX: begin
  1055. tx86operand(operands[i]).opsize := S_Q;
  1056. tx86operand(operands[i]).size := OS_M64;
  1057. break;
  1058. end;
  1059. R_SUBMMY: begin
  1060. tx86operand(operands[i]).opsize := S_XMM;
  1061. tx86operand(operands[i]).size := OS_M128;
  1062. break;
  1063. end;
  1064. else Message(asmr_e_unable_to_determine_reference_size);
  1065. end;
  1066. end;
  1067. end;
  1068. end;
  1069. msiMemRegx64y128z256:
  1070. begin
  1071. for j := 1 to ops do
  1072. begin
  1073. if operands[j].Opr.Typ = OPR_REGISTER then
  1074. begin
  1075. case getsubreg(operands[j].opr.reg) of
  1076. R_SUBMMX: begin
  1077. tx86operand(operands[i]).opsize := S_Q;
  1078. tx86operand(operands[i]).size := OS_M64;
  1079. break;
  1080. end;
  1081. R_SUBMMY: begin
  1082. tx86operand(operands[i]).opsize := S_XMM;
  1083. tx86operand(operands[i]).size := OS_M128;
  1084. break;
  1085. end;
  1086. R_SUBMMZ: begin
  1087. tx86operand(operands[i]).opsize := S_YMM;
  1088. tx86operand(operands[i]).size := OS_M256;
  1089. break;
  1090. end;
  1091. else Message(asmr_e_unable_to_determine_reference_size);
  1092. end;
  1093. end;
  1094. end;
  1095. end;
  1096. msiMemRegx64y256:
  1097. begin
  1098. for j := 1 to ops do
  1099. begin
  1100. if operands[j].Opr.Typ = OPR_REGISTER then
  1101. begin
  1102. case getsubreg(operands[j].opr.reg) of
  1103. R_SUBMMX: begin
  1104. tx86operand(operands[i]).opsize := S_Q;
  1105. tx86operand(operands[i]).size := OS_M64;
  1106. break;
  1107. end;
  1108. R_SUBMMY: begin
  1109. tx86operand(operands[i]).opsize := S_YMM;
  1110. tx86operand(operands[i]).size := OS_M256;
  1111. break;
  1112. end;
  1113. else Message(asmr_e_unable_to_determine_reference_size);
  1114. end;
  1115. end;
  1116. end;
  1117. end;
  1118. msiMemRegx64y256z512:
  1119. begin
  1120. for j := 1 to ops do
  1121. begin
  1122. if operands[j].Opr.Typ = OPR_REGISTER then
  1123. begin
  1124. case getsubreg(operands[j].opr.reg) of
  1125. R_SUBMMX: begin
  1126. tx86operand(operands[i]).opsize := S_Q;
  1127. tx86operand(operands[i]).size := OS_M64;
  1128. break;
  1129. end;
  1130. R_SUBMMY: begin
  1131. tx86operand(operands[i]).opsize := S_YMM;
  1132. tx86operand(operands[i]).size := OS_M256;
  1133. break;
  1134. end;
  1135. R_SUBMMZ: begin
  1136. tx86operand(operands[i]).opsize := S_ZMM;
  1137. tx86operand(operands[i]).size := OS_M512;
  1138. break;
  1139. end;
  1140. else Message(asmr_e_unable_to_determine_reference_size);
  1141. end;
  1142. end;
  1143. end;
  1144. end;
  1145. msiMemRegConst128,
  1146. msiMemRegConst256,
  1147. msiMemRegConst512:
  1148. begin
  1149. ExistConst := false;
  1150. for j := 1 to ops do
  1151. begin
  1152. if operands[j].Opr.Typ = OPR_CONSTANT then
  1153. begin
  1154. ExistConst := true;
  1155. break;
  1156. end;
  1157. end;
  1158. if ExistConst then
  1159. begin
  1160. for j := 1 to ops do
  1161. begin
  1162. if operands[j].Opr.Typ = OPR_REGISTER then
  1163. begin
  1164. if (tx86operand(operands[j]).opsize <> S_NO) and
  1165. (tx86operand(operands[j]).size <> OS_NO) then
  1166. begin
  1167. tx86operand(operands[i]).opsize := tx86operand(operands[j]).opsize;
  1168. tx86operand(operands[i]).size := tx86operand(operands[j]).size;
  1169. break;
  1170. end
  1171. else Message(asmr_e_unable_to_determine_reference_size);
  1172. end;
  1173. end;
  1174. end
  1175. else
  1176. begin
  1177. case MemRefInfo(opcode).MemRefSize of
  1178. msiMemRegConst128: begin
  1179. tx86operand(operands[i]).opsize := S_XMM;
  1180. tx86operand(operands[i]).size := OS_M128;
  1181. break;
  1182. end;
  1183. msiMemRegConst256: begin
  1184. tx86operand(operands[i]).opsize := S_YMM;
  1185. tx86operand(operands[i]).size := OS_M256;
  1186. break;
  1187. end;
  1188. msiMemRegConst512: begin
  1189. tx86operand(operands[i]).opsize := S_ZMM;
  1190. tx86operand(operands[i]).size := OS_M512;
  1191. break;
  1192. end;
  1193. else
  1194. Internalerror(2019081018);
  1195. end;
  1196. end;
  1197. end;
  1198. msiXMem32,
  1199. msiYMem32,
  1200. msiZMem32,
  1201. msiXMem64,
  1202. msiYMem64,
  1203. msiZMem64: ; // ignore; gather/scatter opcodes haven a fixed element-size, not a fixed memory-size
  1204. // the vector-register have indices with base of the memory-address in the memory-operand
  1205. msiNoSize: ; // all memory-sizes are ok
  1206. msiNoMemRef:; // ignore;
  1207. msiVMemMultiple,
  1208. msiVMemRegSize: ; // ignore
  1209. msiUnknown,
  1210. msiUnsupported,
  1211. msiMultiple: Message(asmr_e_unable_to_determine_reference_size); // TODO individual message
  1212. else
  1213. Internalerror(2019081008);
  1214. end;
  1215. end;
  1216. OPR_CONSTANT:
  1217. case MemRefInfo(opcode).ConstSize of
  1218. csiMem8: begin
  1219. tx86operand(operands[i]).opsize := S_B;
  1220. tx86operand(operands[i]).size := OS_8;
  1221. end;
  1222. csiMem16: begin
  1223. tx86operand(operands[i]).opsize := S_W;
  1224. tx86operand(operands[i]).size := OS_16;
  1225. end;
  1226. csiMem32: begin
  1227. tx86operand(operands[i]).opsize := S_L;
  1228. tx86operand(operands[i]).size := OS_32;
  1229. end;
  1230. {$ifdef x86_64}
  1231. csiMem64: begin
  1232. tx86operand(operands[i]).opsize := S_Q;
  1233. tx86operand(operands[i]).size := OS_64;
  1234. end;
  1235. {$else}
  1236. csiMem64: begin
  1237. internalerror(2019050910);
  1238. end;
  1239. {$endif}
  1240. csiUnknown, csiMultiple, csiNoSize:
  1241. ;
  1242. end;
  1243. else
  1244. ;
  1245. end;
  1246. end;
  1247. end;
  1248. end;
  1249. for i:=1 to ops do
  1250. begin
  1251. operands[i].SetCorrectSize(opcode);
  1252. if tx86operand(operands[i]).opsize=S_NO then
  1253. begin
  1254. {$ifdef x86_64}
  1255. if (opcode=A_MOVQ) and
  1256. (ops=2) and
  1257. (operands[1].opr.typ=OPR_CONSTANT) then
  1258. opsize:=S_Q
  1259. else
  1260. {$endif x86_64}
  1261. case operands[i].Opr.Typ of
  1262. OPR_LOCAL,
  1263. OPR_REFERENCE :
  1264. begin
  1265. { for 3-operand opcodes, operand #1 (in ATT order) is always an immediate,
  1266. don't consider it. }
  1267. if i=ops then
  1268. operand2:=i-1
  1269. else
  1270. operand2:=i+1;
  1271. if operand2>0 then
  1272. begin
  1273. { Only allow register as operand to take the size from }
  1274. if operands[operand2].opr.typ=OPR_REGISTER then
  1275. begin
  1276. if ((opcode<>A_MOVD) and
  1277. (opcode<>A_CVTSI2SS)) then
  1278. begin
  1279. //tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize;
  1280. // torsten - 31.01.2012
  1281. // old: xmm/ymm-register operands have a opsize = "S_NO"
  1282. // new: xmm/ymm-register operands have a opsize = "S_XMM/S_YMM"
  1283. // any SSE- and AVX-opcodes have mixed operand sizes (e.g. cvtsd2ss xmmreg, xmmreg/m32)
  1284. // in this case is we need the old handling ("S_NO")
  1285. // =>> ignore
  1286. if (tx86operand(operands[operand2]).opsize <> S_XMM) and
  1287. (tx86operand(operands[operand2]).opsize <> S_YMM) and
  1288. (tx86operand(operands[operand2]).opsize <> S_ZMM) then
  1289. tx86operand(operands[i]).opsize:=tx86operand(operands[operand2]).opsize
  1290. else tx86operand(operands[operand2]).opsize := S_NO;
  1291. end;
  1292. end
  1293. else
  1294. begin
  1295. { if no register then take the opsize (which is available with ATT),
  1296. if not availble then give an error }
  1297. if opsize<>S_NO then
  1298. tx86operand(operands[i]).opsize:=opsize
  1299. else
  1300. begin
  1301. if (m_delphi in current_settings.modeswitches) then
  1302. Message(asmr_w_unable_to_determine_reference_size_using_dword)
  1303. else
  1304. Message(asmr_e_unable_to_determine_reference_size);
  1305. { recovery }
  1306. tx86operand(operands[i]).opsize:=S_L;
  1307. end;
  1308. end;
  1309. end
  1310. else
  1311. begin
  1312. if opsize<>S_NO then
  1313. tx86operand(operands[i]).opsize:=opsize
  1314. end;
  1315. end;
  1316. OPR_SYMBOL :
  1317. begin
  1318. { Fix lea which need a reference }
  1319. if opcode=A_LEA then
  1320. begin
  1321. s:=operands[i].opr.symbol;
  1322. so:=operands[i].opr.symofs;
  1323. operands[i].opr.typ:=OPR_REFERENCE;
  1324. Fillchar(operands[i].opr.ref,sizeof(treference),0);
  1325. operands[i].opr.ref.symbol:=s;
  1326. operands[i].opr.ref.offset:=so;
  1327. end;
  1328. {$if defined(x86_64)}
  1329. tx86operand(operands[i]).opsize:=S_Q;
  1330. {$elseif defined(i386)}
  1331. tx86operand(operands[i]).opsize:=S_L;
  1332. {$elseif defined(i8086)}
  1333. tx86operand(operands[i]).opsize:=S_W;
  1334. {$endif}
  1335. end;
  1336. else
  1337. ;
  1338. end;
  1339. end;
  1340. end;
  1341. if MemRefInfo(opcode).ExistsSSEAVX then
  1342. begin
  1343. // validate broadcast-memory-operands
  1344. vbcst := 0;
  1345. mmregs := [];
  1346. for i := 1 to ops do
  1347. if operands[i].Opr.Typ in [OPR_REFERENCE, OPR_LOCAL] then vbcst := tx86operand(operands[i]).vbcst
  1348. else if operands[i].Opr.Typ = OPR_REGISTER then
  1349. begin
  1350. if getsubreg(operands[i].opr.reg) in [R_SUBMMX, R_SUBMMY, R_SUBMMZ] then
  1351. begin
  1352. include(mmregs, getsubreg(operands[i].opr.reg));
  1353. end;
  1354. end;
  1355. if vbcst <> 0 then
  1356. begin
  1357. // found broadcast-memory-operand (e.g. "{1to8}")
  1358. // check is correct
  1359. multiplicator := 0;
  1360. if mmregs = [R_SUBMMX] then multiplicator := 1
  1361. else if mmregs = [R_SUBMMY] then multiplicator := 2
  1362. else if mmregs = [R_SUBMMZ] then multiplicator := 4
  1363. else
  1364. begin
  1365. //TG TODO
  1366. end;
  1367. if MemRefInfo(opcode).BCSTTypes <> [] then
  1368. begin
  1369. str(MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator, bcst1);
  1370. str(vbcst, bcst2);
  1371. case vbcst of
  1372. 2: if not(bt1to2 in MemRefInfo(opcode).BCSTTypes) then
  1373. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  1374. 4: if not(bt1to4 in MemRefInfo(opcode).BCSTTypes) then
  1375. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  1376. 8: if not(bt1to8 in MemRefInfo(opcode).BCSTTypes) then
  1377. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  1378. 16: if not(bt1to16 in MemRefInfo(opcode).BCSTTypes) then
  1379. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  1380. end;
  1381. end
  1382. else if MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator <> vbcst then
  1383. begin
  1384. str(MemRefInfo(opcode).BCSTXMMMultiplicator * multiplicator, bcst1);
  1385. str(vbcst, bcst2);
  1386. Message2(asmr_e_mismatch_broadcasting_elements, '1to' + bcst1, '1to' + bcst2);
  1387. end;
  1388. end;
  1389. end;
  1390. end;
  1391. procedure Tx86Instruction.SetInstructionOpsize;
  1392. function CheckSSEAVX: Boolean;
  1393. var
  1394. i: integer;
  1395. iSizeMask: int64;
  1396. bBroadcastMemRef: boolean;
  1397. bExistMemRef: boolean;
  1398. ValidOpSizes: Set of topsize;
  1399. begin
  1400. Result := False;
  1401. with MemRefInfo(opcode) do
  1402. begin
  1403. if (ExistsSSEAVX) then
  1404. begin
  1405. bBroadcastMemRef := false;
  1406. for i := 1 to ops do
  1407. bBroadcastMemRef := bBroadcastMemRef or ((tx86operand(operands[i]).vopext and OTVE_VECTOR_BCST) = OTVE_VECTOR_BCST);
  1408. if bBroadcastMemRef then
  1409. begin
  1410. opsize := S_NO;
  1411. result := true;
  1412. end
  1413. else
  1414. begin
  1415. if (gas_needsuffix[opcode] = AttSufMMS) and (ops > 0) then
  1416. begin
  1417. // special handling = use source operand for calculate instructions-opsize
  1418. // e.g. vcvtsi2sd, vcvtsi2ss, vcvtusi2sd, vcvtusi2ss,
  1419. // vfpclass..
  1420. if (ops > 2) and
  1421. (tx86operand(operands[1]).opr.typ = OPR_CONSTANT) then
  1422. opsize:=tx86operand(operands[2]).opsize
  1423. else opsize:=tx86operand(operands[1]).opsize;
  1424. if (MemRefSize in [msiMultipleMinSize128, msiMultipleMinSize256, msiMultipleMinSize512]) and
  1425. (not(opsize in [S_XMM, S_YMM, S_ZMM])) then
  1426. begin
  1427. // special handling for external gas assembler, special opcodes (e.g. vfpclassps/pd)
  1428. case MemRefSize of
  1429. msiMultipleMinSize128: opsize := S_XMM;
  1430. msiMultipleMinSize256: opsize := S_YMM;
  1431. msiMultipleMinSize512: opsize := S_ZMM;
  1432. else;
  1433. end;
  1434. end;
  1435. result := true;
  1436. end
  1437. else if MemRefSize in MemRefMultiples - [msiVMemMultiple] then
  1438. begin
  1439. case ops of
  1440. 2: begin
  1441. opsize:=tx86operand(operands[1]).opsize;
  1442. result := true;
  1443. end;
  1444. 3,4:
  1445. begin
  1446. if (tx86operand(operands[1]).opr.typ <> OPR_CONSTANT) then
  1447. opsize:=tx86operand(operands[1]).opsize
  1448. else opsize:=tx86operand(operands[2]).opsize;
  1449. result := true;
  1450. end;
  1451. end;
  1452. if (result) and
  1453. (ops > 0) and
  1454. (MemRefSize in [msiMultipleMinSize128, msiMultipleMinSize256, msiMultipleMinSize512]) and
  1455. (gas_needsuffix[opcode] in [AttSufMMS, AttSufMMX]) then
  1456. begin
  1457. // external gas assembler need suffix (different opsizes possible)
  1458. // - in fpc not exists datatypes for vector-variables
  1459. // =>> all memsize = ok, but any special opcodes (marked with attSufMMS,attSUFMMX) needed in any combination of operandtypes the exact opsize
  1460. // =>> check instructions-opsize and use the correct vector-mem-opsize
  1461. for i := 1 to ops do
  1462. if tx86operand(operands[i]).opr.typ in [OPR_REGISTER] then
  1463. begin
  1464. ValidOpSizes := [];
  1465. case tx86operand(operands[i]).opsize of
  1466. S_XMM: iSizeMask := RegXMMSizeMask;
  1467. S_YMM: iSizeMask := RegYMMSizeMask;
  1468. S_ZMM: iSizeMask := RegZMMSizeMask;
  1469. else iSizeMask := 0;
  1470. end;
  1471. if iSizemask and OT_BITS128 = OT_BITS128 then include(ValidOpSizes, S_XMM);
  1472. if iSizemask and OT_BITS256 = OT_BITS256 then include(ValidOpSizes, S_YMM);
  1473. if iSizemask and OT_BITS512 = OT_BITS512 then include(ValidOpSizes, S_ZMM);
  1474. if (ValidOpsizes <> []) then
  1475. begin
  1476. if not(opsize in ValidOpSizes) then
  1477. begin
  1478. // instructions-opsize is invalid =>> use smallest valid opsize
  1479. if iSizemask and OT_BITS128 = OT_BITS128 then opsize := S_XMM
  1480. else if iSizemask and OT_BITS256 = OT_BITS256 then opsize := S_YMM
  1481. else if iSizemask and OT_BITS512 = OT_BITS512 then opsize := S_ZMM;
  1482. end;
  1483. end
  1484. else ; // empty ValidOpsize =>> nothing todo???
  1485. break;
  1486. end;
  1487. end;
  1488. end
  1489. else if
  1490. (gas_needsuffix[opcode] = AttSufNone) and
  1491. (not(MemRefSize in [msiMemRegSize])) then
  1492. begin
  1493. // external gnu-assembler: no suffix =>> use instructions.opsize to define memory-reference size
  1494. // Tx86Instruction: local variable: operand.opsize
  1495. for i := 1 to ops do
  1496. if tx86operand(operands[i]).opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1497. begin
  1498. opsize := tx86operand(operands[i]).opsize;
  1499. result := true;
  1500. break;
  1501. end;
  1502. end;
  1503. end;
  1504. end;
  1505. end;
  1506. end;
  1507. begin
  1508. if opsize<>S_NO then
  1509. exit;
  1510. case ops of
  1511. 0 : ;
  1512. 1 :
  1513. begin
  1514. { "push es" must be stored as a long PM }
  1515. if ((opcode=A_PUSH) or
  1516. (opcode=A_POP)) and
  1517. (operands[1].opr.typ=OPR_REGISTER) and
  1518. is_segment_reg(operands[1].opr.reg) then
  1519. {$ifdef i8086}
  1520. opsize:=S_W
  1521. {$else i8086}
  1522. opsize:=S_L
  1523. {$endif i8086}
  1524. else
  1525. opsize:=tx86operand(operands[1]).opsize;
  1526. end;
  1527. 2 : begin
  1528. case opcode of
  1529. A_MOVZX,A_MOVSX :
  1530. begin
  1531. if tx86operand(operands[1]).opsize=S_NO then
  1532. begin
  1533. tx86operand(operands[1]).opsize:=S_B;
  1534. if (m_delphi in current_settings.modeswitches) then
  1535. Message(asmr_w_unable_to_determine_reference_size_using_byte)
  1536. else
  1537. Message(asmr_e_unable_to_determine_reference_size);
  1538. end;
  1539. case tx86operand(operands[1]).opsize of
  1540. S_W :
  1541. case tx86operand(operands[2]).opsize of
  1542. S_L :
  1543. opsize:=S_WL;
  1544. {$ifdef x86_64}
  1545. S_Q :
  1546. opsize:=S_WQ;
  1547. {$endif}
  1548. else
  1549. ;
  1550. end;
  1551. S_B :
  1552. begin
  1553. case tx86operand(operands[2]).opsize of
  1554. S_W :
  1555. opsize:=S_BW;
  1556. S_L :
  1557. opsize:=S_BL;
  1558. {$ifdef x86_64}
  1559. S_Q :
  1560. opsize:=S_BQ;
  1561. {$endif}
  1562. else
  1563. ;
  1564. end;
  1565. end;
  1566. else
  1567. ;
  1568. end;
  1569. end;
  1570. A_MOVD : { movd is a move from a mmx register to a
  1571. 32 bit register or memory, so no opsize is correct here PM }
  1572. exit;
  1573. A_MOVQ :
  1574. opsize:=S_IQ;
  1575. A_OUT :
  1576. opsize:=tx86operand(operands[1]).opsize;
  1577. else
  1578. if not CheckSSEAVX then
  1579. opsize:=tx86operand(operands[2]).opsize;
  1580. end;
  1581. end;
  1582. 3,4 : if not CheckSSEAVX then
  1583. opsize:=tx86operand(operands[ops]).opsize;
  1584. end;
  1585. end;
  1586. procedure Tx86Instruction.CheckOperandSizes;
  1587. var
  1588. sizeerr : boolean;
  1589. i : longint;
  1590. begin
  1591. { Check only the most common opcodes here, the others are done in
  1592. the assembler pass }
  1593. case opcode of
  1594. A_PUSH,A_POP,A_DEC,A_INC,A_NOT,A_NEG,
  1595. A_CMP,A_MOV,
  1596. A_ADD,A_SUB,A_ADC,A_SBB,
  1597. A_AND,A_OR,A_TEST,A_XOR: ;
  1598. else
  1599. exit;
  1600. end;
  1601. { Handle the BW,BL,WL separatly }
  1602. sizeerr:=false;
  1603. { special push/pop selector case }
  1604. if ((opcode=A_PUSH) or
  1605. (opcode=A_POP)) and
  1606. (operands[1].opr.typ=OPR_REGISTER) and
  1607. is_segment_reg(operands[1].opr.reg) then
  1608. exit;
  1609. if opsize in [S_BW,S_BL,S_WL] then
  1610. begin
  1611. if ops<>2 then
  1612. sizeerr:=true
  1613. else
  1614. begin
  1615. case opsize of
  1616. S_BW :
  1617. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_W);
  1618. S_BL :
  1619. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_L);
  1620. S_WL :
  1621. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_L);
  1622. {$ifdef x86_64}
  1623. S_BQ:
  1624. sizeerr:=(tx86operand(operands[1]).opsize<>S_B) or (tx86operand(operands[2]).opsize<>S_Q);
  1625. S_WQ:
  1626. sizeerr:=(tx86operand(operands[1]).opsize<>S_W) or (tx86operand(operands[2]).opsize<>S_Q);
  1627. S_LQ:
  1628. sizeerr:=(tx86operand(operands[1]).opsize<>S_L) or (tx86operand(operands[2]).opsize<>S_Q);
  1629. {$endif}
  1630. else
  1631. ;
  1632. end;
  1633. end;
  1634. end
  1635. else
  1636. begin
  1637. for i:=1 to ops do
  1638. begin
  1639. if (operands[i].opr.typ<>OPR_CONSTANT) and
  1640. (tx86operand(operands[i]).opsize in [S_B,S_W,S_L]) and
  1641. (tx86operand(operands[i]).opsize<>opsize) then
  1642. sizeerr:=true;
  1643. end;
  1644. end;
  1645. if sizeerr then
  1646. begin
  1647. { if range checks are on then generate an error }
  1648. if (cs_compilesystem in current_settings.moduleswitches) or
  1649. not (cs_check_range in current_settings.localswitches) then
  1650. Message(asmr_w_size_suffix_and_dest_dont_match)
  1651. else
  1652. Message(asmr_e_size_suffix_and_dest_dont_match);
  1653. end;
  1654. end;
  1655. { This check must be done with the operand in ATT order
  1656. i.e.after swapping in the intel reader
  1657. but before swapping in the NASM and TASM writers PM }
  1658. procedure Tx86Instruction.CheckNonCommutativeOpcodes;
  1659. begin
  1660. if (
  1661. (ops=2) and
  1662. (operands[1].opr.typ=OPR_REGISTER) and
  1663. (operands[2].opr.typ=OPR_REGISTER) and
  1664. { if the first is ST and the second is also a register
  1665. it is necessarily ST1 .. ST7 }
  1666. ((operands[1].opr.reg=NR_ST) or
  1667. (operands[1].opr.reg=NR_ST0))
  1668. ) or
  1669. (ops=0) then
  1670. if opcode=A_FSUBR then
  1671. opcode:=A_FSUB
  1672. else if opcode=A_FSUB then
  1673. opcode:=A_FSUBR
  1674. else if opcode=A_FDIVR then
  1675. opcode:=A_FDIV
  1676. else if opcode=A_FDIV then
  1677. opcode:=A_FDIVR
  1678. else if opcode=A_FSUBRP then
  1679. opcode:=A_FSUBP
  1680. else if opcode=A_FSUBP then
  1681. opcode:=A_FSUBRP
  1682. else if opcode=A_FDIVRP then
  1683. opcode:=A_FDIVP
  1684. else if opcode=A_FDIVP then
  1685. opcode:=A_FDIVRP;
  1686. if (
  1687. (ops=1) and
  1688. (operands[1].opr.typ=OPR_REGISTER) and
  1689. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1690. (operands[1].opr.reg<>NR_ST) and
  1691. (operands[1].opr.reg<>NR_ST0)
  1692. ) then
  1693. if opcode=A_FSUBRP then
  1694. opcode:=A_FSUBP
  1695. else if opcode=A_FSUBP then
  1696. opcode:=A_FSUBRP
  1697. else if opcode=A_FDIVRP then
  1698. opcode:=A_FDIVP
  1699. else if opcode=A_FDIVP then
  1700. opcode:=A_FDIVRP;
  1701. end;
  1702. procedure Tx86Instruction.FixupOpcode;
  1703. begin
  1704. { does nothing by default }
  1705. end;
  1706. {*****************************************************************************
  1707. opcode Adding
  1708. *****************************************************************************}
  1709. function Tx86Instruction.ConcatInstruction(p : TAsmList) : tai;
  1710. var
  1711. siz : topsize;
  1712. i : longint;
  1713. asize : int64;
  1714. ai : taicpu;
  1715. begin
  1716. ConcatInstruction:=nil;
  1717. ai:=nil;
  1718. for i:=1 to Ops do
  1719. if not operands[i].CheckOperand(self) then
  1720. exit;
  1721. { Get Opsize }
  1722. if (opsize<>S_NO) or (Ops=0) then
  1723. siz:=opsize
  1724. else
  1725. begin
  1726. if (Ops=2) and (operands[1].opr.typ=OPR_REGISTER) then
  1727. siz:=tx86operand(operands[1]).opsize
  1728. else
  1729. siz:=tx86operand(operands[Ops]).opsize;
  1730. { MOVD should be of size S_LQ or S_QL, but these do not exist PM }
  1731. if (ops=2) and
  1732. (tx86operand(operands[1]).opsize<>S_NO) and
  1733. (tx86operand(operands[2]).opsize<>S_NO) and
  1734. (tx86operand(operands[1]).opsize<>tx86operand(operands[2]).opsize) then
  1735. siz:=S_NO;
  1736. end;
  1737. if ((opcode=A_MOVD)or
  1738. (opcode=A_CVTSI2SS)) and
  1739. ((tx86operand(operands[1]).opsize=S_NO) or
  1740. (tx86operand(operands[2]).opsize=S_NO)) then
  1741. siz:=S_NO;
  1742. { NASM does not support FADD without args
  1743. as alias of FADDP
  1744. and GNU AS interprets FADD without operand differently
  1745. for version 2.9.1 and 2.9.5 !! }
  1746. if (ops=0) and
  1747. ((opcode=A_FADD) or
  1748. (opcode=A_FMUL) or
  1749. (opcode=A_FSUB) or
  1750. (opcode=A_FSUBR) or
  1751. (opcode=A_FDIV) or
  1752. (opcode=A_FDIVR)) then
  1753. begin
  1754. if opcode=A_FADD then
  1755. opcode:=A_FADDP
  1756. else if opcode=A_FMUL then
  1757. opcode:=A_FMULP
  1758. else if opcode=A_FSUB then
  1759. opcode:=A_FSUBP
  1760. else if opcode=A_FSUBR then
  1761. opcode:=A_FSUBRP
  1762. else if opcode=A_FDIV then
  1763. opcode:=A_FDIVP
  1764. else if opcode=A_FDIVR then
  1765. opcode:=A_FDIVRP;
  1766. message1(asmr_w_fadd_to_faddp,std_op2str[opcode]);
  1767. end;
  1768. {It is valid to specify some instructions without operand size.}
  1769. if siz=S_NO then
  1770. begin
  1771. if (ops=1) and (opcode=A_INT) then
  1772. siz:=S_B;
  1773. if (ops=1) and (opcode=A_XABORT) then
  1774. siz:=S_B;
  1775. {$ifdef i8086}
  1776. if (ops=1) and (opcode=A_BRKEM) then
  1777. siz:=S_B;
  1778. {$endif i8086}
  1779. if (ops=1) and (opcode=A_RET) or (opcode=A_RETN) or (opcode=A_RETF) or
  1780. (opcode=A_RETW) or (opcode=A_RETNW) or (opcode=A_RETFW) or
  1781. {$ifndef x86_64}
  1782. (opcode=A_RETD) or (opcode=A_RETND) or
  1783. {$endif x86_64}
  1784. (opcode=A_RETFD)
  1785. {$ifdef x86_64}
  1786. or (opcode=A_RETQ) or (opcode=A_RETNQ) or (opcode=A_RETFQ)
  1787. {$endif x86_64}
  1788. then
  1789. siz:=S_W;
  1790. if (ops=1) and (opcode=A_PUSH) then
  1791. begin
  1792. {$ifdef i8086}
  1793. if (tx86operand(operands[1]).opr.val>=-128) and (tx86operand(operands[1]).opr.val<=127) then
  1794. begin
  1795. siz:=S_B;
  1796. message(asmr_w_unable_to_determine_constant_size_using_byte);
  1797. end
  1798. else
  1799. begin
  1800. siz:=S_W;
  1801. message(asmr_w_unable_to_determine_constant_size_using_word);
  1802. end;
  1803. {$else i8086}
  1804. { We are a 32 compiler, assume 32-bit by default. This is Delphi
  1805. compatible but bad coding practise.}
  1806. siz:=S_L;
  1807. message(asmr_w_unable_to_determine_reference_size_using_dword);
  1808. {$endif i8086}
  1809. end;
  1810. if (opcode=A_JMP) or (opcode=A_JCC) or (opcode=A_CALL) then
  1811. if ops=1 then
  1812. siz:=S_NEAR
  1813. else
  1814. siz:=S_FAR;
  1815. end;
  1816. { GNU AS interprets FDIV without operand differently
  1817. for version 2.9.1 and 2.10
  1818. we add explicit args to it !! }
  1819. if (ops=0) and
  1820. ((opcode=A_FSUBP) or
  1821. (opcode=A_FSUBRP) or
  1822. (opcode=A_FDIVP) or
  1823. (opcode=A_FDIVRP) or
  1824. (opcode=A_FSUB) or
  1825. (opcode=A_FSUBR) or
  1826. (opcode=A_FADD) or
  1827. (opcode=A_FADDP) or
  1828. (opcode=A_FDIV) or
  1829. (opcode=A_FDIVR)) then
  1830. begin
  1831. message1(asmr_w_adding_explicit_args_fXX,std_op2str[opcode]);
  1832. ops:=2;
  1833. operands[1].opr.typ:=OPR_REGISTER;
  1834. operands[2].opr.typ:=OPR_REGISTER;
  1835. operands[1].opr.reg:=NR_ST0;
  1836. operands[2].opr.reg:=NR_ST1;
  1837. end;
  1838. if (ops=1) and
  1839. (
  1840. (operands[1].opr.typ=OPR_REGISTER) and
  1841. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1842. (operands[1].opr.reg<>NR_ST) and
  1843. (operands[1].opr.reg<>NR_ST0)
  1844. ) and
  1845. (
  1846. (opcode=A_FSUBP) or
  1847. (opcode=A_FSUBRP) or
  1848. (opcode=A_FDIVP) or
  1849. (opcode=A_FDIVRP) or
  1850. (opcode=A_FADDP) or
  1851. (opcode=A_FMULP)
  1852. ) then
  1853. begin
  1854. message1(asmr_w_adding_explicit_first_arg_fXX,std_op2str[opcode]);
  1855. ops:=2;
  1856. operands[2].opr.typ:=OPR_REGISTER;
  1857. operands[2].opr.reg:=operands[1].opr.reg;
  1858. operands[1].opr.reg:=NR_ST0;
  1859. end;
  1860. if (ops=1) and
  1861. (
  1862. (operands[1].opr.typ=OPR_REGISTER) and
  1863. (getregtype(operands[1].opr.reg)=R_FPUREGISTER) and
  1864. (operands[1].opr.reg<>NR_ST) and
  1865. (operands[1].opr.reg<>NR_ST0)
  1866. ) and
  1867. (
  1868. (opcode=A_FSUB) or
  1869. (opcode=A_FSUBR) or
  1870. (opcode=A_FDIV) or
  1871. (opcode=A_FDIVR) or
  1872. (opcode=A_FADD) or
  1873. (opcode=A_FMUL)
  1874. ) then
  1875. begin
  1876. message1(asmr_w_adding_explicit_second_arg_fXX,std_op2str[opcode]);
  1877. ops:=2;
  1878. operands[2].opr.typ:=OPR_REGISTER;
  1879. operands[2].opr.reg:=NR_ST0;
  1880. end;
  1881. { Check for 'POP CS' }
  1882. if (opcode=A_POP) and (ops=1) and (operands[1].opr.typ=OPR_REGISTER) and
  1883. (operands[1].opr.reg=NR_CS) then
  1884. {$ifdef i8086}
  1885. { On i8086 we print only a warning, because 'POP CS' works on 8086 and 8088
  1886. CPUs, but isn't supported on any later CPU }
  1887. Message(asmr_w_pop_cs_not_portable);
  1888. {$else i8086}
  1889. { On the i386 and x86_64 targets, we print out an error, because no CPU,
  1890. supported by these targets support 'POP CS' }
  1891. Message(asmr_e_pop_cs_not_valid);
  1892. {$endif i8086}
  1893. { I tried to convince Linus Torvalds to add
  1894. code to support ENTER instruction
  1895. (when raising a stack page fault)
  1896. but he replied that ENTER is a bad instruction and
  1897. Linux does not need to support it
  1898. So I think its at least a good idea to add a warning
  1899. if someone uses this in assembler code
  1900. FPC itself does not use it at all PM }
  1901. if (opcode=A_ENTER) and
  1902. (target_info.system in [system_i386_linux,system_i386_FreeBSD,system_i386_android]) then
  1903. Message(asmr_w_enter_not_supported_by_linux);
  1904. ai:=taicpu.op_none(opcode,siz);
  1905. ai.fileinfo:=filepos;
  1906. ai.SetOperandOrder(op_att);
  1907. ai.Ops:=Ops;
  1908. ai.Allocate_oper(Ops);
  1909. for i:=1 to Ops do
  1910. begin
  1911. ai.oper[i-1]^.vopext := (operands[i] as tx86operand).vopext;
  1912. case operands[i].opr.typ of
  1913. OPR_CONSTANT :
  1914. ai.loadconst(i-1,operands[i].opr.val);
  1915. OPR_REGISTER:
  1916. ai.loadreg(i-1,operands[i].opr.reg);
  1917. OPR_SYMBOL:
  1918. {$ifdef i8086}
  1919. if operands[i].opr.symseg then
  1920. taicpu(ai).loadsegsymbol(i-1,operands[i].opr.symbol)
  1921. else
  1922. {$endif i8086}
  1923. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1924. OPR_LOCAL :
  1925. with operands[i].opr do
  1926. begin
  1927. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1928. localscale,localgetoffset,localforceref);
  1929. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1930. // check for embedded broadcast
  1931. if MemRefInfo(opcode).ExistsSSEAVX then
  1932. begin
  1933. asize := 0;
  1934. if ((operands[i] as tx86operand).vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST) and
  1935. (MemRefInfo(opcode).MemRefSizeBCST in [msbBCST32,msbBCST64]) then
  1936. begin
  1937. case operands[i].size of
  1938. OS_32,OS_M32: asize:=OT_BITS32;
  1939. OS_64,OS_M64: asize:=OT_BITS64;
  1940. else;
  1941. end;
  1942. end;
  1943. if asize<>0 then
  1944. //ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and not OT_SIZE_MASK) or asize;
  1945. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and OT_NON_SIZE) or asize;
  1946. end;
  1947. end;
  1948. OPR_REFERENCE:
  1949. begin
  1950. if current_settings.optimizerswitches <> [] then
  1951. if (not(MemRefInfo(opcode).MemRefSize in MemRefSizeInfoVMems)) and (opcode<>A_XLAT) and not is_x86_string_op(opcode) then
  1952. optimize_ref(operands[i].opr.ref,true);
  1953. ai.loadref(i-1,operands[i].opr.ref);
  1954. if operands[i].size<>OS_NO then
  1955. begin
  1956. asize:=0;
  1957. case operands[i].size of
  1958. OS_8,OS_S8 :
  1959. asize:=OT_BITS8;
  1960. OS_16,OS_S16, OS_M16:
  1961. asize:=OT_BITS16;
  1962. OS_32,OS_S32 :
  1963. {$ifdef i8086}
  1964. if siz=S_FAR then
  1965. asize:=OT_FAR
  1966. else
  1967. asize:=OT_BITS32;
  1968. {$else i8086}
  1969. asize:=OT_BITS32;
  1970. {$endif i8086}
  1971. OS_F32,OS_M32 :
  1972. asize:=OT_BITS32;
  1973. OS_64,OS_S64:
  1974. begin
  1975. { Only FPU and SSE/AVX operations know about 64bit
  1976. values, for all integer operations it is seen as 32bit
  1977. this applies only to i386, see tw16622}
  1978. if (gas_needsuffix[opcode] in [attsufFPU,attsufFPUint]) or (MemRefInfo(opcode).ExistsSSEAVX) then
  1979. asize:=OT_BITS64
  1980. {$ifdef i386}
  1981. else
  1982. asize:=OT_BITS32
  1983. {$endif i386}
  1984. ;
  1985. end;
  1986. OS_F64,OS_C64, OS_M64 :
  1987. asize:=OT_BITS64;
  1988. OS_F80 :
  1989. asize:=OT_BITS80;
  1990. OS_128,OS_M128:
  1991. asize := OT_BITS128;
  1992. OS_M256:
  1993. asize := OT_BITS256;
  1994. OS_M512:
  1995. asize := OT_BITS512;
  1996. else
  1997. ;
  1998. end;
  1999. if asize<>0 then
  2000. ai.oper[i-1]^.ot:=(ai.oper[i-1]^.ot and OT_NON_SIZE) or asize;
  2001. end;
  2002. end;
  2003. else
  2004. ;
  2005. end;
  2006. end;
  2007. { Condition ? }
  2008. if condition<>C_None then
  2009. ai.SetCondition(condition);
  2010. { Set is_jmp, it enables asmwriter to emit short jumps if appropriate }
  2011. if (opcode=A_JMP) or (opcode=A_JCC) then
  2012. ai.is_jmp := True;
  2013. { Concat the opcode or give an error }
  2014. if assigned(ai) then
  2015. p.concat(ai)
  2016. else
  2017. Message(asmr_e_invalid_opcode_and_operand);
  2018. result:=ai;
  2019. end;
  2020. function Tx86Instruction.getstring(aAddMemRefSize: boolean): string;
  2021. var
  2022. i : longint;
  2023. s, sval : string;
  2024. regnr: string;
  2025. addsize : boolean;
  2026. begin
  2027. s:='['+std_op2str[opcode];
  2028. for i:=1 to ops do
  2029. begin
  2030. with operands[i] as Tx86Operand do
  2031. begin
  2032. if i=1 then
  2033. s:=s+' '
  2034. else
  2035. s:=s+',';
  2036. { type }
  2037. addsize:=false;
  2038. case operands[i].opr.typ of
  2039. OPR_CONSTANT : begin
  2040. str(operands[i].opr.val, sval);
  2041. s:=s+ sval;
  2042. end;
  2043. OPR_REGISTER : begin
  2044. regnr := '';
  2045. str(getsupreg(opr.reg),regnr);
  2046. if getsubreg(opr.reg)= R_SUBMMX then
  2047. s:=s+'xmmreg' + regnr
  2048. else
  2049. if getsubreg(opr.reg)= R_SUBMMY then
  2050. s:=s+'ymmreg' + regnr
  2051. else
  2052. if getsubreg(opr.reg)= R_SUBMMZ then
  2053. s:=s+'zmmreg' + regnr
  2054. else
  2055. if getregtype(opr.reg)= R_MMXREGISTER then
  2056. s:=s+'mmxreg'
  2057. else
  2058. if getregtype(opr.reg)= R_FPUREGISTER then
  2059. s:=s+'fpureg'
  2060. else
  2061. if getregtype(opr.reg)=R_INTREGISTER then
  2062. begin
  2063. s:=s+'reg';
  2064. addsize:=true;
  2065. end
  2066. else
  2067. if getregtype(opr.reg)=R_ADDRESSREGISTER then
  2068. begin
  2069. s:=s+'k' + regnr;
  2070. end;
  2071. end;
  2072. OPR_LOCAL,
  2073. OPR_REFERENCE: begin
  2074. s:=s + 'mem';
  2075. if aAddMemRefSize then
  2076. addsize:=true;
  2077. end;
  2078. else s:=s + '???';
  2079. end;
  2080. if addsize then
  2081. begin
  2082. sval := '';
  2083. str(tcgsize2size[size], sval);
  2084. s := s + sval;
  2085. end;
  2086. if vopext <> 0 then
  2087. begin
  2088. str(vopext and $07, regnr);
  2089. if vopext and OTVE_VECTOR_WRITEMASK = OTVE_VECTOR_WRITEMASK then
  2090. s := s + ' {k' + regnr + '}';
  2091. if vopext and OTVE_VECTOR_ZERO = OTVE_VECTOR_ZERO then
  2092. s := s + ' {z}';
  2093. if vopext and OTVE_VECTOR_SAE = OTVE_VECTOR_SAE then
  2094. s := s + ' {sae}';
  2095. if vopext and OTVE_VECTOR_BCST = OTVE_VECTOR_BCST then
  2096. case vopext and OTVE_VECTOR_BCST_MASK of
  2097. OTVE_VECTOR_BCST2: s := s + ' {1to2}';
  2098. OTVE_VECTOR_BCST4: s := s + ' {1to4}';
  2099. OTVE_VECTOR_BCST8: s := s + ' {1to8}';
  2100. OTVE_VECTOR_BCST16: s := s + ' {1to16}';
  2101. end;
  2102. if vopext and OTVE_VECTOR_ER = OTVE_VECTOR_ER then
  2103. case vopext and OTVE_VECTOR_ER_MASK of
  2104. OTVE_VECTOR_RNSAE: s := s + ' {rn-sae}';
  2105. OTVE_VECTOR_RDSAE: s := s + ' {rd-sae}';
  2106. OTVE_VECTOR_RUSAE: s := s + ' {ru-sae}';
  2107. OTVE_VECTOR_RZSAE: s := s + ' {rz-sae}';
  2108. end;
  2109. end;
  2110. end;
  2111. end;
  2112. GetString:=s+']';
  2113. end;
  2114. function Tx86Instruction.MightHaveExtension: boolean;
  2115. begin
  2116. Result:=aasmcpu.MightHaveExtension(opcode);
  2117. end;
  2118. end.