about summary refs log tree commit diff stats
path: root/subx/013direct_addressing.cc
Commit message (Collapse)AuthorAgeFilesLines
* 4695Kartik Agaram2018-10-141-58/+58
|
* 4694Kartik Agaram2018-10-131-43/+43
| | | | Check for duplicate docstrings.
* 4693Kartik Agaram2018-10-131-46/+43
| | | | | | | | Add the standard mnemonic for each opcode. We aren't ever going to have complete docs of the subset of the x86 ISA we support, so we need to help readers cross-correlate with the complete docs.
* 4692 - update online help for subxKartik Agaram2018-10-131-32/+32
| | | | | | It now includes details for 8-bit registers. And we'll just use the classic names for the registers so that the relationships between 8- and 32-bit versions are more obvious.
* 4688Kartik Agaram2018-10-121-35/+35
|
* 4686Kartik Agaram2018-10-121-5/+5
|
* 4634Kartik Agaram2018-10-011-5/+5
|
* 4614 - redo simulated RAMKartik Agaram2018-09-291-1/+2
| | | | | | | | | | | Now simulated 'Memory' isn't just a single flat array. Instead it knows about segments and VMAs. The code segment will always be first, and the data/heap segment will always be second. The brk() syscall knows about the data segment. One nice side-effect is that I no longer need to mess with Memory initialization regardless of where I place my segments.
* 4503Kartik Agaram2018-09-221-3/+15
| | | | Include LEA (load effective address) in the SubX subset of x86 ISA.
* 4578 - subx: implement inc/dec operationsKartik Agaram2018-09-211-0/+121
|
* 4547Kartik Agaram2018-09-161-1/+1
|
* 4537Kartik Agaram2018-09-071-8/+48
| | | | | | | | | | | | | | | Streamline the factorial function; we don't need to save a stack variable into a register before operating on it. All instructions can take a stack variable directly. In the process we found two bugs: a) Opcode f7 was not implemented correctly. It was internally consistent but I'd never validated it against a natively running program. Turns out it encodes multiple instructions, not just 'not'. b) The way we look up imm32 operands was sometimes reading them before disp8/disp32 operands.
* 4527 - reading commandline argumentsKartik Agaram2018-08-301-7/+0
| | | | | | | | | | | The new example ex9 doesn't yet work natively. In the process I've emulated the kernel's role in providing args, implemented a couple of instructions acting on 8-bit operands (useful for ASCII string operations), and begun the start of the standard library (ascii_length is the same as strlen). At the level of SubX we're just only going to support ASCII.
* 4469Kartik Agaram2018-08-031-0/+425
0; 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 */
//: Boolean primitives

:(before "End Primitive Recipe Declarations")
AND,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "and", AND);
:(before "End Primitive Recipe Checks")
case AND: {
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_scalar(inst.ingredients.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'and' requires boolean ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  if (SIZE(inst.products) > 1) {
    raise << maybe(get(Recipe, r).name) << "'and' yields exactly one product in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_boolean(inst.products.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'and' should yield a boolean, but got '" << inst.products.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case AND: {
  bool result = true;
  for (int i = 0;  i < SIZE(ingredients);  ++i)
    result = result && scalar_ingredient(ingredients, i);
  products.resize(1);
  products.at(0).push_back(result);
  break;
}
:(code)
double scalar_ingredient(const vector<vector<double> >& ingredients, int i) {
  if (is_mu_address(current_instruction().ingredients.at(i)))
    return ingredients.at(i).at(/*skip alloc id*/1);
  return ingredients.at(i).at(0);
}

void test_and() {
  run(
      "def main [\n"
      "  1:bool <- copy true\n"
      "  2:bool <- copy false\n"
      "  3:bool <- and 1:bool, 2:bool\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 3\n"
  );
}

void test_and_2() {
  run(
      "def main [\n"
      "  1:bool <- and true, true\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 1\n"
  );
}

void test_and_multiple() {
  run(
      "def main [\n"
      "  1:bool <- and true, true, false\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 1\n"
  );
}

void test_and_multiple_2() {
  run(
      "def main [\n"
      "  1:bool <- and true, true, true\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 1\n"
  );
}

:(before "End Primitive Recipe Declarations")
OR,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "or", OR);
:(before "End Primitive Recipe Checks")
case OR: {
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_scalar(inst.ingredients.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'and' requires boolean ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  if (SIZE(inst.products) > 1) {
    raise << maybe(get(Recipe, r).name) << "'or' yields exactly one product in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_boolean(inst.products.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'or' should yield a boolean, but got '" << inst.products.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case OR: {
  bool result = false;
  for (int i = 0;  i < SIZE(ingredients);  ++i)
    result = result || scalar_ingredient(ingredients, i);
  products.resize(1);
  products.at(0).push_back(result);
  break;
}

:(code)
void test_or() {
  run(
      "def main [\n"
      "  1:bool <- copy true\n"
      "  2:bool <- copy false\n"
      "  3:bool <- or 1:bool, 2:bool\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 3\n"
  );
}

void test_or_2() {
  run(
      "def main [\n"
      "  1:bool <- or false, false\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 1\n"
  );
}

void test_or_multiple() {
  run(
      "def main [\n"
      "  1:bool <- or false, false, false\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 1\n"
  );
}

void test_or_multiple_2() {
  run(
      "def main [\n"
      "  1:bool <- or false, false, true\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 1 in location 1\n"
  );
}

:(before "End Primitive Recipe Declarations")
NOT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "not", NOT);
:(before "End Primitive Recipe Checks")
case NOT: {
  if (SIZE(inst.products) != SIZE(inst.ingredients)) {
    raise << "ingredients and products should match in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_scalar(inst.ingredients.at(i)) && !is_mu_address(inst.ingredients.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'not' requires ingredients that can be interpreted as boolean, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  for (int i = 0;  i < SIZE(inst.products);  ++i) {
    if (is_dummy(inst.products.at(i))) continue;
    if (!is_mu_boolean(inst.products.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'not' should yield a boolean, but got '" << inst.products.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case NOT: {
  products.resize(SIZE(ingredients));
  for (int i = 0;  i < SIZE(ingredients);  ++i) {
    products.at(i).push_back(!scalar_ingredient(ingredients, i));
  }
  break;
}

:(code)
void test_not() {
  run(
      "def main [\n"
      "  1:bool <- copy true\n"
      "  2:bool <- not 1:bool\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 2\n"
  );
}

void test_not_multiple() {
  run(
      "def main [\n"
      "  1:bool, 2:bool, 3:bool <- not true, false, true\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 0 in location 1\n"
      "mem: storing 1 in location 2\n"
      "mem: storing 0 in location 3\n"
  );
}