aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/RuntimeCheck.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CodeGen/RuntimeCheck.cc')
-rw-r--r--lib/CodeGen/RuntimeCheck.cc39
1 files changed, 23 insertions, 16 deletions
diff --git a/lib/CodeGen/RuntimeCheck.cc b/lib/CodeGen/RuntimeCheck.cc
index b1eb677..0515f1e 100644
--- a/lib/CodeGen/RuntimeCheck.cc
+++ b/lib/CodeGen/RuntimeCheck.cc
@@ -3,8 +3,8 @@
#include "cps/TypeSystem.h" // kEnumOrdinalBase
#include "llvm/IR/Function.h"
#include "llvm/IR/Type.h"
-#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalVariable.h"
using namespace llvm;
using namespace cps;
@@ -17,16 +17,21 @@ RuntimeCheck::RuntimeCheck(Module &M, LLVMContext &C, IRBuilder<> &B, ExceptionH
void RuntimeCheck::setupExternalFunctions() {
Type *PtrTy = PointerType::getUnqual(TheContext);
Type *I32 = Type::getInt32Ty(TheContext);
- Type *I64 = Type::getInt64Ty(TheContext);
- SnprintfFunc = TheModule.getOrInsertFunction(
- "snprintf", FunctionType::get(I32, {PtrTy, I64, PtrTy}, true));
- WriteFunc = TheModule.getOrInsertFunction(
- "write", FunctionType::get(I32, {I32, PtrTy, I64}, false));
- StrLenFunc = TheModule.getOrInsertFunction(
- "strlen", FunctionType::get(I64, {PtrTy}, false));
- ArrayType *BufTy = ArrayType::get(Type::getInt8Ty(TheContext), 1024);
- ErrBuf = new GlobalVariable(TheModule, BufTy, false, GlobalValue::InternalLinkage,
- ConstantAggregateZero::get(BufTy), "__cps_errbuf");
+ FprintfFunc = TheModule.getOrInsertFunction(
+ "fprintf", FunctionType::get(I32, {PtrTy, PtrTy}, true));
+ // The stderr FILE* is a platform-specific symbol; host and target match.
+#if defined(_WIN32)
+ StderrFunc = TheModule.getOrInsertFunction(
+ "__acrt_iob_func", FunctionType::get(PtrTy, {I32}, false));
+#else
+#if defined(__APPLE__)
+ const char *StderrName = "__stderrp";
+#else
+ const char *StderrName = "stderr";
+#endif
+ StderrVar = new GlobalVariable(TheModule, PtrTy, false, GlobalValue::ExternalLinkage,
+ nullptr, StderrName);
+#endif
DivZeroMsg = Builder.CreateGlobalString("[Fatal] line %d: Division by zero\n", "err_div_zero", 0, &TheModule);
OutOfBoundsMsg = Builder.CreateGlobalString("[Fatal] line %d: Array index out of bounds\n", "err_bounds", 0, &TheModule);
@@ -38,12 +43,14 @@ void RuntimeCheck::setupExternalFunctions() {
}
void RuntimeCheck::emitStderr(Value *Msg, ArrayRef<Value *> FmtArgs) {
- // Format into a fixed buffer, then write(2) so diagnostics bypass stdout.
- std::vector<Value *> Args = {ErrBuf, ConstantInt::get(Type::getInt64Ty(TheContext), 1024), Msg};
+#if defined(_WIN32)
+ Value *Stream = Builder.CreateCall(StderrFunc, {ConstantInt::get(Type::getInt32Ty(TheContext), 2)});
+#else
+ Value *Stream = Builder.CreateLoad(PointerType::getUnqual(TheContext), StderrVar);
+#endif
+ std::vector<Value *> Args = {Stream, Msg};
Args.insert(Args.end(), FmtArgs.begin(), FmtArgs.end());
- Builder.CreateCall(SnprintfFunc, Args);
- Value *Len = Builder.CreateCall(StrLenFunc, {ErrBuf});
- Builder.CreateCall(WriteFunc, {ConstantInt::get(Type::getInt32Ty(TheContext), 2), ErrBuf, Len});
+ Builder.CreateCall(FprintfFunc, Args);
}
void RuntimeCheck::emitCheck(Value *Condition, Value *Msg, int Line) {