Skip to content

Commit

Permalink
Adding LLVM IR Code for Callout Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpionhiccup committed Nov 18, 2015
1 parent f4e68cd commit ffa0c97
Show file tree
Hide file tree
Showing 16 changed files with 546 additions and 455 deletions.
10 changes: 7 additions & 3 deletions src/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void Args::evaluate(Visitor* visitor){
visitor->visit(this);
};

Value * Args::GenCode(Visitor* visitor){
Type * Args::GenCode(Visitor* visitor){
return visitor->CodeGen(this);
};

Expand Down Expand Up @@ -210,15 +210,15 @@ void CharLiteral::evaluate(Visitor* visitor){
visitor->visit(this);
}

Value * CharLiteral::GenCode(Visitor* visitor){
Type * CharLiteral::GenCode(Visitor* visitor){
return visitor->CodeGen(this);
}

void StringLiteral::evaluate(Visitor* visitor){
visitor->visit(this);
}

Value * StringLiteral::GenCode(Visitor* visitor){
Type * StringLiteral::GenCode(Visitor* visitor){
return visitor->CodeGen(this);
}

Expand All @@ -232,4 +232,8 @@ Type * BooleanType::GenCode(Visitor* visitor){

Type * LangType::GenCode(Visitor* visitor){
return visitor->CodeGen(this);
}

Type * ListExpressionRight::GenCode(Visitor* visitor){
return visitor->CodeGen(this);
}
7 changes: 4 additions & 3 deletions src/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Args: public ASTnode{
}
Args(){};
void evaluate(Visitor* visitor);
Value * GenCode(Visitor* visitor);
Type * GenCode(Visitor* visitor);
string getLiteral(){
return this->str;
}
Expand All @@ -140,6 +140,7 @@ class ListExpressionRight: public Args{
ListExpressionRight(list<ExpressionRight*> *expressionRight1){
this->expressionRight=expressionRight1;
}
Type * GenCode(Visitor* visitor);
};


Expand Down Expand Up @@ -286,7 +287,7 @@ class CharLiteral: public Args {
this->charLiteral=charLiteral1;
}
void evaluate(Visitor* visitor);
Value * GenCode(Visitor* visitor);
Type * GenCode(Visitor* visitor);
string getLiteral(){
return this->charLiteral;
}
Expand All @@ -300,7 +301,7 @@ class StringLiteral: public Args{
this->stringLiteral=stringLiteral1;
}
void evaluate(Visitor* visitor);
Value * GenCode(Visitor* visitor);
Type * GenCode(Visitor* visitor);
string getLiteral(){
return this->stringLiteral;
}
Expand Down
61 changes: 50 additions & 11 deletions src/Visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Visitor::~Visitor(){

}

static void printLevel(int depth){
for(int i=0;i<depth-1;i++){
cout<<'\t';
}
}

void printDebug(string str){
if (version==0){
cout<<str<<"\n";
Expand Down Expand Up @@ -249,16 +255,47 @@ void Visitor::visit(CalloutStatement* calloutStatement){
}

Value * Visitor::CodeGen(CalloutStatement* calloutStatement){
Value * V = ConstantInt::get(getGlobalContext(), APInt(32,0));

printDebug("Inside CalloutStatement");

Value * V;

vector<Type*> argTypes;

for (list<Args *>::reverse_iterator it=calloutStatement->Argss->rbegin();
it!=calloutStatement->Argss->rend(); ++it){
(*it)->GenCode(this);
Type * type = static_cast<Type *>((*it)->GenCode(this));
argTypes.push_back(type);
}

return V;
FunctionType *ftype = FunctionType::get(
Type::getInt64Ty(getGlobalContext()),
argTypes, false);

Function *function = Function::Create(ftype,
GlobalValue::InternalLinkage,
calloutStatement->name.c_str(),
this->module);

BasicBlock *bblock = BasicBlock::Create(
getGlobalContext(), "entry", function, 0);
Builder.SetInsertPoint(bblock);
this->pushBlock(bblock);

for (list<Args *>::reverse_iterator it=calloutStatement->Argss->rbegin();
it!=calloutStatement->Argss->rend(); ++it){

AllocaInst *alloc = new AllocaInst(
static_cast<Type *>((*it)->GenCode(this)),
(*it)->getLiteral(),
this->currentBlock());
this->locals()[(*it)->getLiteral()] = alloc;

//(*it)->GenCode(this);
}

this->popBlock();

return function;
}

void Visitor::visit(AssignmentStatement* assignmentStatement){
Expand Down Expand Up @@ -309,15 +346,17 @@ void Visitor::visit(Args* Args){
fprintf(XML_fp, "</Args>\n");
}

Value * Visitor::CodeGen(Args* args){
Value * V = ConstantInt::get(getGlobalContext(), APInt(32,0));
Type * Visitor::CodeGen(Args* args){

string msg="Inside Args ";
msg.append(args->getLiteral());

printDebug(msg);

return V;
return ConstantDataArray::getString(getGlobalContext(),
"args",
false)->getType();

}

void Visitor::visit(RUnaryExpr* rUnaryExpr){
Expand Down Expand Up @@ -493,26 +532,26 @@ void Visitor::visit(CharLiteral* charLiteral){
fprintf(XML_fp, "<character value=\'%s\'' />\n", charLiteral->getLiteral().c_str());
};

Value * Visitor::CodeGen(CharLiteral* charLiteral){
/*Value * Visitor::CodeGen(CharLiteral* charLiteral){
Value * V = ConstantInt::get(getGlobalContext(), APInt(32,0));
printDebug("Inside CharLiteral");
return V;
}
}*/


void Visitor::visit(StringLiteral* stringLiteral){
fprintf(XML_fp, "<string value=%s />\n", stringLiteral->getLiteral().c_str());
};

Value * Visitor::CodeGen(StringLiteral* stringLiteral){
/*Value * Visitor::CodeGen(StringLiteral* stringLiteral){
Value * V = ConstantInt::get(getGlobalContext(), APInt(32,0));
printDebug("Inside StringLiteral");
return V;
}
}*/

void Visitor::visit(Expression* expr){
fprintf(XML_fp, "<expr>\n");
Expand Down
22 changes: 19 additions & 3 deletions src/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class Visitor{
module = new Module("main", getGlobalContext());
}

Value * CodeGen(Args* args);
Type * CodeGen(Args* args);
Value * CodeGen(ASTProgram* aSTProgram);
Value * CodeGen(StringLiteral* stringLiteral);
Value * CodeGen(CharLiteral* charLiteral);
//Value * CodeGen(StringLiteral* stringLiteral);
//Value * CodeGen(CharLiteral* charLiteral);
Value * CodeGen(BinaryExpr* expr, Type * type);
Value * CodeGen(Expression* expr, Type * type);
Value * CodeGen(Bool* bool_obj);
Expand Down Expand Up @@ -97,6 +97,22 @@ class Visitor{
return Type::getVoidTy(getGlobalContext());
};

static Type * CodeGen(CharLiteral* charLiteral){
return ConstantDataArray::getString(getGlobalContext(),
charLiteral->getLiteral(),
false)->getType();
};

static Type * CodeGen(StringLiteral * stringLiteral){
return ConstantDataArray::getString(getGlobalContext(),
stringLiteral->getLiteral(),
false)->getType();
};

static Type * CodeGen(ListExpressionRight* listExpressionRight){
return Type::getInt64Ty(getGlobalContext());
};

std::map<std::string, Value*>& locals();

BasicBlock *currentBlock();
Expand Down
5 changes: 2 additions & 3 deletions src/XML_visitor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<binary_expression type="+" >
<binary_expression type="*" >
<integer value="6" />
<integer value="2" />
<location id="E" />
</binary_expression>
<binary_expression type="/" >
<integer value="3" />
Expand All @@ -36,12 +36,11 @@
<integer value="1" />
</assignment>
<callout function="printf">
<string value="%d%d%s" />
<Args>
</Args>
<Args>
</Args>
<string value="asf" />
<string value="dsa" />
</callout>
</statement_declarations>
</program>
2 changes: 1 addition & 1 deletion src/bison_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SUBTRACTION ENCOUNTERED
ASSIGNMENT OPERATION ENCOUNTERED
LOCATION ENCOUNTERED=F
INT ENCOUNTERED=6
INT ENCOUNTERED=2
LOCATION ENCOUNTERED=E
MULTIPLICATION ENCOUNTERED
INT ENCOUNTERED=3
INT ENCOUNTERED=2
Expand Down
5 changes: 2 additions & 3 deletions src/flex_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ INT: 23
INT: 2
ID: F
INT: 6
INT: 2
ID: E
INT: 3
INT: 2
ID: G
BOOLEAN: true
CALLOUT
STRING: "printf"
STRING: "%d%d%s"
INT: 1
INT: 2
STRING: "asf"
STRING: "dsa"
Loading

0 comments on commit ffa0c97

Please sign in to comment.