use std::fmt;
use std::sync::Arc;
use xur_ast::{BinOpKind, UnOp};
use xur_resolve::{DefId, ModuleIdx, NativeFnId, NativeTypeId};
use xur_span::{Span, Symbol};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Reg(pub u32);
impl fmt::Debug for Reg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "r{}", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlockId(pub u32);
impl fmt::Debug for BlockId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "bb{}", self.0)
}
}
#[derive(Debug)]
pub struct Function {
pub name: String,
pub module: ModuleIdx,
pub captures: u32,
pub params: u32,
pub registers: u32,
pub type_params: Vec<Symbol>,
pub blocks: Vec<Block>,
pub closures: Vec<Arc<Function>>,
pub span: Span,
}
#[derive(Debug)]
pub struct Block {
pub insts: Vec<Inst>,
pub term: Terminator,
pub term_span: Span,
}
#[derive(Debug)]
pub struct Inst {
pub kind: InstKind,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Constant {
Unit,
Bool(bool),
Int(i64),
Float(f64),
Str(Arc<str>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DefRef {
Fn(DefId),
Ctor(DefId),
Const(DefId),
Module(DefId),
Native(NativeFnId),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RtType {
Any,
Unit,
Bool,
Str,
Float,
Int {
name: &'static str,
min: i64,
max: i64,
},
Tuple(Vec<RtType>),
List,
Adt {
def: DefId,
name: String,
},
Native {
id: NativeTypeId,
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeArg {
Named(String),
Param(u32),
}
#[derive(Debug)]
pub enum InstKind {
Const {
dst: Reg,
value: Constant,
},
Copy {
dst: Reg,
src: Reg,
},
Def {
dst: Reg,
def: DefRef,
},
Unary {
dst: Reg,
op: UnOp,
src: Reg,
},
Binary {
dst: Reg,
op: BinOpKind,
lhs: Reg,
rhs: Reg,
},
Call {
dst: Reg,
callee: Reg,
args: Vec<Reg>,
type_args: Vec<TypeArg>,
},
MethodCall {
dst: Reg,
receiver: Reg,
method: Symbol,
args: Vec<Reg>,
},
Field {
dst: Reg,
base: Reg,
field: Symbol,
},
Index {
dst: Reg,
base: Reg,
index: Reg,
},
SetIndex {
base: Reg,
index: Reg,
value: Reg,
},
Tuple {
dst: Reg,
items: Vec<Reg>,
},
List {
dst: Reg,
items: Vec<Reg>,
},
Adt {
dst: Reg,
ctor: DefId,
fields: Vec<Reg>,
},
Range {
dst: Reg,
start: Option<Reg>,
end: Option<Reg>,
inclusive: bool,
},
Closure {
dst: Reg,
function: u32,
captures: Vec<Reg>,
},
IsCtor {
dst: Reg,
value: Reg,
ctor: DefId,
},
IsTuple {
dst: Reg,
value: Reg,
len: u32,
},
Project {
dst: Reg,
value: Reg,
index: u32,
},
CheckType {
value: Reg,
ty: RtType,
},
IterStart {
dst: Reg,
iterable: Reg,
},
Error {
message: String,
},
}
#[derive(Debug)]
pub enum Terminator {
Goto(BlockId),
Branch {
cond: Reg,
then: BlockId,
otherwise: BlockId,
},
Return(Reg),
Next {
iter: Reg,
item: Reg,
body: BlockId,
done: BlockId,
},
NoMatch,
Unreachable,
}
impl std::fmt::Display for RtType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Any => f.write_str("_"),
Self::Unit => f.write_str("()"),
Self::Bool => f.write_str("bool"),
Self::Str => f.write_str("str"),
Self::Float => f.write_str("float"),
Self::Int { name, .. } => f.write_str(name),
Self::Tuple(tys) => {
let tys: Vec<_> = tys.iter().map(ToString::to_string).collect();
write!(f, "({})", tys.join(", "))
}
Self::List => f.write_str("[_]"),
Self::Adt { name, .. } | Self::Native { name, .. } => f.write_str(name),
}
}
}