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