diff options
author | Kartik Agaram <github@akkartik.com> | 2019-08-25 15:11:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-25 15:11:33 -0700 |
commit | e3f1ce673554e64f9ec1710e9ddc9629a0d9d076 (patch) | |
tree | 1960bed25ea27372cfa8344a68f671b1148dc3c7 | |
parent | b60d8c920ef988e8058c1e31bd0907643b60cb5d (diff) | |
parent | 57fdbeb2b67a648c178b8dd1813a6a9f06e96c98 (diff) | |
download | mu-e3f1ce673554e64f9ec1710e9ddc9629a0d9d076.tar.gz |
Merge pull request #35 from akkartik/desugar
Desugaring pass
-rw-r--r-- | 038---literal_strings.cc | 65 | ||||
-rw-r--r-- | 059stop.subx | 8 | ||||
-rw-r--r-- | 061read-byte.subx | 38 | ||||
-rw-r--r-- | 064write-byte.subx | 86 | ||||
-rw-r--r-- | 067parse-hex.subx | 4 | ||||
-rw-r--r-- | 073next-token.subx | 270 | ||||
-rw-r--r-- | 078table.subx | 1696 | ||||
-rwxr-xr-x | apps/assort | bin | 34384 -> 37257 bytes | |||
-rwxr-xr-x | apps/crenshaw2-1 | bin | 24788 -> 30991 bytes | |||
-rw-r--r-- | apps/crenshaw2-1.subx | 23 | ||||
-rwxr-xr-x | apps/crenshaw2-1b | bin | 25347 -> 31550 bytes | |||
-rw-r--r-- | apps/crenshaw2-1b.subx | 23 | ||||
-rwxr-xr-x | apps/desugar | bin | 0 -> 48096 bytes | |||
-rw-r--r-- | apps/desugar.subx | 4117 | ||||
-rwxr-xr-x | apps/dquotes | bin | 40940 -> 43813 bytes | |||
-rw-r--r-- | apps/dquotes.subx | 7 | ||||
-rwxr-xr-x | apps/factorial | bin | 23704 -> 30003 bytes | |||
-rwxr-xr-x | apps/handle | bin | 24558 -> 30857 bytes | |||
-rwxr-xr-x | apps/hex | bin | 36939 -> 39650 bytes | |||
-rw-r--r-- | apps/hex.subx | 32 | ||||
-rwxr-xr-x | apps/pack | bin | 47070 -> 49943 bytes | |||
-rw-r--r-- | apps/pack.subx | 2 | ||||
-rw-r--r-- | apps/subx-common.subx | 1013 | ||||
-rwxr-xr-x | apps/survey | bin | 43605 -> 46540 bytes | |||
-rw-r--r-- | apps/survey.subx | 21 | ||||
-rwxr-xr-x | apps/tests | bin | 33196 -> 36069 bytes | |||
-rwxr-xr-x | build | 10 | ||||
-rwxr-xr-x | clean | 1 | ||||
-rwxr-xr-x | run_one_test | 8 | ||||
-rwxr-xr-x | test_apps | 25 |
30 files changed, 6315 insertions, 1134 deletions
diff --git a/038---literal_strings.cc b/038---literal_strings.cc index ecc80176..48d46108 100644 --- a/038---literal_strings.cc +++ b/038---literal_strings.cc @@ -12,7 +12,7 @@ void test_transform_literal_string() { CHECK_TRACE_CONTENTS( "transform: -- move literal strings to data segment\n" "transform: adding global variable '__subx_global_1' containing \"test\"\n" - "transform: instruction after transform: 'b8 __subx_global_1'\n" + "transform: line after transform: 'b8 __subx_global_1'\n" ); } @@ -26,37 +26,46 @@ Transform.push_back(transform_literal_strings); :(before "End Globals") int Next_auto_global = 1; +:(before "End Reset") +Next_auto_global = 1; :(code) void transform_literal_strings(program& p) { trace(3, "transform") << "-- move literal strings to data segment" << end(); if (p.segments.empty()) return; - segment& code = *find(p, "code"); - segment& data = *find(p, "data"); - for (int i = 0; i < SIZE(code.lines); ++i) { - line& inst = code.lines.at(i); - for (int j = 0; j < SIZE(inst.words); ++j) { - word& curr = inst.words.at(j); - if (curr.data.at(0) != '"') continue; - ostringstream global_name; - global_name << "__subx_global_" << Next_auto_global; - ++Next_auto_global; - add_global_to_data_segment(global_name.str(), curr, data); - curr.data = global_name.str(); + vector<line> new_lines; + for (int s = 0; s < SIZE(p.segments); ++s) { + segment& seg = p.segments.at(s); + trace(99, "transform") << "segment '" << seg.name << "'" << end(); + for (int i = 0; i < SIZE(seg.lines); ++i) { +//? cerr << seg.name << '/' << i << '\n'; + line& line = seg.lines.at(i); + for (int j = 0; j < SIZE(line.words); ++j) { + word& curr = line.words.at(j); + if (curr.data.at(0) != '"') continue; + ostringstream global_name; + global_name << "__subx_global_" << Next_auto_global; + ++Next_auto_global; + add_global_to_data_segment(global_name.str(), curr, new_lines); + curr.data = global_name.str(); + } + trace(99, "transform") << "line after transform: '" << data_to_string(line) << "'" << end(); } - trace(99, "transform") << "instruction after transform: '" << data_to_string(inst) << "'" << end(); } + segment* data = find(p, "data"); + if (data) + data->lines.insert(data->lines.end(), new_lines.begin(), new_lines.end()); } -void add_global_to_data_segment(const string& name, const word& value, segment& data) { +void add_global_to_data_segment(const string& name, const word& value, vector<line>& out) { trace(99, "transform") << "adding global variable '" << name << "' containing " << value.data << end(); // emit label - data.lines.push_back(label(name)); + out.push_back(label(name)); // emit size for size-prefixed array - data.lines.push_back(line()); - emit_hex_bytes(data.lines.back(), SIZE(value.data)-/*skip quotes*/2, 4/*bytes*/); + out.push_back(line()); + emit_hex_bytes(out.back(), SIZE(value.data)-/*skip quotes*/2, 4/*bytes*/); // emit data byte by byte - data.lines.push_back(line()); - line& curr = data.lines.back(); + out.push_back(line()); + line& curr = out.back(); for (int i = /*skip start quote*/1; i < SIZE(value.data)-/*skip end quote*/1; ++i) { char c = value.data.at(i); curr.words.push_back(word()); @@ -81,6 +90,21 @@ void test_instruction_with_string_literal() { CHECK_TRACE_COUNT("parse2", 3); } +void test_string_literal_in_data_segment() { + run( + "== code 0x1\n" + "b8/copy X/imm32\n" + "== data 0x2000\n" + "X:\n" + "\"test\"/imm32\n" + ); + CHECK_TRACE_CONTENTS( + "transform: -- move literal strings to data segment\n" + "transform: adding global variable '__subx_global_1' containing \"test\"\n" + "transform: line after transform: '__subx_global_1'\n" + ); +} + :(before "End Line Parsing Special-cases(line_data -> l)") if (line_data.find('"') != string::npos) { // can cause false-positives, but we can handle them parse_instruction_character_by_character(line_data, l); @@ -133,6 +157,7 @@ void parse_instruction_character_by_character(const string& line_data, vector<li if (c == '"') break; } result.words.back().data = d.str(); + result.words.back().original = d.str(); // slurp metadata ostringstream m; while (!isspace(in.peek()) && has_data(in)) { // peek can sometimes trigger eof(), so do it first diff --git a/059stop.subx b/059stop.subx index dbe8a663..f4e90707 100644 --- a/059stop.subx +++ b/059stop.subx @@ -39,7 +39,8 @@ # Configure an exit-descriptor for a call pushing 'nbytes' bytes of args to # the stack. -# Ugly that we need to know the size of args, but so it goes. +# Ugly that we need to know the size of args. Don't allocate variables between +# tailor-exit-descriptor and the call it's for. tailor-exit-descriptor: # ed : (address exit-descriptor), nbytes : int -> <void> # . prolog 55/push-EBP @@ -123,7 +124,8 @@ test-stop-skips-returns-on-exit: # right place for it, available only as long as it's legal to use. Once this # containing function returns we'll need a new exit descriptor. # var ed/EAX : (address exit-descriptor) - 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # subtract from ESP + 68/push 0/imm32 + 68/push 0/imm32 89/copy 3/mod/direct 0/rm32/EAX . . . 4/r32/ESP . . # copy ESP to EAX # Size the exit-descriptor precisely for the next call below, to _test-stop-1. # tailor-exit-descriptor(ed, 4) @@ -143,7 +145,7 @@ test-stop-skips-returns-on-exit: # restore args 58/pop-to-EAX # check that _test-stop-1 tried to call exit(1) - # check-ints-equal(ed->value, 2, msg) # i.e. stop was called with value 1 + # . check-ints-equal(ed->value, 2, msg) # i.e. stop was called with value 1 # . . push args 68/push "F - test-stop-skips-returns-on-exit"/imm32 68/push 2/imm32 diff --git a/061read-byte.subx b/061read-byte.subx index 99e2babe..5b6aa90f 100644 --- a/061read-byte.subx +++ b/061read-byte.subx @@ -290,4 +290,42 @@ _test-buffered-file: # data 00 00 00 00 00 00 # 6 bytes +_test-input-stream: + # current write index + 0/imm32 + # current read index + 0/imm32 + # length + 0x100/imm32 # 256 bytes + # data (16 lines x 16 bytes/line) + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +# a test buffered file for _test-input-stream +_test-input-buffered-file: + # file descriptor or (address stream) + _test-input-stream/imm32 + # current write index + 0/imm32 + # current read index + 0/imm32 + # length + 6/imm32 + # data + 00 00 00 00 00 00 # 6 bytes + # . . vim:nowrap:textwidth=0 diff --git a/064write-byte.subx b/064write-byte.subx index 057b9164..deda5279 100644 --- a/064write-byte.subx +++ b/064write-byte.subx @@ -285,4 +285,90 @@ test-append-byte-single: # . end c3/return +== data + +_test-output-stream: + # current write index + 0/imm32 + # current read index + 0/imm32 + # length + 0x200/imm32 # 512 bytes + # data (32 lines x 16 bytes/line) + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +# a test buffered file for _test-output-stream +_test-output-buffered-file: + # file descriptor or (address stream) + _test-output-stream/imm32 + # current write index + 0/imm32 + # current read index + 0/imm32 + # length + 6/imm32 + # data + 00 00 00 00 00 00 # 6 bytes + +_test-error-stream: + # current write index + 0/imm32 + # current read index + 0/imm32 + # line + 0x80/imm32 # 128 bytes + # data (8 lines x 16 bytes/line) + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 + +# a test buffered file for _test-error-stream +_test-error-buffered-file: + # file descriptor or (address stream) + _test-error-stream/imm32 + # current write index + 0/imm32 + # current read index + 0/imm32 + # length + 6/imm32 + # data + 00 00 00 00 00 00 # 6 bytes + # . . vim:nowrap:textwidth=0 diff --git a/067parse-hex.subx b/067parse-hex.subx index 45d168d9..185e471f 100644 --- a/067parse-hex.subx +++ b/067parse-hex.subx @@ -621,10 +621,10 @@ test-parse-hex-int-negative: e8/call parse-hex-int/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # check-ints-equal(EAX, 0xfffffffd, msg) + # check-ints-equal(EAX, -3, msg) # . . push args 68/push "F - test-parse-hex-int-negative"/imm32 - 68/push 0xfffffffd/imm32 + 68/push -3/imm32 50/push-EAX # . . call e8/call check-ints-equal/disp32 diff --git a/073next-token.subx b/073next-token.subx index 942d9878..a1dd0c4c 100644 --- a/073next-token.subx +++ b/073next-token.subx @@ -461,6 +461,95 @@ test-skip-chars-matching-none: # end c3/return +skip-chars-matching-whitespace: # in : (address stream) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + 51/push-ECX + 53/push-EBX + 56/push-ESI + # ESI = in + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # ECX = in->read + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 . # copy *(ESI+4) to ECX + # EBX = in->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 3/r32/EBX . . # copy *ESI to EBX +$skip-chars-matching-whitespace:loop: + # if (in->read >= in->write) break + 39/compare 3/mod/direct 1/rm32/ECX . . . 3/r32/EBX . . # compare ECX with EBX + 7d/jump-if-greater-or-equal $skip-chars-matching-whitespace:end/disp8 + # EAX = in->data[in->read] + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy byte at *(ESI+ECX+12) to AL + # if (EAX == ' ') goto body + 3d/compare-EAX-and 0x20/imm32/space + 74/jump-if-equal $skip-chars-matching-whitespace:body/disp8 + # if (EAX == '\n') goto body + 3d/compare-EAX-and 0x0a/imm32/newline + 74/jump-if-equal $skip-chars-matching-whitespace:body/disp8 + # if (EAX == '\t') goto body + 3d/compare-EAX-and 0x09/imm32/tab + 74/jump-if-equal $skip-chars-matching-whitespace:body/disp8 + # if (EAX != '\r') break + 3d/compare-EAX-and 0x0d/imm32/cr + 75/jump-if-not-equal $skip-chars-matching-whitespace:end/disp8 +$skip-chars-matching-whitespace:body: + # ++in->read + 41/increment-ECX + eb/jump $skip-chars-matching-whitespace:loop/disp8 +$skip-chars-matching-whitespace:end: + # persist in->read + 89/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 . # copy ECX to *(ESI+4) + # . restore registers + 5e/pop-to-ESI + 5b/pop-to-EBX + 59/pop-to-ECX + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-chars-matching-whitespace: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # write(_test-stream, " \nab") + # . . push args + 68/push " \nab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # skip-chars-matching-whitespace(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching-whitespace"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-EAX _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # end + c3/return + # minor fork of 'skip-chars-matching' skip-chars-not-matching: # in : (address stream), delimiter : byte # . prolog @@ -674,6 +763,44 @@ $skip-chars-not-matching-whitespace:end: 5d/pop-to-EBP c3/return +test-skip-chars-not-matching-whitespace: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # write(_test-stream, "ab\n") + # . . push args + 68/push "ab\n"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # skip-chars-not-matching-whitespace(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-not-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-whitespace"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-EAX _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # end + c3/return + skip-chars-matching-in-slice: # curr : (address byte), end : (address byte), delimiter : byte -> curr/EAX # . prolog 55/push-EBP @@ -769,6 +896,78 @@ test-skip-chars-matching-in-slice-none: # end c3/return +skip-chars-matching-whitespace-in-slice: # in : (address stream) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 53/push-EBX + # EAX = curr + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to EAX + # ECX = end + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0xc/disp8 . # copy *(EBP+12) to ECX + # EBX = 0 + 31/xor 3/mod/direct 3/rm32/EBX . . . 3/r32/EBX . . # clear EBX +$skip-chars-matching-whitespace-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX + 0f 83/jump-if-greater-or-equal-unsigned $skip-chars-matching-in-slice:end/disp32 + # EBX = *curr + 8a/copy-byte 0/mod/indirect 0/rm32/EAX . . . 3/r32/BL . . # copy byte at *EAX to BL + # if (*curr == ' ') goto body + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x20/imm32/space # compare EBX + 74/jump-if-equal $skip-chars-matching-whitespace-in-slice:body/disp8 + # if (*curr == '\n') goto body + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x0a/imm32/newline # compare EBX + 74/jump-if-equal $skip-chars-matching-whitespace-in-slice:body/disp8 + # if (*curr == '\t') goto body + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x09/imm32/tab # compare EBX + 74/jump-if-equal $skip-chars-matching-whitespace-in-slice:body/disp8 + # if (*curr != '\r') break + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x0d/imm32/cr # compare EBX + 75/jump-if-not-equal $skip-chars-matching-whitespace-in-slice:end/disp8 +$skip-chars-matching-whitespace-in-slice:body: + # ++curr + 40/increment-EAX + eb/jump $skip-chars-matching-whitespace-in-slice:loop/disp8 +$skip-chars-matching-whitespace-in-slice:end: + # . restore registers + 5b/pop-to-EBX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-chars-matching-whitespace-in-slice: + # (EAX..ECX) = " \nab" + b8/copy-to-EAX " \nab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # EAX = skip-chars-matching-whitespace-in-slice(EAX, ECX) + # . . push args + 51/push-ECX + 50/push-EAX + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(ECX-EAX, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching-whitespace-in-slice"/imm32 + 68/push 2/imm32 + # . . push ECX-EAX + 29/subtract 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # subtract EAX from ECX + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # end + c3/return + # minor fork of 'skip-chars-matching-in-slice' skip-chars-not-matching-in-slice: # curr : (address byte), end : (address byte), delimiter : byte -> curr/EAX # . prolog @@ -894,4 +1093,75 @@ test-skip-chars-not-matching-in-slice-all: # end c3/return +skip-chars-not-matching-whitespace-in-slice: # in : (address stream) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 53/push-EBX + # EAX = curr + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to EAX + # ECX = end + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0xc/disp8 . # copy *(EBP+12) to ECX + # EBX = 0 + 31/xor 3/mod/direct 3/rm32/EBX . . . 3/r32/EBX . . # clear EBX +$skip-chars-not-matching-whitespace-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # compare EAX with ECX + 0f 83/jump-if-greater-or-equal-unsigned $skip-chars-not-matching-in-slice:end/disp32 + # EBX = *curr + 8a/copy-byte 0/mod/indirect 0/rm32/EAX . . . 3/r32/BL . . # copy byte at *EAX to BL + # if (*curr == ' ') break + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x20/imm32/space # compare EBX + 74/jump-if-equal $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # if (*curr == '\n') break + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x0a/imm32/newline # compare EBX + 74/jump-if-equal $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # if (*curr == '\t') break + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x09/imm32/tab # compare EBX + 74/jump-if-equal $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # if (*curr == '\r') break + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0x0d/imm32/cr # compare EBX + 74/jump-if-equal $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # ++curr + 40/increment-EAX + eb/jump $skip-chars-not-matching-whitespace-in-slice:loop/disp8 +$skip-chars-not-matching-whitespace-in-slice:end: + # . restore registers + 5b/pop-to-EBX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-chars-not-matching-whitespace-in-slice: + # (EAX..ECX) = "ab\n" + b8/copy-to-EAX "ab\n"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # EAX = skip-chars-not-matching-whitespace-in-slice(EAX, ECX) + # . . push args + 51/push-ECX + 50/push-EAX + # . . call + e8/call skip-chars-not-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(ECX-EAX, 1, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-whitespace-in-slice"/imm32 + 68/push 1/imm32 + # . . push ECX-EAX + 29/subtract 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # subtract EAX from ECX + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # end + c3/return + # . . vim:nowrap:textwidth=0 diff --git a/078table.subx b/078table.subx new file mode 100644 index 00000000..6518789c --- /dev/null +++ b/078table.subx @@ -0,0 +1,1696 @@ +# A table is a stream of (key, value) rows. +# +# Each row consists of a 4-byte key (address to a string) and a variable-size +# value. +# +# Accessing the table performs a linear scan for a key string, and always +# requires passing in the row size. +# +# Table primitives have the form <variant>(stream, <arg>, row-size, ...) -> address/EAX +# +# The following table shows available options for <variant>: +# if not found: | arg=string arg=slice +# ------------------------+--------------------------------------------------- +# abort | get get-slice +# insert key | get-or-insert leaky-get-or-insert-slice +# stop | get-or-stop get-slice-or-stop +# return null | maybe-get maybe-get-slice +# Some variants may take extra args. + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes + +# if no row is found, abort +get: # table : (address stream {string, _}), key : (address string), row-size : int, abort-message-prefix : (address string) -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if string-equal?(key, *curr) + # return curr+4 + # curr += row-size + # abort + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$get:search-loop: + # if (curr >= max) abort + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $get:abort/disp8 + # if (string-equal?(key, *curr)) return curr+4 + # . EAX = string-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $get:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $get:end/disp8 +$get:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $get:search-loop/disp8 +$get:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$get:abort: + # . _write(2/stderr, abort-message-prefix) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . _write(2/stderr, error) + # . . push args + 68/push ": get: key not found: "/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . _write(2/stderr, key) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . _write(2/stderr, "\n") + # . . push args + 68/push "\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-get: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # - setup: create a table with a couple of keys + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # insert(table, "data", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "data"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get:check1: + # EAX = get(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get:check2: + # EAX = get(table, "data", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "data"/imm32 + 51/push-ECX + # . . call + e8/call get/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 12, msg) + # . check-ints-equal(EAX - table, 24, msg) + # . . push args + 68/push "F - test-get/1"/imm32 + 68/push 0x18/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, abort +get-slice: # table : (address stream {string, _}), key : (address slice), row-size : int, abort-message-prefix : (address string) -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if slice-equal?(key, *curr) + # return curr+4 + # curr += row-size + # abort + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$get-slice:search-loop: + # if (curr >= max) abort + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $get-slice:abort/disp8 + # if (slice-equal?(key, *curr)) return curr+4 + # . EAX = slice-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $get-slice:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $get-slice:end/disp8 +$get-slice:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $get-slice:search-loop/disp8 +$get-slice:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$get-slice:abort: + # . _write(2/stderr, abort-message-prefix) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . _write(2/stderr, error) + # . . push args + 68/push ": get-slice: key not found: "/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-slice-buffered(Stderr, key) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + 68/push Stderr/imm32 + # . . call + e8/call write-slice-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . flush(Stderr) + # . . push args + 68/push Stderr/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . _write(2/stderr, "\n") + # . . push args + 68/push "\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-get-slice: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # - setup: create a table with a couple of keys + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # insert(table, "data", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "data"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-slice:check1: + # (EAX..EDX) = "code" + b8/copy-to-EAX "code"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX + 05/add-to-EAX 4/imm32 + # var slice/EDX = {EAX, EDX} + 52/push-EDX + 50/push-EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # EAX = get-slice(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-slice/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-slice:check2: + # (EAX..EDX) = "data" + b8/copy-to-EAX "data"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX + 05/add-to-EAX 4/imm32 + # var slice/EDX = {EAX, EDX} + 52/push-EDX + 50/push-EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # EAX = get-slice(table, "data" slice, 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 12, msg) + # . check-ints-equal(EAX - table, 24, msg) + # . . push args + 68/push "F - test-get-slice/1"/imm32 + 68/push 0x18/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-slice:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, save 'key' to the next available row +# if there are no rows free, abort +# return the address of the value +# Beware: assume keys are immutable; they're inserted by reference +# TODO: pass in an allocation descriptor +get-or-insert: # table : (address stream {string, _}), key : (address string), row-size : int -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if string-equal?(key, *curr) + # return curr+4 + # curr += row-size + # if table->write >= table->length + # abort + # zero-out(max, row-size) + # *max = key + # table->write += row-size + # return max+4 + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$get-or-insert:search-loop: + # if (curr >= max) break + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $get-or-insert:not-found/disp8 + # if (string-equal?(key, *curr)) return curr+4 + # . EAX = string-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $get-or-insert:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $get-or-insert:end/disp8 +$get-or-insert:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $get-or-insert:search-loop/disp8 +$get-or-insert:not-found: + # result/EAX = 0 + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + # if (table->write >= table->length) abort + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX + 3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(ESI+8) + 73/jump-if-greater-or-equal-unsigned $get-or-insert:abort/disp8 + # zero-out(max, row-size) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 . # push *(EBP+16) + 52/push-EDX + # . . call + e8/call zero-out/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # *max = key + # . EAX = key + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX + # . *max = EAX + 89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX + # table->write += row-size + # . EAX = row-size + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX + # . table->write += EAX + 01/add 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # add EAX to *ESI + # return max+4 + # . EAX = max + 89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX + # . EAX += 4 + 05/add-to-EAX 4/imm32 +$get-or-insert:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$get-or-insert:abort: + # . _write(2/stderr, error) + # . . push args + 68/push "get-or-insert: table is full\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-get-or-insert: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX +$test-get-or-insert:first-call: + # - start with an empty table, insert one key, verify that it was inserted + # EAX = get-or-insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-or-insert/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-insert:check2: + # check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-get-or-insert/1"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-get-or-insert/2"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-insert:second-call: + # - insert the same key again, verify that it was reused + # EAX = get-or-insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-or-insert/3"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # no new row inserted + # . check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-get-or-insert/4"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-get-or-insert/5"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-insert:third-call: + # - insert a new key, verify that it was inserted + # EAX = get-or-insert(table, "data", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "data"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # table gets a new row + # check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned + # . check-ints-equal(EAX - table, 24, msg) + # . . push args + 68/push "F - test-get-or-insert/6"/imm32 + 68/push 0x18/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(table->write, 2 rows = 16, msg) + # . . push args + 68/push "F - test-get-or-insert/7"/imm32 + 68/push 0x10/imm32/two-rows + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data+8, "data", msg) + # check-string-equal(*(table+20), "data", msg) + # . . push args + 68/push "F - test-get-or-insert/8"/imm32 + 68/push "data"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-insert:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, save 'key' in the next available row +# if there are no rows free, abort +# WARNING: leaks memory +# TODO: pass in an allocation descriptor +leaky-get-or-insert-slice: # table : (address stream {string, _}), key : (address slice), row-size : int -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if slice-equal?(key, *curr) + # return curr+4 + # curr += row-size + # if table->write >= table->length + # abort + # zero-out(max, row-size) + # *max = slice-to-string(Heap, key) + # table->write += row-size + # return max+4 + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$leaky-get-or-insert-slice:search-loop: + # if (curr >= max) break + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $leaky-get-or-insert-slice:not-found/disp8 + # if (slice-equal?(key, *curr)) return curr+4 + # . EAX = slice-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $leaky-get-or-insert-slice:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $leaky-get-or-insert-slice:end/disp8 +$leaky-get-or-insert-slice:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $leaky-get-or-insert-slice:search-loop/disp8 +$leaky-get-or-insert-slice:not-found: + # result/EAX = 0 + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + # if (table->write >= table->length) abort + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX + 3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(ESI+8) + 7d/jump-if-greater-or-equal $leaky-get-or-insert-slice:abort/disp8 + # zero-out(max, row-size) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 . # push *(EBP+16) + 52/push-EDX + # . . call + e8/call zero-out/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # *max = slice-to-string(Heap, key) + # . EAX = slice-to-string(Heap, key) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + 68/push Heap/imm32 + # . . call + e8/call slice-to-string/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . *max = EAX + 89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX + # table->write += row-size + # . EAX = row-size + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX + # . table->write += EAX + 01/add 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # add EAX to *ESI + # return max+4 + # . EAX = max + 89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX + # . EAX += 4 + 05/add-to-EAX 4/imm32 +$leaky-get-or-insert-slice:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$leaky-get-or-insert-slice:abort: + # . _write(2/stderr, error) + # . . push args + 68/push "leaky-get-or-insert-slice: table is full\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-leaky-get-or-insert-slice: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # (EAX..EDX) = "code" + b8/copy-to-EAX "code"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX + 05/add-to-EAX 4/imm32 + # var slice/EDX = {EAX, EDX} + 52/push-EDX + 50/push-EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX +$test-leaky-get-or-insert-slice:first-call: + # - start with an empty table, insert one key, verify that it was inserted + # EAX = leaky-get-or-insert-slice(table, "code" slice, 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call leaky-get-or-insert-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-leaky-get-or-insert-slice:check2: + # check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/1"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/2"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-leaky-get-or-insert-slice:second-call: + # - insert the same key again, verify that it was reused + # EAX = leaky-get-or-insert-slice(table, "code" slice, 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call leaky-get-or-insert-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/3"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # no new row inserted + # . check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/4"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/5"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-leaky-get-or-insert-slice:third-call: + # - insert a new key, verify that it was inserted + # (EAX..EDX) = "data" + b8/copy-to-EAX "data"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX + 05/add-to-EAX 4/imm32 + # var slice/EDX = {EAX, EDX} + 52/push-EDX + 50/push-EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # EAX = leaky-get-or-insert-slice(table, "data" slice, 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call leaky-get-or-insert-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # table gets a new row + # check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned + # . check-ints-equal(EAX - table, 24, msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/6"/imm32 + 68/push 0x18/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(table->write, 2 rows = 16, msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/7"/imm32 + 68/push 0x10/imm32/two-rows + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data+8, "data", msg) + # check-string-equal(*(table+20), "data", msg) + # . . push args + 68/push "F - test-leaky-get-or-insert-slice/8"/imm32 + 68/push "data"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-leaky-get-or-insert-slice:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, stop(ed) +get-or-stop: # table : (address stream {string, _}), key : (address string), row-size : int, + # abort-message-prefix : (address string), err : (address buffered-file), ed : (address exit-descriptor) + # -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if string-equal?(key, *curr) + # return curr+4 + # curr += row-size + # write-buffered(err, msg) + # stop(ed) + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$get-or-stop:search-loop: + # if (curr >= max) stop(ed) + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $get-or-stop:stop/disp8 + # if (string-equal?(key, *curr)) return curr+4 + # . EAX = string-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $get-or-stop:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $get-or-stop:end/disp8 +$get-or-stop:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $get-or-stop:search-loop/disp8 +$get-or-stop:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$get-or-stop:stop: + # . write-buffered(err, abort-message-prefix) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(err, error) + # . . push args + 68/push ": get-or-stop: key not found: "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(err, key) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(err, "\n") + # . . push args + 68/push "\n"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . stop(ed, 1) + # . . push args + 68/push 1/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x1c/disp8 . # push *(EBP+28) + # . . call + e8/call stop/disp32 + # never gets here +$get-or-stop:terminus: + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # syscall(exit, 1) + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + +test-get-or-stop: + # This test uses exit-descriptors. Use EBP for setting up local variables. + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-error-stream) + # . . push args + 68/push _test-error-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-error-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-error-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # var ed/EDX : (address exit-descriptor) + 68/push 0/imm32 + 68/push 0/imm32 + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # size 'ed' for the calls to 'get-or-stop' + # . tailor-exit-descriptor(ed, 24) + # . . push args + 68/push 0x18/imm32/nbytes-of-args-for-get-or-stop + 52/push-EDX + # . . call + e8/call tailor-exit-descriptor/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-stop:success: + # EAX = get-or-stop(table, "code", row-size=8, msg, _test-error-buffered-file, ed) + # . . push args + 52/push-EDX/ed + 68/push _test-error-buffered-file/imm32 + 68/push "foo"/imm32/abort-prefix + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-stop/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x18/imm32 # add to ESP +$test-get-or-stop:success-assertion: + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-or-stop/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-stop:failure: + # EAX = get-or-stop(table, "data", row-size=8, msg, _test-error-buffered-file, ed) + # . . push args + 52/push-EDX/ed + 68/push _test-error-buffered-file/imm32 + 68/push "foo"/imm32/abort-prefix + 68/push 8/imm32/row-size + 68/push "data"/imm32 + 51/push-ECX + # . . call + e8/call get-or-stop/disp32 + # registers except ESP may be clobbered at this point + # restore register args, discard others + 59/pop-to-ECX + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP + 5a/pop-to-EDX +$test-get-or-stop:failure-assertion: + # check that get-or-stop tried to call stop(1) + # . check-ints-equal(ed->value, 2, msg) + # . . push args + 68/push "F - test-get-or-stop/1"/imm32 + 68/push 2/imm32 + # . . push ed->value + ff 6/subop/push 1/mod/*+disp8 2/rm32/EDX . . . . 4/disp8 . # push *(EDX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-or-stop:end: + # . epilog + # don't restore ESP from EBP; manually reclaim locals + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x24/imm32 # add to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, stop(ed) +get-slice-or-stop: # table : (address stream {string, _}), key : (address slice), row-size : int, + # abort-message-prefix : (address string), err : (address buffered-file), ed : (address exit-descriptor) + # -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if slice-equal?(key, *curr) + # return curr+4 + # curr += row-size + # write-buffered(err, msg) + # stop(ed) + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$get-slice-or-stop:search-loop: + # if (curr >= max) stop(ed) + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $get-slice-or-stop:stop/disp8 + # if (slice-equal?(key, *curr)) return curr+4 + # . EAX = slice-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $get-slice-or-stop:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $get-slice-or-stop:end/disp8 +$get-slice-or-stop:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $get-slice-or-stop:search-loop/disp8 +$get-slice-or-stop:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$get-slice-or-stop:stop: + # . write-buffered(err, abort-message-prefix) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(err, error) + # . . push args + 68/push ": get-slice-or-stop: key not found: "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-slice-buffered(err, key) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-slice-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(err, "\n") + # . . push args + 68/push "\n"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . stop(ed, 1) + # . . push args + 68/push 1/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x1c/disp8 . # push *(EBP+28) + # . . call + e8/call stop/disp32 + # never gets here +$get-slice-or-stop:terminus: + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # syscall(exit, 1) + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + +test-get-slice-or-stop: + # This test uses exit-descriptors. Use EBP for setting up local variables. + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-error-stream) + # . . push args + 68/push _test-error-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-error-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-error-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # var ed/EDX : (address exit-descriptor) + 68/push 0/imm32 + 68/push 0/imm32 + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # var slice/EBX = "code" + # . (EAX..EBX) = "code" + b8/copy-to-EAX "code"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 3/r32/EBX . . # copy *EAX to EBX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 3/index/EBX . 3/r32/EBX 4/disp8 . # copy EAX+EBX+4 to EBX + 05/add-to-EAX 4/imm32 + # . EBX = {EAX, EBX} + 53/push-EBX + 50/push-EAX + 89/copy 3/mod/direct 3/rm32/EBX . . . 4/r32/ESP . . # copy ESP to EBX + # size 'ed' for the calls to 'get-or-stop' (define no locals past this point) + # . tailor-exit-descriptor(ed, 24) + # . . push args + 68/push 0x18/imm32/nbytes-of-args-for-get-or-stop + 52/push-EDX + # . . call + e8/call tailor-exit-descriptor/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-slice-or-stop:success: + # EAX = get-slice-or-stop(table, slice, row-size=8, msg, _test-error-buffered-file, ed) + # . . push args + 52/push-EDX/ed + 68/push _test-error-buffered-file/imm32 + 68/push "foo"/imm32/abort-prefix + 68/push 8/imm32/row-size + 53/push-EBX/slice + 51/push-ECX + # . . call + e8/call get-slice-or-stop/disp32 + # registers except ESP may be clobbered at this point + # restore register args, discard others + 59/pop-to-ECX + 5b/pop-to-EBX + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 5a/pop-to-EDX +$test-get-slice-or-stop:success-assertion: + # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-get-slice-or-stop/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-slice-or-stop:failure: + # slice = "segment2" + # . *EBX = "segment2"->data + b8/copy-to-EAX "segment2"/imm32 + 05/add-to-EAX 4/imm32 + 89/copy 0/mod/indirect 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to *EBX + # . *(EBX+4) = "segment2"->data + len("segment2") + 05/add-to-EAX 8/imm32/strlen + 89/copy 1/mod/*+disp8 3/rm32/EBX . . . 0/r32/EAX 4/disp8 . # copy EAX to *(EBX+4) + # EAX = get-slice-or-stop(table, slice, row-size=8, msg, _test-error-buffered-file, ed) + # . . push args + 52/push-EDX/ed + 68/push _test-error-buffered-file/imm32 + 68/push "foo"/imm32/abort-prefix + 68/push 8/imm32/row-size + 53/push-EBX/slice + 51/push-ECX + # . . call + e8/call get-slice-or-stop/disp32 + # registers except ESP may be clobbered at this point + # restore register args, discard others + 59/pop-to-ECX + 5b/pop-to-EBX + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 5a/pop-to-EDX +$test-get-slice-or-stop:failure-assertion: + # check that get-or-stop tried to call stop(1) + # . check-ints-equal(ed->value, 2, msg) + # . . push args + 68/push "F - test-get-or-stop/1"/imm32 + 68/push 2/imm32 + # . . push ed->value + ff 6/subop/push 1/mod/*+disp8 2/rm32/EDX . . . . 4/disp8 . # push *(EDX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-get-slice-or-stop:end: + # . epilog + # don't restore ESP from EBP; manually reclaim locals + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x2c/imm32 # add to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, return null (0) +maybe-get: # table : (address stream {string, _}), key : (address string), row-size : int -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if string-equal?(key, *curr) + # return curr+4 + # curr += row-size + # return 0 + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$maybe-get:search-loop: + # if (curr >= max) return null + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $maybe-get:null/disp8 + # if (string-equal?(key, *curr)) return curr+4 + # . EAX = string-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $maybe-get:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $maybe-get:end/disp8 +$maybe-get:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $maybe-get:search-loop/disp8 +$maybe-get:null: + b8/copy-to-EAX 0/imm32 +$maybe-get:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-maybe-get: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # - setup: create a table with one row + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = get-or-insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-maybe-get:success: + # - check for the same key, verify that it was reused + # EAX = maybe-get(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call maybe-get/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-maybe-get/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # no new row inserted + # . check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-maybe-get/1"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-maybe-get/2"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-maybe-get:failure: + # - search for a new key + # EAX = maybe-get(table, "data", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "data"/imm32 + 51/push-ECX + # . . call + e8/call maybe-get/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX, 0, msg) + # . . push args + 68/push "F - test-maybe-get/3"/imm32 + 68/push 0/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-maybe-get:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# if no row is found, return null (0) +maybe-get-slice: # table : (address stream {string, _}), key : (address slice), row-size : int -> EAX : (address _) + # pseudocode: + # curr = table->data + # max = &table->data[table->write] + # while curr < max + # if slice-equal?(key, *curr) + # return curr+4 + # curr += row-size + # return 0 + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 56/push-ESI + # ESI = table + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # curr/ECX = table->data + 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX + # max/EDX = table->data + table->write + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX + 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX +$maybe-get-slice:search-loop: + # if (curr >= max) return null + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $maybe-get-slice:null/disp8 + # if (slice-equal?(key, *curr)) return curr+4 + # . EAX = slice-equal?(key, *curr) + # . . push args + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . if (EAX != 0) return EAX = curr+4 + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $maybe-get-slice:mismatch/disp8 + 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX + eb/jump $maybe-get-slice:end/disp8 +$maybe-get-slice:mismatch: + # curr += row-size + 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX + # loop + eb/jump $maybe-get-slice:search-loop/disp8 +$maybe-get-slice:null: + b8/copy-to-EAX 0/imm32 +$maybe-get-slice:end: + # . restore registers + 5e/pop-to-ESI + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-maybe-get-slice: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # - setup: create a table with one row + # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP + 68/push 0x10/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # insert(table, "code", 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 68/push "code"/imm32 + 51/push-ECX + # . . call + e8/call get-or-insert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-maybe-get-slice:success: + # - check for the same key, verify that it was reused + # (EAX..EDX) = "code" + b8/copy-to-EAX "code"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX + 05/add-to-EAX 4/imm32 + # var slice/EDX = {EAX, EDX} + 52/push-EDX + 50/push-EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # EAX = maybe-get-slice(table, "code" slice, 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call maybe-get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX - table->data, 4, msg) + # . check-ints-equal(EAX - table, 16, msg) + # . . push args + 68/push "F - test-maybe-get-slice/0"/imm32 + 68/push 0x10/imm32 + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # no new row inserted + # . check-ints-equal(table->write, row-size = 8, msg) + # . . push args + 68/push "F - test-maybe-get-slice/1"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-string-equal(*table->data, "code", msg) + # . . push args + 68/push "F - test-maybe-get-slice/2"/imm32 + 68/push "code"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) + # . . call + e8/call check-string-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-maybe-get-slice:failure: + # - search for a new key + # (EAX..EDX) = "data" + b8/copy-to-EAX "data"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX + 05/add-to-EAX 4/imm32 + # var slice/EDX = {EAX, EDX} + 52/push-EDX + 50/push-EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX + # EAX = maybe-get-slice(table, "data" slice, 8 bytes per row) + # . . push args + 68/push 8/imm32/row-size + 52/push-EDX + 51/push-ECX + # . . call + e8/call maybe-get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EAX, 0, msg) + # . . push args + 68/push "F - test-maybe-get-slice/3"/imm32 + 68/push 0/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP +$test-maybe-get-slice:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# . . vim:nowrap:textwidth=0 diff --git a/apps/assort b/apps/assort index 70d7aaf3..4787bd57 100755 --- a/apps/assort +++ b/apps/assort Binary files differdiff --git a/apps/crenshaw2-1 b/apps/crenshaw2-1 index 47ee601e..c8affb01 100755 --- a/apps/crenshaw2-1 +++ b/apps/crenshaw2-1 Binary files differdiff --git a/apps/crenshaw2-1.subx b/apps/crenshaw2-1.subx index 079ffb26..bd8cc5d6 100644 --- a/apps/crenshaw2-1.subx +++ b/apps/crenshaw2-1.subx @@ -559,27 +559,4 @@ $is-digit?:end: Look: # (char with some extra padding) 0/imm32 -_test-output-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 8/imm32 - # data - 00 00 00 00 00 00 00 00 # 8 bytes - -_test-error-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 0x40/imm32 - # data (4 lines x 16 bytes/line) - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - # . . vim:nowrap:textwidth=0 diff --git a/apps/crenshaw2-1b b/apps/crenshaw2-1b index fba81a7d..9b059183 100755 --- a/apps/crenshaw2-1b +++ b/apps/crenshaw2-1b Binary files differdiff --git a/apps/crenshaw2-1b.subx b/apps/crenshaw2-1b.subx index 064af459..cb75033f 100644 --- a/apps/crenshaw2-1b.subx +++ b/apps/crenshaw2-1b.subx @@ -759,27 +759,4 @@ $is-digit?:end: Look: # (char with some extra padding) 0/imm32 -_test-output-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 8/imm32 - # data - 00 00 00 00 00 00 00 00 # 8 bytes - -_test-error-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 0x40/imm32 - # data (4 lines x 16 bytes/line) - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - # . . vim:nowrap:textwidth=0 diff --git a/apps/desugar b/apps/desugar new file mode 100755 index 00000000..53e23663 --- /dev/null +++ b/apps/desugar Binary files differdiff --git a/apps/desugar.subx b/apps/desugar.subx new file mode 100644 index 00000000..1a33acfa --- /dev/null +++ b/apps/desugar.subx @@ -0,0 +1,4117 @@ +# Desugar a few kinds of syntax. +# +# 1. +# $ echo "ab %eax" | ./subx run apps/desugar +# ab 3/mod 0/rm32 +# +# 2. +# $ echo "ab *eax" | ./subx run apps/desugar +# ab 0/mod 0/rm32 +# +# 3. +# $ echo "ab *(eax+4)" | ./subx run apps/desugar +# ab 2/mod 0/rm32 4/disp32 +# +# 4. +# $ echo "ab *(eax+ecx)" | ./subx run apps/desugar +# ab 0/mod 4/rm32 0/base 1/index 0/scale +# +# 5. +# $ echo "ab *(eax+ecx+4)" | ./subx run apps/desugar +# ab 2/mod 4/rm32 0/base 1/index 0/scale 4/disp32 +# +# 6. +# $ echo "ab *(eax+ecx<<2+4)" | ./subx run apps/desugar +# ab 2/mod 4/rm32 0/base 1/index 2/scale 4/disp32 +# +# Addition isn't commutative here. Template must always be (base+index<<scale+disp), +# though some components are optional as described above. +# +# No metadata allowed inside '*(...)'. +# Whitespace inside '*(...)' is ok. But not immediately after the '*' or '%'. + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes + +Entry: # run tests if necessary, convert stdin if not + # initialize heap + # . Heap = new-segment(Heap-size) + # . . push args + 68/push Heap/imm32 + ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Heap-size/disp32 # push *Heap-size + # . . call + e8/call new-segment/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + + # run tests if necessary, convert stdin if not + # . prolog + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # - if argc > 1 and argv[1] == "test", then return run_tests() + # . argc > 1 + 81 7/subop/compare 1/mod/*+disp8 5/rm32/EBP . . . . 0/disp8 1/imm32 # compare *EBP + 7e/jump-if-lesser-or-equal $run-main/disp8 + # . argv[1] == "test" + # . . push args + 68/push "test"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call kernel-string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . check result + 3d/compare-EAX-and 1/imm32 + 75/jump-if-not-equal $run-main/disp8 + # . run-tests() + e8/call run-tests/disp32 + 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX + eb/jump $main:end/disp8 +$run-main: + # - otherwise convert stdin + # convert(Stdin, Stdout) + # . . push args + 68/push Stdout/imm32 + 68/push Stdin/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x8/imm32 # add to ESP + # . syscall(exit, 0) + bb/copy-to-EBX 0/imm32 +$main:end: + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + +# error messages considered: +# *x + 34 -> error: base+disp addressing must be within '()' +convert: # in : (address buffered-file), out : (address buffered-file) -> <void> + # pseudocode: + # var line = new-stream(512, 1) + # while true + # clear-stream(line) + # read-line-buffered(in, line) + # if (line->write == 0) break # end of file + # while true + # var word-slice = next-word-or-expression(line) + # if slice-empty?(word-slice) # end of line + # break + # if slice-starts-with?(word-slice, "#") # comment + # continue + # if slice-starts-with?(word-slice, '%') # direct mode + # emit-direct-mode(word-slice, out) + # else if slice-starts-with?(word-slice, '*') # indirect mode + # base, index, scale, disp = parse-effective-address(word-slice) + # emit-indirect-mode(out, base, index, scale, disp) + # else if slice-starts-with?(word-slice, '+') + # abort("'+' only permitted within '*(...)'") + # else + # write-slice-buffered(out, word-slice) + # write(out, " ") + # write(out, "\n") + # flush(out) + # + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + 51/push-ECX + 52/push-EDX + 53/push-EBX + # var line/ECX : (address stream byte) = stream(512) + 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x200/imm32 # subtract from ESP + 68/push 0x200/imm32/length + 68/push 0/imm32/read + 68/push 0/imm32/write + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # var word-slice/EDX = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX +$convert:line-loop: + # clear-stream(line) + # . . push args + 51/push-ECX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # read-line-buffered(in, line) + # . . push args + 51/push-ECX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call read-line-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$convert:check0: + # if (line->write == 0) break + 81 7/subop/compare 0/mod/indirect 1/rm32/ECX . . . . . 0/imm32 # compare *ECX + 0f 84/jump-if-equal $convert:break/disp32 +$convert:word-loop: + # next-word-or-expression(line, word-slice) + # . . push args + 52/push-EDX + 51/push-ECX + # . . call + e8/call next-word-or-expression/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$convert:check1: + # if (slice-empty?(word-slice)) break + # . EAX = slice-empty?(word-slice) + # . . push args + 52/push-EDX + # . . call + e8/call slice-empty?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . if (EAX != 0) break + 3d/compare-EAX-and 0/imm32 + 0f 85/jump-if-not-equal $convert:next-line/disp32 +$convert:check-for-comment: + # if (slice-starts-with?(word-slice, "#")) continue + # . start/EBX = word-slice->start + 8b/copy 0/mod/indirect 2/rm32/EDX . . . 3/r32/EBX . . # copy *EDX to EBX + # . c/EAX = *start + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 0/mod/indirect 3/rm32/EBX . . . 0/r32/AL . . # copy byte at *EBX to AL + # . if (EAX == '#') continue + 3d/compare-EAX-and 0x23/imm32/hash + 74/jump-if-equal $convert:word-loop/disp8 +$convert:check-for-direct-mode: + # if (!slice-starts-with?(word-slice, "%")) goto next check + 3d/compare-EAX-and 0x25/imm32/percent + 75/jump-if-not-equal $convert:check-for-indirect-mode/disp8 +$convert:direct-mode: +#? # dump word-slice {{{ +#? # . write(2/stderr, "w: ") +#? # . . push args +#? 68/push "w: "/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . clear-stream(Stderr+4) +#? # . . save EAX +#? 50/push-EAX +#? # . . push args +#? b8/copy-to-EAX Stderr/imm32 +#? 05/add-to-EAX 4/imm32 +#? 50/push-EAX +#? # . . call +#? e8/call clear-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # . . restore EAX +#? 58/pop-to-EAX +#? # . write-slice-buffered(Stderr, word-slice) +#? # . . push args +#? 52/push-EDX +#? 68/push Stderr/imm32 +#? # . . call +#? e8/call write-slice-buffered/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . flush(Stderr) +#? # . . push args +#? 68/push Stderr/imm32 +#? # . . call +#? e8/call flush/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # emit-direct-mode(word-slice, out) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + 52/push-EDX + # . . call + e8/call emit-direct-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # continue + e9/jump $convert:next-word/disp32 +$convert:check-for-indirect-mode: + # if (!slice-starts-with?(word-slice, "*")) goto next check + 3d/compare-EAX-and 0x2a/imm32/asterisk + 75/jump-if-not-equal $convert:check-for-invalid-addition/disp8 +$convert:indirect-mode: + # spill registers + 50/push-EAX + 51/push-ECX + 52/push-EDX + 53/push-EBX + # base/EAX, index/ECX, scale/EDX, disp/EBX = parse-effective-address(word-slice) + # . . push args + 52/push-EDX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # emit-indirect-mode(out, base, index, scale, disp) + # . . push args + 53/push-EBX + 52/push-EDX + 51/push-ECX + 50/push-EAX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call emit-indirect-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP + # restore registers + 5b/pop-to-EBX + 5a/pop-to-EDX + 59/pop-to-ECX + 58/pop-to-EAX + # continue + e9/jump $convert:next-word/disp32 +$convert:check-for-invalid-addition: + # if (slice-starts-with?(word-slice, "+")) goto error1 + 3d/compare-EAX-and 0x2b/imm32/plus + 74/jump-if-equal $convert:error1/disp8 +$convert:check-for-invalid-left-shift: + # if (slice-starts-with?(word-slice, "<")) goto error1 + 3d/compare-EAX-and 0x3c/imm32/less-than + 74/jump-if-equal $convert:error1/disp8 +$convert:regular-word: + # write-slice-buffered(out, word-slice) + # . . push args + 52/push-EDX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-slice-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # fall through +$convert:next-word: + # write-buffered(out, " ") + # . . push args + 68/push " "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # loop + e9/jump $convert:word-loop/disp32 +$convert:next-line: + # write-buffered(out, "\n") + # . . push args + 68/push Newline/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # loop + e9/jump $convert:line-loop/disp32 +$convert:break: + # flush(out) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +$convert:end: + # . reclaim locals + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x214/imm32 # add to ESP + # . restore registers + 5b/pop-to-EBX + 5a/pop-to-EDX + 59/pop-to-ECX + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$convert:error1: + # print(stderr, "error: '" EAX "' only permitted within '*(...)' in '" line "'") + # . write-buffered(Stderr, "error: '") + # . . push args + 68/push "error: '"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-byte-buffered(Stderr, EAX) + # . . push args + 50/push-EAX + 68/push Stderr/imm32 + # . . call + e8/call write-byte-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(Stderr, "' only permitted within '*(...)' in '") + # . . push args + 68/push "' only permitted within '*(...)' in '"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-stream-data(Stderr, line) + # . . push args + 51/push-ECX + 68/push Stderr/imm32 + # . . call + e8/call write-stream-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(Stderr, "'") + # . . push args + 68/push "'"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . flush(Stderr) + # . . push args + 68/push Stderr/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-convert-passes-most-words-through: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "== abcd 0x1") + # . . push args + 68/push "== abcd 0x1"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "== abcd 0x1 \n", msg) + # . . push args + 68/push "F - test-convert-passes-most-words-through"/imm32 + 68/push "== abcd 0x1 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-direct-mode: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "ab %ecx") + # . . push args + 68/push "ab %ecx"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "ab 3/mod/direct 0x00000001/rm32 \n", msg) + # . . push args + 68/push "F - test-convert-direct-mode"/imm32 + 68/push "ab 3/mod/direct 0x00000001/rm32 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-register-indirect-mode: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "ab *ecx") + # . . push args + 68/push "ab *ecx"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "ab 0/mod/indirect 0x00000001/rm32 \n", msg) + # . . push args + 68/push "F - test-convert-indirect-mode"/imm32 + 68/push "ab 0/mod/indirect 0x00000001/rm32 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-register-indirect-mode-without-displacement: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "ab *(ecx)") + # . . push args + 68/push "ab *(ecx)"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "ab 0/mod/indirect 1/rm32 \n", msg) + # . . push args + 68/push "F - test-convert-indirect-mode-without-displacement"/imm32 + 68/push "ab 0/mod/indirect 0x00000001/rm32 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-register-indirect-mode-with-displacement: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "ab *(ecx+4)") + # . . push args + 68/push "ab *(ecx+4)"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "ab 2/mod/*+disp32 1/rm32 4/disp32 \n", msg) + # . . push args + 68/push "F - test-convert-indirect-mode-with-displacement"/imm32 + 68/push "ab 2/mod/*+disp32 0x00000001/rm32 0x00000004/disp32 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# boss level +test-convert-register-indirect-mode-with-sib-byte: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "ab *(ecx + edx<<3 + 4)") + # . . push args + 68/push "ab *(ecx + edx<<3 + 4)"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "ab 2/mod/*+disp32 4/rm32/sib 1/base 2/index 3/scale 4/disp32 \n", msg) + # . . push args + 68/push "F - test-convert-indirect-mode-with-sib-byte"/imm32 + 68/push "ab 2/mod/*+disp32 4/rm32/sib 0x00000001/base 0x00000002/index 0x00000003/scale 0x00000004/disp32 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-convert-register-indirect-mode-with-sib-byte-negative-displacement: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-input-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-input-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # initialize input + # . write(_test-input-stream, "ab *(ecx + edx<<3 - 4)") + # . . push args + 68/push "ab *(ecx + edx<<3 - 4)"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # convert(_test-input-buffered-file, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 68/push _test-input-buffered-file/imm32 + # . . call + e8/call convert/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check that the line just passed through + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump _test-output-stream {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # . check-stream-equal(_test-output-stream, "ab 2/mod/*+disp32 4/rm32/sib 1/base 2/index 3/scale -4/disp32 \n", msg) + # . . push args + 68/push "F - test-convert-indirect-mode-with-sib-byte-negative-displacement"/imm32 + 68/push "ab 2/mod/*+disp32 4/rm32/sib 0x00000001/base 0x00000002/index 0x00000003/scale 0xfffffffc/disp32 \n"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# beware: modifies 'word' +emit-direct-mode: # word : (address slice), out : (address buffered-file) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + # ++word->start + # . EAX = word + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to EAX + # . ++(*EAX) + ff 0/subop/increment 0/mod/indirect 0/rm32/EAX . . . . . . # increment *EAX + # reg-num/EAX = get-slice(Registers, word, row-size=8) + # . . push args + 68/push "Registers"/imm32 + 68/push 8/imm32/row-size + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + 68/push Registers/imm32 + # . . call + e8/call get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP + # write-buffered(out, "3/mod/direct ") + # . . push args + 68/push "3/mod/direct "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # print-int32-buffered(out, *EAX) + # . . push args + ff 6/subop/push 0/mod/indirect 0/rm32/EAX . . . . . . # push *EAX + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # write-buffered(out, "/rm32") + # . . push args + 68/push "/rm32"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$emit-direct-mode:end: + # . restore registers + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-direct-mode: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var slice/ECX = "%eax" + b8/copy-to-EAX "%eax"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # emit-direct-mode(str, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 51/push-ECX + # . . call + e8/call emit-direct-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "3/mod/direct 0/rm32", msg) + # . . push args + 68/push "F - test-emit-direct-mode/0"/imm32 + 68/push "3/mod/direct 0x00000000/rm32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-direct-mode-2: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var slice/ECX = "%edi" + b8/copy-to-EAX "%edi"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # emit-direct-mode(str/ECX, _test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + 51/push-ECX + # . . call + e8/call emit-direct-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "3/mod/direct 7/rm32", msg) + # . . push args + 68/push "F - test-emit-direct-mode/1"/imm32 + 68/push "3/mod/direct 0x00000007/rm32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# (re)compute the bounds of the next word or parenthetical expression in the line +# return empty string on reaching end of file +# +# error messages considered: +# * ... -> error: no space after '*' +# *(... -> error: *(...) expression must be all on a single line +next-word-or-expression: # line : (address stream byte), out : (address slice) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + 51/push-ECX + 56/push-ESI + 57/push-EDI + # ESI = line + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # EDI = out + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 0xc/disp8 . # copy *(EBP+12) to EDI + # skip-chars-matching(line, ' ') + # . . push args + 68/push 0x20/imm32/space + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call skip-chars-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$next-word-or-expression:check0: + # if (line->read >= line->write) clear out and return + # . EAX = line->read + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy *(ESI+4) to EAX + # . if (EAX < line->write) goto next check + 3b/compare 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # compare EAX with *ESI + 7c/jump-if-lesser $next-word-or-expression:check-for-comment/disp8 + # . return out = {0, 0} + c7 0/subop/copy 0/mod/direct 7/rm32/EDI . . . . . 0/imm32 # copy to *EDI + c7 0/subop/copy 1/mod/*+disp8 7/rm32/EDI . . . . 4/disp8 0/imm32 # copy to *(EDI+4) + eb/jump $next-word-or-expression:end/disp8 +$next-word-or-expression:check-for-comment: + # out->start = &line->data[line->read] + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 . # copy *(ESI+4) to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/EAX 0xc/disp8 . # copy ESI+ECX+12 to EAX + 89/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy EAX to *EDI + # if (line->data[line->read] != '#') goto next check + # . EAX = line->data[line->read] + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy byte at *(ESI+ECX+12) to AL + # . compare + 3d/compare-EAX-and 0x23/imm32/pound + 75/jump-if-not-equal $next-word-or-expression:check-for-paren/disp8 +$next-word-or-expression:comment: + # out->end = &line->data[line->write] + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy ESI+EAX+12 to EAX + 89/copy 1/mod/*+disp8 7/rm32/EDI . . . 0/r32/EAX 4/disp8 . # copy EAX to *(EDI+4) + # line->read = line->write # skip rest of line + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX + 89/copy 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 . # copy EAX to *(ESI+4) + # return + eb/jump $next-word-or-expression:end/disp8 +$next-word-or-expression:check-for-paren: + # if (line->data[line->read] != '*') goto next check + # . EAX = line->data[line->read] + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy byte at *(ESI+ECX+12) to AL + # . compare + 3d/compare-EAX-and 0x2a/imm32/asterisk + 75/jump-if-not-equal $next-word-or-expression:regular-word/disp8 + # if (line->data[line->read] == ' ') goto error1 + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xd/disp8 . # copy byte at *(ESI+ECX+12+1) to AL + # . compare + 3d/compare-EAX-and 0x20/imm32/space + 74/jump-if-equal $next-word-or-expression:error1/disp8 + # if (line->data[line->read] != '(') goto regular word + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xd/disp8 . # copy byte at *(ESI+ECX+12+1) to AL + # . compare + 3d/compare-EAX-and 0x28/imm32/open-paren + 75/jump-if-not-equal $next-word-or-expression:regular-word/disp8 +$next-word-or-expression:paren: + # skip-until-close-paren(line) + # . . push args + 56/push-ESI + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # if (line->data[line->read] != ')') goto error2 + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 . # copy *(ESI+4) to ECX + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy byte at *(ESI+ECX+12) to AL + # . compare + 3d/compare-EAX-and 0x29/imm32/close-paren + 75/jump-if-not-equal $next-word-or-expression:error2/disp8 + # skip ')' + ff 0/subop/increment 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # increment *(ESI+4) + # fall through +$next-word-or-expression:regular-word: + # skip-chars-not-matching-whitespace(line) # including trailing newline + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call skip-chars-not-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # out->end = &line->data[line->read] + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 . # copy *(ESI+4) to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/EAX 0xc/disp8 . # copy ESI+ECX+12 to EAX + 89/copy 1/mod/*+disp8 7/rm32/EDI . . . 0/r32/EAX 4/disp8 . # copy EAX to *(EDI+4) +$next-word-or-expression:end: + # . restore registers + 5f/pop-to-EDI + 5e/pop-to-ESI + 59/pop-to-ECX + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$next-word-or-expression:error1: + # print(stderr, "error: no space allowed after '*' in '" line "'") + # . write-buffered(Stderr, "error: no space allowed after '*' in '") + # . . push args + 68/push "error: no space allowed after '*' in '"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-stream-data(Stderr, line) + # . . push args + 56/push-ESI + 68/push Stderr/imm32 + # . . call + e8/call write-stream-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(Stderr, "'") + # . . push args + 68/push "'"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . flush(Stderr) + # . . push args + 68/push Stderr/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +$next-word-or-expression:error2: + # print(stderr, "error: no space allowed after '*' in '" line "'") + # . write-buffered(Stderr, "error: *(...) expression must be all on a single line in '") + # . . push args + 68/push "error: *(...) expression must be all on a single line in '"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-stream-data(Stderr, line) + # . . push args + 56/push-ESI + 68/push Stderr/imm32 + # . . call + e8/call write-stream-data/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(Stderr, "'") + # . . push args + 68/push "'"/imm32 + 68/push Stderr/imm32 + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . flush(Stderr) + # . . push args + 68/push Stderr/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-next-word-or-expression: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var slice/ECX = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # write(_test-input-stream, " ab") + # . . push args + 68/push " ab"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # next-word-or-expression(_test-input-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-input-stream/imm32 + # . . call + e8/call next-word-or-expression/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(_test-input-stream->read, 4, msg) + # . . push args + 68/push "F - test-next-word-or-expression/updates-stream-read-correctly"/imm32 + 68/push 4/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(slice->start - _test-input-stream->data, 2, msg) + # . check-ints-equal(slice->start - _test-input-stream, 14, msg) + # . . push args + 68/push "F - test-next-word-or-expression: start"/imm32 + 68/push 0xe/imm32 + # . . push slice->start - _test-input-stream + 8b/copy 0/mod/indirect 1/rm32/ECX . . . 0/r32/EAX . . # copy *ECX to EAX + 81 5/subop/subtract 3/mod/direct 0/rm32/EAX . . . . . _test-input-stream/imm32 # subtract from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(slice->end - _test-input-stream->data, 4, msg) + # . check-ints-equal(slice->end - _test-input-stream, 16, msg) + # . . push args + 68/push "F - test-next-word-or-expression: end"/imm32 + 68/push 0x10/imm32 + # . . push slice->end - _test-input-stream + 8b/copy 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy *(ECX+4) to EAX + 81 5/subop/subtract 3/mod/direct 0/rm32/EAX . . . . . _test-input-stream/imm32 # subtract from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-word-or-expression-returns-whole-comment: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var slice/ECX = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # write(_test-input-stream, " # a") + # . . push args + 68/push " # a"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # next-word-or-expression(_test-input-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-input-stream/imm32 + # . . call + e8/call next-word-or-expression/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(_test-input-stream->read, 5, msg) + # . . push args + 68/push "F - test-next-word-or-expression-returns-whole-comment/updates-stream-read-correctly"/imm32 + 68/push 5/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(slice->start - _test-input-stream->data, 2, msg) + # . check-ints-equal(slice->start - _test-input-stream, 14, msg) + # . . push args + 68/push "F - test-next-word-or-expression-returns-whole-comment: start"/imm32 + 68/push 0xe/imm32 + # . . push slice->start - _test-input-stream + 8b/copy 0/mod/indirect 1/rm32/ECX . . . 0/r32/EAX . . # copy *ECX to EAX + 81 5/subop/subtract 3/mod/direct 0/rm32/EAX . . . . . _test-input-stream/imm32 # subtract from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(slice->end - _test-input-stream->data, 5, msg) + # . check-ints-equal(slice->end - _test-input-stream, 17, msg) + # . . push args + 68/push "F - test-next-word-or-expression-returns-whole-comment: end"/imm32 + 68/push 0x11/imm32 + # . . push slice->end - _test-input-stream + 8b/copy 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy *(ECX+4) to EAX + 81 5/subop/subtract 3/mod/direct 0/rm32/EAX . . . . . _test-input-stream/imm32 # subtract from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-word-or-expression-returns-empty-expression-on-eof: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var slice/ECX = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # write nothing to _test-input-stream + # next-word-or-expression(_test-input-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-input-stream/imm32 + # . . call + e8/call next-word-or-expression/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(slice->end - slice->start, 0, msg) + # . . push args + 68/push "F - test-next-word-or-expression-returns-empty-expression-on-eof"/imm32 + 68/push 0/imm32 + # . . push slice->end - slice->start + 8b/copy 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy *(ECX+4) to EAX + 2b/subtract 0/mod/indirect 1/rm32/ECX . . . 0/r32/EAX . . # subtract *ECX from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-word-or-expression-returns-whole-expression: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # var slice/ECX = {0, 0} + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # write(_test-input-stream, " *(a b)/imm32 ") + # . . push args + 68/push " *(a b)/imm32 "/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # next-word-or-expression(_test-input-stream, slice) + # . . push args + 51/push-ECX + 68/push _test-input-stream/imm32 + # . . call + e8/call next-word-or-expression/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(slice->start - _test-input-stream->data, 1, msg) + # . check-ints-equal(slice->start - _test-input-stream, 13, msg) + # . . push args + 68/push "F - test-next-word-or-expression-returns-whole-expression: start"/imm32 + 68/push 0xd/imm32 + # . . push slice->start - _test-input-stream + 8b/copy 0/mod/indirect 1/rm32/ECX . . . 0/r32/EAX . . # copy *ECX to EAX + 81 5/subop/subtract 3/mod/direct 0/rm32/EAX . . . . . _test-input-stream/imm32 # subtract from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(slice->end - _test-input-stream->data, 13, msg) + # . check-ints-equal(slice->end - _test-input-stream, 25, msg) + # . . push args + 68/push "F - test-next-word-or-expression-returns-whole-expression: end"/imm32 + 68/push 0x19/imm32 + # . . push slice->end - _test-input-stream + 8b/copy 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy *(ECX+4) to EAX + 81 5/subop/subtract 3/mod/direct 0/rm32/EAX . . . . . _test-input-stream/imm32 # subtract from EAX + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# Grammar: +# *reg -> 0/mod reg/rm32 +# *(reg) -> 0/mod reg/rm32 +# *(reg+disp) -> 2/mod reg/rm32 disp/disp32 +# *(reg1+reg2<<s) -> 2/mod 4/rm32 reg1/base reg2/index s/scale 0/disp32 +# *(reg1+reg2<<s+disp) -> 2/mod 4/rm32 reg1/base reg2/index s/scale disp/disp32 +# Intermediate structure: base, index, scale, disp +# Default values: base: 0, index: 4 (none), scale: 0, disp: 0 +# beware: modifies 'word' +parse-effective-address: # word : (address slice) -> base/EAX, index/ECX, scale/EDX, disp/EBX + # pseudocode: + # ++word->start to skip '*' + # initialize defaults: base=0, index=4, scale=0, disp=0 + # if (*word->start != '(') { + # base = get-slice(Registers, word, row-size=8) + # return + # } + # # compound expressions + # skip whitespace + # read register into base + # skip whitespace + # if (*word->start == ')') goto end + # if (*word->start == '-') goto displacement + # if (*word->start != '+') goto error2 + # ++word->start to skip '+' + # skip whitespace + # if next 3 characters don't make a register, goto displacement + # read register into index + # skip whitespace + # if (*word->start == ')') goto end + # if (*word->start == '<') { + # ++word->start to skip '<' + # if (*word->start != '<') goto error3 + # ++word->start to skip '<' + # skip whitespace + # read integer into scale + # skip whitespace + # if (*word->start == ')') goto end + # } + # if (*word->start not in '+' '-') goto error4 + # displacement: + # read integer into disp + # skip whitespace + # if (*word->start != ')') goto error5 + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 56/push-ESI + 57/push-EDI + # ESI = word + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # ++word->start to skip '*' + ff 0/subop/increment 0/mod/indirect 6/rm32/ESI . . . . . . # increment *ESI + # initialize defaults + # base is in EDI; we'll move it to EAX just before we return + bf/copy-to-EDI 0/imm32 + b9/copy-to-ECX 4/imm32/no-index + ba/copy-to-EDX 0/imm32/.scale + bb/copy-to-EBX 0/imm32/disp +$parse-effective-address:check-for-simple-register: + # if (*word->start == '(') goto compound expression + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX + 8a/copy-byte 0/mod/indirect 0/rm32/EAX . . . 0/r32/AL . . # copy byte at *EAX to AL + 81 4/subop/and 3/mod/direct 0/rm32/EAX . . . . . 0xff/imm32 # bitwise and of EAX + 3d/compare-EAX-and 0x28/imm32/open-paren + 74/jump-if-equal $parse-effective-address:compound-expression/disp8 +$parse-effective-address:simple-register: + # base = get-slice(Registers, word, row-size=8) + # . EAX = get-slice(Registers, word, row-size=8) + # . . push args + 68/push "Registers"/imm32 + 68/push 8/imm32/row-size + 56/push-ESI + 68/push Registers/imm32 + # . . call + e8/call get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP + # . base = *EAX + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 7/r32/EDI . . # copy *EAX to EDI + # return + e9/jump $parse-effective-address:end/disp32 +$parse-effective-address:compound-expression: + # ++word->start to skip '(' + ff 0/subop/increment 0/mod/indirect 6/rm32/ESI . . . . . . # increment *ESI + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI + # read register into base + # . EAX = next-register(word) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call next-register/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . EDI = *EAX + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 7/r32/EDI . . # copy *EAX to EDI + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI + # if (*word->start == ')') goto end + 8a/copy-byte 0/mod/indirect 0/rm32/EAX . . . 0/r32/AL . . # copy byte at *EAX to AL + 81 4/subop/and 3/mod/direct 0/rm32/EAX . . . . . 0xff/imm32 # bitwise and of EAX + 3d/compare-EAX-and 0x29/imm32/close-paren + 0f 84/jump-if-equal $parse-effective-address:end/disp32 + # if (*word->start == '-') goto displacement + 3d/compare-EAX-and 0x2d/imm32/minus + 0f 84/jump-if-equal $parse-effective-address:displacement/disp32 + # if (*word->start != '+') goto error2 +$parse-effective-address:check-for-index: + # ++word->start to skip '+' + ff 0/subop/increment 0/mod/indirect 6/rm32/ESI . . . . . . # increment *ESI + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI +$parse-effective-address:resolve-ambiguity: + # if next 3 characters don't make a register, goto displacement + # . spill ECX + 51/push-ECX + # . var tmp/ECX = {word->start, word->start+3} + # . . ECX = word->start + 89/copy 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # copy EAX to ECX + # . . EAX = word->start+3 + 05/add-to-EAX 3/imm32 + # . . push + 50/push-EAX + 51/push-ECX + # . . copy ESP to ECX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # . EAX = maybe-get-slice(Register, tmp, row-size=8) + # . . push args + 68/push 8/imm32/row-size + 51/push-ECX + 68/push Registers/imm32 + # . . call + e8/call maybe-get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . reclaim tmp + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . restore ECX + 59/pop-to-ECX + # . if (EAX == 0) goto displacement + 3d/compare-EAX-and 0/imm32 + 0f 84/jump-if-equal $parse-effective-address:displacement/disp32 +$parse-effective-address:index: + # read register into index + # . EAX = next-register(word) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call next-register/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . ECX = *EAX + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI + # if (*word->start == ')') goto end + 8a/copy-byte 0/mod/indirect 0/rm32/EAX . . . 0/r32/AL . . # copy byte at *EAX to AL + 81 4/subop/and 3/mod/direct 0/rm32/EAX . . . . . 0xff/imm32 # bitwise and of EAX + 3d/compare-EAX-and 0x29/imm32/close-paren + 74/jump-if-equal $parse-effective-address:end/disp8 +$parse-effective-address:check-for-scale: + # if (*word->start != '<') goto next check + 3d/compare-EAX-and 0x3c/imm32/less-than + 75/jump-if-not-equal $parse-effective-address:check-for-displacement/disp8 + # ++word->start to skip '<' + ff 0/subop/increment 0/mod/indirect 6/rm32/ESI . . . . . . # increment *ESI + # if (*word->start != '<') goto error3 + # ++word->start to skip '<' + ff 0/subop/increment 0/mod/indirect 6/rm32/ESI . . . . . . # increment *ESI + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI +$parse-effective-address:scale: + # read positive integer into scale + # . EAX = next-positive-hex-int(word) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call next-positive-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . EDX = EAX + 89/copy 3/mod/direct 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to EDX + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI + # if (*word->start == ')') goto end + 8a/copy-byte 0/mod/indirect 0/rm32/EAX . . . 0/r32/AL . . # copy byte at *EAX to AL + 81 4/subop/and 3/mod/direct 0/rm32/EAX . . . . . 0xff/imm32 # bitwise and of EAX + 3d/compare-EAX-and 0x29/imm32/close-paren + 74/jump-if-equal $parse-effective-address:end/disp8 +$parse-effective-address:check-for-displacement: + # if (*word->start not in '+' '-') goto error4 +$parse-effective-address:displacement: + # read integer into disp + # . EAX = next-hex-int(word) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . EBX = EAX + 89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX + # skip whitespace + # . EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + ff 6/subop/push 1/mod/*+disp8 6/rm32/ESI . . . . 4/disp8 . # push *(ESI+4) + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . word->start = EAX + 89/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to *ESI + # if (*word->start != ')') goto error5 +$parse-effective-address:end: + # return base in EAX + 89/copy 3/mod/direct 0/rm32/EAX . . . 7/r32/EDI . . # copy EDI to EAX + # . restore registers + 5f/pop-to-EDI + 5e/pop-to-ESI + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# assumes 'in' starts with a register name, and returns pointer to its code +# side-effect: modifies 'in' to scan past the initial register name +next-register: # in : (address slice) -> reg/EAX : int + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 56/push-ESI + # ESI = in + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # var reg-slice/ECX : (address slice) = {in->start, in->start + 3} + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX + 05/add-to-EAX 3/imm32 + 50/push-EAX + ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # in->start += 3 + 81 0/subop/add 0/mod/indirect 6/rm32/ESI . . . . . 3/imm32 # add to *ESI + # EAX = get-slice(Registers, word, row-size=8) + # . . push args + 68/push "next-register"/imm32 + 68/push 8/imm32/row-size + 51/push-ECX + 68/push Registers/imm32 + # . . call + e8/call get-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP +$next-register:end: + # reclaim locals + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . restore registers + 5e/pop-to-ESI + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-simple: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*esi" + b8/copy-to-EAX "*esi"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-simple/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 4, msg) + # . . push args + 68/push "F - test-parse-effective-address-simple/index"/imm32 + 68/push 4/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-simple/scale"/imm32 + 68/push 0/imm32/none + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-simple/displacement"/imm32 + 68/push 0/imm32/none + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-base: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*(esi )" + b8/copy-to-EAX "*(esi )"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-base/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 4, msg) + # . . push args + 68/push "F - test-parse-effective-address-base/index"/imm32 + 68/push 4/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base/scale"/imm32 + 68/push 0/imm32/none + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base/displacement"/imm32 + 68/push 0/imm32/none + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-base-displacement: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*(esi+3)" + b8/copy-to-EAX "*(esi+3)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-displacement/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 4, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-displacement/index"/imm32 + 68/push 4/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-displacement/scale"/imm32 + 68/push 0/imm32/none + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, 3, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-displacement/displacement"/imm32 + 68/push 3/imm32 + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-base-negative-displacement: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*(esi-3)" + b8/copy-to-EAX "*(esi-3)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-negative-displacement/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 4, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-negative-displacement/index"/imm32 + 68/push 4/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-negative-displacement/scale"/imm32 + 68/push 0/imm32/none + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, -3, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-negative-displacement/displacement"/imm32 + 68/push -3/imm32 + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-base-index: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*(esi+ecx)" + b8/copy-to-EAX "*(esi+ecx)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 1, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index/index"/imm32 + 68/push 1/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index/scale"/imm32 + 68/push 0/imm32/none + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index/displacement"/imm32 + 68/push 0/imm32 + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-base-index-scale: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*(esi+ecx<<2)" + b8/copy-to-EAX "*(esi+ecx<<2)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 1, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/index"/imm32 + 68/push 1/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 2, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/scale"/imm32 + 68/push 2/imm32 + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, 0, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/displacement"/imm32 + 68/push 0/imm32 + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-parse-effective-address-base-index-scale-displacement: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # var slice/ECX = "*(esi + ecx<<2 - 0x34)" + b8/copy-to-EAX "*(esi + ecx<<2 - 0x34)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # . ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX, ECX, EDX, EBX = parse-effective-address(slice) + # . . push args + 51/push-ECX + # . . call + e8/call parse-effective-address/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # slice clobbered beyond this point + # check-ints-equal(EAX, 6, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/base"/imm32 + 68/push 6/imm32/ESI + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(ECX, 1, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/index"/imm32 + 68/push 1/imm32/none + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EDX, 2, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/scale"/imm32 + 68/push 2/imm32 + 52/push-EDX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # check-ints-equal(EBX, -0x34, msg) + # . . push args + 68/push "F - test-parse-effective-address-base-index-scale/displacement"/imm32 + 68/push -0x34/imm32 + 53/push-EBX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# Code generation: +# if index is none and disp is 0, then mod = 0 and rm32 = base +# if index is none, then mod = 2 and rm32 = base and disp32 = disp +# if index is not none, then mod = 2 and rm32 = 4 and base = base and index = index and disp32 = disp +emit-indirect-mode: # out : (address buffered-file), base : int, index : int, scale : int, disp : int + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP +$emit-indirect-mode:check-for-sib: + # if (index == 4/none) goto next check + 81 7/subop/compare 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 4/imm32 # compare *(EBP+16) + 0f 84/jump-if-equal $emit-indirect-mode:check-for-disp/disp32 +$emit-indirect-mode:emit-sib: + # emit(out, "2/mod/indirect 4/rm32/sib " base "/base " index "/index " scale "/scale " disp "/disp32") + # . write-buffered(out, "2/mod/*+disp32 4/rm32/sib ") + # . . push args + 68/push "2/mod/*+disp32 4/rm32/sib "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, base) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/base ") + # . . push args + 68/push "/base "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, index) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 . # push *(EBP+16) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/index ") + # . . push args + 68/push "/index "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, scale) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/scale ") + # . . push args + 68/push "/scale "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, disp) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/disp32") + # . . push args + 68/push "/disp32"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + e9/jump $emit-indirect-mode:end/disp32 +$emit-indirect-mode:check-for-disp: + # if (disp == 0) goto next check + 81 7/subop/compare 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 0/imm32 # compare *(EBP+24) + 74/jump-if-equal $emit-indirect-mode:emit-indirect/disp8 +$emit-indirect-mode:emit-disp: + # emit(out, "2/mod/*+disp32 " base "/rm32 " disp "/disp32") + # . write-buffered(out, "2/mod/*+disp32 ") + # . . push args + 68/push "2/mod/*+disp32 "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, base) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/rm32 ") + # . . push args + 68/push "/rm32 "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, disp) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x18/disp8 . # push *(EBP+24) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/disp32") + # . . push args + 68/push "/disp32"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + eb/jump $emit-indirect-mode:end/disp8 +$emit-indirect-mode:emit-indirect: + # emit(out, "0/mod/indirect " base "/rm32") + # . write-buffered(out, "0/mod/indirect ") + # . . push args + 68/push "0/mod/indirect "/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . print-int32-buffered(out, base) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . write-buffered(out, "/rm32") + # . . push args + 68/push "/rm32"/imm32 + ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) + # . . call + e8/call write-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +$emit-indirect-mode:end: + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-indirect-mode: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # emit-indirect-mode(_test-output-buffered-file, 0, 4/none, 0, 0) + # . . write args + 68/push 0/imm32/.disp + 68/push 0/imm32/.scale + 68/push 4/imm32/.index/none + 68/push 0/imm32/.base + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call emit-indirect-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "0/mod/indirect 0/rm32", msg) + # . . push args + 68/push "F - test-emit-indirect-mode"/imm32 + 68/push "0/mod/indirect 0x00000000/rm32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-indirect-mode-2: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # emit-indirect-mode(_test-output-buffered-file, 6, 4/none, 0, 0) + # . . write args + 68/push 0/imm32/.disp + 68/push 0/imm32/.scale + 68/push 4/imm32/.index/none + 68/push 7/imm32/.base + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call emit-indirect-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "0/mod/indirect 7/rm32", msg) + # . . push args + 68/push "F - test-emit-indirect-mode-2"/imm32 + 68/push "0/mod/indirect 0x00000007/rm32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-indirect-mode-with-disp: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # emit-indirect-mode(_test-output-buffered-file, 6, 4/none, 0, 4) + # . . write args + 68/push 4/imm32/.disp + 68/push 0/imm32/.scale + 68/push 4/imm32/.index/none + 68/push 6/imm32/.base + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call emit-indirect-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "2/mod/*+disp32 6/rm32 4/disp32", msg) + # . . push args + 68/push "F - test-emit-indirect-mode-with-disp"/imm32 + 68/push "2/mod/*+disp32 0x00000006/rm32 0x00000004/disp32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-indirect-mode-with-disp-negative: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # emit-indirect-mode(_test-output-buffered-file, 6, 4/none, 0, -4) + # . . write args + 68/push -4/imm32/.disp + 68/push 0/imm32/.scale + 68/push 4/imm32/.index/none + 68/push 6/imm32/.base + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call emit-indirect-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "2/mod/*+disp32 6/rm32 -4/disp32", msg) + # . . push args + 68/push "F - test-emit-indirect-mode-with-disp"/imm32 + 68/push "2/mod/*+disp32 0x00000006/rm32 0xfffffffc/disp32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-emit-indirect-mode-with-sib: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-output-stream) + # . . push args + 68/push _test-output-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . clear-stream(_test-output-buffered-file+4) + # . . push args + b8/copy-to-EAX _test-output-buffered-file/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # emit-indirect-mode(_test-output-buffered-file, 6/base, 1/index, 2/scale, 4/disp) + # . . write args + 68/push 4/imm32/.disp + 68/push 2/imm32/.scale + 68/push 1/imm32/.index + 68/push 6/imm32/.base + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call emit-indirect-mode/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x14/imm32 # add to ESP + # . flush(_test-output-buffered-file) + # . . push args + 68/push _test-output-buffered-file/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP +#? # dump output {{{ +#? # . write(2/stderr, "^") +#? # . . push args +#? 68/push "^"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write-stream(2/stderr, _test-output-stream) +#? # . . push args +#? 68/push _test-output-stream/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write-stream/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP +#? # }}} + # check-stream-equal(_test-output-stream, "2/mod/indirect 4/rm32/sib 6/base 1/index 2/scale 4/disp", msg) + # . . push args + 68/push "F - test-emit-indirect-mode-with-sib"/imm32 + 68/push "2/mod/*+disp32 4/rm32/sib 0x00000006/base 0x00000001/index 0x00000002/scale 0x00000004/disp32"/imm32 + 68/push _test-output-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# update line->read to ')' +# line->read ends at ')' +skip-until-close-paren: # line : (address stream) + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 50/push-EAX + 51/push-ECX + 52/push-EDX + # ECX = line + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX + # EAX = skip-until-close-paren-in-slice(&line->data[line->read], &line->data[line->write]) + # . . push &line->data[line->write] + 8b/copy 1/mod/*+disp8 1/rm32/ECX . . 2/r32/EDX 8/disp8 . # copy *(ECX+8) to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX 0xc/disp8 . # copy ECX+EDX+12 to EDX + 52/push-EDX + # . . push &line->data[line->read] + 8b/copy 1/mod/*+disp8 1/rm32/ECX . . 2/r32/EDX 4/disp8 . # copy *(ECX+4) to EDX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX 0xc/disp8 . # copy ECX+EDX+12 to EDX + 52/push-EDX + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # line->read = EAX - line->data + 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX + 2d/subtract-from-EAX 0xc/imm32 + 89/copy 1/mod/*+disp8 1/rm32/ECX . . 0/r32/EAX 4/disp8 . # copy EAX to *(ECX+4) +$skip-until-close-paren:end: + # . restore registers + 5a/pop-to-EDX + 59/pop-to-ECX + 58/pop-to-EAX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-until-close-paren: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . write(_test-input-stream, "*(abc) def") + # . indices: 0123 45 + # . . push args + 68/push "*(abc) def"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-until-close-paren/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # skip-until-close-paren(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(line->read, 5, msg) + # . . push args + 68/push "F - test-skip-until-close-paren"/imm32 + 68/push 5/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-until-close-paren-ignores-spaces: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . write(_test-input-stream, "*(a b)/yz") + # . . push args + 68/push "*(a b)/yz"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-until-close-paren-ignores-spaces/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # skip-until-close-paren(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(line->read, 5, msg) + # . . push args + 68/push "F - test-skip-until-close-paren-ignores-spaces"/imm32 + 68/push 5/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-until-close-paren-works-from-mid-stream: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup + # . clear-stream(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . write(_test-input-stream, "0 *(a b)/yz") + # . . push args + 68/push "0 *(a b)/yz"/imm32 + 68/push _test-input-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # precondition: _test-input-stream->read == 2 + c7 0/subop/copy 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 2/imm32 # copy to *(EAX+4) + # skip-until-close-paren(_test-input-stream) + # . . push args + 68/push _test-input-stream/imm32 + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(_test-input-stream->read, 7, msg) + # . . push args + 68/push "F - test-skip-until-close-paren-works-from-mid-stream"/imm32 + 68/push 7/imm32 + b8/copy-to-EAX _test-input-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 4/disp8 . # push *(EAX+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +skip-until-close-paren-in-slice: # curr : (address byte), end : (address byte) -> new_curr/EAX + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + # ECX = curr + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX + # EDX = end + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . 2/r32/EDX 0xc/disp8 . # copy *(EBP+12) to EDX + # EAX = 0 + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + # skip initial dquote + 41/increment-ECX +$skip-until-close-paren-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-unsigned-or-equal $skip-until-close-paren-in-slice:break/disp8 + # AL = *curr + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL +$skip-until-close-paren-in-slice:check-close: + # if (EAX == ')') break + 3d/compare-EAX-and 0x29/imm32/close-paren + 74/jump-if-equal $skip-until-close-paren-in-slice:break/disp8 + # ++curr + 41/increment-ECX + eb/jump $skip-until-close-paren-in-slice:loop/disp8 +$skip-until-close-paren-in-slice:break: + # return curr + 89/copy 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # copy ECX to EAX +$skip-until-close-paren-in-slice:end: + # . restore registers + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-until-close-paren-in-slice: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup: (EAX..ECX) = "*(abc) def" + b8/copy-to-EAX "*(abc) def"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # EAX = skip-until-close-paren-in-slice(EAX, ECX) + # . . push args + 51/push-ECX + 50/push-EAX + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(ECX-EAX, 5, msg) # EAX is at the ')' + # . . push args + 68/push "F - test-skip-until-close-paren-in-slice"/imm32 + 68/push 5/imm32 + # . . push ECX-EAX + 29/subtract 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # subtract EAX from ECX + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-until-close-paren-in-slice-ignores-spaces: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup: (EAX..ECX) = "*(a b)/yz" + b8/copy-to-EAX "*(a b)/yz"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # EAX = skip-until-close-paren-in-slice(EAX, ECX) + # . . push args + 51/push-ECX + 50/push-EAX + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(ECX-EAX, 4, msg) # EAX is at the ')' + # . . push args + 68/push "F - test-skip-until-close-paren-in-slice-ignores-spaces"/imm32 + 68/push 4/imm32 + # . . push ECX-EAX + 29/subtract 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # subtract EAX from ECX + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-skip-until-close-paren-in-slice-stops-at-end: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # setup: (EAX..ECX) = "*(abc" # unbalanced dquote + b8/copy-to-EAX "*(abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # EAX = skip-until-close-paren-in-slice(EAX, ECX) + # . . push args + 51/push-ECX + 50/push-EAX + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # check-ints-equal(ECX-EAX, 0, msg) # skipped to end of slice + # . . push args + 68/push "F - test-skip-until-close-paren-in-slice-stops-at-end"/imm32 + 68/push 0/imm32 + # . . push ECX-EAX + 29/subtract 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # subtract EAX from ECX + 51/push-ECX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# assumes 'in' starts with optional '+' or '-', optional whitespace, and an unsigned integer +# returns the value of the integer +# side-effect: modifies 'in' to skip past the integer +next-hex-int: # in : (address slice) -> result/EAX + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 53/push-EBX + 56/push-ESI + 57/push-EDI + # result/EDI = 0 + 31/xor 3/mod/direct 7/rm32/EDI . . . 7/r32/EDI . . # clear EDI + # ESI = in + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # EDX = in->end + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 2/r32/EDX 4/disp8 . # copy *(ESI+4) to EDX + # curr/ECX = in->start + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX + # negate?/EBX = false + 31/xor 3/mod/direct 3/rm32/EBX . . . 3/r32/EBX . . # clear EBX + # EAX = *curr + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL +$next-hex-int:positive: + # if (*curr == '+') ++curr + 3d/compare-EAX-and 0x2b/imm32/+ + 75/jump-if-not-equal $next-hex-int:negative/disp8 + # . ++curr + 41/increment-ECX + eb/jump $next-hex-int:skip-whitespace/disp8 +$next-hex-int:negative: + # else if (*curr == '-') ++curr, negate = true + 3d/compare-EAX-and 0x2d/imm32/- + 75/jump-if-not-equal $next-hex-int:skip-whitespace/disp8 +$next-hex-int:need-to-negate: + # . ++curr + 41/increment-ECX + # . negate = true + bb/copy-to-EBX 1/imm32/true + # fall through +$next-hex-int:skip-whitespace: + # spill EAX + 50/push-EAX + # EAX = skip-chars-matching-whitespace-in-slice(word->start, word->end) + # . . push args + 52/push-EDX + 51/push-ECX + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # ECX = EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 0/r32/EAX . . # copy EAX to ECX + # restore EAX + 58/pop-to-EAX +$next-hex-int:initial-0: + # skip past leading '0x' + # . if (*curr != '0') jump to loop + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + 3d/compare-EAX-and 0x30/imm32/0 + 75/jump-if-not-equal $next-hex-int:loop/disp8 + # . ++curr + 41/increment-ECX +$next-hex-int:initial-0x: + # . if (curr >= in->end) return result + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $next-hex-int:end/disp8 + # . if (*curr != 'x') jump to loop # the previous '0' is still valid so doesn't need to be checked again + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + 3d/compare-EAX-and 0x78/imm32/x + 75/jump-if-not-equal $next-hex-int:loop/disp8 + # . ++curr + 41/increment-ECX +$next-hex-int:loop: + # if (curr >= in->end) break + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $next-hex-int:break/disp8 + # if (!is-hex-digit?(*curr)) break + # . EAX = *curr + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + # . EAX = is-hex-digit?(*curr) + # . . push args + 50/push-EAX + # . . call + e8/call is-hex-digit?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . if (EAX == 0) break + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $next-hex-int:break/disp8 + # EAX = from-hex-char(*curr) + # . . copy arg to EAX + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + # . . call + e8/call from-hex-char/disp32 + # result = result * 16 + EAX + c1/shift 4/subop/left 3/mod/direct 7/rm32/EDI . . . . . 4/imm8 # shift EDI left by 4 bits + 01/add 3/mod/direct 7/rm32/EDI . . . 0/r32/EAX . . # add EAX to EDI + # ++curr + 41/increment-ECX + # loop + eb/jump $next-hex-int:loop/disp8 +$next-hex-int:break: + 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0/imm32 # compare EBX + 74/jump-if-equal $next-hex-int:end/disp8 +$next-hex-int:negate: + f7 3/subop/negate 3/mod/direct 7/rm32/EDI . . . . . . # negate EDI +$next-hex-int:end: + # word->start = curr + 89/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy ECX to *ESI + # return EDI + 89/copy 3/mod/direct 0/rm32/EAX . . . 7/r32/EDI . . # copy EDI to EAX + # . restore registers + 5f/pop-to-EDI + 5e/pop-to-ESI + 5b/pop-to-EBX + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +$next-hex-int:abort: + # . _write(2/stderr, error) + # . . push args + 68/push "next-hex-int: invalid hex char: "/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . clear-stream(Stderr+4) + # . . save EAX + 50/push-EAX + # . . push args + b8/copy-to-EAX Stderr/imm32 + 05/add-to-EAX 4/imm32 + 50/push-EAX + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . . restore EAX + 58/pop-to-EAX + # . print-int32-buffered(Stderr, EAX) + # . . push args + 50/push-EAX + 68/push Stderr/imm32 + # . . call + e8/call print-int32-buffered/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . flush(Stderr) + # . . push args + 68/push Stderr/imm32 + # . . call + e8/call flush/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . _write(2/stderr, "\n") + # . . push args + 68/push "\n"/imm32 + 68/push 2/imm32/stderr + # . . call + e8/call _write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # . syscall(exit, 1) + bb/copy-to-EBX 1/imm32 + b8/copy-to-EAX 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + +test-next-hex-int-single-digit: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "+a)" + b8/copy-to-EAX "+a)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0xa, msg) + # . . push args + 68/push "F - test-next-hex-int-single-digit"/imm32 + 68/push 0xa/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-hex-int-multi-digit: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "+ 34a)" + b8/copy-to-EAX "+ 34a)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0x34a, msg) + # . . push args + 68/push "F - test-next-hex-int-multi-digit"/imm32 + 68/push 0x34a/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-hex-int-0x-prefix: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "+0x34)" + b8/copy-to-EAX "+0x34)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0x34, msg) + # . . push args + 68/push "F - test-next-hex-int-0x-prefix"/imm32 + 68/push 0x34/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-hex-int-zero: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "+0)" + b8/copy-to-EAX "+0)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0, msg) + # . . push args + 68/push "F - test-next-hex-int-zero"/imm32 + 68/push 0/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-hex-int-0-prefix: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "+ 03)" + b8/copy-to-EAX "+ 03)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 3, msg) + # . . push args + 68/push "F - test-next-hex-int-0-prefix"/imm32 + 68/push 3/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-hex-int-negative: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "-03)" + b8/copy-to-EAX "-03)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, -3, msg) + # . . push args + 68/push "F - test-next-hex-int-negative"/imm32 + 68/push -3/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-hex-int-negative-with-space: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "- 03)" + b8/copy-to-EAX "- 03)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, -3, msg) + # . . push args + 68/push "F - test-next-hex-int-negative-with-space"/imm32 + 68/push -3/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +# assumes 'in' starts a positive unsigned integer +# returns the value of the integer +# side-effect: modifies 'in' to skip past the integer +next-positive-hex-int: # in : (address slice) -> result/EAX + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # . save registers + 51/push-ECX + 52/push-EDX + 53/push-EBX + 56/push-ESI + 57/push-EDI + # result/EDI = 0 + 31/xor 3/mod/direct 7/rm32/EDI . . . 7/r32/EDI . . # clear EDI + # ESI = in + 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI + # EDX = in->end + 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 2/r32/EDX 4/disp8 . # copy *(ESI+4) to EDX + # curr/ECX = in->start + 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX + # negate?/EBX = false + 31/xor 3/mod/direct 3/rm32/EBX . . . 3/r32/EBX . . # clear EBX + # EAX = *curr + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL +$next-positive-hex-int:initial-0: + # skip past leading '0x' + # . if (*curr != '0') jump to loop + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + 3d/compare-EAX-and 0x30/imm32/0 + 75/jump-if-not-equal $next-positive-hex-int:loop/disp8 + # . ++curr + 41/increment-ECX +$next-positive-hex-int:initial-0x: + # . if (curr >= in->end) return result + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $next-positive-hex-int:end/disp8 + # . if (*curr != 'x') jump to loop # the previous '0' is still valid so doesn't need to be checked again + 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + 3d/compare-EAX-and 0x78/imm32/x + 75/jump-if-not-equal $next-positive-hex-int:loop/disp8 + # . ++curr + 41/increment-ECX +$next-positive-hex-int:loop: + # if (curr >= in->end) break + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 73/jump-if-greater-or-equal-unsigned $next-positive-hex-int:end/disp8 + # if (!is-hex-digit?(*curr)) break + # . EAX = *curr + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + # . EAX = is-hex-digit?(*curr) + # . . push args + 50/push-EAX + # . . call + e8/call is-hex-digit?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # . if (EAX == 0) break + 3d/compare-EAX-and 0/imm32 + 74/jump-if-equal $next-positive-hex-int:end/disp8 + # EAX = from-hex-char(*curr) + # . . copy arg to EAX + 8a/copy-byte 0/mod/indirect 1/rm32/ECX . . . 0/r32/AL . . # copy byte at *ECX to AL + # . . call + e8/call from-hex-char/disp32 + # result = result * 16 + EAX + c1/shift 4/subop/left 3/mod/direct 7/rm32/EDI . . . . . 4/imm8 # shift EDI left by 4 bits + 01/add 3/mod/direct 7/rm32/EDI . . . 0/r32/EAX . . # add EAX to EDI + # ++curr + 41/increment-ECX + # loop + eb/jump $next-positive-hex-int:loop/disp8 +$next-positive-hex-int:end: + # word->start = curr + 89/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy ECX to *ESI + # return EDI + 89/copy 3/mod/direct 0/rm32/EAX . . . 7/r32/EDI . . # copy EDI to EAX + # . restore registers + 5f/pop-to-EDI + 5e/pop-to-ESI + 5b/pop-to-EBX + 5a/pop-to-EDX + 59/pop-to-ECX + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-positive-hex-int-single-digit: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "a)" + b8/copy-to-EAX "a)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-positive-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-positive-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0xa, msg) + # . . push args + 68/push "F - test-next-positive-hex-int-single-digit"/imm32 + 68/push 0xa/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-positive-hex-int-multi-digit: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "34a)" + b8/copy-to-EAX "34a)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-positive-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-positive-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0x34a, msg) + # . . push args + 68/push "F - test-next-positive-hex-int-multi-digit"/imm32 + 68/push 0x34a/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-positive-hex-int-0x-prefix: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "0x34)" + b8/copy-to-EAX "0x34)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-positive-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-positive-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0x34, msg) + # . . push args + 68/push "F - test-next-positive-hex-int-0x-prefix"/imm32 + 68/push 0x34/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-positive-hex-int-zero: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "0" + b8/copy-to-EAX "0"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-positive-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-positive-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 0, msg) + # . . push args + 68/push "F - test-next-positive-hex-int-zero"/imm32 + 68/push 0/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +test-next-positive-hex-int-0-prefix: + # . prolog + 55/push-EBP + 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP + # (EAX..ECX) = "03)" + b8/copy-to-EAX "03)"/imm32 + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX + 05/add-to-EAX 4/imm32 + # var slice/ECX = {EAX, ECX} + 51/push-ECX + 50/push-EAX + 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX + # EAX = next-positive-hex-int(slice) + # . . push args + 51/push-ECX + # . . call + e8/call next-positive-hex-int/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP + # check-ints-equal(EAX, 3, msg) + # . . push args + 68/push "F - test-next-positive-hex-int-0-prefix"/imm32 + 68/push 3/imm32 + 50/push-EAX + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + # . epilog + 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP + 5d/pop-to-EBP + c3/return + +== data +Registers: # (table string int) + # a table is a stream + 0x40/imm32/write + 0/imm32/read + 0x40/imm32/length + # data + "eax"/imm32 0/imm32 + "ecx"/imm32 1/imm32 + "edx"/imm32 2/imm32 + "ebx"/imm32 3/imm32 + "esp"/imm32 4/imm32 + "ebp"/imm32 5/imm32 + "esi"/imm32 6/imm32 + "edi"/imm32 7/imm32 + +# . . vim:nowrap:textwidth=0 diff --git a/apps/dquotes b/apps/dquotes index b7a4adfe..c3c7cb5e 100755 --- a/apps/dquotes +++ b/apps/dquotes Binary files differdiff --git a/apps/dquotes.subx b/apps/dquotes.subx index d9ed1f05..2fd2d83e 100644 --- a/apps/dquotes.subx +++ b/apps/dquotes.subx @@ -196,6 +196,7 @@ $convert:check-for-comment: 3d/compare-EAX-and 0x23/imm32/hash 74/jump-if-equal $convert:word-loop/disp8 $convert:check-for-string-literal: + # if (slice-starts-with?(word-slice, '"')) continue 3d/compare-EAX-and 0x22/imm32/dquote 75/jump-if-not-equal $convert:regular-word/disp8 $convert:string-literal: @@ -1759,7 +1760,7 @@ test-emit-metadata-in-string-literal: 5d/pop-to-EBP c3/return -# (re)compute the bounds of the next word in the line +# (re)compute the bounds of the next word or string literal in the line # return empty string on reaching end of file next-word-or-string: # line : (address stream byte), out : (address slice) # . prolog @@ -1798,7 +1799,7 @@ $next-word-or-string:check-for-comment: 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 . # copy *(ESI+4) to ECX 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/EAX 0xc/disp8 . # copy ESI+ECX+12 to EAX 89/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy EAX to *EDI - # if line->data[line->read] == '#' + # if (line->data[line->read] != '#') goto next check # . EAX = line->data[line->read] 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy byte at *(ESI+ECX+12) to AL @@ -1816,7 +1817,7 @@ $next-word-or-string:comment: # return eb/jump $next-word-or-string:end/disp8 $next-word-or-string:check-for-string-literal: - # if line->data[line->read] == '"' + # if (line->data[line->read] != '"') goto next check # . EAX = line->data[line->read] 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0xc/disp8 . # copy byte at *(ESI+ECX+12) to AL diff --git a/apps/factorial b/apps/factorial index b82be120..de1fe8c0 100755 --- a/apps/factorial +++ b/apps/factorial Binary files differdiff --git a/apps/handle b/apps/handle index b6794474..6b2f289c 100755 --- a/apps/handle +++ b/apps/handle Binary files differdiff --git a/apps/hex b/apps/hex index 84575cfd..ffbb7850 100755 --- a/apps/hex +++ b/apps/hex Binary files differdiff --git a/apps/hex.subx b/apps/hex.subx index 02f29b5e..a2580e66 100644 --- a/apps/hex.subx +++ b/apps/hex.subx @@ -1480,36 +1480,4 @@ test-skip-until-newline: # . end c3/return -== data - -_test-error-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # line - 0x80/imm32 # 128 bytes - # data (8 lines x 16 bytes/line) - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - -# a test buffered file for _test-error-stream -_test-error-buffered-file: - # file descriptor or (address stream) - _test-error-stream/imm32 - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 6/imm32 - # data - 00 00 00 00 00 00 # 6 bytes - # . . vim:nowrap:textwidth=0 diff --git a/apps/pack b/apps/pack index 720ba3ea..7bf20991 100755 --- a/apps/pack +++ b/apps/pack Binary files differdiff --git a/apps/pack.subx b/apps/pack.subx index 9bd6a21a..9499f3b3 100644 --- a/apps/pack.subx +++ b/apps/pack.subx @@ -58,7 +58,7 @@ $run-main: # configure ed to really exit() # . ed->target = 0 c7 0/subop/copy 0/mod/direct 0/rm32/EAX . . . . . 0/imm32 # copy to *EAX - # return convert(Stdin, 1/stdout, 2/stderr, ed) + # return convert(Stdin, Stdout, Stderr, ed) # . . push args 50/push-EAX/ed 68/push Stderr/imm32 diff --git a/apps/subx-common.subx b/apps/subx-common.subx index abb223fb..062a9464 100644 --- a/apps/subx-common.subx +++ b/apps/subx-common.subx @@ -24,927 +24,6 @@ Max-labels: # . op subop mod rm32 base index scale r32 # . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes -# - managing tables -# SubX has rudimentary support for tables. -# -# Each table is a stream of rows. -# -# Each row consists of a 4-byte row (address to a string) and a variable-size -# value. -# -# Accessing the table performs a linear scan for a key string, and always -# requires passing in the row size. -# -# Table primitives: -# get(stream, string, row-size) -# aborts if not found -# get-or-insert(stream, string, row-size) -# inserts if not found -# get-slice(stream, slice, row-size) -# aborts if not found -# leaky-get-or-insert-slice(stream, slice, row-size) -# inserts if not found - -# 'table' is a stream of (key, value) rows -# keys are always strings (addresses; size 4 bytes) -# values may be any type, but rows (key+value) always occupy 'row-size' bytes -# scan 'table' for a row with a key 'key' and return the address of the corresponding value -# if no row is found, abort -get: # table : (address stream {string, _}), key : (address string), row-size : int -> EAX : (address _) - # pseudocode: - # curr = table->data - # max = &table->data[table->write] - # while curr < max - # if string-equal?(key, *curr) - # return curr+4 - # curr += row-size - # abort - # - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # . save registers - 51/push-ECX - 52/push-EDX - 56/push-ESI - # ESI = table - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI - # curr/ECX = table->data - 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX - # max/EDX = table->data + table->write - 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX - 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX -$get:search-loop: - # if (curr >= max) abort - 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX - 73/jump-if-greater-or-equal-unsigned $get:abort/disp8 - # if (string-equal?(key, *curr)) return curr+4 - # . EAX = string-equal?(key, *curr) - # . . push args - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - # . . call - e8/call string-equal?/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . if (EAX != 0) return EAX = curr+4 - 3d/compare-EAX-and 0/imm32 - 74/jump-if-equal $get:mismatch/disp8 - 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX - eb/jump $get:end/disp8 -$get:mismatch: - # curr += row-size - 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX - # loop - eb/jump $get:search-loop/disp8 -$get:end: - # . restore registers - 5e/pop-to-ESI - 5a/pop-to-EDX - 59/pop-to-ECX - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -$get:abort: - # . _write(2/stderr, error) - # . . push args - 68/push "get: key not found: "/imm32 - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . _write(2/stderr, key) - # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . _write(2/stderr, "\n") - # . . push args - 68/push "\n"/imm32 - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . syscall(exit, 1) - bb/copy-to-EBX 1/imm32 - b8/copy-to-EAX 1/imm32/exit - cd/syscall 0x80/imm8 - # never gets here - -test-get: - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # - setup: create a table with a couple of keys - # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) - 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP - 68/push 0x10/imm32/length - 68/push 0/imm32/read - 68/push 0/imm32/write - 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX - # insert(table, "code", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "code"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # insert(table, "data", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "data"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get:check1: - # EAX = get(table, "code", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "code"/imm32 - 51/push-ECX - # . . call - e8/call get/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 4, msg) - # . check-ints-equal(EAX - table, 16, msg) - # . . push args - 68/push "F - test-get/0"/imm32 - 68/push 0x10/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get:check2: - # EAX = get(table, "data", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "data"/imm32 - 51/push-ECX - # . . call - e8/call get/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 12, msg) - # . check-ints-equal(EAX - table, 24, msg) - # . . push args - 68/push "F - test-get/1"/imm32 - 68/push 0x18/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get:end: - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -# 'table' is a stream of (key, value) rows -# keys are always strings (addresses; size 4 bytes) -# values may be any type, but rows (key+value) always occupy 'row-size' bytes -# scan 'table' for a row with a key 'key' and return the address of the corresponding value -# if no row is found, abort -get-slice: # table : (address stream {string, _}), key : (address slice), row-size : int -> EAX : (address _) - # pseudocode: - # curr = table->data - # max = &table->data[table->write] - # while curr < max - # if slice-equal?(key, *curr) - # return curr+4 - # curr += row-size - # abort - # - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # . save registers - 51/push-ECX - 52/push-EDX - 56/push-ESI - # ESI = table - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI - # curr/ECX = table->data - 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX - # max/EDX = table->data + table->write - 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX - 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX -$get-slice:search-loop: - # if (curr >= max) abort - 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX - 73/jump-if-greater-or-equal-unsigned $get-slice:abort/disp8 - # if (slice-equal?(key, *curr)) return curr+4 - # . EAX = slice-equal?(key, *curr) - # . . push args - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - # . . call - e8/call slice-equal?/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . if (EAX != 0) return EAX = curr+4 - 3d/compare-EAX-and 0/imm32 - 74/jump-if-equal $get-slice:mismatch/disp8 - 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX - eb/jump $get-slice:end/disp8 -$get-slice:mismatch: - # curr += row-size - 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX - # loop - eb/jump $get-slice:search-loop/disp8 -$get-slice:end: - # . restore registers - 5e/pop-to-ESI - 5a/pop-to-EDX - 59/pop-to-ECX - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -$get-slice:abort: - # . _write(2/stderr, error) - # . . push args - 68/push "get-slice: key not found: "/imm32 - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . write-slice-buffered(Stderr, key) - # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - 68/push Stderr/imm32 - # . . call - e8/call write-slice-buffered/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . flush(Stderr) - # . . push args - 68/push Stderr/imm32 - # . . call - e8/call flush/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP - # . _write(2/stderr, "\n") - # . . push args - 68/push "\n"/imm32 - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . syscall(exit, 1) - bb/copy-to-EBX 1/imm32 - b8/copy-to-EAX 1/imm32/exit - cd/syscall 0x80/imm8 - # never gets here - -test-get-slice: - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # - setup: create a table with a couple of keys - # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) - 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP - 68/push 0x10/imm32/length - 68/push 0/imm32/read - 68/push 0/imm32/write - 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX - # insert(table, "code", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "code"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # insert(table, "data", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "data"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-slice:check1: - # (EAX..EDX) = "code" - b8/copy-to-EAX "code"/imm32 - 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX - 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX - 05/add-to-EAX 4/imm32 - # var slice/EDX = {EAX, EDX} - 52/push-EDX - 50/push-EAX - 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX - # EAX = get-slice(table, "code", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 52/push-EDX - 51/push-ECX - # . . call - e8/call get-slice/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned - # . check-ints-equal(EAX - table, 16, msg) - # . . push args - 68/push "F - test-get-slice/0"/imm32 - 68/push 0x10/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-slice:check2: - # (EAX..EDX) = "data" - b8/copy-to-EAX "data"/imm32 - 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX - 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX - 05/add-to-EAX 4/imm32 - # var slice/EDX = {EAX, EDX} - 52/push-EDX - 50/push-EAX - 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX - # EAX = get-slice(table, "data" slice, 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 52/push-EDX - 51/push-ECX - # . . call - e8/call get-slice/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 12, msg) - # . check-ints-equal(EAX - table, 24, msg) - # . . push args - 68/push "F - test-get-slice/1"/imm32 - 68/push 0x18/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-slice:end: - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -# 'table' is a stream of (key, value) rows -# keys are always strings (addresses; size 4 bytes) -# values may be any type, but rows (key+value) always occupy 'row-size' bytes -# scan 'table' for a row with a key 'key' and return the address of the corresponding value -# if no row is found, save 'key' to the next available row -# if there are no rows free, abort -# return the address of the value -# Beware: assume keys are immutable; they're inserted by reference -# TODO: pass in an allocation descriptor -get-or-insert: # table : (address stream {string, _}), key : (address string), row-size : int -> EAX : (address _) - # pseudocode: - # curr = table->data - # max = &table->data[table->write] - # while curr < max - # if string-equal?(key, *curr) - # return curr+4 - # curr += row-size - # if table->write >= table->length - # abort - # zero-out(max, row-size) - # *max = key - # table->write += row-size - # return max+4 - # - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # . save registers - 51/push-ECX - 52/push-EDX - 56/push-ESI - # ESI = table - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI - # curr/ECX = table->data - 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX - # max/EDX = table->data + table->write - 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX - 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX -$get-or-insert:search-loop: - # if (curr >= max) break - 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX - 73/jump-if-greater-or-equal-unsigned $get-or-insert:not-found/disp8 - # if (string-equal?(key, *curr)) return curr+4 - # . EAX = string-equal?(key, *curr) - # . . push args - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - # . . call - e8/call string-equal?/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . if (EAX != 0) return EAX = curr+4 - 3d/compare-EAX-and 0/imm32 - 74/jump-if-equal $get-or-insert:mismatch/disp8 - 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX - eb/jump $get-or-insert:end/disp8 -$get-or-insert:mismatch: - # curr += row-size - 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX - # loop - eb/jump $get-or-insert:search-loop/disp8 -$get-or-insert:not-found: - # result/EAX = 0 - 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX - # if (table->write >= table->length) abort - 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX - 3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(ESI+8) - 73/jump-if-greater-or-equal-unsigned $get-or-insert:abort/disp8 - # zero-out(max, row-size) - # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 . # push *(EBP+16) - 52/push-EDX - # . . call - e8/call zero-out/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # *max = key - # . EAX = key - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX - # . *max = EAX - 89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX - # table->write += row-size - # . EAX = row-size - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX - # . table->write += EAX - 01/add 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # add EAX to *ESI - # return max+4 - # . EAX = max - 89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX - # . EAX += 4 - 05/add-to-EAX 4/imm32 -$get-or-insert:end: - # . restore registers - 5e/pop-to-ESI - 5a/pop-to-EDX - 59/pop-to-ECX - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -$get-or-insert:abort: - # . _write(2/stderr, error) - # . . push args - 68/push "get-or-insert: table is full\n"/imm32 - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . syscall(exit, 1) - bb/copy-to-EBX 1/imm32 - b8/copy-to-EAX 1/imm32/exit - cd/syscall 0x80/imm8 - # never gets here - -test-get-or-insert: - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) - 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP - 68/push 0x10/imm32/length - 68/push 0/imm32/read - 68/push 0/imm32/write - 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX -$test-get-or-insert:first-call: - # - start with an empty table, insert one key, verify that it was inserted - # EAX = get-or-insert(table, "code", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "code"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned - # . check-ints-equal(EAX - table, 16, msg) - # . . push args - 68/push "F - test-get-or-insert/0"/imm32 - 68/push 0x10/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-or-insert:check2: - # check-ints-equal(table->write, row-size = 8, msg) - # . . push args - 68/push "F - test-get-or-insert/1"/imm32 - 68/push 8/imm32/row-size - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-string-equal(*table->data, "code", msg) - # . . push args - 68/push "F - test-get-or-insert/2"/imm32 - 68/push "code"/imm32 - ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) - # . . call - e8/call check-string-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-or-insert:second-call: - # - insert the same key again, verify that it was reused - # EAX = get-or-insert(table, "code", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "code"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 4, msg) - # . check-ints-equal(EAX - table, 16, msg) - # . . push args - 68/push "F - test-get-or-insert/3"/imm32 - 68/push 0x10/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # no new row inserted - # . check-ints-equal(table->write, row-size = 8, msg) - # . . push args - 68/push "F - test-get-or-insert/4"/imm32 - 68/push 8/imm32/row-size - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-string-equal(*table->data, "code", msg) - # . . push args - 68/push "F - test-get-or-insert/5"/imm32 - 68/push "code"/imm32 - ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) - # . . call - e8/call check-string-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-or-insert:third-call: - # - insert a new key, verify that it was inserted - # EAX = get-or-insert(table, "data", 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 68/push "data"/imm32 - 51/push-ECX - # . . call - e8/call get-or-insert/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # table gets a new row - # check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned - # . check-ints-equal(EAX - table, 24, msg) - # . . push args - 68/push "F - test-get-or-insert/6"/imm32 - 68/push 0x18/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(table->write, 2 rows = 16, msg) - # . . push args - 68/push "F - test-get-or-insert/7"/imm32 - 68/push 0x10/imm32/two-rows - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-string-equal(*table->data+8, "data", msg) - # check-string-equal(*(table+20), "data", msg) - # . . push args - 68/push "F - test-get-or-insert/8"/imm32 - 68/push "data"/imm32 - ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20) - # . . call - e8/call check-string-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-get-or-insert:end: - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -# 'table' is a stream of (key, value) rows -# keys are always strings (addresses; size 4 bytes) -# values may be any type, but rows (key+value) always occupy 'row-size' bytes -# scan 'table' for a row with a key 'key' and return the address of the corresponding value -# if no row is found, save 'key' in the next available row -# if there are no rows free, abort -# WARNING: leaks memory -# TODO: pass in an allocation descriptor -leaky-get-or-insert-slice: # table : (address stream {string, _}), key : (address slice), row-size : int -> EAX : (address _) - # pseudocode: - # curr = table->data - # max = &table->data[table->write] - # while curr < max - # if slice-equal?(key, *curr) - # return curr+4 - # curr += row-size - # if table->write >= table->length - # abort - # zero-out(max, row-size) - # *max = slice-to-string(Heap, key) - # table->write += row-size - # return max+4 - # - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # . save registers - 51/push-ECX - 52/push-EDX - 56/push-ESI - # ESI = table - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI - # curr/ECX = table->data - 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX - # max/EDX = table->data + table->write - 8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX - 8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX -$leaky-get-or-insert-slice:search-loop: - # if (curr >= max) break - 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX - 73/jump-if-greater-or-equal-unsigned $leaky-get-or-insert-slice:not-found/disp8 - # if (slice-equal?(key, *curr)) return curr+4 - # . EAX = slice-equal?(key, *curr) - # . . push args - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - # . . call - e8/call slice-equal?/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . if (EAX != 0) return EAX = curr+4 - 3d/compare-EAX-and 0/imm32 - 74/jump-if-equal $leaky-get-or-insert-slice:mismatch/disp8 - 8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX - eb/jump $leaky-get-or-insert-slice:end/disp8 -$leaky-get-or-insert-slice:mismatch: - # curr += row-size - 03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX - # loop - eb/jump $leaky-get-or-insert-slice:search-loop/disp8 -$leaky-get-or-insert-slice:not-found: - # result/EAX = 0 - 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX - # if (table->write >= table->length) abort - 8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX - 3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(ESI+8) - 7d/jump-if-greater-or-equal $leaky-get-or-insert-slice:abort/disp8 - # zero-out(max, row-size) - # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x10/disp8 . # push *(EBP+16) - 52/push-EDX - # . . call - e8/call zero-out/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # *max = slice-to-string(Heap, key) - # . EAX = slice-to-string(Heap, key) - # . . push args - ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) - 68/push Heap/imm32 - # . . call - e8/call slice-to-string/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . *max = EAX - 89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX - # table->write += row-size - # . EAX = row-size - 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX - # . table->write += EAX - 01/add 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # add EAX to *ESI - # return max+4 - # . EAX = max - 89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX - # . EAX += 4 - 05/add-to-EAX 4/imm32 -$leaky-get-or-insert-slice:end: - # . restore registers - 5e/pop-to-ESI - 5a/pop-to-EDX - 59/pop-to-ECX - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - -$leaky-get-or-insert-slice:abort: - # . _write(2/stderr, error) - # . . push args - 68/push "leaky-get-or-insert-slice: table is full\n"/imm32 - 68/push 2/imm32/stderr - # . . call - e8/call _write/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP - # . syscall(exit, 1) - bb/copy-to-EBX 1/imm32 - b8/copy-to-EAX 1/imm32/exit - cd/syscall 0x80/imm8 - # never gets here - -test-leaky-get-or-insert-slice: - # . prolog - 55/push-EBP - 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes) - 81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP - 68/push 0x10/imm32/length - 68/push 0/imm32/read - 68/push 0/imm32/write - 89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX - # (EAX..EDX) = "code" - b8/copy-to-EAX "code"/imm32 - 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX - 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX - 05/add-to-EAX 4/imm32 - # var slice/EDX = {EAX, EDX} - 52/push-EDX - 50/push-EAX - 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX -$test-leaky-get-or-insert-slice:first-call: - # - start with an empty table, insert one key, verify that it was inserted - # EAX = leaky-get-or-insert-slice(table, "code" slice, 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 52/push-EDX - 51/push-ECX - # . . call - e8/call leaky-get-or-insert-slice/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned - # . check-ints-equal(EAX - table, 16, msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/0"/imm32 - 68/push 0x10/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-leaky-get-or-insert-slice:check2: - # check-ints-equal(table->write, row-size = 8, msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/1"/imm32 - 68/push 8/imm32/row-size - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-string-equal(*table->data, "code", msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/2"/imm32 - 68/push "code"/imm32 - ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) - # . . call - e8/call check-string-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-leaky-get-or-insert-slice:second-call: - # - insert the same key again, verify that it was reused - # EAX = leaky-get-or-insert-slice(table, "code" slice, 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 52/push-EDX - 51/push-ECX - # . . call - e8/call leaky-get-or-insert-slice/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(EAX - table->data, 4, msg) - # . check-ints-equal(EAX - table, 16, msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/3"/imm32 - 68/push 0x10/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # no new row inserted - # . check-ints-equal(table->write, row-size = 8, msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/4"/imm32 - 68/push 8/imm32/row-size - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-string-equal(*table->data, "code", msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/5"/imm32 - 68/push "code"/imm32 - ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12) - # . . call - e8/call check-string-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-leaky-get-or-insert-slice:third-call: - # - insert a new key, verify that it was inserted - # (EAX..EDX) = "data" - b8/copy-to-EAX "data"/imm32 - 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX - 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 2/index/EDX . 2/r32/EDX 4/disp8 . # copy EAX+EDX+4 to EDX - 05/add-to-EAX 4/imm32 - # var slice/EDX = {EAX, EDX} - 52/push-EDX - 50/push-EAX - 89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX - # EAX = leaky-get-or-insert-slice(table, "data" slice, 8 bytes per row) - # . . push args - 68/push 8/imm32/row-size - 52/push-EDX - 51/push-ECX - # . . call - e8/call leaky-get-or-insert-slice/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # table gets a new row - # check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned - # . check-ints-equal(EAX - table, 24, msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/6"/imm32 - 68/push 0x18/imm32 - 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX - 50/push-EAX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-ints-equal(table->write, 2 rows = 16, msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/7"/imm32 - 68/push 0x10/imm32/two-rows - ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX - # . . call - e8/call check-ints-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP - # check-string-equal(*table->data+8, "data", msg) - # check-string-equal(*(table+20), "data", msg) - # . . push args - 68/push "F - test-leaky-get-or-insert-slice/8"/imm32 - 68/push "data"/imm32 - ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20) - # . . call - e8/call check-string-equal/disp32 - # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP -$test-leaky-get-or-insert-slice:end: - # . epilog - 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP - 5d/pop-to-EBP - c3/return - # (re)compute the bounds of the next word in the line # return empty string on reaching end of file next-word: # line : (address stream byte), out : (address slice) @@ -3019,98 +2098,6 @@ $test-is-label?:false: == data -_test-input-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 0x100/imm32 # 256 bytes - # data (16 lines x 16 bytes/line) - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - -# a test buffered file for _test-input-stream -_test-input-buffered-file: - # file descriptor or (address stream) - _test-input-stream/imm32 - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 6/imm32 - # data - 00 00 00 00 00 00 # 6 bytes - -_test-output-stream: - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 0x200/imm32 # 512 bytes - # data (32 lines x 16 bytes/line) - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - -# a test buffered file for _test-output-stream -_test-output-buffered-file: - # file descriptor or (address stream) - _test-output-stream/imm32 - # current write index - 0/imm32 - # current read index - 0/imm32 - # length - 6/imm32 - # data - 00 00 00 00 00 00 # 6 bytes - _test-data-segment: 64/d 61/a 74/t 61/a _test-data-segment-end: diff --git a/apps/survey b/apps/survey index 621efa5c..f0742f44 100755 --- a/apps/survey +++ b/apps/survey Binary files differdiff --git a/apps/survey.subx b/apps/survey.subx index 14f4eb55..d226ecc0 100644 --- a/apps/survey.subx +++ b/apps/survey.subx @@ -61,7 +61,6 @@ Entry: # run tests if necessary, convert stdin if not # . prolog 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP - # initialize heap # - if argc > 1 and argv[1] == "test", then return run_tests() # . argc > 1 81 7/subop/compare 1/mod/*+disp8 5/rm32/EBP . . . . 0/disp8 1/imm32 # compare *EBP @@ -1215,6 +1214,7 @@ $compute-offsets:end: 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP 5d/pop-to-EBP c3/return + $compute-offsets:abort: # . _write(2/stderr, error) # . . push args @@ -1435,7 +1435,7 @@ compute-addresses: # segments : (address stream {string, segment-info}), labels # while true # if (lrow >= max) break # seg-name : (address string) = lrow->segment-name - # label-seg : (address segment-info) = get(segments, seg-name, row-size=16) + # label-seg : (address segment-info) = get(segments, seg-name) # lrow->address = label-seg->address + lrow->segment-offset # trace-sssns("label " lrow->key " is at address " lrow->address) # lrow += 16 # row-size @@ -1589,18 +1589,19 @@ $compute-addresses:label-loop: #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # }}} - # label-seg/EDX : (address segment-info) = get(segments, seg-name, row-size=16) + # label-seg/EDX : (address segment-info) = get(segments, seg-name, row-size=16, "segment table") # . save EAX 50/push-EAX # . EAX = get(segments, seg-name, row-size=16) # . . push args + 68/push "segment table"/imm32 68/push 0x10/imm32/row-size 52/push-EDX ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) # . . call e8/call get/disp32 # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP # . EDX = EAX 89/copy 3/mod/direct 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to EDX # . restore EAX @@ -2264,16 +2265,17 @@ $emit-segments:check-metadata: #? # . . discard args #? 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP #? # }}} - # info/ESI = get-slice(labels, datum, row-size=16) - # . EAX = get-slice(labels, datum, row-size=16) + # info/ESI = get-slice(labels, datum, row-size=16, "label table") + # . EAX = get-slice(labels, datum, row-size=16, "label table") # . . push args + 68/push "label table"/imm32 68/push 0x10/imm32/row-size 57/push-EDI ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0x14/disp8 . # push *(EBP+20) # . . call e8/call get-slice/disp32 # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP # . ESI = EAX 89/copy 3/mod/direct 6/rm32/ESI . . . 0/r32/EAX . . # copy EAX to ESI $emit-segments:check-global-variable: @@ -3377,15 +3379,16 @@ emit-elf-header: # out : (address buffered-file), segments : (address stream {s # *Elf_e_entry = get(labels, "Entry")->address # . EAX = labels 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX - # . label-info/EAX = get(labels, "Entry", row-size=16) + # . label-info/EAX = get(labels, "Entry", row-size=16, "label table") # . . push args + 68/push "label table"/imm32 68/push 0x10/imm32/row-size 68/push "Entry"/imm32 50/push-EAX # . . call e8/call get/disp32 # . . discard args - 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP # . EAX = label-info->address 8b/copy 1/mod/*+disp8 0/rm32/EAX . . . 0/r32/EAX 8/disp8 . # copy *(EAX+8) to EAX # . *Elf_e_entry = EAX diff --git a/apps/tests b/apps/tests index 52af8441..a4888fee 100755 --- a/apps/tests +++ b/apps/tests Binary files differdiff --git a/build b/build index deee6e64..3d2e945e 100755 --- a/build +++ b/build @@ -109,7 +109,7 @@ older_than subx_bin subx.cc *_list && { # We ought to always rebuild all apps if any .subx layers are updated. # But during development it's too slow to update _all_ apps when we're # repeatedly running a single one. -if [ $ALL ] +if [ ! $ONLY_CPP ] then # Assumption: SubX programs don't need to be retranslated every time we @@ -139,6 +139,14 @@ then } done + # higher-level syntax + for phase in desugar + do + older_than apps/$phase apps/$phase.subx apps/subx-common.subx [0-9]*.subx && { + ./subx_bin translate [0-9]*.subx apps/subx-common.subx apps/$phase.subx -o apps/$phase + } + done + fi exit 0 diff --git a/clean b/clean index 2d17099f..69255bef 100755 --- a/clean +++ b/clean @@ -6,4 +6,5 @@ rm -rf subx.cc subx_bin* *_list rm -rf .until test $# -gt 0 && exit 0 # convenience: 'clean top-level' to leave subsidiary tools alone rm -rf enumerate/enumerate tangle/tangle tangle/*_list */*.dSYM termbox/*.[oa] +rm -rf browse_trace/browse_trace_bin browse_trace/*_list rm -rf tmp mu.iso diff --git a/run_one_test b/run_one_test index 41844d82..8d69ad69 100755 --- a/run_one_test +++ b/run_one_test @@ -9,14 +9,14 @@ then echo $FILES > /tmp/last_run_files elif [[ -e /tmp/last_run_files ]] then - FILES=`cat /tmp/last_run_files` + FILES=$(cat /tmp/last_run_files) else echo "no test found" exit 0 # don't open trace fi set -e - # turn newlines into spaces -CFLAGS=$CFLAGS ./subx --debug translate $(echo $FILES) /tmp/run_one_test.subx -o /tmp/a.elf + # turn newlines into spaces +ONLY_CPP=1 CFLAGS=$CFLAGS ./subx --debug translate $(echo $FILES) /tmp/run_one_test.subx -o /tmp/a.elf -./subx --debug --trace run /tmp/a.elf +ONLY_CPP=1 ./subx --debug --trace run /tmp/a.elf diff --git a/test_apps b/test_apps index 79a95196..91929b6d 100755 --- a/test_apps +++ b/test_apps @@ -23,6 +23,8 @@ test $NATIVE && echo 'testing native runs' echo "== translating and running using C++" +# example programs + echo ex1 ./subx translate examples/ex1.subx -o examples/ex1 test "$1" = 'record' || git diff --exit-code examples/ex1 @@ -163,6 +165,8 @@ test "$1" = 'record' || git diff --exit-code examples/ex12 test $EMULATED && ./subx run examples/ex12 # final byte of mmap'd address is well-nigh guaranteed to be 0 test $NATIVE && examples/ex12 +# Larger apps that use the standard library. + echo factorial ./subx translate 0*.subx apps/factorial.subx -o apps/factorial test "$1" = 'record' || git diff --exit-code apps/factorial @@ -217,6 +221,8 @@ test $NATIVE && { grep -q 'lookup failed' handle.out || { echo "missing failure test"; exit 1; } } +# Phases of the self-hosted SubX translator. + echo hex ./subx translate 0*.subx apps/subx-common.subx apps/hex.subx -o apps/hex test "$1" = 'record' || git diff --exit-code apps/hex @@ -289,8 +295,22 @@ test $NATIVE && { echo } +# Higher-level syntax. + +echo desugar +./subx translate 0*.subx apps/subx-common.subx apps/desugar.subx -o apps/desugar +[ "$1" != record ] && git diff --exit-code apps/desugar +./subx run apps/desugar test +echo +test `uname` = 'Linux' && { + apps/desugar test + echo +} + echo "== translating using SubX" +# example programs + echo ex1 test $EMULATED && { cat examples/ex1.subx |./subx_bin run apps/tests |./subx_bin run apps/dquotes |./subx_bin run apps/assort |./subx_bin run apps/pack |./subx_bin run apps/survey |./subx_bin run apps/hex |diff examples/ex1 - @@ -407,4 +427,9 @@ cat 0*.subx apps/subx-common.subx apps/dquotes.subx |apps/tests |apps/dquotes |a echo tests cat 0*.subx apps/subx-common.subx apps/tests.subx |apps/tests |apps/dquotes |apps/assort |apps/pack |apps/survey |apps/hex |diff apps/tests - +# Higher-level syntax. + +echo desugar +cat 0*.subx apps/subx-common.subx apps/desugar.subx |apps/tests |apps/dquotes |apps/assort |apps/pack |apps/survey |apps/hex |diff apps/desugar - + exit 0 |