mirascript_vm/error/
compile.rs1pub use mirascript_core::DiagnosticCode;
2use mirascript_core::OpCode;
3use thiserror::Error;
4
5use super::MiraError;
6
7#[derive(Debug, Error)]
9#[error("{start_line}:{start_col}-{end_line}:{end_col} {code:?}")]
10pub struct Diagnostics {
11 pub code: DiagnosticCode,
13 pub start_line: u32,
15 pub start_col: u32,
17 pub end_line: u32,
19 pub end_col: u32,
21}
22
23#[derive(Debug, Error)]
25pub enum InvalidBytecodeReason {
26 #[error("chunk header is truncated")]
28 BadChunkHeader,
29 #[error("chunk length header is {0}, expected {1}")]
31 ChunkLengthMismatch(usize, usize),
32 #[error("code section exceeds chunk")]
34 CodeSectionExceedsChunk,
35 #[error("constant section length exceeds chunk")]
37 ConstantSectionExceedsChunk,
38 #[error("truncated constant")]
40 TruncatedConstant,
41 #[error("invalid UTF-8 string constant")]
43 InvalidStringConstant,
44 #[error("unknown constant tag {0}")]
46 UnknownConstantTag(u8),
47 #[error("root instruction must be Func")]
49 BadRootStart,
50 #[error("trailing code after root FuncEnd")]
52 BadRootEnd,
53 #[error("root function destination must be register 0")]
55 BadRootDestination,
56 #[error("unknown opcode 0x{0:02x}")]
58 InvalidOpCode(u8),
59 #[error("unexpected end of code while reading opcode")]
61 TruncatedOpCode,
62 #[error("truncated op parameter at code offset {0}")]
64 TruncatedParameter(usize),
65 #[error("register index {0} is out of range 0..={1}")]
67 RegisterIndexOutOfRange(usize, usize),
68 #[error("constant index {0} is out of range 0..{1}")]
70 ConstantIndexOutOfRange(usize, usize),
71 #[error("invalid constant type for instruction")]
73 InvalidConstantType,
74 #[error("function has {0} arguments but only {1} registers")]
76 FunctionArgCountMismatch(usize, usize),
77 #[error("unterminated bytecode block")]
79 UnterminatedBlock,
80 #[error("cannot use wide encoding opcode here")]
82 UnsupportedWide,
83 #[error("unexpected opcode {0}")]
85 UnexpectedOpCode(OpCode),
86 #[error("duplicate export key {0:?}")]
88 DuplicateExportKey(String),
89 #[error("invalid upvalue level {0}")]
91 InvalidUpvalueLevel(usize),
92 #[error("spread argument index {0} is out of range 0..{1}")]
94 SpreadArgumentIndexOutOfRange(usize, usize),
95}
96
97fn decode_diagnostics(diagnostics: &[u32]) -> Vec<Diagnostics> {
98 diagnostics
99 .chunks(5)
100 .map(|chunk| {
101 let start_line = chunk[0] as usize;
102 let start_col = chunk[1] as usize;
103 let end_line = chunk[2] as usize;
104 let end_col = chunk[3] as usize;
105 let code =
106 DiagnosticCode::from_code(chunk[4] as u16).unwrap_or(DiagnosticCode::Unknown);
107 Diagnostics {
108 code,
109 start_line: start_line as u32,
110 start_col: start_col as u32,
111 end_line: end_line as u32,
112 end_col: end_col as u32,
113 }
114 })
115 .collect()
116}
117
118impl MiraError {
119 pub(crate) fn invalid_bytecode(offset: usize, reason: InvalidBytecodeReason) -> Box<Self> {
120 Self::InvalidBytecode { offset, reason }.into()
121 }
122
123 pub(crate) fn compile(diagnostics: &[u32]) -> Box<Self> {
124 Self::Compile {
125 diagnostics: decode_diagnostics(diagnostics),
126 }
127 .into()
128 }
129}