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
use crate::argument::*;
use crate::inst::*;
use crate::module::ModuleContext;
use std;
use std::collections::HashMap;
use crate::ty::*;
use crate::unit::*;
use crate::util::IndirectMapIter;
use crate::value::*;
pub struct Entity {
id: EntityRef,
global: bool,
name: String,
ty: Type,
ins: Vec<Argument>,
outs: Vec<Argument>,
insts: HashMap<InstRef, Inst>,
inst_seq: Vec<InstRef>,
}
impl Entity {
pub fn new(name: impl Into<String>, ty: Type) -> Entity {
let (ins, outs) = {
let (in_tys, out_tys) = ty.as_entity();
let to_arg = |t: &Type| Argument::new(t.clone());
(
in_tys.iter().map(&to_arg).collect(),
out_tys.iter().map(&to_arg).collect(),
)
};
Entity {
id: EntityRef::new(ValueId::alloc()),
global: true,
name: name.into(),
ty: ty,
ins: ins,
outs: outs,
insts: HashMap::new(),
inst_seq: Vec::new(),
}
}
pub fn as_ref(&self) -> EntityRef {
self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn input(&self, idx: usize) -> ArgumentRef {
self.ins[idx].as_ref()
}
pub fn inputs(&self) -> &[Argument] {
&self.ins
}
pub fn inputs_mut(&mut self) -> &mut [Argument] {
&mut self.ins
}
pub fn output(&self, idx: usize) -> ArgumentRef {
self.outs[idx].as_ref()
}
pub fn outputs(&self) -> &[Argument] {
&self.outs
}
pub fn outputs_mut(&mut self) -> &mut [Argument] {
&mut self.outs
}
pub fn add_inst(&mut self, inst: Inst, pos: InstPosition) -> InstRef {
let ir = inst.as_ref();
self.insts.insert(ir, inst);
self.insert_inst(ir, pos);
ir
}
pub fn move_inst(&mut self, inst: InstRef, pos: InstPosition) {
self.detach_inst(inst);
self.insert_inst(inst, pos);
}
pub fn remove_inst(&mut self, inst: InstRef) {
self.detach_inst(inst);
self.insts.remove(&inst);
}
fn insert_inst(&mut self, inst: InstRef, pos: InstPosition) {
let index = match pos {
InstPosition::Begin => 0,
InstPosition::End => self.inst_seq.len(),
InstPosition::Before(i) => self.inst_pos(i),
InstPosition::After(i) => self.inst_pos(i) + 1,
InstPosition::BlockBegin(_) | InstPosition::BlockEnd(_) => {
panic!("entity has no blocks")
}
};
self.inst_seq.insert(index, inst);
}
fn detach_inst(&mut self, inst: InstRef) {
let pos = self.inst_pos(inst);
self.inst_seq.remove(pos);
}
fn inst_pos(&self, inst: InstRef) -> usize {
self.inst_seq
.iter()
.position(|&i| i == inst)
.expect("entity does not contain inst")
}
pub fn inst(&self, inst: InstRef) -> &Inst {
self.insts.get(&inst).unwrap()
}
pub fn inst_mut(&mut self, inst: InstRef) -> &mut Inst {
self.insts.get_mut(&inst).unwrap()
}
pub fn insts(&self) -> IndirectMapIter<std::slice::Iter<InstRef>, Inst> {
IndirectMapIter::new(self.inst_seq.iter(), &self.insts)
}
pub fn inst_refs(&self) -> std::slice::Iter<InstRef> {
self.inst_seq.iter()
}
}
impl Value for Entity {
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 EntityContext<'tctx> {
module: &'tctx ModuleContext<'tctx>,
entity: &'tctx Entity,
}
impl<'tctx> EntityContext<'tctx> {
pub fn new(module: &'tctx ModuleContext, entity: &'tctx Entity) -> EntityContext<'tctx> {
EntityContext {
module: module,
entity: entity,
}
}
}
impl<'tctx> Context for EntityContext<'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(_) => panic!("entity has no blocks"),
ValueRef::Argument(id) => Some(self.argument(id)),
_ => None,
}
}
}
impl<'tctx> UnitContext for EntityContext<'tctx> {
fn inst(&self, inst: InstRef) -> &Inst {
self.entity.inst(inst)
}
fn argument(&self, argument: ArgumentRef) -> &Argument {
self.entity
.ins
.iter()
.chain(self.entity.outs.iter())
.find(|x| argument == x.as_ref())
.unwrap()
}
}