https://github.com/akkartik/mu/blob/master/apps/tile/value-stack.mu
  1 # support for non-int values is untested
  2 
  3 type value-stack {
  4   data: (handle array value)
  5   top: int
  6 }
  7 
  8 fn initialize-value-stack _self: (addr value-stack), n: int {
  9   var self/esi: (addr value-stack) <- copy _self
 10   var d/edi: (addr handle array value) <- get self, data
 11   populate d, n
 12   var top/eax: (addr int) <- get self, top
 13   copy-to *top, 0
 14 }
 15 
 16 fn clear-value-stack _self: (addr value-stack) {
 17   var self/esi: (addr value-stack) <- copy _self
 18   var top/eax: (addr int) <- get self, top
 19   copy-to *top, 0
 20 }
 21 
 22 fn push-int-to-value-stack _self: (addr value-stack), _val: int {
 23   var self/esi: (addr value-stack) <- copy _self
 24   var top-addr/ecx: (addr int) <- get self, top
 25   var data-ah/edx: (addr handle array value) <- get self, data
 26   var data/eax: (addr array value) <- lookup *data-ah
 27   var top/edx: int <- copy *top-addr
 28   var dest-offset/edx: (offset value) <- compute-offset data, top
 29   var dest-addr/edx: (addr value) <- index data, dest-offset
 30   var dest-addr2/eax: (addr int) <- get dest-addr, int-data
 31   var val/esi: int <- copy _val
 32 #?   print-int32-hex-to-real-screen val
 33   copy-to *dest-addr2, val
 34   increment *top-addr
 35   dest-addr2 <- get dest-addr, type
 36   copy-to *dest-addr2, 0  # int
 37 }
 38 
 39 fn push-string-to-value-stack _self: (addr value-stack), val: (handle array byte) {
 40   var self/esi: (addr value-stack) <- copy _self
 41   var top-addr/ecx: (addr int) <- get self, top
 42   var data-ah/edx: (addr handle array value) <- get self, data
 43   var data/eax: (addr array value) <- lookup *data-ah
 44   var top/edx: int <- copy *top-addr
 45   var dest-offset/edx: (offset value) <- compute-offset data, top
 46   var dest-addr/edx: (addr value) <- index data, dest-offset
 47   var dest-addr2/eax: (addr handle array byte) <- get dest-addr, text-data
 48   copy-handle val, dest-addr2
 49   var dest-addr3/eax: (addr int) <- get dest-addr, type
 50 #?   print-string 0, "setting type to 1: "
 51 #?   {
 52 #?     var foo/eax: int <- copy dest-addr3
 53 #?     print-int32-hex 0, foo
 54 #?   }
 55 #?   print-string 0, "\n"
 56   copy-to *dest-addr3, 1  # type string
 57   increment *top-addr
 58 }
 59 
 60 fn push-array-to-value-stack _self: (addr value-stack), val: (handle array value) {
 61   var self/esi: (addr value-stack) <- copy _self
 62   var top-addr/ecx: (addr int) <- get self, top
 63   var data-ah/edx: (addr handle array value) <- get self, data
 64   var data/eax: (addr array value) <- lookup *data-ah
 65   var top/edx: int <- copy *top-addr
 66   var dest-offset/edx: (offset value) <- compute-offset data, top
 67   var dest-addr/edx: (addr value) <- index data, dest-offset
 68   var dest-addr2/eax: (addr handle array value) <- get dest-addr, array-data
 69   copy-handle val, dest-addr2
 70   # update type
 71   var dest-addr3/eax: (addr int) <- get dest-addr, type
 72   copy-to *dest-addr3, 2  # type array
 73   increment *top-addr
 74 }
 75 
 76 fn push-value-stack _self: (addr value-stack), val: (addr value) {
 77   var self/esi: (addr value-stack) <- copy _self
 78   var top-addr/ecx: (addr int) <- get self, top
 79   var data-ah/edx: (addr handle array value) <- get self, data
 80   var data/eax: (addr array value) <- lookup *data-ah
 81   var top/edx: int <- copy *top-addr
 82   var dest-offset/edx: (offset value) <- compute-offset data, top
 83   var dest-addr/edx: (addr value) <- index data, dest-offset
 84   copy-object val, dest-addr
 85   increment *top-addr
 86 }
 87 
 88 fn pop-int-from-value-stack _self: (addr value-stack) -> _/eax: int {
 89   var self/esi: (addr value-stack) <- copy _self
 90   var top-addr/ecx: (addr int) <- get self, top
 91   {
 92     compare *top-addr, 0
 93     break-if->
 94     return -1
 95   }
 96   decrement *top-addr
 97   var data-ah/edx: (addr handle array value) <- get self, data
 98   var data/eax: (addr array value) <- lookup *data-ah
 99   var top/edx: int <- copy *top-addr
100   var dest-offset/edx: (offset value) <- compute-offset data, top
101   var result-addr/eax: (addr value) <- index data, dest-offset
102   var result-addr2/eax: (addr int) <- get result-addr, int-data
103   return *result-addr2
104 }
105 
106 fn value-stack-empty? _self: (addr value-stack) -> _/eax: boolean {
107   var self/esi: (addr value-stack) <- copy _self
108   var top/eax: (addr int) <- get self, top
109   compare *top, 0
110   {
111     break-if-!=
112     return 1  # true
113   }
114   return 0  # false
115 }
116 
117 fn value-stack-length _self: (addr value-stack) -> _/eax: int {
118   var self/esi: (addr value-stack) <- copy _self
119   var top-addr/eax: (addr int) <- get self, top
120   return *top-addr
121 }
122 
123 fn value-stack-max-width _self: (addr value-stack) -> _/eax: int {
124   var self/esi: (addr value-stack) <- copy _self
125   var data-ah/edi: (addr handle array value) <- get self, data
126   var _data/eax: (addr array value) <- lookup *data-ah
127   var data/edi: (addr array value) <- copy _data
128   var top-addr/ecx: (addr int) <- get self, top
129   var i/ebx: int <- copy 0
130   var result: int
131   {
132     compare i, *top-addr
133     break-if->=
134     var o/edx: (offset value) <- compute-offset data, i
135     var v/edx: (addr value) <- index data, o
136     var w/eax: int <- value-width v, 1  # top-level=true
137     # if (w > result) w = result
138     {
139       compare w, result
140       break-if-<=
141       copy-to result, w
142     }
143     i <- increment
144     loop
145   }
146   return result
147 }
148 
149 fn save-lines in-h: (handle array (handle array byte)), _out-ah: (addr handle array value) {
150   var _in/eax: (addr array (handle array byte)) <- lookup in-h
151   var in/esi: (addr array (handle array byte)) <- copy _in
152   var len/ecx: int <- length in
153   var out-ah/edi: (addr handle array value) <- copy _out-ah
154   populate out-ah, len
155   var out/eax: (addr array value) <- lookup *out-ah
156   # copy in into out
157   var i/ebx: int <- copy 0
158   {
159     compare i, len
160     break-if->=
161 #?     print-int32-hex 0, i
162 #?     print-string 0, "\n"
163     var src/ecx: (addr handle array byte) <- index in, i
164     var dest-offset/edx: (offset value) <- compute-offset out, i
165     var dest-val/edx: (addr value) <- index out, dest-offset
166     var dest/eax: (addr handle array byte) <- get dest-val, text-data
167     copy-object src, dest
168     var type/edx: (addr int) <- get dest-val, type
169     copy-to *type, 1  # string
170     i <- increment
171     loop
172   }
173 }