about summary refs log tree commit diff stats
path: root/053new-segment.subx
blob: 83c890eae745b47f0dc2e94b3b53e03b0add8cb7 (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
# Create a new segment (pool of memory for allocating chunks from) in the form
# of an *allocation descriptor* that can be passed to the memory allocator
# (defined in a later layer).
#
# Currently an allocation descriptor consists of just the bounds of the pool of
# available memory:
#
#   curr : address
#   end : address
#
# This isn't enough information to reclaim individual allocations. We can't
# support arbitrary reclamation yet.

== code
#   instruction                     effective address                                                   register    displacement    immediate
# . op          subop               mod             rm32          base        index         scale       r32
# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes

Entry:   # manual test
    # var ad/ECX : (address allocation-descriptor) = {0, 0}
    68/push  0/imm32/limit
    68/push  0/imm32/curr
    89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
    # new-segment(0x1000, ad)
    # . . push args
    51/push-ECX
    68/push  0x1000/imm32
    # . . call
    e8/call  new-segment/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # EAX = ad->curr
    8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy *ECX to EAX
    # write to *EAX to check that we have access to the newly-allocated segment
    c7          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0x34/imm32        # copy to *EAX
    # syscall(exit, EAX)
    89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EBX
    b8/copy-to-EAX  1/imm32/exit
    cd/syscall  0x80/imm8

new-segment:  # len : int, ad : (address allocation-descriptor)
    # . prolog
    55/push-EBP
    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
    # . save registers
    50/push-EAX
    53/push-EBX
    # copy len to _mmap-new-segment->len
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
    89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   _mmap-new-segment:len/disp32      # copy EAX to *_mmap-new-segment:len
    # mmap(_mmap-new-segment)
    bb/copy-to-EBX  _mmap-new-segment/imm32
    b8/copy-to-EAX  0x5a/imm32/mmap
    cd/syscall  0x80/imm8
    # copy {EAX, EAX+len} to *ad
    # . EBX = ad
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
    # . *EBX = EAX
    89/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to *EBX
    # . *(EBX+4) = EAX+len
    03/add                          1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # add *(EBP+8) to EAX
    89/copy                         1/mod/*+disp8   3/rm32/EBX    .           .             .           0/r32/EAX   4/disp8         .                 # copy EAX to *(EBX+4)
$new-segment:end:
    # . restore registers
    5b/pop-to-EBX
    58/pop-to-EAX
    # . epilog
    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
    5d/pop-to-EBP
    c3/return

== data

# various constants used here were found in the Linux sources (search for file mman-common.h)
_mmap-new-segment:  # type mmap_arg_struct
    # addr
    0/imm32
_mmap-new-segment:len:
    # len
    0/imm32
    # protection flags
    3/imm32  # PROT_READ | PROT_WRITE
    # sharing flags
    0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
    # fd
    -1/imm32  # since MAP_ANONYMOUS is specified
    # offset
    0/imm32  # since MAP_ANONYMOUS is specified

# . . vim:nowrap:textwidth=0
ont-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: A big convenience high-level languages provide is the ability to name memory
//: locations. In Mu, a transform called 'transform_names' provides this
//: convenience.

:(scenario transform_names)
def main [
  x:num <- copy 0
]
+name: assign x 2
+mem: storing 0 in location 2

:(scenarios transform)
:(scenario transform_names_fails_on_use_before_define)
% Hide_errors = true;
def main [
  x:num <- copy y:num
]
+error: main: tried to read ingredient 'y' in 'x:num <- copy y:num' but it hasn't been written to yet
# todo: detect conditional defines

:(after "End Type Modifying Transforms")
Transform.push_back(transform_names);  // idempotent

:(before "End Globals")
map<recipe_ordinal, map<string, int> > Name;

//: the Name map is a global, so save it before tests and reset it for every
//: test, just to be safe.
:(before "End Globals")
map<recipe_ordinal, map<string, int> > Name_snapshot;
:(before "End save_snapshots")
Name_snapshot = Name;
:(before "End restore_snapshots")
Name = Name_snapshot;

:(code)
void transform_names(const recipe_ordinal r) {
  recipe& caller = get(Recipe, r);
  trace(9991, "transform") << "--- transform names for recipe " << caller.name << end();
  bool names_used = false;
  bool numeric_locations_used = false;
  map<string, int>& names = Name[r];
  // record the indices 'used' so far in the map
  int& curr_idx = names[""];
  // reserve indices 0 and 1 for the chaining slot in a later layer.
  // transform_names may get called multiple times in later layers, so
  // curr_idx may already be set.
  if (curr_idx < 2) curr_idx = 2;
  for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    instruction& inst = caller.steps.at(i);
    // End transform_names(inst) Special-cases
    // map names to addresses
    for (int in = 0;  in < SIZE(inst.ingredients);  ++in) {
      reagent& ingredient = inst.ingredients.at(in);
      if (is_disqualified(ingredient, inst, caller.name)) continue;
      if (is_numeric_location(ingredient)) numeric_locations_used = true;
      if (is_named_location(ingredient)) names_used = true;
      if (is_integer(ingredient.name)) continue;
      if (!already_transformed(ingredient, names)) {
        raise << maybe(caller.name) << "tried to read ingredient '" << ingredient.name << "' in '" << to_original_string(inst) << "' but it hasn't been written to yet\n" << end();
        // use-before-set Error
        return;
      }
      int v = lookup_name(ingredient, r);
      if (v >= 0) {
        ingredient.set_value(v);
        // Done Placing Ingredient(ingredient, inst, caller)
      }
      else {
        raise << maybe(caller.name) << "can't find a place to store '" << ingredient.name << "'\n" << end();
        return;
      }
    }
    for (int out = 0;  out < SIZE(inst.products);  ++out) {
      reagent& product = inst.products.at(out);
      if (is_disqualified(product, inst, caller.name)) continue;
      if (is_numeric_location(product)) numeric_locations_used = true;
      if (is_named_location(product)) names_used = true;
      if (is_integer(product.name)) continue;
      if (names.find(product.name) == names.end()) {
        trace(9993, "name") << "assign " << product.name << " " << curr_idx << end();
        names[product.name] = curr_idx;
        curr_idx += size_of(product);
      }
      int v = lookup_name(product, r);
      if (v >= 0) {
        product.set_value(v);
        // Done Placing Product(product, inst, caller)
      }
      else {
        raise << maybe(caller.name) << "can't find a place to store '" << product.name << "'\n" << end();
        return;
      }
    }
  }
  if (names_used && numeric_locations_used)
    raise << maybe(caller.name) << "mixing variable names and numeric addresses\n" << end();
}

bool is_disqualified(/*mutable*/ reagent& x, const instruction& inst, const string& recipe_name) {
  if (!x.type) {
    raise << maybe(recipe_name) << "missing type for '" << x.original_string << "' in '" << to_original_string(inst) << "'\n" << end();
    // missing-type Error 1
    return true;
  }
  if (is_raw(x)) return true;
  if (is_literal(x)) return true;
  // End is_disqualified Special-cases
  if (x.initialized) return true;
  return false;
}

bool already_transformed(const reagent& r, const map<string, int>& names) {
  return contains_key(names, r.name);
}

int lookup_name(const reagent& r, const recipe_ordinal default_recipe) {
  return Name[default_recipe][r.name];
}

type_ordinal skip_addresses(type_tree* type) {
  while (type && is_compound_type_starting_with(type, "address"))
    type = type->right;
  if (!type) return -1;  // error handled elsewhere
  if (type->atom) return type->value;
  const type_tree* base_type = type;
  // Update base_type in skip_addresses
  if (base_type->atom)
    return base_type->value;
  assert(base_type->left->atom);
  return base_type->left->value;
}

bool is_compound_type_starting_with(const type_tree* type, const string& expected_name) {
  if (!type) return false;
  if (type->atom) return false;
  if (!type->left->atom) return false;
  return type->left->value == get(Type_ordinal, expected_name);
}

int find_element_offset(const type_ordinal t, const string& name, const string& recipe_name) {
  const type_info& container = get(Type, t);
  for (int i = 0;  i < SIZE(container.elements);  ++i)
    if (container.elements.at(i).name == name) return i;
  raise << maybe(recipe_name) << "unknown element '" << name << "' in container '" << get(Type, t).name << "'\n" << end();
  return -1;
}
int find_element_location(int base_address, const string& name, const type_tree* type, const string& recipe_name) {
  int offset = find_element_offset(get_base_type(type)->value, name, recipe_name);
  if (offset == -1) return offset;
  int result = base_address;
  for (int i = 0; i < offset; ++i)
    result += size_of(element_type(type, i));
  return result;
}

bool is_numeric_location(const reagent& x) {
  if (is_literal(x)) return false;
  if (is_raw(x)) return false;
  if (x.name == "0") return false;  // used for chaining lexical scopes
  return is_integer(x.name);
}

bool is_named_location(const reagent& x) {
  if (is_literal(x)) return false;
  if (is_raw(x)) return false;
  if (is_special_name(x.name)) return false;
  return !is_integer(x.name);
}

// all names here should either be disqualified or also in bind_special_scenario_names
bool is_special_name(const string& s) {
  if (s == "_") return true;
  if (s == "0") return true;
  // End is_special_name Special-cases
  return false;
}

bool is_raw(const reagent& r) {
  return has_property(r, "raw");
}

:(scenario transform_names_supports_containers)
def main [
  x:point <- merge 34, 35
  y:num <- copy 3
]
+name: assign x 2
# skip location 2 because x occupies two locations
+name: assign y 4

:(scenario transform_names_supports_static_arrays)
def main [
  x:@:num:3 <- create-array
  y:num <- copy 3
]
+name: assign x 2
# skip locations 2, 3, 4 because x occupies four locations
+name: assign y 6

:(scenario transform_names_passes_dummy)
# _ is just a dummy result that never gets consumed
def main [
  _, x:num <- copy 0, 1
]
+name: assign x 2
-name: assign _ 2

//: an escape hatch to suppress name conversion that we'll use later
:(scenarios run)
:(scenario transform_names_passes_raw)
% Hide_errors = true;
def main [
  x:num/raw <- copy 0
]
-name: assign x 2
+error: can't write to location 0 in 'x:num/raw <- copy 0'

:(scenarios transform)
:(scenario transform_names_fails_when_mixing_names_and_numeric_locations)
% Hide_errors = true;
def main [
  x:num <- copy 1:num
]
+error: main: mixing variable names and numeric addresses

:(scenario transform_names_fails_when_mixing_names_and_numeric_locations_2)
% Hide_errors = true;
def main [
  x:num <- copy 1
  1:num <- copy x:num
]
+error: main: mixing variable names and numeric addresses

:(scenario transform_names_does_not_fail_when_mixing_names_and_raw_locations)
def main [
  x:num <- copy 1:num/raw
]
-error: main: mixing variable names and numeric addresses
$error: 0

:(scenario transform_names_does_not_fail_when_mixing_names_and_literals)
def main [
  x:num <- copy 1
]
-error: main: mixing variable names and numeric addresses
$error: 0

//:: Support element names for containers in 'get' and 'get-location' and 'put'.
//: (get-location is implemented later)

:(before "End update GET offset_value in Check")
else {
  if (!offset.initialized) {
    raise << maybe(get(Recipe, r).name) << "uninitialized offset '" << offset.name << "' in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  offset_value = offset.value;
}

:(scenario transform_names_transforms_container_elements)
def main [
  p:&:point <- copy null
  a:num <- get *p:&:point, y:offset
  b:num <- get *p:&:point, x:offset
]
+name: element y of type point is at offset 1
+name: element x of type point is at offset 0

:(before "End transform_names(inst) Special-cases")
// replace element names of containers with offsets
if (inst.name == "get" || inst.name == "get-location" || inst.name == "put") {
  //: avoid raising any errors here; later layers will support overloading new
  //: instructions with the same names (static dispatch), which could lead to
  //: spurious errors
  if (SIZE(inst.ingredients) < 2)
    break;  // error raised elsewhere
  if (!is_literal(inst.ingredients.at(1)))
    break;  // error raised elsewhere
  if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) {
    // since first non-address in base type must be a container, we don't have to canonize
    type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type);
    if (contains_key(Type, base_type)) {  // otherwise we'll raise an error elsewhere
      inst.ingredients.at(1).set_value(find_element_offset(base_type, inst.ingredients.at(1).name, get(Recipe, r).name));
      trace(9993, "name") << "element " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " is at offset " << no_scientific(inst.ingredients.at(1).value) << end();
    }
  }
}

:(scenario missing_type_in_get)
% Hide_errors = true;
def main [
  get a, x:offset
]
+error: main: missing type for 'a' in 'get a, x:offset'

:(scenario transform_names_handles_containers)
def main [
  a:point <- merge 0, 0
  b:num <- copy 0
]
+name: assign a 2
+name: assign b 4

//:: Support variant names for exclusive containers in 'maybe-convert'.

:(scenarios run)
:(scenario transform_names_handles_exclusive_containers)
def main [
  12:num <- copy 1
  13:num <- copy 35
  14:num <- copy 36
  20:point, 22:bool <- maybe-convert 12:number-or-point/unsafe, p:variant
]
+name: variant p of type number-or-point has tag 1
+mem: storing 1 in location 22
+mem: storing 35 in location 20
+mem: storing 36 in location 21

:(before "End transform_names(inst) Special-cases")
// convert variant names of exclusive containers
if (inst.name == "maybe-convert") {
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(get(Recipe, r).name) << "exactly 2 ingredients expected in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  assert(is_literal(inst.ingredients.at(1)));
  if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) {
    // since first non-address in base type must be an exclusive container, we don't have to canonize
    type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type);
    if (contains_key(Type, base_type)) {  // otherwise we'll raise an error elsewhere
      inst.ingredients.at(1).set_value(find_element_offset(base_type, inst.ingredients.at(1).name, get(Recipe, r).name));
      trace(9993, "name") << "variant " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " has tag " << no_scientific(inst.ingredients.at(1).value) << end();
    }
  }
}

:(scenario missing_type_in_maybe_convert)
% Hide_errors = true;
def main [
  maybe-convert a, x:variant
]
+error: main: missing type for 'a' in 'maybe-convert a, x:variant'