1 # Helpers for parsing SubX words, with their rules for hex, labels and metadata. 2 3 == code 4 # instruction effective address register displacement immediate 5 # . op subop mod rm32 base index scale r32 6 # . 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 7 8 has-metadata?: # word: (addr slice), s: (addr string) -> result/eax: boolean 9 # pseudocode: 10 # var twig: &slice = next-token-from-slice(word->start, word->end, '/') # skip name 11 # curr = twig->end 12 # while true 13 # twig = next-token-from-slice(curr, word->end, '/') 14 # if (twig.empty()) break 15 # if (slice-equal?(twig, s)) return true 16 # curr = twig->end 17 # return false 18 # . prologue 19 55/push-ebp 20 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp 21 # . save registers 22 51/push-ecx 23 52/push-edx 24 56/push-esi 25 57/push-edi 26 # esi = word 27 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi 28 # var edx: (addr byte) = word->end 29 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 2/r32/edx 4/disp8 . # copy *(esi+4) to edx 30 # var twig/edi: slice 31 68/push 0/imm32/end 32 68/push 0/imm32/start 33 89/copy 3/mod/direct 7/rm32/edi . . . 4/r32/esp . . # copy esp to edi 34 # next-token-from-slice(word->start, word->end, '/', twig) 35 # . . push args 36 57/push-edi 37 68/push 0x2f/imm32/slash 38 52/push-edx 39 ff 6/subop/push 0/mod/indirect 6/rm32/esi . . . . . . # push *esi 40 # . . call 41 e8/call next-token-from-slice/disp32 42 # . . discard args 43 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp 44 $has-metadata?:loop: 45 # next-token-from-slice(curr, word->end, '/', twig) 46 # . . push args 47 57/push-edi 48 68/push 0x2f/imm32/slash 49 52/push-edx 50 ff 6/subop/push 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 . # push *(edi+4) 51 # . . call 52 e8/call next-token-from-slice/disp32 53 # . . discard args 54 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp 55 # if (slice-empty?(twig)) return false 56 # . eax = slice-empty?(twig) 57 # . . push args 58 57/push-edi 59 # . . call 60 e8/call slice-empty?/disp32 61 # . . discard args 62 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp 63 # . if (eax != false) return false 64 3d/compare-eax-and 0/imm32/false 65 75/jump-if-!= $has-metadata?:false/disp8 66 # if (slice-equal?(twig, s)) return true 67 # . eax = slice-equal?(twig, s) 68 # . . push args 69 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) 70 57/push-edi 71 # . . call 72 e8/call slice-equal?/disp32 73 # . . discard args 74 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp 75 # . if (eax != false) return true 76 3d/compare-eax-and 0/imm32/false 77 # eax already contains truepre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */### A debugging helper that lets you zoom in/out on a trace. To try it out, first create an example trace (from the top-level `mu/` directory): ```shell ./subx --trace run apps/factorial ``` This command will save a trace of its execution in a file called `last_run`. The trace consists of a series of lines, each starting with an integer depth and a single-word 'label', followed by a colon and whitespace. Now browse this trace: ```shell tools/browse_trace last_run ``` You should now find yourself in a UI showing a subsequence of lines from the trace, each line starting with a numeric depth, and ending with a parenthetical count of trace lines hidden after it with greater depths. For example, this line: ``` 2 app: line1 (30) ``` indicates that it was logged with depth 2, and that 30 following lines have been hidden at a depth greater than 2. (As an experiment, hidden counts of 1000 or more are highlighted in red.) The UI provides the following hotkeys: * `q` or `ctrl-c`: Quit. * `Enter`: 'Zoom into' this line. Expand lines hidden after it that were at the next higher level. * `Backspace`: 'Zoom out' on a line after zooming in, collapsing lines below expanded by some series of `Enter` commands. * `j` or `down-arrow`: Move cursor down one line. * `k` or `up-arrow`: Move cursor up one line. * `J` or `ctrl-f` or `page-down`: Scroll cursor down one page. * `K` or `ctrl-b` or `page-up`: Scroll cursor up one page. * `h` or `left-arrow`: Scroll cursor left one character. * `l` or `right-arrow`: Scroll cursor right one character. * `H`: Scroll cursor left one screen-width. * `L`: Scroll cursor right one screen-width. * `g` or `home`: Move cursor to start of trace. * `G` or `end`: Move cursor to end of trace. * `t`: Move cursor to top line on screen. * `c`: Move cursor to center line on screen. * `b`: Move cursor to bottom line on screen. * `T`: Scroll line at cursor to top of screen. * `/`: Search forward for a pattern. * `?`: Search backward for a pattern. * `n`: Repeat the previous `/` or `?`. * `N`: Repeat the previous `/` or `?` in the opposite direction. After hitting `/`, the mini-editor on the bottom-most line supports the following hotkeys: * ascii characters: add the key to the pattern. * `Enter`: search for the pattern. * `Esc` or `ctrl-c`: cancel the current search, setting the screen back to its state before the search. * `left-arrow`: move cursor left. * `right-arrow`: move cursor right. * `ctrl-a` or `home`: move cursor to start of search pattern. * `ctrl-e` or `end`: move cursor to end of search pattern. * `ctrl-u`: clear search pattern before cursor * `ctrl-k`: clear search pattern at and after cursor ## wish list * Simple regular expression search: `.` and `*`. * Expand into lower depths as necessary when searching. * Zoom out everything. * Zoom out lines around the cursor to the highest (or specified) depth. Maybe a number followed by `]`?