https://github.com/akkartik/mu/blob/main/linux/bootstrap/033check_operands.cc

      1 //: Since we're tagging arguments with their types, let's start checking these
      2 //: argument types for each instruction.
      3
      4 void test_check_missing_imm8_argument() {
      5   Hide_errors = true;
      6   run(
      7       "== code 0x1\n"
      8       "cd\n"  // interrupt ??
      9   );
     10   CHECK_TRACE_CONTENTS(
     11       "error: 'cd' (software interrupt): missing imm8 argument\n"
     12   );
     13 }
     14
     15 :(before "Pack Operands(segment code)")
     16 check_arguments(code);
     17 if (trace_contains_errors()) return;
     18
     19 :(code)
     20 void check_arguments(const segment& code) {
     21   trace(3, "transform") << "-- check arguments" << end();
     22   for (int i = 0;  i < SIZE(code.lines);  ++i) {
     23     check_arguments(code.lines.at(i));
     24     if (trace_contains_errors()) return;  // stop at the first mal-formed instruction
     25   }
     26 }
     27
     28 void check_arguments(const line& inst) {
     29   word op = preprocess_op(inst.words.at(0));
     30   if (op.data == "0f") {
     31     check_arguments_0f(inst);
     32     return;
     33   }
     34   if (op.data == "f3") {
     35     check_arguments_f3(inst);
     36     return;
     37   }
     38   check_arguments(inst, op);
     39 }
     40
     41 word preprocess_op(word/*copy*/ op) {
     42   op.data = tolower(op.data.c_str());
     43   // opcodes can't be negative
     44   if (starts_with(op.data, "0x"))
     45     op.data = op.data.substr(2);
     46   if (SIZE(op.data) == 1)
     47     op.data = string("0")+op.data;
     48   return op;
     49 }
     50
     51 void test_preprocess_op() {
     52   word w1;  w1.data = "0xf";
     53   word w2;  w2.data = "0f";
     54   CHECK_EQ(preprocess_op(w1).data, preprocess_op(w2).data);
     55 }
     56
     57 //: To check the arguments for an opcode, we'll track the permitted arguments
     58 //: for each supported opcode in a bitvector. That way we can often compute the
     59 //: 'received' argument bitvector for each instruction's arguments and compare
     60 //: it with the 'expected' bitvector.
     61 //:
     62 //: The 'expected' and 'received' bitvectors can be different; the MODRM bit
     63 //: in the 'expected' bitvector maps to multiple 'received' argument types in
     64 //: an instruction. We deal in expected bitvectors throughout.
     65
     66 :(before "End Types")
     67 enum expected_argument_type {
     68   // start from the least significant bit
     69   MODRM,  // more complex, may also involve disp8 or disp32
     70   SUBOP,
     71   DISP8,
     72   DISP16,
     73   DISP32,
     74   IMM8,
     75   IMM32,
     76   NUM_OPERAND_TYPES
     77 };
     78 :(before "End Globals")
     79 vector<string> Operand_type_name;
     80 map<string, expected_argument_type> Operand_type;
     81 :(before "End One-time Setup")
     82 init_op_types();
     83 :(code)
     84 void init_op_types() {
     85   assert(NUM_OPERAND_TYPES <= /*bits in a uint8_t*/8);
     86   Operand_type_name.resize(NUM_OPERAND_TYPES);
     87   #define DEF(type) Operand_type_name.at(type) = tolower(#type), put(Operand_type, tolower(#type), type);
     88   DEF(MODRM);
     89   DEF(SUBOP);
     90   DEF(DISP8);
     91   DEF(DISP16);
     92   DEF(DISP32);
     93   DEF(IMM8);
     94   DEF(IMM32);
     95   #undef DEF
     96 }
     97
     98 :(before "End Globals")
     99 map</*op*/string, /*bitvector*/uint8_t> Permitted_arguments;
    100 const uint8_t INVALID_OPERANDS = 0xff;  // no instruction uses all the argument types
    101 :(before "End One-time Setup")
    102 init_permitted_arguments();
    103 :(code)
    104 void init_permitted_arguments() {
    105   //// Class A: just op, no arguments
    106   // halt
    107   put(Permitted_arguments, "f4", 0x00);
    108   // inc
    109   put(Permitted_arguments, "40", 0x00);
    110   put(Permitted_arguments, "41", 0x00);
    111   put(Permitted_arguments, "42", 0x00);
    112   put(Permitted_arguments, "43", 0x00);
    113   put(Permitted_arguments, "44", 0x00);
    114   put(Permitted_arguments, "45", 0x00);
    115   put(Permitted_arguments, "46", 0x00);
    116   put(Permitted_arguments, "47", 0x00);
    117   // dec
    118   put(Permitted_arguments, "48", 0x00);
    119   put(Permitted_arguments, "49", 0x00);
    120   put(Permitted_arguments, "4a", 0x00);
    121   put(Permitted_arguments, "4b", 0x00);
    122   put(Permitted_arguments, "4c", 0x00);
    123   put(Permitted_arguments, "4d", 0x00);
    124   put(Permitted_arguments, "4e", 0x00);
    125   put(Permitted_arguments, "4f", 0x00);
    126   // push
    127   put(Permitted_arguments, "50", 0x00);
    128   put(Permitted_arguments, "51", 0x00);
    129   put(Permitted_arguments, "52", 0x00);
    130   put(Permitted_arguments, "53", 0x00);
    131   put(Permitted_arguments, "54", 0x00);
    132   put(Permitted_arguments, "55", 0x00);
    133   put(Permitted_arguments, "56", 0x00);
    134   put(Permitted_arguments, "57", 0x00);
    135   // pop
    136   put(Permitted_arguments, "58", 0x00);
    137   put(Permitted_arguments, "59", 0x00);
    138   put(Permitted_arguments, "5a", 0x00);
    139   put(Permitted_arguments, "5b", 0x00);
    140   put(Permitted_arguments, "5c", 0x00);
    141   put(Permitted_arguments, "5d", 0x00);
    142   put(Permitted_arguments, "5e", 0x00);
    143   put(Permitted_arguments, "5f", 0x00);
    144   // sign-extend EAX into EDX
    145   put(Permitted_arguments, "99", 0x00);
    146   // return
    147   put(Permitted_arguments, "c3", 0x00);
    148
    149   //// Class B: just op and disp8
    150   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    151   //  0     0     0      |0       1     0     0
    152
    153   // jump
    154   put(Permitted_arguments, "eb", 0x04);
    155   put(Permitted_arguments, "72", 0x04);
    156   put(Permitted_arguments, "73", 0x04);
    157   put(Permitted_arguments, "74", 0x04);
    158   put(Permitted_arguments, "75", 0x04);
    159   put(Permitted_arguments, "76", 0x04);
    160   put(Permitted_arguments, "77", 0x04);
    161   put(Permitted_arguments, "7c", 0x04);
    162   put(Permitted_arguments, "7d", 0x04);
    163   put(Permitted_arguments, "7e", 0x04);
    164   put(Permitted_arguments, "7f", 0x04);
    165
    166   //// Class D: just op and disp32
    167   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    168   //  0     0     1      |0       0     0     0
    169   put(Permitted_arguments, "e8", 0x10);  // call
    170   put(Permitted_arguments, "e9", 0x10);  // jump
    171
    172   //// Class E: just op and imm8
    173   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    174   //  0     1     0      |0       0     0     0
    175   put(Permitted_arguments, "cd", 0x20);  // software interrupt
    176
    177   //// Class F: just op and imm32
    178   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    179   //  1     0     0      |0       0     0     0
    180   put(Permitted_arguments, "05", 0x40);  // add
    181   put(Permitted_arguments, "2d", 0x40);  // subtract
    182   put(Permitted_arguments, "25", 0x40);  // and
    183   put(Permitted_arguments, "0d", 0x40);  // or
    184   put(Permitted_arguments, "35", 0x40);  // xor
    185   put(Permitted_arguments, "3d", 0x40);  // compare
    186   put(Permitted_arguments, "68", 0x40);  // push
    187   // copy
    188   put(Permitted_arguments, "b8", 0x40);
    189   put(Permitted_arguments, "b9", 0x40);
    190   put(Permitted_arguments, "ba", 0x40);
    191   put(Permitted_arguments, "bb", 0x40);
    192   put(Permitted_arguments, "bc", 0x40);
    193   put(Permitted_arguments, "bd", 0x40);
    194   put(Permitted_arguments, "be", 0x40);
    195   put(Permitted_arguments, "bf", 0x40);
    196
    197   //// Class M: using ModR/M byte
    198   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    199   //  0     0     0      |0       0     0     1
    200
    201   // add
    202   put(Permitted_arguments, "01", 0x01);
    203   put(Permitted_arguments, "03", 0x01);
    204   // subtract
    205   put(Permitted_arguments, "29", 0x01);
    206   put(Permitted_arguments, "2b", 0x01);
    207   // and
    208   put(Permitted_arguments, "21", 0x01);
    209   put(Permitted_arguments, "23", 0x01);
    210   // or
    211   put(Permitted_arguments, "09", 0x01);
    212   put(Permitted_arguments, "0b", 0x01);
    213   // xor
    214   put(Permitted_arguments, "31", 0x01);
    215   put(Permitted_arguments, "33", 0x01);
    216   // compare
    217   put(Permitted_arguments, "39", 0x01);
    218   put(Permitted_arguments, "3b", 0x01);
    219   // copy
    220   put(Permitted_arguments, "88", 0x01);
    221   put(Permitted_arguments, "89", 0x01);
    222   put(Permitted_arguments, "8a", 0x01);
    223   put(Permitted_arguments, "8b", 0x01);
    224   // swap
    225   put(Permitted_arguments, "87", 0x01);
    226   // copy address (lea)
    227   put(Permitted_arguments, "8d", 0x01);
    228
    229   //// Class N: op, ModR/M and subop (not r32)
    230   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    231   //  0     0     0      |0       0     1     1
    232   put(Permitted_arguments, "8f", 0x03);  // pop
    233   put(Permitted_arguments, "d3", 0x03);  // shift
    234   put(Permitted_arguments, "f7", 0x03);  // test/not/mul/div
    235   put(Permitted_arguments, "ff", 0x03);  // jump/push/call
    236
    237   //// Class O: op, ModR/M, subop (not r32) and imm8
    238   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    239   //  0     1     0      |0       0     1     1
    240   put(Permitted_arguments, "c1", 0x23);  // combine
    241   put(Permitted_arguments, "c6", 0x23);  // copy
    242
    243   //// Class P: op, ModR/M, subop (not r32) and imm32
    244   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    245   //  1     0     0      |0       0     1     1
    246   put(Permitted_arguments, "81", 0x43);  // combine
    247   put(Permitted_arguments, "c7", 0x43);  // copy
    248
    249   //// Class Q: op, ModR/M and imm32
    250   //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    251   //  1     0     0      |0       0     0     1
    252   put(Permitted_arguments, "69", 0x41);  // multiply
    253
    254   // End Init Permitted Operands
    255 }
    256
    257 #define HAS(bitvector, bit)  ((bitvector) & (1 << (bit)))
    258 #define SET(bitvector, bit)  ((bitvector) | (1 << (bit)))
    259 #define CLEAR(bitvector, bit)  ((bitvector) & (~(1 << (bit))))
    260
    261 void check_arguments(const line& inst, const word& op) {
    262   if (!is_hex_byte(op)) return;
    263   uint8_t expected_bitvector = get(Permitted_arguments, op.data);
    264   if (HAS(expected_bitvector, MODRM)) {
    265     check_arguments_modrm(inst, op);
    266     compare_bitvector_modrm(inst, expected_bitvector, maybe_name(op));
    267   }
    268   else {
    269     compare_bitvector(inst, expected_bitvector, maybe_name(op));
    270   }
    271 }
    272
    273 //: Many instructions can be checked just by comparing bitvectors.
    274
    275 void compare_bitvector(const line& inst, uint8_t expected, const string& maybe_op_name) {
    276   if (all_hex_bytes(inst) && has_arguments(inst)) return;  // deliberately programming in raw hex; we'll raise a warning elsewhere
    277   uint8_t bitvector = compute_expected_argument_bitvector(inst);
    278   if (trace_contains_errors()) return;  // duplicate argument type
    279   if (bitvector == expected) return;  // all good with this instruction
    280   for (int i = 0;  i < NUM_OPERAND_TYPES;  ++i, bitvector >>= 1, expected >>= 1) {
    281 //?     cerr << "comparing " << HEXBYTE << NUM(bitvector) << " with " << NUM(expected) << '\n';
    282     if ((bitvector & 0x1) == (expected & 0x1)) continue;  // all good with this argument
    283     const string& optype = Operand_type_name.at(i);
    284     if ((bitvector & 0x1) > (expected & 0x1))
    285       raise << "'" << to_string(inst) << "'" << maybe_op_name << ": unexpected " << optype << " argument\n" << end();
    286     else
    287       raise << "'" << to_string(inst) << "'" << maybe_op_name << ": missing " << optype << " argument\n" << end();
    288     // continue giving all errors for a single instruction
    289   }
    290   // ignore settings in any unused bits
    291 }
    292
    293 string maybe_name(const word& op) {
    294   if (!is_hex_byte(op)) return "";
    295   if (!contains_key(Name, op.data)) return "";
    296   // strip stuff in parens from the name
    297   const string& s = get(Name, op.data);
    298   return " ("+s.substr(0, s.find(" ("))+')';
    299 }
    300
    301 uint32_t compute_expected_argument_bitvector(const line& inst) {
    302   set<string> arguments_found;
    303   uint32_t bitvector = 0;
    304   for (int i = /*skip op*/1;  i < SIZE(inst.words);  ++i) {
    305     bitvector = bitvector | expected_bit_for_received_argument(inst.words.at(i), arguments_found, inst);
    306     if (trace_contains_errors()) return INVALID_OPERANDS;  // duplicate argument type
    307   }
    308   return bitvector;
    309 }
    310
    311 bool has_arguments(const line& inst) {
    312   return SIZE(inst.words) > first_argument(inst);
    313 }
    314
    315 int first_argument(const line& inst) {
    316   if (inst.words.at(0).data == "0f") return 2;
    317   if (inst.words.at(0).data == "f2" || inst.words.at(0).data == "f3") {
    318     if (inst.words.at(1).data == "0f")
    319       return 3;
    320     else
    321       return 2;
    322   }
    323   return 1;
    324 }
    325
    326 // Scan the metadata of 'w' and return the expected bit corresponding to any argument type.
    327 // Also raise an error if metadata contains multiple argument types.
    328 uint32_t expected_bit_for_received_argument(const word& w, set<string>& instruction_arguments, const line& inst) {
    329   uint32_t bv = 0;
    330   bool found = false;
    331   for (int i = 0;  i < SIZE(w.metadata);  ++i) {
    332     string/*copy*/ curr = w.metadata.at(i);
    333     string expected_metadata = curr;
    334     if (curr == "mod" || curr == "rm32" || curr == "r32" || curr == "xm32" || curr == "x32" || curr == "scale" || curr == "index" || curr == "base")
    335       expected_metadata = "modrm";
    336     else if (!contains_key(Operand_type, curr)) continue;  // ignore unrecognized metadata
    337     if (found) {
    338       raise << "'" << w.original << "' has conflicting argument types; it should have only one\n" << end();
    339       return INVALID_OPERANDS;
    340     }
    341     if (instruction_arguments.find(curr) != instruction_arguments.end()) {
    342       raise << "'" << to_string(inst) << "': duplicate " << curr << " argument\n" << end();
    343       return INVALID_OPERANDS;
    344     }
    345     instruction_arguments.insert(curr);
    346     bv = (1 << get(Operand_type, expected_metadata));
    347     found = true;
    348   }
    349   return bv;
    350 }
    351
    352 void test_conflicting_argument_type() {
    353   Hide_errors = true;
    354   run(
    355       "== code 0x1\n"
    356       "cd/software-interrupt 80/imm8/imm32\n"
    357   );
    358   CHECK_TRACE_CONTENTS(
    359       "error: '80/imm8/imm32' has conflicting argument types; it should have only one\n"
    360   );
    361 }
    362
    363 //: Instructions computing effective addresses have more complex rules, so
    364 //: we'll hard-code a common set of instruction-decoding rules.
    365
    366 void test_check_missing_mod_argument() {
    367   Hide_errors = true;
    368   run(
    369       "== code 0x1\n"
    370       "81 0/add/subop       3/rm32/ebx 1/imm32\n"
    371   );
    372   CHECK_TRACE_CONTENTS(
    373       "error: '81 0/add/subop 3/rm32/ebx 1/imm32' (combine rm32 with imm32 based on subop): missing mod argument\n"
    374   );
    375 }
    376
    377 void check_arguments_modrm(const line& inst, const word& op) {
    378   if (all_hex_bytes(inst)) return;  // deliberately programming in raw hex; we'll raise a warning elsewhere
    379   check_argument_metadata_present(inst, "mod", op);
    380   if (!has_argument_metadata(inst, "rm32") && !has_argument_metadata(inst, "xm32"))
    381     raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": missing rm32 (or xm32) argument\n" << end();
    382   // no check for r32; some instructions don't use it; just assume it's 0 if missing
    383   if (op.data == "81" || op.data == "8f" || op.data == "f7" || op.data == "ff") {  // keep sync'd with 'help subop'
    384     check_argument_metadata_present(inst, "subop", op);
    385     check_argument_metadata_absent(inst, "r32", op, "should be replaced by subop");
    386     check_argument_metadata_absent(inst, "x32", op, "should be replaced by subop");
    387   }
    388   if (trace_contains_errors()) return;
    389   if (metadata_m32(inst).data != "4") return;
    390   // SIB byte checks
    391   uint8_t mod = hex_byte(metadata(inst, "mod").data);
    392   if (mod != /*direct*/3) {
    393     check_argument_metadata_present(inst, "base", op);
    394     check_argument_metadata_present(inst, "index", op);  // otherwise why go to SIB?
    395   }
    396   else {
    397     check_argument_metadata_absent(inst, "base", op, "direct mode");
    398     check_argument_metadata_absent(inst, "index", op, "direct mode");
    399   }
    400   // no check for scale; 0 (2**0 = 1) by default
    401 }
    402
    403 word metadata_m32(const line& inst) {
    404   for (int i = 0;  i < SIZE(inst.words);  ++i)
    405     if (has_argument_metadata(inst.words.at(i), "rm32") || has_argument_metadata(inst.words.at(i), "xm32"))
    406       return inst.words.at(i);
    407   assert(false);
    408 }
    409
    410 // same as compare_bitvector, with one additional exception for modrm-based
    411 // instructions: they may use an extra displacement on occasion
    412 void compare_bitvector_modrm(const line& inst, uint8_t expected, const string& maybe_op_name) {
    413   if (all_hex_bytes(inst) && has_arguments(inst)) return;  // deliberately programming in raw hex; we'll raise a warning elsewhere
    414   uint8_t bitvector = compute_expected_argument_bitvector(inst);
    415   if (trace_contains_errors()) return;  // duplicate argument type
    416   // update 'expected' bitvector for the additional exception
    417   if (has_argument_metadata(inst, "mod")) {
    418     int32_t mod = parse_int(metadata(inst, "mod").data);
    419     switch (mod) {
    420     case 0:
    421       if (has_argument_metadata(inst, "rm32") && parse_int(metadata(inst, "rm32").data) == 5)
    422         expected |= (1<<DISP32);
    423       break;
    424     case 1:
    425       expected |= (1<<DISP8);
    426       break;
    427     case 2:
    428       expected |= (1<<DISP32);
    429       break;
    430     }
    431   }
    432   if (bitvector == expected) return;  // all good with this instruction
    433   for (int i = 0;  i < NUM_OPERAND_TYPES;  ++i, bitvector >>= 1, expected >>= 1) {
    434 //?     cerr << "comparing for modrm " << HEXBYTE << NUM(bitvector) << " with " << NUM(expected) << '\n';
    435     if ((bitvector & 0x1) == (expected & 0x1)) continue;  // all good with this argument
    436     const string& optype = Operand_type_name.at(i);
    437     if ((bitvector & 0x1) > (expected & 0x1))
    438       raise << "'" << to_string(inst) << "'" << maybe_op_name << ": unexpected " << optype << " argument\n" << end();
    439     else
    440       raise << "'" << to_string(inst) << "'" << maybe_op_name << ": missing " << optype << " argument\n" << end();
    441     // continue giving all errors for a single instruction
    442   }
    443   // ignore settings in any unused bits
    444 }
    445
    446 void check_argument_metadata_present(const line& inst, const string& type, const word& op) {
    447   if (!has_argument_metadata(inst, type))
    448     raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": missing " << type << " argument\n" << end();
    449 }
    450
    451 void check_argument_metadata_absent(const line& inst, const string& type, const word& op, const string& msg) {
    452   if (has_argument_metadata(inst, type))
    453     raise << "'" << to_string(inst) << "'" << maybe_name(op) << ": unexpected " << type << " argument (" << msg << ")\n" << end();
    454 }
    455
    456 void test_modrm_with_displacement() {
    457   Reg[EAX].u = 0x1;
    458   transform(
    459       "== code 0x1\n"
    460       // just avoid null pointer
    461       "8b/copy 1/mod/lookup+disp8 0/rm32/EAX 2/r32/EDX 4/disp8\n"  // copy *(EAX+4) to EDX
    462   );
    463   CHECK_TRACE_COUNT("error", 0);
    464 }
    465
    466 void test_check_missing_disp8() {
    467   Hide_errors = true;
    468   transform(
    469       "== code 0x1\n"
    470       "89/copy 1/mod/lookup+disp8 0/rm32/EAX 1/r32/ECX\n"  // missing disp8
    471   );
    472   CHECK_TRACE_CONTENTS(
    473       "error: '89/copy 1/mod/lookup+disp8 0/rm32/EAX 1/r32/ECX' (copy r32 to rm32): missing disp8 argument\n"
    474   );
    475 }
    476
    477 void test_check_missing_disp32() {
    478   Hide_errors = true;
    479   transform(
    480       "== code 0x1\n"
    481       "8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX\n"  // missing disp32
    482   );
    483   CHECK_TRACE_CONTENTS(
    484       "error: '8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX' (copy rm32 to r32): missing disp32 argument\n"
    485   );
    486 }
    487
    488 void test_conflicting_arguments_in_modrm_instruction() {
    489   Hide_errors = true;
    490   run(
    491       "== code 0x1\n"
    492       "01/add 0/mod 3/mod\n"
    493   );
    494   CHECK_TRACE_CONTENTS(
    495       "error: '01/add 0/mod 3/mod' has conflicting mod arguments\n"
    496   );
    497 }
    498
    499 void test_conflicting_argument_type_modrm() {
    500   Hide_errors = true;
    501   run(
    502       "== code 0x1\n"
    503       "01/add 0/mod 3/rm32/r32\n"
    504   );
    505   CHECK_TRACE_CONTENTS(
    506       "error: '3/rm32/r32' has conflicting argument types; it should have only one\n"
    507   );
    508 }
    509
    510 void test_check_missing_rm32_argument() {
    511   Hide_errors = true;
    512   run(
    513       "== code 0x1\n"
    514       "81 0/add/subop 0/mod            1/imm32\n"
    515   );
    516   CHECK_TRACE_CONTENTS(
    517       "error: '81 0/add/subop 0/mod 1/imm32' (combine rm32 with imm32 based on subop): missing rm32 (or xm32) argument\n"
    518   );
    519 }
    520
    521 void test_check_missing_subop_argument() {
    522   Hide_errors = true;
    523   run(
    524       "== code 0x1\n"
    525       "81             0/mod 3/rm32/ebx 1/imm32\n"
    526   );
    527   CHECK_TRACE_CONTENTS(
    528       "error: '81 0/mod 3/rm32/ebx 1/imm32' (combine rm32 with imm32 based on subop): missing subop argument\n"
    529   );
    530 }
    531
    532 void test_check_missing_base_argument() {
    533   Hide_errors = true;
    534   run(
    535       "== code 0x1\n"
    536       "81 0/add/subop 0/mod/indirect 4/rm32/use-sib 1/imm32\n"
    537   );
    538   CHECK_TRACE_CONTENTS(
    539       "error: '81 0/add/subop 0/mod/indirect 4/rm32/use-sib 1/imm32' (combine rm32 with imm32 based on subop): missing base argument\n"
    540   );
    541 }
    542
    543 void test_check_missing_index_argument() {
    544   Hide_errors = true;
    545   run(
    546       "== code 0x1\n"
    547       "81 0/add/subop 0/mod/indirect 4/rm32/use-sib 0/base 1/imm32\n"
    548   );
    549   CHECK_TRACE_CONTENTS(
    550       "error: '81 0/add/subop 0/mod/indirect 4/rm32/use-sib 0/base 1/imm32' (combine rm32 with imm32 based on subop): missing index argument\n"
    551   );
    552 }
    553
    554 void test_check_missing_base_argument_2() {
    555   Hide_errors = true;
    556   run(
    557       "== code 0x1\n"
    558       "81 0/add/subop 0/mod/indirect 4/rm32/use-sib 2/index 3/scale 1/imm32\n"
    559   );
    560   CHECK_TRACE_CONTENTS(
    561       "error: '81 0/add/subop 0/mod/indirect 4/rm32/use-sib 2/index 3/scale 1/imm32' (combine rm32 with imm32 based on subop): missing base argument\n"
    562   );
    563 }
    564
    565 void test_check_extra_displacement() {
    566   Hide_errors = true;
    567   run(
    568       "== code 0x1\n"
    569       "89/copy 0/mod/indirect 0/rm32/EAX 1/r32/ECX 4/disp8\n"
    570   );
    571   CHECK_TRACE_CONTENTS(
    572       "error: '89/copy 0/mod/indirect 0/rm32/EAX 1/r32/ECX 4/disp8' (copy r32 to rm32): unexpected disp8 argument\n"
    573   );
    574 }
    575
    576 void test_check_duplicate_argument() {
    577   Hide_errors = true;
    578   run(
    579       "== code 0x1\n"
    580       "89/copy 0/mod/indirect 0/rm32/EAX 1/r32/ECX 1/r32\n"
    581   );
    582   CHECK_TRACE_CONTENTS(
    583       "error: '89/copy 0/mod/indirect 0/rm32/EAX 1/r32/ECX 1/r32': duplicate r32 argument\n"
    584   );
    585 }
    586
    587 void test_check_base_argument_not_needed_in_direct_mode() {
    588   run(
    589       "== code 0x1\n"
    590       "81 0/add/subop 3/mod/indirect 4/rm32/use-sib 1/imm32\n"
    591   );
    592   CHECK_TRACE_COUNT("error", 0);
    593 }
    594
    595 void test_extra_modrm() {
    596   Hide_errors = true;
    597   run(
    598       "== code 0x1\n"
    599       "59/pop-to-ECX  3/mod/direct 1/rm32/ECX 4/r32/ESP\n"
    600   );
    601   CHECK_TRACE_CONTENTS(
    602       "error: '59/pop-to-ECX 3/mod/direct 1/rm32/ECX 4/r32/ESP' (pop top of stack to ECX): unexpected modrm argument\n"
    603   );
    604 }
    605
    606 //:: similarly handle multi-byte opcodes
    607
    608 void check_arguments_0f(const line& inst) {
    609   assert(inst.words.at(0).data == "0f");
    610   if (SIZE(inst.words) == 1) {
    611     raise << "opcode '0f' requires a second opcode\n" << end();
    612     return;
    613   }
    614   word op = preprocess_op(inst.words.at(1));
    615   if (!contains_key(Name_0f, op.data)) {
    616     raise << "unknown 2-byte opcode '0f " << op.data << "'\n" << end();
    617     return;
    618   }
    619   check_arguments_0f(inst, op);
    620 }
    621
    622 void check_arguments_f3(const line& inst) {
    623   assert(inst.words.at(0).data == "f3");
    624   if (SIZE(inst.words) == 1) {
    625     raise << "opcode 'f3' requires a second opcode\n" << end();
    626     return;
    627   }
    628   word op = preprocess_op(inst.words.at(1));
    629   if (op.data == "0f") {
    630     word op2 = preprocess_op(inst.words.at(2));
    631     check_arguments_f3_0f(inst, op2);
    632     return;
    633   }
    634   if (!contains_key(Name_f3, op.data)) {
    635     raise << "unknown 2-byte opcode 'f3 " << op.data << "'\n" << end();
    636     return;
    637   }
    638   check_arguments_f3(inst, op);
    639 }
    640
    641 void test_check_missing_disp32_argument() {
    642   Hide_errors = true;
    643   run(
    644       "== code 0x1\n"
    645       "  0f 84  # jmp if ZF to ??\n"
    646   );
    647   CHECK_TRACE_CONTENTS(
    648       "error: '0f 84' (jump disp32 bytes away if equal, if ZF is set): missing disp32 argument\n"
    649   );
    650 }
    651
    652 void test_0f_opcode_with_modrm() {
    653   transform(
    654       "== code 0x1\n"
    655       "0f af/multiply 2/mod/*+disp32 5/rm32/ebp 8/disp32 0/r32\n"
    656   );
    657   CHECK_TRACE_DOESNT_CONTAIN_ERRORS();
    658 }
    659
    660 :(before "End Globals")
    661 map</*op*/string, /*bitvector*/uint8_t> Permitted_arguments_0f;
    662 :(before "End Init Permitted Operands")
    663 //// Class D: just op and disp32
    664 //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    665 //  0     0     1      |0       0     0     0
    666 put_new(Permitted_arguments_0f, "82", 0x10);
    667 put_new(Permitted_arguments_0f, "83", 0x10);
    668 put_new(Permitted_arguments_0f, "84", 0x10);
    669 put_new(Permitted_arguments_0f, "85", 0x10);
    670 put_new(Permitted_arguments_0f, "86", 0x10);
    671 put_new(Permitted_arguments_0f, "87", 0x10);
    672 put_new(Permitted_arguments_0f, "8c", 0x10);
    673 put_new(Permitted_arguments_0f, "8d", 0x10);
    674 put_new(Permitted_arguments_0f, "8e", 0x10);
    675 put_new(Permitted_arguments_0f, "8f", 0x10);
    676
    677 //// Class M: using ModR/M byte
    678 //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    679 //  0     0     0      |0       0     0     1
    680 put_new(Permitted_arguments_0f, "2f", 0x01);  // compare floats
    681 put_new(Permitted_arguments_0f, "af", 0x01);  // multiply ints
    682 // setcc
    683 put_new(Permitted_arguments_0f, "92", 0x01);
    684 put_new(Permitted_arguments_0f, "93", 0x01);
    685 put_new(Permitted_arguments_0f, "94", 0x01);
    686 put_new(Permitted_arguments_0f, "95", 0x01);
    687 put_new(Permitted_arguments_0f, "96", 0x01);
    688 put_new(Permitted_arguments_0f, "97", 0x01);
    689 put_new(Permitted_arguments_0f, "9c", 0x01);
    690 put_new(Permitted_arguments_0f, "9d", 0x01);
    691 put_new(Permitted_arguments_0f, "9e", 0x01);
    692 put_new(Permitted_arguments_0f, "9f", 0x01);
    693
    694 :(before "End Globals")
    695 map</*op*/string, /*bitvector*/uint8_t> Permitted_arguments_f3;
    696 map</*op*/string, /*bitvector*/uint8_t> Permitted_arguments_f3_0f;
    697 :(before "End Init Permitted Operands")
    698 //// Class M: using ModR/M byte
    699 //  imm32 imm8  disp32 |disp16  disp8 subop modrm
    700 //  0     0     0      |0       0     0     1
    701 put_new(Permitted_arguments_f3_0f, "10", 0x01);  // copy xm32 to x32
    702 put_new(Permitted_arguments_f3_0f, "11", 0x01);  // copy x32 to xm32
    703 put_new(Permitted_arguments_f3_0f, "2a", 0x01);  // convert-to-float
    704 put_new(Permitted_arguments_f3_0f, "2c", 0x01);  // truncate-to-int
    705 put_new(Permitted_arguments_f3_0f, "2d", 0x01);  // convert-to-int
    706 put_new(Permitted_arguments_f3_0f, "51", 0x01);  // square root
    707 put_new(Permitted_arguments_f3_0f, "52", 0x01);  // inverse square root
    708 put_new(Permitted_arguments_f3_0f, "53", 0x01);  // reciprocal
    709 put_new(Permitted_arguments_f3_0f, "58", 0x01);  // add floats
    710 put_new(Permitted_arguments_f3_0f, "59", 0x01);  // multiply floats
    711 put_new(Permitted_arguments_f3_0f, "5c", 0x01);  // subtract floats
    712 put_new(Permitted_arguments_f3_0f, "5d", 0x01);  // minimum of floats
    713 put_new(Permitted_arguments_f3_0f, "5e", 0x01);  // divide floats
    714 put_new(Permitted_arguments_f3_0f, "5f", 0x01);  // maximum of floats
    715
    716 :(code)
    717 void check_arguments_0f(const line& inst, const word& op) {
    718   uint8_t expected_bitvector = get(Permitted_arguments_0f, op.data);
    719   if (HAS(expected_bitvector, MODRM)) {
    720     check_arguments_modrm(inst, op);
    721     compare_bitvector_modrm(inst, expected_bitvector, maybe_name_0f(op));
    722   }
    723   else {
    724     compare_bitvector(inst, CLEAR(expected_bitvector, MODRM), maybe_name_0f(op));
    725   }
    726 }
    727
    728 void check_arguments_f3(const line& inst, const word& op) {
    729   uint8_t expected_bitvector = get(Permitted_arguments_f3, op.data);
    730   if (HAS(expected_bitvector, MODRM)) {
    731     check_arguments_modrm(inst, op);
    732     compare_bitvector_modrm(inst, expected_bitvector, maybe_name_f3(op));
    733   }
    734   else {
    735     compare_bitvector(inst, CLEAR(expected_bitvector, MODRM), maybe_name_f3(op));
    736   }
    737 }
    738
    739 void check_arguments_f3_0f(const line& inst, const word& op) {
    740   uint8_t expected_bitvector = get(Permitted_arguments_f3_0f, op.data);
    741   if (HAS(expected_bitvector, MODRM)) {
    742     check_arguments_modrm(inst, op);
    743     compare_bitvector_modrm(inst, expected_bitvector, maybe_name_f3_0f(op));
    744   }
    745   else {
    746     compare_bitvector(inst, CLEAR(expected_bitvector, MODRM), maybe_name_f3_0f(op));
    747   }
    748 }
    749
    750 string maybe_name_0f(const word& op) {
    751   if (!is_hex_byte(op)) return "";
    752   if (!contains_key(Name_0f, op.data)) return "";
    753   // strip stuff in parens from the name
    754   const string& s = get(Name_0f, op.data);
    755   return " ("+s.substr(0, s.find(" ("))+')';
    756 }
    757
    758 string maybe_name_f3(const word& op) {
    759   if (!is_hex_byte(op)) return "";
    760   if (!contains_key(Name_f3, op.data)) return "";
    761   // strip stuff in parens from the name
    762   const string& s = get(Name_f3, op.data);
    763   return " ("+s.substr(0, s.find(" ("))+')';
    764 }
    765
    766 string maybe_name_f3_0f(const word& op) {
    767   if (!is_hex_byte(op)) return "";
    768   if (!contains_key(Name_f3_0f, op.data)) return "";
    769   // strip stuff in parens from the name
    770   const string& s = get(Name_f3_0f, op.data);
    771   return " ("+s.substr(0, s.find(" ("))+')';
    772 }
    773
    774 string tolower(const char* s) {
    775   ostringstream out;
    776   for (/*nada*/;  *s;  ++s)
    777     out << static_cast<char>(tolower(*s));
    778   return out.str();
    779 }
    780
    781 #undef HAS
    782 #undef SET
    783 #undef CLEAR
    784
    785 :(before "End Includes")
    786 #include<cctype>