Skip to main content

yulang_native/
control_ir.rs

1use yulang_typed_ir as typed_ir;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct NativeModule {
5    pub functions: Vec<NativeFunction>,
6    pub roots: Vec<NativeFunction>,
7}
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct NativeFunction {
11    pub name: String,
12    pub captures: Vec<ValueId>,
13    pub params: Vec<ValueId>,
14    pub blocks: Vec<NativeBlock>,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct NativeBlock {
19    pub id: BlockId,
20    pub params: Vec<ValueId>,
21    pub stmts: Vec<NativeStmt>,
22    pub terminator: NativeTerminator,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub enum NativeStmt {
27    Literal {
28        dest: ValueId,
29        literal: NativeLiteral,
30    },
31    Primitive {
32        dest: ValueId,
33        op: typed_ir::PrimitiveOp,
34        args: Vec<ValueId>,
35    },
36    DirectCall {
37        dest: ValueId,
38        target: String,
39        args: Vec<ValueId>,
40    },
41    Tuple {
42        dest: ValueId,
43        items: Vec<ValueId>,
44    },
45    Record {
46        dest: ValueId,
47        base: Option<ValueId>,
48        fields: Vec<NativeRecordField>,
49    },
50    RecordWithoutFields {
51        dest: ValueId,
52        base: ValueId,
53        fields: Vec<typed_ir::Name>,
54    },
55    Variant {
56        dest: ValueId,
57        tag: typed_ir::Name,
58        value: Option<ValueId>,
59    },
60    Select {
61        dest: ValueId,
62        base: ValueId,
63        field: typed_ir::Name,
64    },
65    TupleGet {
66        dest: ValueId,
67        tuple: ValueId,
68        index: usize,
69    },
70    VariantTagEq {
71        dest: ValueId,
72        variant: ValueId,
73        tag: typed_ir::Name,
74    },
75    VariantPayload {
76        dest: ValueId,
77        variant: ValueId,
78    },
79    ValueEq {
80        dest: ValueId,
81        left: ValueId,
82        right: ValueId,
83    },
84    BoolAnd {
85        dest: ValueId,
86        left: ValueId,
87        right: ValueId,
88    },
89    MakeClosure {
90        dest: ValueId,
91        target: String,
92        captures: Vec<ValueId>,
93    },
94    ClosureCall {
95        dest: ValueId,
96        callee: ValueId,
97        args: Vec<ValueId>,
98    },
99}
100
101#[derive(Debug, Clone, PartialEq, Eq)]
102pub struct NativeRecordField {
103    pub name: typed_ir::Name,
104    pub value: ValueId,
105}
106
107#[derive(Debug, Clone, PartialEq, Eq)]
108pub enum NativeTerminator {
109    Return(ValueId),
110    Jump {
111        target: BlockId,
112        args: Vec<ValueId>,
113    },
114    Branch {
115        cond: ValueId,
116        then_block: BlockId,
117        else_block: BlockId,
118    },
119}
120
121#[derive(Debug, Clone, PartialEq, Eq)]
122pub enum NativeLiteral {
123    Int(String),
124    Float(String),
125    String(String),
126    Bool(bool),
127    Unit,
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
131pub struct ValueId(pub usize);
132
133#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
134pub struct BlockId(pub usize);