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
// Copyright (c) 2017 Fabian Schuiki

use crate::argument::*;
use crate::block::*;
use crate::inst::*;
use crate::module::ModuleContext;
use crate::seq_body::*;
use crate::ty::*;
use crate::unit::*;
use crate::value::*;

/// A process. Sequentially executes instructions to react to changes in input
/// signals. Implements *control flow* and *timed execution*.
pub struct Process {
    id: ProcessRef,
    global: bool,
    name: String,
    ty: Type,
    ins: Vec<Argument>,
    outs: Vec<Argument>,
    body: SeqBody,
}

impl Process {
    /// Create a new process with the given name and type signature. Anonymous
    /// arguments are created for each input and output in the type signature.
    /// Use the `inputs_mut` and `outputs_mut` functions get a hold of these
    /// arguments and assign names and additional data to them.
    pub fn new(name: impl Into<String>, ty: Type) -> Process {
        let (ins, outs) = {
            let (in_tys, out_tys) = ty.unwrap_entity();
            let to_arg = |t: &Type| Argument::new(t.clone());
            (
                in_tys.iter().map(&to_arg).collect(),
                out_tys.iter().map(&to_arg).collect(),
            )
        };
        Process {
            id: ProcessRef::new(ValueId::alloc()),
            global: true,
            name: name.into(),
            ty: ty,
            ins: ins,
            outs: outs,
            body: SeqBody::new(),
        }
    }

    /// Obtain a reference to this process.
    pub fn as_ref(&self) -> ProcessRef {
        self.id
    }

    /// Get the name of the process.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get a graph reference to one of the inputs of the entity.
    pub fn input(&self, idx: usize) -> ArgumentRef {
        self.ins[idx].as_ref()
    }

    /// Get a reference to the input arguments of the process.
    pub fn inputs(&self) -> &[Argument] {
        &self.ins
    }

    /// Get a mutable reference to the input arguments of the process.
    pub fn inputs_mut(&mut self) -> &mut [Argument] {
        &mut self.ins
    }

    /// Get a graph reference to one of the outputs of the entity.
    pub fn output(&self, idx: usize) -> ArgumentRef {
        self.outs[idx].as_ref()
    }

    /// Get a reference to the output arguments of the process.
    pub fn outputs(&self) -> &[Argument] {
        &self.outs
    }

    /// Get a mutable reference to the output arguments of the process.
    pub fn outputs_mut(&mut self) -> &mut [Argument] {
        &mut self.outs
    }

    /// Get a reference to the sequential body of the process.
    pub fn body(&self) -> &SeqBody {
        &self.body
    }

    /// Get a mutable reference to the sequential body of the process.
    pub fn body_mut(&mut self) -> &mut SeqBody {
        &mut self.body
    }
}

impl Value for Process {
    fn id(&self) -> ValueId {
        self.id.into()
    }

    fn ty(&self) -> Type {
        self.ty.clone()
    }

    fn name(&self) -> Option<&str> {
        Some(&self.name)
    }

    fn is_global(&self) -> bool {
        self.global
    }
}

pub struct ProcessContext<'tctx> {
    module: &'tctx ModuleContext<'tctx>,
    process: &'tctx Process,
}

impl<'tctx> ProcessContext<'tctx> {
    pub fn new(module: &'tctx ModuleContext, process: &'tctx Process) -> ProcessContext<'tctx> {
        ProcessContext {
            module: module,
            process: process,
        }
    }
}

impl<'tctx> Context for ProcessContext<'tctx> {
    fn parent(&self) -> Option<&Context> {
        Some(self.module.as_context())
    }

    fn try_value(&self, value: &ValueRef) -> Option<&Value> {
        match *value {
            ValueRef::Inst(id) => Some(self.inst(id)),
            ValueRef::Block(id) => Some(self.block(id)),
            ValueRef::Argument(id) => Some(self.argument(id)),
            _ => None,
        }
    }
}

impl<'tctx> UnitContext for ProcessContext<'tctx> {
    fn inst(&self, inst: InstRef) -> &Inst {
        self.process.body.inst(inst)
    }

    fn argument(&self, argument: ArgumentRef) -> &Argument {
        self.process
            .ins
            .iter()
            .chain(self.process.outs.iter())
            .find(|x| argument == x.as_ref())
            .unwrap()
    }
}

impl<'tctx> SequentialContext for ProcessContext<'tctx> {
    fn block(&self, block: BlockRef) -> &Block {
        self.process.body.block(block)
    }
}