Commit 43e429fc authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan

code for generating container struct definitions. updated makefile to reflect...

code for generating container struct definitions. updated makefile to reflect changes to code structure. fix compilation bugs. committing to switch branches
parent 424ac36d
......@@ -7,36 +7,51 @@ CXXFLAGS = -g -fsanitize=address -fno-omit-frame-pointer -Iinclude/ -lasan
CXX = g++
LDFLAGS = -fsanitize=address
objects = lcd_ast.o main.o lcd_idl.o scope.o error.o marshal_op.o ccst.o ast_to_ccst.o marshal_visitor.o
objects = lcd_ast.o main.o lcd_idl.o scope.o variable.o types.o error.o marshal_op.o ccst.o server.o client.o helper.o unmarshal.o marshal.o
$(bin): $(objects)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)
main.o: main/main.cpp include/lcd_ast.h include/lcd_idl.h include/error.h include/ccst.h include/marshal_visitor.h
main.o: main/main.cpp include/lcd_ast.h include/lcd_idl.h include/error.h include/ccst.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
error.o: error/error.cpp include/error.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
ast_to_ccst.o: ast_to_ccst.cpp include/ccst.h include/marshal_op.h
marshal.o: code_gen/marshal.cpp include/ccst.h include/marshal_op.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
unmarshal.o: code_gen/unmarshal.cpp include/ccst.h include/marshal_op.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
helper.o: code_gen/helper.cpp include/ccst.h include/code_gen.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
client.o: code_gen/client.cpp inclue/ccst.h include/code_gen.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
server.o: code_gen/server.cpp include/ccst.h include/code_gen.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
ccst.o: ccst.cpp include/ccst.h include/marshal_op.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
lcd_idl.o: $(idl) include/lcd_ast.h
types.o: ast/types.cpp include/lcd_ast.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
scope.o: ast/scope.cpp include/lcd_ast.h
variable.o: ast/variable.cpp include/lcd_ast.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
marshal_visitor.o: marshal_visitor.cpp include/marshal_visitor.h
lcd_idl.o: $(idl) include/lcd_ast.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
scope.o: ast/scope.cpp include/lcd_ast.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
marshal_op.o: marshal_op.cpp include/marshal_op.h include/lcd_ast.h include/assert.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
lcd_ast.o: ast/lcd_ast.cpp include/lcd_ast.h
lcd_ast.o: ast/lcd_ast.cpp include/marshal_op.h include/lcd_ast.h
$(CXX) $(CXXFLAGS) -c -o $@ $(filter-out %.h,$^)
include/lcd_idl.h: parser/lcd_idl.peg
......
......@@ -83,8 +83,9 @@ std::vector<Parameter*> Rpc::parameters()
return parameters_;
}
Module::Module(std::vector<Rpc*> rpc_definitions, std::vector<GlobalVariable*> globals)
Module::Module(std::vector<Rpc*> rpc_definitions, std::vector<GlobalVariable*> globals, LexicalScope *ls)
{
this->module_scope_ = ls;
this->rpc_definitions_ = rpc_definitions;
this->globals_ = globals;
}
......@@ -98,3 +99,8 @@ std::vector<GlobalVariable*> Module::globals()
{
return this->globals_;
}
LexicalScope* Module::module_scope()
{
return this->module_scope_;
}
......@@ -54,7 +54,7 @@ bool GlobalScope::insert(Rpc *r)
bool GlobalScope::contains(const char *symbol)
{
std::string temp(symbol);
if(this->type_definitions_.find(temp) == this->type_definitions_.end()) {
if(this->types_definitions_.find(temp) == this->types_definitions_.end()) {
return false;
} else {
return true;
......@@ -65,7 +65,7 @@ Type * GlobalScope::lookup(const char * sym, int* err)
{
std::string temp(sym);
if(this->types_definitions_.find(temp) == this->type_definitions_.end())
if(this->types_definitions_.find(temp) == this->types_definitions_.end())
{
*err = 0;
return 0;
......@@ -73,7 +73,7 @@ Type * GlobalScope::lookup(const char * sym, int* err)
else
{
*err = 1;
return type_definitions_[temp];
return types_definitions_[temp];
}
}
......@@ -82,7 +82,7 @@ bool GlobalScope::insert(const char* sym, Type * value)
std::string temp(sym);
printf("insert %s\n",temp.c_str());
std::pair<std::map<std::string,Type*>::iterator,bool> ret;
ret = type_definitions_.insert(std::pair<std::string, Type*>(temp, value));
ret = types_definitions_.insert(std::pair<std::string, Type*>(temp, value));
return ret.second;
}
......@@ -103,8 +103,20 @@ void GlobalScope::add_inner_scopes(std::vector<LexicalScope*> scopes)
printf("add inner scopes global scope todo\n");
}
std::map<std::string, Type*> GlobalScope::type_definitions()
{
return this->types_definitions_;
}
std::vector<LexicalScope*> GlobalScope::inner_scopes()
{
return this->inner_scopes_;
}
/* -------------------------------------------------------------- */
LexicalScope::LexicalScope()
{
this->outer_scope_ = 0x0;
......@@ -148,7 +160,7 @@ Type* LexicalScope::lookup(const char *symbol, int *err)
*err = 0;
return 0x0;
} else {
return this->outer_scope_->lookup_symbol(symbol, err);
return this->outer_scope_->lookup(symbol, err);
}
}
else {
......@@ -161,7 +173,7 @@ bool LexicalScope::insert(const char *symbol, Type *type)
{
std::string temp(symbol);
std::pair<std::map<std::string,Type*>::iterator,bool> ret;
ret = type_definitions_.insert(std::pair<std::string, Type*>(temp, value));
ret = type_definitions_.insert(std::pair<std::string, Type*>(temp, type));
return ret.second;
}
......@@ -180,3 +192,14 @@ void LexicalScope::add_inner_scopes(std::vector<LexicalScope*> scopes)
{
printf("error add inner scopes lexical scope todo\n");
}
std::map<std::string, Type*> LexicalScope::type_definitions()
{
return this->type_definitions_;
}
std::vector<LexicalScope*> LexicalScope::inner_scopes()
{
return this->inner_scopes_;
}
......@@ -202,11 +202,6 @@ CCSTStatement* ProjectionType::accept(AllocateTypeVisitor *worker, Variable *v)
return worker->visit(this, v);
}
const char* ProjectionType::id()
{
return this->id_;
}
// may never be used
const char* ProjectionType::real_type()
{
......
......@@ -18,6 +18,29 @@ const char* GlobalVariable::identifier()
return this->id_;
}
void GlobalVariable::set_accessor(Variable *v)
{
printf("Error this operation not allowed\n");
}
Variable* GlobalVariable::accessor()
{
printf("Error this operation is not allowed\n");
return 0x0;
}
Marshal_type* GlobalVariable::marshal_info()
{
printf("global variable marshal_info not yet implemented\n");
return 0x0;
}
Rpc* GlobalVariable::scope()
{
printf("Error: this operation not allowed\n");
return 0x0;
}
// probably more functions needed
/* end */
......@@ -222,6 +245,12 @@ Variable* ProjectionField::accessor()
return this->accessor_;
}
Marshal_type* ProjectionField::marshal_info()
{
printf("projection field marshal info incomplete\n");
return 0x0;
}
void ProjectionField::set_in(bool b)
{
this->in_ = b;
......
......@@ -1106,12 +1106,17 @@ void CCSTPrimaryExprId::write(FILE *f)
CCSTInteger::CCSTInteger()
{
// todo
this->integer_ = -1;
}
CCSTInteger::CCSTInteger(int i)
{
this->integer_ = i;
}
void CCSTInteger::write(FILE *f)
{
// todo
fprintf(f, "%d", this->integer_);
}
CCSTChar::CCSTChar()
......
#include "ccst.h"
#include "code_gen.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
CCSTFile* generate_client_header(File* f)
CCSTFile* generate_client_header(Module* f)
{
}
CCSTDeclaration* declare_static_variable(Variable *gv)
{
std::vector<CCSTDecSpecifier*> specifier;
specifier.push_back(new CCSTStoClassSpecifier(static_t));
std::vector<CCSTDecSpecifier*> type_for_global = type(gv->type());
specifier.insert(specifier.end(), type_for_global.begin(), type_for_global.end());
std::vector<CCSTInitDeclarator*> declarators;
declarators.push_back(new CCSTDeclarator( create_pointer( count_nested_pointer(gv->type())),
new CCSTDirectDecId(gv->identifier())));
return new CCSTDeclaration(specifier, declarators);
}
CCSTFile* generate_client_source(Module* f)
{
std::vector<CCSTExDeclaration*> definitions;
......
#include "ccst.h"
#include "code_gen.h"
/*
creates the C code for a container struct
for the projection provided
*/
CCSTDeclaration* container_struct(const char* name, ProjectionType *pt)
CCSTDeclaration* container_struct_definition(const char* name, ProjectionType *pt, bool channel)
{
// field for the real struct
// field for other side capability
// field for my ref capability
CCSTStructDeclaration *container_field = new CCSTStructDeclaration();
CCSTStructUnionSpecifier *container = new CCSTStructUnionSpecifier(struct_t, name, container_fields);
// optional channel
std::vector<CCSTStructDeclaration*> container_fields;
std::vector<CCSTStructDeclarator*> real_struct_field;
real_struct_field.push_back(new CCSTStructDeclarator(new CCSTDeclarator(0x0, new CCSTDirectDecId(pt->real_type()))));
container_fields.push_back(new CCSTStructDeclaration(type(pt), real_struct_field));
std::vector<CCSTStructDeclarator*> remote_ref_field;
remote_ref_field.push_back(new CCSTStructDeclarator(new CCSTDeclarator(0x0, new CCSTDirectDecId("remote_ref"))));
std::vector<CCSTDecSpecifier*> dptr_t;
dptr_t.push_back(new CCSTTypedefName("dptr_t"));
container_fields.push_back(new CCSTStructDeclaration(dptr_t, remote_ref_field));
std::vector<CCSTStructDeclarator*> my_ref_field;
my_ref_field.push_back(new CCSTStructDeclarator(new CCSTDeclarator(0x0, new CCSTDirectDecId("my_ref"))));
container_fields.push_back(new CCSTStructDeclaration(dptr_t, my_ref_field));
if(channel) {
std::vector<CCSTDecSpecifier*> cptr_t;
cptr_t.push_back(new CCSTTypedefName("cptr_t"));
std::vector<CCSTStructDeclarator*> channel_field;
channel_field.push_back(new CCSTStructDeclarator(new CCSTDeclarator(0x0, new CCSTDirectDecId("chnl"))));
container_fields.push_back(new CCSTStructDeclaration(cptr_t, channel_field));
}
CCSTStructUnionSpecifier *container = new CCSTStructUnionSpecifier(struct_t, name, container_fields);
std::vector<CCSTDecSpecifier*> struct_specifier;
struct_specifier.push_back(container);
std::vector<CCSTInitDeclarator*> empty;
return new CCSTDeclaration(struct_specifier, empty);
}
CCSTDeclaration* typedef_declaration(Typedef *t)
{
/*
std::vector<CCSTDecSpecifier*>specifier;
specifier.push_back(new CCSTStoClassSpecifier(typedef_t));
std::vector<CCSTInitDeclarator*> decs;
*/
// possibly unnecessary depending on if proper header files are included
printf("todo typedef\n");
return 0x0
}
CCSTAssignOp* equals()
{
return new CCSTAssignOp(equal_t);
}
/*
* confirm this works
* returns a new string with _p on end.
*/
const char* parameter_name(const char* name)
{
int length = strlen(name);
char *new_str = (char*) malloc(sizeof(char)*(length+3));
std::ostringstream total;
total << name << "_p";
strncpy(new_str, total.str().c_str(), length+3);
return new_str;
}
/*
* returns a new string with _container on the end.
*/
const char* container_name(const char* name)
{
int length = strlen(name);
int length2 = strlen("_container");
char *new_str = (char*) malloc(sizeof(char)*(length+length2+1));
std::ostringstream total;
total << name << "_container";
strncpy(new_str, total.str().c_str(), length+length2+1);
return new_str;
}
/*
* takes the vector of global variables with also provides the parameters to the function.
*/
CCSTCompoundStatement* interface_init_function_body(std::vector<GlobalVariable*> globals)
{
// set each global variable to a parameter.
std::vector<CCSTDeclaration*> body_declarations_empty;
std::vector<CCSTStatement*> body_statements;
for(std::vector<GlobalVariable*>::iterator it = globals.begin(); it != globals.end(); it ++) {
GlobalVariable *g = *it;
body_statements.push_back(new CCSTAssignExpr(new CCSTPrimaryExprId(g->identifier())
, equals()
, new CCSTPrimaryExprId(parameter_name(g->identifier()))));
}
body_statements.push_back(new CCSTReturn(new CCSTInteger(0)));
return new CCSTCompoundStatement(body_declarations_empty, body_statements);
}
/*
* CAST code to declare a static variable
*/
CCSTDeclaration* declare_static_variable(Variable *gv)
{
std::vector<CCSTDecSpecifier*> specifier;
specifier.push_back(new CCSTStoClassSpecifier(static_t));
std::vector<CCSTDecSpecifier*> type_for_global = type(gv->type());
specifier.insert(specifier.end(), type_for_global.begin(), type_for_global.end());
std::vector<CCSTInitDeclarator*> declarators;
declarators.push_back(new CCSTDeclarator( pointer( count_nested_pointer(gv->type())),
new CCSTDirectDecId(gv->identifier())));
return new CCSTDeclaration(specifier, declarators);
}
//??????????
CCSTPostFixExpr* access_variable(Variable *p)
{
if(p->accessor() == 0x0) {
......@@ -29,7 +144,7 @@ CCSTPostFixExpr* access_variable(Variable *p)
return new CCSTPostFixExprAccess(access_parameter(p->accessor()), accessor, p->identifier());
}
CCSTExDeclaration* construct_enum(File *f)
CCSTExDeclaration* construct_enum(Module *f)
{
const char* enum_name = "todo";
CCSTEnumeratorList *el = construct_enumlist(f->rpc_defs());
......@@ -42,7 +157,7 @@ CCSTExDeclaration* construct_enum(File *f)
return declaration;
}
const char* construct_enum_name()
const char* enum_name(const char *name)
{
return "todo";
}
......@@ -64,9 +179,9 @@ CCSTEnumeratorList* construct_enumlist(std::vector<Rpc *> rps)
return enum_list;
}
char* string_to_upper(char* str)
const char* string_to_upper(char* str)
{
char* ret = (char*) malloc((sizeof(str)+1)*sizeof(char));
const char* ret = (char*) malloc((sizeof(str)+1)*sizeof(char));
int i;
for(i = 0; i < sizeof(str); i ++)
{
......@@ -103,7 +218,7 @@ int count_nested_pointer(Type *p)
* int * const name
* int * volatile name
*/
CCSTPointer* create_pointer(int p_count)
CCSTPointer* pointer(int p_count)
{
if(p_count == 0)
return 0x0;
......@@ -111,7 +226,7 @@ CCSTPointer* create_pointer(int p_count)
if(p_count == 1)
return new CCSTPointer();
return new CCSTPointer(create_pointer(p_count - 1));
return new CCSTPointer(pointer(p_count - 1));
}
/* Creates a function definition
......@@ -130,7 +245,7 @@ CCSTFuncDef* function_definition(CCSTDeclaration* function_declaration, CCSTComp
return new CCSTFuncDef(function_declaration->specifier_, func, decs, body);
}
CCSTParamTypeList* create_parameter_list()
CCSTParamTypeList* parameter_list()
{
}
......@@ -145,7 +260,7 @@ CCSTDeclaration* function_declaration(Rpc* r)
std::vector<CCSTInitDeclarator*> func; // = new std::vector<CCSTInitDeclarator*>(); // pointer name, params
int pointer_count = count_nested_pointer(r->explicit_return_type());
CCSTPointer *p = create_pointer(pointer_count);
CCSTPointer *p = pointer(pointer_count);
CCSTDirectDecId *name = new CCSTDirectDecId(r->name());
CCSTParamTypeList *param_list = create_parameter_list();
......@@ -238,7 +353,7 @@ CCSTTypeName* type_cast(Type *t)
{
printf("type cast pointer\n");
// do pointer stuff....
pointers = new CCSTAbstDeclarator( create_pointer(count_nested_pointer(t)) , 0x0);
pointers = new CCSTAbstDeclarator( pointer(count_nested_pointer(t)) , 0x0);
PointerType *p = dynamic_cast<PointerType*>(t);
t = get_non_pointer_type(p); // get first non pointer, write function for this
......
#include "ccst.h"
#include "code_gen.h"
CCSTDeclaration* create_dispatch_function_declaration()
CCSTDeclaration* dispatch_function_declaration()
{
std::vector<CCSTDecSpecifier*>specifier;
specifier.push_back(new CCSTSimpleTypeSpecifier(void_t));
......@@ -14,7 +15,7 @@ CCSTDeclaration* create_dispatch_function_declaration()
return new CCSTDeclaration(specifier, decs);
}
CCSTCompoundStatement* create_dispatch_loop_body(std::vector<Rpc*> rps)
CCSTCompoundStatement* dispatch_loop_body(std::vector<Rpc*> rps)
{
std::vector<CCSTDeclaration*> decs_in_body;
......@@ -129,7 +130,7 @@ CCSTCompoundStatement* create_dispatch_loop_body(std::vector<Rpc*> rps)
/* body for a callee function
* does unmarshaling, real function call, etc
*/
CCSTCompoundStatement* create_callee_body(Rpc *r)
CCSTCompoundStatement* callee_body(Rpc *r)
{
// unmarshal parameters based on marshal data.
// which says where params are stored.
......@@ -147,7 +148,7 @@ CCSTCompoundStatement* create_callee_body(Rpc *r)
Parameter *p = (Parameter*) *it;
statements.push_back(unmarshal_parameter(p));
CCSTPrimaryExprId *t = new CCSTPrimaryExprId(p->name());
CCSTPrimaryExprId *t = new CCSTPrimaryExprId(p->identifier());
unmarshalled_args.push_back(t);
}
......@@ -195,7 +196,7 @@ CCSTCompoundStatement* create_callee_body(Rpc *r)
}
CCSTFile* generate_server_header(File *file)
CCSTFile* generate_server_header(Module *file)
{
// #ifndef
// #define
......@@ -203,23 +204,23 @@ CCSTFile* generate_server_header(File *file)
// enum-specifier: enum id
std::vector<CCSTExDeclaration*> definitions; // = new std::vector<CCSTExDeclaration*>();
// check if there are rpcs
if(!file->rpc_defs().empty())
if(!file->rpc_definitions().empty())
{
printf("rpc not empty\n");
definitions.push_back(construct_enum(file));
// function callee function declarations
std::vector<Rpc*> rpcs = file->rpc_defs();
std::vector<Rpc*> rpcs = file->rpc_definitions();
for(std::vector<Rpc*>::iterator it = rpcs.begin(); it != rpcs.end(); it ++)
{
definitions.push_back(construct_callee_declaration((Rpc*) *it));
definitions.push_back(callee_declaration((Rpc*) *it));
}
}
definitions.push_back(create_dispatch_function_declaration());
definitions.push_back(dispatch_function_declaration());
CCSTFile *c_file = new CCSTFile(definitions);
return c_file;
}
CCSTDeclaration* construct_callee_declaration(Rpc* r)
CCSTDeclaration* callee_declaration(Rpc* r)
{
std::vector<CCSTDecSpecifier*> specifier; // = new std::vector<CCSTDecSpecifier*>();
specifier.push_back(new CCSTSimpleTypeSpecifier(int_t));
......@@ -250,50 +251,87 @@ CCSTDeclaration* construct_callee_declaration(Rpc* r)
/* ServerCCSTSourceVisitor */
CCSTFile* generate_server_source(File *file)
/*
* generates the source file for the provided module/interface
*/
CCSTFile* generate_server_source(Module *m)
{
printf("In generate_server_source\n");
// <function-definition> is CCSTFuncDef
// CCSTDecSpecifier* is <type-specifier> is CCSTTypeSpecifier
// <declarator> is CCSTDeclarator
// <declaration> what is this for is CCSTDeclaration
// <compound-statement> is CCSTCompoundStatement
// CCSTDeclaration for body
// CCSTStatement for body
// see notes in notebook
/*
std::vector<CCSTExDeclaration*> defs;
CCSTFuncDef* exec_loop = create_exec_loop(file->rpc_defs());
defs.push_back(exec_loop);
return new CCSTFile(defs);
*/
std::vector<CCSTExDeclaration*> definitions;
// includes. todo
// globals.
std::vector<GlobalVariable*> globals_to_declare = m->globals();
for(std::vector<GlobalVariable*>::iterator it = globals_to_declare.begin(); it != globals_to_declare.end(); it ++) {
GlobalVariable *gv = *it;
definitions.push_back(declare_static_variable(gv));
}
// type definitions
// container structs
// also typedefs.
std::map<std::string, Type*> module_type_definitions = m->module_scope()->type_definitions();
for(std::map<std::string, Type*>::iterator it = module_type_definitions.begin(); it != module_type_definitions.end(); it ++) {
Type* t = it->second;
switch (t->num()) {
case 1: // typedef
{
/*
definitions.push_back(typedef_declaration(t));
*/