veryl-analyzer 0.22.0

A modern hardware description language
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use crate::conv::Context;
use crate::conv::checker::portability::allow_multiple_assign;
use crate::conv::utils::{
    check_compatibility, check_implicit_clock_conversion, eval_array_literal,
};
use crate::ir::assign_table::{AssignContext, AssignTable};
use crate::ir::ff_table::AssignTarget;
use crate::ir::{
    AssignDestination, Comptime, Expression, FfTable, IrResult, Shape, Signature, Statement,
    ValueVariant, VarId, VarIndex, VarPath, VarPathSelect,
};
use crate::symbol::{Direction, Symbol, SymbolId, SymbolKind};
use crate::value::{Value, ValueBigUint};
use crate::{AnalyzerError, HashMap, ir_error};
use indent::indent_all_by;
use std::fmt;
use veryl_parser::resource_table::StrId;
use veryl_parser::token_range::TokenRange;

#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct FuncPath {
    pub path: VarPath,
    pub sig: Signature,
}

impl FuncPath {
    pub fn new(id: SymbolId) -> Self {
        Self {
            path: VarPath::default(),
            sig: Signature::new(id),
        }
    }

    pub fn add_prelude(&mut self, x: &[StrId]) {
        self.path.add_prelude(x)
    }

    pub fn base(&self) -> FuncPath {
        let mut ret = self.clone();
        ret.sig.parameters.clear();
        ret.sig.generic_parameters.clear();
        ret
    }
}

impl fmt::Display for FuncPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut ret = String::new();

        if !self.path.0.is_empty() {
            ret.push_str(&format!("{}.", self.path));
        }
        ret.push_str(&format!("{}", self.sig));

        ret.fmt(f)
    }
}

#[derive(Clone)]
pub struct FuncArg {
    pub name: StrId,
    pub comptime: Comptime,
    pub members: Vec<(VarPath, Comptime, Direction)>,
}

#[derive(Clone)]
pub struct Function {
    pub name: StrId,
    pub id: VarId,
    pub path: FuncPath,
    pub r#type: Comptime,
    pub array: Shape,
    pub arity: usize,
    pub args: Vec<FuncArg>,
    pub is_const: bool,
    pub functions: Vec<FunctionBody>,
    pub token: TokenRange,
}

impl Function {
    pub fn eval_assign(&self, context: &mut Context, assign_table: &mut AssignTable) {
        for x in &self.functions {
            x.eval_assign(context, assign_table);
        }
    }

    pub fn set_index(&mut self, index: &VarIndex) {
        for x in &mut self.functions {
            x.set_index(index);
        }
    }

    pub fn get_function(&self, index: &[usize]) -> Option<FunctionBody> {
        let index = self.array.calc_index(index)?;
        self.functions.get(index).cloned()
    }

    pub fn to_proto(&self) -> FuncProto {
        FuncProto {
            name: self.name,
            id: self.id,
            r#type: self.r#type.clone(),
            arity: self.arity,
            args: self.args.clone(),
            token: self.token,
        }
    }
}

#[derive(Clone)]
pub struct FuncProto {
    pub name: StrId,
    pub id: VarId,
    pub r#type: Comptime,
    pub arity: usize,
    pub args: Vec<FuncArg>,
    pub token: TokenRange,
}

#[derive(Clone)]
pub struct FunctionBody {
    pub ret: Option<VarId>,
    pub arg_map: HashMap<VarPath, VarId>,
    pub statements: Vec<Statement>,
}

impl FunctionBody {
    pub fn eval_assign(&self, context: &mut Context, assign_table: &mut AssignTable) {
        for x in &self.statements {
            x.eval_assign(context, assign_table, AssignContext::Function, &[]);
        }
    }

    pub fn set_index(&mut self, index: &VarIndex) {
        for x in &mut self.statements {
            x.set_index(index);
        }
    }
}

impl fmt::Display for Function {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut ret = String::new();

        for (i, f) in self.functions.iter().enumerate() {
            if self.functions.len() == 1 {
                ret.push_str(&format!("func {}({})", self.id, self.path));
            } else {
                ret.push_str(&format!("func {}[{}]({})", self.id, i, self.path));
            }

            if let Some(x) = f.ret {
                ret.push_str(&format!(" -> {x}"));
            }
            ret.push_str(" {\n");

            for s in &f.statements {
                let text = format!("{}\n", s);
                ret.push_str(&indent_all_by(2, text));
            }

            ret.push_str("}\n");
        }

        ret.trim_end().fmt(f)
    }
}

#[derive(Clone, Debug)]
pub struct FunctionCall {
    pub id: VarId,
    pub index: Option<Vec<usize>>,
    pub comptime: Comptime,
    pub inputs: CallArgs<Expression>,
    /// Output bindings that may actually be written by this specialization.
    pub outputs: CallArgs<Vec<AssignDestination>>,
}

#[derive(Hash, PartialEq, Eq)]
struct FunctionValueKey {
    id: VarId,
    index: Option<Vec<usize>>,
    inputs: Vec<(VarPath, Value)>,
}

/// Reuse pure constant-function results within one evaluation tree. The
/// cache never survives an outer call, so changing conversion contexts and
/// captured runtime state cannot make a later evaluation reuse stale data.
#[derive(Default)]
pub(crate) struct FunctionValueCache {
    depth: usize,
    bits: usize,
    values: HashMap<FunctionValueKey, Value>,
}

/// The element values behind an unpacked-array actual argument: the folded
/// array a const reference carries, or the elements of the variable it names.
/// A partial index is left unresolved -- it would need the element stride, and
/// guessing one is worse than the caller's existing `None` path.
fn array_arg_values(context: &mut Context, expr: &Expression, len: usize) -> Option<Vec<Value>> {
    let Expression::Term(factor) = expr else {
        return None;
    };
    match factor.as_ref() {
        crate::ir::Factor::Value(comptime) => match &comptime.value {
            ValueVariant::NumericArray(values) => values.get(0..len).map(<[Value]>::to_vec),
            _ => None,
        },
        crate::ir::Factor::Variable(id, index, select, _) => {
            if !select.is_empty() || !index.eval_value(context)?.is_empty() {
                return None;
            }
            let src = context.variables.get(id)?;
            src.value.get(0..len).map(<[Value]>::to_vec)
        }
        _ => None,
    }
}

impl FunctionCall {
    pub fn eval_type(&mut self, context: &mut Context) {
        self.comptime.is_const = self.eval_comptime_flag(context);
    }

    pub fn eval_value(&self, context: &mut Context) -> Option<Value> {
        #[cfg(test)]
        VALUE_EVALUATIONS.set(VALUE_EVALUATIONS.get() + 1);
        context.function_value_cache.depth += 1;
        let result = self.eval_value_inner(context);
        context.function_value_cache.depth -= 1;
        if context.function_value_cache.depth == 0 {
            context.function_value_cache.values.clear();
            context.function_value_cache.bits = 0;
        }
        result
    }

    fn eval_value_inner(&self, context: &mut Context) -> Option<Value> {
        let func = context.functions.get(&self.id)?;
        let mut cacheable = func.is_const && self.outputs.is_empty();
        let func = if let Some(x) = &self.index {
            func.get_function(x)
        } else {
            func.get_function(&[])
        }?;

        let mut inputs = Vec::new();
        let mut input_bits = 0usize;
        let mut evaluated_inputs = Vec::new();
        // Evaluate every actual in order, even on a cache hit. Defer copy-in
        // until all actuals are ready: an actual can call this same function
        // and overwrite its formals while the outer call is still evaluating.
        let mut evaluated_arrays = Vec::new();
        for (path, expr) in &self.inputs {
            let id = *func.arg_map.get(path)?;
            let total = context
                .variables
                .get(&id)?
                .r#type
                .total_array()
                .unwrap_or(1);
            if total > 1 {
                // An unpacked array has no single value to evaluate --
                // `get_value(&[])` wants one index per dimension -- so the
                // whole-array actual has to be copied element by element, the
                // way the simulator's inliner does for the same shape.  Having
                // no `Value`, it cannot key a cache entry either, so a call
                // taking one is evaluated every time.
                let values = array_arg_values(context, expr, total)?;
                cacheable = false;
                evaluated_arrays.push((id, values));
                continue;
            }
            let value = expr.eval_value(context)?;
            if cacheable {
                input_bits = input_bits.saturating_add(value.width().max(64));
                inputs.push((path.clone(), value.clone()));
            }
            evaluated_inputs.push((id, value));
        }
        for (id, value) in evaluated_inputs {
            let var = context.variable_mut(&id)?;
            var.set_value(&[], value, None);
        }
        for (id, values) in evaluated_arrays {
            context.variable_mut(&id)?.value = values;
        }
        let key = cacheable.then(|| FunctionValueKey {
            id: self.id,
            index: self.index.clone(),
            inputs,
        });
        if let Some(value) = key
            .as_ref()
            .and_then(|key| context.function_value_cache.values.get(key))
        {
            return Some(value.clone());
        }

        let disable_const_opt = context.disalbe_const_opt;
        context.disalbe_const_opt = true;
        let prior_overflow = context.comptime_for_overflow.take();
        // Saved and restored around the body: a call nested in an argument or
        // in the body itself must not inherit this frame's return state.
        let prior_ret_var = std::mem::replace(&mut context.function_ret_var, func.ret);
        let prior_returned = std::mem::replace(&mut context.function_returned, false);
        for x in &func.statements {
            if x.eval_value(context) == crate::ir::ControlFlow::Break {
                break;
            }
        }
        context.function_ret_var = prior_ret_var;
        context.function_returned = prior_returned;
        let overflowed = context.comptime_for_overflow.is_some();
        if let Some(x) = prior_overflow {
            context.comptime_for_overflow.get_or_insert(x);
        }
        context.disalbe_const_opt = disable_const_opt;

        // TODO get outputs

        // A skipped over-limit for loop leaves the return value at its
        // pre-loop state; treat the call as unevaluable instead.
        if overflowed {
            return None;
        }

        if let Some(x) = &func.ret {
            let variable = context.variables.get(x)?;
            let value = variable.get_value(&[]).cloned();
            if let Some(key) = key
                && let Some(value) = &value
            {
                let bits = input_bits.saturating_add(value.width().max(64));
                let total = context.function_value_cache.bits.saturating_add(bits);
                if total <= context.config.evaluate_size_limit {
                    context.function_value_cache.bits = total;
                    context
                        .function_value_cache
                        .values
                        .insert(key, value.clone());
                }
            }
            value
        } else {
            None
        }
    }

    pub fn eval_comptime(&mut self, context: &mut Context) -> Comptime {
        let value = self.eval_value(context);
        let value = if let Some(x) = value {
            ValueVariant::Numeric(x)
        } else {
            ValueVariant::Unknown
        };

        let mut ret = self.comptime.clone();
        ret.value = value;

        ret.is_const = self.eval_comptime_flag(context);
        ret
    }

    pub fn eval_assign(
        &self,
        context: &mut Context,
        assign_table: &mut AssignTable,
        assign_context: AssignContext,
    ) {
        // Record reads from input expressions so that downstream consumers
        // of `AssignTable.refernced` (e.g. the combinational-loop detector)
        // see input-variable references at the call site.
        for expr in self.inputs.values() {
            expr.eval_assign(context, assign_table, assign_context);
        }
        for output in self.outputs.values() {
            for dst in output {
                if let Some(index) = dst.index.eval_value(context) {
                    let Some(variable) = context.get_variable_info(dst.id) else {
                        continue;
                    };
                    if let Some((beg, end)) = dst.select.conservative_packed_range(
                        context,
                        &variable.r#type,
                        dst.comptime.member_select_domain,
                    ) {
                        let mask = ValueBigUint::gen_mask_range(beg, end);
                        let dynamic = !dst.index.is_const() || !dst.select.is_const_with_range();
                        let (success, tokens) = assign_table.insert_assign(
                            &variable,
                            index,
                            mask,
                            dynamic,
                            dynamic,
                            self.comptime.token,
                        );
                        if !success
                            && assign_context.is_ff()
                            && !allow_multiple_assign(context, dst.id)
                        {
                            context.insert_error(AnalyzerError::multiple_assignment(
                                &variable.path.to_string(),
                                &self.comptime.token,
                                &tokens,
                            ));
                        }
                    }
                }
            }
        }
    }

    pub fn gather_ff(
        &self,
        context: &mut Context,
        table: &mut FfTable,
        decl: usize,
        assign_target: Option<&AssignTarget>,
        from_ff: bool,
    ) {
        for input in self.inputs.values() {
            input.gather_ff(context, table, decl, assign_target, from_ff);
        }
        for dsts in self.outputs.values() {
            for dst in dsts {
                if from_ff {
                    dst.gather_ff(context, table, decl);
                } else {
                    dst.gather_ff_comb_assign(context, table, decl);
                }
            }
        }
    }

    pub fn gather_ff_comb_assign(&self, context: &mut Context, table: &mut FfTable, decl: usize) {
        for dsts in self.outputs.values() {
            for dst in dsts {
                dst.gather_ff_comb_assign(context, table, decl);
            }
        }
    }

    pub fn set_index(&mut self, index: &VarIndex) {
        for x in self.inputs.values_mut() {
            x.set_index(index);
        }
        for x in self.outputs.values_mut() {
            for x in x {
                x.set_index(index);
            }
        }
    }

    fn eval_comptime_flag(&mut self, context: &mut Context) -> bool {
        let mut is_const = context
            .functions
            .get(&self.id)
            .map(|func| func.is_const)
            .unwrap_or(true);
        for expr in self.inputs.values_mut() {
            is_const &= expr.eval_comptime(context, None).is_const;
        }

        // function with side-effect through output ports is not const
        if !self.outputs.is_empty() {
            is_const = false;
        }

        is_const
    }
}

#[cfg(test)]
thread_local! {
    pub(crate) static VALUE_EVALUATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

impl fmt::Display for FunctionCall {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut args = String::new();

        let mut inputs: Vec<_> = self.inputs.iter().map(|(k, v)| (k, v)).collect();
        let mut outputs: Vec<_> = self.outputs.iter().map(|(k, v)| (k, v)).collect();
        inputs.sort_by_key(|x| x.0);
        outputs.sort_by_key(|x| x.0);

        for (id, val) in &inputs {
            args.push_str(&format!("{id}: {val}, "));
        }
        for (id, val) in &outputs {
            if val.len() == 1 {
                args.push_str(&format!("{id}: {}, ", val[0]));
            } else {
                args.push_str(&format!("{id}: {{{}", val[0]));
                for x in &val[1..] {
                    args.push_str(&format!(", {x}"));
                }
                args.push_str("}}, ");
            }
        }
        let args = if args.is_empty() {
            &args
        } else {
            &args[0..args.len() - 2]
        };

        let mut index = String::new();
        if let Some(x) = &self.index {
            for x in x {
                index.push_str(&format!("[{}]", x));
            }
        }

        format!("{}{}({})", self.id, index, args).fmt(f)
    }
}

pub type PositionalArgs = Vec<(Expression, Vec<VarPathSelect>, TokenRange)>;
pub type NamedArgs = Vec<(StrId, (Expression, Vec<VarPathSelect>, TokenRange))>;
pub type FunctionArgs = (CallArgs<Expression>, CallArgs<Vec<AssignDestination>>);

/// Argument bindings of a [`FunctionCall`], in source argument order.
/// Not a map: `VarPath` keys are interned `StrId`s whose numeric values
/// vary run-to-run (parallel interning), so map iteration would make the
/// consumers' emission order — and the generated code — nondeterministic.
/// Built only by `to_function_args`, which preserves the source order.
#[derive(Clone, Debug, Default)]
pub struct CallArgs<T>(Vec<(VarPath, T)>);

impl<T> CallArgs<T> {
    pub fn values(&self) -> impl Iterator<Item = &T> {
        self.0.iter().map(|(_, v)| v)
    }

    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.0.iter_mut().map(|(_, v)| v)
    }
}

impl<T> std::ops::Deref for CallArgs<T> {
    type Target = [(VarPath, T)];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> std::ops::DerefMut for CallArgs<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a, T> IntoIterator for &'a CallArgs<T> {
    type Item = &'a (VarPath, T);
    type IntoIter = std::slice::Iter<'a, (VarPath, T)>;
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

#[derive(Clone)]
pub enum Arguments {
    Positional(PositionalArgs),
    Named(NamedArgs),
    Mixed(PositionalArgs, NamedArgs),
    Null,
}

impl Arguments {
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        match self {
            Arguments::Positional(x) => x.len(),
            Arguments::Named(x) => x.len(),
            Arguments::Mixed(x, y) => x.len() + y.len(),
            Arguments::Null => 0,
        }
    }

    pub fn to_system_function_args(
        self,
        _context: &mut Context,
        symbol: &Symbol,
    ) -> Vec<(Expression, Vec<VarPathSelect>, TokenRange)> {
        let ret = match self {
            Arguments::Positional(x) => x,
            Arguments::Named(_) => {
                // TODO error
                return vec![];
            }
            Arguments::Mixed(_, _) => vec![],
            Arguments::Null => vec![],
        };

        if let SymbolKind::SystemFunction(x) = &symbol.kind {
            let arity = x.ports.len();

            if arity != ret.len() {
                // TODO
                //let name = symbol.token.text.to_string();
                //context.insert_error(AnalyzerError::mismatch_function_arity(
                //    &name,
                //    arity,
                //    ret.len(),
                //    &symbol.token.into(),
                //))
            }
        }

        ret
    }

    pub fn to_function_args(
        self,
        context: &mut Context,
        func: &FuncProto,
        token: TokenRange,
    ) -> IrResult<FunctionArgs> {
        let mut inputs: Vec<(VarPath, Expression)> = Vec::new();
        let mut outputs: Vec<(VarPath, Vec<AssignDestination>)> = Vec::new();

        if func.arity != self.len() {
            context.insert_error(AnalyzerError::mismatch_function_arity(
                &func.name.to_string(),
                func.arity,
                self.len(),
                &token,
            ));
            return Err(ir_error!(func.token));
        }

        let mut arg_map_by_name = HashMap::default();
        let mut arg_map_by_index = HashMap::default();
        for (i, arg) in func.args.iter().enumerate() {
            arg_map_by_name.insert(arg.name, arg.clone());
            arg_map_by_index.insert(i, arg.clone());
        }

        let mut connections = vec![];
        match self {
            Arguments::Positional(x) => {
                for (i, (expr, dst, _)) in x.into_iter().enumerate() {
                    if let Some(arg) = arg_map_by_index.get(&i) {
                        connections.push((arg, expr, dst));
                    }
                }
            }
            Arguments::Named(x) => {
                for (name, (expr, dst, _)) in x {
                    if let Some(arg) = arg_map_by_name.get(&name) {
                        connections.push((arg, expr, dst));
                    }
                }
            }
            Arguments::Mixed(_, _) => (),
            Arguments::Null => (),
        };

        for (arg, mut expr, dst) in connections {
            if arg.members.len() == 1
                && !matches!(
                    &arg.comptime.r#type.kind,
                    crate::ir::TypeKind::Modport(_, _)
                )
            {
                let (path, _, direction) = &arg.members[0];
                match direction {
                    Direction::Input => {
                        let arg_type = &arg.comptime.r#type;
                        // Argument expressions are initially converted without a
                        // destination type. Apply the resolved formal shape here,
                        // where the actual-to-formal binding is first available.
                        if matches!(expr, Expression::ArrayLiteral(..)) {
                            eval_array_literal(
                                context,
                                Some(&arg_type.array),
                                Some(arg_type.width()),
                                &mut expr,
                            )?;
                        }
                        // A function input is assignment-like. Propagate the
                        // formal's packed width through the actual expression
                        // before it can be folded, just as an assignment does.
                        // Otherwise `f(2'b11 + 2'b01)` evaluates the addition
                        // at 2 bits even when `f` takes a wider argument.
                        expr.eval_comptime(context, arg_type.total_width());
                        if arg_type.is_clock() || arg_type.is_reset() {
                            let expr_comptime = expr.eval_comptime(context, None);
                            let expr_token = expr_comptime.token;
                            check_implicit_clock_conversion(
                                context,
                                arg_type,
                                expr_comptime,
                                &expr_token,
                            );
                        }
                        inputs.push((path.clone(), expr));
                    }
                    Direction::Output => {
                        let dst = dst
                            .into_iter()
                            .filter_map(|x| x.to_assign_destination(context, false))
                            .collect();
                        outputs.push((path.clone(), dst));
                    }
                    // `inout` is copy-in / copy-out: the actual is read on entry
                    // and written back on return.
                    Direction::Inout => {
                        expr.eval_comptime(context, arg.comptime.r#type.total_width());
                        inputs.push((path.clone(), expr));
                        let dst = dst
                            .into_iter()
                            .filter_map(|x| x.to_assign_destination(context, false))
                            .collect();
                        outputs.push((path.clone(), dst));
                    }
                    _ => (),
                }
            } else {
                let expr_comptime = expr.eval_comptime(context, None);
                let expr_token = expr_comptime.token;

                check_compatibility(context, &arg.comptime.r#type, expr_comptime, &expr_token);

                let actual = &dst[0];
                for x in &arg.members {
                    let arg_path = x.0.clone();
                    let direction = x.2;
                    let mut expr_path = actual.0.clone();
                    expr_path.0.extend_from_slice(&arg_path.0[1..]);
                    let actual_member = VarPathSelect(expr_path, actual.1.clone(), expr_token);

                    match direction {
                        Direction::Input => {
                            let expr = actual_member.to_expression(context);
                            if let Some(expr) = expr {
                                inputs.push((arg_path, expr));
                            }
                        }
                        Direction::Output => {
                            let dst = actual_member.to_assign_destination(context, false);
                            if let Some(dst) = dst {
                                outputs.push((arg_path, vec![dst]));
                            }
                        }
                        Direction::Inout => {
                            if let Some(expr) = actual_member.clone().to_expression(context) {
                                inputs.push((arg_path.clone(), expr));
                            }
                            if let Some(dst) = actual_member.to_assign_destination(context, false) {
                                outputs.push((arg_path, vec![dst]));
                            }
                        }
                        _ => (),
                    }
                }
            }
        }

        Ok((CallArgs(inputs), CallArgs(outputs)))
    }
}