From a9baaac00b4bacfbee2986482ac0c37a7461927c Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Tue, 31 Dec 2019 21:58:52 -0800 Subject: 5847 - literal inputs --- apps/assort | Bin 42800 -> 43152 bytes apps/braces | Bin 42816 -> 43168 bytes apps/calls | Bin 47526 -> 47878 bytes apps/crenshaw2-1 | Bin 42208 -> 42560 bytes apps/crenshaw2-1b | Bin 42755 -> 43107 bytes apps/dquotes | Bin 46450 -> 46802 bytes apps/factorial | Bin 41227 -> 41579 bytes apps/handle | Bin 42125 -> 42477 bytes apps/hex | Bin 45047 -> 45399 bytes apps/mu | Bin 71441 -> 74956 bytes apps/mu.subx | 208 +++++++++++++++++++++++++++++++++++++++++++++++++++++- apps/pack | Bin 55192 -> 55544 bytes apps/sigils | Bin 55201 -> 55553 bytes apps/survey | Bin 52041 -> 52393 bytes apps/tests | Bin 41598 -> 41950 bytes 15 files changed, 207 insertions(+), 1 deletion(-) (limited to 'apps') diff --git a/apps/assort b/apps/assort index b8cba438..c351c1d0 100755 Binary files a/apps/assort and b/apps/assort differ diff --git a/apps/braces b/apps/braces index ef7eb54a..6d210604 100755 Binary files a/apps/braces and b/apps/braces differ diff --git a/apps/calls b/apps/calls index 15bbc5db..023f3faf 100755 Binary files a/apps/calls and b/apps/calls differ diff --git a/apps/crenshaw2-1 b/apps/crenshaw2-1 index a579ee45..173e1f0f 100755 Binary files a/apps/crenshaw2-1 and b/apps/crenshaw2-1 differ diff --git a/apps/crenshaw2-1b b/apps/crenshaw2-1b index a398a0bc..c960d67f 100755 Binary files a/apps/crenshaw2-1b and b/apps/crenshaw2-1b differ diff --git a/apps/dquotes b/apps/dquotes index ae526a6c..d3bc8105 100755 Binary files a/apps/dquotes and b/apps/dquotes differ diff --git a/apps/factorial b/apps/factorial index dd981d0d..90f5d904 100755 Binary files a/apps/factorial and b/apps/factorial differ diff --git a/apps/handle b/apps/handle index 23993ca6..46571e0a 100755 Binary files a/apps/handle and b/apps/handle differ diff --git a/apps/hex b/apps/hex index d349dea8..2e91f142 100755 Binary files a/apps/hex and b/apps/hex differ diff --git a/apps/mu b/apps/mu index 16f01034..bef1a8b6 100755 Binary files a/apps/mu and b/apps/mu differ diff --git a/apps/mu.subx b/apps/mu.subx index 624846a1..5f3513f4 100644 --- a/apps/mu.subx +++ b/apps/mu.subx @@ -742,6 +742,124 @@ test-convert-function-returns-result: 5d/pop-to-ebp c3/return +test-convert-function-literal-arg: + # function writes to output + # fn foo a: int, b: int -> result/eax: int { + # result <- copy a + # result <- add 1 + # } + # => + # foo: + # # . prologue + # 55/push-ebp + # 89/<- %ebp 4/r32/esp + # { + # 89/-> *(ebp+8) 0/r32/eax + # 05/add-to-eax 1/imm32 + # } + # # . epilogue + # 89/<- %esp 5/r32/ebp + # 5d/pop-to-ebp + # c3/return + # . 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) + # + (write _test-input-stream "fn foo a: int, b: int -> result/eax: int {\n") + (write _test-input-stream " result <- copy a\n") + (write _test-input-stream " result <- add 1\n") + (write _test-input-stream "}\n") + # convert + (convert-mu _test-input-buffered-file _test-output-buffered-file) + (flush _test-output-buffered-file) +#? # dump _test-output-stream {{{ +#? (write 2 "^") +#? (write-stream 2 _test-output-stream) +#? (write 2 "$\n") +#? (rewind-stream _test-output-stream) +#? # }}} + # check output + (check-next-stream-line-equal _test-output-stream "foo:" "F - test-convert-function-literal-arg/0") + (check-next-stream-line-equal _test-output-stream "# . prologue" "F - test-convert-function-literal-arg/1") + (check-next-stream-line-equal _test-output-stream "55/push-ebp" "F - test-convert-function-literal-arg/2") + (check-next-stream-line-equal _test-output-stream "89/<- %ebp 4/r32/esp" "F - test-convert-function-literal-arg/3") + (check-next-stream-line-equal _test-output-stream "{" "F - test-convert-function-literal-arg/4") + (check-next-stream-line-equal _test-output-stream "8b/copy-from *(ebp+0x00000008) 0x00000000/r32" "F - test-convert-function-literal-arg/5") + (check-next-stream-line-equal _test-output-stream "05/add-to-eax 1/imm32" "F - test-convert-function-literal-arg/6") + (check-next-stream-line-equal _test-output-stream "}" "F - test-convert-function-literal-arg/7") + (check-next-stream-line-equal _test-output-stream "# . epilogue" "F - test-convert-function-literal-arg/8") + (check-next-stream-line-equal _test-output-stream "89/<- %esp 5/r32/ebp" "F - test-convert-function-literal-arg/9") + (check-next-stream-line-equal _test-output-stream "5d/pop-to-ebp" "F - test-convert-function-literal-arg/10") + (check-next-stream-line-equal _test-output-stream "c3/return" "F - test-convert-function-literal-arg/11") + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-convert-function-literal-arg-2: + # function writes to output + # fn foo a: int, b: int -> result/ebx: int { + # result <- copy a + # result <- add 1 + # } + # => + # foo: + # # . prologue + # 55/push-ebp + # 89/<- %ebp 4/r32/esp + # { + # 89/-> *(ebp+8) 3/r32/ebx + # 81 0/subop/add %ebx 1/imm32 + # } + # # . epilogue + # 89/<- %esp 5/r32/ebp + # 5d/pop-to-ebp + # c3/return + # . 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) + # + (write _test-input-stream "fn foo a: int, b: int -> result/ebx: int {\n") + (write _test-input-stream " result <- copy a\n") + (write _test-input-stream " result <- add 1\n") + (write _test-input-stream "}\n") + # convert + (convert-mu _test-input-buffered-file _test-output-buffered-file) + (flush _test-output-buffered-file) +#? # dump _test-output-stream {{{ +#? (write 2 "^") +#? (write-stream 2 _test-output-stream) +#? (write 2 "$\n") +#? (rewind-stream _test-output-stream) +#? # }}} + # check output + (check-next-stream-line-equal _test-output-stream "foo:" "F - test-convert-function-literal-arg-2/0") + (check-next-stream-line-equal _test-output-stream "# . prologue" "F - test-convert-function-literal-arg-2/1") + (check-next-stream-line-equal _test-output-stream "55/push-ebp" "F - test-convert-function-literal-arg-2/2") + (check-next-stream-line-equal _test-output-stream "89/<- %ebp 4/r32/esp" "F - test-convert-function-literal-arg-2/3") + (check-next-stream-line-equal _test-output-stream "{" "F - test-convert-function-literal-arg-2/4") + (check-next-stream-line-equal _test-output-stream "8b/copy-from *(ebp+0x00000008) 0x00000003/r32" "F - test-convert-function-literal-arg-2/5") + (check-next-stream-line-equal _test-output-stream "81 0/subop/add %ebx 1/imm32" "F - test-convert-function-literal-arg-2/6") + (check-next-stream-line-equal _test-output-stream "}" "F - test-convert-function-literal-arg-2/7") + (check-next-stream-line-equal _test-output-stream "# . epilogue" "F - test-convert-function-literal-arg-2/8") + (check-next-stream-line-equal _test-output-stream "89/<- %esp 5/r32/ebp" "F - test-convert-function-literal-arg-2/9") + (check-next-stream-line-equal _test-output-stream "5d/pop-to-ebp" "F - test-convert-function-literal-arg-2/10") + (check-next-stream-line-equal _test-output-stream "c3/return" "F - test-convert-function-literal-arg-2/11") + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + ####################################################### # Parsing ####################################################### @@ -2237,7 +2355,7 @@ $parse-mu-stmt:read-inouts: 3d/compare-eax-and 0/imm32 0f 85/jump-if-not-equal $parse-mu-stmt:abort2/disp32 # - (lookup-var %ecx *(ebp+0xc)) # => eax # TODO: lookup-var-or-literal + (lookup-var-or-literal %ecx *(ebp+0xc)) # => eax (append-list Heap %eax *(edi+8)) # Stmt1-inouts => eax 89/<- *(edi+8) 0/r32/eax # Stmt1-inouts e9/jump loop/disp32 @@ -2326,6 +2444,55 @@ $stmt-has-outputs:end: 5d/pop-to-ebp c3/return +# if 'name' starts with a digit, create a new literal var for it +# otherwise return first 'name' from the top (back) of 'vars' and abort if not found +lookup-var-or-literal: # name: (address slice), vars : (address stack (handle var)) -> result/eax: (handle var) + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + 56/push-esi + # esi = name + 8b/-> *(ebp+8) 6/r32/esi + # if slice-empty?(name) abort + (slice-empty? %esi) # => eax + 3d/compare-eax-and 0/imm32 + 0f 85/jump-if-not-equal $lookup-var-or-literal:abort/disp32 + # var ecx : byte = *name->start + 8b/-> *esi 1/r32/ecx + 8a/copy-byte *ecx 1/r32/CL + 81 4/subop/and %ecx 0xff/imm32 + # if is-decimal-digit?(*name->start) return new var(name) + (is-decimal-digit? %ecx) # => eax + 81 7/subop/compare %eax 0/imm32 + { + 74/jump-if-equal break/disp8 + (new-literal-integer Heap %esi) # => eax + } + # otherwise return lookup-var(name, vars) + { + 75/jump-if-not-equal break/disp8 + (lookup-var %esi *(ebp+0xc)) # => eax + } +$lookup-var-or-literal:end: + # . restore registers + 5e/pop-to-esi + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +$lookup-var-or-literal:abort: + (write-buffered Stderr "empty variable!") + (flush Stderr) + # . syscall(exit, 1) + bb/copy-to-ebx 1/imm32 + b8/copy-to-eax 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + # return first 'name' from the top (back) of 'vars' and abort if not found lookup-var: # name: (address slice), vars : (address stack (handle var)) -> result/eax: (handle var) # . prologue @@ -2578,6 +2745,45 @@ $new-var:end: 5d/pop-to-ebp c3/return +new-literal-integer: # ad: (address allocation-descriptor), name: (address slice) -> result/eax: (handle var) + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + # if (!is-hex-int?(name)) abort + (is-hex-int? *(ebp+0xc)) # => eax + 3d/compare-eax-and 0/imm32 + 0f 84/jump-if-equal $new-literal-integer:abort/disp32 + # var s/ecx : (address array byte) + (slice-to-string Heap *(ebp+0xc)) # => eax + 89/<- %ecx 0/r32/eax + # + (allocate *(ebp+8) *Var-size) # => eax + 89/<- *eax 1/r32/ecx # Var-name + c7 0/subop/copy *(eax+4) 0/imm32/tag/literal # Var-type + c7 0/subop/copy *(eax+8) 0/imm32 # Var-block + c7 0/subop/copy *(eax+0xc) 0/imm32 # Var-stack-offset + c7 0/subop/copy *(eax+0x10) 0/imm32 # Var-register +$new-literal-integer:end: + # . restore registers + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +$new-literal-integer:abort: + (write-buffered Stderr "variable cannot begin with a digit '") + (write-slice-buffered Stderr *(ebp+0xc)) + (write-buffered Stderr "'\n") + (flush Stderr) + # . syscall(exit, 1) + bb/copy-to-ebx 1/imm32 + b8/copy-to-eax 1/imm32/exit + cd/syscall 0x80/imm8 + # never gets here + new-block: # ad: (address allocation-descriptor), data: (handle list statement) -> result/eax: (handle statement) # . prologue 55/push-ebp diff --git a/apps/pack b/apps/pack index 005c42f8..18eec14c 100755 Binary files a/apps/pack and b/apps/pack differ diff --git a/apps/sigils b/apps/sigils index a1f33ada..53166e58 100755 Binary files a/apps/sigils and b/apps/sigils differ diff --git a/apps/survey b/apps/survey index 2ec38e5d..f0a911d4 100755 Binary files a/apps/survey and b/apps/survey differ diff --git a/apps/tests b/apps/tests index b292f93b..fcbde7e2 100755 Binary files a/apps/tests and b/apps/tests differ -- cgit 1.4.1-2-gfad0