Skip to content

Commit

Permalink
JumpIfTrue
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie committed Oct 12, 2020
1 parent cda61b4 commit bade10d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions jit/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {

Ok(())
}
Instruction::JumpIfTrue { target } => {
let cond = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;

let then_block = self.builder.create_block();
self.label_to_block.insert(*target, then_block);

let val = self.boolean_val(cond)?;
self.builder.ins().brnz(val, then_block, &[]);

let block = self.builder.create_block();
self.builder.ins().fallthrough(block, &[]);
self.builder.switch_to_block(block);

Ok(())
}
Instruction::Jump { target } => {
let target_block = self.builder.create_block();
self.label_to_block.insert(*target, target_block);
Expand Down
17 changes: 17 additions & 0 deletions jit/tests/bool_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ fn test_not() {
assert_eq!(not_(true), Ok(false));
assert_eq!(not_(false), Ok(true));
}

#[test]
fn test_if_not() {
let if_not = jit_function! { if_not(a: bool) -> i64 => r##"
def if_not(a: bool):
if not a:
return 0
else:
return 1
return -1
"## };

assert_eq!(if_not(true), Ok(1));
assert_eq!(if_not(false), Ok(0));
}

0 comments on commit bade10d

Please sign in to comment.