# Translate literal strings within double quotes. # Replace them with references to new variables in the data segment. # # To run: # $ ./bootstrap translate init.linux 0*.subx apps/subx-params.subx apps/dquotes.subx -o apps/dquotes # $ cat x # == code # ab "cd ef"/imm32 # $ cat x |./bootstrap run apps/dquotes # == code # ab __string1/imm32 # == data # __string1: # 5/imm32 # 0x63/c 0x64/d 0x20/ 0x65/e 0x66/f == 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 Entry: # run tests if necessary, convert stdin if not # . prologue 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # initialize heap # . Heap = new-segment(Heap-size) # . . push args 68/push Heap/imm32 ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Heap-size/disp32 # push *Heap-size # . . call e8/call new-segment/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # - if argc > 1 and argv[1] == "test", then return run-tests() # if (argc <= 1) goto interactive 81 7/subop/compare 1/mod/*+disp8 5/rm32/ebp . . . . 0/disp8 1/imm32 # compare *ebp 7e/jump-if-<= $subx-dquotes-main:interactive/disp8 # if (!kernel-string-equal?(argv[1], "test")) goto interactive # . eax = kernel-string-equal?(argv[1], "test") # . . push args 68/push "test"/imm32 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) # . . call e8/call kernel-string-equal?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . if (eax == false) goto interactive 3d/compare-eax-and 0/imm32/false 74/jump-if-= $subx-dquotes-main:interactive/disp8 # run-tests() e8/call run-tests/disp32 # syscall(exit, *Num-test-failures) 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/ebx Num-test-failures/disp32 # copy *Num-test-failures to ebx eb/jump $subx-dquotes-main:end/disp8 $subx-dquotes-main:interactive: # - otherwise convert stdin # var ed/eax: exit-descriptor 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # subtract from esp 89/copy 3/mod/direct 0/rm32/eax . . . 4/r32/esp . . # copy esp to eax # configure ed to really exit() # . ed->target = 0 c7 0/subop/copy 0/mod/direct 0/rm32/eax . . . . . 0/imm32 # copy to *eax # subx-dquotes(Stdin, Stdout, Stderr, ed) # . . push args 50/push-eax/ed 68/push Stderr/imm32 68/push Stdout/imm32 68/push Stdin/imm32 # . . call e8/call subx-dquotes/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # syscall(exit, 0) bb/copy-to-ebx 0/imm32 $subx-dquotes-main:end: e8/call syscall_exit/disp32 # conceptual hierarchy within a line: # line = words separated by ' ', maybe followed by comment starting with '#' # word = datum until '/', then 0 or more metadata separated by '/' subx-dquotes: # in: (addr buffered-file), out: (addr buffered-file) # pseudocode: # var line: (stream byte 512) # var new-data-segment-handle: (handle stream byte) # new-stream(Heap, Segment-size, 1, new-data-segment-handle) # var new-data-segment: (addr stream byte) = lookup(new-data-segment-handle) # # write(new-data-segment, "== data\n") # # TODO: When it was originally written dquotes ran before assort, so # # it assumes lots of segment headers, and emits a new segment of its # # own. We've since had to reorder the phases (see the explanation # # for a.assort2 in translate_subx). We could clean up a.assort2 if we # # conditionally emit the previous line. But this would require # # teaching dquotes to parse segment headers, so maybe that's not # # best.. # # while true # clear-stream(line) # read-line-buffered(in, line) # if (line->write == 0) break # end of file # while true # var word-slice = next-word-or-string(line) # if slice-empty?(word-slice) # end of line # break # if slice-starts-with?(word-slice, "#") # comment # continue # if slice-starts-with?(word-slice, '"') # string literal <== what we're here for # process-string-literal(word-slice, out, new-data-segment) # else # write-slice-buffered(out, word-slice) # write(out, " ") # write(out, "\n\n") # write-stream-data(out, new-data-segment) # flush(out) # # . 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 line/ecx: (stream byte 512) 81 5/subop/subtract 3/mod/direct 4/rm32/esp . . . . . 0x200/imm32 # subtract from esp 68/push 0x200/imm32/length 68/push 0/imm32/read 68/push 0/imm32/write 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx # var word-slice/edx: slice 68/push 0/imm32/end 68/push 0/imm32/start 89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx # var new-data-segment-handle/edi: (handle stream byte) 68/push 0/imm32 68/push 0/imm32 89/copy 3/mod/direct 7/rm32/edi . . . 4/r32/esp . . # copy esp to edi # new-stream(Heap, Segment-size, 1, new-data-segment-handle) # . . push args 57/push-edi 68/push 1/imm32 ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Segment-size/disp32 # push *Segment-size 68/push Heap/imm32 # . . call e8/call new-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp # var new-data-segment/edi: (addr stream byte) = lookup(*new-data-segment-handle) # . eax = lookup(*new-data-segment-handle) # . . push args ff 6/subop/push 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 . # push *(edi+4) ff 6/subop/push 0/mod/indirect 7/rm32/edi . . . . . . # push *edi # . . call e8/call lookup/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # . new-data-segment = eax 89/copy 3/mod/direct 7/rm32/edi . . . 0/r32/eax . . # copy eax to edi # write(new-data-segment, "== data\n") # . . push args 68/push "== data\n"/imm32 57/push-edi # . . call e8/call write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp $subx-dquotes:line-loop: # clear-stream(line) # . . push args 51/push-ecx # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # read-line-buffered(in, line) # . . push args 51/push-ecx ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) # . . call e8/call read-line-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp $subx-dquotes:check0: # if (line->write == 0) break 81 7/subop/compare 0/mod/indirect 1/rm32/ecx . . . . . 0/imm32 # compare *ecx 0f 84/jump-if-= $subx-dquotes:break/disp32 $subx-dquotes:word-loop: # next-word-or-string(line, word-slice) # . . push args 52/push-edx 51/push-ecx # . . call e8/call next-word-or-string/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp $subx-dquotes:check1: # if (slice-empty?(word-slice)) break # . eax = slice-empty?(word-slice) # . . push args 52/push-edx # . . call e8/call slice-empty?/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . if (eax != false) break 3d/compare-eax-and 0/imm32/false 0f 85/jump-if-!= $subx-dquotes:next-line/disp32 $subx-dquotes:check-for-comment: # if (slice-starts-with?(word-slice, "#")) continue # . var start/esi: (addr byte) = word-slice->start 8b/copy 0/mod/indirect 2/rm32/edx . . . 6/r32/esi . . # copy *edx to esi # . var c/eax: byte = *start 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax 8a/copy-byte 0/mod/indirect 6/rm32/esi . . . 0/r32/AL . . # copy byte at *esi to AL # . if (c == '#') continue 3d/compare-eax-and 0x23/imm32/hash 74/jump-if-= $subx-dquotes:word-loop/disp8 $subx-dquotes:check-for-string-literal: # if (slice-starts-with?(word-slice, '"')) continue 3d/compare-eax-and 0x22/imm32/dquote 75/jump-if-!= $subx-dquotes:regular-word/disp8 $subx-dquotes:string-literal: # process-string-literal(word-slice, out, new-data-segment) # . . push args 57/push-edi ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) 52/push-edx # . . call e8/call process-string-literal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # continue eb/jump $subx-dquotes:next-word/disp8 $subx-dquotes:regular-word: # write-slice-buffered(out, word-slice) # . . push args 52/push-edx ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call e8/call write-slice-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # fall through $subx-dquotes:next-word: # write-buffered(out, " ") # . . push args 68/push Space/imm32 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) # . . call
# We can't really do much with null-terminated kernel strings, and we don't
# want to. Let's turn them into regular length-prefixed strings at the first
# opportunity.
== code
kernel-string-to-string: # ad: (addr allocation-descriptor), in: (addr kernel-string), out: (addr handle array byte)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
52/push-edx
53/push-ebx
56/push-esi
57/push-edi
# var len/ecx: int = length(in)
(kernel-string-length *(ebp+0xc))
89/<- %ecx 0/r32/eax
# result = allocate-array(ad, len)
(allocate-array *(ebp+8) %ecx *(ebp+0x10))
# var c/edx: byte = 0
ba/copy-to-edx 0/imm32
# var src/esi: (addr byte) = in
8b/-> *(ebp+0xc) 6/r32/esi
# var dest/edi: (addr byte) = result->data
8b/-> *(ebp+0x10) 7/r32/edi
(lookup *edi *(edi+4)) # => eax
8d/copy-address *(eax+4) 7/r32/edi
{
$kernel-string-to-string:loop:
# c = *src
8a/byte-> *esi 2/r32/dl
# if (c == 0) break
81 7/subop/compare %edx 0/imm32
74/jump-if-= break/disp8
# *dest = c
88/byte<- *edi 2/r32/dl
# ++src
46/increment-esi
# ++dest
47/increment-edi
eb/jump loop/disp8
}
$kernel-string-to-string: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/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
kernel-string-length: # in: (addr kernel-string) -> result/eax: int
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
52/push-edx
# result = 0
b8/copy-to-eax 0/imm32
# var c/ecx: byte = 0
b9/copy-to-ecx 0/imm32
# var curr/edx: (addr byte) = in
8b/-> *(ebp+8) 2/r32/edx
{
$kernel-string-length:loop:
# c = *curr
8a/byte-> *edx 1/r32/ecx
# if (c == 0) break
81 7/subop/compare %ecx 0/imm32
74/jump-if-= break/disp8
# ++curr
42/increment-edx
# ++result
40/increment-eax
#
eb/jump loop/disp8
}
$kernel-string-length:end:
# . restore registers
5a/pop-to-edx
59/pop-to-ecx
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return