https://github.com/akkartik/mu/blob/main/linux/400.mu
  1 # The 4xx series is for primitives implemented in Mu.
  2 
  3 # Signatures for major SubX functions defined so far.
  4 
  5 # autogenerated
  6 sig run-tests
  7 
  8 # init.linux
  9 # TODO: make this OS-specific
 10 # TODO: include result type at least, even if register args are too much
 11 sig syscall_exit  # status/ebx: int
 12 sig syscall_read  # fd/ebx: int, buf/ecx: addr, size/edx: int -> nbytes-or-error/eax: int
 13 sig syscall_write  # fd/ebx: int, buf/ecx: addr, size/edx: int -> nbytes-or-error/eax: int
 14 sig syscall_open  # filename/ebx: (addr kernel-string), flags/ecx: int, dummy=0x180/edx -> fd-or-error/eax: int
 15 sig syscall_close  # fd/ebx: int -> status/eax
 16 sig syscall_creat  # filename/ebx: (addr kernel-string) -> fd-or-error/eax: int
 17 sig syscall_unlink  # filename/ebx: (addr kernel-string) -> status/eax: int
 18 sig syscall_rename  # source/ebx: (addr kernel-string), dest/ecx: (addr kernel-string) -> status/eax: int
 19 sig syscall_mmap  # arg/ebx: (addr mmap_arg_struct) -> status/eax: int
 20 sig syscall_ioctl  # fd/ebx: int, cmd/ecx: int, arg/edx: (addr _)
 21 sig syscall_nanosleep  # req/ebx: (addr timespec)
 22 sig syscall_clock_gettime  # clock/ebx: int, out/ecx: (addr timespec)
 23 
 24 # Generated using:
 25 #   grep -h '^[a-z]' [0-9]*.subx |grep -v '^test-'
 26 # Functions we don't want to make accessible from Mu are commented out.
 27 # Many functions here may not be usable yet because of missing features
 28 # (global variable support, etc.)
 29 sig check-ints-equal a: int, b: int, msg: (addr array byte)
 30 sig kernel-string-equal? s: (addr kernel-string), benchmark: (addr array byte) -> _/eax: boolean
 31 sig new-segment len: int, ad: (addr allocation-descriptor)
 32 sig string-equal? s: (addr array byte), benchmark: (addr array byte) -> _/eax: boolean
 33 sig string-starts-with? s: (addr array byte), benchmark: (addr array byte) -> _/eax: boolean
 34 sig check-strings-equal s: (addr array byte), expected: (addr array byte), msg: (addr array byte)
 35 sig clear-stream f: (addr stream _)
 36 sig rewind-stream f: (addr stream _)
 37 sig initialize-trace-stream n: int
 38 sig trace line: (addr array byte)
 39 sig check-trace-contains line: (addr string), msg: (addr string)
 40 sig check-trace-scans-to line: (addr string), msg: (addr string)
 41 sig trace-scan line: (addr array byte) -> _/eax: boolean
 42 sig next-line-matches? t: (addr stream byte), line: (addr array byte) -> _/eax: boolean
 43 sig skip-next-line t: (addr stream byte)
 44 sig clear-trace-stream
 45 sig write f: (addr stream byte), s: (addr array byte)  # writing to file descriptor not supported; use buffered-file
 46 sig stream-data-equal? f: (addr stream byte), s: (addr array byte) -> _/eax: boolean
 47 sig check-stream-equal f: (addr stream byte), s: (addr array byte), msg: (addr array byte)
 48 sig next-stream-line-equal? f: (addr stream byte), s: (addr array byte) -> _/eax: boolean
 49 sig check-next-stream-line-equal f: (addr stream byte), s: (addr array byte), msg: (addr array byte)
//: Writing to a literal (not computed) address of 0 in a recipe chains two
//: spaces together. When a variable has a property of /space:1, it looks up
//: the variable in the chained/surrounding space. /space:2 looks up the
//: surrounding space of the surrounding space, etc.
//:
//: todo: warn on default-space abuse. default-space for one recipe should
//: never come from another, otherwise memory will be corrupted.

void test_closure() {
  run(
      "def main [\n"
      "  default-space:space <- new location:type, 30\n"
      "  2:space/names:new-counter <- new-counter\n"
      "  10:num/raw <- increment-counter 2:space/names:new-counter\n"
      "  11:num/raw <- increment-counter 2:space/names:new-counter\n"
      "]\n"
      "def new-counter [\n"
      "  default-space:space <- new location:type, 30\n"
      "  x:num <- copy 23\n"
      "  y:num <- copy 13\n"  // variable that will be incremented
      "  return default-space:space\n"
      "]\n"
      "def increment-counter [\n"
      "  default-space:space <- new location:type, 30\n"
      "  0:space/names:new-counter <- next-ingredient\n"  // outer space must be created by 'new-counter' above
      "  y:num/space:1 <- add y:num/space:1, 1\n"  // increment
      "  y:num <- copy 234\n"  // dummy
      "  return y:num/space:1\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: lexically surrounding space for recipe increment-counter comes from new-counter\n"
      "mem: storing 15 in location 11\n"
  );
}

//: To make this work, compute the recipe that provides names for the
//: surrounding space of each recipe.

:(before "End Globals")
map<recipe_ordinal, recipe_ordinal> Surrounding_space;  // internal to transform; no need to snapshot
:(before "End Reset")
Surrounding_space.clear();

:(before "Begin Type Modifying Transforms")
Transform.push_back(collect_surrounding_spaces);  // idempotent

:(code)
void collect_surrounding_spaces(const recipe_ordinal r) {
  trace(101, "transform") << "--- collect surrounding spaces for recipe " << get(Recipe, r).name << end();
  for (int i = 0;  i < SIZE(get(Recipe, r).steps);  ++i) {
    const instruction& inst = get(Recipe, r).steps.at(i);
    if (inst.is_label) continue;
    for (int j = 0;  j < SIZE(inst.products);  ++j) {
      if (is_literal(inst.products.at(j))) continue;
      if (inst.products.at(j).name != "0") continue;
      if (!is_mu_space(inst.products.at(j))) {
        raise << "slot 0 should always have type address:array:location, but is '" << to_string(inst.products.at(j)) << "'\n" << end();
        continue;
      }
      string_tree* s = property(inst.products.at(j), "names");
      if (!s) {
        raise << "slot 0 requires a /names property in recipe '" << get(Recipe, r).name << "'\n" << end();
        continue;
      }
      if (!s->atom) raise << "slot 0 should have a single value in /names, but got '" << to_string(inst.products.at(j)) << "'\n" << end();
      const string& surrounding_recipe_name = s->value;
      if (surrounding_recipe_name.empty()) {
        raise << "slot 0 doesn't initialize its /names property in recipe '" << get(Recipe, r).name << "'\n" << end();
        continue;
      }
      if (contains_key(Surrounding_space, r)
          && get(Surrounding_space, r) != get(Recipe_ordinal, surrounding_recipe_name)) {
        raise << "recipe '" << get(Recipe, r).name << "' can have only one 'surrounding' recipe but has '" << get(Recipe, get(Surrounding_space, r)).name << "' and '" << surrounding_recipe_name << "'\n" << end();
        continue;
      }
      trace(103, "name") << "lexically surrounding space for recipe " << get(Recipe, r).name << " comes from " << surrounding_recipe_name << end();
      if (!contains_key(Recipe_ordinal, surrounding_recipe_name)) {
        raise << "can't find recipe providing surrounding space for