diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-02-19 18:25:25 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-02-19 18:25:25 -0800 |
commit | 201b2e6c7ce94c986fe7888a60f0e1ba24a05ce9 (patch) | |
tree | 078579498b5dcbcbfbd07634a5164b68561f3016 | |
parent | 7ec3e3b41eb404dbca4943d87ff14838e1177469 (diff) | |
download | mu-201b2e6c7ce94c986fe7888a60f0e1ba24a05ce9.tar.gz |
796 - jump instructions done
-rw-r--r-- | cpp/.traces/jump | 7 | ||||
-rw-r--r-- | cpp/.traces/jump_backward | 23 | ||||
-rw-r--r-- | cpp/.traces/jump_can_skip_instructions | 7 | ||||
-rw-r--r-- | cpp/.traces/jump_if | 10 | ||||
-rw-r--r-- | cpp/.traces/jump_if_fallthrough | 12 | ||||
-rw-r--r-- | cpp/.traces/jump_unless | 10 | ||||
-rw-r--r-- | cpp/.traces/jump_unless_fallthrough | 12 | ||||
-rw-r--r-- | cpp/015jump | 91 | ||||
-rw-r--r-- | cpp/makefile | 3 |
9 files changed, 170 insertions, 5 deletions
diff --git a/cpp/.traces/jump b/cpp/.traces/jump new file mode 100644 index 00000000..e1c0f664 --- /dev/null +++ b/cpp/.traces/jump @@ -0,0 +1,7 @@ +parse/0: instruction: 10 +parse/0: ingredient: {name: "1", type: 0} +parse/0: instruction: 1 +parse/0: ingredient: {name: "1", type: 0} +parse/0: product: {name: "1", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 1 diff --git a/cpp/.traces/jump_backward b/cpp/.traces/jump_backward new file mode 100644 index 00000000..18a79084 --- /dev/null +++ b/cpp/.traces/jump_backward @@ -0,0 +1,23 @@ +parse/0: instruction: 10 +parse/0: ingredient: {name: "1", type: 0} +parse/0: ingredient: {name: "//", type: 0} +parse/0: ingredient: {name: "0", type: 0} +parse/0: ingredient: {name: "-+", type: 0} +parse/0: instruction: 10 +parse/0: ingredient: {name: "1", type: 0} +parse/0: ingredient: {name: "//", type: 0} +parse/0: ingredient: {name: "|", type: 0} +parse/0: ingredient: {name: "1", type: 0} +parse/0: ingredient: {name: "+-+", type: 0} +parse/0: instruction: 10 +parse/0: ingredient: {name: "-2", type: 0} +parse/0: ingredient: {name: "//", type: 0} +parse/0: ingredient: {name: "2", type: 0} +parse/0: ingredient: {name: "+-->+", type: 0} +parse/0: ingredient: {name: "|", type: 0} +run/0: instruction 0 +run/0: ingredient 0 is 1 +run/0: instruction 2 +run/0: ingredient 0 is -2 +run/0: instruction 1 +run/0: ingredient 0 is 1 diff --git a/cpp/.traces/jump_can_skip_instructions b/cpp/.traces/jump_can_skip_instructions new file mode 100644 index 00000000..e1c0f664 --- /dev/null +++ b/cpp/.traces/jump_can_skip_instructions @@ -0,0 +1,7 @@ +parse/0: instruction: 10 +parse/0: ingredient: {name: "1", type: 0} +parse/0: instruction: 1 +parse/0: ingredient: {name: "1", type: 0} +parse/0: product: {name: "1", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 1 diff --git a/cpp/.traces/jump_if b/cpp/.traces/jump_if new file mode 100644 index 00000000..a496582e --- /dev/null +++ b/cpp/.traces/jump_if @@ -0,0 +1,10 @@ +parse/0: instruction: 11 +parse/0: ingredient: {name: "999", type: 0} +parse/0: ingredient: {name: "1", type: 0} +parse/0: instruction: 1 +parse/0: ingredient: {name: "1", type: 0} +parse/0: product: {name: "1", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 999 +run/0: ingredient 1 is 1 +run/0: jumping to instruction 2 diff --git a/cpp/.traces/jump_if_fallthrough b/cpp/.traces/jump_if_fallthrough new file mode 100644 index 00000000..894392ec --- /dev/null +++ b/cpp/.traces/jump_if_fallthrough @@ -0,0 +1,12 @@ +parse/0: instruction: 11 +parse/0: ingredient: {name: "0", type: 0} +parse/0: ingredient: {name: "1", type: 0} +parse/0: instruction: 1 +parse/0: ingredient: {name: "1", type: 0} +parse/0: product: {name: "123", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 0 +run/0: jump-if fell through +run/0: instruction 1 +run/0: ingredient 0 is 1 +mem/0: storing in location 123 diff --git a/cpp/.traces/jump_unless b/cpp/.traces/jump_unless new file mode 100644 index 00000000..68a57639 --- /dev/null +++ b/cpp/.traces/jump_unless @@ -0,0 +1,10 @@ +parse/0: instruction: 12 +parse/0: ingredient: {name: "0", type: 0} +parse/0: ingredient: {name: "1", type: 0} +parse/0: instruction: 1 +parse/0: ingredient: {name: "1", type: 0} +parse/0: product: {name: "1", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 0 +run/0: ingredient 1 is 1 +run/0: jumping to instruction 2 diff --git a/cpp/.traces/jump_unless_fallthrough b/cpp/.traces/jump_unless_fallthrough new file mode 100644 index 00000000..5d6a5d9e --- /dev/null +++ b/cpp/.traces/jump_unless_fallthrough @@ -0,0 +1,12 @@ +parse/0: instruction: 12 +parse/0: ingredient: {name: "999", type: 0} +parse/0: ingredient: {name: "1", type: 0} +parse/0: instruction: 1 +parse/0: ingredient: {name: "1", type: 0} +parse/0: product: {name: "123", type: 1} +run/0: instruction 0 +run/0: ingredient 0 is 999 +run/0: jump-unless fell through +run/0: instruction 1 +run/0: ingredient 0 is 1 +mem/0: storing in location 123 diff --git a/cpp/015jump b/cpp/015jump index bcac4b6c..99b333dd 100644 --- a/cpp/015jump +++ b/cpp/015jump @@ -23,10 +23,93 @@ recipe main [ :(scenario "jump_backward") recipe main [ - jump 1:offset - jump 1:offset - jump -2:offset -] + jump 1:offset // 0 -+ + jump 1:offset // | 1 +-+ + jump -2:offset // 2 +-->+ | +] // 3 \/ +run: instruction 0 +run: instruction 2 +run: instruction 1 + +:(before "End Globals") +const int JUMP_IF = 11; +:(before "End Primitive Recipe Numbers") +Recipe_number["jump-if"] = JUMP_IF; +Next_recipe_number++; +:(before "End Primitive Recipe Implementations") +case JUMP_IF: { + vector<int> arg0 = read_memory(instructions[pc].ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 0 is " << arg0[0]; + if (!arg0[0]) { + trace("run") << "jump-if fell through"; + break; + } + trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name; + pc += to_int(instructions[pc].ingredients[1].name); + trace("run") << "jumping to instruction " << pc+1; + break; +} + +:(scenario "jump_if") +recipe main [ + jump-if 999:literal 1:offset + 1:integer <- copy 1:literal +] ++run: instruction 0 ++run: ingredient 1 is 1 ++run: jumping to instruction 2 +-run: instruction 1 +-mem: storing in location 1 + +:(scenario "jump_if_fallthrough") +recipe main [ + jump-if 0:literal 1:offset + 123:integer <- copy 1:literal +] ++run: instruction 0 ++run: jump-if fell through ++run: instruction 1 ++mem: storing in location 123 + +:(before "End Globals") +const int JUMP_UNLESS = 12; +:(before "End Primitive Recipe Numbers") +Recipe_number["jump-unless"] = JUMP_UNLESS; +Next_recipe_number++; +:(before "End Primitive Recipe Implementations") +case JUMP_UNLESS: { + vector<int> arg0 = read_memory(instructions[pc].ingredients[0]); + assert(arg0.size() == 1); + trace("run") << "ingredient 0 is " << arg0[0]; + if (arg0[0]) { + trace("run") << "jump-unless fell through"; + break; + } + trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name; + pc += to_int(instructions[pc].ingredients[1].name); + trace("run") << "jumping to instruction " << pc+1; + break; +} + +:(scenario "jump_unless") +recipe main [ + jump-unless 0:literal 1:offset + 1:integer <- copy 1:literal +] ++run: instruction 0 ++run: ingredient 1 is 1 ++run: jumping to instruction 2 +-run: instruction 1 +-mem: storing in location 1 + +:(scenario "jump_unless_fallthrough") +recipe main [ + jump-unless 999:literal 1:offset + 123:integer <- copy 1:literal +] ++run: instruction 0 ++run: ingredient 0 is 999 ++run: jump-unless fell through ++run: instruction 1 ++mem: storing in location 123 diff --git a/cpp/makefile b/cpp/makefile index fe801a95..40eb3873 100644 --- a/cpp/makefile +++ b/cpp/makefile @@ -12,7 +12,7 @@ tangle/tangle: cd tangle && make # auto-generated files; by convention they end in '_list'. -.PHONY: autogenerated_lists +.PHONY: autogenerated_lists clena autogenerated_lists: mu.cc function_list test_list function_list: mu.cc @@ -23,6 +23,7 @@ test_list: mu.cc @grep -h "^[[:space:]]*void test_" mu.cc |perl -pwe 's/^\s*void (.*)\(\) {.*/$$1,/' > test_list @grep -h "^[[:space:]]*TEST(" mu.cc |perl -pwe 's/^\s*TEST\((.*)\)$$/test_$$1,/' >> test_list +clena: clean clean: cd tangle && make clean rm -rf mu.cc mu *_list |