https://github.com/akkartik/mu/blob/master/035lookup.cc
  1 //: Go from an address to the payload it points at using /lookup.
  2 //:
  3 //: The tests in this layer use unsafe operations so as to stay decoupled from
  4 //: 'new'.
  5 
  6 :(scenario copy_indirect)
  7 def main [
  8   # skip alloc id for 10:&:num
  9   11:num <- copy 20
 10   # skip alloc id for payload
 11   21:num <- copy 94
 12   # Treat locations 10 and 11 as an address to look up, pointing at the
 13   # payload in locations 20 and 21.
 14   30:num <- copy 10:&:num/lookup
 15 ]
 16 +mem: storing 94 in location 30
 17 
 18 :(before "End Preprocess read_memory(x)")
 19 canonize(x);
 20 
 21 //: similarly, write to addresses pointing at other locations using the
 22 //: 'lookup' property
 23 :(scenario store_indirect)
 24 def main [
 25   # skip alloc id for 10:&:num
 26   11:num <- copy 10
 27   10:&:num/lookup <- copy 94
 28 ]
 29 +mem: storing 94 in location 11
 30 
 31 :(before "End Preprocess write_memory(x, data)")
 32 canonize(x);
 33 
 34 //: writes to address 0 always loudly fail
 35 :(scenario store_to_0_fails)
 36 % Hide_errors = true;
 37 def main [
 38   10:&:num <- copy null
 39   10:&:num/lookup <- copy 94
 40 ]
 41 -mem: storing 94 in location 0
 42 +error: main: tried to lookup 0 in '10:&:num/lookup <- copy 94'
 43 
 44 //: attempts to /lookup address 0 always loudly fail
 45 :(scenario lookup_0_fails)
 46 % Hide_errors = true;
 47 def main [
 48   10:&:num <- copy null
 49   20:num <- copy 10:&:num/lookup
 50 ]
 51 +error: main: tried to lookup 0 in '20:num <- copy 10:&:num/lookup'
 52 
 53 :(scenario lookup_0_dumps_callstack)
 54 % Hide_errors = true;
 55 def main [
 56   foo null
 57 ]
 58 def foo [
 59   10:&:num <- next-input
 60   20:num <- copy 10:&:num/lookup
 61 ]
 62 +error: foo: tried to lookup 0 in '20:num <- copy 10:&:num/lookup'
 63 +error:   called from main: foo null
 64 
 65 :(code)
 66 void canonize(reagent& x) {
 67   if (is_literal(x)) return;
 68   // Begin canonize(x) Lookups
 69   while (has_property(x, "lookup"))
 70     lookup_memory(x);
 71 }
 72 
 73 void lookup_memory(reagent& x) {
 74   if (!x.type || x.type->atom || x.type->left->value != Address_type_ordinal) {
 75     raise << maybe(current_recipe_name()) << "tried to lookup '" << x.original_string << "' but it isn't an address\n" << end();
 76     dump_callstack();
 77     return;
 78   }
 79   // compute value
 80   if (x.value == 0) {
 81     raise << maybe(current_recipe_name()) << "tried to lookup 0\n" << end();
 82     dump_callstack();
 83     return;
 84   }
 85   lookup_memory_core(x, /*check_for_null*/true);
 86 }
 87 
 88 void lookup_memory_core(reagent& x, bool check_for_null) {
 89   double generated by cgit-pink 1.4.1-2-gfad0 (git 2.36.2.497.gbbea4dcf42) at 2024-12-03 20:06:06 +0000
 


94
340   12:num <- copy 95
341   13:num <- copy 96
342   # skip alloc id for address
343   21:num <- copy 30
344   # skip alloc id for payload
345   31:num <- copy 1  # index
346   10:@:num <- put-index 10:@:num, 20:&:num/lookup, 97
347 ]
348 +mem: storing 97 in location 12
349 
350 :(scenario put_index_product_error_with_lookup)
351 % Hide_errors = true;
352 def main [
353   # skip alloc id for 10:&:@:num
354   11:num <- copy 20
355   # skip alloc id for payload
356   21:num <- copy 3  # array length
357   22:num <- copy 94
358   23:num <- copy 95
359   24:num <- copy 96
360   10:&:@:num <- put-index 10:&:@:num/lookup, 1, 34
361 ]
362 +error: main: product of 'put-index' must be first ingredient '10:&:@:num/lookup', but got '10:&:@:num'
363 
364 :(before "End PUT_INDEX Product Checks")
365 reagent/*copy*/ p = inst.products.at(0);
366 if (!canonize_type(p)) break;  // error raised elsewhere
367 reagent/*copy*/ i = inst.ingredients.at(0);
368 if (!canonize_type(i)) break;  // error raised elsewhere
369 if (!types_strictly_match(p, i)) {
370   raise << maybe(get(Recipe, r).name) << "product of 'put-index' must be first ingredient '" << inst.ingredients.at(0).original_string << "', but got '" << inst.products.at(0).original_string << "'\n" << end();
371   break;
372 }
373 
374 :(scenario dilated_reagent_in_static_array)
375 def main [
376   {1: (array (& num) 3)} <- create-array
377   10:&:num <- new num:type
378   {1: (array (& num) 3)} <- put-index {1: (array (& num) 3)}, 0, 10:&:num
379   *10:&:num <- copy 94
380   20:num <- copy *10:&:num
381 ]
382 +run: creating array from 7 locations
383 +mem: storing 94 in location 20
384 
385 :(before "Update PUT_INDEX base in Check")
386 if (!canonize_type(base)) break;
387 :(before "Update PUT_INDEX index in Check")
388 if (!canonize_type(index)) break;
389 :(before "Update PUT_INDEX value in Check")
390 if (!canonize_type(value)) break;
391 
392 :(before "Update PUT_INDEX base in Run")
393 canonize(base);
394 :(before "Update PUT_INDEX index in Run")
395 canonize(index);
396 
397 :(scenario length_indirect)
398 def main [
399   # skip alloc id for 10:&:@:num
400   11:num <- copy 20
401   # skip alloc id for payload
402   21:num <- copy 3  # array length
403   22:num <- copy 94
404   23:num <- copy 95
405   24:num <- copy 96
406   30:num <- length 10:&:array:num/lookup
407 ]
408 +mem: storing 3 in location 30
409 
410 :(before "Update LENGTH array in Check")
411 if (!canonize_type(array)) break;
412 :(before "Update LENGTH array in Run")
413 canonize(array);
414 
415 :(scenario maybe_convert_indirect)
416 def main [
417   # skip alloc id for 10:&:number-or-point
418   11:num <- copy 20
419   # skip alloc id for payload
420   21:number-or-point <- merge 0/number, 94
421   30:num, 31:bool <- maybe-convert 10:&:number-or-point/lookup, i:variant
422 ]
423 +mem: storing 1 in location 31
424 +mem: storing 94 in location 30
425 
426 :(scenario maybe_convert_indirect_2)
427 def main [
428   # skip alloc id for 10:&:number-or-point
429   11:num <- copy 20
430   # skip alloc id for payload
431   21:number-or-point <- merge 0/number, 94
432   # skip alloc id for 30:&:num
433   31:num <- copy 40
434   30:&:num/lookup, 50:bool <- maybe-convert 10:&:number-or-point/lookup, i:variant
435 ]
436 +mem: storing 1 in location 50
437 +mem: storing 94 in location 41
438 
439 :(scenario maybe_convert_indirect_3)
440 def main [
441   # skip alloc id for 10:&:number-or-point
442   11:num <- copy 20
443   # skip alloc id for payload
444   21:number-or-point <- merge 0/number, 94
445   # skip alloc id for 30:&:bool
446   31:num <- copy 40
447   50:num, 30:&:bool/lookup <- maybe-convert 10:&:number-or-point/lookup, i:variant
448 ]
449 +mem: storing 1 in location 41
450 +mem: storing 94 in location 50
451 
452 :(before "Update MAYBE_CONVERT base in Check")
453 if (!canonize_type(base)) break;
454 :(before "Update MAYBE_CONVERT product in Check")
455 if (!canonize_type(product)) break;
456 :(before "Update MAYBE_CONVERT status in Check")
457 if (!canonize_type(status)) break;
458 
459 :(before "Update MAYBE_CONVERT base in Run")
460 canonize(base);
461 :(before "Update MAYBE_CONVERT product in Run")
462 canonize(product);
463 :(before "Update MAYBE_CONVERT status in Run")
464 canonize(status);
465 
466 :(scenario merge_exclusive_container_indirect)
467 def main [
468   # skip alloc id for 10:&:number-or-point
469   11:num <- copy 20
470   10:&:number-or-point/lookup <- merge 0/number, 34
471 ]
472 # skip alloc id
473 +mem: storing 0 in location 21
474 +mem: storing 34 in location 22
475 
476 :(before "Update size_mismatch Check for MERGE(x)
477 canonize(x);
478 
479 //: abbreviation for '/lookup': a prefix '*'
480 
481 :(scenario lookup_abbreviation)
482 def main [
483   # skip alloc id for 10:&:num
484   11:num <- copy 20
485   # skip alloc id for payload
486   21:num <- copy 94
487   30:num <- copy *10:&:num
488 ]
489 +parse: ingredient: {10: ("&" "num"), "lookup": ()}
490 +mem: storing 94 in location 30
491 
492 :(before "End Parsing reagent")
493 {
494   while (starts_with(name, "*")) {
495     name.erase(0, 1);
496     properties.push_back(pair<string, string_tree*>("lookup", NULL));
497   }
498   if (name.empty())
499     raise << "illegal name '" << original_string << "'\n" << end();
500 }
501 
502 //:: helpers for debugging
503 
504 :(before "End Primitive Recipe Declarations")
505 _DUMP,
506 :(before "End Primitive Recipe Numbers")
507 put(Recipe_ordinal, "$dump", _DUMP);
508 :(before "End Primitive Recipe Implementations")
509 case _DUMP: {
510   reagent/*copy*/ after_canonize = current_instruction().ingredients.at(0);
511   canonize(after_canonize);
512   cerr << maybe(current_recipe_name()) << current_instruction().ingredients.at(0).name << ' ' << no_scientific(current_instruction().ingredients.at(0).value) << " => " << no_scientific(after_canonize.value) << " => " << no_scientific(get_or_insert(Memory, after_canonize.value)) << '\n';
513   break;
514 }
515 
516 //: grab an address, and then dump its value at intervals
517 //: useful for tracking down memory corruption (writing to an out-of-bounds address)
518 :(before "End Globals")
519 int Bar = -1;
520 :(before "End Primitive Recipe Declarations")
521 _BAR,
522 :(before "End Primitive Recipe Numbers")
523 put(Recipe_ordinal, "$bar", _BAR);
524 :(before "End Primitive Recipe Implementations")
525 case _BAR: {
526   if (current_instruction().ingredients.empty()) {
527     if (Bar != -1) cerr << Bar << ": " << no_scientific(get_or_insert(Memory, Bar)) << '\n';
528     else cerr << '\n';
529   }
530   else {
531     reagent/*copy*/ tmp = current_instruction().ingredients.at(0);
532     canonize(tmp);
533     Bar = tmp.value;
534   }
535   break;
536 }