From 731a8e6b186adc7aea5baed9ef8649a20ed5e463 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Fri, 17 Jan 2020 15:24:53 -0500 Subject: [PATCH] Update immediate format to literals Following discussion on twitter, this is changing to match Jordan's implementation. https://twitter.com/UINT_MIN/status/1218190029207613441 > FWIW I picked a different integer syntax than you (Swift/Rust-style > 0x123, 0b123) [...] --- toolchain/src/inst/format.rs | 34 +++---- toolchain/src/inst/parse.rs | 23 +++-- toolchain/src/inst/test.rs | 184 +++++++++++++++++------------------ 3 files changed, 125 insertions(+), 116 deletions(-) diff --git a/toolchain/src/inst/format.rs b/toolchain/src/inst/format.rs index 83a490e..7333a38 100644 --- a/toolchain/src/inst/format.rs +++ b/toolchain/src/inst/format.rs @@ -56,25 +56,25 @@ impl Display for Inst { AluR(OpR::Asr, r) => write!(fmt, "ASRR {}", r), Mem(LdSt::Ld, d, r, i) => write!(fmt, "LD{}U {}{}", data(d), r, flag(i)), Mem(LdSt::St, d, r, i) => write!(fmt, "ST{}U {}{}", data(d), r, flag(i)), - AluI(OpI::And(i)) => write!(fmt, "ANDI #x{:02X}", i), - AluI(OpI::Ior(i)) => write!(fmt, "IORI #x{:02X}", i), - AluI(OpI::Xor(i)) => write!(fmt, "XORI #x{:02X}", i), - AluI(OpI::Add(AddImm(i))) => write!(fmt, "ADDI #{}", i), + AluI(OpI::And(i)) => write!(fmt, "ANDI 0x{:02X}", i), + AluI(OpI::Ior(i)) => write!(fmt, "IORI 0x{:02X}", i), + AluI(OpI::Xor(i)) => write!(fmt, "XORI 0x{:02X}", i), + AluI(OpI::Add(AddImm(i))) => write!(fmt, "ADDI {}", i), AluI(OpI::Compact(op, i)) => match op { - OpI3::Rol => write!(fmt, "ROLI #{}", i), - OpI3::Lsl => write!(fmt, "LSLI #{}", i), - OpI3::Lsr => write!(fmt, "LSRI #{}", i), - OpI3::Asr => write!(fmt, "ASRI #{}", i), - OpI3::Clr => write!(fmt, "CLRI #{}", i), - OpI3::Ins => write!(fmt, "INSI #{}", i), - OpI3::Tog => write!(fmt, "TOGI #{}", i), - OpI3::Ext => write!(fmt, "EXTI #{}", i), + OpI3::Rol => write!(fmt, "ROLI {}", i), + OpI3::Lsl => write!(fmt, "LSLI {}", i), + OpI3::Lsr => write!(fmt, "LSRI {}", i), + OpI3::Asr => write!(fmt, "ASRI {}", i), + OpI3::Clr => write!(fmt, "CLRI {}", i), + OpI3::Ins => write!(fmt, "INSI {}", i), + OpI3::Tog => write!(fmt, "TOGI {}", i), + OpI3::Ext => write!(fmt, "EXTI {}", i), }, - BEzI(off) => write!(fmt, "BEZI #{}", off), - JOfI(i) => write!(fmt, "JOFI #{}", i), - CAbI(i) => write!(fmt, "CABI #{}", i), - COfI(i) => write!(fmt, "COFI #{}", i), - GetI(i) => write!(fmt, "GETI #{}", i), + BEzI(off) => write!(fmt, "BEZI {}", off), + JOfI(i) => write!(fmt, "JOFI {}", i), + CAbI(i) => write!(fmt, "CABI {}", i), + COfI(i) => write!(fmt, "COFI {}", i), + GetI(i) => write!(fmt, "GETI {}", i), } } } diff --git a/toolchain/src/inst/parse.rs b/toolchain/src/inst/parse.rs index 5eeff9a..ff6e5f4 100644 --- a/toolchain/src/inst/parse.rs +++ b/toolchain/src/inst/parse.rs @@ -5,7 +5,7 @@ use nom::{ character::complete::{char, digit1, hex_digit1, oct_digit1, one_of, space1}, combinator::{complete, map, map_opt, map_res, opt, recognize, value}, multi::many1, - sequence::{pair, preceded}, + sequence::{pair, preceded, terminated}, IResult, }; use std::convert::TryFrom; @@ -99,29 +99,38 @@ fn reg(s: &str) -> IResult<&str, Reg> { map_opt(num, Reg::new)(s) } +fn sign(s: &str) -> IResult<&str, i16> { + alt(( + value(1, char('+')), + value(-1, char('-')), + value(1, tag_no_case("")), + ))(s) +} + fn imm_dec(s: &str) -> IResult<&str, i16> { - let text = recognize(pair(opt(one_of("+-")), digit1)); - let prefix = alt((tag_no_case("#d"), tag_no_case("#"))); - map_res(preceded(prefix, text), |x: &str| x.parse())(s) + let prefix = terminated(sign, opt(tag_no_case("0d"))); + map_res(pair(prefix, digit1), |p: (i16, &str)| { + p.1.parse::().map(|x| x * p.0) + })(s) } fn imm_hex(s: &str) -> IResult<&str, i16> { let text = recognize(hex_digit1); - map_res(preceded(tag_no_case("#x"), text), |x| { + map_res(preceded(tag_no_case("0x"), text), |x| { i16::from_str_radix(x, 16) })(s) } fn imm_oct(s: &str) -> IResult<&str, i16> { let text = recognize(oct_digit1); - map_res(preceded(tag_no_case("#o"), text), |x| { + map_res(preceded(tag_no_case("0o"), text), |x| { i16::from_str_radix(x, 8) })(s) } fn imm_bin(s: &str) -> IResult<&str, i16> { let text = recognize(many1(one_of("01"))); - map_res(preceded(tag_no_case("#b"), text), |x| { + map_res(preceded(tag_no_case("0b"), text), |x| { i16::from_str_radix(x, 2) })(s) } diff --git a/toolchain/src/inst/test.rs b/toolchain/src/inst/test.rs index 0f04485..9806dbc 100644 --- a/toolchain/src/inst/test.rs +++ b/toolchain/src/inst/test.rs @@ -201,115 +201,115 @@ fn ldst() { #[test] fn alu_i() { test_parse! { - "andi #00" => "1111_0000 0000_0000", - "andi #0000" => "1111_0000 0000_0000", - "andi #10" => "1111_0000 0000_1010", - "andi #255" => "1111_0000 1111_1111", - "andi #-1" => !, - "andi #-23" => !, - "andi #256" => !, - - "andi #d00" => "1111_0000 0000_0000", - "andi #D0000" => "1111_0000 0000_0000", - "andi #d10" => "1111_0000 0000_1010", - "andi #D255" => "1111_0000 1111_1111", - "andi #d-1" => !, - "andi #d-23" => !, - "andi #d256" => !, - - "andi #x00" => "1111_0000 0000_0000", - "andi #XFF" => "1111_0000 1111_1111", - "andi #xAA" => "1111_0000 1010_1010", - "andi #o177" => "1111_0000 0111_1111", - "andi #O177" => "1111_0000 0111_1111", - "andi #b11010000" => "1111_0000 1101_0000", - - "iori #00" => "1111_0001 0000_0000", - "iori #32" => "1111_0001 0010_0000", - "iori #01" => "1111_0001 0000_0001", - "iori #-1" => !, - - "xori #00" => "1111_0010 0000_0000", - "xori #07" => "1111_0010 0000_0111", - "xori #255" => "1111_0010 1111_1111", - "xori #300" => !, - - "addi #0" => "1111_0011 0000_0000", - "addi #1" => "1111_0011 0000_0001", - "addi #127" => "1111_0011 0111_1111", - "addi #128" => !, - "addi #250" => !, - "addi #-1" => "1111_0011 1111_1111", - "addi #-10" => "1111_0011 1111_0110", - "addi #-64" => "1111_0011 1100_0000", - "addi #-65" => !, - "addi #-100" => !, - - "addi #x7F" => "1111_0011 0111_1111", - "addi #xFF" => !, + "andi 00" => "1111_0000 0000_0000", + "andi 0000" => "1111_0000 0000_0000", + "andi 10" => "1111_0000 0000_1010", + "andi 255" => "1111_0000 1111_1111", + "andi -1" => !, + "andi -23" => !, + "andi 256" => !, + + "andi 0d00" => "1111_0000 0000_0000", + "andi 0D0000" => "1111_0000 0000_0000", + "andi 0d10" => "1111_0000 0000_1010", + "andi 0D255" => "1111_0000 1111_1111", + "andi -0d1" => !, + "andi -0d23" => !, + "andi 0d256" => !, + + "andi 0x00" => "1111_0000 0000_0000", + "andi 0XFF" => "1111_0000 1111_1111", + "andi 0xAA" => "1111_0000 1010_1010", + "andi 0o177" => "1111_0000 0111_1111", + "andi 0O177" => "1111_0000 0111_1111", + "andi 0b11010000" => "1111_0000 1101_0000", + + "iori 00" => "1111_0001 0000_0000", + "iori 32" => "1111_0001 0010_0000", + "iori 01" => "1111_0001 0000_0001", + "iori -1" => !, + + "xori 00" => "1111_0010 0000_0000", + "xori 07" => "1111_0010 0000_0111", + "xori 255" => "1111_0010 1111_1111", + "xori 300" => !, + + "addi 0" => "1111_0011 0000_0000", + "addi 1" => "1111_0011 0000_0001", + "addi 127" => "1111_0011 0111_1111", + "addi 128" => !, + "addi 250" => !, + "addi -1" => "1111_0011 1111_1111", + "addi -10" => "1111_0011 1111_0110", + "addi -64" => "1111_0011 1100_0000", + "addi -65" => !, + "addi -100" => !, + + "addi 0x7F" => "1111_0011 0111_1111", + "addi 0xFF" => !, } } #[test] fn alu_compact() { test_parse! { - "roli #0" => "1111_0011 1000_0000", - "roli #7" => "1111_0011 1000_0111", - "roli #-1" => !, - "roli #8" => !, - - "lsli #0" => "1111_0011 1000_1000", - "lsli #7" => "1111_0011 1000_1111", - "lsri #0" => "1111_0011 1001_0000", - "lsri #7" => "1111_0011 1001_0111", - "asri #0" => "1111_0011 1001_1000", - "asri #7" => "1111_0011 1001_1111", - - "clri #0" => "1111_0011 1010_0000", - "clri #7" => "1111_0011 1010_0111", - "insi #0" => "1111_0011 1010_1000", - "insi #7" => "1111_0011 1010_1111", - "togi #0" => "1111_0011 1011_0000", - "togi #7" => "1111_0011 1011_0111", - "exti #0" => "1111_0011 1011_1000", - "exti #7" => "1111_0011 1011_1111", + "roli 0" => "1111_0011 1000_0000", + "roli 7" => "1111_0011 1000_0111", + "roli -1" => !, + "roli 8" => !, + + "lsli 0" => "1111_0011 1000_1000", + "lsli 7" => "1111_0011 1000_1111", + "lsri 0" => "1111_0011 1001_0000", + "lsri 7" => "1111_0011 1001_0111", + "asri 0" => "1111_0011 1001_1000", + "asri 7" => "1111_0011 1001_1111", + + "clri 0" => "1111_0011 1010_0000", + "clri 7" => "1111_0011 1010_0111", + "insi 0" => "1111_0011 1010_1000", + "insi 7" => "1111_0011 1010_1111", + "togi 0" => "1111_0011 1011_0000", + "togi 7" => "1111_0011 1011_0111", + "exti 0" => "1111_0011 1011_1000", + "exti 7" => "1111_0011 1011_1111", } } #[test] fn imm_jumps() { test_parse! { - "bezi #0" => "1111_0100 0000_0000", - "bezi #1" => "1111_0100 0000_0001", - "bezi #-1" => "1111_0100 1111_1111", - "bezi #-128" => "1111_0100 1000_0000", - "bezi #127" => "1111_0100 0111_1111", - "bezi #-129" => !, - "bezi #128" => !, - - "jofi #0" => "1111_0101 0000_0000", - "jofi #1" => "1111_0101 0000_0001", - "jofi #-1" => "1111_0101 1111_1111", - - "cabi #0" => "1111_0110 0000_0000", - "cabi #1" => "1111_0110 0000_0001", - "cabi #-1" => !, - - "cofi #0" => "1111_0111 0000_0000", - "cofi #1" => "1111_0111 0000_0001", - "cofi #-1" => "1111_0111 1111_1111", + "bezi 0" => "1111_0100 0000_0000", + "bezi 1" => "1111_0100 0000_0001", + "bezi -1" => "1111_0100 1111_1111", + "bezi -128" => "1111_0100 1000_0000", + "bezi 127" => "1111_0100 0111_1111", + "bezi -129" => !, + "bezi 128" => !, + + "jofi 0" => "1111_0101 0000_0000", + "jofi 1" => "1111_0101 0000_0001", + "jofi -1" => "1111_0101 1111_1111", + + "cabi 0" => "1111_0110 0000_0000", + "cabi 1" => "1111_0110 0000_0001", + "cabi -1" => !, + + "cofi 0" => "1111_0111 0000_0000", + "cofi 1" => "1111_0111 0000_0001", + "cofi -1" => "1111_0111 1111_1111", } } #[test] fn geti() { test_parse! { - "geti #0" => "1111_1110 0000_0000", - "geti #1" => "1111_1110 0000_0001", - "geti #xFF" => "1111_1110 1111_1111", - "geti #x100" => !, - "geti #-1" => "1111_1110 1111_1111", - "geti #-128" => "1111_1110 1000_0000", - "geti #-129" => !, + "geti 0" => "1111_1110 0000_0000", + "geti 1" => "1111_1110 0000_0001", + "geti 0xFF" => "1111_1110 1111_1111", + "geti 0x100" => !, + "geti -1" => "1111_1110 1111_1111", + "geti -128" => "1111_1110 1000_0000", + "geti -129" => !, } } -- 2.47.0