rax86.pas 83 KB

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