// 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 trace_line& line = Trace_stream->past_lines.at(trace_index); if (line.label.find(pat) == string::npos && line.contents.find(pat) == string::npos) continue; Top_of_screen = trace_index; Cursor_row = 0; refresh_screen_rows(); return; } } void search_previous(const string& pat) { for (int trace_index = get(Trace_index, Cursor_row)-1; trace_index >= 0; --trace_index) { if (!contains_key(Visible, trace_index)) continue; const trace_line& line = Trace_stream->past_lines.at(trace_index); if (line.label.find(pat) == string::npos && line.contents.find(pat) == string::npos) continue; Top_of_screen = trace_index; Cursor_row = 0; refresh_screen_rows(); return; } } void search(const string& pat, search_direction dir) { if (dir == FORWARD) search_next(pat); else search_previous(pat); } search_direction opposite(search_direction dir) { if (dir == FORWARD) return BACKWARD; else return FORWARD; } bool start_search_editor(search_direction dir) { const int bottom_screen_line = tb_height()-1; // run a little editor just in the last line of the screen clear_line(bottom_screen_line); int col = 0; // screen column of cursor on bottom line. also used to update pattern. tb_set_cursor(col, bottom_screen_line); tb_print('/', TB_WHITE, TB_BLACK); ++col; string pattern; while (true) { int key = read_key(); if (key == TB_KEY_ENTER) { if (!pattern.empty()) { Current_search_pattern = pattern; Current_search_direction = dir; } return true; } else if (key == TB_KEY_ESC || key == TB_KEY_CTRL_C) { return false; } else if (key == TB_KEY_ARROW_LEFT) { if (col > /*slash*/1) { --col; tb_set_cursor(col, bottom_screen_line); } } else if (key == TB_KEY_ARROW_RIGHT) { if (col-/*slash*/1 < SIZE(pattern)) { ++col; tb_set_cursor(col, bottom_screen_line); } } else if (key == TB_KEY_HOME || key == TB_KEY_CTRL_A) { col = /*skip slash*/1; tb_set_cursor(col, bottom_screen_line); } else if (key == TB_KEY_END || key == TB_KEY_CTRL_E) { col = SIZE(pattern)+/*skip slash*/1; tb_set_cursor(col, bottom_screen_line); } else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) { if (col > /*slash*/1) { assert(col <= SIZE(pattern)+1); --col; // update pattern pattern.erase(col-/*slash*/1, /*len*/1); // update screen tb_set_cursor(col, bottom_screen_line); for (int x = col; x < SIZE(pattern)+/*skip slash*/1; ++x) tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK); tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(col, bottom_screen_line); } } else if (key == TB_KEY_CTRL_K) { int old_pattern_size = SIZE(pattern); pattern.erase(col-/*slash*/1, SIZE(pattern) - (col-/*slash*/1)); tb_set_cursor(col, bottom_screen_line); for (int x = col; x < old_pattern_size+/*slash*/1; ++x) tb_print(' ', TB_WHITE, TB_BLACK); tb_set_cursor(col, bottom_screen_line); } else if (key == TB_KEY_CTRL_U) { int old_pattern_size = SIZE(pattern); pattern.erase(0, col-/*slash*/1); col = /*skip slash*/1; tb_set_curso
//: Allow instructions to mention literals directly.
//:
//: This layer will transparently move them to the global segment (assumed to
//: always be the second segment).

:(scenario transform_literal_string)
== code
b8/copy  "test"/imm32
== data  # need to manually create this for now
+transform: -- move literal strings to data segment
+transform: adding global variable '__subx_global_1' containing "test"
+transform: instruction after transform: 'b8 __subx_global_1'

//: We don't rely on any transforms running in previous layers, but this layer
//: knows about labels and global variables and will emit them for previous
//: layers to transform.
:(after "Begin Transforms")
//