Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 1910cf3

Browse files
committed
If function, not switching value on condition change
1 parent abd3d92 commit 1910cf3

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use super::{Function, FunctionContext, VarArgs};
2+
use codegen::values::{NumValue};
3+
use inkwell::values::PointerValue;
4+
use inkwell::{FloatPredicate};
5+
//use inkwell::IntPredicate;
6+
use mir::block;
7+
8+
9+
use codegen::{intrinsics, util};
10+
use std::f32::consts;
11+
12+
pub struct IfFunction {}
13+
impl Function for IfFunction {
14+
fn function_type() -> block::Function { block::Function::If }
15+
16+
fn gen_call(
17+
func: &mut FunctionContext,
18+
args: &[PointerValue],
19+
_varargs: Option<VarArgs>,
20+
result: PointerValue,
21+
) {
22+
23+
let result_num = NumValue::new(result);
24+
25+
let condition_num = NumValue::new(args[0]);
26+
let condition_vec = condition_num.get_vec(func.ctx.b);
27+
let left_index = func.ctx.context.i32_type().const_int(0, false);
28+
let condition_left = func
29+
.ctx
30+
.b
31+
.build_extract_element(&condition_vec, &left_index, "condition.left")
32+
.into_float_value();
33+
let condition_bool = func.ctx.b.build_float_compare(
34+
FloatPredicate::OEQ,
35+
condition_left,
36+
func.ctx.context.f32_type().const_float(1.0),
37+
"conditionbool",
38+
);
39+
40+
let then_num = NumValue::new(args[1]);
41+
let then_vec = then_num.get_vec(func.ctx.b);
42+
let then_form = then_num.get_form(func.ctx.b);
43+
44+
let alternative_num = NumValue::new(args[1]);
45+
let alternative_vec = alternative_num.get_vec(func.ctx.b);
46+
47+
let left_index = func.ctx.context.i32_type().const_int(0, false);
48+
let result_vec = func.ctx.b.build_select(condition_bool,
49+
then_vec,
50+
alternative_vec,
51+
"resultvec").into_vector_value();
52+
53+
result_num.set_form(func.ctx.b, &then_form);
54+
result_num.set_vec(func.ctx.b, &result_vec);
55+
}
56+
}

compiler/src/codegen/functions/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod channel_function;
33
mod defer_function;
44
mod delay_function;
55
mod function_context;
6+
mod if_function;
67
mod indexed_function;
78
mod note_function;
89
mod num_function;
@@ -33,6 +34,7 @@ pub use self::biquad_filter_function::*;
3334
pub use self::channel_function::*;
3435
pub use self::defer_function::*;
3536
pub use self::delay_function::*;
37+
pub use self::if_function::*;
3638
pub use self::indexed_function::*;
3739
pub use self::note_function::*;
3840
pub use self::num_function::*;
@@ -161,7 +163,8 @@ map_functions! {
161163
Note => NoteFunction,
162164
Voices => VoicesFunction,
163165
Channel => ChannelFunction,
164-
Indexed => IndexedFunction
166+
Indexed => IndexedFunction,
167+
If => IfFunction
165168
}
166169

167170
fn get_lifecycle_func(

compiler/src/mir/block/function.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ macro_rules! define_functions {
5151
}
5252
}
5353

54-
pub const FUNCTION_TABLE: [&str; 51] = [$($str_name, )*];
54+
pub const FUNCTION_TABLE: [&str; 52] = [$($str_name, )*];
5555
);
5656
}
5757

@@ -106,7 +106,8 @@ define_functions! {
106106
Note = "note" func![(Midi) -> Tuple(vec![Num, Num, Num, Num])],
107107
Voices = "voices" func![(Midi, VarType::new_array(Num)) -> VarType::new_array(Midi)],
108108
Channel = "channel" func![(Midi, Num) -> Midi],
109-
Indexed = "indexed" func![(Num) -> VarType::new_array(Num)]
109+
Indexed = "indexed" func![(Num) -> VarType::new_array(Num)],
110+
If = "if" func![(Num, Num, Num) -> Num]
110111
}
111112

112113
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)