Skip to content

Commit 9e3f864

Browse files
committed
JIT: add internal support for None
1 parent ab47293 commit 9e3f864

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

jit/src/instructions.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum JitValue {
2525
Int(Value),
2626
Float(Value),
2727
Bool(Value),
28+
None,
2829
}
2930

3031
impl JitValue {
@@ -41,12 +42,14 @@ impl JitValue {
4142
JitValue::Int(_) => Some(JitType::Int),
4243
JitValue::Float(_) => Some(JitType::Float),
4344
JitValue::Bool(_) => Some(JitType::Bool),
45+
JitValue::None => None,
4446
}
4547
}
4648

4749
fn into_value(self) -> Option<Value> {
4850
match self {
4951
JitValue::Int(val) | JitValue::Float(val) | JitValue::Bool(val) => Some(val),
52+
JitValue::None => None,
5053
}
5154
}
5255
}
@@ -122,6 +125,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
122125
Ok(self.builder.ins().bint(types::I8, val))
123126
}
124127
JitValue::Bool(val) => Ok(val),
128+
JitValue::None => Ok(self.builder.ins().iconst(types::I8, 0)),
125129
}
126130
}
127131

@@ -193,6 +197,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
193197
self.stack.push(JitValue::Bool(val));
194198
Ok(())
195199
}
200+
BorrowedConstant::None => {
201+
self.stack.push(JitValue::None);
202+
Ok(())
203+
}
196204
_ => Err(JitCompileError::NotSupported),
197205
}
198206
}

jit/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ mod bool_tests;
44
mod float_tests;
55
mod int_tests;
66
mod misc_tests;
7+
mod none_tests;

jit/tests/none_tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[test]
2+
fn test_not() {
3+
let not_ = jit_function! { not_(x: i64) -> bool => r##"
4+
def not_(x: int):
5+
return not None
6+
"## };
7+
8+
assert_eq!(not_(0), Ok(true));
9+
}
10+
11+
#[test]
12+
fn test_if_not() {
13+
let if_not = jit_function! { if_not(x: i64) -> i64 => r##"
14+
def if_not(x: int):
15+
if not None:
16+
return 1
17+
else:
18+
return 0
19+
20+
return -1
21+
"## };
22+
23+
assert_eq!(if_not(0), Ok(1));
24+
}

0 commit comments

Comments
 (0)