Skip to content

Commit

Permalink
I can insert passes
Browse files Browse the repository at this point in the history
  • Loading branch information
bharrisau committed Feb 3, 2014
1 parent 8d7bd49 commit c4b3c4f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mk/rustllvm.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LLVM_EXTRA_INCDIRS_$(1)= -iquote $(S)src/llvm/include \
-iquote $$(CFG_LLVM_BUILD_DIR_$(1))/include
endif

RUSTLLVM_OBJS_CS_$(1) := $$(addprefix rustllvm/, RustWrapper.cpp PassWrapper.cpp)
RUSTLLVM_OBJS_CS_$(1) := $$(addprefix rustllvm/, RustWrapper.cpp PassWrapper.cpp RustStackPass.cpp)

RUSTLLVM_DEF_$(1) := $(1)/rustllvm/rustllvm$(CFG_DEF_SUFFIX_$(1))

Expand Down
18 changes: 16 additions & 2 deletions src/rustllvm/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ LLVMInitializePasses() {
initializeInstCombine(Registry);
initializeInstrumentation(Registry);
initializeTarget(Registry);

// Initialize our pass
PassInfo *PI = new PassInfo("Rust Stack Safety Pass", "rustsafestack", & RustSafeStack::ID,
PassInfo::NormalCtor_t(callDefaultCtor<RustSafeStack>), false, false);
Registry.registerPass(*PI, true);
}

extern "C" bool
Expand Down Expand Up @@ -174,8 +179,17 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
formatted_raw_ostream FOS(OS);

unwrap(Target)->addPassesToEmitFile(*PM, FOS, FileType, false);
PM->run(*unwrap(M));
return true;

StringRef SR("rustsafestack");
PassRegistry *PR = PassRegistry::getPassRegistry();

const PassInfo *PI = PR->getPassInfo(SR);
if (PI) {
PM->add(PI->createPass());
PM->run(*unwrap(M));
return true;
}
return false;
}

extern "C" void
Expand Down
24 changes: 24 additions & 0 deletions src/rustllvm/RustStackPass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "rustllvm.h"

using namespace llvm;

void RustSafeStack::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addPreserved<MachineLoopInfo>();
AU.addPreserved<MachineDominatorTree>();

// Get pass ID for PEI
StringRef SR("prologepilog");
const void *pei_id = PassRegistry::getPassRegistry()->getPassInfo(SR)->getTypeInfo();
AU.addRequiredID(pei_id);

MachineFunctionPass::getAnalysisUsage(AU);
}

bool RustSafeStack::runOnMachineFunction(MachineFunction &Fn) {
errs() << "Rust stack safety running\n";
return false;
}

char RustSafeStack::ID = 0;
//static RegisterPass<RustSafeStack> X("rustsafestack", "Rust Stack Safety Pass", false, false);
15 changes: 15 additions & 0 deletions src/rustllvm/rustllvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#include "llvm-c/BitReader.h"
#include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Object.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineDominators.h"

// Used by RustMCJITMemoryManager::getPointerToNamedFunction()
// to get around glibc issues. See the function for more information.
Expand All @@ -61,3 +64,15 @@
#endif

extern const char* LLVMRustError;

namespace llvm {
class RustSafeStack : public MachineFunctionPass {
public:
static char ID;
RustSafeStack() : MachineFunctionPass(ID) {
//initializePEIPass(*PassRegistry::getPassRegistry());
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
bool runOnMachineFunction(MachineFunction &Fn);
};
}

0 comments on commit c4b3c4f

Please sign in to comment.