// Warning: zero automated tests // Includes #include "termbox/termbox.h" #include #define SIZE(X) (assert((X).size() < (1LL<<(sizeof(int)*8-2))), static_cast((X).size())) #include #include using std::istream; using std::ostream; using std::iostream; using std::cin; using std::cout; using std::cerr; #include #include #include using std::string; #include using std::vector; #include using std::set; #include using std::ostringstream; #include using std::ifstream; using std::ofstream; #include using std::map; #include using std::pair; // End Includes // Types struct trace_line { string contents; string label; int depth; // 0 is 'sea level'; positive integers are progressively 'deeper' and lower level trace_line(string c, string l, int d) { contents = c; label = l; depth = d; } }; struct trace_stream { vector past_lines; }; enum search_direction { FORWARD, BACKWARD }; // End Types // from http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map template typename T::mapped_type& get(T& map, typename T::key_type const& key) { typename T::iterator iter(map.find(key)); assert(iter != map.end()); return iter->second; } template typename T::mapped_type const& get(const T& map, typename T::key_type const& key) { typename T::const_iterator iter(map.find(key)); assert(iter != map.end()); return iter->second; } template typename T::mapped_type const& put(T& map, typename T::key_type const& key, typename T::mapped_type const& value) { // map[key] requires mapped_type to have a zero-arg (default) constructor map.insert(std::make_pair(key, value)).first->second = value; return value; } template bool contains_key(T& map, typename T::key_type const& key) { return map.find(key) != map.end(); } template typename T::mapped_type& get_or_insert(T& map, typename T::key_type const& key) { return map[key]; } // Globals trace_stream* Trace_stream = NULL; ofstream Trace_file; int Cursor_row = 0; // screen coordinate set Visible; int Top_of_screen = 0; // trace coordinate int Left_of_screen = 0; // trace coordinate int Last_printed_row = 0; // screen coordinate map Trace_index; // screen row -> trace index string Current_search_pattern = ""; search_direction Current_search_direction = FORWARD; // End Globals bool has_data(istream& in) { return in && !in.eof(); } void skip_whitespace_but_not_newline(istream& in) { while (true) { if (!has_data(in)) break; else if (in.peek() == '\n') break; else if (isspace(in.peek())) in.get(); else break; } } void load_trace(const char* filename) { ifstream tin(filename); if (!tin) { cerr << "no such file: " << filename << '\n'; exit(1); } Trace_stream = new trace_stream; while (has_data(tin)) { tin >> std::noskipws; skip_whitespace_but_not_newline(tin); if (!isdigit(tin.peek())) { string dummy; getline(tin, dummy); continue; } tin >> std::skipws; int depth; tin >> depth; string label; tin >> label; if (*--label.end() == ':') label.erase(--label.end()); string line; getline(tin, line); Trace_stream->past_lines.push_back(trace_line(line, label, depth)); } cerr << "lines read: " << Trace_stream->past_lines.size() << '\n'; } void refresh_screen_rows() { // Top_of_screen, Visible -> Trace_index int screen_row = 0, index = 0; Trace_index.clear(); for (screen_row = 0, index = Top_of_screen; screen_row < tb_height() && index < SIZE(Trace_stream->past_lines); ++screen_row, ++index) { // skip lines without depth for now while (!contains_key(Visible, index)) { ++index; if (index >= SIZE(Trace_stream->past_lines)) goto done; } assert(index < SIZE(Trace_stream->past_lines)); put(Trace_index, screen_row, index); } done:; } void clear_line(int screen_row) { // -> screen tb_set_cursor(0, screen_row); for (int col = 0; col < tb_width(); ++col) tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(0, screen_row); } int read_key() { tb_event event; do { tb_poll_event(&event); } while (event.type != TB_EVENT_KEY); return event.key ? event.key : event.ch; } int lines_hidden(int screen_row) { assert(contains_key(Trace_index, screen_row)); if (!contains_key(Trace_index, screen_row+1)) return SIZE(Trace_stream->past_lines) - get(Trace_index, screen_row); else return get(Trace_index, screen_row+1) - get(Trace_index, screen_row); } bool in_range(const vector >& highlight_ranges, size_t idx) { for (int i = 0; i < SIZE(highlight_ranges); ++i) { if (idx >= highlight_ranges.at(i).first && idx < highlight_ranges.at(i).second) return true; if (idx < highlight_ranges.at(i).second) break; } return false; } vector > find_all_occurrences(const string& s, const string& pat) { vector > result; if (pat.empty()) return result; size_t idx = 0; while (true) { size_t next_idx = s.find(pat, idx); if (next_idx == string::npos) break; result.push_back(pair(next_idx, next_idx+SIZE(pat))); idx = next_idx+SIZE(pat); } return result; } int bg_color(int depth, int trace_index, int screen_row) { if (screen_row == Cursor_row) { if (trace_index == 0) return /*subtle grey*/240; // ignore the zero-depth sentinel at start of trace if (depth > 0) return /*subtle grey*/240; else return /*subtle red*/88; } if (trace_index == 0) return TB_BLACK; // ignore the zero-depth sentinel at start of trace if (depth == 0) return /*red*/1; if (depth == 1) return /*orange*/166; // start at black, gradually lighten at deeper levels return TB_BLACK + ((depth-2) % 6)*2; } void render_line(int screen_row, const string& s, int bg) { // -> screen int col = 0; int color = TB_WHITE; vector > highlight_ranges = find_all_occurrences(s, Current_search_pattern); tb_set_cursor(0, screen_row); for (col = 0; col < tb_width(); ++col) { char c = ' '; if (col+Left_of_screen < SIZE(s)) c = s.at(col+Left_of_screen); // todo: unicode if (c == '\n') c = ';'; // replace newlines with semi-colons // escapes. hack: can't start a line with them. if (c == '\1') { color = /*red*/1; continue; } if (c == '\2') { color = TB_WHITE; continue; } if (in_range(highlight_ranges, col+Left_of_screen)) tb_print(c, TB_BLACK, /*yellow*/11); else tb_print(c, color, bg); } } void search_next(const string& pat) { for (int trace_index = get(Trace_index, Cursor_row)+1; trace_index < SIZE(Trace_stream->past_lines); ++trace_index) { if (!contains_key(Visible, trace_index)) continue; const t
# Helper to check an array's bounds, and to abort if they're violated.
# Really only intended to be called from code generated by mu.subx.

== code

__check-mu-array-bounds:  # index: int, elem-size: int, arr-size: int, function-name: (addr array byte), array-name: (addr array byte)
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    # . save registers
    50/push-eax
    51/push-ecx
    52/push-edx
    # . not bothering saving ebx; it's only clobbered if we're going to abort
    # ecx = arr-size
    8b/-> *(ebp+0x10) 1/r32/ecx
    # var overflow/edx: int = 0
    ba/copy-to-edx 0/imm32
    # var offset/eax: int = index * elem-size
    8b/-> *(ebp+8) 0/r32/eax
    f7 4/subop/multiply-eax-with *(ebp+0xc)
    # check for overflow
    81 7/subop/compare %edx 0/imm32
    0f 85/jump-if-!= __check-mu-array-bounds:overflow/disp32
    # check bounds
    39/compare %eax 1/r32/ecx
    0f 82/jump-if-unsigned< $__check-mu-array-bounds:end/disp32  # negative index should always abort
    # abort if necessary
    (write-buffered Stderr "fn ")
    (write-buffered Stderr *(ebp+0x14))
    (write-buffered Stderr ": offset ")
    (write-int32-hex-buffered Stderr %eax)
    (write-buffered Stderr " is too large for array '")
    (write-buffered Stderr *(ebp+0x18))
    (write-buffered Stderr "'\n")
    (flush Stderr)
    # exit(1)
    bb/copy-to-ebx 1/imm32
    e8/call syscall_exit/disp32
    # never gets here
$__check-mu-array-bounds:end:
    # . restore registers
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

__check-mu-array-bounds:overflow:
    # "fn " function-name ": offset to array '" array-name "' overflowed 32 bits\n"
    (write-buffered Stderr "fn ")
    (write-buffered Stderr *(ebp+0x14))
    (write-buffered Stderr ": offset to array '")
    (write-buffered Stderr *(ebp+0x18))
    (write-buffered Stderr "' overflowed 32 bits\n")
    (flush Stderr)
    # exit(1)
    bb/copy-to-ebx 1/imm32
    e8/call syscall_exit/disp32
    # never gets here

# potential alternative

#? __bounds-check:  # msg: (addr array byte)
#?   (write-buffered Stderr "abort: array bounds exceeded in fn ")
#?   8b/-> *(esp+4) 0/r32/eax  # we're going to abort, so just clobber away
#?   (write-buffered Stderr %eax)
#?   (write-buffered Stderr Newline)
#?   # exit(1)
#?   bb/copy-to-ebx 1/imm32
#?   e8/call syscall_exit/disp32

# to be called as follows:
#   var/reg <- index arr/rega: (addr array T), idx/regi: int
#     | if size-of(T) is 1, 2, 4 or 8
#         => # temporarily save array size to reg to check bounds
#            "8b/-> *" rega " " reg "/r32"
#            "c1/shift 5/subop/right %" reg " " log2(size-of(T)) "/imm32"
#            "3b/compare " reg "/r32 *" rega
#            "68/push \"" function "\"/imm32"  # pass function name to error message
#            "0f 8d/jump-if->= __bounds_check/disp32"
#            "81 0/subop/add %esp 4/imm32"  # drop function name
#            # actually save the index addr in reg
#            "8d/copy-address *(" rega "+" regi "<<" log2(size-of(T)) "+4) " reg "/r32"
TB_KEY_CARRIAGE_RETURN) { // expand lines under current by one level assert(contains_key(Trace_index, Cursor_row)); int start_index = get(Trace_index, Cursor_row); int index = 0; // simultaneously compute end_index and min_depth int min_depth = 9999; for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) { if (contains_key(Visible, index)) break; trace_line& curr_line = Trace_stream->past_lines.at(index); assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth); if (curr_line.depth < min_depth) min_depth = curr_line.depth; } int end_index = index; // mark as visible all intervening indices at min_depth for (index = start_index; index < end_index; ++index) { trace_line& curr_line = Trace_stream->past_lines.at(index); if (curr_line.depth == min_depth) { Visible.insert(index); } } refresh_screen_rows(); } else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) { // collapse all lines under current assert(contains_key(Trace_index, Cursor_row)); int start_index = get(Trace_index, Cursor_row); int index = 0; // end_index is the next line at a depth same as or lower than start_index int initial_depth = Trace_stream->past_lines.at(start_index).depth; for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) { if (!contains_key(Visible, index)) continue; trace_line& curr_line = Trace_stream->past_lines.at(index); if (curr_line.depth <= initial_depth) break; } int end_index = index; // mark as visible all intervening indices at min_depth for (index = start_index+1; index < end_index; ++index) { Visible.erase(index); } refresh_screen_rows(); } else if (key == '/') { if (start_search_editor(FORWARD)) search(Current_search_pattern, Current_search_direction); } else if (key == '?') { if (start_search_editor(BACKWARD)) search(Current_search_pattern, Current_search_direction); } else if (key == 'n') { if (!Current_search_pattern.empty()) search(Current_search_pattern, Current_search_direction); } else if (key == 'N') { if (!Current_search_pattern.empty()) search(Current_search_pattern, opposite(Current_search_direction)); } } tb_clear(); tb_shutdown(); return 0; }