983 - arc 'integer-array' => c++ 'array:integer'

This commit is contained in:
Kartik K. Agaram 2015-03-26 21:47:29 -07:00
parent f608504a44
commit 7e9c69251b
18 changed files with 86 additions and 88 deletions

View File

@ -1,6 +1,6 @@
parse/0: instruction: 1
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]}
parse/0: product: {name: "a", value: 0, type: 4, properties: [a: point]}
parse/0: product: {name: "a", value: 0, type: 5, properties: [a: point]}
parse/0: instruction: 1
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]}
parse/0: product: {name: "b", value: 0, type: 1, properties: [b: integer]}

View File

@ -1,9 +1,9 @@
parse/0: instruction: 18
parse/0: ingredient: {name: "0", value: 0, type: 4, properties: [0: point]}
parse/0: ingredient: {name: "0", value: 0, type: 5, properties: [0: point]}
parse/0: ingredient: {name: "y", value: 0, type: 0, properties: [y: offset]}
parse/0: product: {name: "a", value: 0, type: 1, properties: [a: integer]}
parse/0: instruction: 18
parse/0: ingredient: {name: "0", value: 0, type: 4, properties: [0: point]}
parse/0: ingredient: {name: "0", value: 0, type: 5, properties: [0: point]}
parse/0: ingredient: {name: "x", value: 0, type: 0, properties: [x: offset]}
parse/0: product: {name: "b", value: 0, type: 1, properties: [b: integer]}
name/0: field y of type point is at offset 1

View File

@ -11,8 +11,8 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "16", value: 0, type: 0, properties: [16: literal]}
parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]}
parse/0: instruction: 1
parse/0: ingredient: {name: "1", value: 0, type: 6, properties: [1: integer-array]}
parse/0: product: {name: "5", value: 0, type: 6, properties: [5: integer-array]}
parse/0: ingredient: {name: "1", value: 0, type: 4-1, properties: [1: array:integer]}
parse/0: product: {name: "5", value: 0, type: 4-1, properties: [5: array:integer]}
after-brace/0: recipe main
after-brace/0: copy ...
after-brace/0: copy ...

View File

@ -8,8 +8,8 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "36", value: 0, type: 0, properties: [36: literal]}
parse/0: product: {name: "14", value: 0, type: 1, properties: [14: integer]}
parse/0: instruction: 1
parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point-integer]}
parse/0: product: {name: "15", value: 0, type: 5, properties: [15: point-integer]}
parse/0: ingredient: {name: "12", value: 0, type: 6, properties: [12: point-integer]}
parse/0: product: {name: "15", value: 0, type: 6, properties: [15: point-integer]}
after-brace/0: recipe main
after-brace/0: copy ...
after-brace/0: copy ...

View File

@ -5,8 +5,8 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]}
parse/0: product: {name: "2", value: 0, type: 1, properties: [2: integer]}
parse/0: instruction: 1
parse/0: ingredient: {name: "1", value: 0, type: 4, properties: [1: point]}
parse/0: product: {name: "3", value: 0, type: 4, properties: [3: point]}
parse/0: ingredient: {name: "1", value: 0, type: 5, properties: [1: point]}
parse/0: product: {name: "3", value: 0, type: 5, properties: [3: point]}
after-brace/0: recipe main
after-brace/0: copy ...
after-brace/0: copy ...

View File

@ -5,7 +5,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]}
parse/0: product: {name: "13", value: 0, type: 1, properties: [13: integer]}
parse/0: instruction: 18
parse/0: ingredient: {name: "12", value: 0, type: 4, properties: [12: point]}
parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point]}
parse/0: ingredient: {name: "1", value: 0, type: 0, properties: [1: offset]}
parse/0: product: {name: "15", value: 0, type: 1, properties: [15: integer]}
after-brace/0: recipe main

View File

@ -5,7 +5,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]}
parse/0: product: {name: "13", value: 0, type: 1, properties: [13: integer]}
parse/0: instruction: 19
parse/0: ingredient: {name: "12", value: 0, type: 4, properties: [12: point]}
parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point]}
parse/0: ingredient: {name: "1", value: 0, type: 0, properties: [1: offset]}
parse/0: product: {name: "15", value: 0, type: 2-1, properties: [15: address:integer]}
after-brace/0: recipe main

View File

@ -8,7 +8,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]}
parse/0: product: {name: "3", value: 0, type: 1, properties: [3: integer]}
parse/0: instruction: 19
parse/0: ingredient: {name: "1", value: 0, type: 2-4, properties: [1: address:point, deref: ]}
parse/0: ingredient: {name: "1", value: 0, type: 2-5, properties: [1: address:point, deref: ]}
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: offset]}
parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]}
after-brace/0: recipe main

View File

@ -8,7 +8,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "36", value: 0, type: 0, properties: [36: literal]}
parse/0: product: {name: "14", value: 0, type: 1, properties: [14: integer]}
parse/0: instruction: 18
parse/0: ingredient: {name: "12", value: 0, type: 5, properties: [12: point-integer]}
parse/0: ingredient: {name: "12", value: 0, type: 6, properties: [12: point-integer]}
parse/0: ingredient: {name: "1", value: 0, type: 0, properties: [1: offset]}
parse/0: product: {name: "15", value: 0, type: 1, properties: [15: integer]}
after-brace/0: recipe main

View File

@ -8,7 +8,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]}
parse/0: product: {name: "3", value: 0, type: 1, properties: [3: integer]}
parse/0: instruction: 18
parse/0: ingredient: {name: "1", value: 0, type: 2-4, properties: [1: address:point, deref: ]}
parse/0: ingredient: {name: "1", value: 0, type: 2-5, properties: [1: address:point, deref: ]}
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: offset]}
parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]}
after-brace/0: recipe main

View File

@ -8,7 +8,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "35", value: 0, type: 0, properties: [35: literal]}
parse/0: product: {name: "3", value: 0, type: 1, properties: [3: integer]}
parse/0: instruction: 18
parse/0: ingredient: {name: "1", value: 0, type: 2-4, properties: [1: address:point, deref: , foo: ]}
parse/0: ingredient: {name: "1", value: 0, type: 2-5, properties: [1: address:point, deref: , foo: ]}
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: offset]}
parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]}
after-brace/0: recipe main

View File

@ -11,7 +11,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "16", value: 0, type: 0, properties: [16: literal]}
parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]}
parse/0: instruction: 20
parse/0: ingredient: {name: "1", value: 0, type: 6, properties: [1: integer-array]}
parse/0: ingredient: {name: "1", value: 0, type: 4-1, properties: [1: array:integer]}
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]}
parse/0: product: {name: "5", value: 0, type: 1, properties: [5: integer]}
after-brace/0: recipe main

View File

@ -11,7 +11,7 @@ parse/0: instruction: 1
parse/0: ingredient: {name: "16", value: 0, type: 0, properties: [16: literal]}
parse/0: product: {name: "4", value: 0, type: 1, properties: [4: integer]}
parse/0: instruction: 21
parse/0: ingredient: {name: "1", value: 0, type: 6, properties: [1: integer-array]}
parse/0: ingredient: {name: "1", value: 0, type: 4-1, properties: [1: array:integer]}
parse/0: ingredient: {name: "0", value: 0, type: 0, properties: [0: literal]}
parse/0: product: {name: "5", value: 0, type: 1, properties: [5: integer]}
after-brace/0: recipe main

View File

@ -1,9 +1,9 @@
parse/0: instruction: 1
parse/0: ingredient: {name: "10", value: 0, type: 0, properties: [10: literal]}
parse/0: product: {name: "default-space", value: 0, type: 2-0, properties: [default-space: address:space]}
parse/0: product: {name: "default-space", value: 0, type: 1-0, properties: [default-space: address:space]}
parse/0: instruction: 1
parse/0: ingredient: {name: "12", value: 0, type: 0, properties: [12: literal]}
parse/0: product: {name: "1", value: 0, type: 1, properties: [1: integer]}
parse/0: product: {name: "1", value: 0, type: 3, properties: [1: integer]}
name/0: assign default-space 1
after-brace/0: recipe main
after-brace/0: copy ...

View File

@ -86,6 +86,8 @@ void setup_types() {
Type[address].name = "address";
int boolean = Type_number["boolean"] = Next_type_number++;
Type[boolean].name = "boolean";
int array = Type_number["array"] = Next_type_number++;
Type[array].name = "array";
// End Mu Types Initialization.
}
:(before "End Setup")
@ -101,13 +103,11 @@ void setup_types() {
struct type_info {
string name;
bool is_record;
bool is_array;
size_t size; // only if is_record; primitives and addresses have size 1 while arrays are dynamic
vector<vector<type_number> > elements; // only if is_record
vector<string> element_names; // only if is_record
vector<type_number> element; // only if is_array
// End type_info Fields.
type_info() :is_record(false), is_array(false), size(0) {}
type_info() :is_record(false), size(0) {}
};
:(before "End Globals")

View File

@ -150,8 +150,7 @@ size_t size_of(const reagent& r) {
return size_of(r.types);
}
size_t size_of(const vector<type_number>& types) {
type_info t = Type[types[0]];
// End size_of Cases.
// End size_of(types) Cases.
return 1;
}

View File

@ -1,5 +1,4 @@
//: Records are compound types with multiple elements of potentially different
//: types.
//: Records contain a fixed number of elements of different types.
:(before "End Mu Types Initialization")
//: We'll use this record as a running example, with two integer elements
int point = Type_number["point"] = Next_type_number++;
@ -47,7 +46,8 @@ recipe main [
]
+mem: storing 36 in location 17
:(before "End size_of Cases")
:(before "End size_of(types) Cases")
type_info t = Type[types[0]];
if (t.is_record) {
size_t result = 0;
for (size_t i = 0; i < t.elements.size(); ++i) {

View File

@ -1,25 +1,13 @@
//: Support for arrays.
:(before "End Mu Types Initialization")
//: We'll use this array as a running example:
int integer_array = Type_number["integer-array"] = Next_type_number++;
Type[integer_array].is_array = true;
Type[integer_array].element.push_back(integer);
Type[integer_array].name = "integer-array";
//: update size mismatch check
:(replace "if (size_of(x) != data.size())" following "void write_memory(reagent x, vector<int> data)")
if (!Type[x.types[0]].is_array && size_of(x) != data.size())
//: Arrays can be copied around with a single instruction just like integers,
//: no matter how large they are.
//: Arrays contain a variable number of elements of the same type.
:(scenario copy_array)
# Arrays can be copied around with a single instruction just like integers,
# no matter how large they are.
recipe main [
1:integer <- copy 3:literal
2:integer <- copy 14:literal
3:integer <- copy 15:literal
4:integer <- copy 16:literal
5:integer-array <- copy 1:integer-array
5:array:integer <- copy 1:array:integer
]
+run: instruction main/4
+run: ingredient 0 is 1
@ -32,49 +20,25 @@ recipe main [
+mem: storing 15 in location 7
+mem: storing 16 in location 8
//: disable the size mismatch check since the destination array need not be initialized
:(replace "if (size_of(x) != data.size())" following "void write_memory(reagent x, vector<int> data)")
if (x.types[0] != Type_number["array"] && size_of(x) != data.size())
:(after "size_t size_of(const reagent& r)")
const type_info& t = Type[r.types[0]];
if (t.is_array) {
int base = r.value;
return 1 + Memory[base]*size_of(t.element);
static const int ARRAY = Type_number["array"];
if (r.types[0] == ARRAY) {
assert(r.types.size() > 1);
// skip the 'array' type to get at the element type
return 1 + Memory[r.value]*size_of(array_element(r.types));
}
:(before "End Globals")
// Operator to look at elements of arrays.
const int INDEX = 20;
:(before "End Primitive Recipe Numbers")
Recipe_number["index"] = INDEX;
assert(Next_recipe_number == INDEX);
Next_recipe_number++;
:(before "End Primitive Recipe Implementations")
case INDEX: {
trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
reagent base = canonize(instructions[pc].ingredients[0]);
int base_address = base.value;
int base_type = base.types[0];
assert(Type[base_type].is_array);
trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
size_t offset = instructions[pc].ingredients[1].value;
int src = base_address + 1 + offset*size_of(Type[base_type].element);
trace("run") << "address to copy is " << src;
int src_type = Type[base_type].element[0];
trace("run") << "its type is " << src_type;
reagent tmp;
tmp.set_value(src);
tmp.types.push_back(src_type);
vector<int> result(read_memory(tmp));
trace("run") << "product 0 is " << result[0];
write_memory(instructions[pc].products[0], result);
break;
}
//: array elements are accessed using 'index'
:(scenario "index")
recipe main [
1:integer <- copy 3:literal
2:integer <- copy 14:literal
3:integer <- copy 15:literal
4:integer <- copy 16:literal
5:integer <- index 1:integer-array, 0:literal
5:integer <- index 1:array:integer, 0:literal
]
+run: instruction main/4
+run: ingredient 0 is 1
@ -86,40 +50,75 @@ recipe main [
+mem: storing 14 in location 5
:(before "End Globals")
// To write to fields of records, you need their address.
const int INDEX_ADDRESS = 21;
// Operator to look at elements of arrays.
const int INDEX = 20;
:(before "End Primitive Recipe Numbers")
Recipe_number["index-address"] = INDEX_ADDRESS;
assert(Next_recipe_number == INDEX_ADDRESS);
Recipe_number["index"] = INDEX;
assert(Next_recipe_number == INDEX);
Next_recipe_number++;
:(before "End Primitive Recipe Implementations")
case INDEX_ADDRESS: {
case INDEX: {
static const int ARRAY = Type_number["array"];
trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
reagent base = canonize(instructions[pc].ingredients[0]);
int base_address = base.value;
int base_type = base.types[0];
assert(Type[base_type].is_array);
assert(base.types[0] == ARRAY);
trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
size_t offset = instructions[pc].ingredients[1].value;
int src = base_address + 1 + offset*size_of(Type[base_type].element);
vector<type_number> element_type = array_element(base.types);
int src = base_address + 1 + offset*size_of(element_type);
trace("run") << "address to copy is " << src;
vector<int> result;
result.push_back(src);
trace("run") << "its type is " << element_type[0];
reagent tmp;
tmp.set_value(src);
copy(element_type.begin(), element_type.end(), inserter(tmp.types, tmp.types.begin()));
vector<int> result(read_memory(tmp));
trace("run") << "product 0 is " << result[0];
write_memory(instructions[pc].products[0], result);
break;
}
:(code)
vector<type_number> array_element(const vector<type_number>& types) {
return vector<type_number>(++types.begin(), types.end());
}
:(scenario "index_address")
recipe main [
1:integer <- copy 3:literal
2:integer <- copy 14:literal
3:integer <- copy 15:literal
4:integer <- copy 16:literal
5:integer <- index-address 1:integer-array, 0:literal
5:integer <- index-address 1:array:integer, 0:literal
]
+run: instruction main/4
+run: ingredient 0 is 1
+run: ingredient 1 is 0
+run: address to copy is 2
+mem: storing 2 in location 5
:(before "End Globals")
// To write to fields of records, you need their address.
const int INDEX_ADDRESS = 21;
:(before "End Primitive Recipe Numbers")
Recipe_number["index-address"] = INDEX_ADDRESS;
assert(Next_recipe_number == INDEX_ADDRESS);
Next_recipe_number++;
:(before "End Primitive Recipe Implementations")
case INDEX_ADDRESS: {
static const int ARRAY = Type_number["array"];
trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name;
reagent base = canonize(instructions[pc].ingredients[0]);
int base_address = base.value;
assert(base.types[0] == ARRAY);
trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
size_t offset = instructions[pc].ingredients[1].value;
vector<type_number> element_type = array_element(base.types);
int src = base_address + 1 + offset*size_of(element_type);
trace("run") << "address to copy is " << src;
vector<int> result;
result.push_back(src);
trace("run") << "product 0 is " << result[0];
write_memory(instructions[pc].products[0], result);
break;
}