Skip to content

Commit

Permalink
cmake with llvm target
Browse files Browse the repository at this point in the history
  • Loading branch information
thaisacs committed Nov 11, 2018
1 parent 988b33b commit 4363a26
Show file tree
Hide file tree
Showing 15 changed files with 451 additions and 297 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ add_executable(
${BISON_MyParser_OUTPUTS}
${FLEX_MyScanner_OUTPUTS})

llvm_map_components_to_libnames(llvm_libs support core irreader)
#llvm_map_components_to_libnames(llvm_libs bitreader core support)

target_link_libraries(grcc ${llvm_libs})
link_directories(/usr/lib/llvm-7/lib)

target_link_libraries(grcc PUBLIC "LLVM-7")
Binary file not shown.
39 changes: 31 additions & 8 deletions includes/AST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ namespace grc {
llvm::Value* codegen() override;
void toPrint(std::ofstream&) override;
};

/*
class NumbersExprAST : public ExprAST {
std::vector<int> Numbers;
public:
NumbersExprAST() {}
void addNumber(int Number);
llvm::Value* codegen() override;
void toPrint(std::ofstream&) override;
};
*/
class BooleanExprAST: public ExprAST {
bool Bool;
public:
Expand All @@ -46,7 +55,23 @@ namespace grc {
llvm::Value* codegen() override;
void toPrint(std::ofstream&) override;
};
/*
class ReadExprAST: public ExprAST {
std::string Name;
public:
ReadExprAST(cont std::string &Name) : Name(Name);
llvm::Value* codegen() override;
void toPrint(std::ofstream&) override;
};
class WriteExprAST: public ExprAST {
std::string Name;
public:
WriteExprAST(cont std::string &Name) : Name(Name);
llvm::Value* codegen() override;
void toPrint(std::ofstream&) override;
};
*/
class CallExprAST : public ExprAST {
std::string Callee;
std::vector<std::unique_ptr<ExprAST>> Args;
Expand Down Expand Up @@ -107,23 +132,21 @@ namespace grc {
void toPrint(std::ofstream&) override;
};

class Variable {
class Var {
std::string Name;
std::unique_ptr<ExprAST> Expr;
bool isArray;
public:
Variable(const std::string Name, std::unique_ptr<ExprAST> Expr, bool isArray) :
Name(Name), Expr(std::move(Expr)), isArray(isArray) {}
Var(const std::string Name, std::unique_ptr<ExprAST> Expr) :
Name(Name), Expr(std::move(Expr)) {}
void toPrint(std::ofstream&);
};

class VarExprAST : public ExprAST {
std::vector<std::unique_ptr<Variable>> Vars;
std::vector<std::unique_ptr<Var>> Vars;
std::unique_ptr<Type> PrimitiveType;
public:
VarExprAST() {}
void addVar(std::unique_ptr<Variable>);
void setType(std::unique_ptr<Type>);
void addVar(std::unique_ptr<Var>);
llvm::Value* codegen() override;
void toPrint(std::ofstream&) override;
};
Expand Down
2 changes: 0 additions & 2 deletions includes/Scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace grc {
void initializeScope();
bool insert(const std::string&, std::shared_ptr<Symbol>);
std::shared_ptr<Symbol> findVariableSymbol(const std::string&);
void setVariableValue(const std::string&, llvm::Value*);
llvm::Value* getVariableValue(const std::string&);
void finalizeScope();
void toPrint(std::ofstream&);
};
Expand Down
55 changes: 31 additions & 24 deletions includes/SymbolTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,62 @@
#include <fstream>

namespace grc {
enum SymbolType {VARIABLE, PROCEDURE, FUNCTION};
enum PrimitiveType {INT, BOOL, STRING};
enum SymbolType {Variable, Procedure, Function};
enum BasicType {Int, Bool, String, Undefined};

class Type {
PrimitiveType PT;
class PrimitiveType {
BasicType BT;
int Size;
public:
Type(PrimitiveType PT, int Size) : PT(PT), Size(Size) {}
Type* copy();
PrimitiveType getPrimitiveType() { return PT; }
PrimitiveType(BasicType BT, int Size) : BT(BT), Size(Size) {}
BasicType getBasicType() { return BT; }
int getSize() { return Size; }
void toPrint(std::ofstream&);
};

class Type {
std::shared_ptr<PrimitiveType> PT;
bool isArray;
public:
Type(std::shared_ptr<PrimitiveType> PT, bool isArray) :
PT(PT), isArray(isArray) {}
std::shared_ptr<PrimitiveType> getPrimitiveType() { return PT; }
bool getIsArray() { return isArray; }
void toPrint(std::ofstream &File);
};

class Symbol {
public:
virtual ~Symbol() = default;
virtual SymbolType getType() = 0;
virtual std::shared_ptr<Type> getType() = 0;
virtual SymbolType getSymbolType() = 0;
virtual void toPrint(std::ofstream&) = 0;
virtual void setValue(llvm::Value*) {};
virtual llvm::Value* getValue() {};
};

class VariableSymbol : public Symbol {
std::unique_ptr<Type> T;
bool isArray;
llvm::Value* V;
std::shared_ptr<Type> T;
public:
VariableSymbol(std::unique_ptr<Type> T, bool isArray) :
T(std::move(T)), isArray(isArray), V(nullptr) {}
SymbolType getType() override { return VARIABLE; }
VariableSymbol(std::shared_ptr<Type> T) : T(T) {}
std::shared_ptr<Type> getType() override;
SymbolType getSymbolType() override;
void toPrint(std::ofstream&) override;
llvm::Value* getValue() { return V; }
void setValue(llvm::Value* VarValue) { V = VarValue; }
};

class ProcedureSymbol : public Symbol {
std::shared_ptr<Type> T;
public:
ProcedureSymbol() {}
SymbolType getType() override { return PROCEDURE; }
ProcedureSymbol(std::shared_ptr<Type> T) : T(T) {}
std::shared_ptr<Type> getType() override;
SymbolType getSymbolType() override;
void toPrint(std::ofstream&) override;
};

class FunctionSymbol : public Symbol {
std::shared_ptr<Type> T;
public:
FunctionSymbol() {}
SymbolType getType() override { return FUNCTION; }
FunctionSymbol(std::shared_ptr<Type> T) : T(T) {}
std::shared_ptr<Type> getType() override;
SymbolType getSymbolType() override;
void toPrint(std::ofstream&) override;
};

Expand All @@ -66,8 +75,6 @@ namespace grc {
SymbolTable() {}
bool insert(const std::string&, std::shared_ptr<Symbol>);
std::shared_ptr<Symbol> findVariableSymbol(const std::string&);
void setVariableValue(const std::string, llvm::Value*);
llvm::Value* getVariableValue(const std::string&);
void toPrint(std::ofstream&);
};
}
9 changes: 4 additions & 5 deletions includes/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
struct Parameter {
std::string Name;
grc::Type* T;
bool isArray;
int Size;
};

struct Parameters{
Expand Down Expand Up @@ -43,6 +41,7 @@ grc::AssignAST* HandleAssign(uint8_t, const std::string);
grc::ExprAST* HandleCmdWhile(grc::ExprAST*, grc::ExprAST*);

grc::PrototypeAST* HandlePrototype(const std::string, Parameters*);
grc::PrototypeAST* HandlePrototype(const std::string, Parameters*, grc::Type*);
Parameter* HandleParameter(const std::string&, bool);
Parameters* HandleParameters();
void HandleParameters(Parameters*, Parameter*);
Expand All @@ -52,8 +51,8 @@ void HandleListOfParams(Parameters*, Parameters*, grc::Type*);
grc::ProcedureAST* HandleProcedure(grc::PrototypeAST*, grc::BlockAST*);

grc::VarExprAST* HandleListOfVar();
void HandleListOfVar(grc::VarExprAST*, grc::Variable*);
grc::Variable* HandleVar(const std::string&);
void HandleListOfVar(grc::VarExprAST*, grc::Var*);
grc::Var* HandleVar(const std::string&);
void HandleVarCmd(grc::VarExprAST*, grc::Type*);

grc::Variable* HandleVar(const std::string &Name, grc::ExprAST* Expr);
grc::Var* HandleVar(const std::string &Name, grc::ExprAST* Expr);
Binary file added src/.SymbolTable.cpp.swp
Binary file not shown.
Loading

0 comments on commit 4363a26

Please sign in to comment.