Commit 5f771595 authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan

support for new scoping stuff, lots of changes in parser

parent 8d2b8ff6
......@@ -6,11 +6,12 @@ Typedef::Typedef(const char* alias, Type* type)
this->alias_ = alias;
this->type_ = type; // need to allocate?
}
/*
Marshal_type* Typedef::accept(MarshalVisitor *worker, Registers *data)
{
return worker->visit(this, data);
}
*/
CCSTStatement* Typedef::accept(AllocateTypeVisitor *worker, Variable *v)
{
......@@ -32,14 +33,20 @@ int Typedef::num()
return 1;
}
VoidType::VoidType()
const char* Typedef::name()
{
return this->alias_;
}
VoidType::VoidType()
{
}
/*
Marshal_type* VoidType::accept(MarshalVisitor *worker, Registers *data)
{
return worker->visit(this, data);
}
*/
CCSTStatement* VoidType::accept(AllocateTypeVisitor *worker, Variable *v)
{
......@@ -51,6 +58,11 @@ int VoidType::num()
return 5;
}
const char* VoidType::name()
{
return "void";
}
IntegerType::IntegerType(PrimType type, bool un, int size)
{
this->type_ = type;
......@@ -58,10 +70,12 @@ IntegerType::IntegerType(PrimType type, bool un, int size)
this->size_ = size;
}
/*
Marshal_type* IntegerType::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
*/
CCSTStatement* IntegerType::accept(AllocateTypeVisitor *worker, Variable *v)
{
......@@ -83,6 +97,12 @@ int IntegerType::num()
return 2;
}
const char* IntegerType::name()
{
printf("todo integer type name function.\n");
return "";
}
PointerType::PointerType(Type* type)
{
this->type_ = type;
......@@ -98,31 +118,38 @@ Type* PointerType::type()
return this->type_;
}
/*
Marshal_type* PointerType::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
*/
CCSTStatement* PointerType::accept(AllocateTypeVisitor *worker, Variable *v)
{
return worker->visit(this, v);
}
ProjectionField::ProjectionField(bool in, bool out, bool alloc, bool bind, Type* field_type, const char* field_name, ProjectionType *container_projection)
const char* PointerType::name()
{
return this->type_->name();
}
ProjectionField::ProjectionField(bool in, bool out, bool alloc, bool bind, Type* field_type, const char* field_name, Variable *accessor)
{
this->in_ = in;
this->out_ = out;
this->alloc_ = alloc;
this->bind_ = bind;
this->out_ = out;
this->field_type_ = field_type;
this->field_name_ = field_name;
this->container_ = container_projection;
this->accessor_ = accessor;
}
/*
Marshal_type* ProjectionField::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
*/
Type* ProjectionField::type()
{
......@@ -164,10 +191,12 @@ ProjectionType::ProjectionType(const char* id, const char* real_type, std::vecto
this->id_ = id; this->real_type_ = real_type; this->fields_ = fields;
}
/*
Marshal_type* ProjectionType::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
*/
CCSTStatement* ProjectionType::accept(AllocateTypeVisitor *worker, Variable *v)
{
......@@ -195,16 +224,23 @@ int ProjectionType::num()
return 4;
}
const char* ProjectionType::name()
{
return this->id_;
}
Parameter::Parameter(Type* type, const char* name)
{
this->type_ = type;
this->name_ = name;
}
/*
Marshal_type* Parameter::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
*/
const char* Parameter::identifier()
{
......@@ -287,12 +323,12 @@ Variable* ReturnVariable::accessor()
return this->accessor_;
}
ImplicitReturnVariable::ImplicitReturnValue(Parameter *p)
ImplicitReturnVariable::ImplicitReturnVariable(Parameter *p)
{
this->p_ = p;
}
void ImplicitReturnValue::set_marshal_info(Marshal_type *mt)
void ImplicitReturnVariable::set_marshal_info(Marshal_type *mt)
{
this->marshal_info_ = mt;
}
......@@ -302,7 +338,7 @@ Marshal_type* ImplicitReturnVariable::marshal_info()
return this->marshal_info_;
}
void ImplicitReturnVariable::set_accesor(Variable *v)
void ImplicitReturnVariable::set_accessor(Variable *v)
{
this->accessor_ = v;
}
......@@ -331,7 +367,7 @@ Rpc::Rpc(ReturnVariable *return_value, char* name, std::vector<Parameter* > para
{
this->explicit_return_ = return_value;
this->name_ = name;
this->params_ = parameters;
this->parameters_ = parameters;
this->set_implicit_returns();
this->symbol_table_ = new SymbolTable();
......@@ -369,10 +405,12 @@ void Rpc::set_implicit_returns()
return name_;
}
/*
Marshal_type* Rpc::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
*/
const char* Rpc::callee_name()
{
......@@ -390,22 +428,6 @@ const char* Rpc::enum_name()
std::vector<Parameter*> Rpc::parameters()
{
return params_;
}
File::File(const char* verbatim, FileScope* fs, std::vector<Rpc* > rpc_definitions)
{
this->verbatim_ = verbatim;
this->scope_ = fs;
this->rpc_defs_ = rpc_definitions;
return parameters_;
}
Marshal_type* File::accept(MarshalVisitor* worker, Registers *data)
{
return worker->visit(this, data);
}
std::vector<Rpc*> File::rpc_defs()
{
return this->rpc_defs_;
}
......@@ -42,6 +42,25 @@ GlobalScope* GlobalScope::instance()
return instance_;
}
bool GlobalScope::insert(Rpc *r)
{
std::string temp(r->name());
std::pair<std::string, std::vector<Parameter*> > p (temp, r->parameters());
std::pair<std::map<std::pair<std::string, std::vector<Parameter*> >, Rpc*>::iterator, bool> ret;
ret = this->rpc_definitions_.insert(std::pair<std::pair<std::string, std::vector<Parameter*> >, Rpc*>(p, r));
return ret.second;
}
bool GlobalScope::contains(const char *symbol)
{
std::string temp(symbol);
if(this->type_definitions_.find(temp) == this->type_definitions_.end()) {
return false;
} else {
return true;
}
}
Type * GlobalScope::lookup(const char * sym, int* err)
{
std::string temp(sym);
......@@ -58,7 +77,7 @@ Type * GlobalScope::lookup(const char * sym, int* err)
}
}
int GlobalScope::insert(const char* sym, Type * value)
bool GlobalScope::insert(const char* sym, Type * value)
{
std::string temp(sym);
printf("insert %s\n",temp.c_str());
......@@ -68,6 +87,22 @@ int GlobalScope::insert(const char* sym, Type * value)
return ret.second;
}
void GlobalScope::set_outer_scope(LexicalScope *ls)
{
printf("error: attempt to set outer scope of global scope\n");
return;
}
void GlobalScope::add_inner_scope(LexicalScope *ls)
{
this->inner_scopes_.push_back(ls);
}
void GlobalScope::add_inner_scopes(std::vector<LexicalScope*> scopes)
{
printf("add inner scopes global scope todo\n");
}
/* -------------------------------------------------------------- */
LexicalScope::LexicalScope()
......@@ -75,11 +110,46 @@ LexicalScope::LexicalScope()
this->outer_scope_ = 0x0;
}
LexicalScope::LexicalScope(LexicalScope *outer_scope)
{
this->outer_scope_ = outer_scope;
}
bool LexicalScope::insert(Rpc *r)
{
std::string temp(r->name());
std::pair<std::string, std::vector<Parameter*> > p (temp, r->parameters());
std::pair<std::map<std::pair<std::string, std::vector<Parameter*> >, Rpc*>::iterator, bool> ret;
ret = this->rpc_definitions_.insert(std::pair<std::pair<std::string, std::vector<Parameter*> >, Rpc*>(p, r));
return ret.second;
}
bool LexicalScope::contains(const char *symbol)
{
std::string temp(symbol);
if(this->type_definitions_.find(temp) == this->type_definitions_.end()) {
if(this->outer_scope_ == 0x0) {
return false;
} else {
return this->outer_scope_->contains(symbol);
}
} else {
return true;
}
}
// is err needed if we just return null? when would null ever be valid?
Type* LexicalScope::lookup(const char *symbol, int *err)
{
std::string temp(symbol);
if(this->type_definitions_.find(temp) == this->type_definitions_.end()) {
return this->outer_scope_->lookup_symbol(symbol, err);
if(this->outer_scope_ == 0x0) {
*err = 0;
return 0x0;
} else {
return this->outer_scope_->lookup_symbol(symbol, err);
}
}
else {
*err = 1;
......@@ -87,13 +157,26 @@ Type* LexicalScope::lookup(const char *symbol, int *err)
}
}
int LexicalScope::insert(const char *symbol, Type *type)
bool LexicalScope::insert(const char *symbol, Type *type)
{
std::string temp(sym);
printf("insert %s\n",temp.c_str());
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));
return ret.second;
}
void LexicalScope::set_outer_scope(LexicalScope *ls)
{
this->outer_scope_ = ls;
}
void LexicalScope::add_inner_scope(LexicalScope *ls)
{
this->inner_scopes_.push_back(ls);
}
void LexicalScope::add_inner_scopes(std::vector<LexicalScope*> scopes)
{
printf("error add inner scopes lexical scope todo\n");
}
......@@ -7,9 +7,14 @@
#include <sstream>
#include <stdio.h>
#include "marshal_op.h"
#include "marshal_visitor.h"
#include "symbol_table.h"
class MarshalVisitor;
class TypeNameVisitor;
class AllocateTypeVisitor;
class Variable;
enum PrimType {pt_char_t, pt_short_t, pt_int_t, pt_long_t, pt_longlong_t, pt_capability_t};
enum type_k {};
......@@ -20,19 +25,25 @@ class ASTVisitor;
class Base
{
public:
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
};
class LexicalScope : public Base
{
LexicalScope *outer_scope_;
std::map<std::string, Type*> type_definitions_;
std::vector<Rpc*> rpc_definitions_; // rpc or function pointer
std::map<std::pair<std::string, std::vector<Parameter*> >, Rpc*> rpc_definitions_; // rpc or function pointer
std::vector<LexicalScope*> inner_scopes_;
public:
LexicalScope();
virtual Type* lookup(const char* sym, int* err);
virtual int insert(const char* sym, Type* type);
LexicalScope(LexicalScope *outer_scope);
virtual bool insert(Rpc *r);
virtual Type* lookup(const char *sym, int* err);
virtual bool insert(const char *sym, Type* type);
virtual bool contains(const char *symbol);
virtual void set_outer_scope(LexicalScope *ls);
virtual void add_inner_scope(LexicalScope *ls);
virtual void add_inner_scopes(std::vector<LexicalScope*> scopes);
};
class GlobalScope : public LexicalScope
......@@ -40,30 +51,35 @@ class GlobalScope : public LexicalScope
static GlobalScope *instance_;
LexicalScope *outer_scope_;
std::map<std::string, Type*> type_definitions_;
std::vector<Rpc*> rpc_definitions_; // rpc or function pointer
std::map<std::pair<std::string, std::vector<Parameter*> >, Rpc*> rpc_definitions_; // rpc or function pointer
std::vector<LexicalScope*> inner_scopes_;
public:
GlobalScope();
virtual bool insert(Rpc *r);
virtual Type* lookup(const char *symbol, int *err);
virtual int insert(const char *symbol, Type *type);
virtual bool insert(const char *symbol, Type *type);
virtual bool contains(const char *symbol);
virtual void set_outer_scope(LexicalScope *ls);
virtual void add_inner_scope(LexicalScope *ls);
virtual void add_inner_scopes(std::vector<LexicalScope*> scopes);
static GlobalScope* instance();
};
class Type : public Base
{
public:
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
virtual CCSTTypeName* accept(TypeNameVisitor *worker) = 0;
virtual CCSTStatement* accept(AllocateTypeVisitor *worker, Variable *v) = 0;
virtual int num() = 0;
virtual const char* name() = 0;
};
class Variable : public Base
{
public:
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
virtual Type* type() = 0;
virtual const char* identifier() = 0;
virtual void set_accessor(Variable *v) = 0;
......@@ -80,9 +96,10 @@ class Typedef : public Type
public:
Typedef(const char* alias, Type* type);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual CCSTTypeName* accept(TypeNameVisitor *worker);
virtual CCSTStatement* accept(AllocateTypeVisitor *worker, Variable *v);
virtual const char* name();
Type* type();
const char* alias();
virtual int num();
......@@ -93,9 +110,10 @@ class VoidType : public Type
{
public:
VoidType();
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual CCSTTypeName* accept(TypeNameVisitor *worker);
virtual CCSTStatement* accept(AllocateTypeVisitor *worker, Variable *v);
virtual const char* name();
virtual int num();
};
......@@ -107,9 +125,10 @@ class IntegerType : public Type
public:
IntegerType(PrimType type, bool un, int size);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual CCSTTypeName* accept(TypeNameVisitor *worker);
virtual CCSTStatement* accept(AllocateTypeVisitor *worker, Variable *v);
virtual const char* name();
PrimType int_type();
bool is_unsigned();
virtual int num();
......@@ -121,9 +140,10 @@ class PointerType : public Type
Type* type_;
public:
PointerType(Type* type);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual CCSTTypeName* accept(TypeNameVisitor *worker);
virtual CCSTStatement* accept(AllocateTypeVisitor *worker, Variable *v);
virtual const char* name();
Type* type();
virtual int num();
~PointerType(){printf("pointer type destructor\n");}
......@@ -138,9 +158,9 @@ class ProjectionField : public Variable //?
Variable *accessor_; //
public:
ProjectionField(bool in, bool out, bool alloc, bool bind, Type* field_type, const char* field_name, ProjectionType *container_projection);
ProjectionField(bool in, bool out, bool alloc, bool bind, Type* field_type, const char* field_name, Variable *container_projection);
~ProjectionField();
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual Type* type();
virtual Rpc* scope();
virtual const char* identifier();
......@@ -158,9 +178,10 @@ class ProjectionType : public Type // complex type
public:
ProjectionType(const char* id, const char* real_type, std::vector<ProjectionField*> fields);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual CCSTTypeName* accept(TypeNameVisitor *worker);
virtual CCSTStatement* accept(AllocateTypeVisitor *worker, Variable *v);
virtual const char* name();
const char* id();
const char* real_type();
std::vector<ProjectionField*> fields();
......@@ -184,7 +205,7 @@ class Parameter : public Variable
public:
Parameter(Type* type, const char* name);
~Parameter();
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
virtual Type* type();
virtual Rpc* scope();
void set_marshal_info(Marshal_type* mt); // ??????????????????????????????
......@@ -207,8 +228,8 @@ class ReturnVariable : public Variable
Rpc* function_;
public:
ReturnValue();
ReturnValue(Type* return_type);
ReturnVariable();
ReturnVariable(Type* return_type);
void set_marshal_info(Marshal_type *mt);
Marshal_type* marshal_info();
......@@ -227,7 +248,7 @@ class ImplicitReturnVariable : public ReturnVariable
Rpc* function_;
public:
ImplicitReturnValue(Parameter *p);
ImplicitReturnVariable(Parameter *p);
void set_marshal_info(Marshal_type *mt);
Marshal_type* marshal_info();
......@@ -242,8 +263,8 @@ class Rpc : public Base
{
const char* enum_name_;
SymbolTable *symbol_table_;
ReturnValue *explicit_return_;
std::vector<ImplicitReturnValue*> implicit_returns_;
ReturnVariable *explicit_return_;
std::vector<ImplicitReturnVariable*> implicit_returns_;
/* -------------- */
char* name_;
......@@ -251,24 +272,23 @@ class Rpc : public Base
std::vector<Parameter* > marshal_parameters_;
void set_implicit_returns();
public:
Rpc(ReturnValue *return_value, char* name, std::vector<Parameter* > parameters);
Rpc(ReturnVariable *return_value, char* name, std::vector<Parameter* > parameters);
char* name();
const char* enum_name();
const char* callee_name();
std::vector<Parameter*> parameters();
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
ReturnValue *return_value();
// virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
ReturnVariable *return_value();
SymbolTable* symbol_table();
};
class Module : public Base
{
const char *verbatim_;
// const char *verbatim_;
LexicalScope *module_scope_;
std::vector<Rpc*> rpc_defs_;
public:
File(const char* verbatim, LexicalScope* scope, std::vector<Rpc*> rpc_definitions);
std::vector<Rpc*> rpc_definitions();
Module(LexicalScope* scope);
std::vector<Rpc*> rpc_definitions();
};
class Project : public Base
......@@ -277,7 +297,7 @@ class Project : public Base
std::vector<Module*> project_modules_;
public:
Project(LexicalScope global, std::vector<Module*> modules);
Project(LexicalScope scope, std::vector<Module*> modules);
};
class TypeNameVisitor // generates CCSTTypeName for each type.
......@@ -290,8 +310,6 @@ class TypeNameVisitor // generates CCSTTypeName for each type.
CCSTTypeName* visit(ProjectionType *pt);
};
class AllocateTypeVisitor
{
Type* first_non_pointer(PointerType *pt);
......@@ -322,4 +340,14 @@ class AllocateVariableVisitor
CCSTStatement* visit(ImplicitReturnVariable *irv);
};
class AllocateRegisterVisitor
{
public:
AllocateRegisterVisitor();
CCSTStatement* visit(ProjectionField *pf);
CCSTStatement* visit(Parameter *p);
CCSTStatement* visit(ReturnVariable *rv);
CCSTStatement* visit(ImplicitReturnVariable *irv);
};
#endif
#ifndef _MARSHAL_
#define _MARSHAL_
#include "lcd_ast.h"
#include <string>
#include <sstream>
class File;
class ProjectionField;
class Rpc;
class Parameter;
class ProjectionType;
class IntegerType;
class PointerType;
class Typedef;
class VoidType;
class Marshal_type;
class Registers;
class FileScope;
class RootScope;
const char* ret_name_gen(char *name, int num);
class MarshalVisitor
{
public:
virtual Marshal_type* visit(File *file, Registers* data);
virtual Marshal_type* visit(ProjectionField *proj_field, Registers *data);
virtual Marshal_type* visit(Rpc *rpc, Registers *data);
virtual Marshal_type* visit(Parameter *parameter, Registers *data);
// Types
virtual Marshal_type* visit(ProjectionType *proj_type, Registers *data);
virtual Marshal_type* visit(IntegerType *t, Registers *data);
virtual Marshal_type* visit(PointerType *t, Registers *data);
virtual Marshal_type* visit(Typedef* t, Registers *data);
virtual Marshal_type* visit(VoidType *vt, Registers *data);