template-compiler 0.1.1

A compiler from a simple template language to Wasm components
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 std::collections::HashSet;

use wasm_encoder::{
    DataCountSection, DataSection, Function, Instruction, MemArg, TypeSection, ValType, ComponentTypeSection, PrimitiveValType, ComponentValType, BlockType,
};

use crate::{parse::Node, FileData};

const REALLOC_FUNC_INDEX: u32 = 0;
const MEMORY_INDEX: u32 = 0;

const MAX_FLAT_PARAMS: u32 = 16;

pub struct TemplateGenerator<'source> {
    params: Params<'source>,
    file_data: &'source FileData<'source>,
}

pub struct Params<'source> {
    text_params: Vec<&'source str>,
    cond_params: Vec<&'source str>,
}

impl<'source> Params<'source> {
    pub fn new(contents: &'source Vec<Node<'source>>) -> Self {
        let mut text_params = HashSet::new();
        let mut cond_params = HashSet::new();
        for node in contents {
            Self::collect_params(node, &mut text_params, &mut cond_params);
        }
        let mut text_params: Vec<&str> = text_params.into_iter().collect();
        let mut cond_params: Vec<&str> = cond_params.into_iter().collect();
        text_params.sort();
        cond_params.sort();
        Params {
            text_params,
            cond_params,
        }
    }

    fn collect_params(
        node: &'source Node<'source>,
        text_params: &mut HashSet<&'source str>,
        cond_params: &mut HashSet<&'source str>,
    ) {
        match node {
            Node::Text { .. } => {}
            Node::Parameter { name } => {
                text_params.insert(name.value);
            }
            Node::Conditional {
                if_kwd: _,
                cond_ident,
                contents,
                endif_kwd: _,
            } => {
                cond_params.insert(cond_ident.value);
                for node in contents {
                    Self::collect_params(node, text_params, cond_params);
                }
            }
        }
    }

    pub fn stack_len(&self) -> u32 {
        self.text_stack_len() + (self.cond_params.len() as u32)
    }

    fn text_stack_len(&self) -> u32 {
        2 * (self.text_params.len() as u32)
    }

    fn text_mem_len(&self) -> u32 {
        8 * (self.text_params.len() as u32)
    }

    pub fn must_spill(&self) -> bool {
        self.stack_len() > MAX_FLAT_PARAMS
    }

    // The number of text parameters
    pub fn text_params_len(&self) -> usize {
        self.text_params.len()
    }

    // The index in the parameters of a given text parameter name
    pub fn text_param_index(&self, param: &str) -> usize {
        self.text_params.binary_search(&param).unwrap()
    }

    // The index in the parameters of a given condition parameter name
    pub fn cond_param_index(&self, param: &str) -> usize {
        self.cond_params.binary_search(&param).unwrap()
    }

    pub fn record_type(&self) -> ComponentTypeSection {
        let mut types = ComponentTypeSection::new();
        let converted_names: Vec<String> = self.text_params.iter().map(|param: &&str| snake_to_kebab(param)).collect();
        let text_fields = converted_names.iter().map(|param| {
            (
                param.as_str(),
                ComponentValType::Primitive(PrimitiveValType::String),
            )
        });
        let converted_names: Vec<String> = self.cond_params.iter().map(|param: &&str| snake_to_kebab(param)).collect();
        let cond_fields = converted_names.iter().map(|param| {
            (
                param.as_str(),
                ComponentValType::Primitive(PrimitiveValType::Bool),
            )
        });
        let fields: Vec<(&str, ComponentValType)> = text_fields.chain(cond_fields).collect();
        types.defined_type().record(fields);
        types
    }

    fn gen_push_text_offset(&self, func: &mut Function, text_index: u32) {
        self.gen_push_text_field(func, text_index, 0)
    }

    fn gen_push_text_len(&self, func: &mut Function, text_index: u32) {
        self.gen_push_text_field(func, text_index, 1)
    }
    
    fn gen_push_text_field(&self, func: &mut Function, text_index: u32, field: u32) {
        if self.must_spill() {
            // push params offset
            func.instruction(&Instruction::LocalGet(0));
            // push param index shift
            let shift = (text_index * 8) + (field * 4);
            let shift = shift.try_into().unwrap();
            func.instruction(&Instruction::I32Const(shift));
            // compute the final param index
            func.instruction(&Instruction::I32Add);
            // load the param string offset
            func.instruction(&Instruction::I32Load(MemArg {
                offset: 0,
                align: 4,
                memory_index: 0,
            }));
        } else {
            let local_index = 2 * text_index + field;
            func.instruction(&Instruction::LocalGet(local_index));
        }
    }

    fn gen_push_cond(&self, func: &mut Function, cond_index: u32) {
        if self.must_spill() {
            // push params offset
            func.instruction(&Instruction::LocalGet(0));
            // push param index shift
            let shift = self.text_mem_len() + cond_index;
            let shift = shift.try_into().unwrap();
            func.instruction(&Instruction::I32Const(shift));
            // compute the final param index
            func.instruction(&Instruction::I32Add);
            // load the param string offset
            func.instruction(&Instruction::I32Load8U(MemArg {
                offset: 0,
                align: 1,
                memory_index: 0,
            }));
        } else {
            let local_index = self.text_stack_len() + cond_index;
            func.instruction(&Instruction::LocalGet(local_index));
        }
    }
}

impl<'source> TemplateGenerator<'source> {
    pub fn new(params: Params<'source>, file_data: &'source FileData<'source>) -> Self {
        Self { params, file_data }
    }

    pub fn params(&self) -> &Params<'source> {
        &self.params
    }

    fn arguments_len(&self) -> u32 {
        if self.params.must_spill() {
            1
        } else {
            self.params.stack_len()
        }
    }

    fn result_len_local(&self) -> u32 {
        self.arguments_len() + 0
    }

    fn result_addr_local(&self) -> u32 {
        self.arguments_len() + 1
    }

    fn return_area_local(&self) -> u32 {
        self.arguments_len() + 2
    }

    fn result_cursor_local(&self) -> u32 {
        self.arguments_len() + 3
    }

    fn locals_len(&self) -> u32 {
        4
    }

    pub fn gen_core_type(&self, types: &mut TypeSection) {
        let params = vec![ValType::I32; self.arguments_len() as usize];
        let results = vec![ValType::I32];
        types.function(params, results);
    }

    pub fn gen_data(&self) -> (DataCountSection, DataSection) {
        let mut count = 0;
        let mut data = DataSection::new();

        for node in self.file_data.contents.iter() {
            Self::collect_data(node, &mut count, &mut data);
        }

        let count = DataCountSection { count };
        (count, data)
    }

    fn collect_data(node: &Node<'source>, count: &mut u32, data: &mut DataSection) {
        match node {
            Node::Text { index: _, text } => {
                data.passive(text.value.bytes());
                *count += 1;
            }
            Node::Parameter { name: _ } => {}
            Node::Conditional {
                if_kwd: _,
                cond_ident: _,
                contents,
                endif_kwd: _,
            } => {
                for node in contents {
                    Self::collect_data(node, count, data);
                }
            }
        }
    }

    pub fn gen_core_function(&self) -> Function {
        // Local variables
        let locals = vec![(self.locals_len(), ValType::I32)];
        let mut func = Function::new(locals);

        self.gen_calculate_len(&mut func);
        self.gen_allocate_results(&mut func);
        self.gen_init_cursor(&mut func);
        self.gen_write_template(&mut func);

        func.instruction(&Instruction::LocalGet(self.return_area_local()));
        func.instruction(&Instruction::End);
        func
    }

    fn gen_calculate_len(&self, func: &mut Function) {
        self.gen_calculate_sequence_len(func, self.file_data.contents.as_slice());
        // Store the calculated length
        func.instruction(&Instruction::LocalSet(self.result_len_local()));
    }

    fn gen_calculate_sequence_len(&self, func: &mut Function, sequence: &[Node<'source>]) {
        let mut base_length = 0;
        let mut param_counts = vec![0; self.params.text_params_len()];
        let mut prior_exists = false;
        for node in sequence.iter() {
            match node {
                Node::Text { index: _, text } => {
                    base_length += text.value.len() as i32;
                }
                Node::Parameter { name } => {
                    let index = self.params.text_param_index(&name.value);
                    param_counts[index] += 1;
                }
                Node::Conditional {
                    if_kwd: _,
                    cond_ident,
                    contents,
                    endif_kwd: _,
                } => {
                    let cond_index = self.params.cond_param_index(cond_ident.value) as u32;

                    self.params.gen_push_cond(func, cond_index);
                    func.instruction(&Instruction::If(BlockType::Result(ValType::I32)));
                    self.gen_calculate_sequence_len(func, &contents);
                    func.instruction(&Instruction::Else);
                    func.instruction(&Instruction::I32Const(0));
                    func.instruction(&Instruction::End);

                    if prior_exists {
                        func.instruction(&Instruction::I32Add);
                    }

                    prior_exists = true;
                }
            }
        }

        // push the base length
        func.instruction(&Instruction::I32Const(base_length));

        if prior_exists {
            func.instruction(&Instruction::I32Add);
        }

        // accumulate the dynamic part of the length
        for (index, count) in param_counts.iter().enumerate() {
            if *count > 0 {
                // load the length of the parameter
                self.params.gen_push_text_len(func, index as u32);
                // push the count of parameter occurrences
                func.instruction(&Instruction::I32Const(*count));
                // multiple the length by the occurrences
                func.instruction(&Instruction::I32Mul);
                // add this length addition to the total length
                func.instruction(&Instruction::I32Add);
            }
        }
    }

    fn gen_allocate_results(&self, func: &mut Function) {
        // allocate result string
        func.instruction(&Instruction::I32Const(0));
        func.instruction(&Instruction::I32Const(0));
        func.instruction(&Instruction::I32Const(1));
        func.instruction(&Instruction::LocalGet(self.result_len_local()));
        func.instruction(&Instruction::Call(REALLOC_FUNC_INDEX));
        // store allocated address
        func.instruction(&Instruction::LocalSet(self.result_addr_local()));

        // allocate return area
        func.instruction(&Instruction::I32Const(0));
        func.instruction(&Instruction::I32Const(0));
        func.instruction(&Instruction::I32Const(4));
        func.instruction(&Instruction::I32Const(8));
        func.instruction(&Instruction::Call(REALLOC_FUNC_INDEX));
        // store allocated address
        func.instruction(&Instruction::LocalSet(self.return_area_local()));

        // populate return area
        // store result addr
        let mem_arg = MemArg {
            offset: 0,
            align: 2,
            memory_index: MEMORY_INDEX,
        };
        func.instruction(&Instruction::LocalGet(self.return_area_local()));
        func.instruction(&Instruction::LocalGet(self.result_addr_local()));
        func.instruction(&Instruction::I32Store(mem_arg));
        // store result len
        func.instruction(&Instruction::LocalGet(self.return_area_local()));
        func.instruction(&Instruction::I32Const(4));
        func.instruction(&Instruction::I32Add);
        func.instruction(&Instruction::LocalGet(self.result_len_local()));
        func.instruction(&Instruction::I32Store(mem_arg));
    }

    fn gen_init_cursor(&self, func: &mut Function) {
        // set cursor to result string address
        func.instruction(&Instruction::LocalGet(self.result_addr_local()));
        func.instruction(&Instruction::LocalSet(self.result_cursor_local()));
    }

    fn gen_write_template(&self, func: &mut Function) {
        self.gen_write_sequence_template(func, &self.file_data.contents);
    }

    fn gen_write_sequence_template(&self, func: &mut Function, sequence: &[Node<'source>]) {
        for node in sequence {
            // note both branches end by pushing the cursor shift
            match node {
                Node::Text { index, text } => {
                    self.gen_write_segment(func, *index as u32, text.value.len() as i32);
                }
                Node::Parameter { name } => {
                    let index = self.params.text_param_index(&name.value);
                    self.gen_write_param(func, index as u32);
                }
                Node::Conditional {
                    if_kwd: _,
                    cond_ident,
                    contents,
                    endif_kwd: _,
                } => {
                    let cond_index = self.params.cond_param_index(cond_ident.value) as u32;

                    self.params.gen_push_cond(func, cond_index);
                    func.instruction(&Instruction::If(BlockType::Empty));
                    self.gen_write_sequence_template(func, contents);
                    func.instruction(&Instruction::Else);
                    func.instruction(&Instruction::End);
                }
            }
            
            if matches!(node, Node::Text { .. }) || matches!(node, Node::Parameter { .. }) {
                // push cursor and add to shift
                func.instruction(&Instruction::LocalGet(self.result_cursor_local()));
                func.instruction(&Instruction::I32Add);
                func.instruction(&Instruction::LocalSet(self.result_cursor_local()));
            }
        }
    }

    fn gen_write_segment(&self, func: &mut Function, data_index: u32, length: i32) {
        // push destination
        func.instruction(&Instruction::LocalGet(self.result_cursor_local()));
        // push source
        func.instruction(&Instruction::I32Const(0));
        // push length
        func.instruction(&Instruction::I32Const(length));
        // copy data segment into output
        func.instruction(&Instruction::MemoryInit { mem: 0, data_index });

        // push length
        func.instruction(&Instruction::I32Const(length));
    }

    fn gen_write_param(&self, func: &mut Function, param_index: u32) {
        // push destination
        func.instruction(&Instruction::LocalGet(self.result_cursor_local()));
        // push source
        self.params.gen_push_text_offset(func, param_index);
        // push length
        self.params.gen_push_text_len(func, param_index);
        // copy the argument data
        func.instruction(&Instruction::MemoryCopy {
            src_mem: MEMORY_INDEX,
            dst_mem: MEMORY_INDEX,
        });

        // push length
        self.params.gen_push_text_len(func, param_index);
    }
}

fn snake_to_kebab(ident: &str) -> String {
    ident.replace("_", "-")
}