rax86.pas 84 KB

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