Skip to content

Instantly share code, notes, and snippets.

@PlloYNiiE
Created March 17, 2011 07:48
Show Gist options
  • Select an option

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

Select an option

Save PlloYNiiE/873983 to your computer and use it in GitHub Desktop.
SieveStatementPrinter.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;
namespace SS {
void SieveStatementPrinter::Indent(int level, string str)
{
int i = 0;
while ( level > i++)
{
llvm::outs() << "\t";
}
llvm::outs() << "Level " << level << ":: " << 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) {
this->collection->order.push_back("Compound");
// 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) {
// this will actually make two things
// 1) STID(preg) -> LDID(arg) if postfix operator
// 2) do the side effect (ie. STID -> ADD -> 1, j)
//
// and in the VisitDeclRefExpr we
// LDID the argument (possibly from the preg we loaded earlier)
// E->dump();
this->collection->order.push_back("Compound");
//llvm::outs() << "LEVEL :: " << level << "\n";
Indent(level, "Visiting Unary Operator");
}
void SieveStatementPrinter::VisitBinaryOperator(BinaryOperator *E) {
Indent(level, "Visiting Binary Operator");
/*
//RhsDR->getDecl();
if (E->isAssignmentOp()) {
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->getLHS())) {
if (VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
if (VD->hasInit()) {
llvm::outs() << "----- HAS INIT" << "\n";
llvm::outs() << VD->getNameAsString() << "<<---->>" << VD->getType().getAsString() << "\n" ;
}
else {
llvm::outs() << "+++++ NO INIT" << "\n";
llvm::outs() << VD->getNameAsString() << "<<---->>" << VD->getType().getAsString() << "\n" ;
}
Expr* RHS = E->getRHS()->IgnoreParenCasts();
if (DeclRefExpr* RhsDR = dyn_cast<DeclRefExpr>(RHS))
{
llvm::outs() << " CASTED RHS \n";
if (VD == dyn_cast<VarDecl>(RhsDR->getDecl()))
{
llvm::outs() << " RhsDR == VD \n";
}
}
else
{
llvm::outs() << " CANT CAST RHS \n";
}
}
}
}
if (E->isAdditiveOp()) {
llvm::outs() << " ++++++ ADD ++++ \n";
}
if (E->isComparisonOp()) {
llvm::outs() << " ====== COMPARE ===== \n";
}
*/
}
void SieveStatementPrinter::VisitCallExpr(CallExpr* CE) {
orderNum = ++number;
llvm::outs() << orderNum;
Indent(level, "Visiting Call Expr");
// llvm::outs() << "CE: " << CE << "\n";
}
void SieveStatementPrinter::VisitIfStmt(IfStmt *S) {
/*
* if (i)
* printf();
*
* IF
* LDID 0 <2,3,i> T<4,.predef_I4,4>
* INTCONST 0 (0x0)
* NE
* THEN
* BLOCK
* CALL 126 <1,43,printf> # flags 0x7e
* END_BLOCK
* ELSE
* BLOCK
* END_BLOCK
*
*/
orderNum = ++number;
//llvm::outs() << "\n\nlevel = " << level <<"\n" ;
llvm::outs() << orderNum ;
Indent(level, "Visiting If Statement");
BinaryOperator* BOCOND = dyn_cast<BinaryOperator>(S->getCond());
///S->getElse()->dump();
if (BOCOND->isComparisonOp() && BOCOND )
{
llvm::outs() << "IF ====== Condition =======\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() << "IF Variable (2) : "
<< VD->getNameAsString()
<< "\n";
llvm::outs() << "IF Compare Operation: "
<< BOCOND->getOpcodeStr()
<< "\n";
if(IntegerLiteral *ILINIT2 = dyn_cast<IntegerLiteral>(BOCOND->getRHS()))
{
APInt Value = ILINIT2->getValue();
llvm::outs() << "IF Compare Value: "
<< ILINIT2->getValue().getSExtValue()
<< "\n" ; // get compare value
}
}
}
}
//current_stmt = new Collection::Decision();
//current_stmt->setType("If");
//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 :: ");
//TheDefaultStmt->dump();
// GotoWn = WhirlWnImpl::createGotoWn(labNum);
} else {
CaseStmt *CS = cast<CaseStmt>(Case);
current_stmt= new Collection::CaseStmt();
current_stmt->setType("While");
//current_stmt = case_stmt;
//CS->dump();
//CS->getLHS()->dump();
Indent(level, "CASE :: ");
}
}
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitWhileStmt(WhileStmt *S) {
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";
temp = VD->getNameAsString();
llvm::outs() << "WHILE Compare Operation: "
<< BOCOND->getOpcodeStr()
<< "\n";
if(IntegerLiteral *ILINIT2 = dyn_cast<IntegerLiteral>(BOCOND->getRHS()))
{
APInt Value = ILINIT2->getValue();
llvm::outs() << "WHILE Compare Value: "
<< ILINIT2->getValue().getSExtValue()
<< "\n" ; // get compare value
}
}
}
/// BODY FCK
//S->getBody()->dump();
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();
if (UOINC->isPrefix())
{
llvm::outs() << " (PREFIX)" << "\n";
}
else if (UOINC->isPostfix())
{
llvm::outs() << " (POST FIX)" << "\n";
}
if (DeclRefExpr* CAO_DR = dyn_cast<DeclRefExpr>(UOINC->getSubExpr()))
{
if (VarDecl *CAO_VD = dyn_cast<VarDecl>(CAO_DR->getDecl()))
{
llvm::outs() << "WHILE Increment Variable: "
<< CAO_VD->getNameAsString()
<< "\n";
}
}
}
if (BinaryOperator* B_OP = dyn_cast<BinaryOperator>(*si))
{
// Increment with +=2, -=
if (B_OP->isCompoundAssignmentOp())
{
CompoundAssignOperator* CAO = dyn_cast<CompoundAssignOperator>(B_OP);
//CAO->dump();
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()))
{
llvm::outs() << "CAO Increment Variable: "
<< CAO_VD->getNameAsString()
<< "\n";
if(IntegerLiteral *IL_CAO = dyn_cast<IntegerLiteral>(CAO->getRHS()))
{
APInt Value = IL_CAO->getValue();
llvm::outs() << "CAO Increment Value: " << IL_CAO->getValue().getSExtValue() << "\n" ; // get initial value
}
}
}
}
}
i++;
}
}
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitDoStmt(DoStmt *S) {
llvm::outs() << "\tVisiting Do Statement\n";
}
void SieveStatementPrinter::VisitForStmt(ForStmt *S) {
//S->dump();
orderNum = ++number;
llvm::outs() << orderNum ;
Indent(level, "Visiting For");
Collection::ForStmt *n = new Collection::ForStmt();
this->collection->order.push_back("For");
//n->setInc("++");
current_stmt = n;
current_stmt->setName("FORSTMT");
//------------------------------------------------ Loop Index ------------------------------------------------- //
if(S->getInit())
{
BinaryOperator* BOINIT = dyn_cast<BinaryOperator>(S->getInit());
if (BOINIT->isAssignmentOp())
{
if (DeclRefExpr* DRINIT = dyn_cast<DeclRefExpr>(BOINIT->getLHS()))
{
llvm::outs() << "Loop Variable(0) : "
<< DRINIT->getDecl()
<< "\n"; //get name (Loop index)
if(IntegerLiteral *ILINIT = dyn_cast<IntegerLiteral>(BOINIT->getRHS()))
{
APInt Value = ILINIT->getValue();
llvm::outs() << "Initial value : "
<< ILINIT->getValue().getSExtValue()
<< "\n" ; // get initial value
}
if (VarDecl *VDINIT = dyn_cast<VarDecl>(DRINIT->getDecl()))
{
llvm::outs() << "Loop Variable(1) : "
<< VDINIT->getNameAsString()
<< " Type: " << VDINIT->getType().getAsString()
<< "\n" ; // get name & get type
}
}
}
}
else
{
llvm::outs() << "not Assignment Operator\n" ;
}
//------------------------------------------------ Loop Conddtion ------------------------------------------------- //
BinaryOperator* BOCOND = dyn_cast<BinaryOperator>(S->getCond());
//BOCOND->dump();
if (BOCOND->isComparisonOp() && BOCOND )
{
llvm::outs() << "FOR ====== 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() << "FOR FORLoop Variable (2) : "
<< VD->getNameAsString()
<< "\n";
llvm::outs() << "FOR Compare Operation: "
<< BOCOND->getOpcodeStr()
<< "\n";
if(IntegerLiteral *ILINIT2 = dyn_cast<IntegerLiteral>(BOCOND->getRHS()))
{
APInt Value = ILINIT2->getValue();
llvm::outs() << "FOR Compare Value: "
<< ILINIT2->getValue().getSExtValue()
<< "\n" ; // get compare value
}
}
}
}
//------------------------------- Loop Increment -------------------------------------------//
llvm::outs() << "===== Loop Increment =====\n";
// BinaryOperator* BOINC = dyn_cast<BinaryOperator>(S->getInc());
// Increment with ++ or --
if (UnaryOperator* UOINC = dyn_cast<UnaryOperator>(S->getInc()))
{
llvm::outs() << "Loop Operator: " << UOINC->getOpcodeStr(UOINC->getOpcode()) ;
if (UOINC->isPrefix())
{
llvm::outs() << " (PREFIX)" << "\n";
}
else if (UOINC->isPostfix())
{
llvm::outs() << " (POST FIX)" << "\n";
}
}
else
{
if (BinaryOperator* B_OP = dyn_cast<BinaryOperator>(S->getInc()))
{
// Increment with +=2, -=
if (B_OP->isCompoundAssignmentOp())
{
CompoundAssignOperator* CAO = dyn_cast<CompoundAssignOperator>(B_OP);
//CAO->dump();
llvm::outs() << "Loop Operator: " << CAO->getOpcodeStr() << "\n";
if (DeclRefExpr* CAO_DR = dyn_cast<DeclRefExpr>(CAO->getLHS()))
{
if (VarDecl *CAO_VD = dyn_cast<VarDecl>(CAO_DR->getDecl()))
{
llvm::outs() << "Increment Variable: "
<< CAO_VD->getNameAsString()
<< "\n";
if(IntegerLiteral *IL_CAO = dyn_cast<IntegerLiteral>(CAO->getRHS()))
{
APInt Value = IL_CAO->getValue();
llvm::outs() << "Increment Value: " << IL_CAO->getValue().getSExtValue() << "\n" ; // get initial value
}
}
}
}
// Increment with other ex: i = i+2
}
}
/*if (BOINC->isCompoundAssignmentOp())
{
}
*/
/*
else
{
}
*/
//llvm::outs() << S->getInc()->getType().getAsString() << "\n";
//llvm::outs() << S->getInc()->getValueKind() << "\n";
//BinaryOperator *BO = dyn_cast<BinaryOperator>(S->getCond());
//llvm::outs() << "BO TYPE:: " << BO->getType().getAsString() << "\n";
//BO->getOpcode();
//llvm::outs() << BO->getOpcodeStr();
//llvm::outs() << S->getInc()->getType().getAsString() << "\n";
//llvm::outs() << "\n";
//S->dump();
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitIntegerLiteral(IntegerLiteral *E) {
APInt Value = E->getValue();
// intCount++;
// Collection::VarDecl *vardecl_tmp = (Collection::VarDecl*) current_stmt;
// vector<string> *val = vardecl_tmp->getVarValue();
// current_stmt->addVarValue(Value.toString(10, true));
Indent(level, "Visiting Integer Literal:: "+ Value.toString(10, true));
}
void SieveStatementPrinter::VisitCharacterLiteral(CharacterLiteral *E) {
char Value = E->getValue();
stringstream ss;
string s ;
ss << Value;
ss >> s;
Indent(level, "Visiting Character Literal:: "+ s);
//llvm::outs() << Value << "\n";
if (level>=2)
{
llvm::outs() << " Type: " << current_stmt->getType() <<"\n";
}
//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);
}
void SieveStatementPrinter::EnterDecl(Decl *D, DeclStmt *S) {
string temp;
switch (D->getKind()) {
default:
llvm::outs() << D->getDeclKindName() << " not yet handled\n";
break;
case Decl::Var:
{
VarDecl* VD = dyn_cast<VarDecl>(D);
/* if(VD->hasInit())
{ llvm::outs() << "-----EnterDecl hasInit-----\n";
Expr *InitialExpr = VD->getInit();
InitialExpr->dump();
if(IntegerLiteral *IL = dyn_cast<IntegerLiteral>(InitialExpr))
{
llvm::outs() << "\t integer value: \t" << IL->getValue().getSExtValue();
}
if(FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InitialExpr))
{
llvm::outs() << "\t float value: \t" << FL->getValue().convertToDouble();
}
if(CharacterLiteral *CL = dyn_cast<CharacterLiteral>(InitialExpr))
{
llvm::outs() << "\t Char value: \t" << CL->getValue();
}
if(ImplicitCastExpr *IM = dyn_cast<ImplicitCastExpr>(InitialExpr))
{
llvm::outs() << "******************" << IM->getSubExpr();
}
}*/
Indent(level, "D->getKind");
llvm::outs() << "\t type >> " << VD->getType().getAsString();
llvm::outs() << " :: " << VD->getDeclName().getAsString() << "\n";
break;
}
}
}
void SieveStatementPrinter::VisitDeclStmt(DeclStmt *S) {
current_stmt = new Collection::VarDecl();
this->collection->order.push_back("Dec");
struct VarStruct tmp;
tmp.name = "NAME";
tmp.type = "INT";
tmp.value = "3";
this->collection->VarDecl.push_back(tmp);
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);
}
}
}
stmts_storage.push_back(current_stmt);
}
void SieveStatementPrinter::VisitDeclRefExpr(DeclRefExpr *E) {
//current_stmt = new Collection::VarDecl();
Indent(level, "Visiting Decl Ref Expr");
//E->dump();
//llvm::outs() << "\tEnc DeclRef\n";
// stmts_storage.push_back(current_stmt);
//E->dump();
}
void SieveStatementPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Indent(level, "Visiting ArraySubscriptExpr Statement");
//llvm:outs() << "Indx:: " << (E->getIdx()) << "\n";
}
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");
// determine if we need to create implicit condition
BinaryOperator *BO = dyn_cast<BinaryOperator>(E->getCond());
if (BO && !BO->isAssignmentOp()) {
/* NOTHING */
} else {
llvm::outs() << "isAssignmentOp" << "\n" ;
}
}
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