bf 0.4.8

An optimizing Brainfuck interpeter and JIT compiler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::ffi::{CString, CStr};
use std::os::raw::{c_char, c_uint};
use std::{mem, ptr};
use std::cell::RefCell;

use llvm_sys;
use llvm_sys::prelude::*;
use llvm_sys::core::*;
use llvm_sys::target;
use llvm_sys::analysis::{LLVMVerifyModule, LLVMVerifierFailureAction};
use llvm_sys::transforms::pass_manager_builder as builder;
use llvm_sys::execution_engine as engine;
pub use llvm_sys::LLVMIntPredicate;

use rts::RtsState;

pub struct Context {
    context_ref: LLVMContextRef,
    strings:     RefCell<Vec<CString>>,
}

impl Context {
    pub fn new() -> Self {
        Context {
            context_ref: unsafe { LLVMContextCreate() },
            strings:     RefCell::new(Vec::new()),
        }
    }

    pub fn new_name(&self, name: &str) -> *const c_char {
        let string = CString::new(name).unwrap();
        let ptr    = string.as_ptr();
        self.strings.borrow_mut().push(string);
        ptr
    }

    fn wrap_value(&self, value_ref: LLVMValueRef) -> Value {
        Value {
            value_ref: value_ref,
            context:   self,
        }
    }

    fn wrap_type(&self, type_ref: LLVMTypeRef) -> Type {
        Type {
            type_ref: type_ref,
            context:  self,
        }
    }
}

impl Drop for Context {
    fn drop(&mut self) {
        unsafe {
            LLVMContextDispose(self.context_ref);
        }
    }
}

#[derive(Clone, Copy)]
pub struct Module<'a> {
    module_ref: LLVMModuleRef,
    context:    &'a Context,
}

impl<'a> Module<'a> {
    pub fn new(context: &'a Context, name: &str) -> Self {
        let name = context.new_name(name);
        Module {
            module_ref: unsafe {
                LLVMModuleCreateWithNameInContext(name, context.context_ref)
            },
            context: context,
        }
    }

    pub fn add_function(&self, name: &str, ty: Type<'a>) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMAddFunction(self.module_ref, name, ty.type_ref)
        })
    }

    // From llvm-alt:
    pub fn optimize(&self, opt_level: usize, size_level: usize) {
        unsafe {
            let builder = builder::LLVMPassManagerBuilderCreate();
            builder::LLVMPassManagerBuilderSetOptLevel(builder, opt_level as _);
            builder::LLVMPassManagerBuilderSetSizeLevel(builder, size_level as _);
            let pass_manager = LLVMCreatePassManager();
            builder::LLVMPassManagerBuilderPopulateModulePassManager(builder, pass_manager);
            builder::LLVMPassManagerBuilderDispose(builder);
            LLVMRunPassManager(pass_manager, self.module_ref);
            LLVMDisposePassManager(pass_manager);
        }
    }

    pub fn dump(&self) {
        unsafe {
            LLVMDumpModule(self.module_ref);
        }
    }

    pub fn verify(&self) -> Result<(), String> {
        let mut out_message: *mut c_char = ptr::null_mut();

        unsafe {
            if LLVMVerifyModule(self.module_ref,
                                LLVMVerifierFailureAction::LLVMReturnStatusAction,
                                &mut out_message) == 0 {
                Ok(())
            } else {
                let result = CStr::from_ptr(out_message).to_string_lossy().into_owned();
                LLVMDisposeMessage(out_message);
                Err(result)
            }
        }
    }

    pub unsafe fn with_function<'b, F>(&self, name: &str, with: F) -> Result<u64, String>
        where F: FnOnce(extern fn (&mut RtsState<'b>,
                                   extern fn(&mut RtsState<'b>) -> u8,
                                   extern fn(&mut RtsState<'b>, u8) -> ()) -> u64) -> u64
    {
        let mut out_message: *mut c_char = ptr::null_mut();
        let mut exec: engine::LLVMExecutionEngineRef = ptr::null_mut();

        engine::LLVMLinkInMCJIT();

        if target::LLVM_InitializeNativeAsmPrinter() == 1 {
            return Err("Could not initialize native asm printer for LLVM.".to_owned());
        }

        if target::LLVM_InitializeNativeTarget() == 1 {
            return Err("Could not initialize native target for LLVM.".to_owned());
        }

        let mut options = engine::LLVMMCJITCompilerOptions {
            OptLevel: 3,
            CodeModel: llvm_sys::target_machine::LLVMCodeModel::LLVMCodeModelDefault,
            NoFramePointerElim: 0,
            EnableFastISel: 0,
            MCJMM: ptr::null_mut(),
        };

        if engine::LLVMCreateMCJITCompilerForModule(
            &mut exec, self.module_ref,
            &mut options,
            mem::size_of::<c_uint>() as _,
            &mut out_message
        ) != 0 {
            let result = CStr::from_ptr(out_message).to_string_lossy().into_owned();
            LLVMDisposeMessage(out_message);
            return Err(result);
        }

        let cname    = CString::new(name).unwrap();
        let fun_addr = engine::LLVMGetFunctionAddress(exec, cname.as_ptr());
        let fun = mem::transmute(fun_addr);

        Ok(with(fun))
    }
}

#[derive(Copy, Clone)]
pub struct Type<'a> {
    type_ref:  LLVMTypeRef,
    context:   &'a Context,
}

impl<'a> Type<'a> {
    pub fn get_i64(context: &'a Context) -> Self {
        context.wrap_type(unsafe {
            LLVMInt64TypeInContext(context.context_ref)
        })
    }

    pub fn get_i32(context: &'a Context) -> Self {
        context.wrap_type(unsafe {
            LLVMInt32TypeInContext(context.context_ref)
        })
    }

    pub fn get_i8(context: &'a Context) -> Self {
        context.wrap_type(unsafe {
            LLVMInt8TypeInContext(context.context_ref)
        })
    }

    pub fn get_bool(context: &'a Context) -> Self {
        context.wrap_type(unsafe {
            LLVMInt1TypeInContext(context.context_ref)
        })
    }

    pub fn get_void(context: &'a Context) -> Self {
        context.wrap_type(unsafe {
            LLVMVoidTypeInContext(context.context_ref)
        })
    }

    pub fn get_pointer(target: Type<'a>) -> Self {
        target.context.wrap_type(unsafe {
            LLVMPointerType(target.type_ref, 0)
        })
    }

    pub fn get_function(args: &[Type<'a>], result: Type<'a>) -> Self {
        let mut args = args.into_iter().map(|arg| arg.type_ref).collect::<Vec<_>>();
        result.context.wrap_type(unsafe {
            LLVMFunctionType(result.type_ref,
                             args.as_mut_ptr(),
                             args.len() as _,
                             0)
        })
    }
}

#[derive(Copy, Clone)]
pub struct Value<'a> {
    value_ref: LLVMValueRef,
    context:   &'a Context,
}

impl<'a> Value<'a> {
    pub fn get_fun_param(&self, index: usize) -> Self {
        self.context.wrap_value(unsafe {
            LLVMGetParam(self.value_ref, index as _)
        })
    }

    pub fn append(&self, name: &str) -> BasicBlock<'a> {
        let name = self.context.new_name(name);
        let bb_ref = unsafe {
            LLVMAppendBasicBlockInContext(self.context.context_ref,
                                          self.value_ref,
                                          name)
        };
        BasicBlock {
            bb_ref: bb_ref,
            _context: self.context,
        }
    }

    pub fn get_u64(context: &'a Context, value: u64) -> Self {
        context.wrap_value(unsafe {
            LLVMConstInt(Type::get_i64(context).type_ref,
                         value as _,
                         false as _)
        })
    }

    pub fn get_u32(context: &'a Context, value: u32) -> Self {
        context.wrap_value(unsafe {
            LLVMConstInt(Type::get_i32(context).type_ref,
                         value as _,
                         false as _)
        })
    }

    pub fn get_u8(context: &'a Context, value: u8) -> Self {
        context.wrap_value(unsafe {
            LLVMConstInt(Type::get_i8(context).type_ref,
                         value as _,
                         false as _)
        })
    }


    pub fn get_bool(context: &'a Context, value: bool) -> Self {
        context.wrap_value(unsafe {
            LLVMConstInt(Type::get_bool(context).type_ref,
                         value as _,
                         false as _)
        })
    }
}

#[derive(Copy, Clone)]
pub struct BasicBlock<'a> {
    bb_ref:  LLVMBasicBlockRef,
    _context: &'a Context,
}

#[derive(Copy, Clone)]
pub struct Builder<'a> {
    builder_ref: LLVMBuilderRef,
    context:     &'a Context,
}

impl<'a> Builder<'a> {
    pub fn new(context: &'a Context) -> Self {
        Builder {
            builder_ref: unsafe { LLVMCreateBuilderInContext(context.context_ref) },
            context:     context,
        }
    }

    pub fn position_at_end(&self, bb: BasicBlock<'a>) {
        unsafe {
            LLVMPositionBuilderAtEnd(self.builder_ref, bb.bb_ref);
        }
    }

    pub fn add(&self, v1: Value<'a>, v2: Value<'a>, name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMBuildAdd(self.builder_ref, v1.value_ref, v2.value_ref, name)
        })
    }

    pub fn alloca(&self, ty: Type<'a>, name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMBuildAlloca(self.builder_ref, ty.type_ref, name)
        })
    }

    pub fn array_alloca(&self, ty: Type<'a>, size: Value<'a>, name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMBuildArrayAlloca(self.builder_ref,
                                 ty.type_ref,
                                 size.value_ref,
                                 name)
        })
    }

    pub fn br(&self, dst: BasicBlock<'a>) {
        unsafe {
            LLVMBuildBr(self.builder_ref, dst.bb_ref);
        }
    }

    pub fn call(&self, fun: Value<'a>, args: &[Value<'a>], name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        let mut args = args.into_iter().map(|arg| arg.value_ref).collect::<Vec<_>>();
        self.context.wrap_value(unsafe {
            LLVMBuildCall(self.builder_ref,
                          fun.value_ref,
                          args.as_mut_ptr(),
                          args.len() as u32,
                          name)
        })
    }

    pub fn cmp(&self, pred: LLVMIntPredicate, lhs: Value<'a>, rhs: Value<'a>,
               name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMBuildICmp(self.builder_ref, pred, lhs.value_ref, rhs.value_ref, name)

        })
    }

    pub fn cond_br(&self, test: Value<'a>, then: BasicBlock<'a>, else_: BasicBlock<'a>) {
        unsafe {
            LLVMBuildCondBr(self.builder_ref, test.value_ref, then.bb_ref, else_.bb_ref);
        }
    }

    pub fn gep(&self, ptr: Value<'a>, indices: &[Value<'a>], name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        let mut indices = indices.into_iter().map(|i| i.value_ref).collect::<Vec<_>>();
        self.context.wrap_value(unsafe {
            LLVMBuildGEP(self.builder_ref,
                         ptr.value_ref,
                         indices.as_mut_ptr(),
                         indices.len() as u32,
                         name)
        })
    }

    pub fn load(&self, ptr: Value<'a>, name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMBuildLoad(self.builder_ref, ptr.value_ref, name)
        })
    }

    pub fn ret(&self, value: Value<'a>) {
        unsafe {
            LLVMBuildRet(self.builder_ref, value.value_ref);
        }
    }

    pub fn store(&self, src: Value<'a>, dst: Value<'a>) {
        unsafe {
            LLVMBuildStore(self.builder_ref, src.value_ref, dst.value_ref);
        }
    }

    pub fn sub(&self, v1: Value<'a>, v2: Value<'a>, name: &str) -> Value<'a> {
        let name = self.context.new_name(name);
        self.context.wrap_value(unsafe {
            LLVMBuildSub(self.builder_ref, v1.value_ref, v2.value_ref, name)
        })
    }

//    pub fn trunc(&self, value: Value<'a>, ty: Type<'a>, name: &str) -> Value<'a> {
//        let name = self.context.new_name(name);
//        self.context.wrap_value(unsafe {
//            LLVMBuildTrunc(self.builder_ref, value.value_ref, ty.type_ref, name)
//        })
//    }
//
//    pub fn zext(&self, value: Value<'a>, ty: Type<'a>, name: &str) -> Value<'a> {
//        let name = self.context.new_name(name);
//        self.context.wrap_value(unsafe {
//            LLVMBuildZExt(self.builder_ref, value.value_ref, ty.type_ref, name)
//        })
//    }
}