Skip to content

Commit

Permalink
Add an enum to specify the endianness of type consts
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Dec 3, 2017
1 parent 9fbf0f1 commit c2ef3eb
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 109 deletions.
42 changes: 22 additions & 20 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,30 +317,32 @@ fn lower_parse_ty_const<'doc, 'a: 'doc, A: DocAllocator<'doc>>(
doc: &'doc A,
ty_const: BinaryTypeConst,
) -> DocBuilder<'doc, A> {
use ir::ast::Endianness as E;

doc.text(match ty_const {
BinaryTypeConst::Empty => "Ok::<_, io::Error>(())",
BinaryTypeConst::U8 => "ddl_util::from_u8(reader)",
BinaryTypeConst::I8 => "ddl_util::from_i8(reader)",
BinaryTypeConst::U16Le => "ddl_util::from_u16le(reader)",
BinaryTypeConst::U24Le => "ddl_util::from_u24le(reader)",
BinaryTypeConst::U32Le => "ddl_util::from_u32le(reader)",
BinaryTypeConst::U64Le => "ddl_util::from_u64le(reader)",
BinaryTypeConst::I16Le => "ddl_util::from_i16le(reader)",
BinaryTypeConst::I24Le => "ddl_util::from_i24le(reader)",
BinaryTypeConst::I32Le => "ddl_util::from_i32le(reader)",
BinaryTypeConst::I64Le => "ddl_util::from_i64le(reader)",
BinaryTypeConst::F32Le => "ddl_util::from_f32le(reader)",
BinaryTypeConst::F64Le => "ddl_util::from_f64le(reader)",
BinaryTypeConst::U16Be => "ddl_util::from_u16be(reader)",
BinaryTypeConst::U24Be => "ddl_util::from_u24be(reader)",
BinaryTypeConst::U32Be => "ddl_util::from_u32be(reader)",
BinaryTypeConst::U64Be => "ddl_util::from_u64be(reader)",
BinaryTypeConst::I16Be => "ddl_util::from_i16be(reader)",
BinaryTypeConst::I24Be => "ddl_util::from_i24be(reader)",
BinaryTypeConst::I32Be => "ddl_util::from_i32be(reader)",
BinaryTypeConst::I64Be => "ddl_util::from_i64be(reader)",
BinaryTypeConst::F32Be => "ddl_util::from_f32be(reader)",
BinaryTypeConst::F64Be => "ddl_util::from_f64be(reader)",
BinaryTypeConst::U16(E::Little) => "ddl_util::from_u16le(reader)",
BinaryTypeConst::U24(E::Little) => "ddl_util::from_u24le(reader)",
BinaryTypeConst::U32(E::Little) => "ddl_util::from_u32le(reader)",
BinaryTypeConst::U64(E::Little) => "ddl_util::from_u64le(reader)",
BinaryTypeConst::I16(E::Little) => "ddl_util::from_i16le(reader)",
BinaryTypeConst::I24(E::Little) => "ddl_util::from_i24le(reader)",
BinaryTypeConst::I32(E::Little) => "ddl_util::from_i32le(reader)",
BinaryTypeConst::I64(E::Little) => "ddl_util::from_i64le(reader)",
BinaryTypeConst::F32(E::Little) => "ddl_util::from_f32le(reader)",
BinaryTypeConst::F64(E::Little) => "ddl_util::from_f64le(reader)",
BinaryTypeConst::U16(E::Big) => "ddl_util::from_u16be(reader)",
BinaryTypeConst::U24(E::Big) => "ddl_util::from_u24be(reader)",
BinaryTypeConst::U32(E::Big) => "ddl_util::from_u32be(reader)",
BinaryTypeConst::U64(E::Big) => "ddl_util::from_u64be(reader)",
BinaryTypeConst::I16(E::Big) => "ddl_util::from_i16be(reader)",
BinaryTypeConst::I24(E::Big) => "ddl_util::from_i24be(reader)",
BinaryTypeConst::I32(E::Big) => "ddl_util::from_i32be(reader)",
BinaryTypeConst::I64(E::Big) => "ddl_util::from_i64be(reader)",
BinaryTypeConst::F32(E::Big) => "ddl_util::from_f32be(reader)",
BinaryTypeConst::F64(E::Big) => "ddl_util::from_f64be(reader)",
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/ir/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use name::{Name, Named};
pub use syntax::ast::Field;
pub use syntax::ast::host::{Binop, Const, IntSuffix, TypeConst, Unop};
pub use syntax::ast::host::{FloatType, SignedType, UnsignedType};
pub use syntax::ast::binary::TypeConst as BinaryTypeConst;
pub use syntax::ast::binary::{Endianness, TypeConst as BinaryTypeConst};
use var::{ScopeIndex, Var};

#[derive(Debug, Clone, PartialEq)]
Expand Down
73 changes: 43 additions & 30 deletions src/syntax/ast/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use source::Span;
use syntax::ast::{self, host, Field, Substitutions};
use var::{ScopeIndex, Var};

/// Kinds of binary types
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Kind {
/// Kind of types
Expand All @@ -24,6 +25,7 @@ impl Kind {
Kind::Arrow { arity }
}

/// The host representation of the binary kind
pub fn repr(self) -> host::Kind {
match self {
Kind::Type => host::Kind::Type,
Expand All @@ -32,33 +34,44 @@ impl Kind {
}
}

/// The endianness (byte order) of a type
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Endianness {
/// Big endian
Big,
/// Little endian
Little,
}

/// A type constant in the binary language
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TypeConst {
/// Empty binary type
Empty,
/// Unsigned 8-bit integer
U8,
/// Signed 8-bit integer
I8,
U16Le,
U24Le,
U32Le,
U64Le,
I16Le,
I24Le,
I32Le,
I64Le,
F32Le,
F64Le,
U16Be,
U24Be,
U32Be,
U64Be,
I16Be,
I24Be,
I32Be,
I64Be,
F32Be,
F64Be,
/// Unsigned 16-bit integer
U16(Endianness),
/// Unsigned 24-bit integer
U24(Endianness),
/// Unsigned 32-bit integer
U32(Endianness),
/// Unsigned 64-bit integer
U64(Endianness),
/// Signed 16-bit integer
I16(Endianness),
/// Signed 24-bit integer
I24(Endianness),
/// Signed 32-bit integer
I32(Endianness),
/// Signed 64-bit integer
I64(Endianness),
/// IEEE-754 32-bit float
F32(Endianness),
/// IEEE-754 64-bit float
F64(Endianness),
}

impl TypeConst {
Expand All @@ -70,16 +83,16 @@ impl TypeConst {
TypeConst::Empty => host::TypeConst::Unit,
TypeConst::U8 => host::TypeConst::Unsigned(UnsignedType::U8),
TypeConst::I8 => host::TypeConst::Signed(SignedType::I8),
TypeConst::U16Le | TypeConst::U16Be => host::TypeConst::Unsigned(UnsignedType::U16),
TypeConst::U24Le | TypeConst::U24Be => host::TypeConst::Unsigned(UnsignedType::U24),
TypeConst::U32Le | TypeConst::U32Be => host::TypeConst::Unsigned(UnsignedType::U32),
TypeConst::U64Le | TypeConst::U64Be => host::TypeConst::Unsigned(UnsignedType::U64),
TypeConst::I16Le | TypeConst::I16Be => host::TypeConst::Signed(SignedType::I16),
TypeConst::I24Le | TypeConst::I24Be => host::TypeConst::Signed(SignedType::I24),
TypeConst::I32Le | TypeConst::I32Be => host::TypeConst::Signed(SignedType::I32),
TypeConst::I64Le | TypeConst::I64Be => host::TypeConst::Signed(SignedType::I64),
TypeConst::F32Le | TypeConst::F32Be => host::TypeConst::Float(FloatType::F32),
TypeConst::F64Le | TypeConst::F64Be => host::TypeConst::Float(FloatType::F64),
TypeConst::U16(_) => host::TypeConst::Unsigned(UnsignedType::U16),
TypeConst::U24(_) => host::TypeConst::Unsigned(UnsignedType::U24),
TypeConst::U32(_) => host::TypeConst::Unsigned(UnsignedType::U32),
TypeConst::U64(_) => host::TypeConst::Unsigned(UnsignedType::U64),
TypeConst::I16(_) => host::TypeConst::Signed(SignedType::I16),
TypeConst::I24(_) => host::TypeConst::Signed(SignedType::I24),
TypeConst::I32(_) => host::TypeConst::Signed(SignedType::I32),
TypeConst::I64(_) => host::TypeConst::Signed(SignedType::I64),
TypeConst::F32(_) => host::TypeConst::Float(FloatType::F32),
TypeConst::F64(_) => host::TypeConst::Float(FloatType::F64),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/syntax/ast/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use source::Span;
use syntax::ast::{self, Field, Substitutions};
use var::{ScopeIndex, Var};

/// Kinds of host type
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Kind {
/// Kind of types
Expand Down Expand Up @@ -266,9 +267,9 @@ impl<N: Name> Expr<N> {

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FloatType {
/// 32-bit float
/// IEE-754 32-bit float
F32,
/// 64-bit float
/// IEE-754 64-bit float
F64,
}

Expand Down
42 changes: 21 additions & 21 deletions src/syntax/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<N: Name> Program<N> {
}

pub fn base_defs<N: Name + for<'a> From<&'a str>>() -> Substitutions<N> {
use syntax::ast::binary::{Type, TypeConst};
use syntax::ast::binary::{Endianness, Type, TypeConst};

btreemap! {
// TODO: "true" = Expr::bool(true)
Expand All @@ -129,26 +129,26 @@ pub fn base_defs<N: Name + for<'a> From<&'a str>>() -> Substitutions<N> {
"u8".into() => Type::Const(TypeConst::U8),
"i8".into() => Type::Const(TypeConst::I8),
// Little endian primitives
"u16le".into() => Type::Const(TypeConst::U16Le),
"u24le".into() => Type::Const(TypeConst::U24Le),
"u32le".into() => Type::Const(TypeConst::U32Le),
"u64le".into() => Type::Const(TypeConst::U64Le),
"i16le".into() => Type::Const(TypeConst::I16Le),
"i24le".into() => Type::Const(TypeConst::I24Le),
"i32le".into() => Type::Const(TypeConst::I32Le),
"i64le".into() => Type::Const(TypeConst::I64Le),
"f32le".into() => Type::Const(TypeConst::F32Le),
"f64le".into() => Type::Const(TypeConst::F64Le),
"u16le".into() => Type::Const(TypeConst::U16(Endianness::Little)),
"u24le".into() => Type::Const(TypeConst::U24(Endianness::Little)),
"u32le".into() => Type::Const(TypeConst::U32(Endianness::Little)),
"u64le".into() => Type::Const(TypeConst::U64(Endianness::Little)),
"i16le".into() => Type::Const(TypeConst::I16(Endianness::Little)),
"i24le".into() => Type::Const(TypeConst::I24(Endianness::Little)),
"i32le".into() => Type::Const(TypeConst::I32(Endianness::Little)),
"i64le".into() => Type::Const(TypeConst::I64(Endianness::Little)),
"f32le".into() => Type::Const(TypeConst::F32(Endianness::Little)),
"f64le".into() => Type::Const(TypeConst::F64(Endianness::Little)),
// Big endian primitives
"u16be".into() => Type::Const(TypeConst::U16Be),
"u24be".into() => Type::Const(TypeConst::U24Be),
"u32be".into() => Type::Const(TypeConst::U32Be),
"u64be".into() => Type::Const(TypeConst::U64Be),
"i16be".into() => Type::Const(TypeConst::I16Be),
"i24be".into() => Type::Const(TypeConst::I24Be),
"i32be".into() => Type::Const(TypeConst::I32Be),
"i64be".into() => Type::Const(TypeConst::I64Be),
"f32be".into() => Type::Const(TypeConst::F32Be),
"f64be".into() => Type::Const(TypeConst::F64Be),
"u16be".into() => Type::Const(TypeConst::U16(Endianness::Big)),
"u24be".into() => Type::Const(TypeConst::U24(Endianness::Big)),
"u32be".into() => Type::Const(TypeConst::U32(Endianness::Big)),
"u64be".into() => Type::Const(TypeConst::U64(Endianness::Big)),
"i16be".into() => Type::Const(TypeConst::I16(Endianness::Big)),
"i24be".into() => Type::Const(TypeConst::I24(Endianness::Big)),
"i32be".into() => Type::Const(TypeConst::I32(Endianness::Big)),
"i64be".into() => Type::Const(TypeConst::I64(Endianness::Big)),
"f32be".into() => Type::Const(TypeConst::F32(Endianness::Big)),
"f64be".into() => Type::Const(TypeConst::F64(Endianness::Big)),
}
}
30 changes: 10 additions & 20 deletions src/syntax/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,26 +389,16 @@ pub fn kind_of<N: Name>(
Type::Const(TypeConst::Empty) |
Type::Const(TypeConst::U8) |
Type::Const(TypeConst::I8) |
Type::Const(TypeConst::U16Le) |
Type::Const(TypeConst::U24Le) |
Type::Const(TypeConst::U32Le) |
Type::Const(TypeConst::U64Le) |
Type::Const(TypeConst::I16Le) |
Type::Const(TypeConst::I24Le) |
Type::Const(TypeConst::I32Le) |
Type::Const(TypeConst::I64Le) |
Type::Const(TypeConst::F32Le) |
Type::Const(TypeConst::F64Le) |
Type::Const(TypeConst::U16Be) |
Type::Const(TypeConst::U24Be) |
Type::Const(TypeConst::U32Be) |
Type::Const(TypeConst::U64Be) |
Type::Const(TypeConst::I16Be) |
Type::Const(TypeConst::I24Be) |
Type::Const(TypeConst::I32Be) |
Type::Const(TypeConst::I64Be) |
Type::Const(TypeConst::F32Be) |
Type::Const(TypeConst::F64Be) => Ok(Kind::Type),
Type::Const(TypeConst::U16(_)) |
Type::Const(TypeConst::U24(_)) |
Type::Const(TypeConst::U32(_)) |
Type::Const(TypeConst::U64(_)) |
Type::Const(TypeConst::I16(_)) |
Type::Const(TypeConst::I24(_)) |
Type::Const(TypeConst::I32(_)) |
Type::Const(TypeConst::I64(_)) |
Type::Const(TypeConst::F32(_)) |
Type::Const(TypeConst::F64(_)) => Ok(Kind::Type),

// Array types
Type::Array(_, ref elem_ty, ref size_expr) => {
Expand Down
8 changes: 6 additions & 2 deletions tests/snapshots/examples.bitmap_ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ Program {
Named("extents", Sequence(
[
Named("width", Const(
U32Be
U32(
Big
)
)),
Named("height", Const(
U32Be
U32(
Big
)
))
],
Struct(
Expand Down
16 changes: 12 additions & 4 deletions tests/snapshots/examples.edid_ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ Program {
[
Named("magic", Assert(
Const(
U64Le
U64(
Little
)
),
Abs(
[
Expand All @@ -344,13 +346,19 @@ Program {
)
)),
Named("mfg_bytes", Const(
U16Le
U16(
Little
)
)),
Named("product_code", Const(
U16Le
U16(
Little
)
)),
Named("serial", Const(
U32Le
U32(
Little
)
)),
Named("mfg_week", Const(
U8
Expand Down
16 changes: 12 additions & 4 deletions tests/snapshots/examples.object_id_ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,24 @@ Program {
Sequence(
[
Named("epoch_time", Const(
I32Be
I32(
Big
)
)),
Named("machine_id", Const(
U24Be
U24(
Big
)
)),
Named("process_id", Const(
U16Be
U16(
Big
)
)),
Named("counter", Const(
U24Be
U24(
Big
)
))
],
Struct(
Expand Down
Loading

0 comments on commit c2ef3eb

Please sign in to comment.