//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Gordon Henriksen and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalVariable.h"
#include "llvm/TypeSymbolTable.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/Support/MemoryBuffer.h"
#include <cassert>
+#include <cstdlib>
+#include <cstring>
using namespace llvm;
+/*===-- Error handling ----------------------------------------------------===*/
+
+void LLVMDisposeMessage(char *Message) {
+ free(Message);
+}
+
+
/*===-- Operations on modules ---------------------------------------------===*/
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
delete unwrap(M);
}
+/*--.. Data layout .........................................................--*/
+const char * LLVMGetDataLayout(LLVMModuleRef M) {
+ return unwrap(M)->getDataLayout().c_str();
+}
+
+void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
+ unwrap(M)->setDataLayout(Triple);
+}
+
+/*--.. Target triple .......................................................--*/
+const char * LLVMGetTarget(LLVMModuleRef M) {
+ return unwrap(M)->getTargetTriple().c_str();
+}
+
+void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
+ unwrap(M)->setTargetTriple(Triple);
+}
+
+/*--.. Type names ..........................................................--*/
int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
return unwrap(M)->addTypeName(Name, unwrap(Ty));
}
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
-LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
+LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
}
-LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType) {
- return wrap(PointerType::get(unwrap(ElementType)));
+LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
+ return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
}
-LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
+LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
return wrap(VectorType::get(unwrap(ElementType), ElementCount));
}
return unwrap<ArrayType>(ArrayTy)->getNumElements();
}
+unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
+ return unwrap<PointerType>(PointerTy)->getAddressSpace();
+}
+
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
return unwrap<VectorType>(VectorTy)->getNumElements();
}
return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
}
+static const fltSemantics &SemanticsForType(Type *Ty) {
+ assert(Ty->isFloatingPoint() && "Type is not floating point!");
+ if (Ty == Type::FloatTy)
+ return APFloat::IEEEsingle;
+ if (Ty == Type::DoubleTy)
+ return APFloat::IEEEdouble;
+ if (Ty == Type::X86_FP80Ty)
+ return APFloat::x87DoubleExtended;
+ if (Ty == Type::FP128Ty)
+ return APFloat::IEEEquad;
+ if (Ty == Type::PPC_FP128Ty)
+ return APFloat::PPCDoubleDouble;
+ return APFloat::Bogus;
+}
+
LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
- return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
+ APFloat APN(N);
+ APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
+ return wrap(ConstantFP::get(unwrap(RealTy), APN));
+}
+
+LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
+ return wrap(ConstantFP::get(unwrap(RealTy),
+ APFloat(SemanticsForType(unwrap(RealTy)), Text)));
}
/*--.. Operations on composite constants ...................................--*/
LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
return wrap(new GlobalVariable(unwrap(Ty), false,
- GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
+ GlobalValue::ExternalLinkage, 0, Name,
+ unwrap(M)));
}
LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
/*===-- Instruction builders ----------------------------------------------===*/
LLVMBuilderRef LLVMCreateBuilder() {
- return wrap(new LLVMBuilder());
+ return wrap(new LLVMFoldingBuilder());
}
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
return wrap(unwrap(B)->CreateUnreachable());
}
+void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
+ LLVMBasicBlockRef Dest) {
+ unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
+}
+
/*--.. Arithmetic ..........................................................--*/
LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
unwrap(Mask), Name));
}
+
+
+/*===-- Module providers --------------------------------------------------===*/
+
+LLVMModuleProviderRef
+LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
+ return wrap(new ExistingModuleProvider(unwrap(M)));
+}
+
+void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
+ delete unwrap(MP);
+}
+
+
+/*===-- Memory buffers ----------------------------------------------------===*/
+
+int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
+ LLVMMemoryBufferRef *OutMemBuf,
+ char **OutMessage) {
+ std::string Error;
+ if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
+ *OutMemBuf = wrap(MB);
+ return 0;
+ }
+
+ *OutMessage = strdup(Error.c_str());
+ return 1;
+}
+
+int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
+ char **OutMessage) {
+ if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
+ *OutMemBuf = wrap(MB);
+ return 0;
+ }
+
+ *OutMessage = strdup("stdin is empty.");
+ return 1;
+}
+
+void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
+ delete unwrap(MemBuf);
+}