# Mu - subx/032check_operand_bounds.cc

     1 //:: Check that the different operands of an instruction aren't too large for their bitfields.
     2
     3 :(scenario check_bitfield_sizes)
     4 % Hide_errors = true;
     5 == 0x1
     6 01/add 4/mod 3/rm32 1/r32  # add ECX to EBX
     7 +error: '4/mod' too large to fit in bitfield mod
     8
     9 :(before "End Globals")
    10 map<string, uint32_t> Operand_bound;
    11 :(before "End One-time Setup")
    12 put_new(Operand_bound, "subop", 1<<3);
    13 put_new(Operand_bound, "mod", 1<<2);
    14 put_new(Operand_bound, "rm32", 1<<3);
    15 put_new(Operand_bound, "base", 1<<3);
    16 put_new(Operand_bound, "index", 1<<3);
    17 put_new(Operand_bound, "scale", 1<<2);
    18 put_new(Operand_bound, "r32", 1<<3);
    19 put_new(Operand_bound, "disp8", 1<<8);
    20 put_new(Operand_bound, "disp16", 1<<16);
    21 // no bound needed for disp32
    22 put_new(Operand_bound, "imm8", 1<<8);
    23 // no bound needed for imm32
    24
    25 :(before "Pack Operands(segment code)")
    26 check_operand_bounds(code);
    27 if (trace_contains_errors()) return;
    28 :(code)
    29 void check_operand_bounds(const segment& code) {
    30   trace(99, "transform") << "-- check operand bounds" << end();
    31   for (int i = 0;  i < SIZE(code.lines);  ++i) {
    32     const line& inst = code.lines.at(i);
    33     for (int j = first_operand(inst);  j < SIZE(inst.words);  ++j)
    34       check_operand_bounds(inst.words.at(j));
    35     if (trace_contains_errors()) return;  // stop at the first mal-formed instruction
    36   }
    37 }
    38
    39 void check_operand_bounds(const word& w) {
    40   for (map<string, uint32_t>::iterator p = Operand_bound.begin();  p != Operand_bound.end();  ++p) {
    41     if (!has_operand_metadata(w, p->first)) continue;
    42     if (!looks_like_hex_int(w.data)) continue;  // later transforms are on their own to do their own bounds checking
    43     int32_t x = parse_int(w.data);
    44     if (x >= 0) {
    45       if (static_cast<uint32_t>(x) >= p->second)
    46         raise << "'" << w.original << "' too large to fit in bitfield " << p->first << '\n' << end();
    47     }
    48     else {
    49       // hacky? assuming bound is a power of 2
    50       if (x < -1*static_cast<int32_t>(p->second/2))
    51         raise << "'" << w.original << "' too large to fit in bitfield " << p->first << '\n' << end();
    52     }
    53   }
    54 }