3394
This commit is contained in:
parent
0f2781f8a2
commit
897ae8c185
24
043space.cc
24
043space.cc
|
@ -2,6 +2,12 @@
|
|||
//: and all addresses in arguments are implicitly based on the 'default-space'
|
||||
//: (unless they have the /raw property)
|
||||
|
||||
//: A space is just an array of any scalar location.
|
||||
:(before "End Mu Types Initialization")
|
||||
put(Type_abbreviations, "space", new_type_tree("address:array:location"));
|
||||
//: Spaces are often called 'scopes' in other languages.
|
||||
put(Type_abbreviations, "scope", new_type_tree("address:array:location"));
|
||||
|
||||
:(scenario set_default_space)
|
||||
# if default-space is 10, and if an array of 5 locals lies from location 12 to 16 (inclusive),
|
||||
# then local 0 is really location 12, local 1 is really location 13, and so on.
|
||||
|
@ -9,7 +15,7 @@ def main [
|
|||
# pretend address:array:location; in practice we'll use new
|
||||
10:num <- copy 0 # refcount
|
||||
11:num <- copy 5 # length
|
||||
default-space:&:@:location <- copy 10/unsafe
|
||||
default-space:space <- copy 10/unsafe
|
||||
1:num <- copy 23
|
||||
]
|
||||
+mem: storing 23 in location 13
|
||||
|
@ -22,7 +28,7 @@ def main [
|
|||
1000:num <- copy 0 # refcount
|
||||
1001:num <- copy 5 # length
|
||||
# actual start of this recipe
|
||||
default-space:&:@:location <- copy 1000/unsafe
|
||||
default-space:space <- copy 1000/unsafe
|
||||
1:&:num <- copy 2000/unsafe # even local variables always contain raw addresses
|
||||
8:num/raw <- copy *1:&:num
|
||||
]
|
||||
|
@ -95,8 +101,8 @@ bool is_space(const reagent& r) {
|
|||
|
||||
:(scenario get_default_space)
|
||||
def main [
|
||||
default-space:&:@:location <- copy 10/unsafe
|
||||
1:&:@:location/raw <- copy default-space:&:@:location
|
||||
default-space:space <- copy 10/unsafe
|
||||
1:space/raw <- copy default-space:space
|
||||
]
|
||||
+mem: storing 10 in location 1
|
||||
|
||||
|
@ -118,7 +124,7 @@ def main [
|
|||
1000:num <- copy 0 # refcount
|
||||
1001:num <- copy 5 # length
|
||||
# actual start of this recipe
|
||||
default-space:&:@:location <- copy 1000/unsafe
|
||||
default-space:space <- copy 1000/unsafe
|
||||
1:&:point <- copy 2000/unsafe
|
||||
9:num/raw <- get *1:&:point, 1:offset
|
||||
]
|
||||
|
@ -139,7 +145,7 @@ def main [
|
|||
1000:num <- copy 0 # refcount
|
||||
1001:num <- copy 5 # length
|
||||
# actual start of this recipe
|
||||
default-space:&:@:location <- copy 1000/unsafe
|
||||
default-space:space <- copy 1000/unsafe
|
||||
1:&:@:num <- copy 2000/unsafe
|
||||
9:num/raw <- index *1:&:@:num, 1
|
||||
]
|
||||
|
@ -168,7 +174,7 @@ if (s == "number-of-locals") return true;
|
|||
|
||||
:(before "End Rewrite Instruction(curr, recipe result)")
|
||||
// rewrite `new-default-space` to
|
||||
// `default-space:&:@:location <- new location:type, number-of-locals:literal`
|
||||
// `default-space:space <- new location:type, number-of-locals:literal`
|
||||
// where N is Name[recipe][""]
|
||||
if (curr.name == "new-default-space") {
|
||||
rewrite_default_space_instruction(curr);
|
||||
|
@ -199,7 +205,7 @@ def main [
|
|||
def foo [
|
||||
local-scope
|
||||
x:num <- copy 34
|
||||
return default-space:&:@:location
|
||||
return default-space:space
|
||||
]
|
||||
# both calls to foo should have received the same default-space
|
||||
+mem: storing 1 in location 3
|
||||
|
@ -304,7 +310,7 @@ void rewrite_default_space_instruction(instruction& curr) {
|
|||
curr.ingredients.push_back(reagent("number-of-locals:literal"));
|
||||
if (!curr.products.empty())
|
||||
raise << "new-default-space can't take any results\n" << end();
|
||||
curr.products.push_back(reagent("default-space:&:@:location"));
|
||||
curr.products.push_back(reagent("default-space:space"));
|
||||
}
|
||||
|
||||
:(scenario local_scope_frees_up_addresses_inside_containers)
|
||||
|
|
|
@ -14,8 +14,8 @@ def main [
|
|||
20:num <- copy 0 # refcount
|
||||
21:num <- copy 5 # length
|
||||
# actual start of this recipe
|
||||
default-space:&:@:location <- copy 10/unsafe
|
||||
0:&:@:location/names:dummy <- copy 20/unsafe # later layers will explain the /names: property
|
||||
default-space:space <- copy 10/unsafe
|
||||
0:space/names:dummy <- copy 20/unsafe # later layers will explain the /names: property
|
||||
1:num <- copy 32
|
||||
1:num/space:1 <- copy 33
|
||||
]
|
||||
|
|
|
@ -5,20 +5,20 @@
|
|||
|
||||
:(scenario closure)
|
||||
def main [
|
||||
default-space:&:@:location <- new location:type, 30
|
||||
1:&:@:location/names:new-counter <- new-counter
|
||||
2:num/raw <- increment-counter 1:&:@:location/names:new-counter
|
||||
3:num/raw <- increment-counter 1:&:@:location/names:new-counter
|
||||
default-space:space <- new location:type, 30
|
||||
1:space/names:new-counter <- new-counter
|
||||
2:num/raw <- increment-counter 1:space/names:new-counter
|
||||
3:num/raw <- increment-counter 1:space/names:new-counter
|
||||
]
|
||||
def new-counter [
|
||||
default-space:&:@:location <- new location:type, 30
|
||||
default-space:space <- new location:type, 30
|
||||
x:num <- copy 23
|
||||
y:num <- copy 3 # variable that will be incremented
|
||||
return default-space:&:@:location
|
||||
return default-space:space
|
||||
]
|
||||
def increment-counter [
|
||||
default-space:&:@:location <- new location:type, 30
|
||||
0:&:@:location/names:new-counter <- next-ingredient # outer space must be created by 'new-counter' above
|
||||
default-space:space <- new location:type, 30
|
||||
0:space/names:new-counter <- next-ingredient # outer space must be created by 'new-counter' above
|
||||
y:num/space:1 <- add y:num/space:1, 1 # increment
|
||||
y:num <- copy 234 # dummy
|
||||
return y:num/space:1
|
||||
|
@ -153,16 +153,16 @@ def new-scope [
|
|||
new-default-space
|
||||
x:&:num <- new number:type
|
||||
*x:&:num <- copy 34
|
||||
return default-space:&:@:location
|
||||
return default-space:space
|
||||
]
|
||||
def use-scope [
|
||||
local-scope
|
||||
outer:&:@:location <- next-ingredient
|
||||
0:&:@:location/names:new-scope <- copy outer:&:@:location
|
||||
outer:space <- next-ingredient
|
||||
0:space/names:new-scope <- copy outer:space
|
||||
return *x:&:num/space:1
|
||||
]
|
||||
def main [
|
||||
1:&:@:location/raw <- new-scope
|
||||
2:num/raw <- use-scope 1:&:@:location/raw
|
||||
1:space/raw <- new-scope
|
||||
2:num/raw <- use-scope 1:space/raw
|
||||
]
|
||||
+mem: storing 34 in location 2
|
||||
|
|
|
@ -18,8 +18,8 @@ def main [
|
|||
20:num <- copy 0 # refcount
|
||||
21:num <- copy 5 # length
|
||||
# actual start of this recipe
|
||||
global-space:&:@:location <- copy 20/unsafe
|
||||
default-space:&:@:location <- copy 10/unsafe
|
||||
global-space:space <- copy 20/unsafe
|
||||
default-space:space <- copy 10/unsafe
|
||||
1:num <- copy 23
|
||||
1:num/space:global <- copy 24
|
||||
]
|
||||
|
@ -63,7 +63,7 @@ if (x.name == "global-space") {
|
|||
|
||||
:(scenario global_space_with_names)
|
||||
def main [
|
||||
global-space:&:@:location <- new location:type, 10
|
||||
global-space:space <- new location:type, 10
|
||||
x:num <- copy 23
|
||||
1:num/space:global <- copy 24
|
||||
]
|
||||
|
|
|
@ -119,7 +119,7 @@ def foo [ # dummy
|
|||
]
|
||||
def main [
|
||||
local-scope
|
||||
0:&:@:location/names:foo <- copy 0 # specify surrounding space
|
||||
0:space/names:foo <- copy 0 # specify surrounding space
|
||||
x:bool <- copy 1/true
|
||||
x:num/space:1 <- copy 34
|
||||
x/space:1 <- copy 35
|
||||
|
|
|
@ -309,19 +309,19 @@ def bar [
|
|||
//: when checking for immutable ingredients, remember to take space into account
|
||||
:(scenario check_space_of_reagents_in_immutability_checks)
|
||||
def main [
|
||||
a:&:@:location <- new-closure
|
||||
a:space <- new-closure
|
||||
b:&:num <- new number:type
|
||||
run-closure b:&:num, a:&:@:location
|
||||
run-closure b:&:num, a:space
|
||||
]
|
||||
def new-closure [
|
||||
new-default-space
|
||||
x:&:num <- new number:type
|
||||
return default-space
|
||||
]
|
||||
def run-closure x:&:num, s:&:@:location [
|
||||
def run-closure x:&:num, s:space [
|
||||
local-scope
|
||||
load-ingredients
|
||||
0:&:@:location/names:new-closure <- copy s
|
||||
0:space/names:new-closure <- copy s
|
||||
# different space; always mutable
|
||||
*x:&:num/space:1 <- copy 34
|
||||
]
|
||||
|
|
|
@ -281,13 +281,13 @@ if (inst.operation == NEXT_INGREDIENT || inst.operation == NEXT_INGREDIENT_WITHO
|
|||
}
|
||||
|
||||
:(scenario next_ingredient_never_leaks_refcounts)
|
||||
def create-scope n:&:num -> default-space:&:@:location [
|
||||
def create-space n:&:num -> default-space:space [
|
||||
default-space <- new location:type, 2
|
||||
load-ingredients
|
||||
]
|
||||
def use-scope [
|
||||
def use-space [
|
||||
local-scope
|
||||
0:&:@:location/names:create-scope <- next-ingredient
|
||||
0:space/names:create-space <- next-ingredient
|
||||
n:&:num/space:1 <- next-ingredient # should decrement refcount
|
||||
*n/space:1 <- copy 34
|
||||
n2:num <- add *n/space:1, 1
|
||||
|
@ -297,9 +297,9 @@ def main [
|
|||
local-scope
|
||||
n:&:num <- copy 12000/unsafe # pretend allocation with a known address
|
||||
*n <- copy 23
|
||||
scope:&:@:location <- create-scope n
|
||||
space:space <- create-space n
|
||||
n2:&:num <- copy 13000/unsafe
|
||||
n3:num <- use-scope scope, n2
|
||||
n3:num <- use-space space, n2
|
||||
]
|
||||
+run: {n: ("address" "number"), "space": "1"} <- next-ingredient
|
||||
+mem: decrementing refcount of 12000: 2 -> 1
|
||||
|
|
10
counters.mu
10
counters.mu
|
@ -1,24 +1,24 @@
|
|||
# example program: maintain multiple counters with isolated lexical scopes
|
||||
# (spaces)
|
||||
|
||||
def new-counter n:num -> default-space:&:@:location [
|
||||
def new-counter n:num -> default-space:space [
|
||||
default-space <- new location:type, 30
|
||||
load-ingredients
|
||||
]
|
||||
|
||||
def increment-counter outer:&:@:location/names:new-counter, x:num -> n:num/space:1 [
|
||||
def increment-counter outer:space/names:new-counter, x:num -> n:num/space:1 [
|
||||
local-scope
|
||||
load-ingredients
|
||||
0:&:@:location/names:new-counter <- copy outer # setup outer space; it *must* come from 'new-counter'
|
||||
0:space/names:new-counter <- copy outer # setup outer space; it *must* come from 'new-counter'
|
||||
n/space:1 <- add n/space:1, x
|
||||
]
|
||||
|
||||
def main [
|
||||
local-scope
|
||||
# counter A
|
||||
a:&:@:location <- new-counter 34
|
||||
a:space <- new-counter 34
|
||||
# counter B
|
||||
b:&:@:location <- new-counter 23
|
||||
b:space <- new-counter 23
|
||||
# increment both by 2 but in different ways
|
||||
increment-counter a, 1
|
||||
b-value:num <- increment-counter b, 2
|
||||
|
|
Loading…
Reference in New Issue