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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734

use crossbeam;
use num_bigint::Sign;
use num_traits::{FromPrimitive, ToPrimitive};

use std::io::{BufRead, Write};
use std::sync::mpsc;
use std::fmt::Display;
use std::str::FromStr;
use std::u8;

use program::{Program, Command, Integer, BigInteger};
use super::{WsError, WsErrorKind, Options};

mod cached_map;

mod simple_state;
use self::simple_state::SimpleState;

mod jit_state;
use self::jit_state::JitState;

mod bigint_state;
use self::bigint_state::BigIntState;


#[cfg(target_arch="x86_64")]
#[macro_use]
mod compiler_x64;
#[cfg(target_arch="x86")]
#[macro_use]
mod compiler_x86;

#[cfg(target_arch="x86_64")]
use self::compiler_x64::JitCompiler;
#[cfg(target_arch="x86")]
use self::compiler_x86::JitCompiler;

#[cfg(target_arch="x86_64")]
pub use self::compiler_x64::debug_compile;
#[cfg(target_arch="x86")]
pub use self::compiler_x86::debug_compile;

#[cfg(any(target_arch="x86_64", target_arch="x86"))]
mod allocator;


pub trait CheckedArith : Sized + Clone + Default + Display + FromStr + ToString + From<isize> {
    fn overflowing_add(&self, rhs: &Self) -> (Self, bool);

    fn overflowing_sub(&self, rhs: &Self) -> (Self, bool);

    fn overflowing_mul(&self, rhs: &Self) -> (Self, bool);

    fn checked_div(&self, rhs: &Self) -> Option<Self>;

    fn checked_rem(&self, rhs: &Self) -> Option<Self>;

    fn is_zero(&self) -> bool;

    fn is_negative(&self) -> bool;

    fn from_u8(from: u8) -> Self;

    fn into_u8(&self) -> Option<u8>;
}

pub trait State<'a> {
    type Var: CheckedArith;
    type HeapIterator: Iterator<Item=(Self::Var, Self::Var)> + 'a;

    /// Return options for the interpreter
    fn options(&self) -> Options;

    /// This method returns the index of the next command that is to be executed
    fn index(&mut self) -> &mut usize;
    /// Returns the stack of this state implementation. The stack is always assumed to be a Vec.
    fn stack(&mut self) -> &mut Vec<Self::Var>;

    /// Set a key on the heap.
    fn set(&mut self, key: Self::Var, value: Self::Var);
    /// Get a key from the heap.
    fn get(&self, key: &Self::Var) -> Option<&Self::Var>;
    /// Iterate through the heap
    fn iter_heap(&'a self) -> Self::HeapIterator;

    /// Push a value onto the callstack
    fn call(&mut self, retloc: usize);
    /// Remove a value from the callstack
    fn ret(&mut self) -> Option<usize>;

    /// access the input stream of the state
    fn input(&mut self) -> &mut dyn BufRead;
    /// access the output stream of the state
    fn output(&mut self) -> &mut dyn Write;

    /// perform input/output functions on the program state
    fn read_char(&mut self) -> Result<Self::Var, WsError> {
        let mut s = [0; 1];
        self.output().flush()          .map_err(|e| WsError::wrap(e, WsErrorKind::IOError, "Could not flush the output file"))?;
        self.input().read_exact(&mut s).map_err(|e| WsError::wrap(e, WsErrorKind::IOError, "Could not read from the input file"))?;
        Ok(Self::Var::from_u8(s[0]))
    }
    fn read_num(&mut self) -> Result<String, WsError> {
        let mut s = String::new();
        self.output().flush()         .map_err(|e| WsError::wrap(e, WsErrorKind::IOError, "Could not flush the output file"))?;
        self.input().read_line(&mut s).map_err(|e| WsError::wrap(e, WsErrorKind::IOError, "Could not read a line from the input file"))?;
        Ok(s)
    }
    fn write_char(&mut self, c: Self::Var) -> Result<(), WsError> {
        if let Some(val) = c.into_u8() {
            self.output().write_all(&[val]).map_err(|e| WsError::wrap(e, WsErrorKind::IOError, "Could not write to the output file"))
        } else {
            Err(WsError::new(WsErrorKind::RuntimeParseError, "The value is too large to be printed as a character"))
        }
    }
    fn write_num(&mut self, c: Self::Var) -> Result<(), WsError> {
        let c = c.to_string();
        self.output().write_all(c.as_bytes()).map_err(|e| WsError::wrap(e, WsErrorKind::IOError, "Could not write to the output file"))
    }

    fn count_instruction(&mut self) { }

    fn push_large(&mut self, _: &BigInteger) -> Result<(), WsError> {
        Err(WsError::new(WsErrorKind::Overflow, "A large integer was pushed"))
    }

    fn input_num(&mut self) -> Result<(), WsError> {
        let s = self.read_num()?;
        let s = s.trim();
        let value = s.parse::<Self::Var>().map_err(|_| WsError::new(WsErrorKind::RuntimeParseError, "Expected a number to parse"))?;
        let key = self.stack().pop().unwrap();
        self.set(key, value);
        Ok(())
    }

    /// Interprets the commands starting at the current command index until
    /// a control flow join is reached. The return value is false if the end
    /// of the program was reached
    fn interpret_block(&mut self, commands: &[Command]) -> Result<bool, WsError> {
        use ::program::Command::*;
        let options = self.options();

        // interpret until we hit something that can cause a flow control convergence (jump, call, ret)
        while let Some(c) = commands.get(*self.index()) {
            let len = self.stack().len();
            self.count_instruction();

            match *c {
                Push {ref value} => self.stack().push(value.clone().into()),
                PushBig {ref value} => self.push_large(value)?,
                Duplicate => if let Some(value) = self.stack().last().cloned() {
                    self.stack().push(value);
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Tried to duplicate but stack is empty"));
                },
                Copy {index} => if index < self.stack().len() {
                    let stack = self.stack();
                    let index = stack.len() - 1 - index;
                    let value = stack[index].clone();
                    stack.push(value);
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Tried to copy from outside the stack"));
                },
                Swap => if len > 1 {
                    self.stack().swap(len - 1, len - 2);
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Cannot swap with empty stack"));
                },
                Discard => if let None = self.stack().pop() {
                    return Err(WsError::new(WsErrorKind::StackError, "Cannot pop from empty stack"));
                },
                Slide {amount} => if len > amount {
                    let top = self.stack().pop().unwrap();
                    self.stack().truncate(len - amount);
                    self.stack()[len - amount - 1] = top;
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Cannot discard more elements than items exist on stack"));
                },
                Add => if len > 1 {
                    let stack = self.stack();
                    let (val, overflow) = stack[len - 2].overflowing_add(&stack[len - 1]);
                    if overflow && !options.contains(Options::IGNORE_OVERFLOW) {
                        return Err(WsError::new(WsErrorKind::Overflow, format!("Overflow during addition: {} + {}", &stack[len - 2], &stack[len - 1])));
                    }
                    stack[len - 2] = val;
                    stack.pop();
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to add"));
                },
                Subtract => if len > 1 {
                    let stack = self.stack();
                    let (val, overflow) = stack[len - 2].overflowing_sub(&stack[len - 1]);
                    if overflow && !options.contains(Options::IGNORE_OVERFLOW) {
                        return Err(WsError::new(WsErrorKind::Overflow, format!("Overflow during subtraction: {} - {}", &stack[len - 2], &stack[len - 1])));
                    }
                    stack[len - 2] = val;
                    stack.pop();
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to subtract"));
                },
                Multiply => if len > 1 {
                    let stack = self.stack();
                    let (val, overflow) = stack[len - 2].overflowing_mul(&stack[len - 1]);
                    if overflow && !options.contains(Options::IGNORE_OVERFLOW) {
                        return Err(WsError::new(WsErrorKind::Overflow, format!("Overflow during multiplication: {} * {}", &stack[len - 2], &stack[len - 1])));
                    }
                    stack[len - 2] = val;
                    stack.pop();
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to multiply"));
                },
                Divide => if len > 1 {
                    let stack = self.stack();
                    if let Some(val) = stack[len - 2].checked_div(&stack[len - 1]) {
                        stack[len - 2] = val;
                    } else if stack[len - 1].is_zero() {
                        return Err(WsError::new(WsErrorKind::DivisionError, "Divide by zero"));
                    } else {
                        return Err(WsError::new(WsErrorKind::Overflow, format!("Overflow during division: {} / {}", stack[len - 2], stack[len - 1])));
                    }
                    stack.pop();
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to divide"));
                },
                Modulo => if len > 1 {
                    let stack = self.stack();
                    if let Some(val) = stack[len - 2].checked_rem(&stack[len - 1]) {
                        stack[len - 2] = val;
                    } else if stack[len - 1].is_zero() {
                        return Err(WsError::new(WsErrorKind::DivisionError, "Modulo by zero"));
                    } else {
                        return Err(WsError::new(WsErrorKind::Overflow, format!("Overflow during modulo: {} % {}", stack[len - 2], stack[len - 1])));
                    }
                    stack.pop();
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to modulo"));
                },
                Set => if len > 1 {
                    let value = self.stack().pop().unwrap();
                    let key = self.stack().pop().unwrap();
                    self.set(key, value);
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to set value"));
                },
                Get => if let Some(last) = self.stack().pop() {
                    if let Some(value) = self.get(&last).cloned() {
                        self.stack().push(value);
                    } else if options.contains(Options::UNCHECKED_HEAP) {
                        self.stack().push(Default::default());
                    } else {
                        let err = Err(WsError::new(WsErrorKind::KeyError, format!("Key does not exist on the heap: {}", &last)));
                        self.stack().push(last);
                        return err;
                    }
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "not enough items on stack to get value"));
                },
                Label => {
                    *self.index() += 1;
                    return Ok(true);
                },
                Call {index} => {
                    let retloc = *self.index() + 1;
                    self.call(retloc);
                    *self.index() = index;
                    return Ok(true);
                },
                Jump {index} => {
                    *self.index() = index;
                    return Ok(true);
                },
                JumpIfZero {index} => if let Some(x) = self.stack().pop() {
                    if x.is_zero() {
                        *self.index() = index;
                        return Ok(true);
                    }
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to test if zero"));
                },
                JumpIfNegative {index} => if let Some(x) = self.stack().pop() {
                    if x.is_negative() {
                        *self.index() = index;
                        return Ok(true);
                    }
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to test if negative"));
                },
                EndSubroutine => if let Some(index) = self.ret() {
                    *self.index() = index;
                    return Ok(true);
                } else {
                    return Err(WsError::new(WsErrorKind::CallStackError, "Not enough items on callstack to return"));
                },
                EndProgram => return Ok(false),
                PrintChar => if let Some(c) = self.stack().pop() {
                    self.write_char(c)?;
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to print"));
                },
                PrintNum => if let Some(c) = self.stack().pop() {
                    self.write_num(c)?;
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to print"));
                },
                InputChar => if len > 0 {
                    let c = self.read_char()?;
                    let key = self.stack().pop().unwrap();
                    self.set(key, c);
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to input character"));
                },
                InputNum => if len > 0 {
                    self.input_num()?;
                } else {
                    return Err(WsError::new(WsErrorKind::StackError, "Not enough items on stack to input number"));
                }
            };
            *self.index() += 1;
        }

        if options.contains(Options::NO_IMPLICIT_EXIT) {
            Err(WsError::new(WsErrorKind::InvalidIndex, "Invalid program counter"))
        } else {
            Ok(false)
        }
    }
}

pub trait SmallIntState<'a> : State<'a, Var=Integer> {
    fn into_bigintstate(&'a mut self) -> BigIntState<'a>;
}

impl CheckedArith for BigInteger {
    fn overflowing_add(&self, rhs: &Self) -> (Self, bool) {
        (self + rhs, false)
    }

    fn overflowing_sub(&self, rhs: &Self) -> (Self, bool) {
        (self - rhs, false)
    }

    fn overflowing_mul(&self, rhs: &Self) -> (Self, bool) {
        (self * rhs, false)
    }

    fn checked_div(&self, rhs: &Self) -> Option<Self> {
        BigInteger::checked_div(self, rhs)
    }

    fn checked_rem(&self, rhs: &Self) -> Option<Self> {
        if rhs.sign() == Sign::NoSign {
            return None;
        }
        Some(self % rhs)
    }

    fn is_zero(&self) -> bool {
        self.sign() == Sign::NoSign
    }

    fn is_negative(&self) -> bool {
        self.sign() == Sign::Minus
    }

    fn from_u8(from: u8) -> Self {
        <Self as FromPrimitive>::from_u8(from).unwrap()
    }

    fn into_u8(&self) -> Option<u8> {
        self.to_u8()
    }
}

impl CheckedArith for Integer {
    fn overflowing_add(&self, rhs: &Self) -> (Self, bool) {
        Integer::overflowing_add(*self, *rhs)
    }

    fn overflowing_sub(&self, rhs: &Self) -> (Self, bool) {
        Integer::overflowing_sub(*self, *rhs)
    }

    fn overflowing_mul(&self, rhs: &Self) -> (Self, bool) {
        Integer::overflowing_mul(*self, *rhs)
    }

    fn checked_div(&self, rhs: &Self) -> Option<Self> {
        Integer::checked_div(*self, *rhs)
    }

    fn checked_rem(&self, rhs: &Self) -> Option<Self> {
        Integer::checked_rem(*self, *rhs)
    }

    fn is_zero(&self) -> bool {
        *self == 0
    }

    fn is_negative(&self) -> bool {
        *self < 0
    }

    fn from_u8(from: u8) -> Self {
        from as Self
    }

    fn into_u8(&self) -> Option<u8> {
        if *self <= u8::MAX as Self && *self >= 0 {
            Some(*self as u8)
        } else {
            None
        }
    }
}


/// A whitespace interpreter. This struct provides various strategies for interpreting a whitespace program.
pub struct Interpreter<'a> {
    program: &'a Program,
    options: Options,
    input: &'a mut (dyn BufRead + 'a),
    output: &'a mut (dyn Write + 'a)
}

impl<'a> Interpreter<'a> {
    /// Construct a new whitespace interpreter from a program, input stream and output stream with the specified options.
    pub fn new(program: &'a Program, options: Options, input: &'a mut (dyn BufRead + 'a), output: &'a mut (dyn Write + 'a)) -> Interpreter<'a> {
        Interpreter {
            program: program,
            options: options,
            input: input,
            output: output,
        }
    }

    fn interpret<'b, T: State<'b>>(state: &mut T, program: &Program) -> Result<(), WsError> {
        loop {
            match state.interpret_block(&program.commands) {
                Ok(true) => continue,
                Ok(false) => return Ok(()),
                Err(mut e) => {
                    e.set_location(*state.index());
                    return Err(e)
                }
            }
        }
    }

    fn bigint_fallback<'b, T: SmallIntState<'b>>(state: &'b mut T, program: &Program, e: WsError) -> Result<(), WsError> {
        match e {
            WsError {kind: WsErrorKind::Overflow, ..} => {
                let mut state = state.into_bigintstate();
                Self::interpret(&mut state, program)
            }
            WsError {kind: WsErrorKind::InumOverflow(key, val), ..} => {
                let mut state = state.into_bigintstate();
                state.set(key.into(), val);
                Self::interpret(&mut state, program)
            }
            e => Err(e)
        }
    }

    /// Similar to Interpreter::interpret_with_simple_state but does not have bigint fallback
    /// and returns the amount of executed instructions on success.
    pub fn count_with_simple_state(&'a mut self) -> Result<usize, WsError> {
        let mut state = SimpleState::new(self.options, &mut self.input, &mut self.output);
        Self::interpret(&mut state, &self.program)?;
        Ok(state.count)
    }

    /// The reference interpreter implementation. It uses simple data structures internally, falling back to
    /// bignum-based simple datastructures if values become too large.
    pub fn interpret_with_simple_state(&'a mut self) -> Result<(), WsError> {
        let mut state = SimpleState::new(self.options, &mut self.input, &mut self.output);
        match Self::interpret(&mut state, &self.program) {
            Ok(()) => Ok(()),
            Err(e) => if self.options.contains(Options::NO_FALLBACK) {
                Err(e)
            } else {
                Self::bigint_fallback(&mut state, &self.program, e)
            }
        }
    }

    /// Interprets the program with optimized data structures, falling back to
    /// bignum-based simple datastructures if values become too large. 
    pub fn interpret_with_fast_state(&'a mut self) -> Result<(), WsError> {
        let mut state = JitState::new(self.options, &mut self.input, &mut self.output);
        match Self::interpret(&mut state, &self.program) {
            Ok(()) => Ok(()),
            Err(e) => if self.options.contains(Options::NO_FALLBACK) {
                Err(e)
            } else {
                Self::bigint_fallback(&mut state, &self.program, e)
            }
        }
    }

    /// Interpret the program using only bignum-based simple datastructures. This is slow.
    pub fn interpret_with_bigint_state(&'a mut self) -> Result<(), WsError> {
        let mut state = BigIntState::new(self.options, &mut self.input, &mut self.output);
        Self::interpret(&mut state, &self.program)
    }

    /// Use a jit compiler that compiles the entire program in advance to execute
    /// the program. It is backed by an optimized datastructure, and will fall back
    /// to interpretation in unsafe situations. When values become too large it will
    /// fall back to bignum-based interpretation.
    #[cfg(any(target_arch="x86_64", target_arch="x86"))]
    pub fn jit_aot(&mut self) -> Result<(), WsError> {
        let program = &self.program;
        let mut state = JitState::new(self.options, &mut self.input, &mut self.output);
        let mut compiler = JitCompiler::new(program, self.options);
        let mut jit_handles = vec![None; program.commands.len()];

        // first compile everything (except the starting block as there's no reason to do that)
        use program::Command::*;
        for (i, c) in program.commands.iter().enumerate() {
            let i = match *c {
                Label | Call {..} if i + 1 != program.commands.len() => i + 1,
                _ => continue
            };

            if let Some(start) = compiler.compile_index(i) {
                jit_handles[i] = Some(start);
            }
        }
        compiler.commit();

        // then run it
        let executor = compiler.executor();
        let lock = executor.lock();

        while let Some(&offset) = jit_handles.get(*state.index()) {
            // can we jit?
            if let Some(offset) = offset {
                let old_index = *state.index();

                unsafe {
                    state.run_block(lock.ptr(offset));
                }
                // if we exit on the same instruction as we started we need to try interpreting first 
                // as otherwise we could get stuck in a loop due to stack errors
                if old_index != *state.index() {
                    continue;
                }
            }

            // fallback interpreting.
            match state.interpret_block(&program.commands) {
                Ok(true) => (),
                Ok(false) => return Ok(()),
                Err(mut e) => if self.options.contains(Options::NO_FALLBACK) {
                    e.set_location(*state.index());
                    return Err(e)
                } else { 
                    e.set_location(*state.index());
                    return Self::bigint_fallback(&mut state, program, e)
                }
            }
        }

        if self.options.contains(Options::NO_IMPLICIT_EXIT) {
            Err(WsError::new(WsErrorKind::InvalidIndex, "Invalid program counter"))
        } else {
            Ok(())
        }
    }

    /// Use a jit compiler that compiles code synchronously while executing
    /// the program. It is backed by an optimized datastructure, and will fall back
    /// to interpretation in unsafe situations. When values become too large it will
    /// fall back to bignum-based interpretation.
    #[cfg(any(target_arch="x86_64", target_arch="x86"))]
    pub fn jit_sync(&mut self) -> Result<(), WsError> {
        let program = &self.program;
        let mut state = JitState::new(self.options, &mut self.input, &mut self.output);
        let mut compiler = JitCompiler::new(program, self.options);
        let mut jit_handles = vec![None; self.program.commands.len()];

        let executor = compiler.executor();
        let mut lock = executor.lock();

        // avoid compiling the first part
        match state.interpret_block(&program.commands) {
            Ok(true) => (),
            Ok(false) => return Ok(()),
            Err(mut e) => if self.options.contains(Options::NO_FALLBACK) {
                e.set_location(*state.index());
                return Err(e)
            } else {
                e.set_location(*state.index());
                return Self::bigint_fallback(&mut state, program, e)
            }
        }

        while let Some(&offset) = jit_handles.get(*state.index()) {
            // can we jit?
            if let Some(offset) = offset {
                let old_index = *state.index();
                unsafe {
                    let lock = executor.lock();
                    state.run_block(lock.ptr(offset));
                }
                // not a bail-out
                if old_index != *state.index() {
                    continue;
                }

            } else if let Some(start) = compiler.compile_index(*state.index()) {
                drop(lock);
                compiler.commit();
                lock = executor.lock();

                let old_index = *state.index();
                jit_handles[old_index] = Some(start);

                unsafe {
                    state.run_block(lock.ptr(start));
                }
                // not a bail-out
                if old_index != *state.index() {
                    continue;
                }
            }

            // fallback interpreting.
            match state.interpret_block(&program.commands) {
                Ok(true) => (),
                Ok(false) => return Ok(()),
                Err(mut e) => if self.options.contains(Options::NO_FALLBACK) {
                    e.set_location(*state.index());
                    return Err(e)
                } else {
                    e.set_location(*state.index());
                    return Self::bigint_fallback(&mut state, program, e)
                }
            }
        }

        if self.options.contains(Options::NO_IMPLICIT_EXIT) {
            Err(WsError::new(WsErrorKind::InvalidIndex, "Invalid program counter"))
        } else {
            Ok(())
        }
    }

    /// Use a jit compiler that compiles the program in a separate thread while executing
    /// the program. It is backed by an optimized datastructure, and will fall back
    /// to interpretation in unsafe situations. When values become too large it will
    /// fall back to bignum-based interpretation.
    #[cfg(any(target_arch="x86_64", target_arch="x86"))]
    pub fn jit_threaded(&mut self) -> Result<(), WsError> {
        // the compiler and comms channel need to live to the end of the crossbeam scope
        let mut compiler = JitCompiler::new(self.program, self.options);
        let (jit_finished_send, jit_finished_receive) = mpsc::channel();

        crossbeam::scope(|scope| {
            // get an executor before we move the compiler to the thread.
            let executor = compiler.executor();

            // this thread compiles our code in the background.
            scope.spawn(move |_| {
                use program::Command::*;
                for (i, c) in compiler.commands.iter().enumerate() {
                    let i = match *c {
                        Label | Call {..} if i + 1 != compiler.commands.len() => i + 1,
                        _ => continue
                    };

                    if let Some(start) = compiler.compile_index(i) {
                        compiler.commit();
                        if jit_finished_send.send((i, start)).is_err() {
                            break;
                        }
                    }
                }
            });

            let mut state = JitState::new(self.options, &mut self.input, &mut self.output);
            let mut jit_handles = vec![None; self.program.commands.len()];

            while let Some(&offset) = jit_handles.get(*state.index()) {
                // can we jit?
                if let Some(offset) = offset {
                    let old_index = *state.index();

                    unsafe {
                        let lock = executor.lock();
                        state.run_block(lock.ptr(offset));
                    }
                    // not a bail-out
                    if old_index != *state.index() {
                        continue;
                    }
                }

                // hot loop optimization: only check for new chunks when we fall back to interpreting.
                while let Ok((index, offset)) = jit_finished_receive.try_recv() {
                    jit_handles[index] = Some(offset);
                }

                // fallback interpreting.
                match state.interpret_block(&self.program.commands) {
                    Ok(true) => (),
                    Ok(false) => {
                        drop(jit_finished_receive);
                        return Ok(())
                    },
                    Err(mut e) => if self.options.contains(Options::NO_FALLBACK) {
                        drop(jit_finished_receive);
                        e.set_location(*state.index());
                        return Err(e);
                    } else {
                        drop(jit_finished_receive);
                        e.set_location(*state.index());
                        return Self::bigint_fallback(&mut state, self.program, e)
                    }
                }
            }

            // drop the channel so the compilation thread will terminate soon
            drop(jit_finished_receive);

            if self.options.contains(Options::NO_IMPLICIT_EXIT) {
                Err(WsError::new(WsErrorKind::InvalidIndex, "Invalid program counter"))
            } else {
                Ok(())
            }
        }).unwrap()
    }
}