Skip to content

Commit

Permalink
Partially ported to new llvm
Browse files Browse the repository at this point in the history
  • Loading branch information
kalibera committed Dec 20, 2016
1 parent 0811972 commit 20ea996
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 65 deletions.
9 changes: 7 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
HOST := $(shell hostname -s)

ifeq ($(HOST), prg)
LLVM := /home/tomas/work/opt/clang+llvm-3.6.1-x86_64-linux-gnu
CXX := g++-4.8
#LLVM := /home/tomas/work/opt/clang+llvm-3.6.1-x86_64-linux-gnu
LLVM := /home/tomas/work/opt/clang+llvm-3.8.1-x86_64-linux-gnu-ubuntu-16.04
#CXX := g++-4.8
CXX := g++

else ifeq ($(HOST), r-lnx400)
LLVM := /var/scratch/tomas/opt/llvm/clang+llvm-3.61-x86_64-fedora20
Expand All @@ -30,6 +32,9 @@ CPPFLAGS := $(shell $(LLVMC) --cppflags)
CXXFLAGS := $(shell $(LLVMC) --cxxflags) -O3 -g3 -MMD
#CXXFLAGS := $(shell $(LLVMC) --cxxflags) -O0 -g3 -MMD

# for GCC, which does not support this warning
CXXFLAGS := $(filter-out -Wcovered-switch-default, $(CXXFLAGS))

CLANG_LIBS := \
-Wl,--start-group \
-lclangAST \
Expand Down
85 changes: 34 additions & 51 deletions src/dofunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ bool ensuresArity(Function *fun) {
assert(nargs == 4);
Function::arg_iterator ai = fun->arg_begin();

Argument *callArg = ai++;
Argument *opArg = ai++;
Argument *argsArg = ai++;
Argument *envArg = ai++;
Argument *callArg = &*ai++;
Argument *opArg = &*ai++;
Argument *argsArg = &*ai++;
Argument *envArg = &*ai++;

map.insert({callArg, AVS_CALL});
map.insert({opArg, AVS_OP});
map.insert({argsArg, AVS_ARGS});
map.insert({envArg, AVS_ENV});

for(BasicBlock::iterator ii = bb->begin(), ie = bb->end(); ii != ie; ++ii) {
Instruction *in = ii;
Instruction *in = &*ii;

CallSite cs(in);
if (cs) {
Expand Down Expand Up @@ -413,69 +413,52 @@ bool isArgument(Value* var, Argument* argsArg) {
for(const_inst_iterator ii = inst_begin(*f), ie = inst_end(*f); ii != ie; ++ii) {
const Instruction *in = &*ii;

MDNode *mnode = NULL;
if (const DbgDeclareInst *ddi = dyn_cast<DbgDeclareInst>(in)) {
if (ddi->getAddress() == var) {
mnode = ddi->getVariable();
if (ddi->getVariable()->getName() == argsArg->getName()) return true;
}
} else if (const DbgValueInst *dvi = dyn_cast<DbgValueInst>(in)) {
if (dvi->getValue() == var) {
mnode = dvi->getVariable();
}
}
if (mnode) {

DIVariable lvar(mnode);
// FIXME: is this always reliable? Couldn't a value be re-declared?
if (lvar.getName() == argsArg->getName() /* && lvar.isInlinedFnArgument(f) */) {
// note it is not correct to require .isInlinedFnArgument to be true
// because it is not true when the argument variable is being overwritten
// e.g. by args = CDR(args)
// the semantics of isInlinedFnArgument is a bit iffy

return true;
if (dvi->getValue() == var) {
if (dvi->getVariable()->getName() == argsArg->getName()) return true;
}
}
}
}
}

return false;
}

// empty variable names means the name is unknown
std::string computeVarName(const Value *var) {
// copied from rchk with some mods (needed?)

std::string computeVarName(const Value *value) {
if (!value) return "";
const AllocaInst *var = dyn_cast<AllocaInst>(value);
// if (!var) return "NULL";
if (!var) return "";
std::string name = var->getName().str();
if (!name.empty()) {
return name;
}

if (isa<Argument>(var)) {
return name;
}

if (const AllocaInst *v = dyn_cast<AllocaInst>(var)) {
const Function *f = v->getParent()->getParent();
const Function *f = var->getParent()->getParent();

// there ought be a simpler way in LLVM, but it seems there is not
for(const_inst_iterator ii = inst_begin(*f), ie = inst_end(*f); ii != ie; ++ii) {
const Instruction *in = &*ii;
// there ought be a simpler way in LLVM, but it seems there is not
for(const_inst_iterator ii = inst_begin(*f), ie = inst_end(*f); ii != ie; ++ii) {
const Instruction *in = &*ii;

if (const DbgDeclareInst *ddi = dyn_cast<DbgDeclareInst>(in)) {
if (ddi->getAddress() == v) {
DIVariable dvar(ddi->getVariable());
return dvar.getName();
}
} else if (const DbgValueInst *dvi = dyn_cast<DbgValueInst>(in)) {
if (dvi->getValue() == v) {
DIVariable dvar(dvi->getVariable());
return dvar.getName();
}
if (const DbgDeclareInst *ddi = dyn_cast<DbgDeclareInst>(in)) {
if (ddi->getAddress() == var) {
return ddi->getVariable()->getName();
}
} else if (const DbgValueInst *dvi = dyn_cast<DbgValueInst>(in)) {
if (dvi->getValue() == var) {
return dvi->getVariable()->getName();
}
}
}
return "";
// return "<unnamed var: " + instructionAsString(var) + ">";
return "";
}

typedef std::map<const Value*, std::string> VarNamesTy;
Expand Down Expand Up @@ -503,7 +486,7 @@ bool getVarName(const Value *var, std::string& name, VarNamesTy& cache) {
bool getSourceLine(Instruction *inst, unsigned &line) {
const DebugLoc& debugLoc = inst->getDebugLoc();

if (debugLoc.isUnknown()) {
if (!debugLoc) {
return false;
}

Expand Down Expand Up @@ -729,10 +712,10 @@ DoFunctionInfo analyzeDoFunction(Function *fun, bool resolveListAccesses, bool r
assert(nargs == 4);
Function::arg_iterator ai = fun->arg_begin();

Argument *callArg = ai++;
Argument *opArg = ai++;
Argument *argsArg = ai++;
Argument *envArg = ai++;
Argument *callArg = &*ai++;
Argument *opArg = &*ai++;
Argument *argsArg = &*ai++;
Argument *envArg = &*ai++;

evmap.insert({callArg, ValueState(VSK_CALL)});
evmap.insert({opArg, ValueState(VSK_OP)});
Expand Down Expand Up @@ -801,7 +784,7 @@ DoFunctionInfo analyzeDoFunction(Function *fun, bool resolveListAccesses, bool r
if (DEBUG) s.dump();

for(BasicBlock::iterator ii = bb->begin(), ie = bb->end(); ii != ie; ++ii) {
Instruction *in = ii;
Instruction *in = &*ii;

// TODO: add support for *LENGTH, *length or args, and hence also integer guards
// TODO: add support for address taken
Expand Down
28 changes: 17 additions & 11 deletions src/einfo.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

#include "dofunc.h"
#include "ftable.h"

Expand Down Expand Up @@ -56,20 +56,18 @@ bool sourceLocation(const Instruction *in, std::string& path, unsigned& line) {
}
const DebugLoc& debugLoc = in->getDebugLoc();

if (debugLoc.isUnknown()) {
if (!debugLoc) {
path = "/unknown";
line = 0;
return false;
}

line = debugLoc.getLine();

DIScope scope(debugLoc.getScope());
if (scope) {
if (sys::path::is_absolute(scope.getFilename())) {
path = scope.getFilename().str();
if (DIScope *scope = dyn_cast<DIScope>(debugLoc.getScope())) {
if (sys::path::is_absolute(scope->getFilename())) {
path = scope->getFilename().str();
} else {
path = scope.getDirectory().str() + "/" + scope.getFilename().str();
path = scope->getDirectory().str() + "/" + scope->getFilename().str();
}
}
return true;
Expand All @@ -92,15 +90,14 @@ std::string funLocation(const Function *f) {
const Instruction *instWithDI = NULL;
for(Function::const_iterator bb = f->begin(), bbe = f->end(); !instWithDI && bb != bbe; ++bb) {
for(BasicBlock::const_iterator in = bb->begin(), ine = bb->end(); !instWithDI && in != ine; ++in) {
if (!in->getDebugLoc().isUnknown()) {
instWithDI = in;
if (in->getDebugLoc()) {
instWithDI = &*in;
}
}
}
return sourceLocation(instWithDI);
}


int main(int argc, char* argv[]) {

LLVMContext context;
Expand Down Expand Up @@ -169,6 +166,15 @@ int main(int argc, char* argv[]) {
(e.isSpecial() ? " SPECIAL" : " BUILTIN") << " " << (e.isPrimitive() ? "PRIMITIVE" : "INTERNAL");

errs() << " " << funLocation(fun) << "\n";

if (0) {
ResolvedListAccessesTy& listAccesses = nfo.listAccesses;
for(ResolvedListAccessesTy::const_iterator li = listAccesses.begin(), le = listAccesses.end(); li != le; ++li) {
ListAccess& la = const_cast<ListAccess&>(li->first);
unsigned aindex = li->second;
errs() << " " << la.str() << " arg " << std::to_string(aindex) << "\n";
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ftable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool readFunctionTable(Module *m, FunctionTableTy& tbl) {
return true;
}

bool dumpFunctions(FunctionTableTy& funtab, std::string type, bool isInternal, bool isPrimitive, bool isBuiltin, bool isSpecial) {
void dumpFunctions(FunctionTableTy& funtab, std::string type, bool isInternal, bool isPrimitive, bool isBuiltin, bool isSpecial) {
unsigned cnt;

cnt = 0;
Expand Down

0 comments on commit 20ea996

Please sign in to comment.