llvm_sys_wrapper/
context.rs

1#![allow(dead_code)]
2
3extern crate llvm_sys;
4
5use self::llvm_sys::core::*;
6use self::llvm_sys::prelude::*;
7use std::os::raw::c_uint;
8use LLVM::Type;
9use builder::Builder;
10use module::Module;
11use struct_type::Struct;
12
13#[derive(Debug)]
14pub struct Context {
15    llvm_context: LLVMContextRef
16}
17
18#[allow(non_snake_case)]
19impl Context {
20    pub fn global_context() -> Context {
21        let context = unsafe { LLVMGetGlobalContext() };
22        Context {
23            llvm_context: context
24        }
25    }
26
27    pub fn new() -> Context {
28        let context = unsafe { LLVMContextCreate() };
29        Context {
30            llvm_context: context
31        }
32    }
33
34    pub fn from_module(module: LLVMModuleRef) -> Context {
35        let context = unsafe { LLVMGetModuleContext(module) };
36        Context {
37            llvm_context: context
38        }
39    }
40
41    pub fn as_ref(&self) -> LLVMContextRef {
42        self.llvm_context
43    }
44
45    pub fn create_builder(&self) -> Builder {
46        Builder::new_in_context(self.as_ref())
47    }
48
49    pub fn create_module(&self, name: &str) -> Module {
50        Module::new_in_context(name, self.as_ref())
51    }
52
53    //
54    // get Type
55    //
56    #[inline]
57    pub fn StructTypeNamed(&self, name: &str) -> Struct {
58        Struct::new_with_name(self.llvm_context, name)
59    }
60
61    #[inline]
62    pub fn StructType(&self, fields: &mut [LLVMTypeRef], packed: bool) -> Struct {
63        Struct::new(self.llvm_context, fields, packed)
64    }
65
66    #[inline]
67    pub fn VoidType(&self) -> LLVMTypeRef {
68        unsafe { LLVMVoidTypeInContext(self.llvm_context) }
69    }
70    #[inline]
71    pub fn IntType(&self, num_bits: c_uint) -> LLVMTypeRef {
72        unsafe { LLVMIntTypeInContext(self.llvm_context, num_bits) }
73    }
74    #[inline]
75    pub fn Int1Type(&self) -> LLVMTypeRef {
76        unsafe { LLVMInt1TypeInContext(self.llvm_context) }
77    }
78    #[inline]
79    pub fn Int8Type(&self) -> LLVMTypeRef {
80        unsafe { LLVMInt8TypeInContext(self.llvm_context) }
81    }
82    #[inline]
83    pub fn Int16Type(&self) -> LLVMTypeRef {
84        unsafe { LLVMInt16TypeInContext(self.llvm_context) }
85    }
86    #[inline]
87    pub fn Int32Type(&self) -> LLVMTypeRef {
88        unsafe { LLVMInt32TypeInContext(self.llvm_context) }
89    }
90    #[inline]
91    pub fn Int64Type(&self) -> LLVMTypeRef {
92        unsafe { LLVMInt64TypeInContext(self.llvm_context) }
93    }
94    #[inline]
95    pub fn Int128Type(&self) -> LLVMTypeRef {
96        unsafe { LLVMInt128TypeInContext(self.llvm_context) }
97    }
98    #[inline]
99    pub fn HalfType(&self) -> LLVMTypeRef {
100        unsafe { LLVMHalfTypeInContext(self.llvm_context) }
101    }
102    #[inline]
103    pub fn FloatType(&self) -> LLVMTypeRef {
104        unsafe { LLVMFloatTypeInContext(self.llvm_context) }
105    }
106    #[inline]
107    pub fn DoubleType(&self) -> LLVMTypeRef {
108        unsafe { LLVMDoubleTypeInContext(self.llvm_context) }
109    }
110    #[inline]
111    pub fn FP128Type(&self) -> LLVMTypeRef {
112        unsafe { LLVMFP128TypeInContext(self.llvm_context) }
113    }
114    #[inline]
115    pub fn X86FP80Type(&self) -> LLVMTypeRef {
116        unsafe { LLVMX86FP80TypeInContext(self.llvm_context) }
117    }
118    #[inline]
119    pub fn PPCFP128Type(&self) -> LLVMTypeRef {
120        unsafe { LLVMPPCFP128TypeInContext(self.llvm_context) }
121    }
122    #[inline]
123    pub fn X86MMXType(&self) -> LLVMTypeRef {
124        unsafe { LLVMX86MMXTypeInContext(self.llvm_context) }
125    }
126    #[inline]
127    pub fn LabelType(&self) -> LLVMTypeRef {
128        unsafe { LLVMLabelTypeInContext(self.llvm_context) }
129    }
130    #[inline]
131    pub fn CharPointerType(&self) -> LLVMTypeRef {
132        Type::PointerType(self.Int8Type(), 0)
133    }
134    #[inline]
135    pub fn Int8PointerType(&self) -> LLVMTypeRef {
136        Type::PointerType(self.Int8Type(), 0)
137    }
138
139    #[inline]
140    pub fn PointerType(&self, typ: LLVMTypeRef) -> LLVMTypeRef {
141        Type::PointerType(typ, 0)
142    }
143
144    //
145    // define Constant util
146    //
147    #[inline]
148    pub fn Null(&self, typ: LLVMTypeRef) -> LLVMValueRef {
149        unsafe { LLVMConstNull(typ) }
150    }
151
152    #[inline]
153    pub fn PointerNull(&self, typ: LLVMTypeRef) -> LLVMValueRef {
154        unsafe { LLVMConstPointerNull(typ) }
155    }
156
157    #[inline]
158    pub fn Bitcast(&self, constant: LLVMValueRef, to_type: LLVMTypeRef) -> LLVMValueRef {
159        unsafe { LLVMConstBitCast(constant, to_type) }
160    }
161    #[inline]
162    pub fn SInt(&self, num_bits: c_uint, val: u64) -> LLVMValueRef {
163        unsafe { LLVMConstInt(LLVMIntTypeInContext(self.llvm_context, num_bits), val, 1) }
164    }
165    #[inline]
166    pub fn UInt(&self, num_bits: c_uint, val: u64) -> LLVMValueRef {
167        unsafe { LLVMConstInt(LLVMIntTypeInContext(self.llvm_context, num_bits), val, 0) }
168    }
169    #[inline]
170    pub fn SInt1(&self, val: u64) -> LLVMValueRef {
171        unsafe { LLVMConstInt(LLVMInt1TypeInContext(self.llvm_context), val, 1) }
172    }
173    #[inline]
174    pub fn UInt1(&self, val: u64) -> LLVMValueRef {
175        unsafe { LLVMConstInt(LLVMInt1TypeInContext(self.llvm_context), val, 0) }
176    }
177    #[inline]
178    pub fn SInt8(&self, val: u64) -> LLVMValueRef {
179        unsafe { LLVMConstInt(LLVMInt8TypeInContext(self.llvm_context), val, 1) }
180    }
181    #[inline]
182    pub fn UInt8(&self, val: u64) -> LLVMValueRef {
183        unsafe { LLVMConstInt(LLVMInt8TypeInContext(self.llvm_context), val, 0) }
184    }
185    #[inline]
186    pub fn SInt16(&self, val: u64) -> LLVMValueRef {
187        unsafe { LLVMConstInt(LLVMInt16TypeInContext(self.llvm_context), val, 1) }
188    }
189    #[inline]
190    pub fn UInt16(&self, val: u64) -> LLVMValueRef {
191        unsafe { LLVMConstInt(LLVMInt16TypeInContext(self.llvm_context), val, 0) }
192    }
193    #[inline]
194    pub fn SInt32(&self, val: u64) -> LLVMValueRef {
195        unsafe { LLVMConstInt(LLVMInt32TypeInContext(self.llvm_context), val, 1) }
196    }
197    #[inline]
198    pub fn UInt32(&self, val: u64) -> LLVMValueRef {
199        unsafe { LLVMConstInt(LLVMInt32TypeInContext(self.llvm_context), val, 0) }
200    }
201    #[inline]
202    pub fn SInt64(&self, val: u64) -> LLVMValueRef {
203        unsafe { LLVMConstInt(LLVMInt64TypeInContext(self.llvm_context), val, 1) }
204    }
205    #[inline]
206    pub fn UInt64(&self, val: u64) -> LLVMValueRef {
207        unsafe { LLVMConstInt(LLVMInt64TypeInContext(self.llvm_context), val, 0) }
208    }
209    #[inline]
210    pub fn SInt128(&self, val: u64) -> LLVMValueRef {
211        unsafe { LLVMConstInt(LLVMInt128TypeInContext(self.llvm_context), val, 1) }
212    }
213    #[inline]
214    pub fn UInt128(&self, val: u64) -> LLVMValueRef {
215        unsafe { LLVMConstInt(LLVMInt128TypeInContext(self.llvm_context), val, 0) }
216    }
217
218    #[inline]
219    pub fn Half(&self, val: f64) -> LLVMValueRef {
220        unsafe { LLVMConstReal(LLVMHalfTypeInContext(self.llvm_context), val) }
221    }
222    #[inline]
223    pub fn Float(&self, val: f64) -> LLVMValueRef {
224        unsafe { LLVMConstReal(LLVMFloatTypeInContext(self.llvm_context), val) }
225    }
226    #[inline]
227    pub fn Double(&self, val: f64) -> LLVMValueRef {
228        unsafe { LLVMConstReal(LLVMDoubleTypeInContext(self.llvm_context), val) }
229    }
230    #[inline]
231    pub fn FP128(&self, val: f64) -> LLVMValueRef {
232        unsafe { LLVMConstReal(LLVMFP128TypeInContext(self.llvm_context), val) }
233    }
234    #[inline]
235    pub fn X86FP80(&self, val: f64) -> LLVMValueRef {
236        unsafe { LLVMConstReal(LLVMX86FP80TypeInContext(self.llvm_context), val) }
237    }
238    #[inline]
239    pub fn PPCFP128(&self, val: f64) -> LLVMValueRef {
240        unsafe { LLVMConstReal(LLVMPPCFP128TypeInContext(self.llvm_context), val) }
241    }
242}