:(scenarios run)
:(scenario new)
recipe main [
1:address:number/raw <- new number:type
2:address:number/raw <- new number:type
3:boolean/raw <- equal 1:address:number/raw, 2:address:number/raw
]
+mem: storing 0 in location 3
:(before "End Globals")
long long int Reserved_for_tests = 1000;
long long int Memory_allocated_until = Reserved_for_tests;
long long int Initial_memory_per_routine = 100000;
:(before "End Setup")
Memory_allocated_until = Reserved_for_tests;
Initial_memory_per_routine = 100000;
:(before "End routine Fields")
long long int alloc, alloc_max;
:(before "End routine Constructor")
alloc = Memory_allocated_until;
Memory_allocated_until += Initial_memory_per_routine;
alloc_max = Memory_allocated_until;
trace(Primitive_recipe_depth, "new") << "routine allocated memory from " << alloc << " to " << alloc_max;
:(before "End Mu Types Initialization")
Type_ordinal["type"] = 0;
:(after "Per-recipe Transforms")
if (inst.operation == Recipe_ordinal["new"]) {
assert(SIZE(inst.ingredients) >= 1);
if (!is_literal(inst.ingredients.at(0)))
raise << "expected literal, got " << inst.ingredients.at(0).to_string() << '\n' << die();
if (inst.ingredients.at(0).properties.at(0).second.at(0) != "type")
raise << "tried to allocate non-type " << inst.ingredients.at(0).to_string() << " in recipe " << Recipe[r].name << '\n' << die();
if (Type_ordinal.find(inst.ingredients.at(0).name) == Type_ordinal.end())
raise << "unknown type " << inst.ingredients.at(0).name << " in recipe " << Recipe[r].name << '\n' << die();
inst.ingredients.at(0).set_value(Type_ordinal[inst.ingredients.at(0).name]);
trace(Primitive_recipe_depth, "new") << inst.ingredients.at(0).name << " -> " << inst.ingredients.at(0).name;
end_new_transform:;
}
:(before "End Primitive Recipe Declarations")
NEW,
:(before "End Primitive Recipe Numbers")
Recipe_ordinal["new"] = NEW;
:(before "End Primitive Recipe Implementations")
case NEW: {
long long int size = 0;
long long int array_length = 0;
{
vector<type_ordinal> type;
assert(is_literal(current_instruction().ingredients.at(0)));
type.push_back(current_instruction().ingredients.at(0).value);
if (SIZE(current_instruction().ingredients) > 1) {
array_length = ingredients.at(1).at(0);
trace(Primitive_recipe_depth, "mem") << "array size is " << array_length;
size = array_length*size_of(type) + 1;
}
else {
size = size_of(type);
}
}
ensure_space(size);
const long long int result = Current_routine->alloc;
trace(Primitive_recipe_depth, "mem") << "new alloc: " << result;
products.resize(1);
products.at(0).push_back(result);
for (long long int address = result; address < result+size; ++address) {
Memory[address] = 0;
}
if (SIZE(current_instruction().ingredients) > 1) {
Memory[result] = array_length;
}
Current_routine->alloc += size;
assert(Current_routine->alloc <= Current_routine->alloc_max);
break;
}
:(code)
void ensure_space(long long int size) {
assert(size <= Initial_memory_per_routine);
if (Current_routine->alloc + size > Current_routine->alloc_max) {
Current_routine->alloc = Memory_allocated_until;
Memory_allocated_until += Initial_memory_per_routine;
Current_routine->alloc_max = Memory_allocated_until;
trace(Primitive_recipe_depth, "new") << "routine allocated memory from " << Current_routine->alloc << " to " << Current_routine->alloc_max;
}
}
:(scenario new_initializes)
% Memory_allocated_until = 10;
% Memory[Memory_allocated_until] = 1;
recipe main [
1:address:number <- new number:type
2:number <- copy 1:address:number/deref
]
+mem: storing 0 in location 2
:(scenario new_array)
recipe main [
1:address:array:number/raw <- new number:type, 5:literal
2:address:number/raw <- new number:type
3:number/raw <- subtract 2:address:number/raw, 1:address:array:number/raw
]
+run: 1:address:array:number/raw <- new number:type, 5:literal
+mem: array size is 5
+mem: storing 6 in location 3
:(scenario new_empty_array)
recipe main [
1:address:array:number/raw <- new number:type, 0:literal
2:address:number/raw <- new number:type
3:number/raw <- subtract 2:address:number/raw, 1:address:array:number/raw
]
+run: 1:address:array:number/raw <- new number:type, 0:literal
+mem: array size is 0
+mem: storing 1 in location 3
:(scenario new_concurrent)
recipe f1 [
start-running f2:recipe
1:address:number/raw <- new number:type
{
loop-unless 4:number/raw
}
]
recipe f2 [
2:address:number/raw <- new number:type
3:boolean/raw <- equal 1:address:number/raw, 2:address:number/raw
4:number/raw <- copy 1:literal
]
+mem: storing 0 in location 3
:(scenario new_overflow)
% Initial_memory_per_routine = 2;
recipe main [
1:address:number/raw <- new number:type
2:address:point/raw <- new point:type
]
+new: routine allocated memory from 1000 to 1002
+new: routine allocated memory from 1002 to 1004
:(scenario new_reclaim)
recipe main [
1:address:number <- new number:type
abandon 1:address:number
2:address:number <- new number:type
3:boolean <- equal 1:address:number, 2:address:number
]
+mem: storing 1 in location 3
:(before "End Globals")
map<long long int, long long int> Free_list;
:(before "End Setup")
Free_list.clear();
:(before "End Primitive Recipe Declarations")
ABANDON,
:(before "End Primitive Recipe Numbers")
Recipe_ordinal["abandon"] = ABANDON;
:(before "End Primitive Recipe Implementations")
case ABANDON: {
if (!scalar(ingredients.at(0))) {
raise << "abandon's ingredient should be scalar\n";
break;
}
long long int address = ingredients.at(0).at(0);
reagent types = canonize(current_instruction().ingredients.at(0));
if (types.types.at(0) != Type_ordinal["address"]) {
raise << "abandon's ingredient should be an address\n";
break;
}
reagent target_type = deref(types);
abandon(address, size_of(target_type));
break;
}
:(code)
void abandon(long long int address, long long int size) {
for (long long int curr = address; curr < address+size; ++curr)
Memory[curr] = 0;
Memory[address] = Free_list[size];
Free_list[size] = address;
}
:(before "ensure_space(size)" following "case NEW")
if (Free_list[size]) {
long long int result = Free_list[size];
Free_list[size] = Memory[result];
for (long long int curr = result+1; curr < result+size; ++curr)
if (Memory[curr] != 0)
raise << current_recipe_name() << ": memory in free list was not zeroed out: " << curr << '/' << result << "; somebody wrote to us after free!!!\n" << die();
if (SIZE(current_instruction().ingredients) > 1)
Memory[result] = array_length;
else
Memory[result] = 0;
products.resize(1);
products.at(0).push_back(result);
break;
}
:(scenario new_differing_size_no_reclaim)
recipe main [
1:address:number <- new number:type
abandon 1:address:number
2:address:number <- new number:type, 2:literal
3:boolean <- equal 1:address:number, 2:address:number
]
+mem: storing 0 in location 3
:(scenario new_reclaim_array)
recipe main [
1:address:array:number <- new number:type, 2:literal
abandon 1:address:array:number
2:address:array:number <- new number:type, 2:literal
3:boolean <- equal 1:address:array:number, 2:address:array:number
]
+mem: storing 1 in location 3
:(scenario new_string)
recipe main [
1:address:array:character <- new [abc def]
2:character <- index 1:address:array:character/deref, 5:literal
]
+mem: storing 101 in location 2
:(scenario new_string_handles_unicode)
recipe main [
1:address:array:character <- new [a«c]
2:number <- length 1:address:array:character/deref
3:character <- index 1:address:array:character/deref, 1:literal
]
+mem: storing 3 in location 2
+mem: storing 171 in location 3
:(before "End NEW Transform Special-cases")
if (!inst.ingredients.empty()
&& !inst.ingredients.at(0).properties.empty()
&& !inst.ingredients.at(0).properties.at(0).second.empty()
&& inst.ingredients.at(0).properties.at(0).second.at(0) == "literal-string") {
inst.ingredients.at(0).initialized = true;
goto end_new_transform;
}
:(after "case NEW" following "Primitive Recipe Implementations")
if (is_literal(current_instruction().ingredients.at(0))
&& current_instruction().ingredients.at(0).properties.at(0).second.at(0) == "literal-string") {
products.resize(1);
products.at(0).push_back(new_string(current_instruction().ingredients.at(0).name));
break;
}
:(code)
long long int new_string(const string& contents) {
long long int string_length = unicode_length(contents);
ensure_space(string_length+1);
long long int result = Current_routine->alloc;
Memory[Current_routine->alloc++] = string_length;
long long int curr = 0;
const char* raw_contents = contents.c_str();
for (long long int i = 0; i < string_length; ++i) {
uint32_t curr_character;
assert(curr < SIZE(contents));
tb_utf8_char_to_unicode(&curr_character, &raw_contents[curr]);
Memory[Current_routine->alloc] = curr_character;
curr += tb_utf8_char_length(raw_contents[curr]);
++Current_routine->alloc;
}
return result;
}
:(scenario new_string_overflow)
% Initial_memory_per_routine = 2;
recipe main [
1:address:number/raw <- new number:type
2:address:array:character/raw <- new [a]
]
+new: routine allocated memory from 1000 to 1002
+new: routine allocated memory from 1002 to 1004
:(code)
long long int unicode_length(const string& s) {
const char* in = s.c_str();
long long int result = 0;
long long int curr = 0;
while (curr < SIZE(s)) {
++result;
curr += tb_utf8_char_length(in[curr]);
}
return result;
}