Skip to content

Commit

Permalink
Code cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
seanyoung committed Apr 8, 2024
1 parent 1790faf commit ce56539
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 61 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- scancode <=> irp mapping
- lircd.conf generate -> send to lircd -> correct result?
- bpf feature should not be the default in irp crate
- DFA edges can be overlapping - removing overlapping parts

ir-keytable -c

Expand Down
120 changes: 59 additions & 61 deletions irp/src/build_bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,51 +201,55 @@ impl<'a> Builder<'a> {
self.decoder_state = decoder_state;

// load the lirc mode2 value and check its type
let lirc = self
let lirc_mode2 = self
.builder
.build_load(
i32,
function.get_first_param().unwrap().into_pointer_value(),
"lirc",
"lirc_mode2",
)
.unwrap()
.into_int_value();

let lirc_ty = self
let lirc_mode2_ty = self
.builder
.build_right_shift(lirc, i32.const_int(24, false), false, "lirc_ty")
.build_right_shift(lirc_mode2, i32.const_int(24, false), false, "lirc_mode2_ty")
.unwrap();

let lirc_ok = context.append_basic_block(function, "lirc_ok");
let error = context.append_basic_block(function, "error");

self.builder
.build_switch(
lirc_ty,
zero_key,
lirc_mode2_ty,
zero_key, // ignore LIRC_MODE2_FREQUENCY and anything unknown
&[
// LIRC_MODE2_SPACE
(i32.const_zero(), lirc_ok),
// LIRC_MODE2_PULSE
(i32.const_int(1, false), lirc_ok),
// LIRC_MODE2_TIMEOUT
(i32.const_int(3, false), lirc_ok),
// LIRC_MODE2_OVERFLOW
(i32.const_int(4, false), error),
],
)
.unwrap();

self.builder.position_at_end(lirc_ok);

// we known the lirc mode2 value is ok,

// we know the lirc mode2 value is ok
let length = self
.builder
.build_and(lirc, i32.const_int(0x00ff_ffff, false), "length")
.build_and(lirc_mode2, i32.const_int(0x00ff_ffff, false), "length")
.unwrap();

// false for LIRC_MODE2_SPACE and LIRC_MODE2_TIMEOUT, true for LIRC_MODE2_PULSE
let is_pulse = self
.builder
.build_int_compare(
IntPredicate::EQ,
lirc_ty,
lirc_mode2_ty,
i32.const_int(1, false),
"is_pulse",
)
Expand All @@ -272,9 +276,9 @@ impl<'a> Builder<'a> {

let mut cases = Vec::new();

// we will add a switch statement at the end of lirc ok once we have built all the cases
// we will add a switch statement at the end of lirc_ok block once we have built all the cases
for (state_no, vert) in self.dfa.verts.iter().enumerate() {
let block = context.append_basic_block(function, &format!("state_{state_no}_entry"));
let block = context.append_basic_block(function, &format!("state_{state_no}"));
self.builder.position_at_end(block);

cases.push((i64.const_int(state_no as u64, false), block));
Expand Down Expand Up @@ -385,12 +389,12 @@ impl<'a> Builder<'a> {
}
}
Action::Set { var, expr } => {
let value = self.emit(expr, context);
let value = self.expression(expr, context);
self.update_var(var, value);
}
Action::AssertEq { left, right } => {
let left = self.emit(left, context);
let right = self.emit(right, context);
let left = self.expression(left, context);
let right = self.expression(right, context);

let ok = context.append_basic_block(function, "ok");

Expand All @@ -416,7 +420,7 @@ impl<'a> Builder<'a> {
}
Action::Done(ev, _) => {
let flags = if self.vars.contains_key("T") {
// T should be 0 or 1; this corresponds with LIRC_SCANCODE_FA
// T should be 0 or 1; this corresponds with LIRC_SCANCODE_FLAGS_TOGGLE
self.load_var("T", context)
} else {
context.i64_type().const_zero()
Expand Down Expand Up @@ -507,28 +511,28 @@ impl<'a> Builder<'a> {
.unwrap();
}

fn emit(&mut self, expr: &'a Rc<Expression>, context: &'a Context) -> IntValue<'a> {
fn expression(&mut self, expr: &'a Rc<Expression>, context: &'a Context) -> IntValue<'a> {
macro_rules! unary {
($expr:expr, $op:ident) => {{
let expr = self.emit($expr, context);
let expr = self.expression($expr, context);

self.builder.$op(expr, "").unwrap()
}};
}

macro_rules! binary {
($left:expr, $right:expr, $op:ident) => {{
let left = self.emit($left, context);
let right = self.emit($right, context);
let left = self.expression($left, context);
let right = self.expression($right, context);

self.builder.$op(left, right, "").unwrap()
}};
}

macro_rules! compare {
($left:expr, $right:expr, $pred:path) => {{
let left = self.emit($left, context);
let right = self.emit($right, context);
let left = self.expression($left, context);
let right = self.expression($right, context);

self.builder
.build_int_z_extend(
Expand All @@ -548,7 +552,7 @@ impl<'a> Builder<'a> {
Expression::Complement(expr) => unary!(expr, build_not),
Expression::Negative(expr) => unary!(expr, build_int_neg),
Expression::Not(expr) => {
let expr = self.emit(expr, context);
let expr = self.expression(expr, context);

self.builder
.build_int_z_extend(
Expand All @@ -566,7 +570,7 @@ impl<'a> Builder<'a> {
.unwrap()
}
Expression::BitCount(expr) => {
let expr = self.emit(expr, context);
let expr = self.expression(expr, context);

let i64 = context.i64_type();

Expand Down Expand Up @@ -596,8 +600,8 @@ impl<'a> Builder<'a> {

Expression::ShiftLeft(left, right) => binary!(left, right, build_left_shift),
Expression::ShiftRight(left, right) => {
let left = self.emit(left, context);
let right = self.emit(right, context);
let left = self.expression(left, context);
let right = self.expression(right, context);

self.builder
.build_right_shift(left, right, false, "")
Expand All @@ -617,51 +621,45 @@ impl<'a> Builder<'a> {
}

fn load_var(&mut self, name: &'a str, context: &'a Context) -> IntValue<'a> {
match self.vars.get_mut(name) {
Some(e) => {
if let Some(value) = e.value {
return value;
}
let e = self.vars.get_mut(name).unwrap();

let load = self
.builder
.build_load(
context.i64_type(),
self.builder
.build_struct_gep(
self.decoder_state_ty,
self.decoder_state,
e.offset as u32,
name,
)
.unwrap(),
if let Some(value) = e.value {
return value;
}

let load = self
.builder
.build_load(
context.i64_type(),
self.builder
.build_struct_gep(
self.decoder_state_ty,
self.decoder_state,
e.offset as u32,
name,
)
.unwrap();
.unwrap(),
name,
)
.unwrap();

load.as_instruction_value()
.unwrap()
.set_alignment(8)
.unwrap();
load.as_instruction_value()
.unwrap()
.set_alignment(8)
.unwrap();

let value = load.into_int_value();
let value = load.into_int_value();

e.value = Some(value);
e.value = Some(value);

value
}
None => unreachable!(),
}
value
}

fn update_var(&mut self, name: &'a str, value: IntValue<'a>) {
match self.vars.get_mut(name) {
Some(e) => {
e.value = Some(value);
e.dirty = true;
}
None => unreachable!(),
}
let e = self.vars.get_mut(name).unwrap();

e.value = Some(value);
e.dirty = true;
}

fn write_dirty(&self) {
Expand Down

0 comments on commit ce56539

Please sign in to comment.