Skip to content

Commit

Permalink
Adds MethodCompiler::insertTrace() for JIT code trace injection
Browse files Browse the repository at this point in the history
Issue: #17
Issue: #92
  • Loading branch information
0x7CFE committed Jul 17, 2016
1 parent 5263fe0 commit c9af814
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/Core.ll
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ declare %TObject* @callPrimitive(i8 %opcode, %TObjectArray* %args, i1* %primitiv
%TContext* ; targetContext
}

declare i32 @printf(i8* noalias nocapture, ...)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;; exception API ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
3 changes: 3 additions & 0 deletions include/jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ class MethodCompiler {

TStackObject allocateStackObject(llvm::IRBuilder<>& builder, uint32_t baseSize, uint32_t fieldsCount);

void insertTrace(TJITContext& jit, const char* message);
void insertTrace(TJITContext& jit, const char* message, llvm::Value* value);

MethodCompiler(
JITRuntime& runtime,
llvm::Module* JITModule,
Expand Down
22 changes: 22 additions & 0 deletions src/MethodCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ std::string to_string(const T& x) {
return ss.str();
}

static Constant* createStringConstant(Module& M, char const* str, Twine const& name) {
LLVMContext& ctx = getGlobalContext();
Constant* strConstant = ConstantDataArray::getString(ctx, str);
GlobalVariable* GVStr =
new GlobalVariable(M, strConstant->getType(), true,
GlobalValue::InternalLinkage, strConstant, name);
Constant* zero = Constant::getNullValue(IntegerType::getInt32Ty(ctx));
Constant* indices[] = {zero, zero};
Constant* strVal = ConstantExpr::getGetElementPtr(GVStr, indices, true);
return strVal;
}

void MethodCompiler::insertTrace(MethodCompiler::TJITContext& jit, const char* message) {
Value* const print = m_JITModule->getFunction("printf");
jit.builder->CreateCall(print, createStringConstant(*m_JITModule, message, "str."));
}

void MethodCompiler::insertTrace(MethodCompiler::TJITContext& jit, const char* message, llvm::Value* value) {
Value* const print = m_JITModule->getFunction("printf");
jit.builder->CreateCall2(print, createStringConstant(*m_JITModule, message, "str."), value);
}

MethodCompiler::MethodCompiler(
JITRuntime& runtime,
llvm::Module* JITModule,
Expand Down

0 comments on commit c9af814

Please sign in to comment.