Created
August 7, 2016 15:20
-
-
Save mattbettcher/6175203971d6ae9b5759edc14d136afa to your computer and use it in GitHub Desktop.
Problem with Tuples and Function Pointers in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate regex; | |
use regex::Regex; | |
macro_rules! mask_shift { | |
($v: ident, $m: expr, $s: expr) => { | |
(($v as u32 & $m as u32) >> $s) as u32 | |
} | |
} | |
macro_rules! shift_mask { | |
($v: ident, $m: expr, $s: expr) => { | |
(($v as u32 >> $s as u32) & $m) as u32 | |
} | |
} | |
#[derive(Debug)] | |
enum Opcode { | |
// data movement | |
EXG, | |
LEA, | |
} | |
// goal of opcode functions is to gen correct code for each instruction | |
// operand is actual operand passed to gen func | |
fn gen_exg(op: u16) { | |
// this needs to gen 3 functions not 63... | |
// must have some way to determine if we have already generated | |
// a specific path... | |
// static EXG_PATH: i32 = 0; | |
// decode the operand | |
let opmode = shift_mask!(op, 0b11111, 3); // this defines the number of generated functions, 3 in this case | |
match opmode { | |
0b01000 => println!("Swap data"), // swap data regs | |
0b01001 => println!("Swap addr"), // swap addr regs | |
0b10001 => println!("Swap data with addr"), // swap data with addr regs | |
_ => println!("BAD"),//panic!("{:4X} Opmode doesn't exist!", op), | |
}; | |
} | |
fn gen_lea(op: u16) { | |
println!("{:?}", op); | |
} | |
fn main() { | |
// optable is opcode name, gen function, and match pattern | |
let mut optable: Vec<(Opcode, fn(u16) -> (), Regex)> = Vec::new(); | |
optable.push((Opcode::EXG, | |
gen_exg, | |
Regex::new(r"1100[0-1]{3}1(01000|01001|10001)[0-1]{3}").unwrap())); | |
optable.push((Opcode::LEA, | |
gen_lea, | |
Regex::new(r"0100[0-1]{3}111(((010|101|110)[0-1]{3})|(111(000|001|010|011)))").unwrap())); | |
for i in 0x0..0x10000 { | |
for op in &optable { | |
if op.2.is_match(&format!("{:016b}", i)) { | |
println!("Found Match! {:?}", op.0); | |
op.1(i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment