This commit is contained in:
Kartik Agaram 2020-10-27 00:49:02 -07:00
parent f926aaedfc
commit 5df70f4dff
3 changed files with 209 additions and 1 deletions

View File

@ -413,3 +413,210 @@ $test-decimal-size-multi-digit-negative:end:
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
_parse-array-of-decimal-ints: # ad: (addr allocation-descriptor), s: (addr array byte), out: (addr handle array int)
# pseudocode
# end = &s->data[s->size]
# curr = s->data
# size = 0
# while true
# if (curr >= end) break
# curr = skip-chars-matching-in-slice(curr, end, ' ')
# if (curr >= end) break
# curr = skip-chars-not-matching-in-slice(curr, end, ' ')
# ++size
# allocate-array(ad, size*4, out)
# var slice: slice = {s->data, 0}
# curr = lookup(out)->data
# while true
# if (slice->start >= end) break
# slice->start = skip-chars-matching-in-slice(slice->start, end, ' ')
# if (slice->start >= end) break
# slice->end = skip-chars-not-matching-in-slice(slice->start, end, ' ')
# *curr = parse-hex-int-from-slice(slice)
# curr += 4
# slice->start = slice->end
# return result
#
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
52/push-edx
53/push-ebx
56/push-esi
57/push-edi
# esi = s
8b/-> *(ebp+0xc) 6/r32/esi
# var curr/ecx: (addr byte) = s->data
8d/copy-address *(esi+4) 1/r32/ecx
# var end/edx: (addr byte) = &s->data[s->size]
# . edx = s->size
8b/-> *esi 2/r32/edx
# . edx += curr
01/add-to %edx 1/r32/ecx
# var size/ebx: int = 0
31/xor-with %ebx 3/r32/ebx
$_parse-array-of-decimal-ints:loop1:
# if (curr >= end) break
39/compare %ecx 2/r32/edx
73/jump-if-addr>= $_parse-array-of-decimal-ints:break1/disp8
# curr = skip-chars-matching-in-slice(curr, end, ' ')
(skip-chars-matching-in-slice %ecx %edx 0x20) # => eax
89/<- %ecx 0/r32/eax
# if (curr >= end) break
39/compare %ecx 2/r32/edx
73/jump-if-addr>= $_parse-array-of-decimal-ints:break1/disp8
# curr = skip-chars-not-matching-in-slice(curr, end, ' ')
(skip-chars-not-matching-in-slice %ecx %edx 0x20) # => eax
89/<- %ecx 0/r32/eax
# size += 4
81 0/subop/add %ebx 4/imm32
eb/jump $_parse-array-of-decimal-ints:loop1/disp8
$_parse-array-of-decimal-ints:break1:
(allocate-array *(ebp+8) %ebx *(ebp+0x10))
$_parse-array-of-decimal-ints:pass2:
# var slice/edi: slice = {s->data, 0}
68/push 0/imm32/end
8d/copy-address *(esi+4) 7/r32/edi
57/push-edi
89/<- %edi 4/r32/esp
# curr = lookup(out)->data
8b/-> *(ebp+0x10) 0/r32/eax
(lookup *eax *(eax+4)) # => eax
8d/copy-address *(eax+4) 1/r32/ecx
$_parse-array-of-decimal-ints:loop2:
# if (slice->start >= end) break
39/compare *edi 2/r32/edx
73/jump-if-addr>= $_parse-array-of-decimal-ints:end/disp8
# slice->start = skip-chars-matching-in-slice(slice->start, end, ' ')
(skip-chars-matching-in-slice *edi %edx 0x20) # => eax
89/<- *edi 0/r32/eax
# if (slice->start >= end) break
39/compare *edi 2/r32/edx
73/jump-if-addr>= $_parse-array-of-decimal-ints:end/disp8
# slice->end = skip-chars-not-matching-in-slice(slice->start, end, ' ')
(skip-chars-not-matching-in-slice *edi %edx 0x20) # => eax
89/<- *(edi+4) 0/r32/eax
# *curr = parse-hex-int-from-slice(slice)
(parse-decimal-int-from-slice %edi)
89/<- *ecx 0/r32/eax
# curr += 4
81 0/subop/add %ecx 4/imm32
# slice->start = slice->end
8b/-> *(edi+4) 0/r32/eax
89/<- *edi 0/r32/eax
eb/jump $_parse-array-of-decimal-ints:loop2/disp8
$_parse-array-of-decimal-ints:end:
# . reclaim locals
81 0/subop/add %esp 8/imm32
# . 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/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-array-of-decimal-ints:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# var h/esi: (handle array int)
68/push 0/imm32
68/push 0/imm32
89/<- %esi 4/r32/esp
# var ecx: (array int) = [1, 2, 3]
68/push 3/imm32
68/push 2/imm32
68/push 1/imm32
68/push 0xc/imm32/size
89/<- %ecx 4/r32/esp
#
(_parse-array-of-decimal-ints Heap "1 2 3" %esi)
(lookup *esi *(esi+4)) # => eax
(array-equal? %ecx %eax) # => eax
(check-ints-equal %eax 1 "F - test-parse-array-of-decimal-ints")
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-array-of-decimal-ints-empty:
# - empty string = empty array
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# var h/esi: handle
68/push 0/imm32
68/push 0/imm32
89/<- %esi 4/r32/esp
#
(_parse-array-of-decimal-ints Heap "" %esi)
(lookup *esi *(esi+4)) # => eax
(check-ints-equal *eax 0 "F - test-parse-array-of-decimal-ints-empty")
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-array-of-decimal-ints-just-whitespace:
# - just whitespace = empty array
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# var h/esi: handle
68/push 0/imm32
68/push 0/imm32
89/<- %esi 4/r32/esp
#
(_parse-array-of-decimal-ints Heap Space %esi)
(lookup *esi *(esi+4)) # => eax
(check-ints-equal *eax 0 "F - test-parse-array-of-decimal-ints-just-whitespace")
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-array-of-decimal-ints-extra-whitespace:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# var h/esi: handle
68/push 0/imm32
68/push 0/imm32
89/<- %esi 4/r32/esp
# var ecx: (array int) = [1, 2, 3]
68/push 3/imm32
68/push 2/imm32
68/push 1/imm32
68/push 0xc/imm32/size
89/<- %ecx 4/r32/esp
#
(_parse-array-of-decimal-ints Heap " 1 2 3 " %esi)
(lookup *esi *(esi+4)) # => eax
(array-equal? %ecx %eax) # => eax
(check-ints-equal %eax 1 "F - test-parse-array-of-decimal-ints-extra-whitespace")
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
parse-array-of-decimal-ints: # s: (addr array byte), out: (addr handle array int)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
#
(_parse-array-of-decimal-ints Heap *(ebp+8) *(ebp+0xc))
$parse-array-of-decimal-ints:end:
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

1
400.mu
View File

@ -140,6 +140,7 @@ sig write-int out: (addr stream byte), n: int
#sig top s: (addr stack) -> n/eax: int
sig array-equal? a: (addr array int), b: (addr array int) -> result/eax: boolean
sig parse-array-of-ints s: (addr array byte), out: (addr handle array int)
sig parse-array-of-decimal-ints s: (addr array byte), out: (addr handle array int)
sig check-array-equal a: (addr array int), expected: (addr string), msg: (addr string)
#sig push-n-zero-bytes n: int
sig kernel-string-to-string ad: (addr allocation-descriptor), in: (addr kernel-string), out: (addr handle array byte)

View File

@ -402,7 +402,7 @@ fn evaluate functions: (addr handle function), bindings: (addr table), scratch:
var input/eax: (addr array byte) <- lookup *input-ah
var h2: (handle array int)
var int-array-ah/esi: (addr handle array int) <- address h2
parse-array-of-ints input, int-array-ah # leak
parse-array-of-decimal-ints input, int-array-ah # leak
var _int-array/eax: (addr array int) <- lookup *int-array-ah
var int-array/esi: (addr array int) <- copy _int-array
var len/ebx: int <- length int-array