6781 - new app: RPN (postfix) calculator

This was surprisingly hard; bugs discovered all over the place.
This commit is contained in:
Kartik Agaram 2020-09-15 22:21:15 -07:00
parent 8815cf7d57
commit ae470b42f1
23 changed files with 451 additions and 12 deletions

View File

@ -393,7 +393,7 @@ parse-hex-int-from-slice: # in: (addr slice) -> result/eax: int
52/push-edx
# ecx = in
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx
# edx = in->end
# var max/edx: (addr byte) = in->end
8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 2/r32/edx 4/disp8 . # copy *(ecx+4) to edx
# var curr/ecx: (addr byte) = in->start
8b/copy 0/mod/indirect 1/rm32/ecx . . . 1/r32/ecx . . # copy *ecx to ecx
@ -582,7 +582,7 @@ test-parse-hex-int-from-slice-0x-prefix:
e8/call parse-hex-int-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 0x34a, msg)
# check-ints-equal(eax, 0x34, msg)
# . . push args
68/push "F - test-parse-hex-int-from-slice-0x-prefix"/imm32
68/push 0x34/imm32
@ -616,7 +616,7 @@ test-parse-hex-int-from-slice-zero:
e8/call parse-hex-int-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 0x34a, msg)
# check-ints-equal(eax, 0, msg)
# . . push args
68/push "F - test-parse-hex-int-from-slice-zero"/imm32
68/push 0/imm32

View File

@ -20,14 +20,13 @@ next-word: # line: (addr stream byte), out: (addr slice)
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi
# edi = out
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi
# skip-chars-matching(line, ' ')
# skip-chars-matching-whitespace(line)
# . . push args
68/push 0x20/imm32/space
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call skip-chars-matching/disp32
e8/call skip-chars-matching-whitespace/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
$next-word:check0:
# if (line->read >= line->write) clear out and return
# . eax = line->read

View File

@ -131,6 +131,19 @@ $print-string-to-real-screen:end:
5d/pop-to-ebp
c3/return
print-slice-to-real-screen: # s: (addr slice)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
#
(write-slice-buffered Stdout *(ebp+8))
(flush Stdout)
$print-slice-to-real-screen:end:
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
print-stream-to-real-screen: # s: (addr stream byte)
# . prologue
55/push-ebp
@ -240,6 +253,8 @@ write-int32-decimal-buffered: # f: (addr buffered-file), n: int
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
# var ecx: (stream byte 16)
81 5/subop/subtract %esp 0x10/imm32
68/push 0x10/imm32/size
@ -249,6 +264,10 @@ write-int32-decimal-buffered: # f: (addr buffered-file), n: int
(write-int32-decimal %ecx *(ebp+0xc))
(write-stream-data *(ebp+8) %ecx)
$write-int32-decimal-buffered:end:
# . reclaim locals
81 0/subop/add %esp 0x1c/imm32
# . restore registers
59/pop-to-ecx
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp

View File

@ -144,6 +144,22 @@ $read-key-from-real-keyboard:end:
5d/pop-to-ebp
c3/return
read-line-from-real-keyboard: # in: (addr stream byte)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
#
(read 0 *(ebp+8)) # => eax
$read-line-from-real-keyboard:end:
# . restore registers
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
== data
# iflags: octal hex

View File

@ -11,7 +11,7 @@ allocate-array2: # ad: (addr allocation-descriptor), array-len: int, elem-size:
52/push-edx
#
8b/-> *(ebp+0xc) 0/r32/eax
f7 4/subop/multiply-into-eax-edx *(ebp+0x10)
f7 4/subop/multiply-into-edx-eax *(ebp+0x10)
# TODO: check edx for overflow
(allocate-array *(ebp+8) %eax *(ebp+0x14))
$allocate-array2:end:

233
311parse-decimal-int.subx Normal file
View File

@ -0,0 +1,233 @@
# Helpers for parsing decimal ints.
parse-decimal-int-from-slice: # in: (addr slice) -> out/eax: int # . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
# ecx = in
8b/-> *(ebp+8) 1/r32/ecx
#
(parse-decimal-int-helper *ecx *(ecx+4)) # => eax
$parse-decimal-int-from-slice:end:
# . restore registers
59/pop-to-ecx
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
parse-decimal-int-helper: # start: (addr byte), end: (addr byte) -> result/eax: int
# . 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 curr/esi: (addr byte) = start
8b/-> *(ebp+8) 6/r32/esi
# edi = end
8b/-> *(ebp+0xc) 7/r32/edi
# var negate?/edx: boolean = false
ba/copy-to-edx 0/imm32/false
# if (*curr == '-') ++curr, negate = true
{
$parse-decimal-int-helper:negative:
b8/copy-to-eax 0/imm32
8a/copy-byte *esi 0/r32/AL
3d/compare-eax-and 0x2d/imm32/-
75/jump-if-!= break/disp8
# . ++curr
46/increment-esi
# . negate = true
ba/copy-to-edx 1/imm32/true
}
# spill negate?
52/push-edx
# var result/eax: int = 0
b8/copy-to-eax 0/imm32
# var digit/ecx: int = 0
b9/copy-to-ecx 0/imm32
# const TEN/ebx: int = 10
bb/copy-to-ebx 0xa/imm32
{
$parse-decimal-int-helper:loop:
# if (curr >= in->end) break
39/compare %esi 7/r32/edi
73/jump-if-addr>= break/disp8
# digit = from-decimal-char(*curr)
8a/copy-byte *esi 1/r32/CL
81 5/subop/subtract %ecx 0x30/imm32/zero
# TODO: error checking
# result = result * 10 + digit
ba/copy-to-edx 0/imm32
f7 4/subop/multiply-into-edx-eax %ebx
# TODO: check edx for overflow
01/add %eax 1/r32/ecx
# ++curr
46/increment-esi
#
eb/jump loop/disp8
}
$parse-decimal-int-helper:negate:
# if (negate?) result = -result
5a/pop-to-edx
{
81 7/subop/compare %edx 0/imm32/false
74/jump-if-= break/disp8
f7 3/subop/negate %eax
}
$parse-decimal-int-helper: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
test-parse-decimal-int-from-slice-single-digit:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "3"
b8/copy-to-eax "3"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax 3 "F - test-parse-decimal-int-from-slice-single-digit")
$test-parse-decimal-int-helper-single-digit:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-decimal-int-from-slice-multi-digit:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "34"
b8/copy-to-eax "34"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax 0x22 "F - test-parse-decimal-int-from-slice-multi-digit") # 34 in hex
$test-parse-decimal-int-helper-multi-digit:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-decimal-int-from-slice-zero:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "00"
b8/copy-to-eax "00"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax 0 "F - test-parse-decimal-int-from-slice-zero")
$test-parse-decimal-int-helper-zero:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-decimal-int-from-slice-negative:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "-3"
b8/copy-to-eax "-3"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax -3 "F - test-parse-decimal-int-from-slice-negative")
$test-parse-decimal-int-helper-negative:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
test-parse-decimal-int-from-slice-multi-digit-negative:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "-32"
b8/copy-to-eax "-32"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax -0x20 "F - test-parse-decimal-int-from-slice-multi-digit-negative") # -32 in hex
$test-parse-decimal-int-helper-multi-digit-negative:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

12
400.mu
View File

@ -69,6 +69,8 @@ sig parse-hex-int-from-slice in: (addr slice) -> result/eax: int
#sig parse-hex-int-helper start: (addr byte), end: (addr byte) -> result/eax: int
sig is-hex-digit? c: byte -> result/eax: boolean
#sig from-hex-char in/eax: byte -> out/eax: nibble
sig parse-decimal-int-from-slice in: (addr slice) -> result/eax: int
#sig parse-decimal-int-helper start: (addr byte), end: (addr byte) -> result/eax: int
sig error-byte ed: (addr exit-descriptor), out: (addr buffered-file), msg: (addr array byte), n: byte
#sig allocate ad: (addr allocation-descriptor), n: int, out: (addr handle _)
#sig allocate-raw ad: (addr allocation-descriptor), n: int, out: (addr handle _)
@ -127,10 +129,10 @@ sig compute-width-of-slice s: (addr slice) -> result/eax: int
sig emit-hex-array out: (addr buffered-file), arr: (addr array byte)
sig next-word-or-string line: (addr stream byte), out: (addr slice)
sig write-int out: (addr stream byte), n: int
sig clear-stack s: (addr stack)
sig push s: (addr stack), n: int
sig pop s: (addr stack) -> n/eax: int
sig top s: (addr stack) -> n/eax: int
#sig clear-stack s: (addr stack)
#sig push s: (addr stack), n: int
#sig pop s: (addr stack) -> n/eax: 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 ad: (addr allocation-descriptor), s: (addr string), out: (addr handle array int)
sig check-array-equal a: (addr array int), expected: (addr string), msg: (addr string)
@ -143,6 +145,7 @@ sig real-screen-size -> nrows/eax: int, ncols/ecx: int
sig clear-real-screen
sig move-cursor-on-real-screen row: int, column: int
sig print-string-to-real-screen s: (addr array byte)
sig print-slice-to-real-screen s: (addr slice)
sig print-stream-to-real-screen s: (addr stream byte)
sig print-grapheme-to-real-screen c: grapheme
sig print-int32-hex-to-real-screen n: int
@ -159,6 +162,7 @@ sig show-cursor-on-real-screen
sig enable-keyboard-immediate-mode
sig enable-keyboard-type-mode
sig read-key-from-real-keyboard -> result/eax: byte
sig read-line-from-real-keyboard in: (addr stream byte)
sig open filename: (addr array byte), write?: boolean, out: (addr handle buffered-file)
sig populate-buffered-file-containing contents: (addr array byte), out: (addr handle buffered-file)
sig new-buffered-file out: (addr handle buffered-file)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
apps/hex

Binary file not shown.

BIN
apps/mu

Binary file not shown.

View File

@ -14518,6 +14518,13 @@ size-of-type-id: # t: type-id -> result/eax: int
b8/copy-to-eax 8/imm32
eb/jump $size-of-type-id:end/disp8 # eax changes type from type-id to int
}
# if t is a slice, return 8
3d/compare-eax-and 0xc/imm32/slice
{
75/jump-if-!= break/disp8
b8/copy-to-eax 8/imm32
eb/jump $size-of-type-id:end/disp8 # eax changes type from type-id to int
}
# if t is a user-defined type, return its size
# TODO: support non-atom type
(find-typeinfo %eax %ecx)

BIN
apps/pack

Binary file not shown.

149
apps/rpn.mu Normal file
View File

@ -0,0 +1,149 @@
# Integer arithmetic using postfix notation
#
# Limitations:
# No division yet.
#
# To build:
# $ ./translate_mu apps/rpn.mu
#
# Example session:
# $ ./a.elf
# press ctrl-c or ctrl-d to exit
# > 1
# 1
# > 1 1 +
# 2
# > 1 2 3 + +
# 6
# > 1 2 3 * +
# 7
# > 1 2 + 3 *
# 9
# > 1 3 4 * +
# 13
# > ^D
# $
#
# Error handling is non-existent. This is just a prototype.
fn main -> exit-status/ebx: int {
var in-storage: (stream byte 0x100)
var in/esi: (addr stream byte) <- address in-storage
print-string 0, "press ctrl-c or ctrl-d to exit\n"
# read-eval-print loop
{
# print prompt
print-string 0, "> "
# read line
clear-stream in
read-line-from-real-keyboard in
var done?/eax: boolean <- stream-empty? in
compare done?, 0
break-if-!=
# parse and eval
var out/eax: int <- simplify in
# print
print-int32-decimal 0, out
print-string 0, "\n"
#
loop
}
exit-status <- copy 0
}
type int-stack {
data: (handle array int)
top: int
}
fn simplify in: (addr stream byte) -> result/eax: int {
var word-storage: slice
var word/ecx: (addr slice) <- address word-storage
var stack-storage: int-stack
var stack/esi: (addr int-stack) <- address stack-storage
initialize-stack stack, 0x10
$simplify:word-loop: {
next-word in, word
var done?/eax: boolean <- slice-empty? word
compare done?, 0
break-if-!=
# if word is an operator, perform it
{
var is-add?/eax: boolean <- slice-equal? word, "+"
compare is-add?, 0
break-if-=
var _b/eax: int <- pop-int-stack stack
var b/edx: int <- copy _b
var a/eax: int <- pop-int-stack stack
a <- add b
push-int-stack stack, a
loop $simplify:word-loop
}
{
var is-sub?/eax: boolean <- slice-equal? word, "-"
compare is-sub?, 0
break-if-=
var _b/eax: int <- pop-int-stack stack
var b/edx: int <- copy _b
var a/eax: int <- pop-int-stack stack
a <- subtract b
push-int-stack stack, a
loop $simplify:word-loop
}
{
var is-mul?/eax: boolean <- slice-equal? word, "*"
compare is-mul?, 0
break-if-=
var _b/eax: int <- pop-int-stack stack
var b/edx: int <- copy _b
var a/eax: int <- pop-int-stack stack
a <- multiply b
push-int-stack stack, a
loop $simplify:word-loop
}
# otherwise it's an int
var n/eax: int <- parse-decimal-int-from-slice word
push-int-stack stack, n
loop
}
result <- pop-int-stack stack
}
fn initialize-stack _self: (addr int-stack), n: int {
var self/esi: (addr int-stack) <- copy _self
var d/edi: (addr handle array int) <- get self, data
populate d, n
var top/eax: (addr int) <- get self, top
copy-to *top, 0
}
fn push-int-stack _self: (addr int-stack), _val: int {
var self/esi: (addr int-stack) <- copy _self
var top-addr/ecx: (addr int) <- get self, top
var data-ah/edx: (addr handle array int) <- get self, data
var data/eax: (addr array int) <- lookup *data-ah
var top/edx: int <- copy *top-addr
var dest-addr/edx: (addr int) <- index data, top
var val/eax: int <- copy _val
copy-to *dest-addr, val
add-to *top-addr, 1
}
fn pop-int-stack _self: (addr int-stack) -> val/eax: int {
$pop-int-stack:body: {
var self/esi: (addr int-stack) <- copy _self
var top-addr/ecx: (addr int) <- get self, top
{
compare *top-addr, 0
break-if->
val <- copy 0
break $pop-int-stack:body
}
subtract-from *top-addr, 1
var data-ah/edx: (addr handle array int) <- get self, data
var data/eax: (addr array int) <- lookup *data-ah
var top/edx: int <- copy *top-addr
var result-addr/eax: (addr int) <- index data, top
val <- copy *result-addr
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -80,6 +80,8 @@ subx-gen-run-tests: # in: (addr buffered-file), out: (addr buffered-file)
# read-line-buffered(in, line)
# if (line->write == 0) break # end of file
# var word-slice = next-word(line)
# if slice-empty?(word-slice) # empty line
# continue
# if is-label?(word-slice)
# if slice-starts-with?(word-slice, "test-")
# tests-found = true
@ -184,6 +186,16 @@ $subx-gen-run-tests:check0:
e8/call next-word/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
$subx-gen-run-tests:check-empty:
# if slice-empty?(word-slice) break
# . eax = slice-empty?(word-slice)
52/push-edx
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
75/jump-if-!= $subx-gen-run-tests:loop/disp8
$subx-gen-run-tests:check-for-label:
# if (!is-label?(word-slice)) continue
# . eax = is-label?(word-slice)