blob: e98800a675d42590f88a348ce18510804e3d3528 (
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
//: A simple memory allocator to create space for new variables at runtime.
:(scenarios run)
:(scenario new)
# call new two times with identical arguments; you should get back different results
recipe main [
1:address:integer/raw <- new integer:type
2:address:integer/raw <- new integer:type
3:boolean/raw <- equal 1:address:integer/raw, 2:address:integer/raw
]
+mem: storing 0 in location 3
:(before "End Globals")
size_t Memory_allocated_until = 1000;
size_t Initial_memory_per_routine = 100000;
:(before "End Setup")
Memory_allocated_until = 1000;
Initial_memory_per_routine = 100000;
:(before "End routine Fields")
size_t alloc, alloc_max;
:(before "End routine Constructor")
alloc = Memory_allocated_until;
Memory_allocated_until += Initial_memory_per_routine;
alloc_max = Memory_allocated_until;
trace("new") << "routine allocated memory from " << alloc << " to " << alloc_max;
//:: First handle 'type' operands.
:(before "End Mu Types Initialization")
Type_number["type"] = 0;
:(after "Per-recipe Transforms")
// replace type names with type_numbers
if (inst.operation == Recipe_number["new"]) {
// first arg must be of type 'type'
assert(inst.ingredients.size() >= 1);
//? cout << inst.ingredients[0].to_string() << '\n'; //? 1
assert(isa_literal(inst.ingredients[0]));
if (inst.ingredients[0].properties[0].second[0] == "type") {
inst.ingredients[0].set_value(Type_number[inst.ingredients[0].name]);
}
trace("new") << inst.ingredients[0].name << " -> " << inst.ingredients[0].value;
}
//:: Now implement the primitive recipe.
:(before "End Primitive Recipe Declarations")
NEW,
:(before "End Primitive Recipe Numbers")
Recipe_number["new"] = NEW;
:(before "End Primitive Recipe Implementations")
case NEW: {
// compute the space we need
size_t size = 0;
size_t array_length = 0;
{
vector<int> type;
type.push_back(current_instruction().ingredients[0].value);
if (current_instruction().ingredients.size() > 1) {
// array
vector<int> capacity = read_memory(current_instruction().ingredients[1]);
array_length = capacity[0];
trace("mem") << "array size is " << array_length;
size = array_length*size_of(type);
}
else {
// scalar
size = size_of(type);
}
}
// compute the resulting location
// really crappy at the moment
assert(size <= Initial_memory_per_routine);
if (Current_routine->alloc + size >= Current_routine->alloc_max) {
// waste the remaining space and create a new chunk
Current_routine->alloc = Memory_allocated_until;
Memory_allocated_until += Initial_memory_per_routine;
Current_routine->alloc_max = Memory_allocated_until;
trace("new") << "routine allocated memory from " << Current_routine->alloc << " to " << Current_routine->alloc_max;
}
const size_t result = Current_routine->alloc;
trace("mem") << "new alloc: " << result;
if (current_instruction().ingredients.size() > 1) {
// initialize array
Memory[result] = array_length;
}
// write result to memory
vector<int> tmp;
tmp.push_back(Current_routine->alloc);
write_memory(current_instruction().products[0], tmp);
// bump
Current_routine->alloc += size;
// no support for reclaiming memory
assert(Current_routine->alloc <= Current_routine->alloc_max);
break;
}
:(scenario new_array)
recipe main [
1:address:array:integer/raw <- new integer:type, 5:literal
2:address:integer/raw <- new integer:type
3:integer/raw <- subtract 2:address:integer/raw, 1:address:array:integer/raw
]
+run: instruction main/0
+mem: array size is 5
+run: instruction main/1
+run: instruction main/2
+mem: storing 5 in location 3
//: Make sure that each routine gets a different alloc to start.
:(scenario new_concurrent)
recipe f1 [
run f2:recipe
1:address:integer/raw <- new integer:type
]
recipe f2 [
2:address:integer/raw <- new integer:type
# hack: assumes scheduler implementation
3:boolean/raw <- equal 1:address:integer/raw, 2:address:integer/raw
]
+mem: storing 0 in location 3
//: If a routine runs out of its initial allocation, it should allocate more.
:(scenario new_overflow)
% Initial_memory_per_routine = 2;
recipe main [
1:address:integer/raw <- new integer:type
2:address:point/raw <- new point:type # not enough room in initial page
]
+new: routine allocated memory from 1000 to 1002
+new: routine allocated memory from 1002 to 1004
//:: Next, extend 'new' to handle a string literal argument.
:(scenario new_string)
recipe main [
1:address:array:character <- new [abc def]
2:character <- index 1:address:array:character/deref, 5:literal
]
# integer code for 'e'
+mem: storing 101 in location 2
:(after "case NEW" following "Primitive Recipe Implementations")
if (current_instruction().ingredients[0].properties[0].second[0] == "literal-string") {
// allocate an array just large enough for it
vector<int> result;
result.push_back(Current_routine->alloc);
write_memory(current_instruction().products[0], result);
// assume that all characters fit in a single location
//? cout << "new string literal: " << current_instruction().ingredients[0].name << '\n'; //? 1
Memory[Current_routine->alloc++] = current_instruction().ingredients[0].name.size();
for (size_t i = 0; i < current_instruction().ingredients[0].name.size(); ++i) {
Memory[Current_routine->alloc++] = current_instruction().ingredients[0].name[i];
}
// mu strings are not null-terminated in memory
break;
}
|