claw-codegen 0.2.6

The Claw language Wasm code generator
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use ast::ExpressionId;
use claw_ast as ast;
use claw_resolver::{ItemId, ResolvedType};

use crate::code::{CodeGenerator, ExpressionAllocator};
use crate::types::{
    Signedness, STRING_CONTENTS_ALIGNMENT, STRING_LENGTH_FIELD, STRING_OFFSET_FIELD,
};
use crate::GenerationError;

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

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

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

impl EncodeExpression for ast::Expression {
    fn alloc_expr_locals(
        &self,
        expression: ExpressionId,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        let expr: &dyn EncodeExpression = match self {
            ast::Expression::Identifier(expr) => expr,
            ast::Expression::Enum(expr) => expr,
            ast::Expression::Literal(expr) => expr,
            ast::Expression::Call(expr) => expr,
            ast::Expression::Unary(expr) => expr,
            ast::Expression::Binary(expr) => expr,
        };
        expr.alloc_expr_locals(expression, allocator)
    }

    fn encode(
        &self,
        expression: ExpressionId,
        code_gen: &mut CodeGenerator,
    ) -> Result<(), GenerationError> {
        let expr: &dyn EncodeExpression = match self {
            ast::Expression::Identifier(expr) => expr,
            ast::Expression::Enum(expr) => expr,
            ast::Expression::Literal(expr) => expr,
            ast::Expression::Call(expr) => expr,
            ast::Expression::Unary(expr) => expr,
            ast::Expression::Binary(expr) => expr,
        };
        expr.encode(expression, code_gen)?;
        Ok(())
    }
}

impl EncodeExpression for ast::Identifier {
    fn alloc_expr_locals(
        &self,
        expression: ExpressionId,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc(expression)
    }

    fn encode(
        &self,
        expression: ExpressionId,
        code_gen: &mut CodeGenerator,
    ) -> Result<(), GenerationError> {
        let fields = code_gen.fields(expression)?;
        match code_gen.lookup_name(self.ident) {
            ItemId::ImportFunc(_) => panic!("Cannot use imported function as value!!"),
            ItemId::Type(_) => panic!("Cannot use type as value!!"),
            ItemId::Global(global) => {
                // TODO handle composite globals
                let field = code_gen.one_field(expression)?;
                code_gen.instruction(&Instruction::GlobalGet(global.index() as u32));
                code_gen.write_expr_field(expression, &field);
            }
            ItemId::Param(param) => {
                for field in fields.iter() {
                    code_gen.read_param_field(param, field);
                    code_gen.write_expr_field(expression, field);
                }
            }
            ItemId::Local(local) => {
                for field in fields.iter() {
                    code_gen.read_local_field(local, field);
                    code_gen.write_expr_field(expression, field);
                }
            }
            ItemId::Function(_) => panic!("Cannot use function as value!!"),
        }
        Ok(())
    }
}

impl EncodeExpression for ast::EnumLiteral {
    fn alloc_expr_locals(
        &self,
        expression: ExpressionId,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc(expression)
    }

    fn encode(
        &self,
        expression: ExpressionId,
        code_gen: &mut CodeGenerator,
    ) -> Result<(), GenerationError> {
        match code_gen.lookup_name(self.enum_name) {
            ItemId::Type(ResolvedType::Import(import_type)) => {
                let import_type = code_gen.lookup_import_type(import_type);
                match import_type {
                    claw_resolver::ImportType::Enum(enum_type) => {
                        let case_name = code_gen.lookup_name_str(self.case_name);
                        // TODO nice error instead of unwrap
                        let case_index =
                            enum_type.cases.iter().position(|c| c == case_name).unwrap();
                        code_gen.const_i32(case_index as i32);
                        let field = code_gen.one_field(expression)?;
                        code_gen.write_expr_field(expression, &field);
                    }
                }
            }
            _ => unreachable!(),
        }
        Ok(())
    }
}

impl EncodeExpression for ast::Literal {
    fn alloc_expr_locals(
        &self,
        expression: ExpressionId,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc(expression)
    }

    fn encode(
        &self,
        expression: ExpressionId,
        code_gen: &mut CodeGenerator,
    ) -> Result<(), GenerationError> {
        match self {
            ast::Literal::String(string) => {
                // Allocate string pointer
                code_gen.const_i32(0);
                code_gen.const_i32(0);
                code_gen.const_i32(2i32.pow(STRING_CONTENTS_ALIGNMENT));
                code_gen.const_i32(string.len() as i32);
                code_gen.allocate();
                code_gen.write_expr_field(expression, &STRING_OFFSET_FIELD);
                // Store the string length
                code_gen.const_i32(string.len() as i32);
                code_gen.write_expr_field(expression, &STRING_LENGTH_FIELD);
                // Copy in the data segment
                let index = code_gen.encode_const_bytes(string.as_bytes());
                code_gen.read_expr_field(expression, &STRING_OFFSET_FIELD);
                code_gen.const_i32(0);
                code_gen.read_expr_field(expression, &STRING_LENGTH_FIELD);
                code_gen.instruction(&enc::Instruction::MemoryInit {
                    mem: 0,
                    data_index: index.into(),
                })
            }
            ast::Literal::Integer(int) => {
                let field = code_gen.one_field(expression)?;
                code_gen.encode_const_int(*int, &field);
                code_gen.write_expr_field(expression, &field);
            }
            ast::Literal::Float(float) => {
                let field = code_gen.one_field(expression)?;
                code_gen.encode_const_float(*float, &field);
                code_gen.write_expr_field(expression, &field);
            }
        }
        Ok(())
    }
}

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

    fn encode(
        &self,
        expression: ExpressionId,
        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, &self.args, Some(expression))
    }
}

impl EncodeExpression for ast::UnaryExpression {
    fn alloc_expr_locals(
        &self,
        expression: ExpressionId,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc(expression)?;
        allocator.alloc_child(self.inner)
    }

    fn encode(
        &self,
        expression: ExpressionId,
        code_gen: &mut CodeGenerator,
    ) -> Result<(), GenerationError> {
        code_gen.const_i32(0); // TODO support 64 bit ints
        code_gen.encode_child(self.inner)?;
        for field in code_gen.fields(self.inner)?.iter() {
            code_gen.read_expr_field(self.inner, field);
        }
        code_gen.instruction(&enc::Instruction::I32Sub);
        for field in code_gen.fields(expression)?.iter() {
            code_gen.write_expr_field(expression, field);
        }
        Ok(())
    }
}

impl EncodeExpression for ast::BinaryExpression {
    fn alloc_expr_locals(
        &self,
        expression: ExpressionId,
        allocator: &mut ExpressionAllocator,
    ) -> Result<(), GenerationError> {
        allocator.alloc(expression)?;
        allocator.alloc_child(self.left)?;
        allocator.alloc_child(self.right)?;
        Ok(())
    }

    fn encode(
        &self,
        expression: ExpressionId,
        code_gen: &mut CodeGenerator,
    ) -> Result<(), GenerationError> {
        code_gen.encode_child(self.left)?;
        code_gen.encode_child(self.right)?;

        let ptype = code_gen.get_ptype(expression)?;
        if ptype == Some(ast::PrimitiveType::String) {
            if self.op == ast::BinaryOp::Add {
                encode_string_concatenation(expression, self.left, self.right, code_gen)
            } else {
                panic!("Strings can only be concatenated with '+'");
            }
        } else {
            encode_binary_arithmetic(self.op, expression, self.left, self.right, code_gen)
        }
    }
}

fn encode_string_concatenation(
    expression: ExpressionId,
    left: ExpressionId,
    right: ExpressionId,
    code_gen: &mut CodeGenerator,
) -> Result<(), GenerationError> {
    // Compute new length
    code_gen.read_expr_field(left, &STRING_LENGTH_FIELD);
    code_gen.read_expr_field(right, &STRING_LENGTH_FIELD);
    code_gen.instruction(&enc::Instruction::I32Add);
    code_gen.write_expr_field(expression, &STRING_LENGTH_FIELD);
    // Allocate new string
    code_gen.const_i32(0);
    code_gen.const_i32(0);
    code_gen.const_i32(2i32.pow(STRING_CONTENTS_ALIGNMENT));
    code_gen.read_expr_field(expression, &STRING_LENGTH_FIELD);
    code_gen.allocate();
    code_gen.write_expr_field(expression, &STRING_OFFSET_FIELD);
    // Copy in the left string
    code_gen.read_expr_field(expression, &STRING_OFFSET_FIELD);
    code_gen.read_expr_field(left, &STRING_OFFSET_FIELD);
    code_gen.read_expr_field(left, &STRING_LENGTH_FIELD);
    code_gen.instruction(&enc::Instruction::MemoryCopy {
        src_mem: 0,
        dst_mem: 0,
    });
    // Copy in the right string
    code_gen.read_expr_field(expression, &STRING_OFFSET_FIELD);
    code_gen.read_expr_field(left, &STRING_LENGTH_FIELD);
    code_gen.instruction(&enc::Instruction::I32Add);
    code_gen.read_expr_field(right, &STRING_OFFSET_FIELD);
    code_gen.read_expr_field(right, &STRING_LENGTH_FIELD);
    code_gen.instruction(&enc::Instruction::MemoryCopy {
        src_mem: 0,
        dst_mem: 0,
    });
    Ok(())
}

const S: Signedness = Signedness::Signed;
const U: Signedness = Signedness::Unsigned;

fn encode_binary_arithmetic(
    op: ast::BinaryOp,
    expression: ExpressionId,
    left: ExpressionId,
    right: ExpressionId,
    code_gen: &mut CodeGenerator,
) -> Result<(), GenerationError> {
    let left_field = code_gen.one_field(left)?;
    let right_field = code_gen.one_field(right)?;
    let field = code_gen.one_field(expression)?;

    let valtype = left_field.stack_type;
    let signedness = left_field.signedness;
    let mask = left_field.arith_mask;

    code_gen.read_expr_field(left, &left_field);
    code_gen.read_expr_field(right, &right_field);

    let instruction = match (op, valtype, signedness) {
        // Multiply
        (ast::BinaryOp::Multiply, enc::ValType::I32, _) => enc::Instruction::I32Mul,
        (ast::BinaryOp::Multiply, enc::ValType::I64, _) => enc::Instruction::I64Mul,
        (ast::BinaryOp::Multiply, enc::ValType::F32, _) => enc::Instruction::F32Mul,
        (ast::BinaryOp::Multiply, enc::ValType::F64, _) => enc::Instruction::F64Mul,
        // Divide
        (ast::BinaryOp::Divide, enc::ValType::I32, S) => enc::Instruction::I32DivS,
        (ast::BinaryOp::Divide, enc::ValType::I32, U) => enc::Instruction::I32DivU,
        (ast::BinaryOp::Divide, enc::ValType::I64, S) => enc::Instruction::I64DivS,
        (ast::BinaryOp::Divide, enc::ValType::I64, U) => enc::Instruction::I64DivU,
        (ast::BinaryOp::Divide, enc::ValType::F32, _) => enc::Instruction::F32Div,
        (ast::BinaryOp::Divide, enc::ValType::F64, _) => enc::Instruction::F64Div,
        // Modulo
        (ast::BinaryOp::Modulo, enc::ValType::I32, S) => enc::Instruction::I32RemS,
        (ast::BinaryOp::Modulo, enc::ValType::I32, U) => enc::Instruction::I32RemU,
        (ast::BinaryOp::Modulo, enc::ValType::I64, S) => enc::Instruction::I64RemS,
        (ast::BinaryOp::Modulo, enc::ValType::I64, U) => enc::Instruction::I64RemU,
        // Addition
        (ast::BinaryOp::Add, enc::ValType::I32, _) => enc::Instruction::I32Add,
        (ast::BinaryOp::Add, enc::ValType::I64, _) => enc::Instruction::I64Add,
        (ast::BinaryOp::Add, enc::ValType::F32, _) => enc::Instruction::F32Add,
        (ast::BinaryOp::Add, enc::ValType::F64, _) => enc::Instruction::F64Add,
        // Subtraction
        (ast::BinaryOp::Subtract, enc::ValType::I32, _) => enc::Instruction::I32Sub,
        (ast::BinaryOp::Subtract, enc::ValType::I64, _) => enc::Instruction::I64Sub,
        (ast::BinaryOp::Subtract, enc::ValType::F32, _) => enc::Instruction::F32Sub,
        (ast::BinaryOp::Subtract, enc::ValType::F64, _) => enc::Instruction::F64Sub,
        // Logical Bit Shifting
        (ast::BinaryOp::BitShiftL, enc::ValType::I32, _) => enc::Instruction::I32Shl,
        (ast::BinaryOp::BitShiftL, enc::ValType::I64, _) => enc::Instruction::I64Shl,
        (ast::BinaryOp::BitShiftR, enc::ValType::I32, _) => enc::Instruction::I32ShrU,
        (ast::BinaryOp::BitShiftR, enc::ValType::I64, _) => enc::Instruction::I64ShrU,
        // Arithmetic Bit Shifting
        (ast::BinaryOp::ArithShiftR, enc::ValType::I32, S) => enc::Instruction::I32ShrS,
        (ast::BinaryOp::ArithShiftR, enc::ValType::I32, U) => enc::Instruction::I32ShrU,
        (ast::BinaryOp::ArithShiftR, enc::ValType::I64, S) => enc::Instruction::I64ShrS,
        (ast::BinaryOp::ArithShiftR, enc::ValType::I64, U) => enc::Instruction::I64ShrU,
        // Less than
        (ast::BinaryOp::LessThan, enc::ValType::I32, S) => enc::Instruction::I32LtS,
        (ast::BinaryOp::LessThan, enc::ValType::I32, U) => enc::Instruction::I32LtU,
        (ast::BinaryOp::LessThan, enc::ValType::I64, S) => enc::Instruction::I64LtS,
        (ast::BinaryOp::LessThan, enc::ValType::I64, U) => enc::Instruction::I64LtU,
        (ast::BinaryOp::LessThan, enc::ValType::F32, _) => enc::Instruction::F32Lt,
        (ast::BinaryOp::LessThan, enc::ValType::F64, _) => enc::Instruction::F64Lt,
        // Less than equal
        (ast::BinaryOp::LessThanEqual, enc::ValType::I32, S) => enc::Instruction::I32LeS,
        (ast::BinaryOp::LessThanEqual, enc::ValType::I32, U) => enc::Instruction::I32LeU,
        (ast::BinaryOp::LessThanEqual, enc::ValType::I64, S) => enc::Instruction::I64LeS,
        (ast::BinaryOp::LessThanEqual, enc::ValType::I64, U) => enc::Instruction::I64LeU,
        (ast::BinaryOp::LessThanEqual, enc::ValType::F32, _) => enc::Instruction::F32Le,
        (ast::BinaryOp::LessThanEqual, enc::ValType::F64, _) => enc::Instruction::F64Le,
        // Greater than
        (ast::BinaryOp::GreaterThan, enc::ValType::I32, S) => enc::Instruction::I32GtS,
        (ast::BinaryOp::GreaterThan, enc::ValType::I32, U) => enc::Instruction::I32GtU,
        (ast::BinaryOp::GreaterThan, enc::ValType::I64, S) => enc::Instruction::I64GtS,
        (ast::BinaryOp::GreaterThan, enc::ValType::I64, U) => enc::Instruction::I64GtU,
        (ast::BinaryOp::GreaterThan, enc::ValType::F32, _) => enc::Instruction::F32Gt,
        (ast::BinaryOp::GreaterThan, enc::ValType::F64, _) => enc::Instruction::F64Gt,
        // Greater than or equal
        (ast::BinaryOp::GreaterThanEqual, enc::ValType::I32, S) => enc::Instruction::I32GeS,
        (ast::BinaryOp::GreaterThanEqual, enc::ValType::I32, U) => enc::Instruction::I32GeU,
        (ast::BinaryOp::GreaterThanEqual, enc::ValType::I64, S) => enc::Instruction::I64GeS,
        (ast::BinaryOp::GreaterThanEqual, enc::ValType::I64, U) => enc::Instruction::I64GeU,
        (ast::BinaryOp::GreaterThanEqual, enc::ValType::F32, _) => enc::Instruction::F32Ge,
        (ast::BinaryOp::GreaterThanEqual, enc::ValType::F64, _) => enc::Instruction::F64Ge,
        // Equal
        (ast::BinaryOp::Equals, enc::ValType::I32, _) => enc::Instruction::I32Eq,
        (ast::BinaryOp::Equals, enc::ValType::I64, _) => enc::Instruction::I64Eq,
        (ast::BinaryOp::Equals, enc::ValType::F32, _) => enc::Instruction::F32Eq,
        (ast::BinaryOp::Equals, enc::ValType::F64, _) => enc::Instruction::F64Eq,
        // Not equal
        (ast::BinaryOp::NotEquals, enc::ValType::I32, _) => enc::Instruction::I32Ne,
        (ast::BinaryOp::NotEquals, enc::ValType::I64, _) => enc::Instruction::I64Ne,
        (ast::BinaryOp::NotEquals, enc::ValType::F32, _) => enc::Instruction::F32Ne,
        (ast::BinaryOp::NotEquals, enc::ValType::F64, _) => enc::Instruction::F64Ne,
        // Bitwise and
        (ast::BinaryOp::BitAnd, enc::ValType::I32, _) => enc::Instruction::I32And,
        (ast::BinaryOp::BitAnd, enc::ValType::I64, _) => enc::Instruction::I64And,
        // Bitwise xor
        (ast::BinaryOp::BitXor, enc::ValType::I32, _) => enc::Instruction::I32Xor,
        (ast::BinaryOp::BitXor, enc::ValType::I64, _) => enc::Instruction::I64Xor,
        // Bitwise or
        (ast::BinaryOp::BitOr, enc::ValType::I32, _) => enc::Instruction::I32Or,
        (ast::BinaryOp::BitOr, enc::ValType::I64, _) => enc::Instruction::I64Or,
        // Logical and/or
        (ast::BinaryOp::LogicalAnd, enc::ValType::I32, _) => enc::Instruction::I32And,
        (ast::BinaryOp::LogicalOr, enc::ValType::I32, _) => enc::Instruction::I32Or,
        // Fallback
        (operator, valtype, _) => panic!(
            "Cannot apply binary operator {:?} to type {:?}",
            operator, valtype
        ),
    };
    code_gen.instruction(&instruction);

    if let Some(mask) = mask {
        code_gen.const_i32(mask);
        code_gen.instruction(&enc::Instruction::I32And);
    }

    code_gen.write_expr_field(expression, &field);
    Ok(())
}