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 impl_type_param_count: u32,
pub type_params: Vec<Symbol>,
pub type_param_required: Vec<bool>,
pub param_type_params: Vec<Option<u32>>,
pub param_names: Vec<Option<Symbol>>,
pub param_types: Vec<Option<RtType>>,
pub ret_type: Option<RtType>,
pub decorators: Vec<Decorator>,
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>),
Bytes(Arc<[u8]>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DefRef {
Fn(DefId),
Ctor(DefId),
Const(DefId),
Module(DefId),
Native(NativeFnId),
}
pub use xur_resolve::RtType;
#[derive(Debug, Clone)]
pub struct Decorator {
pub def: DefRef,
pub args: Vec<Constant>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecordFields {
Positional(Vec<Reg>),
Named(Vec<(Symbol, Reg)>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeArg {
Named { name: String, def: Option<DefId> },
Param(u32),
Record,
}
#[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,
},
SetField {
dst: Reg,
base: Reg,
field: Symbol,
value: Reg,
},
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>,
},
Map {
dst: Reg,
entries: 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,
},
RecordLit {
dst: Reg,
fields: Vec<(Symbol, Reg)>,
},
Record {
dst: Reg,
ty: NativeTypeId,
variant: Option<Symbol>,
fields: RecordFields,
},
IsRecord {
dst: Reg,
value: Reg,
ty: NativeTypeId,
variant: Option<Symbol>,
},
RecordArity {
dst: Reg,
value: Reg,
len: u32,
},
IsTuple {
dst: Reg,
value: Reg,
len: u32,
},
IsList {
dst: Reg,
value: Reg,
len: u32,
at_least: bool,
},
Project {
dst: Reg,
value: Reg,
index: u32,
},
CheckType {
value: Reg,
ty: RtType,
coerce: bool,
},
IterStart {
dst: Reg,
iterable: Reg,
},
TyParamCall {
dst: Reg,
type_param: u32,
method: Symbol,
args: Vec<Reg>,
},
TypeLiteral {
dst: Reg,
ty: RtType,
},
Deref {
dst: Reg,
ptr: Reg,
},
IsTryUnwrap {
dst: Reg,
inner: Reg,
value: Reg,
},
Error {
message: String,
},
}
#[derive(Debug)]
pub enum Terminator {
Goto(BlockId),
Branch {
cond: Reg,
then: BlockId,
otherwise: BlockId,
},
Return(Reg),
TryReturn(Reg),
Next {
iter: Reg,
item: Reg,
body: BlockId,
done: BlockId,
},
NoMatch,
Unreachable,
}