https://github.com/akkartik/mu/blob/main/linux/bootstrap/013direct_addressing.cc
   1 //: operating directly on a register
   2 
   3 :(before "End Initialize Op Names")
   4 put_new(Name, "01", "add r32 to rm32 (add)");
   5 
   6 :(code)
   7 void test_add_r32_to_r32() {
   8   Reg[EAX].i = 0x10;
   9   Reg[EBX].i = 1;
  10   run(
  11       "== code 0x1\n"  // code segment
  12       // op     ModR/M  SIB   displacement  immediate
  13       "  01     d8                                    \n" // add EBX to EAX
  14       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
  15   );
  16   CHECK_TRACE_CONTENTS(
  17       "run: add EBX to r/m32\n"
  18       "run: r/m32 is EAX\n"
  19       "run: storing 0x00000011\n"
  20   );
  21 }
  22 
  23 :(before "End Single-Byte Opcodes")
  24 case 0x01: {  // add r32 to r/m32
  25   uint8_t modrm = next();
  26   uint8_t arg2 = (modrm>>3)&0x7;
  27   trace(Callstack_depth+1, "run") << "add " << rname(arg2) << " to r/m32" << end();
  28   int32_t* signed_arg1 = effective_address(modrm);
  29   int32_t signed_result = *signed_arg1 + Reg[arg2].i;
  30   SF = (signed_result < 0);
  31   ZF = (signed_result == 0);
  32   int64_t signed_full_result = static_cast<int64_t>(*signed_arg1) + Reg[arg2].i;
  33   OF = (signed_result != signed_full_result);
  34   // set CF
  35   uint32_t unsigned_arg1 = static_cast<uint32_t>(*signed_arg1);
  36   uint32_t unsigned_result = unsigned_arg1 + Reg[arg2].u;
  37   uint64_t unsigned_full_result = static_cast<uint64_t>(unsigned_arg1) + Reg[arg2].u;
  38   CF = (unsigned_result != unsigned_full_result);
  39   trace(Callstack_depth+1, "run") << "SF=" << SF << "; ZF=" << ZF << "; CF=" << CF << "; OF=" << OF << end();
  40   *signed_arg1 = signed_result;
  41   trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *signed_arg1 << end();
  42   break;
  43 }
  44 
  45 :(code)
  46 void test_add_r32_to_r32_signed_overflow() {
  47   Reg[EAX].i = INT32_MAX;
  48   Reg[EBX].i = 1;
  49   run(
  50       "== code 0x1\n"  // code segment
  51       // op     ModR/M  SIB   displacement  immediate
  52       "  01     d8                                    \n" // add EBX to EAX
  53       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
  54   );
  55   CHECK_TRACE_CONTENTS(
  56       "run: add EBX to r/m32\n"
  57       "run: r/m32 is EAX\n"
  58       "run: SF=1; ZF=0; CF=0; OF=1\n"
  59       "run: storing 0x80000000\n"
  60   );
  61 }
  62 
  63 void test_add_r32_to_r32_unsigned_overflow() {
  64   Reg[EAX].u = UINT32_MAX;
  65   Reg[EBX].u = 1;
  66   run(
  67       "== code 0x1\n"  // code segment
  68       // op     ModR/M  SIB   displacement  immediate
  69       "  01     d8                                    \n" // add EBX to EAX
  70       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
  71   );
  72   CHECK_TRACE_CONTENTS(
  73       "run: add EBX to r/m32\n"
  74       "run: r/m32 is EAX\n"
  75       "run: SF=0; ZF=1; CF=1; OF=0\n"
  76       "run: storing 0x00000000\n"
  77   );
  78 }
  79 
  80 void test_add_r32_to_r32_unsigned_and_signed_overflow() {
  81   Reg[EAX].i = Reg[EBX].i = INT32_MIN;
  82   run(
  83       "== code 0x1\n"  // code segment
  84       // op     ModR/M  SIB   displacement  immediate
  85       "  01     d8                                    \n" // add EBX to EAX
  86       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
  87   );
  88   CHECK_TRACE_CONTENTS(
  89       "run: add EBX to r/m32\n"
  90       "run: r/m32 is EAX\n"
  91       "run: SF=0; ZF=1; CF=1; OF=1\n"
  92       "run: storing 0x00000000\n"
  93   );
  94 }
  95 
  96 :(code)
  97 // Implement tables 2-2 and 2-3 in the Intel manual, Volume 2.
  98 // We return a pointer so that instructions can write to multiple bytes in
  99 // 'Mem' at once.
 100 // beware: will eventually have side-effects
 101 int32_t* effective_address(uint8_t modrm) {
 102   const uint8_t mod = (modrm>>6);
 103   // ignore middle 3 'reg opcode' bits
 104   const uint8_t rm = modrm & 0x7;
 105   if (mod == 3) {
 106     // mod 3 is just register direct addressing
 107     trace(Callstack_depth+1, "run") << "r/m32 is " << rname(rm) << end();
 108     return &Reg[rm].i;
 109   }
 110   uint32_t addr = effective_address_number(modrm);
 111   trace(Callstack_depth+1, "run") << "effective address contains 0x" << HEXWORD << read_mem_i32(addr) << end();
 112   return mem_addr_i32(addr);
 113 }
 114 
 115 // beware: will eventually have side-effects
 116 uint32_t effective_address_number(uint8_t modrm) {
 117   const uint8_t mod = (modrm>>6);
 118   // ignore middle 3 'reg opcode' bits
 119   const uint8_t rm = modrm & 0x7;
 120   uint32_t addr = 0;
 121   switch (mod) {
 122   case 3:
 123     // mod 3 is just register direct addressing
 124     raise << "unexpected direct addressing mode\n" << end();
 125     return 0;
 126   // End Mod Special-cases(addr)
 127   default:
 128     cerr << "unrecognized mod bits: " << NUM(mod) << '\n';
 129     exit(1);
 130   }
 131   //: other mods are indirect, and they'll set addr appropriately
 132   // Found effective_address(addr)
 133   return addr;
 134 }
 135 
 136 string rname(uint8_t r) {
 137   switch (r) {
 138   case 0: return "EAX";
 139   case 1: return "ECX";
 140   case 2: return "EDX";
 141   case 3: return "EBX";
 142   case 4: return "ESP";
 143   case 5: return "EBP";
 144   case 6: return "ESI";
 145   case 7: return "EDI";
 146   default: raise << "invalid register " << r << '\n' << end();  return "";
 147   }
 148 }
 149 
 150 //:: subtract
 151 
 152 :(before "End Initialize Op Names")
 153 put_new(Name, "29", "subtract r32 from rm32 (sub)");
 154 
 155 :(code)
 156 void test_subtract_r32_from_r32() {
 157   Reg[EAX].i = 10;
 158   Reg[EBX].i = 1;
 159   run(
 160       "== code 0x1\n"  // code segment
 161       // op     ModR/M  SIB   displacement  immediate
 162       "  29     d8                                    \n"  // subtract EBX from EAX
 163       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
 164   );
 165   CHECK_TRACE_CONTENTS(
 166       "run: subtract EBX from r/m32\n"
 167       "run: r/m32 is EAX\n"
 168       "run: storing 0x00000009\n"
 169   );
 170 }
 171 
 172 :(before "End Single-Byte Opcodes")
 173 case 0x29: {  // subtract r32 from r/m32
 174   const uint8_t modrm = next();
 175   const uint8_t arg2 = (modrm>>3)&0x7;
 176   trace(Callstack_depth+1, "run") << "subtract " << rname(arg2) << " from r/m32" << end();
 177   int32_t* signed_arg1 = effective_address(modrm);
 178   int32_t signed_result = *signed_arg1 - Reg[arg2].i;
 179   SF = (signed_result < 0);
 180   ZF = (signed_result == 0);
 181   int64_t signed_full_result = static_cast<int64_t>(*signed_arg1) - Reg[arg2].i;
 182   OF = (signed_result != signed_full_result);
 183   // set CF
 184   uint32_t unsigned_arg1 = static_cast<uint32_t>(*signed_arg1);
 185   uint32_t unsigned_result = unsigned_arg1 - Reg[arg2].u;
 186   uint64_t unsigned_full_result = static_cast<uint64_t>(unsigned_arg1) - Reg[arg2].u;
 187   CF = (unsigned_result != unsigned_full_result);
 188   trace(Callstack_depth+1, "run") << "SF=" << SF << "; ZF=" << ZF << "; CF=" << CF << "; OF=" << OF << end();
 189   *signed_arg1 = signed_result;
 190   trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *signed_arg1 << end();
 191   break;
 192 }
 193 
 194 :(code)
 195 void test_subtract_r32_from_r32_signed_overflow() {
 196   Reg[EAX].i = INT32_MIN;
 197   Reg[EBX].i = INT32_MAX;
 198   run(
 199       "== code 0x1\n"  // code segment
 200       // op     ModR/M  SIB   displacement  immediate
 201       "  29     d8                                    \n"  // subtract EBX from EAX
 202       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
 203   );
 204   CHECK_TRACE_CONTENTS(
 205       "run: subtract EBX from r/m32\n"
 206       "run: r/m32 is EAX\n"
 207       "run: SF=0; ZF=0; CF=0; OF=1\n"
 208       "run: storing 0x00000001\n"
 209   );
 210 }
 211 
 212 void test_subtract_r32_from_r32_unsigned_overflow() {
 213   Reg[EAX].i = 0;
 214   Reg[EBX].i = 1;
 215   run(
 216       "== code 0x1\n"  // code segment
 217       // op     ModR/M  SIB   displacement  immediate
 218       "  29     d8                                    \n"  // subtract EBX from EAX
 219       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
 220   );
 221   CHECK_TRACE_CONTENTS(
 222       "run: subtract EBX from r/m32\n"
 223       "run: r/m32 is EAX\n"
 224       "run: SF=1; ZF=0; CF=1; OF=0\n"
 225       "run: storing 0xffffffff\n"
 226   );
 227 }
 228 
 229 void test_subtract_r32_from_r32_signed_and_unsigned_overflow() {
 230   Reg[EAX].i = 0;
 231   Reg[EBX].i = INT32_MIN;
 232   run(
 233       "== code 0x1\n"  // code segment
 234       // op     ModR/M  SIB   displacement  immediate
 235       "  29     d8                                    \n"  // subtract EBX from EAX
 236       // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX)
 237   );
 238   CHECK_TRACE_CONTENTS(
 239       "run: subtract EBX from r/m32\n"
 240       "run: r/m32 is EAX\n"
 241       "run: SF=1; ZF=0; CF=1; OF=1\n"
 242       "run: storing 0x80000000\n"
 243   );
 244 }
 245 
 246 //:: multiply
 247 
 248 :(before "End Initialize Op Names")
 249 put_new(Name, "f7", "negate/multiply/divide rm32 (with EAX and EDX if necessary) depending on subop (neg/mul/idiv)");
 250 
 251 :(code)
 252 void test_multiply_EAX_by_r32() {
 253   Reg[EAX].i = 4;
 254   Reg[ECX].i = 3;
 255   run(
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: Construct types out of their constituent fields.

void test_merge() {
  run(
      "container foo [\n"
      "  x:num\n"
      "  y:num\n"
      "]\n"
      "def main [\n"
      "  1:foo <- merge 3, 4\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 3 in location 1\n"
      "mem: storing 4 in location 2\n"
  );
}

:(before "End Primitive Recipe Declarations")
MERGE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "merge", MERGE);
:(before "End Primitive Recipe Checks")
case MERGE: {
  // type-checking in a separate transform below
  break;
}
:(before "End Primitive Recipe Implementations")
case MERGE: {
  products.resize(1);
  for (int i = 0;  i < SIZE(ingredients);  ++i)
    for (int j = 0;  j < SIZE(ingredients.at(i));  ++j)
      products.at(0).push_back(ingredients.at(i).at(j));
  break;
}

//: type-check 'merge' to avoid interpreting numbers as addresses

:(code)
void test_merge_check() {
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_merge_check_missing_element() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too few ingredients in '1:point <- merge 3'\n"
  );
}

void test_merge_check_extra_element() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3, 4, 5\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too many ingredients in '1:point <- merge 3, 4, 5'\n"
  );
}

//: We want to avoid causing memory corruption, but other than that we want to
//: be flexible in how we construct containers of containers. It should be
//: equally easy to define a container out of primitives or intermediate
//: container fields.

void test_merge_check_recursive_containers() {
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "  1:point-number <- merge 1:point, 5\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_merge_check_recursive_containers_2() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "  2:point-number <- merge 1:point\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too few ingredients in '2:point-number <- merge 1:point'\n"
  );
}

void test_merge_check_recursive_containers_3() {
  run(
      "def main [\n"
      "  1:point-number <- merge 3, 4, 5\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

void test_merge_check_recursive_containers_4() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point-number <- merge 3, 4\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: too few ingredients in '1:point-number <- merge 3, 4'\n"
  );
}

void test_merge_check_reflexive() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:point <- merge 3, 4\n"
      "  2:point <- merge 1:point\n"
      "]\n"
  );
  CHECK_TRACE_COUNT("error", 0);
}

//: Since a container can be merged in several ways, we need to be able to
//: backtrack through different possibilities. Later we'll allow creating
//: exclusive containers which contain just one of rather than all of their
//: elements. That will also require backtracking capabilities. Here's the
//: state we need to maintain for backtracking:

:(before "End Types")
struct merge_check_point {
  reagent container;
  int container_element_index;
  merge_check_point(const reagent& c, int i) :container(c), container_element_index(i) {}
};

struct merge_check_state {
  stack<merge_check_point> data;
};

:(before "End Checks")
Transform.push_back(check_merge_calls);  // idempotent
:(code)
void check_merge_calls(const recipe_ordinal r) {
  const recipe& caller = get(Recipe, r);
  trace(101, "transform") << "--- type-check merge instructions in recipe " << caller.name << end();
  for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    const instruction& inst = caller.steps.at(i);
    if (inst.name != "merge") continue;
    if (SIZE(inst.products) != 1) {
      raise << maybe(caller.name) << "'merge' should yield a single product in '" << to_original_string(inst) << "'\n" << end();
      continue;
    }
    reagent/*copy*/ product = inst.products.at(0);
    // Update product While Type-checking Merge
    const type_tree* product_base_type = product.type->atom ? product.type : product.type->left;
    assert(product_base_type->atom);
    if (product_base_type->value == 0 || !contains_key(Type, product_base_type->value)) {
      raise << maybe(caller.name) << "'merge' should yield a container in '" << to_original_string(inst) << "'\n" << end();
      continue;
    }
    const type_info& info = get(Type, product_base_type->value);
    if (info.kind != CONTAINER && info.kind != EXCLUSIVE_CONTAINER) {
      raise << maybe(caller.name) << "'merge' should yield a container in '" << to_original_string(inst) << "'\n" << end();
      continue;
    }
    check_merge_call(inst.ingredients, product, caller, inst);
  }
}

void check_merge_call(const vector<reagent>& ingredients, const reagent& product, const recipe& caller, const instruction& inst) {
  int ingredient_index = 0;
  merge_check_state state;
  state.data.push(merge_check_point(product, 0));
  while (true) {
    assert(!state.data.empty());
    trace(102, "transform") << ingredient_index << " vs " << SIZE(ingredients) << end();
    if (ingredient_index >= SIZE(ingredients)) {
      raise << maybe(caller.name) << "too few ingredients in '" << to_original_string(inst) << "'\n" << end();
      return;
    }
    reagent& container = state.data.top().container;
    if (!container.type) return;  // error handled elsewhere
    const type_tree* top_root_type = container.type->atom ? container.type : container.type->left;
    assert(top_root_type->atom);
    type_info& container_info = get(Type, top_root_type->value);
    switch (container_info.kind) {
      case CONTAINER: {
        // degenerate case: merge with the same type always succeeds
        if (state.data.top().container_element_index == 0 && types_coercible(container, inst.ingredients.at(ingredient_index)))
          return;
        const reagent& expected_ingredient = element_type(container.type, state.data.top().container_element_index);
        trace(102, "transform") << "checking container " << to_string(container) << " || " << to_string(expected_ingredient) << " vs ingredient " << ingredient_index << end();
        // if the current element is the ingredient we expect, move on to the next element/ingredient
        if (types_coercible(expected_ingredient, ingredients.at(ingredient_index))) {
          ++ingredient_index;
          ++state.data.top().container_element_index;
          while (state.data.top().container_element_index >= SIZE(get(Type, get_base_type(state.data.top().container.type)->value).elements)) {
            state.data.pop();
            if (state.data.empty()) {
              if (ingredient_index < SIZE(ingredients))
                raise << maybe(caller.name) << "too many ingredients in '" << to_original_string(inst) << "'\n" << end();
              return;
            }
            ++state.data.top().container_element_index;
          }
        }
        // if not, maybe it's a field of the current element
        else {
          // no change to ingredient_index
          state.data.push(merge_check_point(expected_ingredient, 0));
        }
        break;
      }
      // End check_merge_call Special-cases
      default: {
        if (!types_coercible(container, ingredients.at(ingredient_index))) {
          raise << maybe(caller.name) << "incorrect type of ingredient " << ingredient_index << " in '" << to_original_string(inst) << "'\n" << end();
          raise << "  (expected '" << debug_string(container) << "')\n" << end();
          raise << "  (got '" << debug_string(ingredients.at(ingredient_index)) << "')\n" << end();
          return;
        }
        ++ingredient_index;
        // ++state.data.top().container_element_index;  // unnecessary, but wouldn't do any harm
        do {
          state.data.pop();
          if (state.data.empty()) {
            if (ingredient_index < SIZE(ingredients))
              raise << maybe(caller.name) << "too many ingredients in '" << to_original_string(inst) << "'\n" << end();
            return;
          }
          ++state.data.top().container_element_index;
        } while (state.data.top().container_element_index >= SIZE(get(Type, get_base_type(state.data.top().container.type)->value).elements));
      }
    }
  }
  // never gets here
  assert(false);
}

//: replaced in a later layer
//: todo: find some clean way to take this call completely out of this layer
const type_tree* get_base_type(const type_tree* t) {
  return t;
}

void test_merge_check_product() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  1:num <- merge 3\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: 'merge' should yield a container in '1:num <- merge 3'\n"
  );
}

:(before "End Includes")
#include <stack>
using std::stack;
L598" class="LineNr"> 598 "== code 0x1\n" // code segment 599 // op ModR/M SIB displacement immediate 600 " d3 fb \n" // shift EBX right by CL bits, while preserving sign 601 // ModR/M in binary: 11 (direct mode) 111 (subop shift right arithmetic) 011 (dest EBX) 602 ); 603 CHECK_TRACE_CONTENTS( 604 "run: operate on r/m32\n" 605 "run: r/m32 is EBX\n" 606 "run: subop: shift right by CL bits, while preserving sign\n" 607 // result: -2 608 "run: storing 0xfffffffe\n" 609 ); 610 } 611 612 //:: shift right logical 613 614 :(code) 615 void test_shift_right_logical_r32_with_cl() { 616 Reg[EBX].i = 26; 617 Reg[ECX].i = 1; 618 run( 619 "== code 0x1\n" // code segment 620 // op ModR/M SIB displacement immediate 621 " d3 eb \n" // shift EBX right by CL bits, while padding zeroes 622 // ModR/M in binary: 11 (direct mode) 101 (subop shift right logical) 011 (dest EBX) 623 ); 624 CHECK_TRACE_CONTENTS( 625 "run: operate on r/m32\n" 626 "run: r/m32 is EBX\n" 627 "run: subop: shift right by CL bits, while padding zeroes\n" 628 // result: 13 629 "run: storing 0x0000000d\n" 630 ); 631 } 632 633 :(before "End Op d3 Subops") 634 case 5: { // shift right r/m32 by CL, padding zeroes 635 trace(Callstack_depth+1, "run") << "subop: shift right by CL bits, while padding zeroes" << end(); 636 uint8_t count = Reg[ECX].u & 0x1f; 637 // OF is only defined if count is 1 638 if (count == 1) { 639 bool msb = (*arg1 & 0x80000000) >> 1; 640 bool pnsb = (*arg1 & 0x40000000); 641 OF = (msb != pnsb); 642 } 643 uint32_t* uarg1 = reinterpret_cast<uint32_t*>(arg1); 644 *uarg1 = (*uarg1 >> count); 645 ZF = (*uarg1 == 0); 646 // result is always positive by definition 647 SF = false; 648 // CF undefined 649 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *arg1 << end(); 650 break; 651 } 652 653 :(code) 654 void test_shift_right_logical_odd_r32_with_cl() { 655 Reg[EBX].i = 27; 656 Reg[ECX].i = 1; 657 run( 658 "== code 0x1\n" // code segment 659 // op ModR/M SIB displacement immediate 660 " d3 eb \n" // shift EBX right by CL bits, while padding zeroes 661 // ModR/M in binary: 11 (direct mode) 101 (subop shift right logical) 011 (dest EBX) 662 ); 663 CHECK_TRACE_CONTENTS( 664 "run: operate on r/m32\n" 665 "run: r/m32 is EBX\n" 666 "run: subop: shift right by CL bits, while padding zeroes\n" 667 // result: 13 668 "run: storing 0x0000000d\n" 669 ); 670 } 671 672 void test_shift_right_logical_negative_r32_with_cl() { 673 Reg[EBX].i = 0xfffffffd; 674 Reg[ECX].i = 1; 675 run( 676 "== code 0x1\n" // code segment 677 // op ModR/M SIB displacement immediate 678 " d3 eb \n" // shift EBX right by CL bits, while padding zeroes 679 // ModR/M in binary: 11 (direct mode) 101 (subop shift right logical) 011 (dest EBX) 680 ); 681 CHECK_TRACE_CONTENTS( 682 "run: operate on r/m32\n" 683 "run: r/m32 is EBX\n" 684 "run: subop: shift right by CL bits, while padding zeroes\n" 685 "run: storing 0x7ffffffe\n" 686 ); 687 } 688 689 //:: and 690 691 :(before "End Initialize Op Names") 692 put_new(Name, "21", "rm32 = bitwise AND of r32 with rm32 (and)"); 693 694 :(code) 695 void test_and_r32_with_r32() { 696 Reg[EAX].i = 0x0a0b0c0d; 697 Reg[EBX].i = 0x000000ff; 698 run( 699 "== code 0x1\n" // code segment 700 // op ModR/M SIB displacement immediate 701 " 21 d8 \n" // and EBX with destination EAX 702 // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX) 703 ); 704 CHECK_TRACE_CONTENTS( 705 "run: and EBX with r/m32\n" 706 "run: r/m32 is EAX\n" 707 "run: storing 0x0000000d\n" 708 ); 709 } 710 711 :(before "End Single-Byte Opcodes") 712 case 0x21: { // and r32 with r/m32 713 const uint8_t modrm = next(); 714 const uint8_t arg2 = (modrm>>3)&0x7; 715 trace(Callstack_depth+1, "run") << "and " << rname(arg2) << " with r/m32" << end(); 716 // bitwise ops technically operate on unsigned numbers, but it makes no 717 // difference 718 int32_t* signed_arg1 = effective_address(modrm); 719 *signed_arg1 &= Reg[arg2].i; 720 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *signed_arg1 << end(); 721 SF = (*signed_arg1 >> 31); 722 ZF = (*signed_arg1 == 0); 723 CF = false; 724 OF = false; 725 trace(Callstack_depth+1, "run") << "SF=" << SF << "; ZF=" << ZF << "; CF=" << CF << "; OF=" << OF << end(); 726 break; 727 } 728 729 //:: or 730 731 :(before "End Initialize Op Names") 732 put_new(Name, "09", "rm32 = bitwise OR of r32 with rm32 (or)"); 733 734 :(code) 735 void test_or_r32_with_r32() { 736 Reg[EAX].i = 0x0a0b0c0d; 737 Reg[EBX].i = 0xa0b0c0d0; 738 run( 739 "== code 0x1\n" // code segment 740 // op ModR/M SIB displacement immediate 741 " 09 d8 \n" // or EBX with destination EAX 742 // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX) 743 ); 744 CHECK_TRACE_CONTENTS( 745 "run: or EBX with r/m32\n" 746 "run: r/m32 is EAX\n" 747 "run: storing 0xaabbccdd\n" 748 ); 749 } 750 751 :(before "End Single-Byte Opcodes") 752 case 0x09: { // or r32 with r/m32 753 const uint8_t modrm = next(); 754 const uint8_t arg2 = (modrm>>3)&0x7; 755 trace(Callstack_depth+1, "run") << "or " << rname(arg2) << " with r/m32" << end(); 756 // bitwise ops technically operate on unsigned numbers, but it makes no 757 // difference 758 int32_t* signed_arg1 = effective_address(modrm); 759 *signed_arg1 |= Reg[arg2].i; 760 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *signed_arg1 << end(); 761 SF = (*signed_arg1 >> 31); 762 ZF = (*signed_arg1 == 0); 763 CF = false; 764 OF = false; 765 trace(Callstack_depth+1, "run") << "SF=" << SF << "; ZF=" << ZF << "; CF=" << CF << "; OF=" << OF << end(); 766 break; 767 } 768 769 //:: xor 770 771 :(before "End Initialize Op Names") 772 put_new(Name, "31", "rm32 = bitwise XOR of r32 with rm32 (xor)"); 773 774 :(code) 775 void test_xor_r32_with_r32() { 776 Reg[EAX].i = 0x0a0b0c0d; 777 Reg[EBX].i = 0xaabbc0d0; 778 run( 779 "== code 0x1\n" // code segment 780 // op ModR/M SIB displacement immediate 781 " 31 d8 \n" // xor EBX with destination EAX 782 // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX) 783 ); 784 CHECK_TRACE_CONTENTS( 785 "run: xor EBX with r/m32\n" 786 "run: r/m32 is EAX\n" 787 "run: storing 0xa0b0ccdd\n" 788 ); 789 } 790 791 :(before "End Single-Byte Opcodes") 792 case 0x31: { // xor r32 with r/m32 793 const uint8_t modrm = next(); 794 const uint8_t arg2 = (modrm>>3)&0x7; 795 trace(Callstack_depth+1, "run") << "xor " << rname(arg2) << " with r/m32" << end(); 796 // bitwise ops technically operate on unsigned numbers, but it makes no 797 // difference 798 int32_t* signed_arg1 = effective_address(modrm); 799 *signed_arg1 ^= Reg[arg2].i; 800 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *signed_arg1 << end(); 801 SF = (*signed_arg1 >> 31); 802 ZF = (*signed_arg1 == 0); 803 CF = false; 804 OF = false; 805 trace(Callstack_depth+1, "run") << "SF=" << SF << "; ZF=" << ZF << "; CF=" << CF << "; OF=" << OF << end(); 806 break; 807 } 808 809 //:: not 810 811 :(code) 812 void test_not_r32() { 813 Reg[EBX].i = 0x0f0f00ff; 814 run( 815 "== code 0x1\n" // code segment 816 // op ModR/M SIB displacement immediate 817 " f7 d3 \n" // not EBX 818 // ModR/M in binary: 11 (direct mode) 010 (subop not) 011 (dest EBX) 819 ); 820 CHECK_TRACE_CONTENTS( 821 "run: operate on r/m32\n" 822 "run: r/m32 is EBX\n" 823 "run: subop: not\n" 824 "run: storing 0xf0f0ff00\n" 825 ); 826 } 827 828 :(before "End Op f7 Subops") 829 case 2: { // not r/m32 830 trace(Callstack_depth+1, "run") << "subop: not" << end(); 831 *arg1 = ~(*arg1); 832 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *arg1 << end(); 833 // no flags affected 834 break; 835 } 836 837 //:: compare (cmp) 838 839 :(before "End Initialize Op Names") 840 put_new(Name, "39", "compare: set SF if rm32 < r32 (cmp)"); 841 842 :(code) 843 void test_compare_r32_with_r32_greater() { 844 Reg[EAX].i = 0x0a0b0c0d; 845 Reg[EBX].i = 0x0a0b0c07; 846 run( 847 "== code 0x1\n" // code segment 848 // op ModR/M SIB displacement immediate 849 " 39 d8 \n" // compare EAX with EBX 850 // ModR/M in binary: 11 (direct mode) 011 (rhs EBX) 000 (lhs EAX) 851 ); 852 CHECK_TRACE_CONTENTS( 853 "run: compare r/m32 with EBX\n" 854 "run: r/m32 is EAX\n" 855 "run: SF=0; ZF=0; CF=0; OF=0\n" 856 ); 857 } 858 859 :(before "End Single-Byte Opcodes") 860 case 0x39: { // set SF if r/m32 < r32 861 const uint8_t modrm = next(); 862 const uint8_t reg2 = (modrm>>3)&0x7; 863 trace(Callstack_depth+1, "run") << "compare r/m32 with " << rname(reg2) << end(); 864 const int32_t* signed_arg1 = effective_address(modrm); 865 const int32_t signed_difference = *signed_arg1 - Reg[reg2].i; 866 SF = (signed_difference < 0); 867 ZF = (signed_difference == 0); 868 const int64_t signed_full_difference = static_cast<int64_t>(*signed_arg1) - Reg[reg2].i; 869 OF = (signed_difference != signed_full_difference); 870 // set CF 871 const uint32_t unsigned_arg1 = static_cast<uint32_t>(*signed_arg1); 872 const uint32_t unsigned_difference = unsigned_arg1 - Reg[reg2].u; 873 const uint64_t unsigned_full_difference = static_cast<uint64_t>(unsigned_arg1) - Reg[reg2].u; 874 CF = (unsigned_difference != unsigned_full_difference); 875 trace(Callstack_depth+1, "run") << "SF=" << SF << "; ZF=" << ZF << "; CF=" << CF << "; OF=" << OF << end(); 876 break; 877 } 878 879 :(code) 880 void test_compare_r32_with_r32_lesser_unsigned_and_signed() { 881 Reg[EAX].i = 0x0a0b0c07; 882 Reg[EBX].i = 0x0a0b0c0d; 883 run( 884 "== code 0x1\n" // code segment 885 // op ModR/M SIB displacement immediate 886 " 39 d8 \n" // compare EAX with EBX 887 // ModR/M in binary: 11 (direct mode) 011 (rhs EBX) 000 (lhs EAX) 888 ); 889 CHECK_TRACE_CONTENTS( 890 "run: compare r/m32 with EBX\n" 891 "run: r/m32 is EAX\n" 892 "run: SF=1; ZF=0; CF=1; OF=0\n" 893 ); 894 } 895 896 void test_compare_r32_with_r32_lesser_unsigned_and_signed_due_to_overflow() { 897 Reg[EAX].i = INT32_MAX; 898 Reg[EBX].i = INT32_MIN; 899 run( 900 "== code 0x1\n" // code segment 901 // op ModR/M SIB displacement immediate 902 " 39 d8 \n" // compare EAX with EBX 903 // ModR/M in binary: 11 (direct mode) 011 (rhs EBX) 000 (lhs EAX) 904 ); 905 CHECK_TRACE_CONTENTS( 906 "run: compare r/m32 with EBX\n" 907 "run: r/m32 is EAX\n" 908 "run: SF=1; ZF=0; CF=1; OF=1\n" 909 ); 910 } 911 912 void test_compare_r32_with_r32_lesser_signed() { 913 Reg[EAX].i = -1; 914 Reg[EBX].i = 1; 915 run( 916 "== code 0x1\n" // code segment 917 // op ModR/M SIB displacement immediate 918 " 39 d8 \n" // compare EAX with EBX 919 // ModR/M in binary: 11 (direct mode) 011 (rhs EBX) 000 (lhs EAX) 920 ); 921 CHECK_TRACE_CONTENTS( 922 "run: compare r/m32 with EBX\n" 923 "run: r/m32 is EAX\n" 924 "run: SF=1; ZF=0; CF=0; OF=0\n" 925 ); 926 } 927 928 void test_compare_r32_with_r32_lesser_unsigned() { 929 Reg[EAX].i = 1; 930 Reg[EBX].i = -1; 931 run( 932 "== code 0x1\n" // code segment 933 // op ModR/M SIB displacement immediate 934 " 39 d8 \n" // compare EAX with EBX 935 // ModR/M in binary: 11 (direct mode) 011 (rhs EBX) 000 (lhs EAX) 936 ); 937 CHECK_TRACE_CONTENTS( 938 "run: compare r/m32 with EBX\n" 939 "run: r/m32 is EAX\n" 940 "run: SF=0; ZF=0; CF=1; OF=0\n" 941 ); 942 } 943 944 void test_compare_r32_with_r32_equal() { 945 Reg[EAX].i = 0x0a0b0c0d; 946 Reg[EBX].i = 0x0a0b0c0d; 947 run( 948 "== code 0x1\n" // code segment 949 // op ModR/M SIB displacement immediate 950 " 39 d8 \n" // compare EAX and EBX 951 // ModR/M in binary: 11 (direct mode) 011 (rhs EBX) 000 (lhs EAX) 952 ); 953 CHECK_TRACE_CONTENTS( 954 "run: compare r/m32 with EBX\n" 955 "run: r/m32 is EAX\n" 956 "run: SF=0; ZF=1; CF=0; OF=0\n" 957 ); 958 } 959 960 //:: copy (mov) 961 962 :(before "End Initialize Op Names") 963 put_new(Name, "89", "copy r32 to rm32 (mov)"); 964 965 :(code) 966 void test_copy_r32_to_r32() { 967 Reg[EBX].i = 0xaf; 968 run( 969 "== code 0x1\n" // code segment 970 // op ModR/M SIB displacement immediate 971 " 89 d8 \n" // copy EBX to EAX 972 // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX) 973 ); 974 CHECK_TRACE_CONTENTS( 975 "run: copy EBX to r/m32\n" 976 "run: r/m32 is EAX\n" 977 "run: storing 0x000000af\n" 978 ); 979 } 980 981 :(before "End Single-Byte Opcodes") 982 case 0x89: { // copy r32 to r/m32 983 const uint8_t modrm = next(); 984 const uint8_t rsrc = (modrm>>3)&0x7; 985 trace(Callstack_depth+1, "run") << "copy " << rname(rsrc) << " to r/m32" << end(); 986 int32_t* dest = effective_address(modrm); 987 *dest = Reg[rsrc].i; // Write multiple elements of vector<uint8_t> at once. Assumes sizeof(int) == 4 on the host as well. 988 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *dest << end(); 989 break; 990 } 991 992 //:: xchg 993 994 :(before "End Initialize Op Names") 995 put_new(Name, "87", "swap the contents of r32 and rm32 (xchg)"); 996 997 :(code) 998 void test_xchg_r32_with_r32() { 999 Reg[EBX].i = 0xaf; 1000 Reg[EAX].i = 0x2e; 1001 run( 1002 "== code 0x1\n" // code segment 1003 // op ModR/M SIB displacement immediate 1004 " 87 d8 \n" // exchange EBX with EAX 1005 // ModR/M in binary: 11 (direct mode) 011 (src EBX) 000 (dest EAX) 1006 ); 1007 CHECK_TRACE_CONTENTS( 1008 "run: exchange EBX with r/m32\n" 1009 "run: r/m32 is EAX\n" 1010 "run: storing 0x000000af in r/m32\n" 1011 "run: storing 0x0000002e in EBX\n" 1012 ); 1013 } 1014 1015 :(before "End Single-Byte Opcodes") 1016 case 0x87: { // exchange r32 with r/m32 1017 const uint8_t modrm = next(); 1018 const uint8_t reg2 = (modrm>>3)&0x7; 1019 trace(Callstack_depth+1, "run") << "exchange " << rname(reg2) << " with r/m32" << end(); 1020 int32_t* arg1 = effective_address(modrm); 1021 const int32_t tmp = *arg1; 1022 *arg1 = Reg[reg2].i; 1023 Reg[reg2].i = tmp; 1024 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << *arg1 << " in r/m32" << end(); 1025 trace(Callstack_depth+1, "run") << "storing 0x" << HEXWORD << Reg[reg2].i << " in " << rname(reg2) << end(); 1026 break; 1027 } 1028 1029 //:: increment 1030 1031 :(before "End Initialize Op Names") 1032 put_new(Name, "40", "increment EAX (inc)"); 1033 put_new(Name, "41", "increment ECX (inc)"); 1034 put_new(Name, "42", "increment EDX (inc)"); 1035 put_new(Name, "43", "increment EBX (inc)"); 1036 put_new(Name, "44", "increment ESP (inc)"); 1037 put_new(Name, "45", "increment EBP (inc)"); 1038 put_new(Name, "46", "increment ESI (inc)"); 1039 put_new(Name, "47", "increment EDI (inc)"); 1040 1041 :(code) 1042 void test_increment_r32() { 1043 Reg[ECX].u = 0x1f; 1044 run( 1045 "== code 0x1\n" // code segment 1046 // op ModR/M SIB displacement immediate 1047 " 41 \n" // increment ECX 1048 ); 1049 CHECK_TRACE_CONTENTS( 1050 "run: increment ECX\n" 1051 "run: storing value 0x00000020\n" 1052 ); 1053 } 1054 1055 :(before "End Single-Byte Opcodes") 1056 case 0x40: 1057 case 0x41: 1058 case 0x42: 1059 case 0x43: 1060 case 0x44: 1061 case 0x45: 1062 case 0x46: 1063 case 0x47: { // increment r32 1064 const uint8_t reg = op & 0x7; 1065 trace(Callstack_depth+1, "run") << "increment " << rname(reg) << end(); 1066 ++Reg[reg].u; 1067 trace(Callstack_depth+1, "run") << "storing value 0x" << HEXWORD << Reg[reg].u << end(); 1068 break; 1069 } 1070 1071 :(before "End Initialize Op Names") 1072 put_new(Name, "ff", "increment/decrement/jump/push/call rm32 based on subop (inc/dec/jmp/push/call)"); 1073 1074 :(code) 1075 void test_increment_rm32() { 1076 Reg[EAX].u = 0x20; 1077 run( 1078 "== code 0x1\n" // code segment 1079 // op ModR/M SIB displacement immediate 1080 " ff c0 \n" // increment EAX 1081 // ModR/M in binary: 11 (direct mode) 000 (subop inc) 000 (EAX) 1082 ); 1083 CHECK_TRACE_CONTENTS( 1084 "run: increment r/m32\n" 1085 "run: r/m32 is EAX\n" 1086 "run: storing value 0x00000021\n" 1087 ); 1088 } 1089 1090 :(before "End Single-Byte Opcodes") 1091 case 0xff: { 1092 const uint8_t modrm = next(); 1093 const uint8_t subop = (modrm>>3)&0x7; // middle 3 'reg opcode' bits 1094 switch (subop) { 1095 case 0: { // increment r/m32 1096 trace(Callstack_depth+1, "run") << "increment r/m32" << end(); 1097 int32_t* arg = effective_address(modrm); 1098 ++*arg; 1099 trace(Callstack_depth+1, "run") << "storing value 0x" << HEXWORD << *arg << end(); 1100 break; 1101 } 1102 default: 1103 cerr << "unrecognized subop for ff: " << HEXBYTE << NUM(subop) << '\n'; 1104 exit(1); 1105 // End Op ff Subops 1106 } 1107 break; 1108 } 1109 1110 //:: decrement 1111 1112 :(before "End Initialize Op Names") 1113 put_new(Name, "48", "decrement EAX (dec)"); 1114 put_new(Name, "49", "decrement ECX (dec)"); 1115 put_new(Name, "4a", "decrement EDX (dec)"); 1116 put_new(Name, "4b", "decrement EBX (dec)"); 1117 put_new(Name, "4c", "decrement ESP (dec)"); 1118 put_new(Name, "4d", "decrement EBP (dec)"); 1119 put_new(Name, "4e", "decrement ESI (dec)"); 1120 put_new(Name, "4f", "decrement EDI (dec)"); 1121 1122 :(code) 1123 void test_decrement_r32() { 1124 Reg[ECX].u = 0x1f; 1125 run( 1126 "== code 0x1\n" // code segment 1127 // op ModR/M SIB displacement immediate 1128 " 49 \n" // decrement ECX 1129 ); 1130 CHECK_TRACE_CONTENTS( 1131 "run: decrement ECX\n" 1132 "run: storing value 0x0000001e\n" 1133 ); 1134 } 1135 1136 :(before "End Single-Byte Opcodes") 1137 case 0x48: 1138 case 0x49: 1139 case 0x4a: 1140 case 0x4b: 1141 case 0x4c: 1142 case 0x4d: 1143 case 0x4e: 1144 case 0x4f: { // decrement r32 1145 const uint8_t reg = op & 0x7; 1146 trace(Callstack_depth+1, "run") << "decrement " << rname(reg) << end(); 1147 --Reg[reg].u; 1148 trace(Callstack_depth+1, "run") << "storing value 0x" << HEXWORD << Reg[reg].u << end(); 1149 break; 1150 } 1151 1152 :(code) 1153 void test_decrement_rm32() { 1154 Reg[EAX].u = 0x20; 1155 run( 1156 "== code 0x1\n" // code segment 1157 // op ModR/M SIB displacement immediate 1158 " ff c8 \n" // decrement EAX 1159 // ModR/M in binary: 11 (direct mode) 001 (subop inc) 000 (EAX) 1160 ); 1161 CHECK_TRACE_CONTENTS( 1162 "run: decrement r/m32\n" 1163 "run: r/m32 is EAX\n" 1164 "run: storing value 0x0000001f\n" 1165 ); 1166 } 1167 1168 :(before "End Op ff Subops") 1169 case 1: { // decrement r/m32 1170 trace(Callstack_depth+1, "run") << "decrement r/m32" << end(); 1171 int32_t* arg = effective_address(modrm); 1172 --*arg; 1173 trace(Callstack_depth+1, "run") << "storing value 0x" << HEXWORD << *arg << end(); 1174 break; 1175 } 1176 1177 //:: push 1178 1179 :(before "End Initialize Op Names") 1180 put_new(Name, "50", "push EAX to stack (push)"); 1181 put_new(Name, "51", "push ECX to stack (push)"); 1182 put_new(Name, "52", "push EDX to stack (push)"); 1183 put_new(Name, "53", "push EBX to stack (push)"); 1184 put_new(Name, "54", "push ESP to stack (push)"); 1185 put_new(Name, "55", "push EBP to stack (push)"); 1186 put_new(Name, "56", "push ESI to stack (push)"); 1187 put_new(Name, "57", "push EDI to stack (push)"); 1188 1189 :(code) 1190 void test_push_r32() { 1191 Mem.push_back(vma(0xbd000000)); // manually allocate memory 1192 Reg[ESP].u = 0xbd000008; 1193 Reg[EBX].i = 0x0000000a; 1194 run( 1195 "== code 0x1\n" // code segment 1196 // op ModR/M SIB displacement immediate 1197 " 53 \n" // push EBX to stack 1198 ); 1199 CHECK_TRACE_CONTENTS( 1200 "run: push EBX\n" 1201 "run: decrementing ESP to 0xbd000004\n" 1202 "run: pushing value 0x0000000a\n" 1203 ); 1204 } 1205 1206 :(before "End Single-Byte Opcodes") 1207 case 0x50: 1208 case 0x51: 1209 case 0x52: 1210 case 0x53: 1211 case 0x54: 1212 case 0x55: 1213 case 0x56: 1214 case 0x57: { // push r32 to stack 1215 uint8_t reg = op & 0x7; 1216 trace(Callstack_depth+1, "run") << "push " << rname(reg) << end(); 1217 //? cerr << "push: " << NUM(reg) << ": " << Reg[reg].u << " => " << Reg[ESP].u << '\n'; 1218 push(Reg[reg].u); 1219 break; 1220 } 1221 1222 //:: pop 1223 1224 :(before "End Initialize Op Names") 1225 put_new(Name, "58", "pop top of stack to EAX (pop)"); 1226 put_new(Name, "59", "pop top of stack to ECX (pop)"); 1227 put_new(Name, "5a", "pop top of stack to EDX (pop)"); 1228 put_new(Name, "5b", "pop top of stack to EBX (pop)"); 1229 put_new(Name, "5c", "pop top of stack to ESP (pop)"); 1230 put_new(Name, "5d", "pop top of stack to EBP (pop)"); 1231 put_new(Name, "5e", "pop top of stack to ESI (pop)"); 1232 put_new(Name, "5f", "pop top of stack to EDI (pop)"); 1233 1234 :(code) 1235 void test_pop_r32() { 1236 Mem.push_back(vma(0xbd000000)); // manually allocate memory 1237 Reg[ESP].u = 0xbd000008; 1238 write_mem_i32(0xbd000008, 0x0000000a); // ..before this write 1239 run( 1240 "== code 0x1\n" // code segment 1241 // op ModR/M SIB displacement immediate 1242 " 5b \n" // pop stack to EBX 1243 "== data 0x2000\n" // data segment 1244 "0a 00 00 00\n" // 0xa 1245 ); 1246 CHECK_TRACE_CONTENTS( 1247 "run: pop into EBX\n" 1248 "run: popping value 0x0000000a\n" 1249 "run: incrementing ESP to 0xbd00000c\n" 1250 ); 1251 } 1252 1253 :(before "End Single-Byte Opcodes") 1254 case 0x58: 1255 case 0x59: 1256 case 0x5a: 1257 case 0x5b: 1258 case 0x5c: 1259 case 0x5d: 1260 case 0x5e: 1261 case 0x5f: { // pop stack into r32 1262 const uint8_t reg = op & 0x7; 1263 trace(Callstack_depth+1, "run") << "pop into " << rname(reg) << end(); 1264 //? cerr << "pop from " << Reg[ESP].u << '\n'; 1265 Reg[reg].u = pop(); 1266 //? cerr << "=> " << NUM(reg) << ": " << Reg[reg].u << '\n'; 1267 break; 1268 } 1269 :(code) 1270 uint32_t pop() { 1271 const uint32_t result = read_mem_u32(Reg[ESP].u); 1272 trace(Callstack_depth+1, "run") << "popping value 0x" << HEXWORD << result << end(); 1273 Reg[ESP].u += 4; 1274 trace(Callstack_depth+1, "run") << "incrementing ESP to 0x" << HEXWORD << Reg[ESP].u << end(); 1275 assert(Reg[ESP].u < AFTER_STACK); 1276 return result; 1277 } 1278 1279 :(before "End Includes") 1280 #include <climits>