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
36 ValueKind::Matrix(elem, dims) => {
37 let elem_id = ts.get_or_intern(elem);
38 b.write_u32::<LittleEndian>(elem_id).unwrap();
39 b.write_u32::<LittleEndian>(dims.len() as u32).unwrap();
40 for &d in dims { b.write_u32::<LittleEndian>(d as u32).unwrap(); }
41 match &**elem {
42 ValueKind::U8 => TypeTag::MatrixU8,
43 ValueKind::U16 => TypeTag::MatrixU16,
44 ValueKind::U32 => TypeTag::MatrixU32,
45 ValueKind::U64 => TypeTag::MatrixU64,
46 ValueKind::U128 => TypeTag::MatrixU128,
47 ValueKind::I8 => TypeTag::MatrixI8,
48 ValueKind::I16 => TypeTag::MatrixI16,
49 ValueKind::I32 => TypeTag::MatrixI32,
50 ValueKind::I64 => TypeTag::MatrixI64,
51 ValueKind::I128 => TypeTag::MatrixI128,
52 ValueKind::F32 => TypeTag::MatrixF32,
53 ValueKind::F64 => TypeTag::MatrixF64,
54 ValueKind::C64 => TypeTag::MatrixC64,
55 ValueKind::R64 => TypeTag::MatrixR64,
56 ValueKind::String => TypeTag::MatrixString,
57 ValueKind::Bool => TypeTag::MatrixBool,
58 ValueKind::Index => TypeTag::MatrixIndex,
59 _ => panic!("Unsupported matrix element type {:?}", elem),
60 }
61 }
62
63 ValueKind::Enum(space) => {
64 b.write_u64::<LittleEndian>(*space).unwrap();
65 TypeTag::EnumTag
66 }
67
68 ValueKind::Record(fields) => {
69 b.write_u32::<LittleEndian>(fields.len() as u32).unwrap();
70 for (name, ty) in fields {
71 let name_bytes = name.as_bytes();
72 b.write_u32::<LittleEndian>(name_bytes.len() as u32).unwrap();
73 b.extend_from_slice(name_bytes);
74 let tid = ts.get_or_intern(ty);
75 b.write_u32::<LittleEndian>(tid).unwrap();
76 }
77 TypeTag::Record
78 }
79
80 ValueKind::Map(k,v) => {
81 let kid = ts.get_or_intern(k);
82 let vid = ts.get_or_intern(v);
83 b.write_u32::<LittleEndian>(kid).unwrap();
84 b.write_u32::<LittleEndian>(vid).unwrap();
85 TypeTag::Map
86 }
87
88 ValueKind::Atom(id) => {
89 b.write_u64::<LittleEndian>(*id).unwrap();
90 TypeTag::Atom
91 }
92
93 ValueKind::Table(cols, pk_col) => {
94 b.write_u32::<LittleEndian>(cols.len() as u32).unwrap();
95 for (name, ty) in cols {
96 let name_b = name.as_bytes();
97 b.write_u32::<LittleEndian>(name_b.len() as u32).unwrap();
98 b.extend_from_slice(name_b);
99 let tid = ts.get_or_intern(ty);
100 b.write_u32::<LittleEndian>(tid).unwrap();
101 }
102 b.write_u32::<LittleEndian>(*pk_col as u32).unwrap();
103 TypeTag::Table
104 }
105
106 ValueKind::Tuple(elems) => {
107 b.write_u32::<LittleEndian>(elems.len() as u32).unwrap();
108 for t in elems {
109 let tid = ts.get_or_intern(t);
110 b.write_u32::<LittleEndian>(tid).unwrap();
111 }
112 TypeTag::Tuple
113 }
114
115 ValueKind::Reference(inner) => {
116 let id = ts.get_or_intern(inner);
117 b.write_u32::<LittleEndian>(id).unwrap();
118 TypeTag::Reference
119 }
120
121 ValueKind::Set(elem, max) => {
122 let id = ts.get_or_intern(elem);
123 b.write_u32::<LittleEndian>(id).unwrap();
124 match max {
125 Some(m) => { b.push(1); use byteorder::WriteBytesExt; b.write_u32::<LittleEndian>(*m as u32).unwrap(); }
126 None => { b.push(0); }
127 }
128 TypeTag::Set
129 }
130
131 ValueKind::Option(inner) => {
132 let id = ts.get_or_intern(inner);
133 b.write_u32::<LittleEndian>(id).unwrap();
134 TypeTag::OptionT
135 }
136 };
137 (tag, b)
138}