https://github.com/akkartik/mu/blob/master/034address.cc
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 :(before "End Globals")
43 long long Next_alloc_id = 0;
44 :(before "End Reset")
45 Next_alloc_id = 0;
46
47
48
49
50
51
52
53
54
55 :(scenario new)
56
57
58 def main [
59 10:&:num <- new num:type
60 12:&:num <- new num:type
61 20:bool <- equal 10:&:num, 12:&:num
62 ]
63 +mem: storing 1000 in location 11
64 +mem: storing 0 in location 20
65
66 :(scenario new_array)
67
68 def main [
69 10:&:@:num <- new num:type, 5
70 12:&:num <- new num:type
71 20:num/alloc2, 21:num/alloc1 <- deaddress 10:&:@:num, 12:&:num
72 30:num <- subtract 21:num/alloc2, 20:num/alloc1
73 ]
74 +run: {10: ("address" "array" "number")} <- new {num: "type"}, {5: "literal"}
75 +mem: array length is 5
76
77 +mem: storing 1000 in location 11
78
79 +mem: storing 7 in location 30
80
81 :(scenario dilated_reagent_with_new)
82 def main [
83 10:&:&:num <- new {(& num): type}
84 ]
85 +new: size of '(& num)' is 2
86
87
88 :(before "End Mu Types Initialization")
89 put(Type_ordinal, "type", 0);
90 :(code)
91 bool is_mu_type_literal(const reagent& r) {
92 return is_literal(r) && r.type && r.type->name == "type";
93 }
94
95 :(before "End Primitive Recipe Declarations")
96 NEW,
97 :(before "End Primitive Recipe Numbers")
98 put(Recipe_ordinal, "new", NEW);
99 :(before "End Primitive Recipe Checks")
100 case NEW: {
101 const recipe& caller = get(Recipe, r);
102 if (inst.ingredients.empty() || SIZE(inst.ingredients) > 2) {
103 raise << maybe(caller.name) << "'new' requires one or two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
104 break;
105 }
106
107 const reagent& type = inst.ingredients.at(0);
108 if (!is_mu_type_literal(type)) {
109 raise << maybe(caller.name) << "first ingredient of 'new' should be a type, but got '" << type.original_string << "'\n" << end();
110 break;
111 }
112 if (SIZE(inst.ingredients) > 1 && !is_mu_number(inst.ingredients.at(1))) {
113 raise << maybe(caller.name) << "second ingredient of 'new' should be a number (array length), but got '" << type.original_string << "'\n" << end();
114 break;
115 }
116 if (inst.products.empty()) {
117 raise << maybe(caller.name) << "result of 'new' should never be ignored\n" << end();
118 break;
119 }
120 if (!product_of_new_is_valid(inst)) {
121 raise << maybe(caller.name) << "product of 'new' has incorrect type: '" << to_original_string(inst) << "'\n" << end();
122 break;
123 }
124 break;
125 }
126 :(code)
127 bool product_of_new_is_valid(const instruction& inst) {
128 reagent product = inst.products.at(0);
129
130 if (!product.type || product.type->atom || product.type->left->value != Address_type_ordinal)
131 return false;
132 drop_from_type(product, "address");
133 if (SIZE(inst.ingredients) > 1) {
134
135 if (!product.type || product.type->atom || product.type->left->value != Array_type_ordinal)
136 return false;
137 drop_from_type(product, "array");
138 }
139 reagent expected_product(new_type_tree(inst.ingredients.at(0).name));
140 return types_strictly_match(product, expected_product);
141 }
142
143 void drop_from_type(reagent& r, string expected_type) {
144 assert(!r.type->atom);
145 if (r.type->left->name != expected_type) {
146 raise << "can't drop2 " << expected_type << " from '" << to_string(r) << "'\n" << end();
147 return;
148 }
149
150 type_tree* tmp = r.type;
151 r.type = tmp->right;
152 tmp->right = NULL;
153 delete tmp;
154
155 assert(!r.type->atom);
156 if (r.type->right) return;
157 tmp = r.type;
158 r.type = tmp->left;
159 tmp->left = NULL;
160 delete tmp;
161 }
162
163 :(scenario new_returns_incorrect_type)
164 % Hide_errors = true;
165 def main [
166 1:bool <- new num:type
167 ]
168 +error: main: product of 'new' has incorrect type: '1:bool <- new num:type'
169
170 :(scenario new_discerns_singleton_list_from_atom_container)
171 % Hide_errors = true;
172 def main [
173 1:&:num <- new {(num): type}
174 ]
175 +error: main: product of 'new' has incorrect type: '1:&:num <- new {(num): type}'
176
177 :(scenario new_with_type_abbreviation)
178 def main [
179 1:&:num <- new num:type
180 ]
181 $error: 0
182
183 :(scenario new_with_type_abbreviation_inside_compound)
184 def main [
185 {1: (address address number), raw: ()} <- new {(& num): type}
186 ]
187 $error: 0
188
189 :(scenario equal_result_of_new_with_null)
190 def main [
191 1:&:num <- new num:type
192 10:bool <- equal 1:&:num, null
193 ]
194 +mem: storing 0 in location 10
195
196
197
198
199
200
201
202 :(before "End Primitive Recipe Checks")
203 case ALLOCATE: {
204 raise << "never call 'allocate' directly'; always use 'new'\n" << end();
205 break;
206 }
207 :(before "End Primitive Recipe Implementations")
208 case NEW: {
209 raise << "no implementation for 'new'; why wasn't it translated to 'allocate'? Please save a copy of your program and send it to Kartik.\n" << end();
210 break;
211 }
212
213 :(after "Transform.push_back(check_instruction)")
214 Transform.push_back(transform_new_to_allocate);
215
216 :(code)
217 void transform_new_to_allocate(const recipe_ordinal r) {
218 trace(9991, "transform") << "--- convert 'new' to 'allocate' for recipe " << get(Recipe, r).name << end();
219 for (int i = 0; i < SIZE(get(Recipe, r).steps); ++i) {
220 instruction& inst = get(Recipe, r).steps.at(i);
221
222 if (inst.name == "new") {
223 if (inst.ingredients.empty()) return;
224 inst.operation = ALLOCATE;
225 type_tree* type = new_type_tree(inst.ingredients.at(0).name);
226 inst.ingredients.at(0).set_value(size_of(type));
227 trace(9992, "new") << "size of '" << inst.ingredients.at(0).name << "' is " << inst.ingredients.at(0).value << end();
228 delete type;
229 }
230 }
231 }
232
233
234
235 :(before "End Globals")
236 extern const int Reserved_for_tests = 1000;
237 int Memory_allocated_until = Reserved_for_tests;
238 int Initial_memory_per_routine = 100000;
239 :(before "End Reset")
240 Memory_allocated_until = Reserved_for_tests;
241 Initial_memory_per_routine = 100000;
242 :(before "End routine Fields")
243 int alloc, alloc_max;
244 :(before "End routine Constructor")
245 alloc = Memory_allocated_until;
246 Memory_allocated_until += Initial_memory_per_routine;
247 alloc_max = Memory_allocated_until;
248 trace("new") << "routine allocated memory from " << alloc << " to " << alloc_max << end();
249
250 :(before "End Primitive Recipe Declarations")
251 ALLOCATE,
252 :(before "End Primitive Recipe Numbers")
253 put(Recipe_ordinal, "allocate", ALLOCATE);
254 :(before "End Primitive Recipe Implementations")
255 case ALLOCATE: {
256
257 int size = ingredients.at(0).at(0);
258 int alloc_id = Next_alloc_id;
259 Next_alloc_id++;
260 if (SIZE(ingredients) > 1) {
261
262 trace("mem") << "array length is " << ingredients.at(1).at(0) << end();
263 size = 1 + size*ingredients.at(1).at(0);
264 }
265 int result = allocate(size);
266
267 trace("mem") << "storing alloc-id " << alloc_id << " in location " << result << end();
268 put(Memory, result, alloc_id);
269 if (SIZE(current_instruction().ingredients) > 1) {
270
271 trace("mem") << "storing array length " << ingredients.at(1).at(0) << " in location " << result+1 << end();
272 put(Memory, result+1, ingredients.at(1).at(0));
273 }
274 products.resize(1);
275 products.at(0).push_back(alloc_id);
276 products.at(0).push_back(result);
277 break;
278 }
279 :(code)
280 int allocate(int size) {
281
282 ++size;
283 trace("mem") << "allocating size " << size << end();
284
285
286
287
288
289 ensure_space(size);
290 const int result = Current_routine->alloc;
291 trace("mem") << "new alloc: " << result << end();
292
293 for (int address = result; address < result+size; ++address) {
294 trace("mem") << "storing 0 in location " << address << end();
295 put(Memory, address, 0);
296 }
297 Current_routine->alloc += size;
298
299 assert(Current_routine->alloc <= Current_routine->alloc_max);
300 return result;
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 :(code)
318 void ensure_space(int size) {
319 if (size > Initial_memory_per_routine) {
320 cerr << "can't allocate " << size << " locations, that's too much compared to " << Initial_memory_per_routine << ".\n";
321 exit(1);
322 }
323 if (Current_routine->alloc + size > Current_routine->alloc_max) {
324
325 Current_routine->alloc = Memory_allocated_until;
326 Memory_allocated_until += Initial_memory_per_routine;
327 Current_routine->alloc_max = Memory_allocated_until;
328 trace("new") << "routine allocated memory from " << Current_routine->alloc << " to " << Current_routine->alloc_max << end();
329 }
330 }
331
332 :(scenario new_initializes)
333 % Memory_allocated_until = 10;
334 % put(Memory, Memory_allocated_until, 1);
335 def main [
336 1:&:num <- new num:type
337 ]
338 +mem: storing 0 in location 10
339 +mem: storing 0 in location 11
340 +mem: storing 10 in location 2
341
342 :(scenario new_initializes_alloc_id)
343 % Memory_allocated_until = 10;
344 % put(Memory, Memory_allocated_until, 1);
345 % Next_alloc_id = 23;
346 def main [
347 1:&:num <- new num:type
348 ]
349
350 +mem: storing 0 in location 10
351 +mem: storing 0 in location 11
352
353 +mem: storing alloc-id 23 in location 10
354
355 +mem: storing 23 in location 1
356
357 :(scenario new_size)
358 def main [
359 10:&:num <- new num:type
360 12:&:num <- new num:type
361 20:num/alloc1, 21:num/alloc2 <- deaddress 10:&:num, 12:&:num
362 30:num <- subtract 21:num/alloc2, 20:num/alloc1
363 ]
364
365 +mem: storing 2 in location 30
366
367 :(scenario new_array_size)
368 def main [
369 10:&:@:num <- new num:type, 5
370 12:&:num <- new num:type
371 20:num/alloc1, 21:num/alloc2 <- deaddress 10:&:num, 12:&:num
372 30:num <- subtract 21:num/alloc2, 20:num/alloc1
373 ]
374
375 +mem: storing 7 in location 30
376
377 :(scenario new_empty_array)
378 def main [
379 10:&:@:num <- new num:type, 0
380 12:&:num <- new num:type
381 20:num/alloc1, 21:num/alloc2 <- deaddress 10:&:@:num, 12:&:num
382 30:num <- subtract 21:num/alloc2, 20:num/alloc1
383 ]
384 +run: {10: ("address" "array" "number")} <- new {num: "type"}, {0: "literal"}
385 +mem: array length is 0
386
387 +mem: storing 2 in location 30
388
389
390 :(scenario new_overflow)
391 % Initial_memory_per_routine = 3; // barely enough room for point allocation below
392 def main [
393 10:&:num <- new num:type
394 12:&:point <- new point:type
395 ]
396 +new: routine allocated memory from 1000 to 1003
397 +new: routine allocated memory from 1003 to 1006
398
399 :(scenario new_without_ingredient)
400 % Hide_errors = true;
401 def main [
402 1:&:num <- new
403 ]
404 +error: main: 'new' requires one or two ingredients, but got '1:&:num <- new'
405
406
407
408 :(before "End Primitive Recipe Declarations")
409 DEADDRESS,
410 :(before "End Primitive Recipe Numbers")
411 put(Recipe_ordinal, "deaddress", DEADDRESS);
412 :(before "End Primitive Recipe Checks")
413 case DEADDRESS: {
414
415 for (int i = 0; i < SIZE(inst.ingredients); ++i) {
416 if (!is_mu_address(inst.ingredients.at(i))) {
417 raise << maybe(get(Recipe, r).name) << "'deaddress' requires address ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
418 goto finish_checking_instruction;
419 }
420 }
421 if (SIZE(inst.products) > SIZE(inst.ingredients)) {
422 raise << maybe(get(Recipe, r).name) << "too many products in '" << to_original_string(inst) << "'\n" << end();
423 break;
424 }
425 for (int i = 0; i < SIZE(inst.products); ++i) {
426 if (!is_real_mu_number(inst.products.at(i))) {
427 raise << maybe(get(Recipe, r).name) << "'deaddress' requires number products, but got '" << inst.products.at(i).original_string << "'\n" << end();
428 goto finish_checking_instruction;
429 }
430 }
431 break;
432 }
433 :(before "End Primitive Recipe Implementations")
434 case DEADDRESS: {
435 products.resize(SIZE(ingredients));
436 for (int i = 0; i < SIZE(ingredients); ++i) {
437 products.at(i).push_back(ingredients.at(i).at(1));
438 }
439 break;
440 }