Created
June 30, 2023 21:07
-
-
Save yuanweixin/457be1af286f7c90f80bb106ac9842ef to your computer and use it in GitHub Desktop.
Showing the size of types in Rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem::size_of; | |
type TigerInt = i32; | |
type Label = usize; | |
type Temp = usize; | |
#[derive(Debug)] | |
pub enum IrExp { | |
Const(TigerInt), | |
Name(Label), | |
Temp(Temp), | |
Binop(IrBinop, Box<IrExp>, Box<IrExp>), | |
Mem(Box<IrExp>), | |
Call(Box<IrExp>, Vec<IrExp>), | |
Eseq(Box<IrStm>, Box<IrExp>), | |
} | |
#[derive(Debug)] | |
pub enum IrStm { | |
Move(IrExp, IrExp), | |
Exp(IrExp), | |
Jump(IrExp, Vec<Label>), | |
Cjump(IrRelop, IrExp, IrExp, Label, Label), | |
Seq(Box<IrStm>, Box<IrStm>), | |
Label(Label), | |
} | |
#[derive(Debug)] | |
pub enum IrBinop { | |
Plus, | |
Minus, | |
Mul, | |
Div, | |
And, | |
Or, | |
Lshift, | |
Rshift, | |
ArShift, | |
Xor, | |
} | |
#[derive(Debug)] | |
pub enum IrRelop { | |
Eq, | |
Ne, | |
Lt, | |
Gt, | |
Le, | |
Ge, | |
Ult, | |
Ule, | |
Ugt, | |
Uge, | |
} | |
macro_rules! show_size { | |
(header) => ( | |
println!("{:<28} {:>4} {}", "Type", "T", "Option<T>"); | |
); | |
($t:ty) => ( | |
println!("{:<28} {:4} {:4}", stringify!($t), size_of::<$t>(), size_of::<Option<$t>>()) | |
) | |
} | |
fn main() { | |
show_size!(header); | |
show_size!(IrExp); | |
show_size!(IrStm); | |
show_size!(IrRelop); | |
show_size!(Option<bool>); | |
show_size!(Option<Option<bool>>); | |
show_size!(Option<Option<Option<bool>>>); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment