mech_core/program/compiler/
mod.rs1use crate::*;
2use byteorder::{LittleEndian, WriteBytesExt, ReadBytesExt};
3use std::io::Cursor;
4#[cfg(not(feature = "no_std"))]
5use std::collections::HashSet;
6#[cfg(feature = "no_std")]
7use hashbrown::HashSet;
8
9pub mod sections;
10pub mod constants;
11pub mod context;
12
13pub use self::sections::*;
14pub use self::constants::*;
15pub use self::context::*;
16
17pub type Register = u32;
18
19pub fn encode_value_kind(ts: &mut TypeSection, vk: &ValueKind) -> (TypeTag, Vec<u8>) {
20 let mut b = Vec::new();
21 let tag = match vk {
22 ValueKind::U8 => TypeTag::U8, ValueKind::U16 => TypeTag::U16, ValueKind::U32 => TypeTag::U32,
23 ValueKind::U64 => TypeTag::U64, ValueKind::U128 => TypeTag::U128,
24 ValueKind::I8 => TypeTag::I8, ValueKind::I16 => TypeTag::I16, ValueKind::I32 => TypeTag::I32,
25 ValueKind::I64 => TypeTag::I64, ValueKind::I128 => TypeTag::I128,
26 ValueKind::F32 => TypeTag::F32, ValueKind::F64 => TypeTag::F64,
27 ValueKind::C64 => TypeTag::C64,
28 ValueKind::R64 => TypeTag::R64,
29 ValueKind::String => TypeTag::String,
30 ValueKind::Bool => TypeTag::Bool,
31 ValueKind::Id => TypeTag::Id,
32 ValueKind::Index => TypeTag::Index,
33 ValueKind::Empty => TypeTag::Empty,
34 ValueKind::Any => TypeTag::Any,
35 ValueKind::None => TypeTag::None,
36
37 ValueKind::Matrix(elem, dims) => {
38 let elem_id = ts.get_or_intern(elem);
39 b.write_u32::<LittleEndian>(elem_id).unwrap();
40 b.write_u32::<LittleEndian>(dims.len() as u32).unwrap();
41 for &d in dims { b.write_u32::<LittleEndian>(d as u32).unwrap(); }
42 match &**elem {
43 ValueKind::U8 => TypeTag::MatrixU8,
44 ValueKind::U16 => TypeTag::MatrixU16,
45 ValueKind::U32 => TypeTag::MatrixU32,
46 ValueKind::U64 => TypeTag::MatrixU64,
47 ValueKind::U128 => TypeTag::MatrixU128,
48 ValueKind::I8 => TypeTag::MatrixI8,
49 ValueKind::I16 => TypeTag::MatrixI16,
50 ValueKind::I32 => TypeTag::MatrixI32,
51 ValueKind::I64 => TypeTag::MatrixI64,
52 ValueKind::I128 => TypeTag::MatrixI128,
53 ValueKind::F32 => TypeTag::MatrixF32,
54 ValueKind::F64 => TypeTag::MatrixF64,
55 ValueKind::C64 => TypeTag::MatrixC64,
56 ValueKind::R64 => TypeTag::MatrixR64,
57 ValueKind::String => TypeTag::MatrixString,
58 ValueKind::Bool => TypeTag::MatrixBool,
59 ValueKind::Index => TypeTag::MatrixIndex,
60 _ => panic!("Unsupported matrix element type {:?}", elem),
61 }
62 }
63
64 ValueKind::Enum(space) => {
65 b.write_u64::<LittleEndian>(*space).unwrap();
66 TypeTag::EnumTag
67 }
68
69 ValueKind::Record(fields) => {
70 b.write_u32::<LittleEndian>(fields.len() as u32).unwrap();
71 for (name, ty) in fields {
72 let name_bytes = name.as_bytes();
73 b.write_u32::<LittleEndian>(name_bytes.len() as u32).unwrap();
74 b.extend_from_slice(name_bytes);
75 let tid = ts.get_or_intern(ty);
76 b.write_u32::<LittleEndian>(tid).unwrap();
77 }
78 TypeTag::Record
79 }
80
81 ValueKind::Map(k,v) => {
82 let kid = ts.get_or_intern(k);
83 let vid = ts.get_or_intern(v);
84 b.write_u32::<LittleEndian>(kid).unwrap();
85 b.write_u32::<LittleEndian>(vid).unwrap();
86 TypeTag::Map
87 }
88
89 ValueKind::Atom(id) => {
90 b.write_u64::<LittleEndian>(*id).unwrap();
91 TypeTag::Atom
92 }
93
94 ValueKind::Table(cols, pk_col) => {
95 b.write_u32::<LittleEndian>(cols.len() as u32).unwrap();
96 for (name, ty) in cols {
97 let name_b = name.as_bytes();
98 b.write_u32::<LittleEndian>(name_b.len() as u32).unwrap();
99 b.extend_from_slice(name_b);
100 let tid = ts.get_or_intern(ty);
101 b.write_u32::<LittleEndian>(tid).unwrap();
102 }
103 b.write_u32::<LittleEndian>(*pk_col as u32).unwrap();
104 TypeTag::Table
105 }
106
107 ValueKind::Tuple(elems) => {
108 b.write_u32::<LittleEndian>(elems.len() as u32).unwrap();
109 for t in elems {
110 let tid = ts.get_or_intern(t);
111 b.write_u32::<LittleEndian>(tid).unwrap();
112 }
113 TypeTag::Tuple
114 }
115
116 ValueKind::Reference(inner) => {
117 let id = ts.get_or_intern(inner);
118 b.write_u32::<LittleEndian>(id).unwrap();
119 TypeTag::Reference
120 }
121
122 ValueKind::Set(elem, max) => {
123 let id = ts.get_or_intern(elem);
124 b.write_u32::<LittleEndian>(id).unwrap();
125 match max {
126 Some(m) => { b.push(1); use byteorder::WriteBytesExt; b.write_u32::<LittleEndian>(*m as u32).unwrap(); }
127 None => { b.push(0); }
128 }
129 TypeTag::Set
130 }
131
132 ValueKind::Option(inner) => {
133 let id = ts.get_or_intern(inner);
134 b.write_u32::<LittleEndian>(id).unwrap();
135 TypeTag::OptionT
136 }
137 };
138 (tag, b)
139}