]> git.lizzy.rs Git - rust.git/blob - src/rustc/lib/llvm.rs
1440a654a8978a4ec956093a2847b758ac29b4d8
[rust.git] / src / rustc / lib / llvm.rs
1 import std::map::hashmap;
2
3 import libc::{c_char, c_int, c_uint, c_longlong, c_ulonglong};
4
5 type Opcode = u32;
6 type Bool = c_uint;
7 const True: Bool = 1 as Bool;
8 const False: Bool = 0 as Bool;
9
10 // Consts for the LLVM CallConv type, pre-cast to uint.
11
12 enum CallConv {
13     CCallConv = 0,
14     FastCallConv = 8,
15     ColdCallConv = 9,
16     X86StdcallCallConv = 64,
17     X86FastcallCallConv = 65,
18 }
19
20 enum Visibility {
21     LLVMDefaultVisibility = 0,
22     HiddenVisibility = 1,
23     ProtectedVisibility = 2,
24 }
25
26 enum Linkage {
27     ExternalLinkage = 0,
28     AvailableExternallyLinkage = 1,
29     LinkOnceAnyLinkage = 2,
30     LinkOnceODRLinkage = 3,
31     WeakAnyLinkage = 4,
32     WeakODRLinkage = 5,
33     AppendingLinkage = 6,
34     InternalLinkage = 7,
35     PrivateLinkage = 8,
36     DLLImportLinkage = 9,
37     DLLExportLinkage = 10,
38     ExternalWeakLinkage = 11,
39     GhostLinkage = 12,
40     CommonLinkage = 13,
41     LinkerPrivateLinkage = 14,
42     LinkerPrivateWeakLinkage = 15,
43     LinkerPrivateWeakDefAutoLinkage = 16,
44 }
45
46 enum Attribute {
47     ZExtAttribute = 1,
48     SExtAttribute = 2,
49     NoReturnAttribute = 4,
50     InRegAttribute = 8,
51     StructRetAttribute = 16,
52     NoUnwindAttribute = 32,
53     NoAliasAttribute = 64,
54     ByValAttribute = 128,
55     NestAttribute = 256,
56     ReadNoneAttribute = 512,
57     ReadOnlyAttribute = 1024,
58     NoInlineAttribute = 2048,
59     AlwaysInlineAttribute = 4096,
60     OptimizeForSizeAttribute = 8192,
61     StackProtectAttribute = 16384,
62     StackProtectReqAttribute = 32768,
63     // 31 << 16
64     AlignmentAttribute = 2031616,
65     NoCaptureAttribute = 2097152,
66     NoRedZoneAttribute = 4194304,
67     NoImplicitFloatAttribute = 8388608,
68     NakedAttribute = 16777216,
69     InlineHintAttribute = 33554432,
70     // 7 << 26
71     StackAttribute = 469762048,
72     ReturnsTwiceAttribute = 536870912,
73     // 1 << 30
74     UWTableAttribute = 1073741824,
75     NonLazyBindAttribute = 2147483648,
76 }
77
78 // enum for the LLVM IntPredicate type
79 enum IntPredicate {
80     IntEQ = 32,
81     IntNE = 33,
82     IntUGT = 34,
83     IntUGE = 35,
84     IntULT = 36,
85     IntULE = 37,
86     IntSGT = 38,
87     IntSGE = 39,
88     IntSLT = 40,
89     IntSLE = 41,
90 }
91
92 // enum for the LLVM RealPredicate type
93 enum RealPredicate {
94     RealOEQ = 1,
95     RealOGT = 2,
96     RealOGE = 3,
97     RealOLT = 4,
98     RealOLE = 5,
99     RealONE = 6,
100     RealORD = 7,
101     RealUNO = 8,
102     RealUEQ = 9,
103     RealUGT = 10,
104     RealUGE = 11,
105     RealULT = 12,
106     RealULE = 13,
107     RealUNE = 14,
108 }
109
110 // enum for the LLVM TypeKind type - must stay in sync with the def of
111 // LLVMTypeKind in llvm/include/llvm-c/Core.h
112 enum TypeKind {
113     Void      = 0,
114     Half      = 1,
115     Float     = 2,
116     Double    = 3,
117     X86_FP80  = 4,
118     FP128     = 5,
119     PPC_FP128 = 6,
120     Label     = 7,
121     Integer   = 8,
122     Function  = 9,
123     Struct    = 10,
124     Array     = 11,
125     Pointer   = 12,
126     Vector    = 13,
127     Metadata  = 14,
128     X86_MMX   = 15
129 }
130
131 impl TypeKind : cmp::Eq {
132     pure fn eq(&&other: TypeKind) -> bool {
133         match (self, other) {
134             (Void, Void) => true,
135             (Half, Half) => true,
136             (Float, Float) => true,
137             (Double, Double) => true,
138             (X86_FP80, X86_FP80) => true,
139             (FP128, FP128) => true,
140             (PPC_FP128, PPC_FP128) => true,
141             (Label, Label) => true,
142             (Integer, Integer) => true,
143             (Function, Function) => true,
144             (Struct, Struct) => true,
145             (Array, Array) => true,
146             (Pointer, Pointer) => true,
147             (Vector, Vector) => true,
148             (Metadata, Metadata) => true,
149             (X86_MMX, X86_MMX) => true,
150             (Void, _) => false,
151             (Half, _) => false,
152             (Float, _) => false,
153             (Double, _) => false,
154             (X86_FP80, _) => false,
155             (FP128, _) => false,
156             (PPC_FP128, _) => false,
157             (Label, _) => false,
158             (Integer, _) => false,
159             (Function, _) => false,
160             (Struct, _) => false,
161             (Array, _) => false,
162             (Pointer, _) => false,
163             (Vector, _) => false,
164             (Metadata, _) => false,
165             (X86_MMX, _) => false,
166         }
167     }
168 }
169
170 enum AtomicBinOp {
171     Xchg = 0,
172     Add  = 1,
173     Sub  = 2,
174     And  = 3,
175     Nand = 4,
176     Or   = 5,
177     Xor  = 6,
178     Max  = 7,
179     Min  = 8,
180     UMax = 9,
181     UMin = 10,
182 }
183
184 enum AtomicOrdering {
185     NotAtomic = 0,
186     Unordered = 1,
187     Monotonic = 2,
188     // Consume = 3,  // Not specified yet.
189     Acquire = 4,
190     Release = 5,
191     AcquireRelease = 6,
192     SequentiallyConsistent = 7
193 }
194
195 // FIXME: Not used right now, but will be once #2334 is fixed
196 // Consts for the LLVMCodeGenFileType type (in include/llvm/c/TargetMachine.h)
197 enum FileType {
198     AssemblyFile = 0,
199     ObjectFile = 1
200 }
201
202 // Opaque pointer types
203 enum Module_opaque {}
204 type ModuleRef = *Module_opaque;
205 enum Context_opaque {}
206 type ContextRef = *Context_opaque;
207 enum Type_opaque {}
208 type TypeRef = *Type_opaque;
209 enum Value_opaque {}
210 type ValueRef = *Value_opaque;
211 enum BasicBlock_opaque {}
212 type BasicBlockRef = *BasicBlock_opaque;
213 enum Builder_opaque {}
214 type BuilderRef = *Builder_opaque;
215 enum MemoryBuffer_opaque {}
216 type MemoryBufferRef = *MemoryBuffer_opaque;
217 enum PassManager_opaque {}
218 type PassManagerRef = *PassManager_opaque;
219 enum PassManagerBuilder_opaque {}
220 type PassManagerBuilderRef = *PassManagerBuilder_opaque;
221 enum Use_opaque {}
222 type UseRef = *Use_opaque;
223 enum TargetData_opaque {}
224 type TargetDataRef = *TargetData_opaque;
225 enum ObjectFile_opaque {}
226 type ObjectFileRef = *ObjectFile_opaque;
227 enum SectionIterator_opaque {}
228 type SectionIteratorRef = *SectionIterator_opaque;
229
230 #[link_args = "-Lrustllvm"]
231 #[link_name = "rustllvm"]
232 #[abi = "cdecl"]
233 extern mod llvm {
234     /* Create and destroy contexts. */
235     fn LLVMContextCreate() -> ContextRef;
236     fn LLVMGetGlobalContext() -> ContextRef;
237     fn LLVMContextDispose(C: ContextRef);
238     fn LLVMGetMDKindIDInContext(C: ContextRef, Name: *c_char, SLen: c_uint) ->
239        c_uint;
240     fn LLVMGetMDKindID(Name: *c_char, SLen: c_uint) -> c_uint;
241
242     /* Create and destroy modules. */
243     fn LLVMModuleCreateWithNameInContext(ModuleID: *c_char, C: ContextRef) ->
244        ModuleRef;
245     fn LLVMDisposeModule(M: ModuleRef);
246
247     /** Data layout. See Module::getDataLayout. */
248     fn LLVMGetDataLayout(M: ModuleRef) -> *c_char;
249     fn LLVMSetDataLayout(M: ModuleRef, Triple: *c_char);
250
251     /** Target triple. See Module::getTargetTriple. */
252     fn LLVMGetTarget(M: ModuleRef) -> *c_char;
253     fn LLVMSetTarget(M: ModuleRef, Triple: *c_char);
254
255     /** See Module::dump. */
256     fn LLVMDumpModule(M: ModuleRef);
257
258     /** See Module::setModuleInlineAsm. */
259     fn LLVMSetModuleInlineAsm(M: ModuleRef, Asm: *c_char);
260
261     /** See llvm::LLVMTypeKind::getTypeID. */
262     fn LLVMGetTypeKind(Ty: TypeRef) -> TypeKind;
263
264     /** See llvm::LLVMType::getContext. */
265     fn LLVMGetTypeContext(Ty: TypeRef) -> ContextRef;
266
267     /* Operations on integer types */
268     fn LLVMInt1TypeInContext(C: ContextRef) -> TypeRef;
269     fn LLVMInt8TypeInContext(C: ContextRef) -> TypeRef;
270     fn LLVMInt16TypeInContext(C: ContextRef) -> TypeRef;
271     fn LLVMInt32TypeInContext(C: ContextRef) -> TypeRef;
272     fn LLVMInt64TypeInContext(C: ContextRef) -> TypeRef;
273     fn LLVMIntTypeInContext(C: ContextRef, NumBits: c_uint) -> TypeRef;
274
275     fn LLVMInt1Type() -> TypeRef;
276     fn LLVMInt8Type() -> TypeRef;
277     fn LLVMInt16Type() -> TypeRef;
278     fn LLVMInt32Type() -> TypeRef;
279     fn LLVMInt64Type() -> TypeRef;
280     fn LLVMIntType(NumBits: c_uint) -> TypeRef;
281     fn LLVMGetIntTypeWidth(IntegerTy: TypeRef) -> c_uint;
282
283     /* Operations on real types */
284     fn LLVMFloatTypeInContext(C: ContextRef) -> TypeRef;
285     fn LLVMDoubleTypeInContext(C: ContextRef) -> TypeRef;
286     fn LLVMX86FP80TypeInContext(C: ContextRef) -> TypeRef;
287     fn LLVMFP128TypeInContext(C: ContextRef) -> TypeRef;
288     fn LLVMPPCFP128TypeInContext(C: ContextRef) -> TypeRef;
289
290     fn LLVMFloatType() -> TypeRef;
291     fn LLVMDoubleType() -> TypeRef;
292     fn LLVMX86FP80Type() -> TypeRef;
293     fn LLVMFP128Type() -> TypeRef;
294     fn LLVMPPCFP128Type() -> TypeRef;
295
296     /* Operations on function types */
297     fn LLVMFunctionType(ReturnType: TypeRef, ParamTypes: *TypeRef,
298                         ParamCount: c_uint, IsVarArg: Bool) -> TypeRef;
299     fn LLVMIsFunctionVarArg(FunctionTy: TypeRef) -> Bool;
300     fn LLVMGetReturnType(FunctionTy: TypeRef) -> TypeRef;
301     fn LLVMCountParamTypes(FunctionTy: TypeRef) -> c_uint;
302     fn LLVMGetParamTypes(FunctionTy: TypeRef, Dest: *TypeRef);
303
304     /* Operations on struct types */
305     fn LLVMStructTypeInContext(C: ContextRef, ElementTypes: *TypeRef,
306                                ElementCount: c_uint,
307                                Packed: Bool) -> TypeRef;
308     fn LLVMStructType(ElementTypes: *TypeRef, ElementCount: c_uint,
309                       Packed: Bool) -> TypeRef;
310     fn LLVMCountStructElementTypes(StructTy: TypeRef) -> c_uint;
311     fn LLVMGetStructElementTypes(StructTy: TypeRef, Dest: *TypeRef);
312     fn LLVMIsPackedStruct(StructTy: TypeRef) -> Bool;
313
314     /* Operations on array, pointer, and vector types (sequence types) */
315     fn LLVMArrayType(ElementType: TypeRef,
316                      ElementCount: c_uint) -> TypeRef;
317     fn LLVMPointerType(ElementType: TypeRef,
318                        AddressSpace: c_uint) -> TypeRef;
319     fn LLVMVectorType(ElementType: TypeRef,
320                       ElementCount: c_uint) -> TypeRef;
321
322     fn LLVMGetElementType(Ty: TypeRef) -> TypeRef;
323     fn LLVMGetArrayLength(ArrayTy: TypeRef) -> c_uint;
324     fn LLVMGetPointerAddressSpace(PointerTy: TypeRef) -> c_uint;
325     fn LLVMGetVectorSize(VectorTy: TypeRef) -> c_uint;
326
327     /* Operations on other types */
328     fn LLVMVoidTypeInContext(C: ContextRef) -> TypeRef;
329     fn LLVMLabelTypeInContext(C: ContextRef) -> TypeRef;
330     fn LLVMMetadataTypeInContext(C: ContextRef) -> TypeRef;
331
332     fn LLVMVoidType() -> TypeRef;
333     fn LLVMLabelType() -> TypeRef;
334     fn LLVMMetadataType() -> TypeRef;
335
336     /* Operations on all values */
337     fn LLVMTypeOf(Val: ValueRef) -> TypeRef;
338     fn LLVMGetValueName(Val: ValueRef) -> *c_char;
339     fn LLVMSetValueName(Val: ValueRef, Name: *c_char);
340     fn LLVMDumpValue(Val: ValueRef);
341     fn LLVMReplaceAllUsesWith(OldVal: ValueRef, NewVal: ValueRef);
342     fn LLVMHasMetadata(Val: ValueRef) -> c_int;
343     fn LLVMGetMetadata(Val: ValueRef, KindID: c_uint) -> ValueRef;
344     fn LLVMSetMetadata(Val: ValueRef, KindID: c_uint, Node: ValueRef);
345
346     /* Operations on Uses */
347     fn LLVMGetFirstUse(Val: ValueRef) -> UseRef;
348     fn LLVMGetNextUse(U: UseRef) -> UseRef;
349     fn LLVMGetUser(U: UseRef) -> ValueRef;
350     fn LLVMGetUsedValue(U: UseRef) -> ValueRef;
351
352     /* Operations on Users */
353     fn LLVMGetOperand(Val: ValueRef, Index: c_uint) -> ValueRef;
354     fn LLVMSetOperand(Val: ValueRef, Index: c_uint, Op: ValueRef);
355
356     /* Operations on constants of any type */
357     fn LLVMConstNull(Ty: TypeRef) -> ValueRef;
358     /* all zeroes */
359     fn LLVMConstAllOnes(Ty: TypeRef) -> ValueRef;
360     /* only for int/vector */
361     fn LLVMGetUndef(Ty: TypeRef) -> ValueRef;
362     fn LLVMIsConstant(Val: ValueRef) -> Bool;
363     fn LLVMIsNull(Val: ValueRef) -> Bool;
364     fn LLVMIsUndef(Val: ValueRef) -> Bool;
365     fn LLVMConstPointerNull(Ty: TypeRef) -> ValueRef;
366
367     /* Operations on metadata */
368     fn LLVMMDStringInContext(C: ContextRef, Str: *c_char, SLen: c_uint) ->
369        ValueRef;
370     fn LLVMMDString(Str: *c_char, SLen: c_uint) -> ValueRef;
371     fn LLVMMDNodeInContext(C: ContextRef, Vals: *ValueRef, Count: c_uint) ->
372        ValueRef;
373     fn LLVMMDNode(Vals: *ValueRef, Count: c_uint) -> ValueRef;
374     fn LLVMAddNamedMetadataOperand(M: ModuleRef, Str: *c_char,
375                                    Val: ValueRef);
376
377     /* Operations on scalar constants */
378     fn LLVMConstInt(IntTy: TypeRef, N: c_ulonglong, SignExtend: Bool) ->
379        ValueRef;
380     fn LLVMConstIntOfString(IntTy: TypeRef, Text: *c_char, Radix: u8) ->
381        ValueRef;
382     fn LLVMConstIntOfStringAndSize(IntTy: TypeRef, Text: *c_char,
383                                    SLen: c_uint,
384                                    Radix: u8) -> ValueRef;
385     fn LLVMConstReal(RealTy: TypeRef, N: f64) -> ValueRef;
386     fn LLVMConstRealOfString(RealTy: TypeRef, Text: *c_char) -> ValueRef;
387     fn LLVMConstRealOfStringAndSize(RealTy: TypeRef, Text: *c_char,
388                                     SLen: c_uint) -> ValueRef;
389     fn LLVMConstIntGetZExtValue(ConstantVal: ValueRef) -> c_ulonglong;
390     fn LLVMConstIntGetSExtValue(ConstantVal: ValueRef) -> c_longlong;
391
392
393     /* Operations on composite constants */
394     fn LLVMConstStringInContext(C: ContextRef, Str: *c_char, Length: c_uint,
395                                 DontNullTerminate: Bool) -> ValueRef;
396     fn LLVMConstStructInContext(C: ContextRef, ConstantVals: *ValueRef,
397                                 Count: c_uint, Packed: Bool) -> ValueRef;
398
399     fn LLVMConstString(Str: *c_char, Length: c_uint,
400                        DontNullTerminate: Bool) -> ValueRef;
401     fn LLVMConstArray(ElementTy: TypeRef, ConstantVals: *ValueRef,
402                       Length: c_uint) -> ValueRef;
403     fn LLVMConstStruct(ConstantVals: *ValueRef,
404                        Count: c_uint, Packed: Bool) -> ValueRef;
405     fn LLVMConstVector(ScalarConstantVals: *ValueRef,
406                        Size: c_uint) -> ValueRef;
407
408     /* Constant expressions */
409     fn LLVMAlignOf(Ty: TypeRef) -> ValueRef;
410     fn LLVMSizeOf(Ty: TypeRef) -> ValueRef;
411     fn LLVMConstNeg(ConstantVal: ValueRef) -> ValueRef;
412     fn LLVMConstNSWNeg(ConstantVal: ValueRef) -> ValueRef;
413     fn LLVMConstNUWNeg(ConstantVal: ValueRef) -> ValueRef;
414     fn LLVMConstFNeg(ConstantVal: ValueRef) -> ValueRef;
415     fn LLVMConstNot(ConstantVal: ValueRef) -> ValueRef;
416     fn LLVMConstAdd(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
417     fn LLVMConstNSWAdd(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
418        ValueRef;
419     fn LLVMConstNUWAdd(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
420        ValueRef;
421     fn LLVMConstFAdd(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
422        ValueRef;
423     fn LLVMConstSub(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
424     fn LLVMConstNSWSub(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
425        ValueRef;
426     fn LLVMConstNUWSub(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
427        ValueRef;
428     fn LLVMConstFSub(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
429        ValueRef;
430     fn LLVMConstMul(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
431     fn LLVMConstNSWMul(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
432        ValueRef;
433     fn LLVMConstNUWMul(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
434        ValueRef;
435     fn LLVMConstFMul(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
436        ValueRef;
437     fn LLVMConstUDiv(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
438        ValueRef;
439     fn LLVMConstSDiv(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
440        ValueRef;
441     fn LLVMConstExactSDiv(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
442        ValueRef;
443     fn LLVMConstFDiv(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
444        ValueRef;
445     fn LLVMConstURem(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
446        ValueRef;
447     fn LLVMConstSRem(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
448        ValueRef;
449     fn LLVMConstFRem(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
450        ValueRef;
451     fn LLVMConstAnd(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
452     fn LLVMConstOr(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
453     fn LLVMConstXor(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
454     fn LLVMConstShl(LHSConstant: ValueRef, RHSConstant: ValueRef) -> ValueRef;
455     fn LLVMConstLShr(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
456        ValueRef;
457     fn LLVMConstAShr(LHSConstant: ValueRef, RHSConstant: ValueRef) ->
458        ValueRef;
459     fn LLVMConstGEP(ConstantVal: ValueRef, ConstantIndices: *uint,
460                     NumIndices: c_uint) -> ValueRef;
461     fn LLVMConstInBoundsGEP(ConstantVal: ValueRef, ConstantIndices: *uint,
462                             NumIndices: c_uint) -> ValueRef;
463     fn LLVMConstTrunc(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
464     fn LLVMConstSExt(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
465     fn LLVMConstZExt(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
466     fn LLVMConstFPTrunc(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
467     fn LLVMConstFPExt(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
468     fn LLVMConstUIToFP(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
469     fn LLVMConstSIToFP(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
470     fn LLVMConstFPToUI(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
471     fn LLVMConstFPToSI(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
472     fn LLVMConstPtrToInt(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
473     fn LLVMConstIntToPtr(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
474     fn LLVMConstBitCast(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
475     fn LLVMConstZExtOrBitCast(ConstantVal: ValueRef, ToType: TypeRef) ->
476        ValueRef;
477     fn LLVMConstSExtOrBitCast(ConstantVal: ValueRef, ToType: TypeRef) ->
478        ValueRef;
479     fn LLVMConstTruncOrBitCast(ConstantVal: ValueRef, ToType: TypeRef) ->
480        ValueRef;
481     fn LLVMConstPointerCast(ConstantVal: ValueRef, ToType: TypeRef) ->
482        ValueRef;
483     fn LLVMConstIntCast(ConstantVal: ValueRef, ToType: TypeRef,
484                         isSigned: Bool) -> ValueRef;
485     fn LLVMConstFPCast(ConstantVal: ValueRef, ToType: TypeRef) -> ValueRef;
486     fn LLVMConstSelect(ConstantCondition: ValueRef, ConstantIfTrue: ValueRef,
487                        ConstantIfFalse: ValueRef) -> ValueRef;
488     fn LLVMConstExtractElement(VectorConstant: ValueRef,
489                                IndexConstant: ValueRef) -> ValueRef;
490     fn LLVMConstInsertElement(VectorConstant: ValueRef,
491                               ElementValueConstant: ValueRef,
492                               IndexConstant: ValueRef) -> ValueRef;
493     fn LLVMConstShuffleVector(VectorAConstant: ValueRef,
494                               VectorBConstant: ValueRef,
495                               MaskConstant: ValueRef) -> ValueRef;
496     fn LLVMConstExtractValue(AggConstant: ValueRef, IdxList: *uint,
497                              NumIdx: c_uint) -> ValueRef;
498     fn LLVMConstInsertValue(AggConstant: ValueRef,
499                             ElementValueConstant: ValueRef, IdxList: *uint,
500                             NumIdx: c_uint) -> ValueRef;
501     fn LLVMConstInlineAsm(Ty: TypeRef, AsmString: *c_char,
502                           Constraints: *c_char, HasSideEffects: Bool,
503                           IsAlignStack: Bool) -> ValueRef;
504     fn LLVMBlockAddress(F: ValueRef, BB: BasicBlockRef) -> ValueRef;
505
506
507
508     /* Operations on global variables, functions, and aliases (globals) */
509     fn LLVMGetGlobalParent(Global: ValueRef) -> ModuleRef;
510     fn LLVMIsDeclaration(Global: ValueRef) -> Bool;
511     fn LLVMGetLinkage(Global: ValueRef) -> c_uint;
512     fn LLVMSetLinkage(Global: ValueRef, Link: c_uint);
513     fn LLVMGetSection(Global: ValueRef) -> *c_char;
514     fn LLVMSetSection(Global: ValueRef, Section: *c_char);
515     fn LLVMGetVisibility(Global: ValueRef) -> c_uint;
516     fn LLVMSetVisibility(Global: ValueRef, Viz: c_uint);
517     fn LLVMGetAlignment(Global: ValueRef) -> c_uint;
518     fn LLVMSetAlignment(Global: ValueRef, Bytes: c_uint);
519
520
521     /* Operations on global variables */
522     fn LLVMAddGlobal(M: ModuleRef, Ty: TypeRef, Name: *c_char) -> ValueRef;
523     fn LLVMAddGlobalInAddressSpace(M: ModuleRef, Ty: TypeRef, Name: *c_char,
524                                    AddressSpace: c_uint) -> ValueRef;
525     fn LLVMGetNamedGlobal(M: ModuleRef, Name: *c_char) -> ValueRef;
526     fn LLVMGetFirstGlobal(M: ModuleRef) -> ValueRef;
527     fn LLVMGetLastGlobal(M: ModuleRef) -> ValueRef;
528     fn LLVMGetNextGlobal(GlobalVar: ValueRef) -> ValueRef;
529     fn LLVMGetPreviousGlobal(GlobalVar: ValueRef) -> ValueRef;
530     fn LLVMDeleteGlobal(GlobalVar: ValueRef);
531     fn LLVMGetInitializer(GlobalVar: ValueRef) -> ValueRef;
532     fn LLVMSetInitializer(GlobalVar: ValueRef, ConstantVal: ValueRef);
533     fn LLVMIsThreadLocal(GlobalVar: ValueRef) -> Bool;
534     fn LLVMSetThreadLocal(GlobalVar: ValueRef, IsThreadLocal: Bool);
535     fn LLVMIsGlobalConstant(GlobalVar: ValueRef) -> Bool;
536     fn LLVMSetGlobalConstant(GlobalVar: ValueRef, IsConstant: Bool);
537
538     /* Operations on aliases */
539     fn LLVMAddAlias(M: ModuleRef, Ty: TypeRef, Aliasee: ValueRef,
540                     Name: *c_char) -> ValueRef;
541
542     /* Operations on functions */
543     fn LLVMAddFunction(M: ModuleRef, Name: *c_char, FunctionTy: TypeRef) ->
544        ValueRef;
545     fn LLVMGetNamedFunction(M: ModuleRef, Name: *c_char) -> ValueRef;
546     fn LLVMGetFirstFunction(M: ModuleRef) -> ValueRef;
547     fn LLVMGetLastFunction(M: ModuleRef) -> ValueRef;
548     fn LLVMGetNextFunction(Fn: ValueRef) -> ValueRef;
549     fn LLVMGetPreviousFunction(Fn: ValueRef) -> ValueRef;
550     fn LLVMDeleteFunction(Fn: ValueRef);
551     fn LLVMGetOrInsertFunction(M: ModuleRef, Name: *c_char,
552                                FunctionTy: TypeRef) -> ValueRef;
553     fn LLVMGetIntrinsicID(Fn: ValueRef) -> c_uint;
554     fn LLVMGetFunctionCallConv(Fn: ValueRef) -> c_uint;
555     fn LLVMSetFunctionCallConv(Fn: ValueRef, CC: c_uint);
556     fn LLVMGetGC(Fn: ValueRef) -> *c_char;
557     fn LLVMSetGC(Fn: ValueRef, Name: *c_char);
558     fn LLVMAddFunctionAttr(Fn: ValueRef, PA: c_ulonglong, HighPA:
559                            c_ulonglong);
560     fn LLVMGetFunctionAttr(Fn: ValueRef) -> c_ulonglong;
561     fn LLVMRemoveFunctionAttr(Fn: ValueRef, PA: c_ulonglong, HighPA:
562                               c_ulonglong);
563
564     /* Operations on parameters */
565     fn LLVMCountParams(Fn: ValueRef) -> c_uint;
566     fn LLVMGetParams(Fn: ValueRef, Params: *ValueRef);
567     fn LLVMGetParam(Fn: ValueRef, Index: c_uint) -> ValueRef;
568     fn LLVMGetParamParent(Inst: ValueRef) -> ValueRef;
569     fn LLVMGetFirstParam(Fn: ValueRef) -> ValueRef;
570     fn LLVMGetLastParam(Fn: ValueRef) -> ValueRef;
571     fn LLVMGetNextParam(Arg: ValueRef) -> ValueRef;
572     fn LLVMGetPreviousParam(Arg: ValueRef) -> ValueRef;
573     fn LLVMAddAttribute(Arg: ValueRef, PA: c_uint);
574     fn LLVMRemoveAttribute(Arg: ValueRef, PA: c_uint);
575     fn LLVMGetAttribute(Arg: ValueRef) -> c_uint;
576     fn LLVMSetParamAlignment(Arg: ValueRef, align: c_uint);
577
578     /* Operations on basic blocks */
579     fn LLVMBasicBlockAsValue(BB: BasicBlockRef) -> ValueRef;
580     fn LLVMValueIsBasicBlock(Val: ValueRef) -> Bool;
581     fn LLVMValueAsBasicBlock(Val: ValueRef) -> BasicBlockRef;
582     fn LLVMGetBasicBlockParent(BB: BasicBlockRef) -> ValueRef;
583     fn LLVMCountBasicBlocks(Fn: ValueRef) -> c_uint;
584     fn LLVMGetBasicBlocks(Fn: ValueRef, BasicBlocks: *ValueRef);
585     fn LLVMGetFirstBasicBlock(Fn: ValueRef) -> BasicBlockRef;
586     fn LLVMGetLastBasicBlock(Fn: ValueRef) -> BasicBlockRef;
587     fn LLVMGetNextBasicBlock(BB: BasicBlockRef) -> BasicBlockRef;
588     fn LLVMGetPreviousBasicBlock(BB: BasicBlockRef) -> BasicBlockRef;
589     fn LLVMGetEntryBasicBlock(Fn: ValueRef) -> BasicBlockRef;
590
591     fn LLVMAppendBasicBlockInContext(C: ContextRef, Fn: ValueRef,
592                                      Name: *c_char) -> BasicBlockRef;
593     fn LLVMInsertBasicBlockInContext(C: ContextRef, BB: BasicBlockRef,
594                                      Name: *c_char) -> BasicBlockRef;
595
596     fn LLVMAppendBasicBlock(Fn: ValueRef, Name: *c_char) -> BasicBlockRef;
597     fn LLVMInsertBasicBlock(InsertBeforeBB: BasicBlockRef, Name: *c_char) ->
598        BasicBlockRef;
599     fn LLVMDeleteBasicBlock(BB: BasicBlockRef);
600
601     /* Operations on instructions */
602     fn LLVMGetInstructionParent(Inst: ValueRef) -> BasicBlockRef;
603     fn LLVMGetFirstInstruction(BB: BasicBlockRef) -> ValueRef;
604     fn LLVMGetLastInstruction(BB: BasicBlockRef) -> ValueRef;
605     fn LLVMGetNextInstruction(Inst: ValueRef) -> ValueRef;
606     fn LLVMGetPreviousInstruction(Inst: ValueRef) -> ValueRef;
607
608     /* Operations on call sites */
609     fn LLVMSetInstructionCallConv(Instr: ValueRef, CC: c_uint);
610     fn LLVMGetInstructionCallConv(Instr: ValueRef) -> c_uint;
611     fn LLVMAddInstrAttribute(Instr: ValueRef, index: c_uint, IA: c_uint);
612     fn LLVMRemoveInstrAttribute(Instr: ValueRef, index: c_uint,
613                                 IA: c_uint);
614     fn LLVMSetInstrParamAlignment(Instr: ValueRef, index: c_uint,
615                                   align: c_uint);
616
617     /* Operations on call instructions (only) */
618     fn LLVMIsTailCall(CallInst: ValueRef) -> Bool;
619     fn LLVMSetTailCall(CallInst: ValueRef, IsTailCall: Bool);
620
621     /* Operations on phi nodes */
622     fn LLVMAddIncoming(PhiNode: ValueRef, IncomingValues: *ValueRef,
623                        IncomingBlocks: *BasicBlockRef, Count: c_uint);
624     fn LLVMCountIncoming(PhiNode: ValueRef) -> c_uint;
625     fn LLVMGetIncomingValue(PhiNode: ValueRef, Index: c_uint) -> ValueRef;
626     fn LLVMGetIncomingBlock(PhiNode: ValueRef,
627                             Index: c_uint) -> BasicBlockRef;
628
629     /* Instruction builders */
630     fn LLVMCreateBuilderInContext(C: ContextRef) -> BuilderRef;
631     fn LLVMCreateBuilder() -> BuilderRef;
632     fn LLVMPositionBuilder(Builder: BuilderRef, Block: BasicBlockRef,
633                            Instr: ValueRef);
634     fn LLVMPositionBuilderBefore(Builder: BuilderRef, Instr: ValueRef);
635     fn LLVMPositionBuilderAtEnd(Builder: BuilderRef, Block: BasicBlockRef);
636     fn LLVMGetInsertBlock(Builder: BuilderRef) -> BasicBlockRef;
637     fn LLVMClearInsertionPosition(Builder: BuilderRef);
638     fn LLVMInsertIntoBuilder(Builder: BuilderRef, Instr: ValueRef);
639     fn LLVMInsertIntoBuilderWithName(Builder: BuilderRef, Instr: ValueRef,
640                                      Name: *c_char);
641     fn LLVMDisposeBuilder(Builder: BuilderRef);
642
643     /* Metadata */
644     fn LLVMSetCurrentDebugLocation(Builder: BuilderRef, L: ValueRef);
645     fn LLVMGetCurrentDebugLocation(Builder: BuilderRef) -> ValueRef;
646     fn LLVMSetInstDebugLocation(Builder: BuilderRef, Inst: ValueRef);
647
648     /* Terminators */
649     fn LLVMBuildRetVoid(B: BuilderRef) -> ValueRef;
650     fn LLVMBuildRet(B: BuilderRef, V: ValueRef) -> ValueRef;
651     fn LLVMBuildAggregateRet(B: BuilderRef, RetVals: *ValueRef,
652                              N: c_uint) -> ValueRef;
653     fn LLVMBuildBr(B: BuilderRef, Dest: BasicBlockRef) -> ValueRef;
654     fn LLVMBuildCondBr(B: BuilderRef, If: ValueRef, Then: BasicBlockRef,
655                        Else: BasicBlockRef) -> ValueRef;
656     fn LLVMBuildSwitch(B: BuilderRef, V: ValueRef, Else: BasicBlockRef,
657                        NumCases: c_uint) -> ValueRef;
658     fn LLVMBuildIndirectBr(B: BuilderRef, Addr: ValueRef,
659                            NumDests: c_uint) -> ValueRef;
660     fn LLVMBuildInvoke(B: BuilderRef, Fn: ValueRef, Args: *ValueRef,
661                        NumArgs: c_uint, Then: BasicBlockRef,
662                        Catch: BasicBlockRef, Name: *c_char) -> ValueRef;
663     fn LLVMBuildLandingPad(B: BuilderRef, Ty: TypeRef, PersFn: ValueRef,
664                            NumClauses: c_uint, Name: *c_char) -> ValueRef;
665     fn LLVMBuildResume(B: BuilderRef, Exn: ValueRef) -> ValueRef;
666     fn LLVMBuildUnreachable(B: BuilderRef) -> ValueRef;
667
668     /* Add a case to the switch instruction */
669     fn LLVMAddCase(Switch: ValueRef, OnVal: ValueRef, Dest: BasicBlockRef);
670
671     /* Add a destination to the indirectbr instruction */
672     fn LLVMAddDestination(IndirectBr: ValueRef, Dest: BasicBlockRef);
673
674     /* Add a clause to the landing pad instruction */
675     fn LLVMAddClause(LandingPad: ValueRef, ClauseVal: ValueRef);
676
677     /* Set the cleanup on a landing pad instruction */
678     fn LLVMSetCleanup(LandingPad: ValueRef, Val: Bool);
679
680     /* Arithmetic */
681     fn LLVMBuildAdd(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
682                     Name: *c_char) -> ValueRef;
683     fn LLVMBuildNSWAdd(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
684                        Name: *c_char) -> ValueRef;
685     fn LLVMBuildNUWAdd(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
686                        Name: *c_char) -> ValueRef;
687     fn LLVMBuildFAdd(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
688                      Name: *c_char) -> ValueRef;
689     fn LLVMBuildSub(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
690                     Name: *c_char) -> ValueRef;
691     fn LLVMBuildNSWSub(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
692                        Name: *c_char) -> ValueRef;
693     fn LLVMBuildNUWSub(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
694                        Name: *c_char) -> ValueRef;
695     fn LLVMBuildFSub(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
696                      Name: *c_char) -> ValueRef;
697     fn LLVMBuildMul(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
698                     Name: *c_char) -> ValueRef;
699     fn LLVMBuildNSWMul(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
700                        Name: *c_char) -> ValueRef;
701     fn LLVMBuildNUWMul(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
702                        Name: *c_char) -> ValueRef;
703     fn LLVMBuildFMul(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
704                      Name: *c_char) -> ValueRef;
705     fn LLVMBuildUDiv(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
706                      Name: *c_char) -> ValueRef;
707     fn LLVMBuildSDiv(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
708                      Name: *c_char) -> ValueRef;
709     fn LLVMBuildExactSDiv(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
710                           Name: *c_char) -> ValueRef;
711     fn LLVMBuildFDiv(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
712                      Name: *c_char) -> ValueRef;
713     fn LLVMBuildURem(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
714                      Name: *c_char) -> ValueRef;
715     fn LLVMBuildSRem(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
716                      Name: *c_char) -> ValueRef;
717     fn LLVMBuildFRem(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
718                      Name: *c_char) -> ValueRef;
719     fn LLVMBuildShl(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
720                     Name: *c_char) -> ValueRef;
721     fn LLVMBuildLShr(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
722                      Name: *c_char) -> ValueRef;
723     fn LLVMBuildAShr(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
724                      Name: *c_char) -> ValueRef;
725     fn LLVMBuildAnd(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
726                     Name: *c_char) -> ValueRef;
727     fn LLVMBuildOr(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
728                    Name: *c_char) -> ValueRef;
729     fn LLVMBuildXor(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
730                     Name: *c_char) -> ValueRef;
731     fn LLVMBuildBinOp(B: BuilderRef, Op: Opcode, LHS: ValueRef, RHS: ValueRef,
732                       Name: *c_char) -> ValueRef;
733     fn LLVMBuildNeg(B: BuilderRef, V: ValueRef, Name: *c_char) -> ValueRef;
734     fn LLVMBuildNSWNeg(B: BuilderRef, V: ValueRef, Name: *c_char) -> ValueRef;
735     fn LLVMBuildNUWNeg(B: BuilderRef, V: ValueRef, Name: *c_char) -> ValueRef;
736     fn LLVMBuildFNeg(B: BuilderRef, V: ValueRef, Name: *c_char) -> ValueRef;
737     fn LLVMBuildNot(B: BuilderRef, V: ValueRef, Name: *c_char) -> ValueRef;
738
739     /* Memory */
740     fn LLVMBuildMalloc(B: BuilderRef, Ty: TypeRef, Name: *c_char) -> ValueRef;
741     fn LLVMBuildArrayMalloc(B: BuilderRef, Ty: TypeRef, Val: ValueRef,
742                             Name: *c_char) -> ValueRef;
743     fn LLVMBuildAlloca(B: BuilderRef, Ty: TypeRef, Name: *c_char) -> ValueRef;
744     fn LLVMBuildArrayAlloca(B: BuilderRef, Ty: TypeRef, Val: ValueRef,
745                             Name: *c_char) -> ValueRef;
746     fn LLVMBuildFree(B: BuilderRef, PointerVal: ValueRef) -> ValueRef;
747     fn LLVMBuildLoad(B: BuilderRef, PointerVal: ValueRef, Name: *c_char) ->
748        ValueRef;
749     fn LLVMBuildStore(B: BuilderRef, Val: ValueRef, Ptr: ValueRef) ->
750        ValueRef;
751     fn LLVMBuildGEP(B: BuilderRef, Pointer: ValueRef, Indices: *ValueRef,
752                     NumIndices: c_uint, Name: *c_char) -> ValueRef;
753     fn LLVMBuildInBoundsGEP(B: BuilderRef, Pointer: ValueRef,
754                             Indices: *ValueRef, NumIndices: c_uint,
755                             Name: *c_char)
756        -> ValueRef;
757     fn LLVMBuildStructGEP(B: BuilderRef, Pointer: ValueRef, Idx: c_uint,
758                           Name: *c_char) -> ValueRef;
759     fn LLVMBuildGlobalString(B: BuilderRef, Str: *c_char, Name: *c_char) ->
760        ValueRef;
761     fn LLVMBuildGlobalStringPtr(B: BuilderRef, Str: *c_char, Name: *c_char) ->
762        ValueRef;
763
764     /* Casts */
765     fn LLVMBuildTrunc(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
766                       Name: *c_char) -> ValueRef;
767     fn LLVMBuildZExt(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
768                      Name: *c_char) -> ValueRef;
769     fn LLVMBuildSExt(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
770                      Name: *c_char) -> ValueRef;
771     fn LLVMBuildFPToUI(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
772                        Name: *c_char) -> ValueRef;
773     fn LLVMBuildFPToSI(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
774                        Name: *c_char) -> ValueRef;
775     fn LLVMBuildUIToFP(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
776                        Name: *c_char) -> ValueRef;
777     fn LLVMBuildSIToFP(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
778                        Name: *c_char) -> ValueRef;
779     fn LLVMBuildFPTrunc(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
780                         Name: *c_char) -> ValueRef;
781     fn LLVMBuildFPExt(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
782                       Name: *c_char) -> ValueRef;
783     fn LLVMBuildPtrToInt(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
784                          Name: *c_char) -> ValueRef;
785     fn LLVMBuildIntToPtr(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
786                          Name: *c_char) -> ValueRef;
787     fn LLVMBuildBitCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
788                         Name: *c_char) -> ValueRef;
789     fn LLVMBuildZExtOrBitCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
790                               Name: *c_char) -> ValueRef;
791     fn LLVMBuildSExtOrBitCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
792                               Name: *c_char) -> ValueRef;
793     fn LLVMBuildTruncOrBitCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
794                                Name: *c_char) -> ValueRef;
795     fn LLVMBuildCast(B: BuilderRef, Op: Opcode, Val: ValueRef,
796                      DestTy: TypeRef, Name: *c_char) -> ValueRef;
797     fn LLVMBuildPointerCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
798                             Name: *c_char) -> ValueRef;
799     fn LLVMBuildIntCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
800                         Name: *c_char) -> ValueRef;
801     fn LLVMBuildFPCast(B: BuilderRef, Val: ValueRef, DestTy: TypeRef,
802                        Name: *c_char) -> ValueRef;
803
804     /* Comparisons */
805     fn LLVMBuildICmp(B: BuilderRef, Op: c_uint, LHS: ValueRef,
806                      RHS: ValueRef, Name: *c_char) -> ValueRef;
807     fn LLVMBuildFCmp(B: BuilderRef, Op: c_uint, LHS: ValueRef,
808                      RHS: ValueRef, Name: *c_char) -> ValueRef;
809
810     /* Miscellaneous instructions */
811     fn LLVMBuildPhi(B: BuilderRef, Ty: TypeRef, Name: *c_char) -> ValueRef;
812     fn LLVMBuildCall(B: BuilderRef, Fn: ValueRef, Args: *ValueRef,
813                      NumArgs: c_uint, Name: *c_char) -> ValueRef;
814     fn LLVMBuildSelect(B: BuilderRef, If: ValueRef, Then: ValueRef,
815                        Else: ValueRef, Name: *c_char) -> ValueRef;
816     fn LLVMBuildVAArg(B: BuilderRef, list: ValueRef, Ty: TypeRef,
817                       Name: *c_char)
818        -> ValueRef;
819     fn LLVMBuildExtractElement(B: BuilderRef, VecVal: ValueRef,
820                                Index: ValueRef, Name: *c_char) -> ValueRef;
821     fn LLVMBuildInsertElement(B: BuilderRef, VecVal: ValueRef,
822                               EltVal: ValueRef, Index: ValueRef,
823                               Name: *c_char)
824        -> ValueRef;
825     fn LLVMBuildShuffleVector(B: BuilderRef, V1: ValueRef, V2: ValueRef,
826                               Mask: ValueRef, Name: *c_char) -> ValueRef;
827     fn LLVMBuildExtractValue(B: BuilderRef, AggVal: ValueRef, Index: c_uint,
828                              Name: *c_char) -> ValueRef;
829     fn LLVMBuildInsertValue(B: BuilderRef, AggVal: ValueRef, EltVal: ValueRef,
830                             Index: c_uint, Name: *c_char) -> ValueRef;
831
832     fn LLVMBuildIsNull(B: BuilderRef, Val: ValueRef,
833                        Name: *c_char) -> ValueRef;
834     fn LLVMBuildIsNotNull(B: BuilderRef, Val: ValueRef, Name: *c_char) ->
835        ValueRef;
836     fn LLVMBuildPtrDiff(B: BuilderRef, LHS: ValueRef, RHS: ValueRef,
837                         Name: *c_char) -> ValueRef;
838
839     /* Atomic Operations */
840     fn LLVMBuildAtomicRMW(B: BuilderRef, ++Op: AtomicBinOp,
841                           LHS: ValueRef, RHS: ValueRef,
842                           ++Order: AtomicOrdering) -> ValueRef;
843
844     /* Selected entries from the downcasts. */
845     fn LLVMIsATerminatorInst(Inst: ValueRef) -> ValueRef;
846
847     /** Writes a module to the specified path. Returns 0 on success. */
848     fn LLVMWriteBitcodeToFile(M: ModuleRef, Path: *c_char) -> c_int;
849
850     /** Creates target data from a target layout string. */
851     fn LLVMCreateTargetData(StringRep: *c_char) -> TargetDataRef;
852     /** Adds the target data to the given pass manager. The pass manager
853         references the target data only weakly. */
854     fn LLVMAddTargetData(TD: TargetDataRef, PM: PassManagerRef);
855     /** Number of bytes clobbered when doing a Store to *T. */
856     fn LLVMStoreSizeOfType(TD: TargetDataRef, Ty: TypeRef) -> c_ulonglong;
857
858     /** Number of bytes clobbered when doing a Store to *T. */
859     fn LLVMSizeOfTypeInBits(TD: TargetDataRef, Ty: TypeRef) -> c_ulonglong;
860
861     /** Distance between successive elements in an array of T.
862     Includes ABI padding. */
863     fn LLVMABISizeOfType(TD: TargetDataRef, Ty: TypeRef) -> c_uint;
864
865     /** Returns the preferred alignment of a type. */
866     fn LLVMPreferredAlignmentOfType(TD: TargetDataRef,
867                                     Ty: TypeRef) -> c_uint;
868     /** Returns the minimum alignment of a type. */
869     fn LLVMABIAlignmentOfType(TD: TargetDataRef,
870                               Ty: TypeRef) -> c_uint;
871     /** Returns the minimum alignment of a type when part of a call frame. */
872     fn LLVMCallFrameAlignmentOfType(TD: TargetDataRef,
873                                     Ty: TypeRef) -> c_uint;
874
875     /** Disposes target data. */
876     fn LLVMDisposeTargetData(TD: TargetDataRef);
877
878     /** Creates a pass manager. */
879     fn LLVMCreatePassManager() -> PassManagerRef;
880     /** Disposes a pass manager. */
881     fn LLVMDisposePassManager(PM: PassManagerRef);
882     /** Runs a pass manager on a module. */
883     fn LLVMRunPassManager(PM: PassManagerRef, M: ModuleRef) -> Bool;
884
885     /** Adds a verification pass. */
886     fn LLVMAddVerifierPass(PM: PassManagerRef);
887
888     fn LLVMAddGlobalOptimizerPass(PM: PassManagerRef);
889     fn LLVMAddIPSCCPPass(PM: PassManagerRef);
890     fn LLVMAddDeadArgEliminationPass(PM: PassManagerRef);
891     fn LLVMAddInstructionCombiningPass(PM: PassManagerRef);
892     fn LLVMAddCFGSimplificationPass(PM: PassManagerRef);
893     fn LLVMAddFunctionInliningPass(PM: PassManagerRef);
894     fn LLVMAddFunctionAttrsPass(PM: PassManagerRef);
895     fn LLVMAddScalarReplAggregatesPass(PM: PassManagerRef);
896     fn LLVMAddScalarReplAggregatesPassSSA(PM: PassManagerRef);
897     fn LLVMAddJumpThreadingPass(PM: PassManagerRef);
898     fn LLVMAddConstantPropagationPass(PM: PassManagerRef);
899     fn LLVMAddReassociatePass(PM: PassManagerRef);
900     fn LLVMAddLoopRotatePass(PM: PassManagerRef);
901     fn LLVMAddLICMPass(PM: PassManagerRef);
902     fn LLVMAddLoopUnswitchPass(PM: PassManagerRef);
903     fn LLVMAddLoopDeletionPass(PM: PassManagerRef);
904     fn LLVMAddLoopUnrollPass(PM: PassManagerRef);
905     fn LLVMAddGVNPass(PM: PassManagerRef);
906     fn LLVMAddMemCpyOptPass(PM: PassManagerRef);
907     fn LLVMAddSCCPPass(PM: PassManagerRef);
908     fn LLVMAddDeadStoreEliminationPass(PM: PassManagerRef);
909     fn LLVMAddStripDeadPrototypesPass(PM: PassManagerRef);
910     fn LLVMAddConstantMergePass(PM: PassManagerRef);
911     fn LLVMAddArgumentPromotionPass(PM: PassManagerRef);
912     fn LLVMAddTailCallEliminationPass(PM: PassManagerRef);
913     fn LLVMAddIndVarSimplifyPass(PM: PassManagerRef);
914     fn LLVMAddAggressiveDCEPass(PM: PassManagerRef);
915     fn LLVMAddGlobalDCEPass(PM: PassManagerRef);
916     fn LLVMAddCorrelatedValuePropagationPass(PM: PassManagerRef);
917     fn LLVMAddPruneEHPass(PM: PassManagerRef);
918     fn LLVMAddSimplifyLibCallsPass(PM: PassManagerRef);
919     fn LLVMAddLoopIdiomPass(PM: PassManagerRef);
920     fn LLVMAddEarlyCSEPass(PM: PassManagerRef);
921     fn LLVMAddTypeBasedAliasAnalysisPass(PM: PassManagerRef);
922     fn LLVMAddBasicAliasAnalysisPass(PM: PassManagerRef);
923
924     fn LLVMPassManagerBuilderCreate() -> PassManagerBuilderRef;
925     fn LLVMPassManagerBuilderDispose(PMB: PassManagerBuilderRef);
926     fn LLVMPassManagerBuilderSetOptLevel(PMB: PassManagerBuilderRef,
927                                          OptimizationLevel: c_uint);
928     fn LLVMPassManagerBuilderSetSizeLevel(PMB: PassManagerBuilderRef,
929                                           Value: Bool);
930     fn LLVMPassManagerBuilderSetDisableUnitAtATime(PMB: PassManagerBuilderRef,
931                                                    Value: Bool);
932     fn LLVMPassManagerBuilderSetDisableUnrollLoops(PMB: PassManagerBuilderRef,
933                                                    Value: Bool);
934     fn LLVMPassManagerBuilderSetDisableSimplifyLibCalls
935         (PMB: PassManagerBuilderRef, Value: Bool);
936     fn LLVMPassManagerBuilderUseInlinerWithThreshold
937         (PMB: PassManagerBuilderRef, threshold: c_uint);
938     fn LLVMPassManagerBuilderPopulateModulePassManager
939         (PMB: PassManagerBuilderRef, PM: PassManagerRef);
940
941     fn LLVMPassManagerBuilderPopulateFunctionPassManager
942         (PMB: PassManagerBuilderRef, PM: PassManagerRef);
943
944     /** Destroys a memory buffer. */
945     fn LLVMDisposeMemoryBuffer(MemBuf: MemoryBufferRef);
946
947
948     /* Stuff that's in rustllvm/ because it's not upstream yet. */
949
950     /** Opens an object file. */
951     fn LLVMCreateObjectFile(MemBuf: MemoryBufferRef) -> ObjectFileRef;
952     /** Closes an object file. */
953     fn LLVMDisposeObjectFile(ObjectFile: ObjectFileRef);
954
955     /** Enumerates the sections in an object file. */
956     fn LLVMGetSections(ObjectFile: ObjectFileRef) -> SectionIteratorRef;
957     /** Destroys a section iterator. */
958     fn LLVMDisposeSectionIterator(SI: SectionIteratorRef);
959     /** Returns true if the section iterator is at the end of the section
960         list: */
961     fn LLVMIsSectionIteratorAtEnd(ObjectFile: ObjectFileRef,
962                                   SI: SectionIteratorRef) -> Bool;
963     /** Moves the section iterator to point to the next section. */
964     fn LLVMMoveToNextSection(SI: SectionIteratorRef);
965     /** Returns the current section name. */
966     fn LLVMGetSectionName(SI: SectionIteratorRef) -> *c_char;
967     /** Returns the current section size. */
968     fn LLVMGetSectionSize(SI: SectionIteratorRef) -> c_ulonglong;
969     /** Returns the current section contents as a string buffer. */
970     fn LLVMGetSectionContents(SI: SectionIteratorRef) -> *c_char;
971
972     /** Reads the given file and returns it as a memory buffer. Use
973         LLVMDisposeMemoryBuffer() to get rid of it. */
974     fn LLVMRustCreateMemoryBufferWithContentsOfFile(Path: *c_char) ->
975        MemoryBufferRef;
976
977     fn LLVMRustWriteOutputFile(PM: PassManagerRef, M: ModuleRef,
978                                Triple: *c_char,
979                                // FIXME: When #2334 is fixed, change
980                                // c_uint to FileType
981                                Output: *c_char, FileType: c_uint,
982                                OptLevel: c_int,
983                                EnableSegmentedStacks: bool) -> bool;
984
985     /** Returns a string describing the last error caused by an LLVMRust*
986         call. */
987     fn LLVMRustGetLastError() -> *c_char;
988
989     /** Parses the bitcode in the given memory buffer. */
990     fn LLVMRustParseBitcode(MemBuf: MemoryBufferRef) -> ModuleRef;
991
992     /** Parses LLVM asm in the given file */
993     fn LLVMRustParseAssemblyFile(Filename: *c_char) -> ModuleRef;
994
995     fn LLVMRustAddPrintModulePass(PM: PassManagerRef, M: ModuleRef,
996                                   Output: *c_char);
997
998     /** Turn on LLVM pass-timing. */
999     fn LLVMRustEnableTimePasses();
1000
1001     /** Print the pass timings since static dtors aren't picking them up. */
1002     fn LLVMRustPrintPassTimings();
1003
1004     fn LLVMStructCreateNamed(C: ContextRef, Name: *c_char) -> TypeRef;
1005
1006     fn LLVMStructSetBody(StructTy: TypeRef, ElementTypes: *TypeRef,
1007                          ElementCount: c_uint, Packed: Bool);
1008
1009     fn LLVMConstNamedStruct(S: TypeRef, ConstantVals: *ValueRef,
1010                             Count: c_uint) -> ValueRef;
1011
1012     /** Enables LLVM debug output. */
1013     fn LLVMSetDebug(Enabled: c_int);
1014 }
1015
1016 fn SetInstructionCallConv(Instr: ValueRef, CC: CallConv) {
1017     llvm::LLVMSetInstructionCallConv(Instr, CC as c_uint);
1018 }
1019 fn SetFunctionCallConv(Fn: ValueRef, CC: CallConv) {
1020     llvm::LLVMSetFunctionCallConv(Fn, CC as c_uint);
1021 }
1022 fn SetLinkage(Global: ValueRef, Link: Linkage) {
1023     llvm::LLVMSetLinkage(Global, Link as c_uint);
1024 }
1025
1026 /* Memory-managed object interface to type handles. */
1027
1028 type type_names = @{type_names: std::map::hashmap<TypeRef, ~str>,
1029                     named_types: std::map::hashmap<~str, TypeRef>};
1030
1031 fn associate_type(tn: type_names, s: ~str, t: TypeRef) {
1032     assert tn.type_names.insert(t, s);
1033     assert tn.named_types.insert(s, t);
1034 }
1035
1036 fn type_has_name(tn: type_names, t: TypeRef) -> Option<~str> {
1037     return tn.type_names.find(t);
1038 }
1039
1040 fn name_has_type(tn: type_names, s: ~str) -> Option<TypeRef> {
1041     return tn.named_types.find(s);
1042 }
1043
1044 fn mk_type_names() -> type_names {
1045     pure fn hash(t: &TypeRef) -> uint { *t as uint }
1046     pure fn eq(a: &TypeRef, b: &TypeRef) -> bool { *a == *b }
1047     @{type_names: std::map::hashmap(hash, eq),
1048       named_types: std::map::str_hash()}
1049 }
1050
1051 fn type_to_str(names: type_names, ty: TypeRef) -> ~str {
1052     return type_to_str_inner(names, ~[], ty);
1053 }
1054
1055 fn type_to_str_inner(names: type_names, outer0: ~[TypeRef], ty: TypeRef) ->
1056    ~str {
1057     match type_has_name(names, ty) {
1058       option::Some(n) => return n,
1059       _ => {}
1060     }
1061
1062     let outer = vec::append_one(outer0, ty);
1063
1064     let kind = llvm::LLVMGetTypeKind(ty);
1065
1066     fn tys_str(names: type_names, outer: ~[TypeRef],
1067                tys: ~[TypeRef]) -> ~str {
1068         let mut s: ~str = ~"";
1069         let mut first: bool = true;
1070         for tys.each |t| {
1071             if first { first = false; } else { s += ~", "; }
1072             s += type_to_str_inner(names, outer, t);
1073         }
1074         return s;
1075     }
1076
1077     match kind {
1078       Void => return ~"Void",
1079       Half => return ~"Half",
1080       Float => return ~"Float",
1081       Double => return ~"Double",
1082       X86_FP80 => return ~"X86_FP80",
1083       FP128 => return ~"FP128",
1084       PPC_FP128 => return ~"PPC_FP128",
1085       Label => return ~"Label",
1086       Integer => {
1087         return ~"i" + int::str(llvm::LLVMGetIntTypeWidth(ty) as int);
1088       }
1089       Function => {
1090         let mut s = ~"fn(";
1091         let out_ty: TypeRef = llvm::LLVMGetReturnType(ty);
1092         let n_args = llvm::LLVMCountParamTypes(ty) as uint;
1093         let args = vec::from_elem(n_args, 0 as TypeRef);
1094         unsafe {
1095             llvm::LLVMGetParamTypes(ty, vec::unsafe::to_ptr(args));
1096         }
1097         s += tys_str(names, outer, args);
1098         s += ~") -> ";
1099         s += type_to_str_inner(names, outer, out_ty);
1100         return s;
1101       }
1102       Struct => {
1103         let mut s: ~str = ~"{";
1104         let n_elts = llvm::LLVMCountStructElementTypes(ty) as uint;
1105         let elts = vec::from_elem(n_elts, 0 as TypeRef);
1106         unsafe {
1107             llvm::LLVMGetStructElementTypes(ty, vec::unsafe::to_ptr(elts));
1108         }
1109         s += tys_str(names, outer, elts);
1110         s += ~"}";
1111         return s;
1112       }
1113       Array => {
1114         let el_ty = llvm::LLVMGetElementType(ty);
1115         return ~"[" + type_to_str_inner(names, outer, el_ty) + ~" x " +
1116             uint::str(llvm::LLVMGetArrayLength(ty) as uint) + ~"]";
1117       }
1118       Pointer => {
1119         let mut i: uint = 0u;
1120         for outer0.each |tout| {
1121             i += 1u;
1122             if tout as int == ty as int {
1123                 let n: uint = vec::len::<TypeRef>(outer0) - i;
1124                 return ~"*\\" + int::str(n as int);
1125             }
1126         }
1127         let addrstr = {
1128             let addrspace = llvm::LLVMGetPointerAddressSpace(ty) as uint;
1129             if addrspace == 0u {
1130                 ~""
1131             } else {
1132                 fmt!("addrspace(%u)", addrspace)
1133             }
1134         };
1135         return addrstr + ~"*" +
1136                 type_to_str_inner(names, outer, llvm::LLVMGetElementType(ty));
1137       }
1138       Vector => return ~"Vector",
1139       Metadata => return ~"Metadata",
1140       X86_MMX => return ~"X86_MMAX"
1141     }
1142 }
1143
1144 fn float_width(llt: TypeRef) -> uint {
1145     return match llvm::LLVMGetTypeKind(llt) as int {
1146           1 => 32u,
1147           2 => 64u,
1148           3 => 80u,
1149           4 | 5 => 128u,
1150           _ => fail ~"llvm_float_width called on a non-float type"
1151         };
1152 }
1153
1154 fn fn_ty_param_tys(fn_ty: TypeRef) -> ~[TypeRef] unsafe {
1155     let args = vec::from_elem(llvm::LLVMCountParamTypes(fn_ty) as uint,
1156                              0 as TypeRef);
1157     llvm::LLVMGetParamTypes(fn_ty, vec::unsafe::to_ptr(args));
1158     return args;
1159 }
1160
1161
1162 /* Memory-managed interface to target data. */
1163
1164 struct target_data_res {
1165     let TD: TargetDataRef;
1166     new(TD: TargetDataRef) { self.TD = TD; }
1167     drop { llvm::LLVMDisposeTargetData(self.TD); }
1168 }
1169
1170 type target_data = {lltd: TargetDataRef, dtor: @target_data_res};
1171
1172 fn mk_target_data(string_rep: ~str) -> target_data {
1173     let lltd =
1174         str::as_c_str(string_rep, |buf| llvm::LLVMCreateTargetData(buf) );
1175     return {lltd: lltd, dtor: @target_data_res(lltd)};
1176 }
1177
1178 /* Memory-managed interface to pass managers. */
1179
1180 struct pass_manager_res {
1181     let PM: PassManagerRef;
1182     new(PM: PassManagerRef) { self.PM = PM; }
1183     drop { llvm::LLVMDisposePassManager(self.PM); }
1184 }
1185
1186 type pass_manager = {llpm: PassManagerRef, dtor: @pass_manager_res};
1187
1188 fn mk_pass_manager() -> pass_manager {
1189     let llpm = llvm::LLVMCreatePassManager();
1190     return {llpm: llpm, dtor: @pass_manager_res(llpm)};
1191 }
1192
1193 /* Memory-managed interface to object files. */
1194
1195 struct object_file_res {
1196     let ObjectFile: ObjectFileRef;
1197     new(ObjectFile: ObjectFileRef) { self.ObjectFile = ObjectFile; }
1198     drop { llvm::LLVMDisposeObjectFile(self.ObjectFile); }
1199 }
1200
1201 type object_file = {llof: ObjectFileRef, dtor: @object_file_res};
1202
1203 fn mk_object_file(llmb: MemoryBufferRef) -> Option<object_file> {
1204     let llof = llvm::LLVMCreateObjectFile(llmb);
1205     if llof as int == 0 { return option::None::<object_file>; }
1206     return option::Some({llof: llof, dtor: @object_file_res(llof)});
1207 }
1208
1209 /* Memory-managed interface to section iterators. */
1210
1211 struct section_iter_res {
1212     let SI: SectionIteratorRef;
1213     new(SI: SectionIteratorRef) { self.SI = SI; }
1214     drop { llvm::LLVMDisposeSectionIterator(self.SI); }
1215 }
1216
1217 type section_iter = {llsi: SectionIteratorRef, dtor: @section_iter_res};
1218
1219 fn mk_section_iter(llof: ObjectFileRef) -> section_iter {
1220     let llsi = llvm::LLVMGetSections(llof);
1221     return {llsi: llsi, dtor: @section_iter_res(llsi)};
1222 }
1223
1224 //
1225 // Local Variables:
1226 // mode: rust
1227 // fill-column: 78;
1228 // indent-tabs-mode: nil
1229 // c-basic-offset: 4
1230 // buffer-file-coding-system: utf-8-unix
1231 // End:
1232 //