Add schema-driven FIELDS_PUBLISH without collection-specific runtime behavior. Validate every nullable destination and complete field group before allocation-free publication, with neutral ownership, malformed-input and quota coverage.
415 lines
16 KiB
Rust
415 lines
16 KiB
Rust
//! Minimal deterministic assembler for the canonical disassembly format.
|
|
//!
|
|
//! This is intended primarily for roundtrip tests: `bytes -> disassemble -> assemble -> bytes`.
|
|
//! It supports all mnemonics emitted by `disassembler.rs` and their operand formats.
|
|
|
|
use crate::isa::core::CoreOpCode;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum AsmError {
|
|
EmptyLine,
|
|
UnknownMnemonic(String),
|
|
UnexpectedOperand(String),
|
|
MissingOperand(String),
|
|
InvalidOperand(String),
|
|
}
|
|
|
|
fn emit_u16(v: u16, out: &mut Vec<u8>) {
|
|
out.extend_from_slice(&v.to_le_bytes());
|
|
}
|
|
fn emit_u32(v: u32, out: &mut Vec<u8>) {
|
|
out.extend_from_slice(&v.to_le_bytes());
|
|
}
|
|
fn emit_i32(v: i32, out: &mut Vec<u8>) {
|
|
out.extend_from_slice(&v.to_le_bytes());
|
|
}
|
|
fn emit_i64(v: i64, out: &mut Vec<u8>) {
|
|
out.extend_from_slice(&v.to_le_bytes());
|
|
}
|
|
fn emit_f64_bits(bits: u64, out: &mut Vec<u8>) {
|
|
out.extend_from_slice(&bits.to_le_bytes());
|
|
}
|
|
|
|
fn parse_u32_any(s: &str) -> Result<u32, AsmError> {
|
|
let s = s.trim();
|
|
if let Some(rest) = s.strip_prefix("0x") {
|
|
u32::from_str_radix(rest, 16).map_err(|_| AsmError::InvalidOperand(s.into()))
|
|
} else {
|
|
s.parse::<u32>().map_err(|_| AsmError::InvalidOperand(s.into()))
|
|
}
|
|
}
|
|
|
|
fn parse_i32_any(s: &str) -> Result<i32, AsmError> {
|
|
s.trim().parse::<i32>().map_err(|_| AsmError::InvalidOperand(s.into()))
|
|
}
|
|
|
|
fn parse_i64_any(s: &str) -> Result<i64, AsmError> {
|
|
s.trim().parse::<i64>().map_err(|_| AsmError::InvalidOperand(s.into()))
|
|
}
|
|
|
|
fn parse_f64_bits(s: &str) -> Result<u64, AsmError> {
|
|
let s = s.trim();
|
|
let s = s.strip_prefix("f64:").ok_or_else(|| AsmError::InvalidOperand(s.into()))?;
|
|
let hex = s.strip_prefix("0x").ok_or_else(|| AsmError::InvalidOperand(s.into()))?;
|
|
if hex.len() != 16 {
|
|
return Err(AsmError::InvalidOperand(s.into()));
|
|
}
|
|
u64::from_str_radix(hex, 16).map_err(|_| AsmError::InvalidOperand(s.into()))
|
|
}
|
|
|
|
fn parse_keyvals(s: &str) -> Result<(&str, &str), AsmError> {
|
|
// Parses keyed u32 immediates such as "fn=123, type=2" or "type=3, method=1"
|
|
let mut parts = s.split(',');
|
|
let a = parts.next().ok_or_else(|| AsmError::MissingOperand(s.into()))?.trim();
|
|
let b = parts.next().ok_or_else(|| AsmError::MissingOperand(s.into()))?.trim();
|
|
if parts.next().is_some() {
|
|
return Err(AsmError::InvalidOperand(s.into()));
|
|
}
|
|
Ok((a, b))
|
|
}
|
|
|
|
fn parse_pair<'a>(a: &'a str, ka: &str, b: &'a str, kb: &str) -> Result<(u32, u32), AsmError> {
|
|
let (ka_l, va_s) = a.split_once('=').ok_or_else(|| AsmError::InvalidOperand(a.into()))?;
|
|
let (kb_l, vb_s) = b.split_once('=').ok_or_else(|| AsmError::InvalidOperand(b.into()))?;
|
|
if ka_l.trim() != ka || kb_l.trim() != kb {
|
|
return Err(AsmError::InvalidOperand(format!("expected keys {} and {}", ka, kb)));
|
|
}
|
|
let va = parse_u32_any(va_s)?;
|
|
let vb = parse_u32_any(vb_s)?;
|
|
Ok((va, vb))
|
|
}
|
|
|
|
fn parse_mnemonic(line: &str) -> (&str, &str) {
|
|
let line = line.trim();
|
|
if let Some(sp) = line.find(char::is_whitespace) {
|
|
let (mn, rest) = line.split_at(sp);
|
|
(mn, rest.trim())
|
|
} else {
|
|
(line, "")
|
|
}
|
|
}
|
|
|
|
pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
|
let mut out = Vec::new();
|
|
for raw_line in src.lines() {
|
|
let line = raw_line.trim();
|
|
if line.is_empty() {
|
|
continue;
|
|
}
|
|
let (mn, ops) = parse_mnemonic(line);
|
|
match mn {
|
|
// Zero-operand
|
|
"NOP" => {
|
|
emit_u16(CoreOpCode::Nop as u16, &mut out);
|
|
}
|
|
"POP" => emit_u16(CoreOpCode::Pop as u16, &mut out),
|
|
"HALT" => {
|
|
emit_u16(CoreOpCode::Halt as u16, &mut out);
|
|
}
|
|
"TRAP" => {
|
|
emit_u16(CoreOpCode::Trap as u16, &mut out);
|
|
}
|
|
"DUP" => {
|
|
emit_u16(CoreOpCode::Dup as u16, &mut out);
|
|
}
|
|
"SWAP" => {
|
|
emit_u16(CoreOpCode::Swap as u16, &mut out);
|
|
}
|
|
"ADD" => {
|
|
emit_u16(CoreOpCode::Add as u16, &mut out);
|
|
}
|
|
"SUB" => {
|
|
emit_u16(CoreOpCode::Sub as u16, &mut out);
|
|
}
|
|
"MUL" => {
|
|
emit_u16(CoreOpCode::Mul as u16, &mut out);
|
|
}
|
|
"DIV" => {
|
|
emit_u16(CoreOpCode::Div as u16, &mut out);
|
|
}
|
|
"MOD" => {
|
|
emit_u16(CoreOpCode::Mod as u16, &mut out);
|
|
}
|
|
"NEG" => {
|
|
emit_u16(CoreOpCode::Neg as u16, &mut out);
|
|
}
|
|
"EQ" => {
|
|
emit_u16(CoreOpCode::Eq as u16, &mut out);
|
|
}
|
|
"NEQ" => {
|
|
emit_u16(CoreOpCode::Neq as u16, &mut out);
|
|
}
|
|
"LT" => {
|
|
emit_u16(CoreOpCode::Lt as u16, &mut out);
|
|
}
|
|
"LTE" => {
|
|
emit_u16(CoreOpCode::Lte as u16, &mut out);
|
|
}
|
|
"GT" => {
|
|
emit_u16(CoreOpCode::Gt as u16, &mut out);
|
|
}
|
|
"GTE" => {
|
|
emit_u16(CoreOpCode::Gte as u16, &mut out);
|
|
}
|
|
"AND" => {
|
|
emit_u16(CoreOpCode::And as u16, &mut out);
|
|
}
|
|
"OR" => {
|
|
emit_u16(CoreOpCode::Or as u16, &mut out);
|
|
}
|
|
"NOT" => {
|
|
emit_u16(CoreOpCode::Not as u16, &mut out);
|
|
}
|
|
"BIT_AND" => {
|
|
emit_u16(CoreOpCode::BitAnd as u16, &mut out);
|
|
}
|
|
"BIT_OR" => {
|
|
emit_u16(CoreOpCode::BitOr as u16, &mut out);
|
|
}
|
|
"BIT_XOR" => {
|
|
emit_u16(CoreOpCode::BitXor as u16, &mut out);
|
|
}
|
|
"SHL" => {
|
|
emit_u16(CoreOpCode::Shl as u16, &mut out);
|
|
}
|
|
"SHR" => {
|
|
emit_u16(CoreOpCode::Shr as u16, &mut out);
|
|
}
|
|
"RET" => {
|
|
emit_u16(CoreOpCode::Ret as u16, &mut out);
|
|
}
|
|
"YIELD" | "SLEEP" | "SPAWN" => {
|
|
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()));
|
|
}
|
|
"FRAME_SYNC" => {
|
|
emit_u16(CoreOpCode::FrameSync as u16, &mut out);
|
|
}
|
|
|
|
// One u32 immediate (decimal or hex accepted; SYSCALL/HOSTCALL commonly use hex/idx)
|
|
"JMP" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::Jmp as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"JMP_IF_FALSE" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::JmpIfFalse as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"JMP_IF_TRUE" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::JmpIfTrue as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"PUSH_CONST" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::PushConst as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"PUSH_I64" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::PushI64 as u16, &mut out);
|
|
emit_i64(parse_i64_any(ops)?, &mut out);
|
|
}
|
|
"PUSH_F64" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::PushF64 as u16, &mut out);
|
|
emit_f64_bits(parse_f64_bits(ops)?, &mut out);
|
|
}
|
|
"PUSH_BOOL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
let v = parse_u32_any(ops)? as u8;
|
|
emit_u16(CoreOpCode::PushBool as u16, &mut out);
|
|
out.push(v);
|
|
}
|
|
"PUSH_I32" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::PushI32 as u16, &mut out);
|
|
emit_i32(parse_i32_any(ops)?, &mut out);
|
|
}
|
|
"POP_N" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::PopN as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"GET_GLOBAL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::GetGlobal as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"SET_GLOBAL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::SetGlobal as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"GET_LOCAL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::GetLocal as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"SET_LOCAL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::SetLocal as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"CALL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::Call as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"CALL_CLOSURE" | "GET_CAPTURE" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
let opcode = if mn == "CALL_CLOSURE" {
|
|
CoreOpCode::CallClosure
|
|
} else {
|
|
CoreOpCode::GetCapture
|
|
};
|
|
emit_u16(opcode as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"MAKE_CLOSURE" | "CALL_TRAIT" => {
|
|
let (a, b) = parse_keyvals(ops)?;
|
|
let (key_a, key_b, opcode) = if mn == "MAKE_CLOSURE" {
|
|
("fn", "type", CoreOpCode::MakeClosure)
|
|
} else {
|
|
("type", "method", CoreOpCode::CallTrait)
|
|
};
|
|
let (first, second) = if a.starts_with(&format!("{}=", key_a)) {
|
|
parse_pair(a, key_a, b, key_b)?
|
|
} else {
|
|
let (second, first) = parse_pair(a, key_b, b, key_a)?;
|
|
(first, second)
|
|
};
|
|
emit_u16(opcode as u16, &mut out);
|
|
emit_u32(first, &mut out);
|
|
emit_u32(second, &mut out);
|
|
}
|
|
"HOSTCALL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::Hostcall as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"SYSCALL" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::Syscall as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"INTRINSIC" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::Intrinsic as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
|
|
"INDEX_GUARD" => {
|
|
if !ops.is_empty() {
|
|
return Err(AsmError::InvalidOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::IndexGuard as u16, &mut out);
|
|
}
|
|
"STORAGE_PUBLISH" => {
|
|
let parts: Vec<_> = ops.split(',').map(str::trim).collect();
|
|
if parts.len() != 4 {
|
|
return Err(AsmError::InvalidOperand(line.into()));
|
|
}
|
|
emit_u16(CoreOpCode::StoragePublish as u16, &mut out);
|
|
for part in parts {
|
|
emit_u32(parse_u32_any(part)?, &mut out);
|
|
}
|
|
}
|
|
"STRUCT_GET" | "STRUCT_SET" => {
|
|
let (a, b) = parse_keyvals(ops)?;
|
|
let (ty, field) = parse_pair(a, "type", b, "field")?;
|
|
let op =
|
|
if mn == "STRUCT_GET" { CoreOpCode::StructGet } else { CoreOpCode::StructSet };
|
|
emit_u16(op as u16, &mut out);
|
|
emit_u32(ty, &mut out);
|
|
emit_u32(field, &mut out);
|
|
}
|
|
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET"
|
|
| "ARRAY_CHECK_LENGTH" | "ARRAY_COPY" | "FIELDS_PUBLISH" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
let op = match mn {
|
|
"FIELDS_PUBLISH" => CoreOpCode::FieldsPublish,
|
|
"STRUCT_NEW" => CoreOpCode::StructNew,
|
|
"ARRAY_NEW" => CoreOpCode::ArrayNew,
|
|
"ARRAY_LEN" => CoreOpCode::ArrayLen,
|
|
"ARRAY_GET" => CoreOpCode::ArrayGet,
|
|
"ARRAY_CHECK_LENGTH" => CoreOpCode::ArrayCheckLength,
|
|
"ARRAY_COPY" => CoreOpCode::ArrayCopy,
|
|
_ => CoreOpCode::ArraySet,
|
|
};
|
|
emit_u16(op as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
let opcode = match mn {
|
|
"DYN_PACK" => CoreOpCode::DynPack,
|
|
"DYN_UNPACK" => CoreOpCode::DynUnpack,
|
|
_ => CoreOpCode::DynCanUnpack,
|
|
};
|
|
emit_u16(opcode as u16, &mut out);
|
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
|
}
|
|
"REF_IS_EXACT" | "REF_IMPLEMENTS" | "REF_TO_TRAIT" | "REF_CAST" => {
|
|
if ops.is_empty() {
|
|
return Err(AsmError::MissingOperand(line.into()));
|
|
}
|
|
let (a, b) = parse_keyvals(ops)?;
|
|
let (source, target) = parse_pair(a, "source", b, "target")?;
|
|
let opcode = match mn {
|
|
"REF_IS_EXACT" => CoreOpCode::RefIsExact,
|
|
"REF_IMPLEMENTS" => CoreOpCode::RefImplements,
|
|
"REF_TO_TRAIT" => CoreOpCode::RefToTrait,
|
|
_ => CoreOpCode::RefCast,
|
|
};
|
|
emit_u16(opcode as u16, &mut out);
|
|
emit_u32(source, &mut out);
|
|
emit_u32(target, &mut out);
|
|
}
|
|
other => return Err(AsmError::UnknownMnemonic(other.into())),
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|