diff --git a/src/codegen.rs b/src/codegen.rs index 47bd3aaed..4469f8cd2 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -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)", }) } diff --git a/src/ir/ast.rs b/src/ir/ast.rs index f9e95594e..70c77ea5b 100644 --- a/src/ir/ast.rs +++ b/src/ir/ast.rs @@ -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)] diff --git a/src/syntax/ast/binary.rs b/src/syntax/ast/binary.rs index 6bff18b7b..95aa25b19 100644 --- a/src/syntax/ast/binary.rs +++ b/src/syntax/ast/binary.rs @@ -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 @@ -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, @@ -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 { @@ -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), } } } diff --git a/src/syntax/ast/host.rs b/src/syntax/ast/host.rs index ef1e342ac..17ccaefa9 100644 --- a/src/syntax/ast/host.rs +++ b/src/syntax/ast/host.rs @@ -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 @@ -266,9 +267,9 @@ impl Expr { #[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, } diff --git a/src/syntax/ast/mod.rs b/src/syntax/ast/mod.rs index cc7f815ff..b29d6e190 100644 --- a/src/syntax/ast/mod.rs +++ b/src/syntax/ast/mod.rs @@ -120,7 +120,7 @@ impl Program { } pub fn base_defs From<&'a str>>() -> Substitutions { - use syntax::ast::binary::{Type, TypeConst}; + use syntax::ast::binary::{Endianness, Type, TypeConst}; btreemap! { // TODO: "true" = Expr::bool(true) @@ -129,26 +129,26 @@ pub fn base_defs From<&'a str>>() -> Substitutions { "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)), } } diff --git a/src/syntax/check/mod.rs b/src/syntax/check/mod.rs index eb7412ace..90c78c68e 100644 --- a/src/syntax/check/mod.rs +++ b/src/syntax/check/mod.rs @@ -389,26 +389,16 @@ pub fn kind_of( 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) => { diff --git a/tests/snapshots/examples.bitmap_ir.snap b/tests/snapshots/examples.bitmap_ir.snap index 82114c519..02a361319 100644 --- a/tests/snapshots/examples.bitmap_ir.snap +++ b/tests/snapshots/examples.bitmap_ir.snap @@ -37,10 +37,14 @@ Program { Named("extents", Sequence( [ Named("width", Const( - U32Be + U32( + Big + ) )), Named("height", Const( - U32Be + U32( + Big + ) )) ], Struct( diff --git a/tests/snapshots/examples.edid_ir.snap b/tests/snapshots/examples.edid_ir.snap index e84342ca9..267dda4ec 100644 --- a/tests/snapshots/examples.edid_ir.snap +++ b/tests/snapshots/examples.edid_ir.snap @@ -322,7 +322,9 @@ Program { [ Named("magic", Assert( Const( - U64Le + U64( + Little + ) ), Abs( [ @@ -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 diff --git a/tests/snapshots/examples.object_id_ir.snap b/tests/snapshots/examples.object_id_ir.snap index 1fddda295..fa6fcf768 100644 --- a/tests/snapshots/examples.object_id_ir.snap +++ b/tests/snapshots/examples.object_id_ir.snap @@ -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( diff --git a/tests/snapshots/examples.stl_ir.snap b/tests/snapshots/examples.stl_ir.snap index 8c47a8847..189294bb4 100644 --- a/tests/snapshots/examples.stl_ir.snap +++ b/tests/snapshots/examples.stl_ir.snap @@ -53,7 +53,9 @@ Program { ) )), Named("num_triangles", Const( - U32Le + U32( + Little + ) )), Named("triangles", Repeat( Var( @@ -153,7 +155,9 @@ Program { ) )), Named("attribute_bytes", Const( - U16Le + U16( + Little + ) )) ], Struct( @@ -226,13 +230,19 @@ Program { Sequence( [ Named("x", Const( - F32Le + F32( + Little + ) )), Named("y", Const( - F32Le + F32( + Little + ) )), Named("z", Const( - F32Le + F32( + Little + ) )) ], Struct(