rax86.pas 83 KB

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