From ea8a64cfb0f431f92846242332c51714cbaf5909 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Thu, 12 Nov 2020 23:51:43 -0800 Subject: 7232 - mu.subx: more checks for byte types --- apps/mu | Bin 552851 -> 554858 bytes apps/mu.subx | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 149 insertions(+), 11 deletions(-) (limited to 'apps') diff --git a/apps/mu b/apps/mu index f5535cf8..12d13375 100755 Binary files a/apps/mu and b/apps/mu differ diff --git a/apps/mu.subx b/apps/mu.subx index 36022a49..69d85941 100644 --- a/apps/mu.subx +++ b/apps/mu.subx @@ -3191,7 +3191,6 @@ test-dereference-of-var-on-stack: 5d/pop-to-ebp c3/return -# variables of type 'byte' are not allowed on the stack test-convert-function-with-byte-operations: # . prologue 55/push-ebp @@ -3249,6 +3248,96 @@ test-convert-function-with-byte-operations: 5d/pop-to-ebp c3/return +# variables of type 'byte' are not allowed on the stack +test-byte-values-on-stack: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # setup + (clear-stream _test-input-stream) + (clear-stream $_test-input-buffered-file->buffer) + (clear-stream _test-output-stream) + (clear-stream $_test-output-buffered-file->buffer) + (clear-stream _test-error-stream) + (clear-stream $_test-error-buffered-file->buffer) + # var ed/edx: exit-descriptor = tailor-exit-descriptor(16) + 68/push 0/imm32 + 68/push 0/imm32 + 89/<- %edx 4/r32/esp + (tailor-exit-descriptor %edx 0x10) + # + (write _test-input-stream "fn foo {\n") + (write _test-input-stream " var x: byte\n") + (write _test-input-stream "}\n") + # convert + (convert-mu _test-input-buffered-file _test-output-buffered-file _test-error-buffered-file %edx) + # registers except esp clobbered at this point + # restore ed + 89/<- %edx 4/r32/esp + (flush _test-output-buffered-file) + (flush _test-error-buffered-file) +#? # dump _test-error-stream {{{ +#? (write 2 "^") +#? (write-stream 2 _test-error-stream) +#? (write 2 "$\n") +#? (rewind-stream _test-error-stream) +#? # }}} + # check output + (check-stream-equal _test-output-stream "" "F - test-byte-values-on-stack: output should be empty") + (check-next-stream-line-equal _test-error-stream "fn foo: var 'x' of type 'byte' cannot be on the stack" "F - test-byte-values-on-stack: error message") + # check that stop(1) was called + (check-ints-equal *(edx+4) 2 "F - test-byte-values-on-stack: exit status") + # don't restore from ebp + 81 0/subop/add %esp 8/imm32 + # . epilogue + 5d/pop-to-ebp + c3/return + +# variables of type 'byte' are not allowed in esi or edi +test-byte-values-in-unsupported-registers: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # setup + (clear-stream _test-input-stream) + (clear-stream $_test-input-buffered-file->buffer) + (clear-stream _test-output-stream) + (clear-stream $_test-output-buffered-file->buffer) + (clear-stream _test-error-stream) + (clear-stream $_test-error-buffered-file->buffer) + # var ed/edx: exit-descriptor = tailor-exit-descriptor(16) + 68/push 0/imm32 + 68/push 0/imm32 + 89/<- %edx 4/r32/esp + (tailor-exit-descriptor %edx 0x10) + # + (write _test-input-stream "fn foo {\n") + (write _test-input-stream " var x/esi: byte <- copy 0\n") + (write _test-input-stream "}\n") + # convert + (convert-mu _test-input-buffered-file _test-output-buffered-file _test-error-buffered-file %edx) + # registers except esp clobbered at this point + # restore ed + 89/<- %edx 4/r32/esp + (flush _test-output-buffered-file) + (flush _test-error-buffered-file) +#? # dump _test-error-stream {{{ +#? (write 2 "^") +#? (write-stream 2 _test-error-stream) +#? (write 2 "$\n") +#? (rewind-stream _test-error-stream) +#? # }}} + # check output + (check-stream-equal _test-output-stream "" "F - test-byte-values-in-unsupported-registers: output should be empty") + (check-next-stream-line-equal _test-error-stream "fn foo: var 'x' of type 'byte' cannot be in esi or edi" "F - test-byte-values-in-unsupported-registers: error message") + # check that stop(1) was called + (check-ints-equal *(edx+4) 2 "F - test-byte-values-in-unsupported-registers: exit status") + # don't restore from ebp + 81 0/subop/add %esp 8/imm32 + # . epilogue + 5d/pop-to-ebp + c3/return + # variables of type 'byte' _can_ be function args. They then occupy 4 bytes. test-copy-byte-var-from-fn-arg: # . prologue @@ -16275,7 +16364,7 @@ parse-mu-var-def: # line: (addr stream byte), vars: (addr stack live-var), out: 50/push-eax 51/push-ecx 52/push-edx - 53/push-ebx + 56/push-esi 57/push-edi # edi = out 8b/-> *(ebp+0x10) 7/r32/edi @@ -16290,17 +16379,21 @@ parse-mu-var-def: # line: (addr stream byte), vars: (addr stack live-var), out: # v = parse-var-with-type(next-mu-token(line)) (next-mu-token *(ebp+8) %ecx) (parse-var-with-type %ecx *(ebp+8) %edx *(ebp+0x18) *(ebp+0x1c)) - # var v-addr/eax: (addr var) + # var v-addr/esi: (addr var) (lookup *edx *(edx+4)) # => eax + 89/<- %esi 0/r32/eax # v->block-depth = *Curr-block-depth - 8b/-> *Curr-block-depth 3/r32/ebx - 89/<- *(eax+0x10) 3/r32/ebx # Var-block-depth + 8b/-> *Curr-block-depth 0/r32/eax + 89/<- *(esi+0x10) 0/r32/eax # Var-block-depth # either v has no register and there's no more to this line - 8b/-> *(eax+0x18) 0/r32/eax # Var-register - 3d/compare-eax-and 0/imm32 + 81 7/subop/compare *(esi+0x18) 0/imm32 { 75/jump-if-!= break/disp8 - # TODO: disallow vars of type 'byte' on the stack + # if v-addr->type == byte, abort + (lookup *(esi+8) *(esi+0xc)) # Var-type Var-type => eax + (is-simple-mu-type? %eax 8) # byte => eax + 3d/compare-eax-and 0/imm32/false + 0f 85/jump-if-!= $parse-mu-var-def:error-byte-on-stack/disp32 # ensure that there's nothing else on this line (next-mu-token *(ebp+8) %ecx) (slice-empty? %ecx) # => eax @@ -16313,7 +16406,21 @@ parse-mu-var-def: # line: (addr stream byte), vars: (addr stack live-var), out: # or v has a register and there's more to this line { 0f 84/jump-if-= break/disp32 - # TODO: disallow vars of type 'byte' in registers 'esi' or 'edi' + # if v-addr->type == byte, check for unsupported registers + { + (lookup *(esi+8) *(esi+0xc)) # Var-type Var-type => eax + (is-simple-mu-type? %eax 8) # byte => eax + 3d/compare-eax-and 0/imm32/false + 74/jump-if-= break/disp8 + (lookup *(esi+0x18) *(esi+0x1c)) # => eax + (string-equal? %eax "esi") # => eax + 3d/compare-eax-and 0/imm32/false + 0f 85/jump-if-!= $parse-mu-var-def:error-byte-registers/disp32 + (lookup *(esi+0x18) *(esi+0x1c)) # => eax + (string-equal? %eax "edi") # => eax + 3d/compare-eax-and 0/imm32/false + 0f 85/jump-if-!= $parse-mu-var-def:error-byte-registers/disp32 + } # TODO: vars of type 'byte' should only be initialized by clearing to 0 # ensure that the next word is '<-' (next-mu-token *(ebp+8) %ecx) @@ -16335,7 +16442,7 @@ $parse-mu-var-def:end: 81 0/subop/add %esp 0x10/imm32 # . restore registers 5f/pop-to-edi - 5b/pop-to-ebx + 5e/pop-to-esi 5a/pop-to-edx 59/pop-to-ecx 58/pop-to-eax @@ -16356,7 +16463,6 @@ $parse-mu-var-def:error1: # never gets here $parse-mu-var-def:error2: - (rewind-stream *(ebp+8)) # error("fn " fn ": var " var ": variables on the stack can't take an initializer\n") (write-buffered *(ebp+0x18) "fn ") 8b/-> *(ebp+0x14) 0/r32/eax @@ -16372,6 +16478,38 @@ $parse-mu-var-def:error2: (stop *(ebp+0x1c) 1) # never gets here +$parse-mu-var-def:error-byte-on-stack: + # error("fn " fn ": var '" var "' of type 'byte' cannot be on the stack\n") + (write-buffered *(ebp+0x18) "fn ") + 8b/-> *(ebp+0x14) 0/r32/eax + (lookup *eax *(eax+4)) # Function-name Function-name => eax + (write-buffered *(ebp+0x18) %eax) + (write-buffered *(ebp+0x18) ": var '") + # var v-addr/eax: (addr var) = lookup(v) + (lookup *edx *(edx+4)) # => eax + (lookup *eax *(eax+4)) # Var-name Var-name => eax + (write-buffered *(ebp+0x18) %eax) + (write-buffered *(ebp+0x18) "' of type 'byte' cannot be on the stack\n") + (flush *(ebp+0x18)) + (stop *(ebp+0x1c) 1) + # never gets here + +$parse-mu-var-def:error-byte-registers: + # error("fn " fn ": var '" var "' of type 'byte' cannot be in esi or edi\n") + (write-buffered *(ebp+0x18) "fn ") + 8b/-> *(ebp+0x14) 0/r32/eax + (lookup *eax *(eax+4)) # Function-name Function-name => eax + (write-buffered *(ebp+0x18) %eax) + (write-buffered *(ebp+0x18) ": var '") + # var v-addr/eax: (addr var) = lookup(v) + (lookup *edx *(edx+4)) # => eax + (lookup *eax *(eax+4)) # Var-name Var-name => eax + (write-buffered *(ebp+0x18) %eax) + (write-buffered *(ebp+0x18) "' of type 'byte' cannot be in esi or edi\n") + (flush *(ebp+0x18)) + (stop *(ebp+0x1c) 1) + # never gets here + test-parse-mu-var-def: # 'var n: int' # . prologue -- cgit 1.4.1-2-gfad0