#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>
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();
}
GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
}
+LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
+ return wrap(unwrap(M)->getNamedGlobal(Name));
+}
+
void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
}
}
int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
- bool res = unwrap<GlobalVariable>(GlobalVar)->isConstant();
- fprintf(stderr, "LLVMIsConstantGlobal(0x%08x) = %s\n",
- (int) GlobalVar, res? "YES" : "NO");
return unwrap<GlobalVariable>(GlobalVar)->isConstant();
}
GlobalValue::ExternalLinkage, Name, unwrap(M)));
}
+LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
+ return wrap(unwrap(M)->getFunction(Name));
+}
+
void LLVMDeleteFunction(LLVMValueRef Fn) {
unwrap<Function>(Fn)->eraseFromParent();
}
return unwrap<Function>(Fn)->setCallingConv(CC);
}
+const char *LLVMGetCollector(LLVMValueRef Fn) {
+ Function *F = unwrap<Function>(Fn);
+ return F->hasCollector()? F->getCollector() : 0;
+}
+
+void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
+ Function *F = unwrap<Function>(Fn);
+ if (Coll)
+ F->setCollector(Coll);
+ else
+ F->clearCollector();
+}
+
/*--.. Operations on basic blocks ..........................................--*/
LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
}
+/*--.. Operations on phi nodes .............................................--*/
+
+void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
+ LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
+ PHINode *PhiVal = unwrap<PHINode>(PhiNode);
+ for (unsigned I = 0; I != Count; ++I)
+ PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
+}
+
+unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
+ return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
+}
+
+LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
+ return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
+}
+
+LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
+ return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
+}
+
/*===-- Instruction builders ----------------------------------------------===*/
LLVMBuilderRef LLVMCreateBuilder() {
- return wrap(new LLVMBuilder());
+ return wrap(new LLVMFoldingBuilder());
}
void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
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);
+}