diff --git a/include/Core.ll b/include/Core.ll index 66fc1c0..7c3c0b7 100644 --- a/include/Core.ll +++ b/include/Core.ll @@ -350,6 +350,8 @@ declare %TObject* @callPrimitive(i8 %opcode, %TObjectArray* %args, i1* %primitiv %TContext* ; targetContext } +declare i32 @printf(i8* noalias nocapture, ...) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;; exception API ;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/include/jit.h b/include/jit.h index 10e17b1..1f45c68 100644 --- a/include/jit.h +++ b/include/jit.h @@ -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, diff --git a/src/MethodCompiler.cpp b/src/MethodCompiler.cpp index b9d60bc..98afcc9 100644 --- a/src/MethodCompiler.cpp +++ b/src/MethodCompiler.cpp @@ -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,