Skip to content

Instantly share code, notes, and snippets.

@PlloYNiiE
Created March 17, 2011 11:02
Show Gist options
  • Select an option

  • Save PlloYNiiE/874162 to your computer and use it in GitHub Desktop.

Select an option

Save PlloYNiiE/874162 to your computer and use it in GitHub Desktop.
f.cpp
#include "SieveStmtPrinter.h"
#include <string>
#include "collection.h"
#include <vector>
#include <sstream>
using namespace clang;
using namespace llvm;
int number = 0;
int orderNum = 0;
int intCount=0;
int level=0;
int need_literal = 0;
namespace SS {
void SieveStatementPrinter::Indent(int level, string str)
{
int i = 0;
while ( level > i++)
{
llvm::outs() << "\t";
}
//llvm::outs() << "-- Level " << level << " " ;
llvm::outs() << str;
llvm::outs() << "\n";
}
void SieveStatementPrinter::VisitSubTree(Stmt* s) {
//llvm::outs() << " VISIT SUB TREE " << endl;
if (s)
{
if (isa<CompoundStmt>(s))
level++;
// Dispatch the statement processing.
Visit(s);
// Visit children.
Stmt::child_iterator si = s->child_begin(), se = s->child_end();
for (; si != se; ++si)
VisitSubTree(*si);
if (isa<CompoundStmt>(s))
level--;
}
}
void SieveStatementPrinter::VisitCompoundStmt(CompoundStmt *S) {
// we ignore the Compound statements in switch() {}
if (S->size() > 0)
if (isa<SwitchCase>(*S->body_begin()))
return;
orderNum = ++number;
llvm::outs() << orderNum;
Indent(level, "Visiting Compound Statement");
//S->dump();
}
void SieveStatementPrinter::VisitSwitchCase(SwitchCase *S) {
orderNum = ++number;
llvm::outs() << orderNum;
Indent(level, "Visiting Switch Case");
//S->getNextSwitchCase()->dump();
}
void SieveStatementPrinter::VisitCaseStmt(CaseStmt *S) {
// S->dump();
// current_stmt = new Collection::Decision();
orderNum = ++number;
llvm::outs() << orderNum;
Indent(level, "Visiting Case Statement");
// stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitDefaultStmt(DefaultStmt *S) {
Indent(level, " Visiting Default Statement");
}
void SieveStatementPrinter::VisitLabelStmt(LabelStmt *S) {
Indent(level, " Visiting Label Statement\n");
}
void SieveStatementPrinter::VisitUnaryOperator(UnaryOperator *E) {
Indent(level, " Visiting Unary Operator");
}
void SieveStatementPrinter::VisitBinaryOperator(BinaryOperator *E)
{
Indent(level, " Visit Binary Operator");
if (E->isAssignmentOp())
{
if (DeclRefExpr* DRINIT = dyn_cast<DeclRefExpr>(E->getLHS()))
{
if (VarDecl *VDINIT = dyn_cast<VarDecl>(DRINIT->getDecl()))
{
Indent(level+1, "Var Name: " + VDINIT->getNameAsString());
Indent(level+1, "Var Type: " + VDINIT->getType().getAsString());
if(IntegerLiteral *ILINIT = dyn_cast<IntegerLiteral>(E->getRHS()))
{
APInt Value = ILINIT->getValue();
Indent(level+1, "Assigned Value: " + Value.toString(10, true));
}
}
}
}
}
void SieveStatementPrinter::VisitCallExpr(CallExpr* CE) {
orderNum = ++number;
llvm::outs() << orderNum;
Indent(level, "Visiting Call Expr");
}
void SieveStatementPrinter::VisitIfStmt(IfStmt *S) {
orderNum = ++number;
llvm::outs() << orderNum ;
Indent(level, "Visiting If Statement");
string if_var_name;
string if_cond_op;
string if_cond_rvalue;
BinaryOperator* BOCOND = dyn_cast<BinaryOperator>(S->getCond());
if (BOCOND->isComparisonOp() && BOCOND )
{
ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(BOCOND->getLHS());
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ICE->getSubExpr()))
{
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
{
if_var_name = VD->getNameAsString();
if_cond_op = BOCOND->getOpcodeStr();
if(IntegerLiteral *ILINIT2 = dyn_cast<IntegerLiteral>(BOCOND->getRHS()))
{
APInt Value = ILINIT2->getValue();
if_cond_rvalue = Value.toString(10, true);
}
}
}
else
{
llvm::outs() << "===== WTF\n";
}
}
else
{
llvm::outs() << "ThE OTHER.";
if (BOCOND->isLogicalOp())
{
llvm::outs() << "LOGIC OP!" << "\n";
}
}
Indent(level+1, "IF Variable : " + if_var_name);
Indent(level+1, "IF Condition : " + if_cond_op);
Indent(level+1, "IF RValue : " + if_cond_rvalue);
Collection::IfStmt* iftemp = new Collection::IfStmt();
iftemp->setDecisionVarName(if_var_name);
iftemp->setDecisionOperator(if_cond_op);
iftemp->setDecisionRValue(if_cond_rvalue);
iftemp->setDecisionType("if");
current_stmt = iftemp;
current_stmt->setType("if");
this->stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitSwitchStmt(SwitchStmt *S) {
// S->dump();
current_stmt = new Collection::Decision();
Indent(level, "Visiting Switch Statement");
//S->dump();
DefaultStmt *TheDefaultStmt = NULL;
for(SwitchCase *Case = S->getSwitchCaseList(); Case; Case = Case->getNextSwitchCase()) {
//// Has default statement
if (DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
TheDefaultStmt = DS;
Indent(level, "DEFAULT :: ");
} else {
CaseStmt *CS = cast<CaseStmt>(Case);
current_stmt= new Collection::CaseStmt();
current_stmt->setType("While");
Indent(level, "CASE :: ");
need_literal = 1;
}
}
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitWhileStmt(WhileStmt *S) {
string while_cond_var_name;
string while_cond_op;
string while_cond_value;
string while_inc_pre_post;
string while_cond_kind;
string while_cond_inc_op;
string while_inc_name;
string while_inc_value;
current_stmt = new Collection::WhileStmt();
current_stmt->setType("WhileStmt");
//llvm::outs() << "\n\nlevel = " << level <<"\n" ;
orderNum = ++number;
llvm::outs() << orderNum ;
Indent(level, "Visiting While Statement");
//llvm::outs() << " TYPE: " << current_stmt->getType() << "\n";
string temp;
BinaryOperator* BOCOND = dyn_cast<BinaryOperator>(S->getCond());
if (BOCOND->isComparisonOp() && BOCOND )
{
//llvm::outs() << "WHILE ====== COMPLARE =======\n";
ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(BOCOND->getLHS());
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ICE->getSubExpr()))
{
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
{
/*llvm::outs() << "WHILE Loop Variable (2) : "
<< VD->getNameAsString()
<< "\n";*/
while_cond_var_name = VD->getNameAsString();
temp = VD->getNameAsString();
// llvm::outs() << "WHILE Compare Operation: "
// << BOCOND->getOpcodeStr()
// << "\n";
while_cond_op = BOCOND->getOpcodeStr();
if(IntegerLiteral *ILINIT2 = dyn_cast<IntegerLiteral>(BOCOND->getRHS()))
{
APInt Value = ILINIT2->getValue();
while_cond_value = Value.toString(10, true);
// llvm::outs() << "WHILE Compare Value: "
// << ILINIT2->getValue().getSExtValue()
// << "\n" ; // get compare value
}
}
}
/// BODY FCK
Stmt* s = S->getBody();
// Visit children.
int i =0;
Stmt::child_iterator si = s->child_begin(), se = s->child_end();
for (; si != se; ++si)
{
if (UnaryOperator* UOINC = dyn_cast<UnaryOperator>(*si))
{
// llvm::outs() << " UNARY " << "\n";
// llvm::outs() << "Loop Operator: " << UOINC->getOpcodeStr(UOINC->getOpcode()) ;
//UOINC->dump();
while_cond_kind = "unary";
if (UOINC->isPrefix())
{
while_inc_pre_post = "prefix";
// llvm::outs() << " (PREFIX)" << "\n";
}
else if (UOINC->isPostfix())
{
while_inc_pre_post = "postfix";
// llvm::outs() << " (POST FIX)" << "\n";
}
if (DeclRefExpr* CAO_DR = dyn_cast<DeclRefExpr>(UOINC->getSubExpr()))
{
if (VarDecl *CAO_VD = dyn_cast<VarDecl>(CAO_DR->getDecl()))
{
string predicted_inc = CAO_VD->getNameAsString();
if (predicted_inc.compare(while_cond_var_name) == 0)
{
while_inc_name = predicted_inc;
// llvm::outs() << "(SURE); WHILE Increment Variable: "
// << CAO_VD->getNameAsString()
// << "\n";
}
}
}
}
if (BinaryOperator* B_OP = dyn_cast<BinaryOperator>(*si))
{
while_cond_kind = "binary";
// Increment with +=2, -=
if (B_OP->isCompoundAssignmentOp())
{
while_inc_pre_post = "compound_assign_op";
CompoundAssignOperator* CAO = dyn_cast<CompoundAssignOperator>(B_OP);
while_cond_inc_op = CAO->getOpcodeStr();
// llvm::outs() << "CAO Operator: " << CAO->getOpcodeStr() << "\n";
if (DeclRefExpr* CAO_DR = dyn_cast<DeclRefExpr>(CAO->getLHS()))
{
if (VarDecl *CAO_VD = dyn_cast<VarDecl>(CAO_DR->getDecl()))
{
string predicted_inc = CAO_VD->getNameAsString();
if (predicted_inc.compare(while_cond_var_name) == 0)
{
while_inc_name = predicted_inc;
if(IntegerLiteral *IL_CAO = dyn_cast<IntegerLiteral>(CAO->getRHS()))
{
APInt Value = IL_CAO->getValue();
while_inc_value = Value.toString(10, true);
// llvm::outs() << "SURE CAO Increment Value: "
// << IL_CAO->getValue().getSExtValue() << "\n" ; // get initial value
}
}
}
}
}
}
i++;
}
}
// Indent(level+1, "WHILE VAR TYPE: " + while_cond_var_type);
Indent(level+1, "WHILE COND VAR NAME: " + while_cond_var_name);
// Indent(level+1, "FOR INIT: " + for_var_init_value);
Indent(level+1, "WHILE COND OP: " + while_cond_op);
Indent(level+1, "WHILE COND VALUE: " + while_cond_value);
Indent(level+1, "While Inc : " + while_inc_name);
Indent(level+1, "While Inc Op : "+ while_cond_inc_op);
Indent(level+1, "While Inc Value : "+ while_inc_value);
Indent(level+1, "While Inc Type : " + while_inc_pre_post);
Indent(level+1, "While Inc Kind : " + while_cond_kind);
Collection::WhileStmt* whiletemp = new Collection::WhileStmt();
whiletemp->setLoopType("while");
whiletemp->setVarName(while_cond_var_name);
whiletemp->setLoopStart("-999");
whiletemp->setLoopEnd(while_cond_value);
whiletemp->setLoopConditionOperator(while_cond_op);
whiletemp->setLoopIncName(while_inc_name);
whiletemp->setLoopIncOperator(while_cond_inc_op);
whiletemp->setLoopIncAry(while_cond_kind);
whiletemp->setLoopIncValue(while_inc_value);
whiletemp->setLoopExpressionType(while_inc_pre_post);
current_stmt = whiletemp;
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitDoStmt(DoStmt *S) {
llvm::outs() << " Visiting Do Statement\n";
}
void SieveStatementPrinter::VisitForStmt(ForStmt *S) {
//S->dump();
orderNum = ++number;
llvm::outs() << orderNum ;
Indent(level, "Visiting For");
string for_var_name;
string for_var_init_value;
string for_var_type;
string for_cond_name;
string for_cond_op;
string for_cond_value;
string for_inc_op;
string for_inc_name;
string for_inc_pre_post; // postfix / prefix
string for_inc_kind; // unary / binary
string for_inc_value;
//------------------------------------------------ Loop Index ------------------------------------------------- //
if(S->getInit())
{
BinaryOperator* BOINIT = dyn_cast<BinaryOperator>(S->getInit());
if (BOINIT->isAssignmentOp())
{
if (DeclRefExpr* DRINIT = dyn_cast<DeclRefExpr>(BOINIT->getLHS()))
{
if (VarDecl *VDINIT = dyn_cast<VarDecl>(DRINIT->getDecl()))
{
for_var_name = VDINIT->getNameAsString();
for_var_type = VDINIT->getType().getAsString();
if(IntegerLiteral *ILINIT = dyn_cast<IntegerLiteral>(BOINIT->getRHS()))
{
APInt Value = ILINIT->getValue();
for_var_init_value = Value.toString(10, true);
//llvm::outs() << "XX Initial value : "+ Value.toString(10, true) << "\n" ; // get initial value
}
}
}
}
}
else
{
llvm::outs() << "not Assignment Operator\n" ;
}
//------------------------------------------------ Loop Conddtion ------------------------------------------------- //
BinaryOperator* BOCOND = dyn_cast<BinaryOperator>(S->getCond());
if (BOCOND->isComparisonOp() && BOCOND )
{
ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(BOCOND->getLHS());
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ICE->getSubExpr()))
{
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
{
for_cond_op = BOCOND->getOpcodeStr();
for_cond_name = VD->getNameAsString();
if(IntegerLiteral *ILINIT2 = dyn_cast<IntegerLiteral>(BOCOND->getRHS()))
{
APInt Value = ILINIT2->getValue();
for_cond_value = Value.toString(10, true);
}
}
}
}
//------------------------------- Loop Increment -------------------------------------------//
// Increment with ++ or --
if (UnaryOperator* UOINC = dyn_cast<UnaryOperator>(S->getInc()))
{
for_inc_kind = "unary";
//llvm::outs() << "Loop Operator: " << UOINC->getOpcodeStr(UOINC->getOpcode()) ;
for_inc_op = UOINC->getOpcodeStr(UOINC->getOpcode());
if (UOINC->isPrefix())
{
for_inc_pre_post = "prefix";
}
else if (UOINC->isPostfix())
{
for_inc_pre_post = "postfix";
}
if (DeclRefExpr* CAO_DR = dyn_cast<DeclRefExpr>(UOINC->getSubExpr()))
{
if (VarDecl *CAO_VD = dyn_cast<VarDecl>(CAO_DR->getDecl()))
{
for_inc_name.assign(CAO_VD->getNameAsString());
}
}
}
else
{
if (BinaryOperator* B_OP = dyn_cast<BinaryOperator>(S->getInc()))
{
// Increment with +=2, -=
if (B_OP->isCompoundAssignmentOp())
{
for_inc_pre_post = "binary";
for_inc_kind = "compound_assign_op";
CompoundAssignOperator* CAO = dyn_cast<CompoundAssignOperator>(B_OP);
for_inc_op = CAO->getOpcodeStr();;
if (DeclRefExpr* CAO_DR = dyn_cast<DeclRefExpr>(CAO->getLHS()))
{
if (VarDecl *CAO_VD = dyn_cast<VarDecl>(CAO_DR->getDecl()))
{
for_inc_name = CAO_VD->getNameAsString();
if(IntegerLiteral *IL_CAO = dyn_cast<IntegerLiteral>(CAO->getRHS()))
{
APInt Value = IL_CAO->getValue();
for_inc_value = Value.toString(10, true);
}
}
}
}
// Increment with other ex: i = i+2
}
}
Indent(level+1, "FOR VAR TYPE: " + for_var_type);
Indent(level+1, "FOR VARNAME: " + for_var_name);
Indent(level+1, "FOR INIT: " + for_var_init_value);
Indent(level+1, "FOR CON OP: " + for_cond_op);
Indent(level+1, "FOR CON Value: " + for_cond_value);
Indent(level+1, "FOR Inc : " + for_inc_name);
Indent(level+1, "For Inc Op : "+ for_inc_op);
Indent(level+1, "For Inc Target : "+ for_inc_value);
Indent(level+1, "FOR Inc Type : " + for_inc_pre_post);
Indent(level+1, "FOR Inc Kind : " + for_inc_kind);
Collection::ForStmt* fortemp = new Collection::ForStmt();
fortemp->setLoopType("while");
fortemp->setVarName(for_var_name);
fortemp->setLoopStart(for_var_init_value);
fortemp->setLoopEnd(for_cond_value);
fortemp->setLoopConditionOperator(for_cond_op);
fortemp->setLoopIncName(for_inc_name);
fortemp->setLoopIncOperator(for_inc_op);
fortemp->setLoopIncAry(for_inc_kind);
fortemp->setLoopIncValue(for_inc_value);
fortemp->setLoopExpressionType(for_inc_pre_post);
current_stmt = fortemp;
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitIntegerLiteral(IntegerLiteral *E) {
APInt Value = E->getValue();
Indent(level, " Visiting Integer Literal:: "+ Value.toString(10, true));
if (need_literal>0)
{
Indent(level, " Using Literal");
current_stmt->addVarValue(Value.toString(10, true));
--need_literal;
}
}
void SieveStatementPrinter::VisitCharacterLiteral(CharacterLiteral *E) {
char Value = E->getValue();
stringstream ss;
string s ;
ss << Value;
ss >> s;
Indent(level, " Visiting Character Literal:: "+ s);
if (level>=2)
{
llvm::outs() << " Type: " << current_stmt->getType() <<"\n";
}
if (need_literal>0)
{
Indent(level, " Using Literal");
--need_literal;
}
//Collection::VarDecl *vardecl_tmp = (Collection::VarDecl*) current_stmt;
//vector<string> *val = vardecl_tmp->getVarValue();
//current_stmt->addVarValue(s);
}
void SieveStatementPrinter::VisitFloatingLiteral(FloatingLiteral* F) {
//current_stmt->setType("VarDecl");
APFloat Value = F->getValue();
stringstream ss;
string s ;
ss << Value.convertToDouble();
s = ss.str();
//ss << Value.convertToInteger(;
//ss >> s;
Indent(level, " Visit Floating Literal:: "+ s);
if (need_literal>0)
{
Indent(level, " Using Literal");
--need_literal;
}
}
void SieveStatementPrinter::EnterDecl(Decl *D, DeclStmt *S)
{
string temp;
switch (D->getKind())
{
case Decl::Var:
{
Collection::VarDecl* vartemp = new Collection::VarDecl();
VarDecl* VD = dyn_cast<VarDecl>(D);
string decl_type = VD->getType().getAsString();
string decl_var_name = VD->getDeclName().getAsString();
vartemp->setType("VarDecl");
vartemp->setVarName(decl_var_name);
vartemp->setVarType(decl_type);
Indent(level+1, "DECL TYPE: " + decl_type);
Indent(level+1, "DECL NAME: " + decl_var_name);
need_literal = 1;
if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(VD->getType()))
{
Indent(level+1, "Array Size: " + CAT->getSize().toString(10, true));
need_literal = CAT->getSize().getSExtValue();
}
current_stmt = vartemp;
stmts_storage.push_back(current_stmt);
break;
}
default:
{
llvm::outs() << D->getDeclKindName() << " not yet handled\n";
break;
}
} // end switch
}
void SieveStatementPrinter::VisitDeclStmt(DeclStmt *S) {
orderNum = ++number;
llvm::outs() << orderNum;
Indent(level, "Visiting Decl Statement");
if (S->isSingleDecl())
{
Decl *D = S->getSingleDecl();
EnterDecl(D, S);
}
else
{
for (DeclStmt::decl_iterator it = S->decl_begin(), ie = S->decl_end();
it != ie; it++)
{
Decl *D = *it;
// extend the block for every member except the first
if (it != S->decl_begin())
{
EnterDecl(D, S);
}
}
}
}
void SieveStatementPrinter::VisitDeclRefExpr(DeclRefExpr *E)
{
}
void SieveStatementPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
{
Indent(level, " Visiting ArraySubscriptExpr Statement");
}
void SieveStatementPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *CAO) {
orderNum = ++number;
llvm::outs() <<orderNum ;
Indent(level, " Visiting CompoundAssign Operator");
}
void SieveStatementPrinter::VisitConditionalOperator(ConditionalOperator *E) {
Indent(level, " Visiting Conditional Operator");
}
void SieveStatementPrinter::VisitStmtExpr(StmtExpr *E) {
CompoundStmt *CS = E->getSubStmt();
/* Visit the last one in the compound statement.
* Thus we'll get the load of it.
*/
VisitSubTree(CS->body_back());
orderNum = ++number;
level--;
llvm::outs() << orderNum;
Indent(level, "Visiting Stmt Expr\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment