1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Gordon Henriksen and is distributed under the
6 // University of Illinois Open Source 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 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
47 return unwrap(M)->addTypeName(Name, unwrap(Ty));
50 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
53 TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
54 for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
60 /*===-- Operations on types -----------------------------------------------===*/
62 /*--.. Operations on all types (mostly) ....................................--*/
64 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
65 return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
68 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
69 DerivedType *Ty = unwrap<DerivedType>(AbstractType);
70 Ty->refineAbstractTypeTo(unwrap(ConcreteType));
73 /*--.. Operations on integer types .........................................--*/
75 LLVMTypeRef LLVMInt1Type() { return (LLVMTypeRef) Type::Int1Ty; }
76 LLVMTypeRef LLVMInt8Type() { return (LLVMTypeRef) Type::Int8Ty; }
77 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
78 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
79 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
81 LLVMTypeRef LLVMIntType(unsigned NumBits) {
82 return wrap(IntegerType::get(NumBits));
85 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
86 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
89 /*--.. Operations on real types ............................................--*/
91 LLVMTypeRef LLVMFloatType() { return (LLVMTypeRef) Type::FloatTy; }
92 LLVMTypeRef LLVMDoubleType() { return (LLVMTypeRef) Type::DoubleTy; }
93 LLVMTypeRef LLVMX86FP80Type() { return (LLVMTypeRef) Type::X86_FP80Ty; }
94 LLVMTypeRef LLVMFP128Type() { return (LLVMTypeRef) Type::FP128Ty; }
95 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
97 /*--.. Operations on function types ........................................--*/
99 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
100 LLVMTypeRef *ParamTypes, unsigned ParamCount,
102 std::vector<const Type*> Tys;
103 for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
104 Tys.push_back(unwrap(*I));
106 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
109 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
110 return unwrap<FunctionType>(FunctionTy)->isVarArg();
113 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
114 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
117 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
118 return unwrap<FunctionType>(FunctionTy)->getNumParams();
121 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
122 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
123 for (FunctionType::param_iterator I = Ty->param_begin(),
124 E = Ty->param_end(); I != E; ++I)
128 /*--.. Operations on struct types ..........................................--*/
130 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
131 unsigned ElementCount, int Packed) {
132 std::vector<const Type*> Tys;
133 for (LLVMTypeRef *I = ElementTypes,
134 *E = ElementTypes + ElementCount; I != E; ++I)
135 Tys.push_back(unwrap(*I));
137 return wrap(StructType::get(Tys, Packed != 0));
140 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
141 return unwrap<StructType>(StructTy)->getNumElements();
144 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
145 StructType *Ty = unwrap<StructType>(StructTy);
146 for (FunctionType::param_iterator I = Ty->element_begin(),
147 E = Ty->element_end(); I != E; ++I)
151 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
152 return unwrap<StructType>(StructTy)->isPacked();
155 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
157 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
158 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
161 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
162 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
165 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
166 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
169 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
170 return wrap(unwrap<SequentialType>(Ty)->getElementType());
173 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
174 return unwrap<ArrayType>(ArrayTy)->getNumElements();
177 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
178 return unwrap<PointerType>(PointerTy)->getAddressSpace();
181 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
182 return unwrap<VectorType>(VectorTy)->getNumElements();
185 /*--.. Operations on other types ...........................................--*/
187 LLVMTypeRef LLVMVoidType() { return (LLVMTypeRef) Type::VoidTy; }
188 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
190 LLVMTypeRef LLVMOpaqueType() {
191 return wrap(llvm::OpaqueType::get());
194 /* Operations on type handles */
196 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
197 return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
200 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
201 delete unwrap(TypeHandle);
204 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
205 return wrap(unwrap(TypeHandle)->get());
208 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
209 unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
213 /*===-- Operations on values ----------------------------------------------===*/
215 /*--.. Operations on all values ............................................--*/
217 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
218 return wrap(unwrap(Val)->getType());
221 const char *LLVMGetValueName(LLVMValueRef Val) {
222 return unwrap(Val)->getNameStart();
225 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
226 unwrap(Val)->setName(Name);
229 void LLVMDumpValue(LLVMValueRef Val) {
233 /*--.. Operations on constants of any type .................................--*/
235 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
236 return wrap(Constant::getNullValue(unwrap(Ty)));
239 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
240 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
243 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
244 return wrap(UndefValue::get(unwrap(Ty)));
247 int LLVMIsConstant(LLVMValueRef Ty) {
248 return isa<Constant>(unwrap(Ty));
251 int LLVMIsNull(LLVMValueRef Val) {
252 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
253 return C->isNullValue();
257 int LLVMIsUndef(LLVMValueRef Val) {
258 return isa<UndefValue>(unwrap(Val));
261 /*--.. Operations on scalar constants ......................................--*/
263 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
265 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
268 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
269 return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
272 /*--.. Operations on composite constants ...................................--*/
274 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
275 int DontNullTerminate) {
276 /* Inverted the sense of AddNull because ', 0)' is a
277 better mnemonic for null termination than ', 1)'. */
278 return wrap(ConstantArray::get(std::string(Str, Length),
279 DontNullTerminate == 0));
282 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
283 LLVMValueRef *ConstantVals, unsigned Length) {
284 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
285 unwrap<Constant>(ConstantVals, Length),
289 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
291 return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
292 Count, Packed != 0));
295 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
296 return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
300 /*--.. Constant expressions ................................................--*/
302 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
303 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
306 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
307 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
310 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
311 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
314 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
315 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
316 unwrap<Constant>(RHSConstant)));
319 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
320 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
321 unwrap<Constant>(RHSConstant)));
324 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
325 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
326 unwrap<Constant>(RHSConstant)));
329 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
330 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
331 unwrap<Constant>(RHSConstant)));
334 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
335 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
336 unwrap<Constant>(RHSConstant)));
339 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
340 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
341 unwrap<Constant>(RHSConstant)));
344 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
345 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
346 unwrap<Constant>(RHSConstant)));
349 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
350 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
351 unwrap<Constant>(RHSConstant)));
354 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
355 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
356 unwrap<Constant>(RHSConstant)));
359 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
360 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
361 unwrap<Constant>(RHSConstant)));
364 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
365 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
366 unwrap<Constant>(RHSConstant)));
369 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
370 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
371 unwrap<Constant>(RHSConstant)));
374 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
375 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
376 return wrap(ConstantExpr::getICmp(Predicate,
377 unwrap<Constant>(LHSConstant),
378 unwrap<Constant>(RHSConstant)));
381 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
382 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
383 return wrap(ConstantExpr::getFCmp(Predicate,
384 unwrap<Constant>(LHSConstant),
385 unwrap<Constant>(RHSConstant)));
388 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
389 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
390 unwrap<Constant>(RHSConstant)));
393 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
394 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
395 unwrap<Constant>(RHSConstant)));
398 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
399 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
400 unwrap<Constant>(RHSConstant)));
403 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
404 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
405 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
406 unwrap<Constant>(ConstantIndices,
411 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
412 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
416 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
417 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
421 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
422 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
426 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
427 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
431 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
432 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
436 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
437 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
441 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
442 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
446 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
447 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
451 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
452 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
456 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
457 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
461 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
462 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
466 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
467 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
471 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
472 LLVMValueRef ConstantIfTrue,
473 LLVMValueRef ConstantIfFalse) {
474 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
475 unwrap<Constant>(ConstantIfTrue),
476 unwrap<Constant>(ConstantIfFalse)));
479 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
480 LLVMValueRef IndexConstant) {
481 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
482 unwrap<Constant>(IndexConstant)));
485 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
486 LLVMValueRef ElementValueConstant,
487 LLVMValueRef IndexConstant) {
488 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
489 unwrap<Constant>(ElementValueConstant),
490 unwrap<Constant>(IndexConstant)));
493 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
494 LLVMValueRef VectorBConstant,
495 LLVMValueRef MaskConstant) {
496 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
497 unwrap<Constant>(VectorBConstant),
498 unwrap<Constant>(MaskConstant)));
501 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
503 int LLVMIsDeclaration(LLVMValueRef Global) {
504 return unwrap<GlobalValue>(Global)->isDeclaration();
507 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
508 return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
511 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
512 unwrap<GlobalValue>(Global)
513 ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
516 const char *LLVMGetSection(LLVMValueRef Global) {
517 return unwrap<GlobalValue>(Global)->getSection().c_str();
520 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
521 unwrap<GlobalValue>(Global)->setSection(Section);
524 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
525 return static_cast<LLVMVisibility>(
526 unwrap<GlobalValue>(Global)->getVisibility());
529 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
530 unwrap<GlobalValue>(Global)
531 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
534 unsigned LLVMGetAlignment(LLVMValueRef Global) {
535 return unwrap<GlobalValue>(Global)->getAlignment();
538 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
539 unwrap<GlobalValue>(Global)->setAlignment(Bytes);
542 /*--.. Operations on global variables ......................................--*/
544 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
545 return wrap(new GlobalVariable(unwrap(Ty), false,
546 GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
549 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
550 return wrap(unwrap(M)->getNamedGlobal(Name));
553 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
554 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
557 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
558 return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
561 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
562 return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
565 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
566 unwrap<GlobalVariable>(GlobalVar)
567 ->setInitializer(unwrap<Constant>(ConstantVal));
570 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
571 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
574 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
575 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
578 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
579 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
582 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
583 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
586 /*--.. Operations on functions .............................................--*/
588 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
589 LLVMTypeRef FunctionTy) {
590 return wrap(new Function(unwrap<FunctionType>(FunctionTy),
591 GlobalValue::ExternalLinkage, Name, unwrap(M)));
594 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
595 return wrap(unwrap(M)->getFunction(Name));
598 void LLVMDeleteFunction(LLVMValueRef Fn) {
599 unwrap<Function>(Fn)->eraseFromParent();
602 unsigned LLVMCountParams(LLVMValueRef FnRef) {
603 // This function is strictly redundant to
604 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
605 return unwrap<Function>(FnRef)->getArgumentList().size();
608 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
609 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
615 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
616 Function *Fn = unwrap<Function>(FnRef);
617 for (Function::arg_iterator I = Fn->arg_begin(),
618 E = Fn->arg_end(); I != E; I++)
619 *ParamRefs++ = wrap(I);
622 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
623 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
624 return F->getIntrinsicID();
628 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
629 return unwrap<Function>(Fn)->getCallingConv();
632 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
633 return unwrap<Function>(Fn)->setCallingConv(CC);
636 const char *LLVMGetCollector(LLVMValueRef Fn) {
637 Function *F = unwrap<Function>(Fn);
638 return F->hasCollector()? F->getCollector() : 0;
641 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll) {
642 Function *F = unwrap<Function>(Fn);
644 F->setCollector(Coll);
649 /*--.. Operations on basic blocks ..........................................--*/
651 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
652 return wrap(static_cast<Value*>(unwrap(Bb)));
655 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
656 return isa<BasicBlock>(unwrap(Val));
659 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
660 return wrap(unwrap<BasicBlock>(Val));
663 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
664 return unwrap<Function>(FnRef)->getBasicBlockList().size();
667 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
668 Function *Fn = unwrap<Function>(FnRef);
669 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
670 *BasicBlocksRefs++ = wrap(I);
673 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
674 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
677 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
678 return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
681 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
683 BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
684 return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
688 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
689 unwrap(BBRef)->eraseFromParent();
692 /*--.. Call and invoke instructions ........................................--*/
694 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
695 Value *V = unwrap(Instr);
696 if (CallInst *CI = dyn_cast<CallInst>(V))
697 return CI->getCallingConv();
698 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
699 return II->getCallingConv();
700 assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
704 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
705 Value *V = unwrap(Instr);
706 if (CallInst *CI = dyn_cast<CallInst>(V))
707 return CI->setCallingConv(CC);
708 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
709 return II->setCallingConv(CC);
710 assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
713 /*--.. Operations on phi nodes .............................................--*/
715 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
716 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
717 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
718 for (unsigned I = 0; I != Count; ++I)
719 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
722 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
723 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
726 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
727 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
730 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
731 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
735 /*===-- Instruction builders ----------------------------------------------===*/
737 LLVMBuilderRef LLVMCreateBuilder() {
738 return wrap(new LLVMFoldingBuilder());
741 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
742 Instruction *I = unwrap<Instruction>(Instr);
743 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
746 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
747 BasicBlock *BB = unwrap(Block);
748 unwrap(Builder)->SetInsertPoint(BB);
751 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
752 delete unwrap(Builder);
755 /*--.. Instruction builders ................................................--*/
757 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
758 return wrap(unwrap(B)->CreateRetVoid());
761 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
762 return wrap(unwrap(B)->CreateRet(unwrap(V)));
765 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
766 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
769 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
770 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
771 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
774 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
775 LLVMBasicBlockRef Else, unsigned NumCases) {
776 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
779 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
780 LLVMValueRef *Args, unsigned NumArgs,
781 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
783 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
784 unwrap(Args), unwrap(Args) + NumArgs,
788 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
789 return wrap(unwrap(B)->CreateUnwind());
792 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
793 return wrap(unwrap(B)->CreateUnreachable());
796 /*--.. Arithmetic ..........................................................--*/
798 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
800 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
803 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
805 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
808 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
810 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
813 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
815 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
818 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
820 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
823 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
825 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
828 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
830 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
833 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
835 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
838 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
840 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
843 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
845 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
848 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
850 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
853 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
855 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
858 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
860 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
863 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
865 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
868 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
870 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
873 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
874 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
877 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
878 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
881 /*--.. Memory ..............................................................--*/
883 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
885 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
888 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
889 LLVMValueRef Val, const char *Name) {
890 return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
893 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
895 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
898 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
899 LLVMValueRef Val, const char *Name) {
900 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
903 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
904 return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
908 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
910 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
913 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
914 LLVMValueRef PointerVal) {
915 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
918 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
919 LLVMValueRef *Indices, unsigned NumIndices,
921 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
922 unwrap(Indices) + NumIndices, Name));
925 /*--.. Casts ...............................................................--*/
927 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
928 LLVMTypeRef DestTy, const char *Name) {
929 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
932 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
933 LLVMTypeRef DestTy, const char *Name) {
934 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
937 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
938 LLVMTypeRef DestTy, const char *Name) {
939 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
942 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
943 LLVMTypeRef DestTy, const char *Name) {
944 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
947 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
948 LLVMTypeRef DestTy, const char *Name) {
949 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
952 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
953 LLVMTypeRef DestTy, const char *Name) {
954 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
957 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
958 LLVMTypeRef DestTy, const char *Name) {
959 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
962 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
963 LLVMTypeRef DestTy, const char *Name) {
964 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
967 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
968 LLVMTypeRef DestTy, const char *Name) {
969 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
972 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
973 LLVMTypeRef DestTy, const char *Name) {
974 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
977 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
978 LLVMTypeRef DestTy, const char *Name) {
979 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
982 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
983 LLVMTypeRef DestTy, const char *Name) {
984 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
987 /*--.. Comparisons .........................................................--*/
989 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
990 LLVMValueRef LHS, LLVMValueRef RHS,
992 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
993 unwrap(LHS), unwrap(RHS), Name));
996 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
997 LLVMValueRef LHS, LLVMValueRef RHS,
999 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1000 unwrap(LHS), unwrap(RHS), Name));
1003 /*--.. Miscellaneous instructions ..........................................--*/
1005 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1006 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1009 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1010 LLVMValueRef *Args, unsigned NumArgs,
1012 return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1013 unwrap(Args) + NumArgs, Name));
1016 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1017 LLVMValueRef Then, LLVMValueRef Else,
1019 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1023 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1024 LLVMTypeRef Ty, const char *Name) {
1025 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1028 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1029 LLVMValueRef Index, const char *Name) {
1030 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1034 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1035 LLVMValueRef EltVal, LLVMValueRef Index,
1037 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1038 unwrap(Index), Name));
1041 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1042 LLVMValueRef V2, LLVMValueRef Mask,
1044 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1045 unwrap(Mask), Name));
1049 /*===-- Module providers --------------------------------------------------===*/
1051 LLVMModuleProviderRef
1052 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1053 return wrap(new ExistingModuleProvider(unwrap(M)));
1056 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1061 /*===-- Memory buffers ----------------------------------------------------===*/
1063 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1064 LLVMMemoryBufferRef *OutMemBuf,
1065 char **OutMessage) {
1067 if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, strlen(Path), &Error)) {
1068 *OutMemBuf = wrap(MB);
1072 *OutMessage = strdup(Error.c_str());
1076 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1077 char **OutMessage) {
1078 if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1079 *OutMemBuf = wrap(MB);
1083 *OutMessage = strdup("stdin is empty.");
1087 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1088 delete unwrap(MemBuf);