From 5d57bfa0e17ba4b435d3cb507c83b9d1cc5b0287 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Fri, 3 Apr 2020 19:36:57 -0700 Subject: standard library tests all passing --- 100array-equal.subx | 107 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 43 deletions(-) (limited to '100array-equal.subx') diff --git a/100array-equal.subx b/100array-equal.subx index 6e4d5225..fea70997 100644 --- a/100array-equal.subx +++ b/100array-equal.subx @@ -171,7 +171,7 @@ test-compare-inequal-arrays-equal-sizes: 5d/pop-to-ebp c3/return -parse-array-of-ints: # ad: (addr allocation-descriptor), s: (addr string) -> result/eax: (handle array int) +parse-array-of-ints: # ad: (addr allocation-descriptor), s: (addr string), out: (addr handle array int) # pseudocode # end = &s->data[s->size] # curr = s->data @@ -182,17 +182,16 @@ parse-array-of-ints: # ad: (addr allocation-descriptor), s: (addr string) -> re # if (curr >= end) break # curr = skip-chars-not-matching-in-slice(curr, end, ' ') # ++size - # result = allocate(ad, (size+1)*4) - # result->size = (size+1)*4 + # allocate-array(ad, size*4, out) # var slice: slice = {s->data, 0} - # out = result->data + # 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, ' ') - # *out = parse-hex-int-from-slice(slice) - # out += 4 + # *curr = parse-hex-int-from-slice(slice) + # curr += 4 # slice->start = slice->end # return result # @@ -200,6 +199,7 @@ parse-array-of-ints: # ad: (addr allocation-descriptor), s: (addr string) -> re 55/push-ebp 89/<- %ebp 4/r32/esp # . save registers + 50/push-eax 51/push-ecx 52/push-edx 53/push-ebx @@ -233,46 +233,40 @@ $parse-array-of-ints:loop1: 81 0/subop/add %ebx 4/imm32 eb/jump $parse-array-of-ints:loop1/disp8 $parse-array-of-ints:break1: - # var result/edi: (handle array int) - 89/<- %eax 3/r32/ebx - 05/add-to-eax 4/imm32 - (allocate *(ebp+8) %eax) - 89/<- %edi 0/r32/eax - # result->size = size - 89/<- *edi 3/r32/ebx + (allocate-array *(ebp+8) %ebx *(ebp+0x10)) $parse-array-of-ints:pass2: - # var slice/ecx: slice = {s->data, 0} + # var slice/edi: slice = {s->data, 0} 68/push 0/imm32/end - 8d/copy-address *(esi+4) 1/r32/ecx - 51/push-ecx - 89/<- %ecx 4/r32/esp - # var out/ebx: (addr byte) = result->data - 8d/copy-address *(eax+4) 3/r32/ebx + 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-ints:loop2: # if (slice->start >= end) break - 39/compare *ecx 2/r32/edx + 39/compare *edi 2/r32/edx 73/jump-if-addr>= $parse-array-of-ints:end/disp8 # slice->start = skip-chars-matching-in-slice(slice->start, end, ' ') - (skip-chars-matching-in-slice *ecx %edx 0x20) # => eax - 89/<- *ecx 0/r32/eax + (skip-chars-matching-in-slice *edi %edx 0x20) # => eax + 89/<- *edi 0/r32/eax # if (slice->start >= end) break - 39/compare *ecx 2/r32/edx + 39/compare *edi 2/r32/edx 73/jump-if-addr>= $parse-array-of-ints:end/disp8 # slice->end = skip-chars-not-matching-in-slice(slice->start, end, ' ') - (skip-chars-not-matching-in-slice *ecx %edx 0x20) # => eax - 89/<- *(ecx+4) 0/r32/eax - # *out = parse-hex-int-from-slice(slice) - (parse-hex-int-from-slice %ecx) - 89/<- *ebx 0/r32/eax - # out += 4 - 81 0/subop/add %ebx 4/imm32 - # slice->start = slice->end - 8b/-> *(ecx+4) 0/r32/eax + (skip-chars-not-matching-in-slice *edi %edx 0x20) # => eax + 89/<- *(edi+4) 0/r32/eax + # *curr = parse-hex-int-from-slice(slice) + (parse-hex-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-ints:loop2/disp8 $parse-array-of-ints:end: - # return edi - 89/<- %eax 7/r32/edi # . reclaim locals 81 0/subop/add %esp 8/imm32 # . restore registers @@ -281,6 +275,7 @@ $parse-array-of-ints:end: 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 @@ -290,6 +285,10 @@ test-parse-array-of-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 @@ -297,7 +296,8 @@ test-parse-array-of-ints: 68/push 0xc/imm32/size 89/<- %ecx 4/r32/esp # - (parse-array-of-ints Heap "1 2 3") # => eax + (parse-array-of-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-ints") # . epilogue @@ -310,8 +310,13 @@ test-parse-array-of-ints-empty: # . 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-ints Heap "") # => eax + (parse-array-of-ints Heap "" %esi) + (lookup *esi *(esi+4)) # => eax (check-ints-equal *eax 0 "F - test-parse-array-of-ints-empty") # . epilogue 89/<- %esp 5/r32/ebp @@ -323,9 +328,14 @@ test-parse-array-of-ints-just-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 # - (parse-array-of-ints Heap Space) # => eax - (check-ints-equal *eax 0 "F - test-parse-array-of-ints-empty") + (parse-array-of-ints Heap Space %esi) + (lookup *esi *(esi+4)) # => eax + (check-ints-equal *eax 0 "F - test-parse-array-of-ints-just-whitespace") # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp @@ -335,6 +345,10 @@ test-parse-array-of-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 @@ -342,7 +356,8 @@ test-parse-array-of-ints-extra-whitespace: 68/push 0xc/imm32/size 89/<- %ecx 4/r32/esp # - (parse-array-of-ints Heap " 1 2 3 ") # => eax + (parse-array-of-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-ints-extra-whitespace") # . epilogue @@ -358,14 +373,20 @@ check-array-equal: # a: (addr array int), expected: (addr string), msg: (addr s 89/<- %ebp 4/r32/esp # . save registers 50/push-eax - # var b/ecx: (handle array int) = parse-array-of-ints(Heap, expected) - (parse-array-of-ints Heap *(ebp+0xc)) # => eax - 89/<- %ecx 0/r32/eax + 56/push-esi + # var h/esi: handle + 68/push 0/imm32 + 68/push 0/imm32 + 89/<- %esi 4/r32/esp + # var b/eax: (addr array int) = parse-array-of-ints(Heap, expected) + (parse-array-of-ints Heap *(ebp+0xc) %esi) + (lookup *esi *(esi+4)) # => eax # - (array-equal? *(ebp+8) %ecx) + (array-equal? *(ebp+8) %eax) (check-ints-equal %eax 1 *(ebp+0x10)) $check-array-equal:end: # . restore registers + 5e/pop-to-esi 58/pop-to-eax # . epilogue 89/<- %esp 5/r32/ebp -- cgit 1.4.1-2-gfad0