about summary refs log tree commit diff stats
path: root/apps/advent2020/2b.mu
blob: 116029b0c5ab992a2e8a7f4d270b96615359f6ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# https://adventofcode.com/2020/day/2
#
# To run (on Linux):
#   $ git clone https://github.com/akkartik/mu
#   $ cd mu
#   $ ./translate_mu apps/advent2020/2b.mu
#   $ ./a.elf < input
#
# You'll need to register to download the 'input' file for yourself.

fn main -> _/ebx: int {
  var valid-password-count/edi: int <- copy 0
  var line-storage: (stream byte 0x100)  # 256 bytes
  var line/edx: (addr stream byte) <- address line-storage
  var slice-storage: slice
  var slice/ecx: (addr slice) <- address slice-storage
  {
    # read line from stdin
    clear-stream line
    read-line-from-real-keyboard line
    # if line is empty (not even a newline), quit
    var done?/eax: boolean <- stream-empty? line
    compare done?, 0  # false
    break-if-!=
    print-stream-to-real-screen line
    # slice = next-token(line, '-')
    next-token line, 0x2d, slice
    # pos1 = parse-int(slice)
    var _pos1/eax: int <- parse-decimal-int-from-slice slice
    var pos1/ebx: int <- copy _pos1
    var dash/eax: byte <- read-byte line  # skip '-'
    # slice = next-token(line, ' ')
    next-token line, 0x20, slice
    var _pos2/eax: int <- parse-decimal-int-from-slice slice
    var pos2/esi: int <- copy _pos2
    print-int32-decimal 0, pos1
    print-string 0, " "
    print-int32-decimal 0, pos2
    print-string 0, "\n"
    compare pos1, pos2
    {
      break-if-<=
      print-string 0, "out of order!\n"
      return 1
    }
    # letter = next non-space
    skip-chars-matching-whitespace line
    var letter/eax: byte <- read-byte line
    # skip some stuff
    {
      var colon/eax: byte <- read-byte line  # skip ':'
    }
    skip-chars-matching-whitespace line
    # now check the rest of the line
    var is-valid?/eax: boolean <- is-valid? pos1, pos2, letter, line
    compare is-valid?, 0  # false
    {
      break-if-=
      print-string 0, "valid!\n"
      valid-password-count <- increment
    }
    loop
  }
  print-int32-decimal 0, valid-password-count
  print-string 0, "\n"
  return 0
}

# ideally password would be a random-access array
# we'll just track an index
# one benefit: we can easily start at 1
fn is-valid? pos1: int, pos2: int, letter: byte, password: (addr stream byte) -> _/eax: boolean {
  var i/esi: int <- copy 1
  var letter-count/edi: int <- copy 0
  # while password stream isn't empty
  #   c = read byte from password
  #   if (c == letter)
  #     if (i == pos1)
  #       ++letter-count
  #     if (i == pos2)
  #       ++letter-count
  #     ++i
  {
#?     print-string 0, "  "
#?     print-int32-decimal 0, i
#?     print-string 0, "\n"
    var done?/eax: boolean <- stream-empty? password
    compare done?, 0  # false
    break-if-!=
    var c/eax: byte <- read-byte password
#?     {
#?       var c2/eax: int <- copy c
#?       print-int32-decimal 0, c2
#?       print-string 0, "\n"
#?     }
    compare c, letter
    {
      break-if-!=
      compare i, pos1
      {
        break-if-!=
        letter-count <- increment
#?         print-string 0, "  hit\n"
      }
      compare i, pos2
      {
        break-if-!=
        letter-count <- increment
#?         print-string 0, "  hit\n"
      }
    }
    i <- increment
    loop
  }
  # return (letter-count == 1)
  compare letter-count, 1
  {
    break-if-!=
    return 1  # true
  }
  return 0  # false
}
abandon.cc?h=main&id=882989243a31354b3b82df021847bb332b483fd2'>^
192d59d3 ^
0be82cde ^

192d59d3 ^
0be82cde ^
192d59d3 ^
0be82cde ^
192d59d3 ^
0be82cde ^
















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
                                               
                                                                       


                       




                                                                                    
                              

                                                        
                             
 
                                                                           

                                                                            
                                                   
 






                                                                                   

                                                                                                                
                         
                 
                                          
                                      
                                  



                                                                         
                                        
                                              
                                                                                  
                                       



                                                                                      
                                            

                                                
   
                 
                                                                 

                                         


                                                                                                      

 
                                 




                                                                                        
                         
                                                         




                                                                                                                                                                        
                



                                         




                                                             
                              

          
                             


                             




                                                        
                              
 
                                                
                             
 
                                
          
                                  
                                            

                                  





                                                       
                              
          
                                  
                                                       

                         

         
                                  
                                                                             
                         


                                                       


                                                                          






                                                      
                                      
          
                  
                     

                                                                   
                                                                                   
                                             


                                                                                 




                                                  
               

          
                                  
                               

                                                               





















                                                                                                                                




                                                                                          














                                                                                                                                                                                 
               

          
                                  
                                        
                              
                                                                  
                         
















                                                                                                                                        
//: Reclaiming memory when it's no longer used.
//: The top of the address layer has the complete life cycle of memory.

:(scenario new_reclaim)
def main [
  1:address:num <- new number:type
  2:num <- copy 1:address:num  # because 1 will get reset during abandon below
  1:address:num <- copy 0  # abandon
  3:address:num <- new number:type  # must be same size as abandoned memory to reuse
  4:num <- copy 3:address:num
  5:bool <- equal 2:num, 4:num
]
# both allocations should have returned the same address
+mem: storing 1 in location 5

:(before "End Decrement Refcount(old_address, payload_type, payload_size)")
if (old_refcount == 0) {
  trace(9999, "mem") << "automatically abandoning " << old_address << end();
  abandon(old_address, payload_type, payload_size);
}

//: When abandoning addresses we'll save them to a 'free list', segregated by size.

:(before "End routine Fields")
map<int, int> free_list;

:(code)
void abandon(int address, const type_tree* payload_type, int payload_size) {
  trace(9999, "abandon") << "updating refcounts inside " << address << ": " << to_string(payload_type) << end();
//?   Total_free += size;
//?   ++Num_free;
//?   cerr << "abandon: " << size << '\n';
  // decrement any contained refcounts
  if (is_mu_array(payload_type)) {
    reagent element;
    element.type = copy_array_element(payload_type);
    int array_length = get_or_insert(Memory, address+/*skip refcount*/1);
    assert(element.type->name != "array");
    int element_size = size_of(element);
    for (int i = 0;  i < array_length;  ++i) {
      element.set_value(address + /*skip refcount and length*/2 + i*element_size);
      decrement_any_refcounts(element);
    }
  }
  else if (is_mu_container(payload_type) || is_mu_exclusive_container(payload_type)) {
    reagent tmp;
    tmp.type = new type_tree(*payload_type);
    tmp.set_value(address + /*skip refcount*/1);
    decrement_any_refcounts(tmp);
  }
  // clear memory
  for (int curr = address;  curr < address+payload_size;  ++curr)
    put(Memory, curr, 0);
  // append existing free list to address
  trace(9999, "abandon") << "saving " << address << " in free-list of size " << payload_size << end();
  put(Memory, address, get_or_insert(Current_routine->free_list, payload_size));
  put(Current_routine->free_list, payload_size, address);
}

:(after "Allocate Special-cases")
if (get_or_insert(Current_routine->free_list, size)) {
  trace(9999, "abandon") << "picking up space from free-list of size " << size << end();
  int result = get_or_insert(Current_routine->free_list, size);
  trace(9999, "mem") << "new alloc from free list: " << result << end();
  put(Current_routine->free_list, size, get_or_insert(Memory, result));
  put(Memory, result, 0);
  for (int curr = result;  curr < result+size;  ++curr) {
    if (get_or_insert(Memory, curr) != 0) {
      raise << maybe(current_recipe_name()) << "memory in free list was not zeroed out: " << curr << '/' << result << "; somebody wrote to us after free!!!\n" << end();
      break;  // always fatal
    }
  }
  return result;
}

:(scenario new_differing_size_no_reclaim)
def main [
  1:address:num <- new number:type
  2:num <- copy 1:address:num
  1:address:num <- copy 0  # abandon
  3:address:array:num <- new number:type, 2  # different size
  4:num <- copy 3:address:array:num
  5:bool <- equal 2:num, 4:num
]
# no reuse
+mem: storing 0 in location 5

:(scenario new_reclaim_array)
def main [
  1:address:array:num <- new number:type, 2
  2:num <- copy 1:address:array:num
  1:address:array:num <- copy 0  # abandon
  3:address:array:num <- new number:type, 2  # same size
  4:num <- copy 3:address:array:num
  5:bool <- equal 2:num, 4:num
]
# both calls to new returned identical addresses
+mem: storing 1 in location 5

:(scenario abandon_on_overwrite)
def main [
  1:address:num <- new number:type
  # over-writing one allocation with another
  1:address:num <- new number:type
  1:address:num <- copy 0
]
+run: {1: ("address" "number")} <- new {number: "type"}
+mem: incrementing refcount of 1000: 0 -> 1
+run: {1: ("address" "number")} <- new {number: "type"}
+mem: automatically abandoning 1000

:(scenario abandon_after_call)
def main [
  1:address:num <- new number:type
  # passing in addresses to recipes increments refcount
  foo 1:address:num
  1:address:num <- copy 0
]
def foo [
  2:address:num <- next-ingredient
  # return does NOT yet decrement refcount; memory must be explicitly managed
  2:address:num <- copy 0
]
+run: {1: ("address" "number")} <- new {number: "type"}
+mem: incrementing refcount of 1000: 0 -> 1
+run: foo {1: ("address" "number")}
# leave ambiguous precisely when the next increment happens; a later layer
# will mess with that
+mem: incrementing refcount of 1000: 1 -> 2
+run: {2: ("address" "number")} <- copy {0: "literal"}
+mem: decrementing refcount of 1000: 2 -> 1
+run: {1: ("address" "number")} <- copy {0: "literal"}
+mem: decrementing refcount of 1000: 1 -> 0
+mem: automatically abandoning 1000

:(scenario abandon_on_overwrite_array)
def main [
  1:num <- copy 30
  # allocate an array
  10:address:array:num <- new number:type, 20
  11:num <- copy 10:address:array:num  # doesn't increment refcount
  # allocate another array in its place, implicitly freeing the previous allocation
  10:address:array:num <- new number:type, 25
]
+run: {10: ("address" "array" "number")} <- new {number: "type"}, {25: "literal"}
# abandoned array is of old size (20, not 25)
+abandon: saving 1000 in free-list of size 22

:(scenario refcounts_abandon_address_in_container)
# container containing an address
container foo [
  x:address:num
]
def main [
  1:address:num <- new number:type
  2:address:foo <- new foo:type
  *2:address:foo <- put *2:address:foo, x:offset, 1:address:num
  1:address:num <- copy 0
  2:address:foo <- copy 0
]
+run: {1: ("address" "number")} <- new {number: "type"}
+mem: incrementing refcount of 1000: 0 -> 1
+run: {2: ("address" "foo")} <- new {foo: "type"}
+mem: incrementing refcount of 1002: 0 -> 1
+run: {2: ("address" "foo"), "lookup": ()} <- put {2: ("address" "foo"), "lookup": ()}, {x: "offset"}, {1: ("address" "number")}
+mem: incrementing refcount of 1000: 1 -> 2
+run: {1: ("address" "number")} <- copy {0: "literal"}
+mem: decrementing refcount of 1000: 2 -> 1
+run: {2: ("address" "foo")} <- copy {0: "literal"}
# start abandoning container containing address
+mem: decrementing refcount of 1002: 1 -> 0
# nested abandon
+mem: decrementing refcount of 1000: 1 -> 0
+abandon: saving 1000 in free-list of size 2
# actually abandon the container containing address
+abandon: saving 1002 in free-list of size 2

# todo: move past dilated reagent
:(scenario refcounts_abandon_address_in_array)
def main [
  1:address:num <- new number:type
  2:address:array:address:num <- new {(address number): type}, 3
  *2:address:array:address:num <- put-index *2:address:array:address:num, 1, 1:address:num
  1:address:num <- copy 0
  2:address:array:address:num <- copy 0
]
+run: {1: ("address" "number")} <- new {number: "type"}
+mem: incrementing refcount of 1000: 0 -> 1
+run: {2: ("address" "array" "address" "number"), "lookup": ()} <- put-index {2: ("address" "array" "address" "number"), "lookup": ()}, {1: "literal"}, {1: ("address" "number")}
+mem: incrementing refcount of 1000: 1 -> 2
+run: {1: ("address" "number")} <- copy {0: "literal"}
+mem: decrementing refcount of 1000: 2 -> 1
+run: {2: ("address" "array" "address" "number")} <- copy {0: "literal"}
# nested abandon
+mem: decrementing refcount of 1000: 1 -> 0
+abandon: saving 1000 in free-list of size 2

:(scenario refcounts_abandon_address_in_container_in_array)
# container containing an address
container foo [
  x:address:num
]
def main [
  1:address:num <- new number:type
  2:address:array:foo <- new foo:type, 3
  3:foo <- merge 1:address:num
  *2:address:array:foo <- put-index *2:address:array:foo, 1, 3:foo
  1:address:num <- copy 0
  3:foo <- merge 0
  2:address:array:foo <- copy 0
]
+run: {1: ("address" "number")} <- new {number: "type"}
+mem: incrementing refcount of 1000: 0 -> 1
+run: {3: "foo"} <- merge {1: ("address" "number")}
+mem: incrementing refcount of 1000: 1 -> 2
+run: {2: ("address" "array" "foo"), "lookup": ()} <- put-index {2: ("address" "array" "foo"), "lookup": ()}, {1: "literal"}, {3: "foo"}
+mem: incrementing refcount of 1000: 2 -> 3
+run: {1: ("address" "number")} <- copy {0: "literal"}
+mem: decrementing refcount of 1000: 3 -> 2
+run: {3: "foo"} <- merge {0: "literal"}
+mem: decrementing refcount of 1000: 2 -> 1
+run: {2: ("address" "array" "foo")} <- copy {0: "literal"}
# nested abandon
+mem: decrementing refcount of 1000: 1 -> 0
+abandon: saving 1000 in free-list of size 2