cmtir 0.1.2

The intermediate representation for Cement (cmt2) languages and compiler tools.
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//! IR structure (module, function, circuit).

pub use super::*;
pub use json;

/// io bindings for external modules.
#[derive(Default, Debug, Clone, SExpr)]
pub struct InOutBindings {
  pub inputs: Vec<String>,
  pub outputs: Vec<String>,
}

/// FIRRTL string in external modules.
#[derive(Default, Debug, Clone)]
pub struct FirString {
  pub inner: String,
}

impl Parse for FirString {
  fn parse(p: &mut Parser) -> Result<Self, String> {
    Ok(FirString {
      inner: p.expect(Token::MultiLineStr)?.to_string(),
    })
  }
}

impl Print for FirString {
  fn print(&self, p: &mut Printer) {
    write!(p, "\n{}", self.inner);
  }
}

/// Body of external modules: bindings, clock, reset, and FIRRTL string.
#[derive(Default, Debug, Clone, SExpr)]
pub struct ExtModuleBody {
  #[pp(surrounded = "bindings")]
  pub bindings: InOutBindings,
  #[pp(surrounded = "clock")]
  pub clock: Option<String>,
  #[pp(surrounded = "reset")]
  pub reset: Option<String>,
  #[pp(surrounded = "fir")]
  pub fir_str: FirString,
}

impl ExtModuleBody {
  /// Create an `ExtModuleBody` with bindings, clock, reset, and FIRRTL string.
  pub fn new(
    inputs: Vec<String>,
    outputs: Vec<String>,
    clock: Option<String>,
    reset: Option<String>,
    fir_str: String,
  ) -> Self {
    ExtModuleBody {
      bindings: InOutBindings { inputs, outputs },
      clock,
      reset,
      fir_str: FirString { inner: fir_str },
    }
  }
  /// Create an `ExtModuleBody` with only FIRRTL string.
  pub fn with_firrtl(fir_str: String) -> Self {
    ExtModuleBody {
      fir_str: FirString { inner: fir_str },
      ..Default::default()
    }
  }
}

/// Body of native modules: instances.
#[derive(Default, Debug, Clone, SExpr)]
pub struct NativeModuleBody {
  #[pp(list_ml = "instances")]
  pub instances: Vec<InstDef>,
}

/// Body of modules: external modules or native modules or placeholders (for
/// virtual modules).
#[derive(Debug, Clone, SExpr)]
pub enum ModuleBody {
  #[pp(surrounded)]
  #[pp(indent = 1)]
  Ext(ExtModuleBody),
  Native(NativeModuleBody),
  PlaceHolder,
}

impl Default for ModuleBody {
  fn default() -> Self {
    ModuleBody::Native(NativeModuleBody { instances: vec![] })
  }
}

impl ModuleBody {
  /// Get the native module body or panic if it is not a native module.
  pub fn as_native(&self) -> &NativeModuleBody {
    match self {
      ModuleBody::Native(body) => body,
      _ => panic!("ModuleBody is not native"),
    }
  }
  /// Get the native module body or panic if it is not a native module.
  pub fn as_native_mut(&mut self) -> &mut NativeModuleBody {
    match self {
      ModuleBody::Native(body) => body,
      _ => panic!("ModuleBody is not native"),
    }
  }
  /// Get the external module body or panic if it is not an external module.
  pub fn as_ext(&self) -> &ExtModuleBody {
    match self {
      ModuleBody::Ext(body) => body,
      _ => panic!("ModuleBody is not ext"),
    }
  }
  /// Get the external module body or panic if it is not an external module.
  pub fn as_ext_mut(&mut self) -> &mut ExtModuleBody {
    match self {
      ModuleBody::Ext(body) => body,
      _ => panic!("ModuleBody is not ext"),
    }
  }
}

/// A module in the IR.
/// (module "add2_2stage" (
///   (annotations <json>)
///   (inputs (<list of value ids>))
///   (outputs (<list of value ids>))
///   (wires (<list of value ids>))
///   (native/ext/placeholder <body>)
///   (rules
///     (<rule>)*
///   )
///   (rule_rels
///     (<rule_rel>)*
///   )
/// ))
#[derive(Debug, Clone, SExpr)]
pub struct Module {
  #[pp(open)]
  #[pp(kw = "module")]
  pub name: String,
  #[pp(map)]
  /// A map from value id to value (name, type).
  pub values: SlotMap<ValueId, Value>,
  #[pp(open = 1)]
  #[pp(surrounded = "annotations")]
  pub annotations: json::object::Object,
  #[pp(nl)]
  #[pp(surrounded = "inputs")]
  pub inputs: Vec<ValueId>,
  #[pp(nl)]
  #[pp(surrounded = "outputs")]
  pub outputs: Vec<ValueId>,
  #[pp(nl)]
  #[pp(surrounded = "wires")]
  pub wires: Vec<ValueId>,
  #[pp(nl)]
  pub body: ModuleBody,
  #[pp(nl)]
  #[pp(list_ml = "rules")]
  pub rules: Vec<Rule>,
  #[pp(nl)]
  #[pp(list_ml = "rule_rels")]
  #[pp(close = -1)]
  #[pp(close)]
  pub rule_rels: Vec<RuleRel>,
}

impl Module {
  /// Get the name of the module.
  pub fn name(&self) -> &str {
    &self.name
  }
  /// Create a new module with the given name.
  pub fn with_name(name: String) -> Self {
    Module {
      name,
      values: ValueMap::with_key(),
      annotations: json::object::Object::new(),
      inputs: vec![],
      outputs: vec![],
      wires: vec![],
      body: Default::default(),
      rule_rels: vec![],
      rules: vec![],
    }
  }
  /// Get the spans of the module from annotations.
  pub fn spans(&self) -> impl Iterator<Item = MySpan> + '_ {
    self.annotations.iter().filter_map(|(k, v)| {
      if k.ends_with("span") {
        MySpan::from_json(v)
      } else {
        None
      }
    })
  }
  /// Check if the module has an annotation with the given key and value is true.
  pub fn annotation_is_true(&self, key: &str) -> bool {
    // log::info!("module {}'s annotation has key {}? {}", self.name, key,
    // self.annotations.get(key).is_some());
    self.annotations.get(key).map_or(false, |v| {
      // convert v to bool
      // log::info!("value is {}", v.to_string());
      match v {
        json::JsonValue::Boolean(b) => *b,
        json::JsonValue::Short(n) => {
          // log::info!("value {} == true? {}", n, n.as_str() == "true");
          n.as_str() == "true"
        }
        json::JsonValue::String(s) => {
          // log::info!("value {} == true? {}", s, s == "true");
          s == "true"
        }
        _ => false,
      }
    })
  }
  /// Check if the module has an annotation with the given key and value is false.
  pub fn annotation_is_false(&self, key: &str) -> bool {
    !self.annotation_is_true(key)
  }
  /// Set the module as top module.
  pub fn as_top(mut self) -> Self {
    self.add_annotation("is_top".to_string(), "true".to_string());
    self
  }
  /// Set the module as top module. 
  pub fn set_top(&mut self) {
    self.add_annotation("is_top".to_string(), "true".to_string());
  }
  /// Unset the module as top module.
  pub fn unset_top(&mut self) {
    self.annotations.remove("is_top");
  }
  /// Check if the module is top module.
  pub fn is_top(&self) -> bool {
    self.annotation_is_true("is_top")
  }
  /// Check if the module is testbench.
  pub fn is_tb(&self) -> bool {
    self.annotation_is_true("is_tb")
  }
  /// Set the module as testbench.
  pub fn set_tb(&mut self) {
    self.add_annotation("is_tb".to_string(), "true".to_string());
  }
  /// Unset the module as testbench.
  pub fn unset_tb(&mut self) {
    self.annotations.remove("is_tb");
  }
  /// Set the module as synthesized.
  pub fn set_synthesis(&mut self) {
    self.add_annotation("synthesis".to_string(), "true".to_string());
  }
  /// Check if the module is synthesized.
  pub fn to_be_synthesize(&self) -> bool {
    !self.is_tb()
      && !self.is_virtual()
      && (self.is_top()
        || self.is_ext()
        || self.annotation_is_true("synthesis"))
  }

  /// Get all instances in the module.
  pub fn instances(&self) -> Vec<&InstDef> {
    match &self.body {
      ModuleBody::Native(body) => body.instances.iter().collect(),
      _ => vec![],
    }
  }
  /// Get the instance definition by name.
  pub fn instance_by_name(&self, name: &str) -> Option<&InstDef> {
    self
      .body
      .as_native()
      .instances
      .iter()
      .find(|inst| inst.name == name)
  }
  /// Get the instance definition (mut) by name.
  pub fn instance_by_name_mut(&mut self, name: &str) -> Option<&mut InstDef> {
    self
      .body
      .as_native_mut()
      .instances
      .iter_mut()
      .find(|inst| inst.name == name)
  }
  /// Get all instances in the module (mut).
  pub fn instances_mut(&mut self) -> &mut Vec<InstDef> {
    self.body.as_native_mut().instances.as_mut()
  }
  /// Add an instance to the module.
  pub fn add_instance(&mut self, name: String, module: String) {
    self
      .body
      .as_native_mut()
      .instances
      .push(InstDef { name, module });
  }
  /// Add a value to the module.
  pub fn add_value(
    &mut self,
    name: Option<String>,
    typ: Option<Type>,
  ) -> ValueId {
    self.values.insert(Value { ty: typ, name })
  }
  /// Add an annotation (key, value) to the module.
  pub fn add_annotation(&mut self, key: String, value: String) {
    self
      .annotations
      .insert(&key, json::JsonValue::Boolean(value == "true"));
  }
  /// Get all rules in the module (mut).
  pub fn rules_mut(&mut self) -> &mut Vec<Rule> {
    self.rules.as_mut()
  }
  /// Get all rules in the module.
  pub fn rules(&self) -> impl Iterator<Item = &Rule> {
    self.rules.iter()
  }
  /// Get all always rules in the module.
  pub fn always_rules(&self) -> impl Iterator<Item = &Rule> {
    self.rules.iter().filter(|r| r.is_always())
  }
  /// Get all method rules in the module.
  pub fn method_rules(&self) -> impl Iterator<Item = &Rule> {
    self.rules.iter().filter(|r| r.is_method())
  }
  /// Get the rule by name.
  pub fn rule_by_name(&self, name: &str) -> Option<&Rule> {
    self.rules.iter().find(|r| r.name == name)
  }
  /// Add a rule to the module.
  pub fn add_rule(&mut self, rule: Rule) {
    self.rules.push(rule);
  }
  /// Get all rule relations in the module (mut).
  pub fn rule_rels_mut(&mut self) -> &mut Vec<RuleRel> {
    self.rule_rels.as_mut()
  }
  /// Get the type of the value by id.
  pub fn value_typ(&self, id: ValueId) -> Option<&Type> {
    self.values.get(id).and_then(|v| v.ty.as_ref())
  }
  /// Add a rule relation to the module.
  pub fn add_rule_rel(&mut self, rule_rel: RuleRel) {
    self.rule_rels.push(rule_rel);
  }
  /// Add an input to the module.
  pub fn add_input(&mut self, id: ValueId) {
    self.inputs.push(id);
  }
  /// Add an output to the module.
  pub fn add_output(&mut self, id: ValueId) {
    self.outputs.push(id);
  }
  /// Add a wire to the module.
  pub fn add_wire(&mut self, id: ValueId) {
    self.wires.push(id);
  }
  /// Add FIRRTL string to the module.
  pub fn add_firrtl(&mut self, firrtl: String) {
    self.body.as_ext_mut().fir_str.inner = firrtl;
  }
  /// Get the FIRRTL string of the module.
  pub fn firrtl(&self) -> String {
    self.body.as_ext().fir_str.inner.clone()
  }
  /// Set the module as external module.
  pub fn with_ext_body(&mut self, body: ExtModuleBody) {
    self.body = ModuleBody::Ext(body);
  }
  /// Set the module as placeholder.
  pub fn with_placeholder(&mut self) {
    self.body = ModuleBody::PlaceHolder;
  }
  /// Set the module as default external module body.
  pub fn with_default_ext_body(&mut self) {
    let body = ExtModuleBody::default();
    self.with_ext_body(body)
  }
  /// Add a bind input to the module.
  pub fn add_bind_input(&mut self, input: String) {
    self.body.as_ext_mut().bindings.inputs.push(input);
  }
  /// Get all inputs with binding.
  pub fn inputs_with_binding(
    &self,
  ) -> impl Iterator<Item = (ValueId, String)> + use<'_> {
    self
      .inputs
      .iter()
      .cloned()
      .zip(self.body.as_ext().bindings.inputs.iter().cloned())
  }
  /// Add a bind output to the module.
  pub fn add_bind_output(&mut self, output: String) {
    self.body.as_ext_mut().bindings.outputs.push(output);
  }
  /// Get all outputs with binding.
  pub fn outputs_with_binding(
    &self,
  ) -> impl Iterator<Item = (ValueId, String)> + use<'_> {
    self
      .outputs
      .iter()
      .cloned()
      .zip(self.body.as_ext().bindings.outputs.iter().cloned())
  }

  /// Add a bind clock to the module. 
  pub fn add_bind_clock(&mut self, clock: String) {
    self.body.as_ext_mut().clock = Some(clock);
  }
  /// Add an external rule to the module.
  pub fn add_ext_rule(&mut self, rule: Rule) {
    assert!((self.is_ext() || self.is_virtual()) && rule.is_ext());
    self.rules.push(rule);
  }
  /// Check if the module is external module.
  pub fn is_ext(&self) -> bool {
    matches!(self.body, ModuleBody::Ext(_))
  }
  /// Check if the module is virtual module.
  pub fn is_virtual(&self) -> bool {
    matches!(self.body, ModuleBody::PlaceHolder)
      || self.annotation_is_true("is_virtual")
  }
  /// Check if the module is native module.
  pub fn is_native(&self) -> bool {
    matches!(self.body, ModuleBody::Native(_))
  }
  /// Get all rule relations in the module.
  pub fn rule_rels(&self) -> impl Iterator<Item = &RuleRel> {
    self.rule_rels.iter()
  }
  /// Clear all rule relations in the module.
  pub fn clear_rule_sched_orders(&mut self) {
    self
      .rule_rels
      .retain(|rel| !matches!(rel, RuleRel::Schedule { .. }));
  }
  /// Add a schedule to the module.
  pub fn add_schedule(&mut self, rules: Vec<InstRule>) {
    self.rule_rels.push(RuleRel::Schedule(rules));
  }
  /// Get all schedules in the module.
  pub fn schedules(&self) -> impl Iterator<Item = Vec<InstRule>> + '_ {
    self.rule_rels.iter().filter_map(|rel| {
      if let RuleRel::Schedule(rules) = rel {
        Some(rules.iter().map(|r| r.canonicalize()).collect())
      } else {
        None
      }
    })
  }
  /// Add a method relation to the module.
  pub fn add_method_rel(
    &mut self,
    rel: MethodRel,
    lhs: Vec<InstRule>,
    rhs: Vec<InstRule>,
  ) {
    self.rule_rels.push(RuleRel::Method { rel, lhs, rhs });
  }
  /// Get all method relations in the module.
  pub fn method_rels(&self) -> impl Iterator<Item = &RuleRel> {
    self
      .rule_rels
      .iter()
      .filter(|rel| matches!(rel, RuleRel::Method { .. }))
  }
  /// Check if the module has an input by id.
  pub fn has_input(&self, id: ValueId) -> bool {
    self.inputs.contains(&id)
  }
  /// Get all inputs in the module.
  pub fn inputs(&self) -> impl Iterator<Item = ValueId> + '_ {
    self.inputs.iter().cloned()
  }
  /// Check if the module has an output by id.
  pub fn has_output(&self, id: ValueId) -> bool {
    self.outputs.contains(&id)
  }
  /// Get all outputs in the module.
  pub fn outputs(&self) -> impl Iterator<Item = ValueId> + '_ {
    self.outputs.iter().cloned()
  }
  /// Check if the module has a wire by id.
  pub fn wires(&self) -> impl Iterator<Item = ValueId> + '_ {
    self.wires.iter().cloned()
  }
  /// Clone a value and add it to the module.
  pub fn clone_value(&mut self, id: ValueId, suffix: &str) -> ValueId {
    if let Some(value) = self.values.get(id) {
      self.add_value(
        value.name.clone().map(|n| format!("{}_{}", n, suffix)),
        value.ty.clone(),
      )
    } else {
      panic!("value {} not found", id);
    }
  }
  /// Get the external module body of the module.
  pub fn ext_body(&self) -> Option<ExtModuleBody> {
    if self.is_ext() {
      Some(self.body.as_ext().clone())
    } else {
      None
    }
  }
}

/// Function in `cmtir`. 
/// **Not supported in `cmtrs` frontend yet.**
#[derive(Default, Debug, Clone, SExpr)]
pub struct Function {
  #[pp(open)]
  #[pp(kw = "function")]
  pub name: String,
  #[pp(map)]
  pub values: SlotMap<ValueId, Value>,
  #[pp(open = 1)]
  #[pp(surrounded = "inputs")]
  pub inputs: Vec<ValueId>,
  #[pp(nl)]
  #[pp(surrounded = "outputs")]
  pub outputs: Vec<Option<Type>>,
  #[pp(nl)]
  #[pp(list_ml = "instances")]
  pub instances: Vec<InstDef>,
  #[pp(nl)]
  #[pp(list_ml)]
  #[pp(close = -1)]
  #[pp(close)]
  pub ops: Vec<Op>,
}

impl Function {
  pub fn name(&self) -> &str {
    &self.name
  }

  pub fn with_name(name: String) -> Self {
    Function {
      name,
      values: ValueMap::with_key(),
      inputs: vec![],
      instances: vec![],
      outputs: vec![],
      ops: vec![],
    }
  }

  pub fn add_value(
    &mut self,
    name: Option<String>,
    typ: Option<Type>,
  ) -> ValueId {
    self.values.insert(Value { ty: typ, name })
  }

  pub fn add_input(&mut self, id: ValueId) {
    self.inputs.push(id);
  }

  pub fn add_output(&mut self, ty: Option<Type>) {
    self.outputs.push(ty);
  }

  pub fn add_instance(&mut self, name: String, module: String) {
    self.instances.push(InstDef { name, module });
  }

  pub fn value_typ(&self, id: ValueId) -> Option<&Type> {
    self.values.get(id).and_then(|v| v.ty.as_ref())
  }

  pub fn ops(&self) -> impl Iterator<Item = &Op> {
    self.ops.iter()
  }

  pub fn ops_mut(&mut self) -> impl Iterator<Item = &mut Op> {
    self.ops.iter_mut()
  }

  pub fn replace_op(&mut self, target_op: &Op, new_ops: Vec<Op>) {
    let mut indices_to_replace = vec![];
    for (index, op) in self.ops.iter().enumerate() {
      if op == target_op {
        indices_to_replace.push(index);
      }
    }

    // Replace each matching operation
    for index in indices_to_replace.into_iter().rev() {
      let ops = new_ops.clone();
      self.ops.splice(index..=index, ops);
    }

    // Recursively replace sub-operations
    for op in &mut self.ops {
      op.replace_op(target_op, new_ops.clone());
    }
  }

  pub fn remove_unused_op(&mut self) {
    let mut indices_to_remove = vec![];

    for (index, op) in self.ops_mut().enumerate() {
      if let OpEnum::Assign(AssignOp { res, value }) = op.inner_mut() {
        if res == value {
          indices_to_remove.push(index);
        }
      } else {
        op.remove_unused_op();
      }
    }

    indices_to_remove.sort_unstable_by(|a, b| b.cmp(a));
    for &index in &indices_to_remove {
      self.ops.remove(index);
    }
  }
}

/// Circuit in `cmtir`.
/// (circuit <top_module_name> {<annotations>} (<functions>*) (<modules>*)
#[derive(Debug, Clone, SExpr)]
pub struct Circuit {
  #[pp(open)]
  #[pp(kw = "circuit")]
  /// circuit name = name of the top module.
  pub top_module_name: String,
  pub annotations: json::object::Object,
  #[pp(nl)]
  #[pp(list_ml)]
  pub functions: Vec<Function>,
  #[pp(list_ml)]
  #[pp(close)]
  pub modules: Vec<Module>,
}

impl Circuit {
  /// Create a circuit with the given top module name.
  pub fn with_top_module_name(name: String) -> Self {
    Circuit {
      top_module_name: name,
      annotations: json::object::Object::new(),
      modules: vec![],
      functions: vec![],
    }
  }
  /// Create a circuit with the given top module name and modules.
  pub fn new(name: String, modules: Vec<Module>) -> Self {
    Circuit {
      top_module_name: name,
      annotations: json::object::Object::new(),
      modules,
      functions: vec![],
    }
  }
  /// Get the top module of the circuit.
  pub fn top_module(&self) -> &Module {
    self
      .modules
      .iter()
      .find(|m| m.name == self.top_module_name)
      .unwrap()
  }

  /// Get all module names in the circuit.
  pub fn module_names(&self) -> impl Iterator<Item = &str> {
    self.modules.iter().map(|m| m.name.as_str())
  }

  /// Get all modules in the circuit.
  pub fn modules(&self) -> impl Iterator<Item = &Module> {
    self.modules.iter()
  }

  /// Get all modules in the circuit (mut).
  pub fn modules_mut(&mut self) -> impl Iterator<Item = &mut Module> {
    self.modules.iter_mut()
  }

  /// Get the index of the module by name.
  pub fn module_idx(&self, name: &str) -> usize {
    let module_names = self
      .modules
      .iter()
      .map(|m| m.name.clone())
      .collect::<Vec<String>>()
      .join(", ");
    self
      .modules
      .iter()
      .position(|m| m.name == name)
      .expect(format!("module {} not found in {}", name, module_names).as_str())
  }

  /// Get the module by name (mut).
  pub fn module_mut(&mut self, name: &str) -> &mut Module {
    let module_names = self
      .modules
      .iter()
      .map(|m| m.name.clone())
      .collect::<Vec<String>>()
      .join(", ");
    if let Some(module) = self.modules.iter_mut().find(|m| m.name == name) {
      module
    } else {
      log::error!("there are modules: {}", module_names);
      panic!("module {} not found", name);
    }
  }

  /// Get the module by name.
  pub fn module(&self, name: &str) -> Option<&Module> {
    self.modules.iter().find(|m| m.name == name)
  }

  /// Add a module to the circuit.
  pub fn add_module(&mut self, module: Module) -> String {
    let name = module.name().to_owned();
    if self.module(module.name()).is_none() {
      self.modules.push(module);
    }
    name
    // TODO: do we need to check if two modules of the same name are identical?
  }

  /// Get all function names in the circuit.
  pub fn function_names(&self) -> impl Iterator<Item = &str> {
    self.functions.iter().map(|f| f.name.as_str())
  }

  /// Add a function to the circuit.
  pub fn add_function(&mut self, function: Function) {
    self.functions.push(function);
  }

  /// Get all functions in the circuit.
  pub fn functions(&self) -> impl Iterator<Item = &Function> {
    self.functions.iter()
  }

  /// Get all functions in the circuit (mut).
  pub fn functions_mut(&mut self) -> impl Iterator<Item = &mut Function> {
    self.functions.iter_mut()
  }

  /// Get the index of the function by name.
  pub fn function_idx(&self, name: &str) -> usize {
    let function_names = self
      .functions
      .iter()
      .map(|m| m.name.clone())
      .collect::<Vec<String>>()
      .join(", ");
    self.functions.iter().position(|f| f.name == name).expect(
      format!("module {} not found in {}", name, function_names).as_str(),
    )
  }

  /// Get the output types of the rule in the module.
  pub fn rule_output_types(
    &self,
    module_name: &str,
    rule_name: &str,
  ) -> Result<Vec<Type>, String> {
    let module = self
      .module(module_name)
      .ok_or(format!("module {} not found", module_name))?;
    let rule = match module.rules().find(|r| r.name == rule_name) {
      Some(r) => r,
      None => return Ok(vec![]),
    };
    let outputs = rule.outputs();
    let output_types = outputs
      .iter()
      .map(|id| {
        module
          .value_typ(*id)
          .map(|t| t.clone())
          .ok_or(format!("value {} not found in module {}", id, module_name))
      })
      .collect::<Result<Vec<Type>, String>>()?;
    Ok(output_types)
  }
}

impl<T> From<T> for Circuit
where
  T: AsRef<str>,
{
  fn from(value: T) -> Self {
    let mut parser = Parser::new(value.as_ref());
    let circuit = parser.parse::<Circuit>().unwrap();
    circuit
  }
}