1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::code::{CodeGenerator, ExpressionAllocator};

use super::GenerationError;
use ast::{ExpressionId, NameId, Statement};
use claw_ast as ast;
use claw_resolver::ItemId;

use cranelift_entity::EntityRef;
use wasm_encoder as enc;
use wasm_encoder::Instruction;

pub trait EncodeStatement {
    fn alloc_expr_locals(&self, allocator: &mut ExpressionAllocator)
        -> Result<(), GenerationError>;

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError>;
}

impl EncodeStatement for Statement {
    fn alloc_expr_locals(
        &self,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        let statement: &dyn EncodeStatement = match self {
            Statement::Let(statement) => statement,
            Statement::Assign(statement) => statement,
            Statement::Call(statement) => statement,
            Statement::If(statement) => statement,
            Statement::Return(statement) => statement,
        };
        statement.alloc_expr_locals(allocator)
    }

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError> {
        let statement: &dyn EncodeStatement = match self {
            Statement::Let(statement) => statement,
            Statement::Assign(statement) => statement,
            Statement::Call(statement) => statement,
            Statement::If(statement) => statement,
            Statement::Return(statement) => statement,
        };
        statement.encode(code_gen)
    }
}

impl EncodeStatement for ast::Let {
    fn alloc_expr_locals(
        &self,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc_child(self.expression)
    }

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError> {
        encode_assignment(self.ident, self.expression, code_gen)
    }
}

impl EncodeStatement for ast::Assign {
    fn alloc_expr_locals(
        &self,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc_child(self.expression)
    }

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError> {
        encode_assignment(self.ident, self.expression, code_gen)
    }
}

impl EncodeStatement for ast::Call {
    fn alloc_expr_locals(
        &self,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        for arg in self.args.iter() {
            allocator.alloc_child(*arg)?;
        }
        Ok(())
    }

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError> {
        for arg in self.args.iter() {
            code_gen.encode_child(*arg)?;
        }
        let item = code_gen.lookup_name(self.ident);
        code_gen.encode_call(item)?;
        Ok(())
    }
}

impl EncodeStatement for ast::If {
    fn alloc_expr_locals(
        &self,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc_child(self.condition)?;
        for statement in self.block.iter() {
            allocator.alloc_statement(*statement)?;
        }
        Ok(())
    }

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError> {
        code_gen.encode_child(self.condition)?;
        let fields = code_gen.fields(self.condition)?;
        assert_eq!(fields.len(), 1);
        code_gen.read_expr_field(self.condition, &fields[0]);
        code_gen.instruction(&Instruction::If(enc::BlockType::Empty));
        for statement in self.block.iter() {
            code_gen.encode_statement(*statement)?;
        }
        code_gen.instruction(&Instruction::End);
        Ok(())
    }
}

impl EncodeStatement for ast::Return {
    fn alloc_expr_locals(
        &self,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        if let Some(expression) = self.expression {
            allocator.alloc_child(expression)?;
        }
        Ok(())
    }

    fn encode(&self, code_gen: &mut CodeGenerator) -> Result<(), GenerationError> {
        if let Some(expression) = self.expression {
            code_gen.encode_child(expression)?;

            let fields = code_gen.fields(expression)?;
            for field in fields.iter() {
                if code_gen.spill_return() {
                    code_gen.read_return_ptr()?;
                    code_gen.field_address(field);
                    code_gen.read_expr_field(expression, field);
                    code_gen.write_mem(field);
                } else {
                    code_gen.read_expr_field(expression, field);
                }
            }

            if code_gen.spill_return() {
                code_gen.read_return_ptr()?;
            }
        }
        code_gen.instruction(&Instruction::Return);
        Ok(())
    }
}

fn encode_assignment(
    ident: NameId,
    expression: ExpressionId,
    code_gen: &mut CodeGenerator,
) -> Result<(), GenerationError> {
    code_gen.encode_child(expression)?;
    let fields = code_gen.fields(expression)?;
    match code_gen.lookup_name(ident) {
        ItemId::Import(_) => panic!("Assigning to imports isn't allowed!!"),
        ItemId::Global(global) => {
            // TODO handle composite globals
            for field in fields {
                code_gen.read_expr_field(expression, &field);
                code_gen.instruction(&Instruction::GlobalSet(global.index() as u32));
            }
        }
        ItemId::Param(_) => panic!("Assigning to parameters isn't allowed!!"),
        ItemId::Local(local) => {
            for field in fields {
                code_gen.read_expr_field(expression, &field);
                code_gen.write_local_field(local, &field);
            }
        }
        ItemId::Function(_) => panic!("Assigning to functions isn't allowed!!"),
    }
    Ok(())
}