about summary refs log tree commit diff stats
path: root/107trace.subx
blob: cc6a7b4345d2a505cbbc08c7ff6c50212cfad9dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pre { 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 */
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - static-dispatch.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.LineNr { }
.Constant { color: #008787; }
.Special { color: #ff6060; }
.Comment { color: #005faf; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/static-dispatch.mu'>https://github.com/akkartik/mu/blob/master/static-dispatch.mu</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment"># Example program showing how multiple functions with the same name can</span>
<span id="L2" class="LineNr"> 2 </span><span class="Comment"># coexist, and how we select between them.</span>
<span id="L3" class="LineNr"> 3 </span><span class="Comment">#</span>
<span id="L4" class="LineNr"> 4 </span><span class="Comment"># Expected output:</span>
<span id="L5" class="LineNr"> 5 </span><span class="Comment">#   4</span>
<span id="L6" class="LineNr"> 6 </span><span class="Comment">#   7</span>
<span id="L7" class="LineNr"> 7 </span><span class="Comment">#   7</span>
<span id="L8" class="LineNr"> 8 </span>
<span id="L9" class="LineNr"> 9 </span><span class="muRecipe">def</span> test a:num<span class="muRecipe"> -&gt; </span>b:num [
<span id="L10" class="LineNr">10 </span>  <span class="Constant">local-scope</span>
<span id="L11" class="LineNr">11 </span>  <span class="Constant">load-inputs</span>
<span id="L12" class="LineNr">12 </span>  b <span class="Special">&lt;-</span> add a,<span class="Constant"> 1</span>
<span id="L13" class="LineNr">13 </span>]
<span id="L14" class="LineNr">14 </span>
<span id="L15" class="LineNr">15 </span><span class="muRecipe">def</span> test a:num, b:num<span class="muRecipe"> -&gt; </span>c:num [
<span id="L16" class="LineNr">16 </span>  <span class="Constant">local-scope</span>
<span id="L17" class="LineNr">17 </span>  <span class="Constant">load-inputs</span>
<span id="L18" class="LineNr">18 </span>  c <span class="Special">&lt;-</span> add a, b
<span id="L19" class="LineNr">19 </span>]
<span id="L20" class="LineNr">20 </span>
<span id="L21" class="LineNr">21 </span><span class="muRecipe">def</span> <a href='static-dispatch.mu.html#L21'>main</a> [
<span id="L22" class="LineNr">22 </span>  <span class="Constant">local-scope</span>
<span id="L23" class="LineNr">23 </span>  a:num <span class="Special">&lt;-</span
# primitives for emitting traces to a 'trace' stream, and for tests to make assertions on its contents
#
# A trace stream looks like a regular stream:
#   write: int  # index at which writes go
#   read: int  # index that we've read until
#   data: (array byte)  # prefixed by size as usual
# Usually the trace stream will be in a separate segment set aside for the purpose.
#
# primitives for operating on traces (arguments in quotes):
#   - initialize-trace-stream: populates Trace-stream with a new segment of the given 'size'
#   - trace: adds a 'line' to Trace-stream
#   - check-trace-contains: scans from Trace-stream's start for a matching 'line', prints a 'message' to stderr on failure
#   - check-trace-scans-to: scans from Trace-stream's read pointer for a matching 'line', prints a 'message' to stderr on failure

== data

# Handles are addresses created on the heap.
# In safe Mu they'll be fat pointers. But in SubX they're just addresses, since
# SubX programs never reclaim memory.

Trace-stream:  # (handle stream byte)
    0/imm32
    # we don't have safe handles (fat pointers) yet

Trace-segment:
    0/imm32/curr
    0/imm32/limit

# Fake trace-stream for tests.
# Also illustrates the layout of the real trace-stream (segment).
_test-trace-stream:  # (stream byte)
    # current write index
    0/imm32
    # current read index
    0/imm32
    # size
    8/imm32
    # data
    00 00 00 00 00 00 00 00  # 8 bytes

== 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

# Allocate a new segment for the trace stream, initialize its size, and save its address to Trace-stream.
# The Trace-stream segment will consist of variable-length lines separated by newlines (0x0a)
initialize-trace-stream:  # n: int
    # . prologue
    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
    # ecx = n
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # Trace-segment = new-segment(n)
    # . . push args
    68/push  Trace-segment/imm32
    51/push-ecx
    # . . call
    e8/call  new-segment/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # copy Trace-segment->curr to *Trace-stream
    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-segment/disp32              # copy *Trace-segment to eax
#?     # watch point to catch Trace-stream leaks
#? $watch-1:
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # Trace-stream->size = n - 12
    # . ecx -= 12
    81          5/subop/subtract    3/mod/direct    1/rm32/ecx    .           .             .           .           .               0xc/imm32         # subtract from ecx
    # . Trace-stream->size = ecx
    89/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   8/disp8         .                 # copy ecx to *(eax+8)
$initialize-trace-stream:end:
    # . restore registers
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# Append a string to the given trace stream.
# Silently give up if it's already full. Or truncate the string if there isn't enough room.
trace:  # line: (addr array byte)
    # . prologue
    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
    56/push-esi
    57/push-edi
    # var edi: (addr stream byte) = *Trace-stream
    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           7/r32/edi   Trace-stream/disp32               # copy *Trace-stream to edi
    # esi = line
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
    # var ecx: int = t->write
    8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
    # var edx: int = t->size
    8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(edi+8) to edx
    # eax = _append-3(&t->data[t->write], &t->data[t->size], line)
    # . . push line
    56/push-esi
    # . . push &t->data[t->size]
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  2/index/edx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+edx+12 to ebx
    53/push-ebx
    # . . push &t->data[t->write]
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  1/index/ecx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+ecx+12 to ebx
    53/push-ebx
    # . . call
    e8/call  _append-3/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # if (eax == 0) return
    3d/compare-eax-and  0/imm32
    74/jump-if-=  $trace:end/disp8
    # t->write += eax
    01/add                          0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # add eax to *edi
    # refresh ecx = t->write
    8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
    # eax = _append-3(&t->data[t->write], &t->data[t->size], line)
    # . . push line
    68/push  Newline/imm32
    # . . push &t->data[t->size]
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  2/index/edx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+edx+12 to ebx
    53/push-ebx
    # . . push &t->data[t->write]
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  1/index/ecx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+ecx+12 to ebx
    53/push-ebx
    # . . call
    e8/call  _append-3/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # t->write += eax
    01/add                          0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # add eax to *edi
$trace:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-trace-single:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # trace("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(*_test-trace-stream->data, 41/A 62/b 0a/newline 00, msg)
    # . . push args
    68/push  "F - test-trace-single"/imm32
    68/push  0x0a6241/imm32/Ab-newline
    # . . push *_test-trace-stream->data
    b8/copy-to-eax  _test-trace-stream/imm32
    ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # end
    c3/return

test-trace-appends:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # trace("C")
    # . . push args
    68/push  "C"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # trace("D")
    # . . push args
    68/push  "D"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(*_test-trace-stream->data, 43/C 0a/newline 44/D 0a/newline, msg)
    # . . push args
    68/push  "F - test-trace-appends"/imm32
    68/push  0x0a440a43/imm32/C-newline-D-newline
    # . . push *_test-trace-stream->data
    b8/copy-to-eax  _test-trace-stream/imm32
    ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # end
    c3/return

test-trace-empty-line:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # trace("")
    # . . push args
    68/push  ""/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(*_test-trace-stream->data, 0, msg)
    # . . push args
    68/push  "F - test-trace-empty-line"/imm32
    68/push  0/imm32
    # . . push *_test-trace-stream->data
    b8/copy-to-eax  _test-trace-stream/imm32
    ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # end
    c3/return

check-trace-contains:  # line: (addr string), msg: (addr string)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # rewind-stream(*Trace-stream)
    # . . push args
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # . . call
    e8/call  rewind-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-trace-scans-to(line, msg)
    # . . 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  check-trace-scans-to/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
$check-trace-contains:end:
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

check-trace-scans-to:  # line: (addr string), msg: (addr string)
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    50/push-eax
    # eax = trace-scan(line)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  trace-scan/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    68/push  1/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
$check-trace-scans-to:end:
    # . restore registers
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# Start scanning from Trace-stream->read for 'line'. If found, update Trace-stream->read and return true.
trace-scan:  # line: (addr array byte) -> result/eax: boolean
    # pseudocode:
    #   push Trace-stream->read
    #   while true:
    #     if Trace-stream->read >= Trace-stream->write
    #       break
    #     if next-line-matches?(Trace-stream, line)
    #       skip-next-line(Trace-stream)
    #       dump saved copy of Trace-stream->read
    #       return true
    #     skip-next-line(Trace-stream)
    #   pop saved copy of Trace-stream->read
    #   return false
    #
    # . prologue
    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 = *Trace-stream
    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           6/r32/esi   Trace-stream/disp32               # copy *Trace-stream to esi
    # ecx = Trace-stream->write
    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx                   .                 # copy *esi to ecx
    # push Trace-stream->read
    ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
$trace-scan:loop:
    # if (Trace-stream->read >= Trace-stream->write) return false
    39/compare                      1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # compare ecx with *(esi+4)
    7d/jump-if->=  $trace-scan:false/disp8
    # eax = next-line-matches?(Trace-stream, line)
    # . . push args
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    56/push-esi
    # . . call
    e8/call  next-line-matches?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # if (eax == false) continue
    3d/compare-eax-and  0/imm32/false
    74/jump-if-=  $trace-scan:continue/disp8
$trace-scan:true:
    # skip-next-line(Trace-stream)
    # . . push args
    56/push-esi
    # . . call
    e8/call  skip-next-line/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # dump saved copy of Trace-stream->read
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # return true
    b8/copy-to-eax  1/imm32/true
    eb/jump  $trace-scan:end/disp8
$trace-scan:continue:
    # skip-next-line(Trace-stream)
    # . . push args
    56/push-esi
    # . . call
    e8/call  skip-next-line/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    eb/jump  $trace-scan:loop/disp8
$trace-scan:false:
    # restore saved copy of Trace-stream->read
    8f          0/subop/pop         1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # pop to *(esi+4)
    # return false
    b8/copy-to-eax  0/imm32/false
$trace-scan:end:
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-trace-scan-first:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # setup
    # . *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # . clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # . trace("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # eax = trace-scan("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace-scan/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-trace-scan-first"/imm32
    68/push  1/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
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # . end
    c3/return

test-trace-scan-skips-lines-until-found:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # setup
    # . *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # . clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # . trace("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . trace("cd")
    # . . push args
    68/push  "cd"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # eax = trace-scan("cd")
    # . . push args
    68/push  "cd"/imm32
    # . . call
    e8/call  trace-scan/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-trace-scan-skips-lines-until-found"/imm32
    68/push  1/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
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # . end
    c3/return

test-trace-second-scan-starts-where-first-left-off:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # setup
    # . *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # . clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # . trace("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . eax = trace-scan("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace-scan/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # second scan fails
    # . eax = trace-scan("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace-scan/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-trace-second-scan-starts-where-first-left-off"/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
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # . end
    c3/return

test-trace-scan-failure-leaves-read-index-untouched:
    # push *Trace-stream
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # setup
    # . *Trace-stream = _test-trace-stream
    b8/copy-to-eax  _test-trace-stream/imm32
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Trace-stream/disp32               # copy eax to *Trace-stream
    # . clear-trace-stream()
    e8/call  clear-trace-stream/disp32
    # . trace("Ab")
    # . . push args
    68/push  "Ab"/imm32
    # . . call
    e8/call  trace/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . check-ints-equal(_test-trace-stream->read, 0, msg)
    # . . push args
    68/push  "F - test-trace-second-scan-starts-where-first-left-off/precondition-failure"/imm32
    68/push  0/imm32
    b8/copy-to-eax  _test-trace-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
    # perform a failing scan
    # . eax = trace-scan("Ax")
    # . . push args
    68/push  "Ax"/imm32
    # . . call
    e8/call  trace-scan/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # no change in read index
    # . check-ints-equal(_test-trace-stream->read, 0, msg)
    # . . push args
    68/push  "F - test-trace-second-scan-starts-where-first-left-off"/imm32
    68/push  0/imm32
    b8/copy-to-eax  _test-trace-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
    # pop into *Trace-stream
    8f          0/subop/pop         0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # pop into *Trace-stream
    # . end
    c3/return

next-line-matches?:  # t: (addr stream byte), line: (addr array byte) -> result/eax: boolean
    # pseudocode:
    #   while true:
    #     if (currl >= maxl) break
    #     if (currt >= maxt) return false
    #     if (*currt != *currl) return false
    #     ++currt
    #     ++currl
    #   return *currt == '\n'
    #
    # . prologue
    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
    # edx = line
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
    # var currl/esi: (addr byte) = line->data
    # . esi = line/edx->data
    8d/copy-address                 1/mod/*+disp8   2/rm32/edx    .           .             .           6/r32/esi   4/disp8         .                 # copy edx+4 to esi
    # var maxl/ecx: (addr byte) = &line->data[line->size]
    # . eax = line/edx->size
    8b/copy                         0/mod/indirect  2/rm32/edx    .           .                         0/r32/eax   .               .                 # copy *edx to eax
    # . maxl = &line->data[line->size]
    8d/copy-address                 0/mod/indirect  4/rm32/sib    6/base/esi  0/index/eax   .           1/r32/ecx   .               .                 # copy edx+eax to ecx
    # edi = t
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
    # var ebx: (addr byte) = t->data
    8d/copy-address                 1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   0xc/disp8       .                 # copy edi+12 to ebx
    # var maxt/edx: (addr byte) = &t->data[t->write]
    # . eax = t->write
    8b/copy                         0/mod/indirect  7/rm32/edi    .           .                         0/r32/eax   .               .                 # copy *edi to eax
    # . maxt = &t->data[t->write]
    8d/copy-address                 0/mod/indirect  4/rm32/sib    3/base/ebx  0/index/eax   .           2/r32/edx   .               .                 # copy ebx+eax to edx
    # var currt/edi: (addr byte) = &t->data[t->read]
    # . eax = t/edi->read
    8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .                         0/r32/eax   4/disp8         .                 # copy *(edi+4) to eax
    # . currt = &t->data[t->read]
    8d/copy-address                 0/mod/indirect  4/rm32/sib    3/base/ebx  0/index/eax   .           7/r32/edi   .               .                 # copy ebx+eax to edi
$next-line-matches?:loop:
    # if (currl >= maxl) break
    39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi and ecx
    73/jump-if-addr>=  $next-line-matches?:break/disp8
    # if (currt >= maxt) return false
    # . eax = false
    b8/copy-to-eax  0/imm32/false
    39/compare                      3/mod/direct    7/rm32/edi    .           .             .           2/r32/edx   .               .                 # compare edi and edx
    73/jump-if-addr>=  $next-line-matches?:end/disp8
    # if (*currt != *currl) return false
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    31/xor                          3/mod/direct    3/rm32/eax    .           .             .           3/r32/eax   .               .                 # clear ebx
    # . eax: byte = *currt
    8a/copy-byte                    0/mod/indirect  7/rm32/edi    .           .                         0/r32/eax   .               .                 # copy *edi to eax
    # . ebx: byte = *currl
    8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .                         3/r32/ebx   .               .                 # copy *esi to ebx
    # . eax >= ebx
    39/compare                      3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # compare eax and ebx
    # . eax = false
    b8/copy-to-eax  0/imm32/false
    75/jump-if-!=  $next-line-matches?:end/disp8
    # ++currt
    47/increment-edi
    # ++currl
    46/increment-esi
    eb/jump  $next-line-matches?:loop/disp8
$next-line-matches?:break:
    # return *currt == '\n'
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    # . eax: byte = *currt
    8a/copy-byte                    0/mod/indirect  7/rm32/edi    .           .                         0/r32/eax   .               .                 # copy *edi to eax
    3d/compare-eax-and  0xa/imm32/newline
    # . eax = false
    b8/copy-to-eax  1/imm32/true
    74/jump-if-=  $next-line-matches?:end/disp8
    b8/copy-to-eax  0/imm32/true
$next-line-matches?:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-next-line-matches?-no-match-1:
    # next line of "ABABA" does not match "blah blah"
    # . eax = next-line-matches?(_test-stream-line-ABABA, "blah blah")
    # . . push args
    68/push  "blah blah"/imm32
    68/push  _test-stream-line-ABABA/imm32
    # . . call
    e8/call  next-line-matches?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-next-line-matches?-no-match-1"/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
    c3/return

test-next-line-matches?-no-match-2:
    # next line of "ABABA" does not match ""
    # . eax = next-line-matches?(_test-stream-line-ABABA, "")
    # . . push args
    68/push  ""/imm32
    68/push  _test-stream-line-ABABA/imm32
    # . . call
    e8/call  next-line-matches?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-next-line-matches?-no-match-2"/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
    c3/return

test-next-line-matches?-no-match-3:
    # next line of "ABABA" does not match  "AA"
    # . eax = next-line-matches?(_test-stream-line-ABABA, "AA")
    # . . push args
    68/push  "AA"/imm32
    68/push  _test-stream-line-ABABA/imm32
    # . . call
    e8/call  next-line-matches?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . check-ints-equal(eax, 0, msg)
    # . . push args
    68/push  "F - test-next-line-matches?-no-match-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
    c3/return

test-next-line-matches?-match:
    # next line of "ABABA" matches "ABABA"
    # . eax = next-line-matches?(_test-stream-line-ABABA, "ABABA")
    # . . push args
    68/push  "ABABA"/imm32
    68/push  _test-stream-line-ABABA/imm32
    # . . call
    e8/call  next-line-matches?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-next-line-matches?-match"/imm32
    68/push  1/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
    c3/return

# move t->read to _after_ next newline
skip-next-line:  # t: (addr stream byte)
    # pseudocode:
    #   max = &t->data[t->write]
    #   i = t->read
    #   curr = &t->data[t->read]
    #   while true
    #     if (curr >= max) break
    #     ++i
    #     if (*curr == '\n') break
    #     ++curr
    #   t->read = i
    #
    # . prologue
    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
    # ecx = t
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # edx = t->data
    8d/copy-address                 1/mod/*+disp8   1/rm32/ecx    .           .             .           2/r32/edx   0xc/disp8       .                 # copy ecx+12 to edx
    # eax = t->write
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
    # var max/ebx: (addr byte) = &t->data[t->write]
    8d/copy-address                 0/mod/indirect  4/rm32/sib    2/base/edx  0/index/eax   .           3/r32/ebx   .               .                 # copy edx+eax to ebx
    # eax = t->read
    8b/copy                         1/mod/*+disp8   1/rm32/ecx    .           .             .           0/r32/eax   4/disp8         .                 # copy *(ecx+4) to edx
    # var curr/ecx: (addr byte) = &t->data[t->read]
    8d/copy-address                 0/mod/indirect  4/rm32/sib    2/base/edx  0/index/eax   .           1/r32/ecx   .               .                 # copy edx+eax to ecx
    # var i/edx: int = t->read
    89/copy                         3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to edx
$skip-next-line:loop:
    # if (curr >= max) break
    39/compare                      3/mod/direct    1/rm32/ecx    .           .             .           3/r32/ebx   .               .                 # compare ecx and ebx
    73/jump-if-addr>=  $skip-next-line:end/disp8
    # ++i
    42/increment-edx
    # if (*curr == '\n') break
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    8a/copy-byte                    0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/eax   .               .                 # copy *ecx to eax
    3d/compare-eax-and  0a/imm32/newline
    74/jump-if-=  $skip-next-line:end/disp8
    # ++curr
    41/increment-ecx
    # loop
    eb/jump  $skip-next-line:loop/disp8
$skip-next-line:end:
    # ecx = t
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # t->read = i
    89/copy                         1/mod/*+disp8   1/rm32/ecx    .           .             .           2/r32/edx   4/disp8         .                 # copy edx to *(ecx+4)
    # . restore registers
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    58/pop-to-eax
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-skip-next-line-empty:
    # skipping next line in empty stream leaves read pointer at 0
    # . skip-next-line(_test-stream-empty)
    # . . push args
    68/push  _test-stream-empty/imm32
    # . . call
    e8/call  skip-next-line/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . check-ints-equal(_test-stream-empty->read, 0, msg)
    # . . push args
    68/push  "F - test-skip-next-line-empty"/imm32
    68/push  0/imm32
    b8/copy-to-eax  _test-stream-empty/imm32
    8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to 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
    c3/return

test-skip-next-line-filled:
    # skipping next line increments read pointer by length of line + 1 (for newline)
    # . skip-next-line(_test-stream-filled)
    # . . push args
    68/push  _test-stream-filled/imm32
    # . . call
    e8/call  skip-next-line/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . check-ints-equal(_test-stream-filled->read, 5, msg)
    # . . push args
    68/push  "F - test-skip-next-line-filled"/imm32
    68/push  5/imm32
    b8/copy-to-eax  _test-stream-filled/imm32
    8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to 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
    c3/return

clear-trace-stream:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # clear-stream(*Trace-stream)
    # . . push args
    ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
$clear-trace-stream:end:
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# - helpers

# 3-argument variant of _append
_append-3:  # out: (addr byte), outend: (addr byte), s: (addr array byte) -> num_bytes_appended/eax
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    # eax = _append-4(out, outend, &s->data[0], &s->data[s->size])
    # . . push &s->data[s->size]
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
    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
    51/push-ecx
    # . . push &s->data[0]
    8d/copy-address                 1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   4/disp8         .                 # copy eax+4 to ecx
    51/push-ecx
    # . . push outend
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . push out
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  _append-4/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
$_append-3:end:
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# 4-argument variant of _append
_append-4:  # out: (addr byte), outend: (addr byte), in: (addr byte), inend: (addr byte) -> num_bytes_appended/eax: int
    # . prologue
    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
    # num_bytes_appended = 0
    b8/copy-to-eax  0/imm32
    # edi = out
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
    # edx = outend
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
    # esi = in
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0x10/disp8      .                 # copy *(ebp+16) to esi
    # ecx = inend
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x14/disp8      .                 # copy *(ebp+20) to ecx
$_append-4:loop:
    # if (in >= inend) break
    39/compare                      3/mod/direct    6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare esi with ecx
    73/jump-if-addr>=  $_append-4:end/disp8
    # if (out >= outend) abort  # just to catch test failures fast
    39/compare                      3/mod/direct    7/rm32/edi    .           .             .           2/r32/edx   .               .                 # compare edi with edx
    73/jump-if-addr>=  $_append-4:abort/disp8
    # *out = *in
    8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           3/r32/BL    .               .                 # copy byte at *esi to BL
    88/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           3/r32/BL    .               .                 # copy byte at BL to *edi
    # ++num_bytes_appended
    40/increment-eax
    # ++in
    46/increment-esi
    # ++out
    47/increment-edi
    eb/jump  $_append-4:loop/disp8
$_append-4:end:
    # . restore registers
    5f/pop-to-edi
    5e/pop-to-esi
    5b/pop-to-ebx
    5a/pop-to-edx
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

$_append-4:abort:
    # . _write(2/stderr, error)
    # . . push args
    68/push  "stream overflow\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
    e8/call  syscall_exit/disp32
    # never gets here

== data

_test-stream-line-ABABA:  # (stream byte)
    # write
    8/imm32
    # read
    0/imm32
    # size
    8/imm32
    # data
    41 42 41 42 41 0a 00 00  # 8 bytes

_test-stream-empty:  # (stream byte)
    # write
    0/imm32
    # read
    0/imm32
    # size
    8/imm32
    # data
    00 00 00 00 00 00 00 00  # 8 bytes

_test-stream-filled:  # (stream byte)
    # write
    8/imm32
    # read
    0/imm32
    # size
    8/imm32
    # data
    41 41 41 41 0a 41 41 41  # 8 bytes

# . . vim:nowrap:textwidth=0