aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/CodeGen/FunctionGen.cc2
-rw-r--r--lib/CodeGen/RuntimeCheck.cc39
2 files changed, 24 insertions, 17 deletions
diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc
index 4e70faf..d985bce 100644
--- a/lib/CodeGen/FunctionGen.cc
+++ b/lib/CodeGen/FunctionGen.cc
@@ -14,7 +14,7 @@ static bool isReservedRuntimeName(const std::string &Name) {
static const char *Reserved[] = {
"__cps_main", "printf", "scanf", "malloc", "free", "strlen", "memcpy",
"strcpy", "strcat", "toupper", "tolower", "sprintf", "snprintf", "strtol", "strtoll",
- "strtod", "memset", "exit", "write",
+ "strtod", "memset", "exit", "fprintf",
"fopen", "fclose", "fgetc", "ungetc", "fputs", "fputc",
"realloc", "strcmp", "__cxa_allocate_exception", "__cxa_throw",
"__cxa_begin_catch", "__cxa_end_catch", "__gxx_personality_v0",
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) {