about summary refs log tree commit diff stats
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/survey.subx316
1 files changed, 51 insertions, 265 deletions
diff --git a/apps/survey.subx b/apps/survey.subx
index d09f1f1d..f2825525 100644
--- a/apps/survey.subx
+++ b/apps/survey.subx
@@ -157,56 +157,6 @@ subx-survey:  # infile: (addr buffered-file), out: (addr buffered-file)
     # . write
     68/push  0/imm32/write
     89/copy                         3/mod/direct    6/rm32/esi    .           .             .           4/r32/esp   .               .                 # copy esp to esi
-#?     # dump labels->write {{{
-#?     # . write(2/stderr, "labels->write right after initialization: ")
-#?     # . . push args
-#?     68/push  "labels->write right after initialization: "/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . clear-stream($Stderr->buffer)
-#?     # . . push args
-#?     68/push  $Stderr->buffer/imm32
-#?     # . . call
-#?     e8/call  clear-stream/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # . print-int32-buffered(Stderr, labels->write)
-#?     # . . push args
-#? $watch-1:
-#?     ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
-#?     68/push  Stderr/imm32
-#?     # . . call
-#?     e8/call  print-int32-buffered/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . flush(Stderr)
-#?     # . . push args
-#?     68/push  Stderr/imm32
-#?     # . . call
-#?     e8/call  flush/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # . write(2/stderr, "\n")
-#?     # . . push args
-#?     68/push  Newline/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # }}}
-#?     # write(2/stderr, "slurp in\n") {{{
-#?     # . . push args
-#?     68/push  "slurp in\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
-#?     # }}}
     # slurp(infile, in)
     # . . push args
     56/push-esi
@@ -215,88 +165,6 @@ subx-survey:  # infile: (addr buffered-file), out: (addr buffered-file)
     e8/call  slurp/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # dump labels->write {{{
-#?     # . write(2/stderr, "labels->write after slurp: ")
-#?     # . . push args
-#?     68/push  "labels->write after slurp: "/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . clear-stream($Stderr->buffer)
-#?     # . . push args
-#?     68/push  $Stderr->buffer/imm32
-#?     # . . call
-#?     e8/call  clear-stream/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # . print-int32-buffered(Stderr, labels->write)
-#?     # . . push args
-#?     ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
-#?     68/push  Stderr/imm32
-#?     # . . call
-#?     e8/call  print-int32-buffered/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . flush(Stderr)
-#?     # . . push args
-#?     68/push  Stderr/imm32
-#?     # . . call
-#?     e8/call  flush/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # . write(2/stderr, "\n")
-#?     # . . push args
-#?     68/push  Newline/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
-#?     # }}}
-#?     # dump in {{{
-#?     # . write(2/stderr, "in: ")
-#?     # . . push args
-#?     68/push  "in: "/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # write-stream(2/stderr, in)
-#?     # . . push args
-#?     56/push-esi
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write-stream/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . write(2/stderr, "$\n")
-#?     # . . push args
-#?     68/push  "$\n"/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . rewind-stream(in)
-#?     # . . push args
-#?     56/push-esi
-#?     # . . call
-#?     e8/call  rewind-stream/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # }}}
-#?     # write(2/stderr, "compute-offsets\n") {{{
-#?     # . . push args
-#?     68/push  "compute-offsets\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
-#?     # }}}
     # compute-offsets(in, segments, labels)
     # . . push args
     52/push-edx
@@ -306,15 +174,6 @@ subx-survey:  # infile: (addr buffered-file), out: (addr buffered-file)
     e8/call  compute-offsets/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
-#?     # write(2/stderr, "compute-addresses\n") {{{
-#?     # . . push args
-#?     68/push  "compute-addresses\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
-#?     # }}}
     # compute-addresses(segments, labels)
     # . . push args
     52/push-edx
@@ -330,81 +189,6 @@ subx-survey:  # infile: (addr buffered-file), out: (addr buffered-file)
     e8/call  rewind-stream/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # write(2/stderr, "emit-output\n") {{{
-#?     # . . push args
-#?     68/push  "emit-output\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
-#?     # }}}
-#?     # dump *Trace-stream {{{
-#?     # . write(2/stderr, "^")
-#?     # . . push args
-#?     68/push  "^"/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . write-stream(2/stderr, *Trace-stream)
-#?     # . . push args
-#?     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Trace-stream/disp32               # push *Trace-stream
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write-stream/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . write(2/stderr, "$\n")
-#?     # . . push args
-#?     68/push  "$\n"/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # }}}
-#?     # dump labels->write {{{
-#?     # . write(2/stderr, "labels->write after rewinding input: ")
-#?     # . . push args
-#?     68/push  "labels->write after rewinding input: "/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . clear-stream($Stderr->buffer)
-#?     # . . push args
-#?     68/push  $Stderr->buffer/imm32
-#?     # . . call
-#?     e8/call  clear-stream/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # . print-int32-buffered(Stderr, labels)
-#?     # . . push args
-#?     ff          6/subop/push        0/mod/indirect  2/rm32/edx    .           .             .           .           .               .                 # push *edx
-#?     68/push  Stderr/imm32
-#?     # . . call
-#?     e8/call  print-int32-buffered/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # . flush(Stderr)
-#?     # . . push args
-#?     68/push  Stderr/imm32
-#?     # . . call
-#?     e8/call  flush/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
-#?     # . write(2/stderr, "\n")
-#?     # . . push args
-#?     68/push  Newline/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # }}}
     # emit-output(in, out, segments, labels)
     # . . push args
     52/push-edx
@@ -969,7 +753,10 @@ $compute-offsets:case-label:
     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
 $compute-offsets:save-label-offset:
     # linfo->segment-name = curr-segment-name
-    89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           6/r32/esi   .               .                 # copy esi to *edi
+    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
+    89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to *edi
+    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy *(esi+4) to eax
+    89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           0/r32/eax   4/disp8         .                 # copy eax to *(edi+4)
     # trace-slsss("label '" word-slice "' is in segment '" current-segment-name "'.")
     # . eax = lookup(curr-segment-name)
     # . . push args
@@ -994,7 +781,7 @@ $compute-offsets:save-label-offset:
     # . ebx = segment-offset
     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   compute-offsets:segment-offset/disp32  # copy *segment-offset to ebx
     # . linfo->segment-offset = ebx
-    89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   4/disp8         .                 # copy ebx to *(edi+4)
+    89/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           3/r32/ebx   8/disp8         .                 # copy ebx to *(edi+8)
     # trace-slsns("label '" word-slice "' is at segment offset " *segment-offset/eax ".")
     # . . push args
     68/push  "."/imm32
@@ -1500,32 +1287,32 @@ $compute-addresses:label-loop:
     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
     # . edx = eax
     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to edx
-    # dump seg-name {{{
-    # . write(2/stderr, "compute-addresses: seg-name: ")
-    # . . push args
-    68/push  "seg-name: "/imm32
-    68/push  2/imm32/stderr
-    # . . call
-    e8/call  write/disp32
-    # . . discard args
-    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-    # . write(2/stderr, seg-name)
-    # . . push args
-    52/push-edx
-    68/push  2/imm32/stderr
-    # . . call
-    e8/call  write/disp32
-    # . . discard args
-    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-    # . write(2/stderr, "$\n")
-    # . . push args
-    68/push  "$\n"/imm32
-    68/push  2/imm32/stderr
-    # . . call
-    e8/call  write/disp32
-    # . . discard args
-    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-    # }}}
+#?     # dump seg-name {{{
+#?     # . write(2/stderr, "compute-addresses: seg-name: ")
+#?     # . . push args
+#?     68/push  "seg-name: "/imm32
+#?     68/push  2/imm32/stderr
+#?     # . . call
+#?     e8/call  write/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+#?     # . write(2/stderr, seg-name)
+#?     # . . push args
+#?     52/push-edx
+#?     68/push  2/imm32/stderr
+#?     # . . call
+#?     e8/call  write/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+#?     # . write(2/stderr, "$\n")
+#?     # . . push args
+#?     68/push  "$\n"/imm32
+#?     68/push  2/imm32/stderr
+#?     # . . call
+#?     e8/call  write/disp32
+#?     # . . discard args
+#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+#?     # }}}
     # var label-seg/edx: (addr segment-info) = get(segments, seg-name, row-size=20, "segment table")
     # . eax = get(segments, seg-name, row-size=20)
     # . . push args
@@ -3261,12 +3048,12 @@ test-emit-segments-code-label-absolute:
 emit-headers:  # out: (addr buffered-file), segments: (addr stream {(handle array byte), segment-info}), labels: (addr stream {(handle array byte), label-info})
     # pseudocode:
     #   emit-elf-header(out, segments, labels)
-    #   curr-segment = segments->data
+    #   var curr-segment-row: (addr handle array byte) = segments->data
     #   max = &segments->data[segments->write]
     #   while true
     #     if (curr-segment >= max) break
-    #     emit-elf-program-header-entry(out, curr-segment)
-    #     curr-segment += 16                        # size of a row
+    #     emit-elf-program-header-entry(out, curr-segment-row)
+    #     curr-segment-row += 20                        # size of a row
     #
     # . prologue
     55/push-ebp
@@ -3274,15 +3061,6 @@ emit-headers:  # out: (addr buffered-file), segments: (addr stream {(handle arra
     # . save registers
     50/push-eax
     51/push-ecx
-#?     # write(2/stderr, "emit-elf-header\n") {{{
-#?     # . . push args
-#?     68/push  "emit-elf-header\n"/imm32
-#?     68/push  2/imm32/stderr
-#?     # . . call
-#?     e8/call  write/disp32
-#?     # . . discard args
-#?     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-#?     # }}}
     # emit-elf-header(out, segments, labels)
     # . . push args
     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
@@ -3384,8 +3162,8 @@ $emit-headers:loop:
     e8/call  emit-elf-program-header-entry/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
-    # curr-segment += 16                        # size of a row
-    81          0/subop/add         3/mod/direct    0/rm32/eax    .           .             .           .           .               0x10/imm32        # add to eax
+    # curr-segment += 20                        # size of a row
+    81          0/subop/add         3/mod/direct    0/rm32/eax    .           .             .           .           .               0x14/imm32        # add to eax
     e9/jump  $emit-headers:loop/disp32
 $emit-headers:end:
     # . restore registers
@@ -3464,6 +3242,8 @@ $emit-elf-header:end:
     5d/pop-to-ebp
     c3/return
 
+# segment-info: {address, file-offset, size}                                  (12 bytes)
+# segments: (addr stream {(handle array byte), segment-info})                 (20 bytes per row)
 emit-elf-program-header-entry:  # out: (addr buffered-file), curr-segment: (addr {(handle array byte), segment-info})
     # pseudocode:
     #   *$Elf_p_offset = curr-segment->file-offset
@@ -3488,27 +3268,33 @@ emit-elf-program-header-entry:  # out: (addr buffered-file), curr-segment: (addr
     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
     # *$Elf_p_offset = curr-segment->file-offset
     # . eax = curr-segment->file-offset
-    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(esi+8) to eax
+    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(esi+12) to eax
     # . *$Elf_p_offset = eax
     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_offset/disp32              # copy eax to *$Elf_p_offset
     # *$Elf_p_vaddr = curr-segment->address
     # . eax = curr-segment->address
-    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy *(esi+4) to eax
+    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(esi+8) to eax
     # . *$Elf_p_vaddr = eax
     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_vaddr/disp32               # copy eax to *$Elf_p_vaddr
     # *$Elf_p_paddr = curr-segment->address
     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_paddr/disp32               # copy eax to *$Elf_p_paddr
     # *$Elf_p_filesz = curr-segment->size
     # . eax = curr-segment->size
-    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(esi+12) to eax
+    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   0x10/disp8       .                # copy *(esi+16) to eax
     # . *$Elf_p_filesz = eax
     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_filesz/disp32              # copy eax to *$Elf_p_filesz
     # *$Elf_p_memsz = curr-segment->size
     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   $Elf_p_memsz/disp32               # copy eax to *$Elf_p_memsz
-    # if (!string-equal?(curr-segment->name, "code") goto next check
-    # . eax = curr-segment->name
-    8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
-    # . eax = string-equal?(curr-segment->name, "code")
+    # if (!string-equal?(name, "code") goto next check
+    # . var name/eax: (addr array byte) = lookup(curr-segment->name)
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
+    ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
+    # . . call
+    e8/call  lookup/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # . eax = string-equal?(name, "code")
     # . . push args
     68/push  "code"/imm32
     50/push-eax