1 //===- X86DisassemblerTables.cpp - Disassembler tables ----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is part of the X86 Disassembler Emitter. 10 // It contains the implementation of the disassembler tables. 11 // Documentation for the disassembler emitter in general can be found in 12 // X86DisassemblerEmitter.h. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "X86DisassemblerTables.h" 17 #include "X86DisassemblerShared.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/Format.h" 21 #include <map> 22 23 using namespace llvm; 24 using namespace X86Disassembler; 25 26 /// stringForContext - Returns a string containing the name of a particular 27 /// InstructionContext, usually for diagnostic purposes. 28 /// 29 /// @param insnContext - The instruction class to transform to a string. 30 /// @return - A statically-allocated string constant that contains the 31 /// name of the instruction class. 32 static inline const char* stringForContext(InstructionContext insnContext) { 33 switch (insnContext) { 34 default: 35 llvm_unreachable("Unhandled instruction class"); 36 #define ENUM_ENTRY(n, r, d) case n: return #n; break; 37 #define ENUM_ENTRY_K_B(n, r, d) ENUM_ENTRY(n, r, d) ENUM_ENTRY(n##_K_B, r, d)\ 38 ENUM_ENTRY(n##_KZ, r, d) ENUM_ENTRY(n##_K, r, d) ENUM_ENTRY(n##_B, r, d)\ 39 ENUM_ENTRY(n##_KZ_B, r, d) 40 INSTRUCTION_CONTEXTS 41 #undef ENUM_ENTRY 42 #undef ENUM_ENTRY_K_B 43 } 44 } 45 46 /// stringForOperandType - Like stringForContext, but for OperandTypes. 47 static inline const char* stringForOperandType(OperandType type) { 48 switch (type) { 49 default: 50 llvm_unreachable("Unhandled type"); 51 #define ENUM_ENTRY(i, d) case i: return #i; 52 TYPES 53 #undef ENUM_ENTRY 54 } 55 } 56 57 /// stringForOperandEncoding - like stringForContext, but for 58 /// OperandEncodings. 59 static inline const char* stringForOperandEncoding(OperandEncoding encoding) { 60 switch (encoding) { 61 default: 62 llvm_unreachable("Unhandled encoding"); 63 #define ENUM_ENTRY(i, d) case i: return #i; 64 ENCODINGS 65 #undef ENUM_ENTRY 66 } 67 } 68 69 /// inheritsFrom - Indicates whether all instructions in one class also belong 70 /// to another class. 71 /// 72 /// @param child - The class that may be the subset 73 /// @param parent - The class that may be the superset 74 /// @return - True if child is a subset of parent, false otherwise. 75 static inline bool inheritsFrom(InstructionContext child, 76 InstructionContext parent, bool noPrefix = true, 77 bool VEX_LIG = false, bool VEX_WIG = false, 78 bool AdSize64 = false) { 79 if (child == parent) 80 return true; 81 82 switch (parent) { 83 case IC: 84 return(inheritsFrom(child, IC_64BIT, AdSize64) || 85 (noPrefix && inheritsFrom(child, IC_OPSIZE, noPrefix)) || 86 inheritsFrom(child, IC_ADSIZE) || 87 (noPrefix && inheritsFrom(child, IC_XD, noPrefix)) || 88 (noPrefix && inheritsFrom(child, IC_XS, noPrefix))); 89 case IC_64BIT: 90 return(inheritsFrom(child, IC_64BIT_REXW) || 91 (noPrefix && inheritsFrom(child, IC_64BIT_OPSIZE, noPrefix)) || 92 (!AdSize64 && inheritsFrom(child, IC_64BIT_ADSIZE)) || 93 (noPrefix && inheritsFrom(child, IC_64BIT_XD, noPrefix)) || 94 (noPrefix && inheritsFrom(child, IC_64BIT_XS, noPrefix))); 95 case IC_OPSIZE: 96 return inheritsFrom(child, IC_64BIT_OPSIZE) || 97 inheritsFrom(child, IC_OPSIZE_ADSIZE); 98 case IC_ADSIZE: 99 return (noPrefix && inheritsFrom(child, IC_OPSIZE_ADSIZE, noPrefix)); 100 case IC_OPSIZE_ADSIZE: 101 return false; 102 case IC_64BIT_ADSIZE: 103 return (noPrefix && inheritsFrom(child, IC_64BIT_OPSIZE_ADSIZE, noPrefix)); 104 case IC_64BIT_OPSIZE_ADSIZE: 105 return false; 106 case IC_XD: 107 return inheritsFrom(child, IC_64BIT_XD); 108 case IC_XS: 109 return inheritsFrom(child, IC_64BIT_XS); 110 case IC_XD_OPSIZE: 111 return inheritsFrom(child, IC_64BIT_XD_OPSIZE); 112 case IC_XS_OPSIZE: 113 return inheritsFrom(child, IC_64BIT_XS_OPSIZE); 114 case IC_XD_ADSIZE: 115 return inheritsFrom(child, IC_64BIT_XD_ADSIZE); 116 case IC_XS_ADSIZE: 117 return inheritsFrom(child, IC_64BIT_XS_ADSIZE); 118 case IC_64BIT_REXW: 119 return((noPrefix && inheritsFrom(child, IC_64BIT_REXW_XS, noPrefix)) || 120 (noPrefix && inheritsFrom(child, IC_64BIT_REXW_XD, noPrefix)) || 121 (noPrefix && inheritsFrom(child, IC_64BIT_REXW_OPSIZE, noPrefix)) || 122 (!AdSize64 && inheritsFrom(child, IC_64BIT_REXW_ADSIZE))); 123 case IC_64BIT_OPSIZE: 124 return inheritsFrom(child, IC_64BIT_REXW_OPSIZE) || 125 (!AdSize64 && inheritsFrom(child, IC_64BIT_OPSIZE_ADSIZE)) || 126 (!AdSize64 && inheritsFrom(child, IC_64BIT_REXW_ADSIZE)); 127 case IC_64BIT_XD: 128 return(inheritsFrom(child, IC_64BIT_REXW_XD) || 129 (!AdSize64 && inheritsFrom(child, IC_64BIT_XD_ADSIZE))); 130 case IC_64BIT_XS: 131 return(inheritsFrom(child, IC_64BIT_REXW_XS) || 132 (!AdSize64 && inheritsFrom(child, IC_64BIT_XS_ADSIZE))); 133 case IC_64BIT_XD_OPSIZE: 134 case IC_64BIT_XS_OPSIZE: 135 return false; 136 case IC_64BIT_XD_ADSIZE: 137 case IC_64BIT_XS_ADSIZE: 138 return false; 139 case IC_64BIT_REXW_XD: 140 case IC_64BIT_REXW_XS: 141 case IC_64BIT_REXW_OPSIZE: 142 case IC_64BIT_REXW_ADSIZE: 143 return false; 144 case IC_VEX: 145 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_VEX_L_W)) || 146 (VEX_WIG && inheritsFrom(child, IC_VEX_W)) || 147 (VEX_LIG && inheritsFrom(child, IC_VEX_L)); 148 case IC_VEX_XS: 149 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_VEX_L_W_XS)) || 150 (VEX_WIG && inheritsFrom(child, IC_VEX_W_XS)) || 151 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XS)); 152 case IC_VEX_XD: 153 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_VEX_L_W_XD)) || 154 (VEX_WIG && inheritsFrom(child, IC_VEX_W_XD)) || 155 (VEX_LIG && inheritsFrom(child, IC_VEX_L_XD)); 156 case IC_VEX_OPSIZE: 157 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE)) || 158 (VEX_WIG && inheritsFrom(child, IC_VEX_W_OPSIZE)) || 159 (VEX_LIG && inheritsFrom(child, IC_VEX_L_OPSIZE)); 160 case IC_VEX_W: 161 return VEX_LIG && inheritsFrom(child, IC_VEX_L_W); 162 case IC_VEX_W_XS: 163 return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XS); 164 case IC_VEX_W_XD: 165 return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_XD); 166 case IC_VEX_W_OPSIZE: 167 return VEX_LIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE); 168 case IC_VEX_L: 169 return VEX_WIG && inheritsFrom(child, IC_VEX_L_W); 170 case IC_VEX_L_XS: 171 return VEX_WIG && inheritsFrom(child, IC_VEX_L_W_XS); 172 case IC_VEX_L_XD: 173 return VEX_WIG && inheritsFrom(child, IC_VEX_L_W_XD); 174 case IC_VEX_L_OPSIZE: 175 return VEX_WIG && inheritsFrom(child, IC_VEX_L_W_OPSIZE); 176 case IC_VEX_L_W: 177 case IC_VEX_L_W_XS: 178 case IC_VEX_L_W_XD: 179 case IC_VEX_L_W_OPSIZE: 180 return false; 181 case IC_EVEX: 182 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W)) || 183 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W)) || 184 (VEX_WIG && inheritsFrom(child, IC_EVEX_W)) || 185 (VEX_LIG && inheritsFrom(child, IC_EVEX_L)) || 186 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2)); 187 case IC_EVEX_XS: 188 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS)) || 189 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS)) || 190 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XS)) || 191 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XS)) || 192 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XS)); 193 case IC_EVEX_XD: 194 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD)) || 195 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD)) || 196 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XD)) || 197 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XD)) || 198 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XD)); 199 case IC_EVEX_OPSIZE: 200 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE)) || 201 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE)) || 202 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_OPSIZE)) || 203 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_OPSIZE)) || 204 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_OPSIZE)); 205 case IC_EVEX_K: 206 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_K)) || 207 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_K)) || 208 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_K)) || 209 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_K)) || 210 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_K)); 211 case IC_EVEX_XS_K: 212 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_K)) || 213 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_K)) || 214 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XS_K)) || 215 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XS_K)) || 216 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XS_K)); 217 case IC_EVEX_XD_K: 218 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_K)) || 219 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_K)) || 220 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XD_K)) || 221 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XD_K)) || 222 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XD_K)); 223 case IC_EVEX_OPSIZE_K: 224 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_K)) || 225 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_K)) || 226 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_OPSIZE_K)) || 227 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_OPSIZE_K)) || 228 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_OPSIZE_K)); 229 case IC_EVEX_KZ: 230 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_KZ)) || 231 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_KZ)) || 232 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_KZ)) || 233 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_KZ)) || 234 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_KZ)); 235 case IC_EVEX_XS_KZ: 236 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_KZ)) || 237 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_KZ)) || 238 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XS_KZ)) || 239 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XS_KZ)) || 240 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XS_KZ)); 241 case IC_EVEX_XD_KZ: 242 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_KZ)) || 243 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_KZ)) || 244 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XD_KZ)) || 245 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XD_KZ)) || 246 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XD_KZ)); 247 case IC_EVEX_OPSIZE_KZ: 248 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_KZ)) || 249 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_KZ)) || 250 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_OPSIZE_KZ)) || 251 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_OPSIZE_KZ)) || 252 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_OPSIZE_KZ)); 253 case IC_EVEX_W: 254 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W)) || 255 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W)); 256 case IC_EVEX_W_XS: 257 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XS)) || 258 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XS)); 259 case IC_EVEX_W_XD: 260 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XD)) || 261 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XD)); 262 case IC_EVEX_W_OPSIZE: 263 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE)) || 264 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE)); 265 case IC_EVEX_W_K: 266 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_K)) || 267 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_K)); 268 case IC_EVEX_W_XS_K: 269 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XS_K)) || 270 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XS_K)); 271 case IC_EVEX_W_XD_K: 272 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XD_K)) || 273 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XD_K)); 274 case IC_EVEX_W_OPSIZE_K: 275 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_K)) || 276 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_K)); 277 case IC_EVEX_W_KZ: 278 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_KZ)) || 279 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_KZ)); 280 case IC_EVEX_W_XS_KZ: 281 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XS_KZ)) || 282 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XS_KZ)); 283 case IC_EVEX_W_XD_KZ: 284 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XD_KZ)) || 285 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XD_KZ)); 286 case IC_EVEX_W_OPSIZE_KZ: 287 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_KZ)) || 288 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_KZ)); 289 case IC_EVEX_L: 290 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W); 291 case IC_EVEX_L_XS: 292 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS); 293 case IC_EVEX_L_XD: 294 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD); 295 case IC_EVEX_L_OPSIZE: 296 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE); 297 case IC_EVEX_L_K: 298 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_K); 299 case IC_EVEX_L_XS_K: 300 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_K); 301 case IC_EVEX_L_XD_K: 302 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_K); 303 case IC_EVEX_L_OPSIZE_K: 304 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_K); 305 case IC_EVEX_L_KZ: 306 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_KZ); 307 case IC_EVEX_L_XS_KZ: 308 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_KZ); 309 case IC_EVEX_L_XD_KZ: 310 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_KZ); 311 case IC_EVEX_L_OPSIZE_KZ: 312 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_KZ); 313 case IC_EVEX_L_W: 314 case IC_EVEX_L_W_XS: 315 case IC_EVEX_L_W_XD: 316 case IC_EVEX_L_W_OPSIZE: 317 return false; 318 case IC_EVEX_L_W_K: 319 case IC_EVEX_L_W_XS_K: 320 case IC_EVEX_L_W_XD_K: 321 case IC_EVEX_L_W_OPSIZE_K: 322 return false; 323 case IC_EVEX_L_W_KZ: 324 case IC_EVEX_L_W_XS_KZ: 325 case IC_EVEX_L_W_XD_KZ: 326 case IC_EVEX_L_W_OPSIZE_KZ: 327 return false; 328 case IC_EVEX_L2: 329 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W); 330 case IC_EVEX_L2_XS: 331 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS); 332 case IC_EVEX_L2_XD: 333 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD); 334 case IC_EVEX_L2_OPSIZE: 335 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE); 336 case IC_EVEX_L2_K: 337 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_K); 338 case IC_EVEX_L2_XS_K: 339 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_K); 340 case IC_EVEX_L2_XD_K: 341 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_K); 342 case IC_EVEX_L2_OPSIZE_K: 343 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_K); 344 case IC_EVEX_L2_KZ: 345 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_KZ); 346 case IC_EVEX_L2_XS_KZ: 347 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_KZ); 348 case IC_EVEX_L2_XD_KZ: 349 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_KZ); 350 case IC_EVEX_L2_OPSIZE_KZ: 351 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_KZ); 352 case IC_EVEX_L2_W: 353 case IC_EVEX_L2_W_XS: 354 case IC_EVEX_L2_W_XD: 355 case IC_EVEX_L2_W_OPSIZE: 356 return false; 357 case IC_EVEX_L2_W_K: 358 case IC_EVEX_L2_W_XS_K: 359 case IC_EVEX_L2_W_XD_K: 360 case IC_EVEX_L2_W_OPSIZE_K: 361 return false; 362 case IC_EVEX_L2_W_KZ: 363 case IC_EVEX_L2_W_XS_KZ: 364 case IC_EVEX_L2_W_XD_KZ: 365 case IC_EVEX_L2_W_OPSIZE_KZ: 366 return false; 367 case IC_EVEX_B: 368 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_B)) || 369 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_B)) || 370 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_B)) || 371 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_B)) || 372 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_B)); 373 case IC_EVEX_XS_B: 374 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_B)) || 375 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_B)) || 376 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XS_B)) || 377 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XS_B)) || 378 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XS_B)); 379 case IC_EVEX_XD_B: 380 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_B)) || 381 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_B)) || 382 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XD_B)) || 383 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XD_B)) || 384 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XD_B)); 385 case IC_EVEX_OPSIZE_B: 386 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_B)) || 387 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_B)) || 388 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_OPSIZE_B)) || 389 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_OPSIZE_B)) || 390 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_OPSIZE_B)); 391 case IC_EVEX_K_B: 392 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_K_B)) || 393 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_K_B)) || 394 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_K_B)) || 395 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_K_B)) || 396 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_K_B)); 397 case IC_EVEX_XS_K_B: 398 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_K_B)) || 399 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_K_B)) || 400 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XS_K_B)) || 401 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XS_K_B)) || 402 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XS_K_B)); 403 case IC_EVEX_XD_K_B: 404 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_K_B)) || 405 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_K_B)) || 406 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XD_K_B)) || 407 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XD_K_B)) || 408 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XD_K_B)); 409 case IC_EVEX_OPSIZE_K_B: 410 return (VEX_LIG && VEX_WIG && 411 inheritsFrom(child, IC_EVEX_L_W_OPSIZE_K_B)) || 412 (VEX_LIG && VEX_WIG && 413 inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_K_B)) || 414 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_OPSIZE_K_B)) || 415 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_OPSIZE_K_B)) || 416 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_OPSIZE_K_B)); 417 case IC_EVEX_KZ_B: 418 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_KZ_B)) || 419 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_KZ_B)) || 420 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_KZ_B)) || 421 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_KZ_B)) || 422 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_KZ_B)); 423 case IC_EVEX_XS_KZ_B: 424 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_KZ_B)) || 425 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_KZ_B)) || 426 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XS_KZ_B)) || 427 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XS_KZ_B)) || 428 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XS_KZ_B)); 429 case IC_EVEX_XD_KZ_B: 430 return (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_KZ_B)) || 431 (VEX_LIG && VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_KZ_B)) || 432 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_XD_KZ_B)) || 433 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_XD_KZ_B)) || 434 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_XD_KZ_B)); 435 case IC_EVEX_OPSIZE_KZ_B: 436 return (VEX_LIG && VEX_WIG && 437 inheritsFrom(child, IC_EVEX_L_W_OPSIZE_KZ_B)) || 438 (VEX_LIG && VEX_WIG && 439 inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_KZ_B)) || 440 (VEX_WIG && inheritsFrom(child, IC_EVEX_W_OPSIZE_KZ_B)) || 441 (VEX_LIG && inheritsFrom(child, IC_EVEX_L_OPSIZE_KZ_B)) || 442 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_OPSIZE_KZ_B)); 443 case IC_EVEX_W_B: 444 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_B)) || 445 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_B)); 446 case IC_EVEX_W_XS_B: 447 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XS_B)) || 448 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XS_B)); 449 case IC_EVEX_W_XD_B: 450 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XD_B)) || 451 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XD_B)); 452 case IC_EVEX_W_OPSIZE_B: 453 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_B)) || 454 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_B)); 455 case IC_EVEX_W_K_B: 456 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_K_B)) || 457 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_K_B)); 458 case IC_EVEX_W_XS_K_B: 459 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XS_K_B)) || 460 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XS_K_B)); 461 case IC_EVEX_W_XD_K_B: 462 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XD_K_B)) || 463 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XD_K_B)); 464 case IC_EVEX_W_OPSIZE_K_B: 465 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_K_B)) || 466 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_K_B)); 467 case IC_EVEX_W_KZ_B: 468 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_KZ_B)) || 469 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_KZ_B)); 470 case IC_EVEX_W_XS_KZ_B: 471 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XS_KZ_B)) || 472 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XS_KZ_B)); 473 case IC_EVEX_W_XD_KZ_B: 474 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_XD_KZ_B)) || 475 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_XD_KZ_B)); 476 case IC_EVEX_W_OPSIZE_KZ_B: 477 return (VEX_LIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_KZ_B)) || 478 (VEX_LIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_KZ_B)); 479 case IC_EVEX_L_B: 480 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_B); 481 case IC_EVEX_L_XS_B: 482 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_B); 483 case IC_EVEX_L_XD_B: 484 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_B); 485 case IC_EVEX_L_OPSIZE_B: 486 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_B); 487 case IC_EVEX_L_K_B: 488 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_K_B); 489 case IC_EVEX_L_XS_K_B: 490 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_K_B); 491 case IC_EVEX_L_XD_K_B: 492 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_K_B); 493 case IC_EVEX_L_OPSIZE_K_B: 494 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_K_B); 495 case IC_EVEX_L_KZ_B: 496 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_KZ_B); 497 case IC_EVEX_L_XS_KZ_B: 498 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XS_KZ_B); 499 case IC_EVEX_L_XD_KZ_B: 500 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_XD_KZ_B); 501 case IC_EVEX_L_OPSIZE_KZ_B: 502 return VEX_WIG && inheritsFrom(child, IC_EVEX_L_W_OPSIZE_KZ_B); 503 case IC_EVEX_L_W_B: 504 case IC_EVEX_L_W_XS_B: 505 case IC_EVEX_L_W_XD_B: 506 case IC_EVEX_L_W_OPSIZE_B: 507 return false; 508 case IC_EVEX_L_W_K_B: 509 case IC_EVEX_L_W_XS_K_B: 510 case IC_EVEX_L_W_XD_K_B: 511 case IC_EVEX_L_W_OPSIZE_K_B: 512 return false; 513 case IC_EVEX_L_W_KZ_B: 514 case IC_EVEX_L_W_XS_KZ_B: 515 case IC_EVEX_L_W_XD_KZ_B: 516 case IC_EVEX_L_W_OPSIZE_KZ_B: 517 return false; 518 case IC_EVEX_L2_B: 519 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_B); 520 case IC_EVEX_L2_XS_B: 521 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_B); 522 case IC_EVEX_L2_XD_B: 523 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_B); 524 case IC_EVEX_L2_OPSIZE_B: 525 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_B); 526 case IC_EVEX_L2_K_B: 527 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_K_B); 528 case IC_EVEX_L2_XS_K_B: 529 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_K_B); 530 case IC_EVEX_L2_XD_K_B: 531 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_K_B); 532 case IC_EVEX_L2_OPSIZE_K_B: 533 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_K_B); 534 case IC_EVEX_L2_KZ_B: 535 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_KZ_B); 536 case IC_EVEX_L2_XS_KZ_B: 537 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XS_KZ_B); 538 case IC_EVEX_L2_XD_KZ_B: 539 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_XD_KZ_B); 540 case IC_EVEX_L2_OPSIZE_KZ_B: 541 return VEX_WIG && inheritsFrom(child, IC_EVEX_L2_W_OPSIZE_KZ_B); 542 case IC_EVEX_L2_W_B: 543 case IC_EVEX_L2_W_XS_B: 544 case IC_EVEX_L2_W_XD_B: 545 case IC_EVEX_L2_W_OPSIZE_B: 546 return false; 547 case IC_EVEX_L2_W_K_B: 548 case IC_EVEX_L2_W_XS_K_B: 549 case IC_EVEX_L2_W_XD_K_B: 550 case IC_EVEX_L2_W_OPSIZE_K_B: 551 return false; 552 case IC_EVEX_L2_W_KZ_B: 553 case IC_EVEX_L2_W_XS_KZ_B: 554 case IC_EVEX_L2_W_XD_KZ_B: 555 case IC_EVEX_L2_W_OPSIZE_KZ_B: 556 return false; 557 default: 558 errs() << "Unknown instruction class: " << 559 stringForContext((InstructionContext)parent) << "\n"; 560 llvm_unreachable("Unknown instruction class"); 561 } 562 } 563 564 /// outranks - Indicates whether, if an instruction has two different applicable 565 /// classes, which class should be preferred when performing decode. This 566 /// imposes a total ordering (ties are resolved toward "lower") 567 /// 568 /// @param upper - The class that may be preferable 569 /// @param lower - The class that may be less preferable 570 /// @return - True if upper is to be preferred, false otherwise. 571 static inline bool outranks(InstructionContext upper, 572 InstructionContext lower) { 573 assert(upper < IC_max); 574 assert(lower < IC_max); 575 576 #define ENUM_ENTRY(n, r, d) r, 577 #define ENUM_ENTRY_K_B(n, r, d) ENUM_ENTRY(n, r, d) \ 578 ENUM_ENTRY(n##_K_B, r, d) ENUM_ENTRY(n##_KZ_B, r, d) \ 579 ENUM_ENTRY(n##_KZ, r, d) ENUM_ENTRY(n##_K, r, d) ENUM_ENTRY(n##_B, r, d) 580 static int ranks[IC_max] = { 581 INSTRUCTION_CONTEXTS 582 }; 583 #undef ENUM_ENTRY 584 #undef ENUM_ENTRY_K_B 585 586 return (ranks[upper] > ranks[lower]); 587 } 588 589 /// getDecisionType - Determines whether a ModRM decision with 255 entries can 590 /// be compacted by eliminating redundant information. 591 /// 592 /// @param decision - The decision to be compacted. 593 /// @return - The compactest available representation for the decision. 594 static ModRMDecisionType getDecisionType(ModRMDecision &decision) { 595 bool satisfiesOneEntry = true; 596 bool satisfiesSplitRM = true; 597 bool satisfiesSplitReg = true; 598 bool satisfiesSplitMisc = true; 599 600 for (unsigned index = 0; index < 256; ++index) { 601 if (decision.instructionIDs[index] != decision.instructionIDs[0]) 602 satisfiesOneEntry = false; 603 604 if (((index & 0xc0) == 0xc0) && 605 (decision.instructionIDs[index] != decision.instructionIDs[0xc0])) 606 satisfiesSplitRM = false; 607 608 if (((index & 0xc0) != 0xc0) && 609 (decision.instructionIDs[index] != decision.instructionIDs[0x00])) 610 satisfiesSplitRM = false; 611 612 if (((index & 0xc0) == 0xc0) && 613 (decision.instructionIDs[index] != decision.instructionIDs[index&0xf8])) 614 satisfiesSplitReg = false; 615 616 if (((index & 0xc0) != 0xc0) && 617 (decision.instructionIDs[index] != decision.instructionIDs[index&0x38])) 618 satisfiesSplitMisc = false; 619 } 620 621 if (satisfiesOneEntry) 622 return MODRM_ONEENTRY; 623 624 if (satisfiesSplitRM) 625 return MODRM_SPLITRM; 626 627 if (satisfiesSplitReg && satisfiesSplitMisc) 628 return MODRM_SPLITREG; 629 630 if (satisfiesSplitMisc) 631 return MODRM_SPLITMISC; 632 633 return MODRM_FULL; 634 } 635 636 /// stringForDecisionType - Returns a statically-allocated string corresponding 637 /// to a particular decision type. 638 /// 639 /// @param dt - The decision type. 640 /// @return - A pointer to the statically-allocated string (e.g., 641 /// "MODRM_ONEENTRY" for MODRM_ONEENTRY). 642 static const char* stringForDecisionType(ModRMDecisionType dt) { 643 #define ENUM_ENTRY(n) case n: return #n; 644 switch (dt) { 645 default: 646 llvm_unreachable("Unknown decision type"); 647 MODRMTYPES 648 }; 649 #undef ENUM_ENTRY 650 } 651 652 DisassemblerTables::DisassemblerTables() { 653 for (unsigned i = 0; i < array_lengthof(Tables); i++) 654 Tables[i] = std::make_unique<ContextDecision>(); 655 656 HasConflicts = false; 657 } 658 659 DisassemblerTables::~DisassemblerTables() { 660 } 661 662 void DisassemblerTables::emitModRMDecision(raw_ostream &o1, raw_ostream &o2, 663 unsigned &i1, unsigned &i2, 664 unsigned &ModRMTableNum, 665 ModRMDecision &decision) const { 666 static uint32_t sTableNumber = 0; 667 static uint32_t sEntryNumber = 1; 668 ModRMDecisionType dt = getDecisionType(decision); 669 670 if (dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0) { 671 // Empty table. 672 o2 << "{" << stringForDecisionType(dt) << ", 0}"; 673 return; 674 } 675 676 std::vector<unsigned> ModRMDecision; 677 678 switch (dt) { 679 default: 680 llvm_unreachable("Unknown decision type"); 681 case MODRM_ONEENTRY: 682 ModRMDecision.push_back(decision.instructionIDs[0]); 683 break; 684 case MODRM_SPLITRM: 685 ModRMDecision.push_back(decision.instructionIDs[0x00]); 686 ModRMDecision.push_back(decision.instructionIDs[0xc0]); 687 break; 688 case MODRM_SPLITREG: 689 for (unsigned index = 0; index < 64; index += 8) 690 ModRMDecision.push_back(decision.instructionIDs[index]); 691 for (unsigned index = 0xc0; index < 256; index += 8) 692 ModRMDecision.push_back(decision.instructionIDs[index]); 693 break; 694 case MODRM_SPLITMISC: 695 for (unsigned index = 0; index < 64; index += 8) 696 ModRMDecision.push_back(decision.instructionIDs[index]); 697 for (unsigned index = 0xc0; index < 256; ++index) 698 ModRMDecision.push_back(decision.instructionIDs[index]); 699 break; 700 case MODRM_FULL: 701 for (unsigned index = 0; index < 256; ++index) 702 ModRMDecision.push_back(decision.instructionIDs[index]); 703 break; 704 } 705 706 unsigned &EntryNumber = ModRMTable[ModRMDecision]; 707 if (EntryNumber == 0) { 708 EntryNumber = ModRMTableNum; 709 710 ModRMTableNum += ModRMDecision.size(); 711 o1 << "/*Table" << EntryNumber << "*/\n"; 712 i1++; 713 for (std::vector<unsigned>::const_iterator I = ModRMDecision.begin(), 714 E = ModRMDecision.end(); I != E; ++I) { 715 o1.indent(i1 * 2) << format("0x%hx", *I) << ", /*" 716 << InstructionSpecifiers[*I].name << "*/\n"; 717 } 718 i1--; 719 } 720 721 o2 << "{" << stringForDecisionType(dt) << ", " << EntryNumber << "}"; 722 723 switch (dt) { 724 default: 725 llvm_unreachable("Unknown decision type"); 726 case MODRM_ONEENTRY: 727 sEntryNumber += 1; 728 break; 729 case MODRM_SPLITRM: 730 sEntryNumber += 2; 731 break; 732 case MODRM_SPLITREG: 733 sEntryNumber += 16; 734 break; 735 case MODRM_SPLITMISC: 736 sEntryNumber += 8 + 64; 737 break; 738 case MODRM_FULL: 739 sEntryNumber += 256; 740 break; 741 } 742 743 // We assume that the index can fit into uint16_t. 744 assert(sEntryNumber < 65536U && 745 "Index into ModRMDecision is too large for uint16_t!"); 746 747 ++sTableNumber; 748 } 749 750 void DisassemblerTables::emitOpcodeDecision(raw_ostream &o1, raw_ostream &o2, 751 unsigned &i1, unsigned &i2, 752 unsigned &ModRMTableNum, 753 OpcodeDecision &opDecision) const { 754 o2 << "{"; 755 ++i2; 756 757 unsigned index; 758 for (index = 0; index < 256; ++index) { 759 auto &decision = opDecision.modRMDecisions[index]; 760 ModRMDecisionType dt = getDecisionType(decision); 761 if (!(dt == MODRM_ONEENTRY && decision.instructionIDs[0] == 0)) 762 break; 763 } 764 if (index == 256) { 765 // If all 256 entries are MODRM_ONEENTRY, omit output. 766 assert(MODRM_ONEENTRY == 0); 767 --i2; 768 o2 << "},\n"; 769 } else { 770 o2 << " /* struct OpcodeDecision */ {\n"; 771 for (index = 0; index < 256; ++index) { 772 o2.indent(i2); 773 774 o2 << "/*0x" << format("%02hhx", index) << "*/"; 775 776 emitModRMDecision(o1, o2, i1, i2, ModRMTableNum, 777 opDecision.modRMDecisions[index]); 778 779 if (index < 255) 780 o2 << ","; 781 782 o2 << "\n"; 783 } 784 o2.indent(i2) << "}\n"; 785 --i2; 786 o2.indent(i2) << "},\n"; 787 } 788 } 789 790 void DisassemblerTables::emitContextDecision(raw_ostream &o1, raw_ostream &o2, 791 unsigned &i1, unsigned &i2, 792 unsigned &ModRMTableNum, 793 ContextDecision &decision, 794 const char* name) const { 795 o2.indent(i2) << "static const struct ContextDecision " << name << " = {{/* opcodeDecisions */\n"; 796 i2++; 797 798 for (unsigned index = 0; index < IC_max; ++index) { 799 o2.indent(i2) << "/*"; 800 o2 << stringForContext((InstructionContext)index); 801 o2 << "*/ "; 802 803 emitOpcodeDecision(o1, o2, i1, i2, ModRMTableNum, 804 decision.opcodeDecisions[index]); 805 } 806 807 i2--; 808 o2.indent(i2) << "}};" << "\n"; 809 } 810 811 void DisassemblerTables::emitInstructionInfo(raw_ostream &o, 812 unsigned &i) const { 813 unsigned NumInstructions = InstructionSpecifiers.size(); 814 815 o << "static const struct OperandSpecifier x86OperandSets[][" 816 << X86_MAX_OPERANDS << "] = {\n"; 817 818 typedef SmallVector<std::pair<OperandEncoding, OperandType>, 819 X86_MAX_OPERANDS> OperandListTy; 820 std::map<OperandListTy, unsigned> OperandSets; 821 822 unsigned OperandSetNum = 0; 823 for (unsigned Index = 0; Index < NumInstructions; ++Index) { 824 OperandListTy OperandList; 825 826 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS; 827 ++OperandIndex) { 828 OperandEncoding Encoding = (OperandEncoding)InstructionSpecifiers[Index] 829 .operands[OperandIndex].encoding; 830 OperandType Type = (OperandType)InstructionSpecifiers[Index] 831 .operands[OperandIndex].type; 832 OperandList.push_back(std::make_pair(Encoding, Type)); 833 } 834 unsigned &N = OperandSets[OperandList]; 835 if (N != 0) continue; 836 837 N = ++OperandSetNum; 838 839 o << " { /* " << (OperandSetNum - 1) << " */\n"; 840 for (unsigned i = 0, e = OperandList.size(); i != e; ++i) { 841 const char *Encoding = stringForOperandEncoding(OperandList[i].first); 842 const char *Type = stringForOperandType(OperandList[i].second); 843 o << " { " << Encoding << ", " << Type << " },\n"; 844 } 845 o << " },\n"; 846 } 847 o << "};" << "\n\n"; 848 849 o.indent(i * 2) << "static const struct InstructionSpecifier "; 850 o << INSTRUCTIONS_STR "[" << InstructionSpecifiers.size() << "] = {\n"; 851 852 i++; 853 854 for (unsigned index = 0; index < NumInstructions; ++index) { 855 o.indent(i * 2) << "{ /* " << index << " */\n"; 856 i++; 857 858 OperandListTy OperandList; 859 for (unsigned OperandIndex = 0; OperandIndex < X86_MAX_OPERANDS; 860 ++OperandIndex) { 861 OperandEncoding Encoding = (OperandEncoding)InstructionSpecifiers[index] 862 .operands[OperandIndex].encoding; 863 OperandType Type = (OperandType)InstructionSpecifiers[index] 864 .operands[OperandIndex].type; 865 OperandList.push_back(std::make_pair(Encoding, Type)); 866 } 867 o.indent(i * 2) << (OperandSets[OperandList] - 1) << ",\n"; 868 869 o.indent(i * 2) << "/* " << InstructionSpecifiers[index].name << " */\n"; 870 871 i--; 872 o.indent(i * 2) << "},\n"; 873 } 874 875 i--; 876 o.indent(i * 2) << "};" << "\n"; 877 } 878 879 void DisassemblerTables::emitContextTable(raw_ostream &o, unsigned &i) const { 880 o.indent(i * 2) << "static const uint8_t " CONTEXTS_STR 881 "[" << ATTR_max << "] = {\n"; 882 i++; 883 884 for (unsigned index = 0; index < ATTR_max; ++index) { 885 o.indent(i * 2); 886 887 if ((index & ATTR_EVEX) || (index & ATTR_VEX) || (index & ATTR_VEXL)) { 888 if (index & ATTR_EVEX) 889 o << "IC_EVEX"; 890 else 891 o << "IC_VEX"; 892 893 if ((index & ATTR_EVEX) && (index & ATTR_EVEXL2)) 894 o << "_L2"; 895 else if (index & ATTR_VEXL) 896 o << "_L"; 897 898 if (index & ATTR_REXW) 899 o << "_W"; 900 901 if (index & ATTR_OPSIZE) 902 o << "_OPSIZE"; 903 else if (index & ATTR_XD) 904 o << "_XD"; 905 else if (index & ATTR_XS) 906 o << "_XS"; 907 908 if ((index & ATTR_EVEX)) { 909 if (index & ATTR_EVEXKZ) 910 o << "_KZ"; 911 else if (index & ATTR_EVEXK) 912 o << "_K"; 913 914 if (index & ATTR_EVEXB) 915 o << "_B"; 916 } 917 } 918 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XS)) 919 o << "IC_64BIT_REXW_XS"; 920 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && (index & ATTR_XD)) 921 o << "IC_64BIT_REXW_XD"; 922 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && 923 (index & ATTR_OPSIZE)) 924 o << "IC_64BIT_REXW_OPSIZE"; 925 else if ((index & ATTR_64BIT) && (index & ATTR_REXW) && 926 (index & ATTR_ADSIZE)) 927 o << "IC_64BIT_REXW_ADSIZE"; 928 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_OPSIZE)) 929 o << "IC_64BIT_XD_OPSIZE"; 930 else if ((index & ATTR_64BIT) && (index & ATTR_XD) && (index & ATTR_ADSIZE)) 931 o << "IC_64BIT_XD_ADSIZE"; 932 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_OPSIZE)) 933 o << "IC_64BIT_XS_OPSIZE"; 934 else if ((index & ATTR_64BIT) && (index & ATTR_XS) && (index & ATTR_ADSIZE)) 935 o << "IC_64BIT_XS_ADSIZE"; 936 else if ((index & ATTR_64BIT) && (index & ATTR_XS)) 937 o << "IC_64BIT_XS"; 938 else if ((index & ATTR_64BIT) && (index & ATTR_XD)) 939 o << "IC_64BIT_XD"; 940 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE) && 941 (index & ATTR_ADSIZE)) 942 o << "IC_64BIT_OPSIZE_ADSIZE"; 943 else if ((index & ATTR_64BIT) && (index & ATTR_OPSIZE)) 944 o << "IC_64BIT_OPSIZE"; 945 else if ((index & ATTR_64BIT) && (index & ATTR_ADSIZE)) 946 o << "IC_64BIT_ADSIZE"; 947 else if ((index & ATTR_64BIT) && (index & ATTR_REXW)) 948 o << "IC_64BIT_REXW"; 949 else if ((index & ATTR_64BIT)) 950 o << "IC_64BIT"; 951 else if ((index & ATTR_XS) && (index & ATTR_OPSIZE)) 952 o << "IC_XS_OPSIZE"; 953 else if ((index & ATTR_XD) && (index & ATTR_OPSIZE)) 954 o << "IC_XD_OPSIZE"; 955 else if ((index & ATTR_XS) && (index & ATTR_ADSIZE)) 956 o << "IC_XS_ADSIZE"; 957 else if ((index & ATTR_XD) && (index & ATTR_ADSIZE)) 958 o << "IC_XD_ADSIZE"; 959 else if (index & ATTR_XS) 960 o << "IC_XS"; 961 else if (index & ATTR_XD) 962 o << "IC_XD"; 963 else if ((index & ATTR_OPSIZE) && (index & ATTR_ADSIZE)) 964 o << "IC_OPSIZE_ADSIZE"; 965 else if (index & ATTR_OPSIZE) 966 o << "IC_OPSIZE"; 967 else if (index & ATTR_ADSIZE) 968 o << "IC_ADSIZE"; 969 else 970 o << "IC"; 971 972 o << ", // " << index << "\n"; 973 } 974 975 i--; 976 o.indent(i * 2) << "};" << "\n"; 977 } 978 979 void DisassemblerTables::emitContextDecisions(raw_ostream &o1, raw_ostream &o2, 980 unsigned &i1, unsigned &i2, 981 unsigned &ModRMTableNum) const { 982 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[0], ONEBYTE_STR); 983 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[1], TWOBYTE_STR); 984 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[2], THREEBYTE38_STR); 985 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[3], THREEBYTE3A_STR); 986 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[4], XOP8_MAP_STR); 987 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[5], XOP9_MAP_STR); 988 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[6], XOPA_MAP_STR); 989 emitContextDecision(o1, o2, i1, i2, ModRMTableNum, *Tables[7], THREEDNOW_MAP_STR); 990 } 991 992 void DisassemblerTables::emit(raw_ostream &o) const { 993 unsigned i1 = 0; 994 unsigned i2 = 0; 995 996 std::string s1; 997 std::string s2; 998 999 raw_string_ostream o1(s1); 1000 raw_string_ostream o2(s2); 1001 1002 emitInstructionInfo(o, i2); 1003 o << "\n"; 1004 1005 emitContextTable(o, i2); 1006 o << "\n"; 1007 1008 unsigned ModRMTableNum = 0; 1009 1010 o << "static const InstrUID modRMTable[] = {\n"; 1011 i1++; 1012 std::vector<unsigned> EmptyTable(1, 0); 1013 ModRMTable[EmptyTable] = ModRMTableNum; 1014 ModRMTableNum += EmptyTable.size(); 1015 o1 << "/*EmptyTable*/\n"; 1016 o1.indent(i1 * 2) << "0x0,\n"; 1017 i1--; 1018 emitContextDecisions(o1, o2, i1, i2, ModRMTableNum); 1019 1020 o << o1.str(); 1021 o << " 0x0\n"; 1022 o << "};\n"; 1023 o << "\n"; 1024 o << o2.str(); 1025 o << "\n"; 1026 o << "\n"; 1027 } 1028 1029 void DisassemblerTables::setTableFields(ModRMDecision &decision, 1030 const ModRMFilter &filter, 1031 InstrUID uid, 1032 uint8_t opcode) { 1033 for (unsigned index = 0; index < 256; ++index) { 1034 if (filter.accepts(index)) { 1035 if (decision.instructionIDs[index] == uid) 1036 continue; 1037 1038 if (decision.instructionIDs[index] != 0) { 1039 InstructionSpecifier &newInfo = 1040 InstructionSpecifiers[uid]; 1041 InstructionSpecifier &previousInfo = 1042 InstructionSpecifiers[decision.instructionIDs[index]]; 1043 1044 if(previousInfo.name == "NOOP" && (newInfo.name == "XCHG16ar" || 1045 newInfo.name == "XCHG32ar" || 1046 newInfo.name == "XCHG64ar")) 1047 continue; // special case for XCHG*ar and NOOP 1048 1049 if (outranks(previousInfo.insnContext, newInfo.insnContext)) 1050 continue; 1051 1052 if (previousInfo.insnContext == newInfo.insnContext) { 1053 errs() << "Error: Primary decode conflict: "; 1054 errs() << newInfo.name << " would overwrite " << previousInfo.name; 1055 errs() << "\n"; 1056 errs() << "ModRM " << index << "\n"; 1057 errs() << "Opcode " << (uint16_t)opcode << "\n"; 1058 errs() << "Context " << stringForContext(newInfo.insnContext) << "\n"; 1059 HasConflicts = true; 1060 } 1061 } 1062 1063 decision.instructionIDs[index] = uid; 1064 } 1065 } 1066 } 1067 1068 void DisassemblerTables::setTableFields(OpcodeType type, 1069 InstructionContext insnContext, 1070 uint8_t opcode, 1071 const ModRMFilter &filter, 1072 InstrUID uid, 1073 bool is32bit, 1074 bool noPrefix, 1075 bool ignoresVEX_L, 1076 bool ignoresVEX_W, 1077 unsigned addressSize) { 1078 ContextDecision &decision = *Tables[type]; 1079 1080 for (unsigned index = 0; index < IC_max; ++index) { 1081 if ((is32bit || addressSize == 16) && 1082 inheritsFrom((InstructionContext)index, IC_64BIT)) 1083 continue; 1084 1085 bool adSize64 = addressSize == 64; 1086 if (inheritsFrom((InstructionContext)index, 1087 InstructionSpecifiers[uid].insnContext, noPrefix, 1088 ignoresVEX_L, ignoresVEX_W, adSize64)) 1089 setTableFields(decision.opcodeDecisions[index].modRMDecisions[opcode], 1090 filter, 1091 uid, 1092 opcode); 1093 } 1094 } 1095