mu/042new.cc

158 lines
5.5 KiB
C++
Raw Normal View History

2015-03-27 17:51:27 +00:00
//: A simple memory allocator to create space for new variables at runtime.
:(scenarios run)
2015-04-24 17:19:03 +00:00
:(scenario new)
2015-03-27 17:51:27 +00:00
# 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 Reserved_for_tests = 1000;
index_t Memory_allocated_until = Reserved_for_tests;
2015-04-25 05:55:25 +00:00
size_t Initial_memory_per_routine = 100000;
2015-04-25 05:14:07 +00:00
:(before "End Setup")
Memory_allocated_until = Reserved_for_tests;
2015-04-25 05:55:25 +00:00
Initial_memory_per_routine = 100000;
2015-04-13 03:56:45 +00:00
:(before "End routine Fields")
index_t alloc, alloc_max;
2015-04-27 01:17:39 +00:00
:(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;
2015-03-27 17:51:27 +00:00
//:: First handle 'type' operands.
2015-03-27 17:51:27 +00:00
:(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);
2015-03-31 04:22:29 +00:00
//? cout << inst.ingredients[0].to_string() << '\n'; //? 1
2015-03-27 17:51:27 +00:00
assert(isa_literal(inst.ingredients[0]));
2015-03-31 04:22:29 +00:00
if (inst.ingredients[0].properties[0].second[0] == "type") {
2015-04-14 17:34:00 +00:00
inst.ingredients[0].set_value(Type_number[inst.ingredients[0].name]);
2015-03-31 04:22:29 +00:00
}
2015-03-27 17:51:27 +00:00
trace("new") << inst.ingredients[0].name << " -> " << inst.ingredients[0].value;
}
//:: Now implement the primitive recipe.
:(before "End Primitive Recipe Declarations")
NEW,
2015-03-27 17:51:27 +00:00
:(before "End Primitive Recipe Numbers")
Recipe_number["new"] = NEW;
:(before "End Primitive Recipe Implementations")
case NEW: {
2015-04-25 06:41:17 +00:00
// compute the space we need
size_t size = 0;
size_t array_length = 0;
{
2015-05-05 00:15:17 +00:00
vector<type_number> type;
2015-04-25 06:41:17 +00:00
type.push_back(current_instruction().ingredients[0].value);
if (current_instruction().ingredients.size() > 1) {
// array
2015-05-06 00:20:11 +00:00
vector<long long int> capacity = read_memory(current_instruction().ingredients[1]);
2015-04-25 06:41:17 +00:00
array_length = capacity[0];
trace("mem") << "array size is " << array_length;
size = array_length*size_of(type) + /*space for length*/1;
2015-04-25 06:41:17 +00:00
}
else {
// scalar
size = size_of(type);
}
2015-03-29 02:26:40 +00:00
}
2015-04-25 06:41:17 +00:00
// compute the resulting location
2015-04-25 06:43:35 +00:00
// 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 index_t result = Current_routine->alloc;
2015-04-25 06:41:17 +00:00
trace("mem") << "new alloc: " << result;
if (current_instruction().ingredients.size() > 1) {
// initialize array
Memory[result] = array_length;
2015-03-29 02:26:40 +00:00
}
2015-04-25 06:41:17 +00:00
// write result to memory
2015-05-06 00:20:11 +00:00
vector<long long int> tmp;
2015-04-25 06:41:17 +00:00
tmp.push_back(Current_routine->alloc);
write_memory(current_instruction().products[0], tmp);
// bump
Current_routine->alloc += size;
2015-04-25 06:43:35 +00:00
// no support for reclaiming memory
assert(Current_routine->alloc <= Current_routine->alloc_max);
2015-03-27 17:51:27 +00:00
break;
}
2015-03-29 02:26:40 +00:00
2015-04-24 17:19:03 +00:00
:(scenario new_array)
2015-03-29 02:26:40 +00:00
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
]
2015-04-02 17:30:27 +00:00
+run: instruction main/0
+mem: array size is 5
+run: instruction main/1
+run: instruction main/2
+mem: storing 6 in location 3
2015-04-11 06:59:59 +00:00
2015-04-25 06:36:25 +00:00
//: Make sure that each routine gets a different alloc to start.
:(scenario new_concurrent)
recipe f1 [
2015-05-01 22:25:47 +00:00
start-running f2:recipe
2015-04-25 06:36:25 +00:00
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
2015-04-25 06:43:35 +00:00
//: 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.
2015-04-24 17:19:03 +00:00
:(scenario new_string)
2015-04-17 18:00:56 +00:00
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") {
2015-04-17 18:00:56 +00:00
// allocate an array just large enough for it
2015-05-06 00:20:11 +00:00
vector<long long int> result;
2015-04-17 18:00:56 +00:00
result.push_back(Current_routine->alloc);
write_memory(current_instruction().products[0], result);
2015-04-17 18:00:56 +00:00
// 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 (index_t i = 0; i < current_instruction().ingredients[0].name.size(); ++i) {
Memory[Current_routine->alloc++] = current_instruction().ingredients[0].name[i];
2015-04-17 18:00:56 +00:00
}
// mu strings are not null-terminated in memory
break;
}