about summary refs log blame commit diff stats
path: root/cpp/019address
blob: a642ceb1e9ead2da23b01da761633d988191c868 (plain) (tree)
> 109 110

                                                                              
                           





                                                                     
                        

                      
                              



                                                 
                       
                              


                  
                     
                                             
                           
                                                    






                                                                

                                                                       
                            



                                            
                        
                     
                              
 

                                                             
                                                               
                  
                                          
                     


                                                              
                                                                                 





                                       
                                 
                
                                                                     












                                                    
                              
                                                    
                     
                                
 
                  
                                    
                                                                   






                                                                                       
                             

                                                 

                           
                        



                                                 
 
                                      
                          





                                                  
                        

                          
                              
 









                                                      
                              
 



                                                                             
                                


                                                                             
                                                       
                                                        

                                       


                                                        





                                                     
              
                     






                                                     
                                  






                                                          
                        






                                                                             
                                


                                                                             
                                                       
                                                        

                                       


                                                        
   







                                                     
//: Instructions can read from addresses pointing at other locations using the
//: 'deref' property.
:(scenario "copy_indirect")
recipe main [
  1:address:integer <- copy 2:literal
  2:integer <- copy 34:literal
  # This loads location 1 as an address and looks up *that* location.
  3:integer <- copy 1:address:integer/deref
]
+run: instruction main/2
+mem: location 1 is 2
+mem: location 2 is 34
+mem: storing 34 in location 3

:(replace{} "vector<int> read_memory(reagent x)")
vector<int> read_memory(reagent x) {
  vector<int> result;
  if (isa_literal(x)) {
    result.push_back(x.value);
    return result;
  }
  x = canonize(x);
  int base = x.value;
//?   cout << "base: " << base << '\n'; //? 1
  size_t size = size_of(x);
  for (size_t offset = 0; offset < size; ++offset) {
    int val = Memory[base+offset];
    trace("mem") << "location " << base+offset << " is " << val;
    result.push_back(val);
  }
  return result;
}

//: similarly, write to addresses pointing at other locations using the
//: 'deref' property
:(scenario "store_indirect")
recipe main [
  1:address:integer <- copy 2:literal
  1:address:integer/deref <- copy 34:literal
]
+run: instruction main/1
+mem: location 1 is 2
+mem: storing 34 in location 2

:(replace{} "void write_memory(reagent x, vector<int> data)")
void write_memory(reagent x, vector<int> data) {
  if (x.name == "_") return;  // dummy results are never stored
  x = canonize(x);
//?   cout << x.to_string() << '\n'; //? 1
  int base = x.value;
  if (!Type[x.types[0]].is_array && size_of(x) != data.size())
    raise << "size mismatch in storing to " << x.to_string();
  for (size_t offset = 0; offset < data.size(); ++offset) {
    trace("mem") << "storing " << data[offset] << " in location " << base+offset;
    Memory[base+offset] = data[offset];
  }
}

:(code)
reagent canonize(reagent x) {
//?   cout << "canonize\n"; //? 1
  reagent r = x;
//?   cout << x.to_string() << " => " << r.to_string() << '\n'; //? 1
  while (has_property(r, "deref"))
    r = deref(r);
  return r;
}

bool has_property(reagent x, string name) {
  for (size_t i = 0; i < x.properties.size(); ++i) {
    if (x.properties[i].first == name) return true;
  }
  return false;
}

reagent deref(reagent x) {
//?   cout << "deref\n"; //? 1
  static const int ADDRESS = Type_number["address"];
  reagent result("");
  assert(x.types[0] == ADDRESS);

  // compute value
  result.set_value(Memory[x.value]);
  trace("mem") << "location " << x.value << " is " << result.value;

  // populate types
  copy(++x.types.begin(), x.types.end(), inserter(result.types, result.types.begin()));

  // drop-one 'deref'
  int i = 0;
  int len = x.properties.size();
  for (i = 0; i < len; ++i) {
    if (x.properties[i].first == "deref") break;
    result.properties.push_back(x.properties[i]);
  }
  ++i;  // skip first deref
  for (; i < len; ++i) {
    result.properties.push_back(x.properties[i]);
  }
  return result;
}

//: 'get' can read from record address
:(scenario "get_indirect")
recipe main [
  1:integer <- copy 2:literal
  2:integer <- copy 34:literal
  3:integer <- copy 35:literal
  4:integer <- get 1:address:point/deref, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 34
+mem: storing 34 in location 4

:(scenario "include_nonderef_properties")
recipe main [
  1:integer <- copy 2:literal
  2:integer <- copy 34:literal
  3:integer <- copy 35:literal
  4:integer <- get 1:address:point/deref/foo, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 34
+mem: storing 34 in location 4

:(replace{} "case GET:")
case GET: {
  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_record);
  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
  assert(isa_literal(instructions[pc].ingredients[1]));
  size_t offset = instructions[pc].ingredients[1].value;
  int src = base_address;
  for (size_t i = 0; i < offset; ++i) {
    reagent tmp;
    tmp.types.push_back(Type[base_type].elements[i][0]);
    src += size_of(tmp);
  }
  trace("run") << "address to copy is " << src;
  assert(Type[base_type].is_record);
  assert(Type[base_type].elements.size() > offset);
  int src_type = Type[base_type].elements[offset][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;
}

:(scenario "get_address_indirect")
# 'get' can read from record address
recipe main [
  1:integer <- copy 2:literal
  2:integer <- copy 34:literal
  3:integer <- copy 35:literal
  4:integer <- get-address 1:address:point/deref, 0:offset
]
+run: instruction main/3
+run: address to copy is 2
+run: product 0 is 2

:(replace{} "case GET_ADDRESS:")
case GET_ADDRESS: {
  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_record);
  trace("run") << "ingredient 1 is " << instructions[pc].ingredients[1].name;
  assert(isa_literal(instructions[pc].ingredients[1]));
  size_t offset = instructions[pc].ingredients[1].value;
  int src = base_address;
  for (size_t i = 0; i < offset; ++i) {
    reagent tmp;
    tmp.types.push_back(Type[base_type].elements[i][0]);
    src += size_of(tmp);
  }
  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;
}