Commit a0ca5275 authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan
Browse files

declare and allocate container structs for projections passed to a function

parent 4fbcfebd
......@@ -188,7 +188,7 @@ std::vector<Rpc*> GlobalScope::function_pointer_to_rpc()
if(pf->type()->num() == 7) { // function pointer field
Function *f = dynamic_cast<Function*>(pf->type());
rpcs.push_back(f->to_rpc(this, pt));
rpcs.push_back(f->to_rpc(pt));
}
}
}
......@@ -381,7 +381,7 @@ std::vector<Rpc*> LexicalScope::function_pointer_to_rpc()
if(pf->type()->num() == 7) { // function pointer field
Function *f = dynamic_cast<Function*>(pf->type());
rpcs.push_back(f->to_rpc(this, pt));
rpcs.push_back(f->to_rpc(pt));
}
}
}
......
......@@ -23,11 +23,12 @@ const char* hidden_args_name(const char* name)
return new_name(name, "_hidden_args");
}
Function::Function(const char *id, ReturnVariable *return_var, std::vector<Parameter*> parameters)
Function::Function(const char *id, ReturnVariable *return_var, std::vector<Parameter*> parameters, LexicalScope *ls)
{
this->identifier_ = id;
this->return_var_ = return_var;
this->parameters_ = parameters;
this->current_scope_ = ls;
}
Marshal_type* Function::accept(MarshalPrepareVisitor *worker)
......@@ -66,28 +67,28 @@ void Function::create_trampoline_structs(LexicalScope *ls)
return;
}
Rpc* Function::to_rpc(LexicalScope *ls, ProjectionType *pt)
Rpc* Function::to_rpc(ProjectionType *pt)
{
// adding extra parameters here. but maybe depending on needs this could be done at parse time
// and these extra parameters can be added to the Function.
std::vector<Parameter*> new_parameters;
new_parameters.insert(new_parameters.end(), this->parameters_.begin(), this->parameters_.end());
int err;
Type *dstore = ls->lookup("dstore", &err);
Type *dstore = this->current_scope_->lookup("dstore", &err);
if(dstore == 0x0) {
printf("Error: dstore is not in scope\n");
}
new_parameters.push_back(new Parameter(dstore, "dstore", 1));
const char* c_name = container_name(pt->name());
Type *container = ls->lookup(c_name, &err);
Type *container = this->current_scope_->lookup(c_name, &err);
if(container == 0x0) {
printf("Error: container is not in scope\n");
}
new_parameters.push_back(new Parameter(container, c_name, 1));
Rpc* tmp = new Rpc(this->return_var_, this->identifier_, new_parameters, ls);
Rpc* tmp = new Rpc(this->return_var_, this->identifier_, new_parameters, this->current_scope_);
tmp->set_function_pointer_defined(true);
return tmp;
}
......
......@@ -1187,9 +1187,7 @@ CCSTEnumConst::CCSTEnumConst()
void CCSTEnumConst::write(FILE *f)
{
printf("incomplete enum const\n");
//TODO
// unsure
fprintf(f, "%s", this->enum_val_);
}
CCSTExpression::CCSTExpression()
......
......@@ -90,18 +90,70 @@ CCSTCompoundStatement* caller_body(Rpc *r)
// allocate necessary container shit
/* code that loops through parameters and allocates/initializes whatever necessary before marshalling*/
// loop through params, declare a tmp and pull out marshal value
std::vector<Parameter*> params = r->parameters();
for(std::vector<Parameter*>::iterator it = params.begin(); it != params.end(); it ++) {
Parameter *p = *it;
if(p->in()) {
statements.push_back(helper(p)); // declare, alloc, init parameters as necessary
// helper is incomplete
// for each parameter that is ia projection -- & is alloc
for(std::vector<Parameter*>::iterator it = params.begin(); it != params.end(); it ++)
{
Parameter *p = *it;
if(p->type()->num() == 4 && p->alloc_caller()) {
ProjectionType *pt = dynamic_cast<ProjectionType*>(p->type());
Assert(pt != 0x0, "Error: dynamic cast to Projection type failed!\n");
// lookup container struct
int err;
const char* container_name_ = container_name(pt->name());
Type *container_tmp = r->current_scope()->lookup(container_name_, &err);
printf("looked up container %s for function %s. pointer for regular projection %p\n", container_name_, r->name(), r->current_scope()->lookup(pt->name(), &err));
Assert(container_tmp != 0x0, "Error: could not find container in environment\n");
ProjectionType *container = dynamic_cast<ProjectionType*>(container_tmp);
Assert(container != 0x0, "Error: dynamic cast to Projection type failed!\n");
// declare instance of container
std::vector<CCSTInitDeclarator*> decs;
decs.push_back(new CCSTDeclarator(new CCSTPointer(), new CCSTDirectDecId(container_name_)));
CCSTDeclaration *container_declaration = new CCSTDeclaration(type2(container), decs);
// finish declaring instance of container
declarations.push_back(container_declaration);
// file_container = kzalloc(sizeof(*file_container), GFP_KERNEL);
std::vector<CCSTAssignExpr*> kzalloc_args;
kzalloc_args.push_back(new CCSTUnaryExprSizeOf(new CCSTUnaryExprCastExpr(new CCSTUnaryOp(unary_mult_t), new CCSTPrimaryExprId(container_name_))));
kzalloc_args.push_back(new CCSTEnumConst("GFP_KERNEL"));
statements.push_back(new CCSTAssignExpr(new CCSTPrimaryExprId(container_name_), equals(), function_call("kzalloc", kzalloc_args)));
// if null
// LIBLCD_ERR("kzalloc");
// lcd_exit(-1); /* abort */
std::vector<CCSTDeclaration*> if_body_declarations;
std::vector<CCSTStatement*> if_body_statements;
std::vector<CCSTAssignExpr*> liblcd_err_args;
liblcd_err_args.push_back(new CCSTString("kzalloc"));
if_body_statements.push_back(function_call("LIBLCD_ERR", liblcd_err_args));
std::vector<CCSTAssignExpr*> lcd_exit_args;
lcd_exit_args.push_back(new CCSTInteger(-1));
if_body_statements.push_back(function_call("lcd_exit", lcd_exit_args));
CCSTCompoundStatement *if_body = new CCSTCompoundStatement(if_body_declarations, if_body_statements);
statements.push_back(new CCSTIfStatement(new CCSTUnaryExprCastExpr(Not(), new CCSTPrimaryExprId(container_name_))
, if_body));
// insert into dstore
// do error checking
}
}
}
/* end of this code, could go in own function maybe?*/
/* end of this code, could go in own function maybe?*/
/*
std::vector<CCSTStatement*> statements_tmp = marshal_in_parameters(r->parameters());
statements.insert(statements.end(), statements_tmp.begin(), statements_tmp.end());
......@@ -122,7 +174,7 @@ CCSTCompoundStatement* caller_body(Rpc *r)
else {
statements.push_back(new CCSTReturn());
}
*/
return new CCSTCompoundStatement(declarations, statements);
}
......
......@@ -58,6 +58,11 @@ CCSTAssignOp* equals()
return new CCSTAssignOp(equal_t);
}
CCSTUnaryOp* Not()
{
return new CCSTUnaryOp(unary_bang_t);
}
/*
* confirm this works
* returns a new string with _p on end.
......@@ -585,9 +590,9 @@ CCSTPrimaryExprId* function_name(const char *func_name)
return new CCSTPrimaryExprId(func_name);
}
CCSTPostFixExprAssnExpr* function_call(CCSTPrimaryExprId *func_name, std::vector<CCSTAssignExpr*> args)
CCSTPostFixExprAssnExpr* function_call(const char *func_name, std::vector<CCSTAssignExpr*> args)
{
return new CCSTPostFixExprAssnExpr(func_name, args);
return new CCSTPostFixExprAssnExpr(new CCSTPrimaryExprId(func_name), args);
}
/*
......
......@@ -32,7 +32,7 @@ CCSTStatement* MarshalTypeVisitor::visit(Typedef *td, Variable *v)
} else {
arguments.push_back(access_v);
}
return function_call( function_name( store_register_mapping(mt->get_register()))
return function_call(store_register_mapping(mt->get_register())
, arguments);
}
......@@ -56,7 +56,7 @@ CCSTStatement* MarshalTypeVisitor::visit(IntegerType *it, Variable *v)
} else {
arguments.push_back(access_v);
}
return function_call( function_name( store_register_mapping(mt->get_register()))
return function_call( store_register_mapping(mt->get_register())
, arguments);
}
......
......@@ -140,57 +140,62 @@ CCSTCompoundStatement* callee_body(Rpc *r)
// loop through params, declare a tmp and pull out marshal value
std::vector<Parameter*> params = r->parameters();
std::vector<char*> param_names;
std::vector<CCSTAssignExpr*> unmarshalled_args;
// for each parameter that is ia projection -- & is alloc
for(std::vector<Parameter*>::iterator it = params.begin(); it != params.end(); it ++)
{
Parameter *p = (Parameter*) *it;
statements.push_back(unmarshal_variable(p));
CCSTPrimaryExprId *t = new CCSTPrimaryExprId(p->identifier());
unmarshalled_args.push_back(t);
}
// make real call and get return value if there is one
ReturnVariable *rv = r->return_variable();
/* redo everything after this? at least read through */
Parameter *p = *it;
if(p->type()->num() == 4 && p->alloc_caller()) {
ProjectionType *pt = dynamic_cast<ProjectionType*>(p->type());
Assert(pt != 0x0, "Error: dynamic cast to Projection type failed!\n");
// lookup container struct
int err;
const char* container_name_ = container_name(pt->name());
Type *container_tmp = r->current_scope()->lookup(container_name_, &err);
Assert(container_tmp != 0x0, "Error: could not find container in environment\n");
ProjectionType *container = dynamic_cast<ProjectionType*>(container_tmp);
Assert(container != 0x0, "Error: dynamic cast to Projection type failed!\n");
// declare instance of container
std::vector<CCSTInitDeclarator*> decs;
decs.push_back(new CCSTDeclarator(new CCSTPointer(), new CCSTDirectDecId(container_name_)));
CCSTDeclaration *container_declaration = new CCSTDeclaration(type2(container), decs);
// finish declaring instance of container
declarations.push_back(container_declaration);
// file_container = kzalloc(sizeof(*file_container), GFP_KERNEL);
std::vector<CCSTAssignExpr*> kzalloc_args;
kzalloc_args.push_back(new CCSTUnaryExprSizeOf(new CCSTUnaryExprCastExpr(new CCSTUnaryOp(unary_mult_t), new CCSTPrimaryExprId(container_name_))));
kzalloc_args.push_back(new CCSTEnumConst("GFP_KERNEL"));
if(rv->type()->num() != 5) // not void
{
/*
Marshal_type *ret_info = r->explicit_ret_marshal_info();
statements.push_back(new CCSTAssignExpr(new CCSTPrimaryExprId(container_name_), equals(), function_call("kzalloc", kzalloc_args)));
CCSTPointer *p = 0x0;
if(r->explicit_return_type()->num() == 3)
p = new CCSTPointer();
// if null
// LIBLCD_ERR("kzalloc");
// lcd_exit(-1); /* abort */
std::vector<CCSTDeclaration*> if_body_declarations;
std::vector<CCSTStatement*> if_body_statements;
std::vector<CCSTAssignExpr*> liblcd_err_args;
liblcd_err_args.push_back(new CCSTString("kzalloc"));
if_body_statements.push_back(function_call("LIBLCD_ERR", liblcd_err_args));
std::vector<CCSTAssignExpr*> lcd_exit_args;
lcd_exit_args.push_back(new CCSTInteger(-1));
if_body_statements.push_back(function_call("lcd_exit", lcd_exit_args));
CCSTCompoundStatement *if_body = new CCSTCompoundStatement(if_body_declarations, if_body_statements);
statements.push_back(new CCSTIfStatement(new CCSTUnaryExprCastExpr(Not(), new CCSTPrimaryExprId(container_name_))
, if_body));
std::vector<CCSTDecSpecifier*> ret_type = type(r->explicit_return_type());
std::vector<CCSTInitDeclarator*> inits;
inits.push_back(new CCSTInitDeclarator(new CCSTDeclarator(p, new CCSTDirectDecId(ret_info->get_name()))
, new CCSTInitializer( new CCSTPostFixExprAssnExpr(new CCSTPrimaryExprId(r->name()) , unmarshalled_args))));
std::vector<CCSTDeclaration*> cd;
std::vector<CCSTStatement*> cs;
cd.push_back(new CCSTDeclaration(ret_type, inits));
MarshalTypeVisitor *visitor = new MarshalTypeVisitor();
cs.push_back(ret_info->accept(visitor));
statements.push_back(new CCSTCompoundStatement(cd, cs));
*/
}
else
{
statements.push_back(new CCSTPostFixExprAssnExpr(new CCSTPrimaryExprId(r->name()) ,unmarshalled_args));
// insert into dstore
// do error checking
}
}
// implicit returns
// just loop through parameters
std::vector<CCSTAssignExpr*> empty_args;
statements.push_back(new CCSTPostFixExprAssnExpr(new CCSTPrimaryExprId("reply") , empty_args));
return new CCSTCompoundStatement(declarations, statements);
}
......@@ -284,7 +289,7 @@ CCSTFile* generate_server_source(Module *m)
}
}
}
// print trampoline structs
std::vector<Rpc*> rpcs = m->rpc_definitions();
for(std::vector<Rpc*>::iterator it = rpcs.begin(); it != rpcs.end(); it ++) {
......@@ -302,7 +307,7 @@ CCSTFile* generate_server_source(Module *m)
definitions.push_back(new CCSTDeclaration(specifier, empty));
}
}
// globals.
std::vector<GlobalVariable*> globals = m->globals();
......
......@@ -308,16 +308,17 @@ class Function : public Type
const char *identifier_;
ReturnVariable *return_var_;
std::vector<Parameter*> parameters_;
LexicalScope *current_scope_;
public:
Function(const char *id, ReturnVariable *return_var, std::vector<Parameter*> parameters);
Function(const char *id, ReturnVariable *return_var, std::vector<Parameter*> parameters, LexicalScope *ls);
virtual Marshal_type* accept(MarshalPrepareVisitor *worker);
virtual CCSTTypeName* accept(TypeNameVisitor *worker);
virtual CCSTStatement* accept(TypeVisitor *worker, Variable *v);
virtual int num();
virtual const char* name();
virtual void resolve_types(LexicalScope *ls);
Rpc* to_rpc(LexicalScope *ls, ProjectionType *pt);
Rpc* to_rpc(ProjectionType *pt);
virtual void create_trampoline_structs(LexicalScope *ls);
};
......
......@@ -66,8 +66,9 @@ code: {{
{
int count = 0;
for(Value::iterator it = v.getValues().begin(); it != v.getValues().end(); it++) {
const Value & v = *it;
char star = (char) (intptr_t) v.getValue();
const Value & v2 = *it;
char star = (char) (intptr_t) v2.getValue();
printf("supposed to be a star is %c\n", star);
if(star == '*') {
count += 1;
} else {
......@@ -210,7 +211,7 @@ Interface_special = "module" {{
}}
Typedef = "typedef" Spacing "<" Spacing rt:RealType "," Spacing mt:Type_form ">" Spacing id:Identifier Spacing {{
Typedef * val = new Typedef((char*) rt.getValue(), (Type*) mt.getValue());
Typedef * val = new Typedef((char*) id.getValue(), (char*) rt.getValue(), (Type*) mt.getValue());
insertType((char*)id.getValue(),val);
}}
......@@ -285,7 +286,7 @@ Include = "include" Space+ "<" Spacing p:Pathname Spacing ">" Spacing{{
FP_Parameter = "rpc" Spacing s:Specification? rt:Type_form pointers:star* Spacing OPEN Spacing "*" Spacing id:Identifier Spacing CLOSE Spacing OPEN p:Parameters CLOSE {{
std::vector<Parameter*> *params = (std::vector<Parameter*>*) p.getValue();
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params);
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params, get_current_scope());
Parameter *p = new Parameter(fp, (char *) id.getValue(), 1);
......@@ -317,8 +318,8 @@ FP_Parameter = "rpc" Spacing s:Specification? rt:Type_form pointers:star* Spacin
}}
FirstParam = t:Type_form s:Specification? pointers:star* Spacing id:Identifier Spacing COMMA Spacing {{
Parameter *p = new Parameter((Type *) t.getValue(), (char *) id.getValue(), count_pointers(pointers));
int p_count = count_pointers(pointers);
Parameter *p = new Parameter((Type *) t.getValue(), (char *) id.getValue(), p_count);
if (s.getValue() != 0x0) {
std::vector<bool>* specs = (std::vector<bool>*) s.getValue();
......@@ -386,12 +387,13 @@ FP_FirstParam = t:Type_form s:Specification? pointers:star* Spacing COMMA Spacin
}}
| "rpc" Space+ rt:Type_form pointers:star* Spacing OPEN Spacing "*" Spacing id:Identifier Spacing CLOSE Spacing OPEN p:Parameters CLOSE SEMI Spacing COMMA Spacing {{
std::vector<Parameter*> *params = (std::vector<Parameter*>*) p.getValue();
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params);
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params, get_current_scope());
value = new FPParameter(fp, 1);
}}
LastParam = t:Type_form s:Specification? pointers:star* Spacing id:Identifier Spacing {{
Parameter *p = new Parameter((Type *) t.getValue(), (char *) id.getValue(), count_pointers(pointers));
int p_count = count_pointers(pointers);
Parameter *p = new Parameter((Type *) t.getValue(), (char *) id.getValue(), p_count);
if (s.getValue() != 0x0) {
std::vector<bool>* specs = (std::vector<bool>*) s.getValue();
......@@ -450,7 +452,7 @@ FP_LastParam = t:Type_form s:Specification? pointers:star* Spacing {{
| "rpc" Space+ rt:Type_form pointers:star* Spacing OPEN Spacing "*" Spacing id:Identifier Spacing CLOSE Spacing OPEN p:Parameters CLOSE SEMI Spacing {{
// todo change return type to return variable and add pointer
std::vector<Parameter*> *params = (std::vector<Parameter*>*) p.getValue();
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params);
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params, get_current_scope());
value = new FPParameter(fp, 1);
}}
......@@ -542,7 +544,6 @@ Projection_constructor = s:Projection_constructor_special Spacing "<" Spacing
ProjectionType *val = new ProjectionType((char*) id.getValue(), (char*) t.getValue(), fields, globals);
insertType((char*) id.getValue(), val);
set_current_scope(get_current_scope()->outer_scope()); // leaving current scope
// creating constructor
std::vector<ProjectionField*> container_fields;
......@@ -554,7 +555,7 @@ Projection_constructor = s:Projection_constructor_special Spacing "<" Spacing
ProjectionType * container = new ProjectionType(container_name_str, container_name_str, container_fields);
insertType(container_name_str, container);
// finished creating constructor
set_current_scope(get_current_scope()->outer_scope()); // leaving current scope
value = val;
}}
......@@ -737,7 +738,7 @@ ProjField = "projection" Space+ id:Identifier Spacing s:Specification? pointers:
printf("matched a function pointer field\n");
std::vector<Parameter*> *params = (std::vector<Parameter*>*) p.getValue();
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params);
Function *fp = new Function((char*) id.getValue(), new ReturnVariable((Type*) rt.getValue(), count_pointers(pointers)), *params, get_current_scope());
ProjectionField *pf = new ProjectionField(fp, (char*) id.getValue(), 1);
if (s.getValue() != 0x0) {
......@@ -773,9 +774,7 @@ Last = k:Keyword Spacing {{ value = k; }}
Option = "projection"
star = [*] {{
printf("matched a star\n");
}}
star = [*]
signed_type = "int" Spacing {{
printf("matched int type\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment