rax86.pas 83 KB

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