yulang-native 0.1.1

Native backend experiments for Yulang
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use std::collections::{HashMap, HashSet};
use std::fmt;

use crate::abi::{NativeAbiBlock, NativeAbiFunction, NativeAbiModule, NativeAbiStmt};
use crate::control_ir::{BlockId, NativeTerminator, ValueId};

pub type NativeAbiValidateResult<T> = Result<T, NativeAbiValidateError>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NativeAbiValidateError {
    DuplicateFunction {
        name: String,
    },
    DuplicateBlock {
        function: String,
        block: BlockId,
    },
    DuplicateBlockParam {
        function: String,
        block: BlockId,
        value: ValueId,
    },
    DuplicateValue {
        function: String,
        block: BlockId,
        value: ValueId,
    },
    UndefinedValue {
        function: String,
        block: BlockId,
        value: ValueId,
    },
    MissingBlock {
        function: String,
        block: BlockId,
    },
    EnvSlotOutOfRange {
        function: String,
        block: BlockId,
        slot: usize,
        slots: usize,
    },
}

impl fmt::Display for NativeAbiValidateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NativeAbiValidateError::DuplicateFunction { name } => {
                write!(f, "duplicate native ABI function `{name}`")
            }
            NativeAbiValidateError::DuplicateBlock { function, block } => {
                write!(f, "duplicate native ABI block {block:?} in `{function}`")
            }
            NativeAbiValidateError::DuplicateBlockParam {
                function,
                block,
                value,
            } => write!(
                f,
                "duplicate native ABI block param {value:?} in block {block:?} of `{function}`"
            ),
            NativeAbiValidateError::DuplicateValue {
                function,
                block,
                value,
            } => write!(
                f,
                "duplicate native ABI value {value:?} in block {block:?} of `{function}`"
            ),
            NativeAbiValidateError::UndefinedValue {
                function,
                block,
                value,
            } => write!(
                f,
                "undefined native ABI value {value:?} in block {block:?} of `{function}`"
            ),
            NativeAbiValidateError::MissingBlock { function, block } => {
                write!(f, "missing native ABI block {block:?} in `{function}`")
            }
            NativeAbiValidateError::EnvSlotOutOfRange {
                function,
                block,
                slot,
                slots,
            } => write!(
                f,
                "native ABI env slot {slot} is out of range for {slots} slots in block {block:?} of `{function}`"
            ),
        }
    }
}

impl std::error::Error for NativeAbiValidateError {}

pub fn validate_abi_module(module: &NativeAbiModule) -> NativeAbiValidateResult<()> {
    let mut functions = HashSet::new();
    for function in module.functions.iter().chain(&module.roots) {
        if !functions.insert(function.name.clone()) {
            return Err(NativeAbiValidateError::DuplicateFunction {
                name: function.name.clone(),
            });
        }
        validate_function(function)?;
    }
    Ok(())
}

fn validate_function(function: &NativeAbiFunction) -> NativeAbiValidateResult<()> {
    let mut blocks = HashSet::new();
    for block in &function.blocks {
        if !blocks.insert(block.id) {
            return Err(NativeAbiValidateError::DuplicateBlock {
                function: function.name.clone(),
                block: block.id,
            });
        }
    }
    let entry = function.blocks.first().map(|block| block.id);
    let block_start_values = function_block_start_values(function);
    for block in &function.blocks {
        let values = block_start_values
            .get(&block.id)
            .cloned()
            .unwrap_or_default();
        validate_block(function, block, &blocks, Some(block.id) == entry, values)?;
    }
    Ok(())
}

fn validate_block(
    function: &NativeAbiFunction,
    block: &NativeAbiBlock,
    blocks: &HashSet<BlockId>,
    is_entry: bool,
    mut values: HashSet<ValueId>,
) -> NativeAbiValidateResult<()> {
    let block_params = if is_entry && block.params.starts_with(&function.params) {
        &block.params[function.params.len()..]
    } else {
        block.params.as_slice()
    };
    let mut seen_params = function.params.iter().copied().collect::<HashSet<_>>();
    for param in block_params {
        if !seen_params.insert(*param) {
            return Err(NativeAbiValidateError::DuplicateBlockParam {
                function: function.name.clone(),
                block: block.id,
                value: *param,
            });
        }
    }
    for stmt in &block.stmts {
        validate_stmt_uses(function, block, stmt, &values)?;
        let dest = stmt_dest(stmt);
        if !values.insert(dest) {
            return Err(NativeAbiValidateError::DuplicateValue {
                function: function.name.clone(),
                block: block.id,
                value: dest,
            });
        }
    }
    validate_terminator(function, block, blocks, &values)
}

fn function_block_start_values(function: &NativeAbiFunction) -> HashMap<BlockId, HashSet<ValueId>> {
    let mut start = function
        .blocks
        .iter()
        .map(|block| {
            (
                block.id,
                block.params.iter().copied().collect::<HashSet<_>>(),
            )
        })
        .collect::<HashMap<_, _>>();
    if let Some(entry) = function.blocks.first() {
        start
            .entry(entry.id)
            .or_default()
            .extend(function.params.iter().copied());
    }

    let mut changed = true;
    while changed {
        changed = false;
        for block in &function.blocks {
            let mut out = start.get(&block.id).cloned().unwrap_or_default();
            for stmt in &block.stmts {
                out.insert(stmt_dest(stmt));
            }
            for successor in terminator_successors(&block.terminator) {
                let entry = start.entry(successor).or_default();
                let old_len = entry.len();
                entry.extend(out.iter().copied());
                changed |= entry.len() != old_len;
            }
        }
    }
    start
}

fn validate_stmt_uses(
    function: &NativeAbiFunction,
    block: &NativeAbiBlock,
    stmt: &NativeAbiStmt,
    values: &HashSet<ValueId>,
) -> NativeAbiValidateResult<()> {
    match stmt {
        NativeAbiStmt::Literal { .. } => Ok(()),
        NativeAbiStmt::Primitive { args, .. }
        | NativeAbiStmt::DirectCall { args, .. }
        | NativeAbiStmt::Tuple { items: args, .. }
        | NativeAbiStmt::IndirectClosureCall { args, .. } => {
            for arg in args {
                require_value(function, block, values, *arg)?;
            }
            if let NativeAbiStmt::IndirectClosureCall { callee, .. } = stmt {
                require_value(function, block, values, *callee)?;
            }
            Ok(())
        }
        NativeAbiStmt::Record { base, fields, .. } => {
            if let Some(base) = base {
                require_value(function, block, values, *base)?;
            }
            for field in fields {
                require_value(function, block, values, field.value)?;
            }
            Ok(())
        }
        NativeAbiStmt::RecordWithoutFields { base, .. } => {
            require_value(function, block, values, *base)
        }
        NativeAbiStmt::Variant { value, .. } => {
            if let Some(value) = value {
                require_value(function, block, values, *value)?;
            }
            Ok(())
        }
        NativeAbiStmt::Select { base, .. } => require_value(function, block, values, *base),
        NativeAbiStmt::TupleGet { tuple, .. } => require_value(function, block, values, *tuple),
        NativeAbiStmt::VariantTagEq { variant, .. }
        | NativeAbiStmt::VariantPayload { variant, .. } => {
            require_value(function, block, values, *variant)
        }
        NativeAbiStmt::ValueEq { left, right, .. } => {
            require_value(function, block, values, *left)?;
            require_value(function, block, values, *right)
        }
        NativeAbiStmt::BoolAnd { left, right, .. } => {
            require_value(function, block, values, *left)?;
            require_value(function, block, values, *right)
        }
        NativeAbiStmt::LoadEnv { slot, .. } => {
            if *slot >= function.environment_slots {
                return Err(NativeAbiValidateError::EnvSlotOutOfRange {
                    function: function.name.clone(),
                    block: block.id,
                    slot: *slot,
                    slots: function.environment_slots,
                });
            }
            Ok(())
        }
        NativeAbiStmt::AllocateClosure { environment, .. } => {
            for value in environment {
                require_value(function, block, values, *value)?;
            }
            Ok(())
        }
    }
}

fn validate_terminator(
    function: &NativeAbiFunction,
    block: &NativeAbiBlock,
    blocks: &HashSet<BlockId>,
    values: &HashSet<ValueId>,
) -> NativeAbiValidateResult<()> {
    match &block.terminator {
        NativeTerminator::Return(value) => require_value(function, block, values, *value),
        NativeTerminator::Jump { target, args } => {
            require_block(function, *target, blocks)?;
            for arg in args {
                require_value(function, block, values, *arg)?;
            }
            Ok(())
        }
        NativeTerminator::Branch {
            cond,
            then_block,
            else_block,
        } => {
            require_value(function, block, values, *cond)?;
            require_block(function, *then_block, blocks)?;
            require_block(function, *else_block, blocks)
        }
    }
}

fn terminator_successors(terminator: &NativeTerminator) -> Vec<BlockId> {
    match terminator {
        NativeTerminator::Return(_) => Vec::new(),
        NativeTerminator::Jump { target, .. } => vec![*target],
        NativeTerminator::Branch {
            then_block,
            else_block,
            ..
        } => vec![*then_block, *else_block],
    }
}

fn stmt_dest(stmt: &NativeAbiStmt) -> ValueId {
    match stmt {
        NativeAbiStmt::Literal { dest, .. }
        | NativeAbiStmt::Primitive { dest, .. }
        | NativeAbiStmt::DirectCall { dest, .. }
        | NativeAbiStmt::Tuple { dest, .. }
        | NativeAbiStmt::Record { dest, .. }
        | NativeAbiStmt::RecordWithoutFields { dest, .. }
        | NativeAbiStmt::Variant { dest, .. }
        | NativeAbiStmt::Select { dest, .. }
        | NativeAbiStmt::TupleGet { dest, .. }
        | NativeAbiStmt::VariantTagEq { dest, .. }
        | NativeAbiStmt::VariantPayload { dest, .. }
        | NativeAbiStmt::ValueEq { dest, .. }
        | NativeAbiStmt::BoolAnd { dest, .. }
        | NativeAbiStmt::LoadEnv { dest, .. }
        | NativeAbiStmt::AllocateClosure { dest, .. }
        | NativeAbiStmt::IndirectClosureCall { dest, .. } => *dest,
    }
}

fn require_value(
    function: &NativeAbiFunction,
    block: &NativeAbiBlock,
    values: &HashSet<ValueId>,
    value: ValueId,
) -> NativeAbiValidateResult<()> {
    if values.contains(&value) {
        Ok(())
    } else {
        Err(NativeAbiValidateError::UndefinedValue {
            function: function.name.clone(),
            block: block.id,
            value,
        })
    }
}

fn require_block(
    function: &NativeAbiFunction,
    block: BlockId,
    blocks: &HashSet<BlockId>,
) -> NativeAbiValidateResult<()> {
    if blocks.contains(&block) {
        Ok(())
    } else {
        Err(NativeAbiValidateError::MissingBlock {
            function: function.name.clone(),
            block,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::abi::{NativeAbiBlock, NativeAbiFunction, NativeAbiModule, NativeAbiStmt};
    use crate::control_ir::{BlockId, NativeLiteral, NativeTerminator, ValueId};

    use super::*;

    #[test]
    fn accepts_valid_abi_module() {
        let module = NativeAbiModule {
            functions: Vec::new(),
            roots: vec![NativeAbiFunction {
                name: "root".to_string(),
                params: Vec::new(),
                environment_slots: 1,
                blocks: vec![NativeAbiBlock {
                    id: BlockId(0),
                    params: Vec::new(),
                    stmts: vec![
                        NativeAbiStmt::LoadEnv {
                            dest: ValueId(0),
                            slot: 0,
                        },
                        NativeAbiStmt::Literal {
                            dest: ValueId(1),
                            literal: NativeLiteral::Int("1".to_string()),
                        },
                        NativeAbiStmt::AllocateClosure {
                            dest: ValueId(2),
                            target: "root#lambda0".to_string(),
                            environment: vec![ValueId(0), ValueId(1)],
                        },
                    ],
                    terminator: NativeTerminator::Return(ValueId(2)),
                }],
            }],
        };

        validate_abi_module(&module).expect("valid abi");
    }

    #[test]
    fn rejects_out_of_range_env_slot() {
        let module = NativeAbiModule {
            functions: Vec::new(),
            roots: vec![NativeAbiFunction {
                name: "root".to_string(),
                params: Vec::new(),
                environment_slots: 0,
                blocks: vec![NativeAbiBlock {
                    id: BlockId(0),
                    params: Vec::new(),
                    stmts: vec![NativeAbiStmt::LoadEnv {
                        dest: ValueId(0),
                        slot: 0,
                    }],
                    terminator: NativeTerminator::Return(ValueId(0)),
                }],
            }],
        };

        assert_eq!(
            validate_abi_module(&module),
            Err(NativeAbiValidateError::EnvSlotOutOfRange {
                function: "root".to_string(),
                block: BlockId(0),
                slot: 0,
                slots: 0,
            })
        );
    }

    #[test]
    fn rejects_undefined_call_argument() {
        let module = NativeAbiModule {
            functions: Vec::new(),
            roots: vec![NativeAbiFunction {
                name: "root".to_string(),
                params: Vec::new(),
                environment_slots: 0,
                blocks: vec![NativeAbiBlock {
                    id: BlockId(0),
                    params: Vec::new(),
                    stmts: vec![NativeAbiStmt::DirectCall {
                        dest: ValueId(1),
                        target: "f".to_string(),
                        args: vec![ValueId(0)],
                    }],
                    terminator: NativeTerminator::Return(ValueId(1)),
                }],
            }],
        };

        assert_eq!(
            validate_abi_module(&module),
            Err(NativeAbiValidateError::UndefinedValue {
                function: "root".to_string(),
                block: BlockId(0),
                value: ValueId(0),
            })
        );
    }
}