1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/TypeSymbolTable.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/Support/MemoryBuffer.h"
29 /*===-- Error handling ----------------------------------------------------===*/
31 void LLVMDisposeMessage(char *Message) {
36 /*===-- Operations on modules ---------------------------------------------===*/
38 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
39 return wrap(new Module(ModuleID));
42 void LLVMDisposeModule(LLVMModuleRef M) {
46 /*--.. Data layout .........................................................--*/
47 const char * LLVMGetDataLayout(LLVMModuleRef M) {
48 return unwrap(M)->getDataLayout().c_str();
51 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
52 unwrap(M)->setDataLayout(Triple);
55 /*--.. Target triple .......................................................--*/
56 const char * LLVMGetTarget(LLVMModuleRef M) {
57 return unwrap(M)->getTargetTriple().c_str();
60 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
61 unwrap(M)->setTargetTriple(Triple);
64 /*--.. Type names ..........................................................--*/
65 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
66 return unwrap(M)->addTypeName(Name, unwrap(Ty));
69 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
72 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
73 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
79 /*===-- Operations on types -----------------------------------------------===*/
81 /*--.. Operations on all types (mostly) ....................................--*/
83 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
84 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
87 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
88 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
89 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
92 /*--.. Operations on integer types .........................................--*/
94 LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
95 LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
96 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
97 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
98 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
100 LLVMTypeRef LLVMIntType(unsigned NumBits) {
101 return wrap(IntegerType::get(NumBits));
104 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
105 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
108 /*--.. Operations on real types ............................................--*/
110 LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
111 LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
112 LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
113 LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
114 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
116 /*--.. Operations on function types ........................................--*/
118 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
119 LLVMTypeRef *ParamTypes, unsigned ParamCount,
121 std::vector<const Type*> Tys;
122 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
123 Tys.push_back(unwrap(*I));
125 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
128 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
129 return unwrap<FunctionType>(FunctionTy)->isVarArg();
132 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
133 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
136 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
137 return unwrap<FunctionType>(FunctionTy)->getNumParams();
140 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
141 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
142 for (FunctionType::param_iterator I = Ty->param_begin(),
143 E = Ty->param_end(); I != E; ++I)
147 /*--.. Operations on struct types ..........................................--*/
149 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
150 unsigned ElementCount, int Packed) {
151 std::vector<const Type*> Tys;
152 for (LLVMTypeRef *I = ElementTypes,
153 *E = ElementTypes + ElementCount; I != E; ++I)
154 Tys.push_back(unwrap(*I));
156 return wrap(StructType::get(Tys, Packed != 0));
159 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
160 return unwrap<StructType>(StructTy)->getNumElements();
163 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
164 StructType *Ty = unwrap<StructType>(StructTy);
165 for (FunctionType::param_iterator I = Ty->element_begin(),
166 E = Ty->element_end(); I != E; ++I)
170 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
171 return unwrap<StructType>(StructTy)->isPacked();
174 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
176 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
177 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
180 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
181 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
184 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
185 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
188 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
189 return wrap(unwrap<SequentialType>(Ty)->getElementType());
192 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
193 return unwrap<ArrayType>(ArrayTy)->getNumElements();
196 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
197 return unwrap<PointerType>(PointerTy)->getAddressSpace();
200 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
201 return unwrap<VectorType>(VectorTy)->getNumElements();
204 /*--.. Operations on other types ...........................................--*/
206 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
207 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
209 LLVMTypeRef LLVMOpaqueType() {
210 return wrap(llvm::OpaqueType::get());
213 /* Operations on type handles */
215 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
216 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
219 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
220 delete unwrap(TypeHandle);
223 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
224 return wrap(unwrap(TypeHandle)->get());
227 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
228 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
232 /*===-- Operations on values ----------------------------------------------===*/
234 /*--.. Operations on all values ............................................--*/
236 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
237 return wrap(unwrap(Val)->getType());
240 const char *LLVMGetValueName(LLVMValueRef Val) {
241 return unwrap(Val)->getNameStart();
244 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
245 unwrap(Val)->setName(Name);
248 void LLVMDumpValue(LLVMValueRef Val) {
252 /*--.. Operations on constants of any type .................................--*/
254 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
255 return wrap(Constant::getNullValue(unwrap(Ty)));
258 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
259 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
262 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
263 return wrap(UndefValue::get(unwrap(Ty)));
266 int LLVMIsConstant(LLVMValueRef Ty) {
267 return isa<Constant>(unwrap(Ty));
270 int LLVMIsNull(LLVMValueRef Val) {
271 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
272 return C->isNullValue();
276 int LLVMIsUndef(LLVMValueRef Val) {
277 return isa<UndefValue>(unwrap(Val));
280 /*--.. Operations on scalar constants ......................................--*/
282 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
284 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
287 static const fltSemantics &SemanticsForType(Type *Ty) {
288 assert(Ty->isFloatingPoint() && "Type is not floating point!");
289 if (Ty == Type::FloatTy)
290 return APFloat::IEEEsingle;
291 if (Ty == Type::DoubleTy)
292 return APFloat::IEEEdouble;
293 if (Ty == Type::X86_FP80Ty)
294 return APFloat::x87DoubleExtended;
295 if (Ty == Type::FP128Ty)
296 return APFloat::IEEEquad;
297 if (Ty == Type::PPC_FP128Ty)
298 return APFloat::PPCDoubleDouble;
299 return APFloat::Bogus;
302 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
304 APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven);
305 return wrap(ConstantFP::get(unwrap(RealTy), APN));
308 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
309 return wrap(ConstantFP::get(unwrap(RealTy),
310 APFloat(SemanticsForType(unwrap(RealTy)), Text)));
313 /*--.. Operations on composite constants ...................................--*/
315 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
316 int DontNullTerminate) {
317 /* Inverted the sense of AddNull because ', 0)' is a
318 better mnemonic for null termination than ', 1)'. */
319 return wrap(ConstantArray::get(std::string(Str, Length),
320 DontNullTerminate == 0));
323 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
324 LLVMValueRef *ConstantVals, unsigned Length) {
325 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
326 unwrap<Constant>(ConstantVals, Length),
330 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
332 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
333 Count, Packed != 0));
336 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
337 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
341 /*--.. Constant expressions ................................................--*/
343 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
344 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
347 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
348 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
351 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
352 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
355 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
356 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
357 unwrap<Constant>(RHSConstant)));
360 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
361 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
362 unwrap<Constant>(RHSConstant)));
365 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
366 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
367 unwrap<Constant>(RHSConstant)));
370 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
371 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
372 unwrap<Constant>(RHSConstant)));
375 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
377 unwrap<Constant>(RHSConstant)));
380 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
381 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
382 unwrap<Constant>(RHSConstant)));
385 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
386 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
387 unwrap<Constant>(RHSConstant)));
390 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
391 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
392 unwrap<Constant>(RHSConstant)));
395 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
396 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
397 unwrap<Constant>(RHSConstant)));
400 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
401 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
402 unwrap<Constant>(RHSConstant)));
405 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
406 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
407 unwrap<Constant>(RHSConstant)));
410 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
411 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
412 unwrap<Constant>(RHSConstant)));
415 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
416 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417 return wrap(ConstantExpr::getICmp(Predicate,
418 unwrap<Constant>(LHSConstant),
419 unwrap<Constant>(RHSConstant)));
422 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
423 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
424 return wrap(ConstantExpr::getFCmp(Predicate,
425 unwrap<Constant>(LHSConstant),
426 unwrap<Constant>(RHSConstant)));
429 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
430 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
431 unwrap<Constant>(RHSConstant)));
434 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
435 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
436 unwrap<Constant>(RHSConstant)));
439 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
441 unwrap<Constant>(RHSConstant)));
444 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
445 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
446 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
447 unwrap<Constant>(ConstantIndices,
452 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
453 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
457 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
458 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
462 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
463 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
467 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
468 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
472 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
473 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
477 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
478 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
482 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
483 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
487 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
488 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
492 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
493 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
497 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
498 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
502 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
503 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
507 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
508 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
512 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
513 LLVMValueRef ConstantIfTrue,
514 LLVMValueRef ConstantIfFalse) {
515 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
516 unwrap<Constant>(ConstantIfTrue),
517 unwrap<Constant>(ConstantIfFalse)));
520 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
521 LLVMValueRef IndexConstant) {
522 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
523 unwrap<Constant>(IndexConstant)));
526 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
527 LLVMValueRef ElementValueConstant,
528 LLVMValueRef IndexConstant) {
529 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
530 unwrap<Constant>(ElementValueConstant),
531 unwrap<Constant>(IndexConstant)));
534 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
535 LLVMValueRef VectorBConstant,
536 LLVMValueRef MaskConstant) {
537 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
538 unwrap<Constant>(VectorBConstant),
539 unwrap<Constant>(MaskConstant)));
542 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
544 int LLVMIsDeclaration(LLVMValueRef Global) {
545 return unwrap<GlobalValue>(Global)->isDeclaration();
548 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
549 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
552 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
553 unwrap<GlobalValue>(Global)
554 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
557 const char *LLVMGetSection(LLVMValueRef Global) {
558 return unwrap<GlobalValue>(Global)->getSection().c_str();
561 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
562 unwrap<GlobalValue>(Global)->setSection(Section);
565 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
566 return static_cast<LLVMVisibility>(
567 unwrap<GlobalValue>(Global)->getVisibility());
570 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
571 unwrap<GlobalValue>(Global)
572 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
575 unsigned LLVMGetAlignment(LLVMValueRef Global) {
576 return unwrap<GlobalValue>(Global)->getAlignment();
579 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
580 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
583 /*--.. Operations on global variables ......................................--*/
585 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
586 return wrap(new GlobalVariable(unwrap(Ty), false,
587 GlobalValue::ExternalLinkage, 0, Name,
591 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
592 return wrap(unwrap(M)->getNamedGlobal(Name));
595 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
596 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
599 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
600 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
603 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
604 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
607 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
608 unwrap<GlobalVariable>(GlobalVar)
609 ->setInitializer(unwrap<Constant>(ConstantVal));
612 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
613 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
616 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
617 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
620 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
621 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
624 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
625 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
628 /*--.. Operations on functions .............................................--*/
630 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
631 LLVMTypeRef FunctionTy) {
632 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
633 GlobalValue::ExternalLinkage, Name, unwrap(M)));
636 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
637 return wrap(unwrap(M)->getFunction(Name));
640 void LLVMDeleteFunction(LLVMValueRef Fn) {
641 unwrap<Function>(Fn)->eraseFromParent();
644 unsigned LLVMCountParams(LLVMValueRef FnRef) {
645 // This function is strictly redundant to
646 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
647 return unwrap<Function>(FnRef)->getArgumentList().size();
650 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
651 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
657 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
658 Function *Fn = unwrap<Function>(FnRef);
659 for (Function::arg_iterator I = Fn->arg_begin(),
660 E = Fn->arg_end(); I != E; I++)
661 *ParamRefs++ = wrap(I);
664 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
665 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
666 return F->getIntrinsicID();
670 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
671 return unwrap<Function>(Fn)->getCallingConv();
674 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
675 return unwrap<Function>(Fn)->setCallingConv(CC);
678 const char *LLVMGetCollector(LLVMValueRef Fn) {
679 Function *F = unwrap<Function>(Fn);
680 return F->hasCollector()? F->getCollector() : 0;
683 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
684 Function *F = unwrap<Function>(Fn);
686 F->setCollector(Coll);
691 /*--.. Operations on basic blocks ..........................................--*/
693 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
694 return wrap(static_cast<Value*>(unwrap(Bb)));
697 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
698 return isa<BasicBlock>(unwrap(Val));
701 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
702 return wrap(unwrap<BasicBlock>(Val));
705 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
706 return unwrap<Function>(FnRef)->getBasicBlockList().size();
709 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
710 Function *Fn = unwrap<Function>(FnRef);
711 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
712 *BasicBlocksRefs++ = wrap(I);
715 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
716 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
719 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
720 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
723 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
725 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
726 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
730 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
731 unwrap(BBRef)->eraseFromParent();
734 /*--.. Call and invoke instructions ........................................--*/
736 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
737 Value *V = unwrap(Instr);
738 if (CallInst *CI = dyn_cast<CallInst>(V))
739 return CI->getCallingConv();
740 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
741 return II->getCallingConv();
742 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
746 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
747 Value *V = unwrap(Instr);
748 if (CallInst *CI = dyn_cast<CallInst>(V))
749 return CI->setCallingConv(CC);
750 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
751 return II->setCallingConv(CC);
752 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
755 /*--.. Operations on phi nodes .............................................--*/
757 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
758 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
759 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
760 for (unsigned I = 0; I != Count; ++I)
761 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
764 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
765 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
768 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
769 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
772 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
773 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
777 /*===-- Instruction builders ----------------------------------------------===*/
779 LLVMBuilderRef LLVMCreateBuilder() {
780 return wrap(new LLVMFoldingBuilder());
783 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
784 Instruction *I = unwrap<Instruction>(Instr);
785 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
788 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
789 BasicBlock *BB = unwrap(Block);
790 unwrap(Builder)->SetInsertPoint(BB);
793 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
794 delete unwrap(Builder);
797 /*--.. Instruction builders ................................................--*/
799 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
800 return wrap(unwrap(B)->CreateRetVoid());
803 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
804 return wrap(unwrap(B)->CreateRet(unwrap(V)));
807 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
808 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
811 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
812 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
813 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
816 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
817 LLVMBasicBlockRef Else, unsigned NumCases) {
818 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
821 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
822 LLVMValueRef *Args, unsigned NumArgs,
823 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
825 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
826 unwrap(Args), unwrap(Args) + NumArgs,
830 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
831 return wrap(unwrap(B)->CreateUnwind());
834 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
835 return wrap(unwrap(B)->CreateUnreachable());
838 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
839 LLVMBasicBlockRef Dest) {
840 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
843 /*--.. Arithmetic ..........................................................--*/
845 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
847 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
850 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
852 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
855 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
857 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
860 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
862 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
865 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
867 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
870 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
872 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
875 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
877 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
880 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
882 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
885 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
887 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
890 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
892 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
895 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
897 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
900 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
902 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
905 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
907 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
910 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
912 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
915 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
917 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
920 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
921 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
924 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
925 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
928 /*--.. Memory ..............................................................--*/
930 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
932 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
935 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
936 LLVMValueRef Val, const char *Name) {
937 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
940 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
942 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
945 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
946 LLVMValueRef Val, const char *Name) {
947 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
950 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
951 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
955 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
957 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
960 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
961 LLVMValueRef PointerVal) {
962 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
965 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
966 LLVMValueRef *Indices, unsigned NumIndices,
968 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
969 unwrap(Indices) + NumIndices, Name));
972 /*--.. Casts ...............................................................--*/
974 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
975 LLVMTypeRef DestTy, const char *Name) {
976 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
979 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
980 LLVMTypeRef DestTy, const char *Name) {
981 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
984 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
985 LLVMTypeRef DestTy, const char *Name) {
986 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
989 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
990 LLVMTypeRef DestTy, const char *Name) {
991 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
994 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
995 LLVMTypeRef DestTy, const char *Name) {
996 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
999 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1000 LLVMTypeRef DestTy, const char *Name) {
1001 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1004 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1005 LLVMTypeRef DestTy, const char *Name) {
1006 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1009 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1010 LLVMTypeRef DestTy, const char *Name) {
1011 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1014 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1015 LLVMTypeRef DestTy, const char *Name) {
1016 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1019 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1020 LLVMTypeRef DestTy, const char *Name) {
1021 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1024 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1025 LLVMTypeRef DestTy, const char *Name) {
1026 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1029 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1030 LLVMTypeRef DestTy, const char *Name) {
1031 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1034 /*--.. Comparisons .........................................................--*/
1036 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1037 LLVMValueRef LHS, LLVMValueRef RHS,
1039 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1040 unwrap(LHS), unwrap(RHS), Name));
1043 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1044 LLVMValueRef LHS, LLVMValueRef RHS,
1046 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1047 unwrap(LHS), unwrap(RHS), Name));
1050 /*--.. Miscellaneous instructions ..........................................--*/
1052 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1053 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1056 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1057 LLVMValueRef *Args, unsigned NumArgs,
1059 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1060 unwrap(Args) + NumArgs, Name));
1063 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1064 LLVMValueRef Then, LLVMValueRef Else,
1066 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1070 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1071 LLVMTypeRef Ty, const char *Name) {
1072 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1075 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1076 LLVMValueRef Index, const char *Name) {
1077 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1081 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1082 LLVMValueRef EltVal, LLVMValueRef Index,
1084 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1085 unwrap(Index), Name));
1088 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1089 LLVMValueRef V2, LLVMValueRef Mask,
1091 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1092 unwrap(Mask), Name));
1096 /*===-- Module providers --------------------------------------------------===*/
1098 LLVMModuleProviderRef
1099 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1100 return wrap(new ExistingModuleProvider(unwrap(M)));
1103 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1108 /*===-- Memory buffers ----------------------------------------------------===*/
1110 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1111 LLVMMemoryBufferRef *OutMemBuf,
1112 char **OutMessage) {
1114 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1115 *OutMemBuf = wrap(MB);
1119 *OutMessage = strdup(Error.c_str());
1123 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1124 char **OutMessage) {
1125 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1126 *OutMemBuf = wrap(MB);
1130 *OutMessage = strdup("stdin is empty.");
1134 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1135 delete unwrap(MemBuf);