use crate::parse::ast::Type;
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone)]
pub struct TackyProgram {
pub functions: Vec<TackyFunction>,
pub static_vars: Vec<TackyStaticVar>,
pub static_constants: Vec<TackyStaticConstant>,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct TackyFunction {
pub name: String,
pub global: bool,
pub params: Vec<String>,
pub body: Vec<TackyInstruction>,
pub return_type: Type,
pub var_types: HashMap<String, Type>,
pub is_variadic: bool,
pub static_var_names: HashSet<String>,
pub has_sret: bool,
}
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum TackyInstruction {
Return(TackyVal),
ReturnVoid,
Unary {
op: TackyUnaryOp,
src: TackyVal,
dst: TackyVal,
},
Binary {
op: TackyBinaryOp,
left: TackyVal,
right: TackyVal,
dst: TackyVal,
},
Copy { src: TackyVal, dst: TackyVal },
Jump(String),
JumpIfZero { condition: TackyVal, target: String },
JumpIfNotZero { condition: TackyVal, target: String },
Label(String),
FunCall {
name: String,
args: Vec<TackyVal>,
dst: TackyVal,
dst_type: Type,
is_variadic: bool,
},
SignExtend { src: TackyVal, dst: TackyVal },
ZeroExtend { src: TackyVal, dst: TackyVal },
Truncate { src: TackyVal, dst: TackyVal },
IntToDouble { src: TackyVal, dst: TackyVal },
DoubleToInt { src: TackyVal, dst: TackyVal },
UIntToDouble { src: TackyVal, dst: TackyVal },
DoubleToUInt { src: TackyVal, dst: TackyVal },
FloatToDouble { src: TackyVal, dst: TackyVal },
DoubleToFloat { src: TackyVal, dst: TackyVal },
IntToFloat { src: TackyVal, dst: TackyVal },
FloatToInt { src: TackyVal, dst: TackyVal },
UIntToFloat { src: TackyVal, dst: TackyVal },
FloatToUInt { src: TackyVal, dst: TackyVal },
GetAddress { src: TackyVal, dst: TackyVal },
Load { src_ptr: TackyVal, dst: TackyVal },
Store { src: TackyVal, dst_ptr: TackyVal },
AddPtr {
ptr: TackyVal,
index: TackyVal,
scale: usize,
dst: TackyVal,
},
CopyToOffset {
src: TackyVal,
dst: String,
offset: usize,
},
CopyFromOffset {
src: String,
offset: usize,
dst: TackyVal,
},
CopyStruct {
src: TackyVal,
dst: TackyVal,
size: usize,
},
JumpIndirect {
target: TackyVal,
possible_targets: Vec<String>,
},
VaStart {
ap: TackyVal,
gp_offset_init: i32,
fp_offset_init: i32,
},
VaArg {
ap: TackyVal,
dst: TackyVal,
arg_type: Type,
},
VaEnd,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TackyVal {
Constant(TackyConst),
Var(String),
}
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum TackyConst {
Int(i32),
Long(i64),
UInt(u32),
ULong(u64),
Float(f32),
Double(f64),
Char(i8),
UChar(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TackyUnaryOp {
Complement,
Negate,
Not,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TackyBinaryOp {
Add,
Subtract,
Multiply,
Divide,
Remainder,
Equal,
NotEqual,
LessThan,
LessOrEqual,
GreaterThan,
GreaterOrEqual,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
ShiftLeft,
ShiftRight,
AddDouble,
SubDouble,
MulDouble,
DivDouble,
AddFloat,
SubFloat,
MulFloat,
DivFloat,
}
#[derive(Debug, Clone)]
pub struct TackyStaticVar {
pub name: String,
pub global: bool,
pub var_type: Type,
pub init: TackyStaticInit,
}
#[derive(Debug, Clone, PartialEq)]
#[allow(clippy::enum_variant_names)]
pub enum TackyStaticInit {
IntInit(i64),
FloatInit(f32),
DoubleInit(f64),
ZeroInit(usize),
StringInit(String, usize),
ByteArrayInit(Vec<u8>),
PointerArrayInit(Vec<String>),
ArrayInit(Vec<TackyStaticInit>),
}
#[derive(Debug, Clone)]
pub struct TackyStaticConstant {
pub name: String,
pub alignment: usize,
pub init: TackyStaticInit,
}