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
use common::Context;
use ty;
use std::fmt::{self, Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::cell::{Cell, RefCell};

pub type FuncContext<'c> = Context<Function<'c>>;

pub struct Function<'c> {
  pub name: String,
  pub ty: ty::Function<'c>,
  pub blocks: BlockContext<'c>,
  pub values: ValueContext<'c>,
}

impl<'c> Function<'c> {
  pub fn add_block(&'c self) -> &'c Block<'c> {
    self.blocks.push(
      Block {
        number: self.blocks.len() as u32,
        terminator: Cell::new(Terminator::None),
        block_values: RefCell::new(vec![]),
        func: self,
      })
  }

  #[inline(always)]
  pub fn ty(&self) -> &ty::Function<'c> {
    &self.ty
  }
}

impl<'c> Display for Function<'c> {
  fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
    try!(writeln!(f, "define {}{} {{", self.name, self.ty));
    for blk in &self.blocks {
      try!(write!(f, "{:?}", blk));
    }
    write!(f, "}}")
  }
}

impl<'c> PartialEq for Function<'c> {
  fn eq(&self, rhs: &Self) -> bool {
    self.name == rhs.name
  }
}
impl<'c> Eq for Function<'c> { }
impl<'c> Hash for Function<'c> {
  fn hash<H>(&self, state: &mut H) where H: Hasher {
    self.name.hash(state)
  }
}

pub type BlockContext<'c> = Context<Block<'c>>;

#[derive(Copy, Clone)]
pub enum Terminator<'c> {
  Branch(&'c Block<'c>),
  // final return in a function
  Return(&'c Value<'c>),
  None,
}

impl<'c> Terminator<'c> {
  pub fn is_none(self) -> bool {
    if let Terminator::None = self {
      true
    } else {
      false
    }
  }
}

impl<'c> Display for Terminator<'c> {
  fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
    match *self {
      Terminator::Branch(b) => {
        write!(f, "branch {}", b)
      },
      Terminator::Return(r) => {
        write!(f, "return {}", r)
      }
      Terminator::None => { Ok(()) }
    }
  }
}

// TODO(ubsan): don't allow terminators to be re-set, and stop allowing stuff to
// build after setting terminator
// .build_return, .build_branch, etc.
pub struct Block<'c> {
  pub number: u32,
  pub terminator: Cell<Terminator<'c>>,
  pub block_values: RefCell<Vec<&'c Value<'c>>>,
  pub func: &'c Function<'c>,
}

impl<'c> Block<'c> {
  pub fn add_value(&'c self, kind: ValueKind<'c>) -> &'c Value<'c> {
    let ret = self.func.values.push(
      Value {
        number: self.func.values.len() as u32,
        kind: kind,
        func: &self.func,
      });
    self.block_values.borrow_mut().push(ret);
    ret
  }
}

impl<'c> Debug for Block<'c> {
  fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
    try!(writeln!(f, "{}:", self));
    for value in &*self.block_values.borrow() {
      try!(writeln!(f, "  {}: {} = {:?}", value, value.ty(), value));
    }
    try!(writeln!(f, "  {}", self.terminator.get()));
    Ok(())
  }
}

impl<'c> Display for Block<'c> {
  fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
    write!(f, "bb{}", self.number)
  }
}

pub type ValueContext<'c> = Context<Value<'c>>;

pub struct Value<'c> {
  pub number: u32,
  pub kind: ValueKind<'c>,
  pub func: &'c Function<'c>,
}
impl<'c> Value<'c> {
  pub fn ty(&self) -> &'c ty::Type {
    match self.kind {
      ValueKind::ConstInt {
        ty,
        ..
      } => ty,
      ValueKind::Call {
        function,
        ..
      } => function.ty.output,
      ValueKind::Mul(lhs, _) => lhs.ty(),
      ValueKind::UDiv(lhs, _) => lhs.ty(),
      ValueKind::SDiv(lhs, _) => lhs.ty(),
      ValueKind::URem(lhs, _) => lhs.ty(),
      ValueKind::SRem(lhs, _) => lhs.ty(),

      ValueKind::Add(lhs, _) => lhs.ty(),
      ValueKind::Sub(lhs, _) => lhs.ty(),

      ValueKind::Shl(lhs, _) => lhs.ty(),
      ValueKind::ZShr(lhs, _) => lhs.ty(),
      ValueKind::SShr(lhs, _) => lhs.ty(),

      ValueKind::And(lhs, _) => lhs.ty(),
      ValueKind::Xor(lhs, _) => lhs.ty(),
      ValueKind::Or(lhs, _) => lhs.ty(),

      ValueKind::Eq(_, _) => unimplemented!(),
      ValueKind::Neq(_, _) => unimplemented!(),
      ValueKind::Lt(_, _) => unimplemented!(),
      ValueKind::Gt(_, _) => unimplemented!(),
      ValueKind::Lte(_, _) => unimplemented!(),
      ValueKind::Gte(_, _) => unimplemented!(),
      ValueKind::Parameter(ty) => ty,
    }
  }

}

pub enum ValueKind<'c> {
  ConstInt {
    ty: &'c ty::Type,
    value: u64,
  },
  Call {
    function: &'c Function<'c>,
    parameters: Box<[&'c Value<'c>]>
  },

  // -- binops --
  Mul(&'c Value<'c>, &'c Value<'c>),
  UDiv(&'c Value<'c>, &'c Value<'c>),
  SDiv(&'c Value<'c>, &'c Value<'c>),
  URem(&'c Value<'c>, &'c Value<'c>),
  SRem(&'c Value<'c>, &'c Value<'c>),

  Add(&'c Value<'c>, &'c Value<'c>),
  Sub(&'c Value<'c>, &'c Value<'c>),

  Shl(&'c Value<'c>, &'c Value<'c>),
  ZShr(&'c Value<'c>, &'c Value<'c>), // zero-extend
  SShr(&'c Value<'c>, &'c Value<'c>), // sign-extend

  And(&'c Value<'c>, &'c Value<'c>),
  Xor(&'c Value<'c>, &'c Value<'c>),
  Or(&'c Value<'c>, &'c Value<'c>),

  Eq(&'c Value<'c>, &'c Value<'c>),
  Neq(&'c Value<'c>, &'c Value<'c>),
  Lt(&'c Value<'c>, &'c Value<'c>),
  Gt(&'c Value<'c>, &'c Value<'c>),
  Lte(&'c Value<'c>, &'c Value<'c>),
  Gte(&'c Value<'c>, &'c Value<'c>),

  // parameter (this *may not* be built; it's simply a placeholder)
  Parameter(&'c ty::Type),
}

impl<'c> Debug for Value<'c> {
  fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
    match self.kind {
      ValueKind::ConstInt {
        value,
        ..
      } => {
        try!(write!(f, "{}", value));
      }
      ValueKind::Call {
        function,
        ref parameters
      } => {
        try!(write!(f, "call {}(", function.name));
        if !parameters.is_empty() {
          for i in 0..parameters.len() - 1 {
            try!(write!(f, "%{}, ", i));
          }
          try!(write!(f, "%{}", parameters.len() - 1));
        }
        try!(write!(f, ")"));
      }
      ValueKind::Mul(lhs, rhs) => try!(write!(f, "mul {} {}", lhs, rhs)),
      ValueKind::UDiv(lhs, rhs) => try!(write!(f, "udiv {} {}", lhs, rhs)),
      ValueKind::SDiv(lhs, rhs) => try!(write!(f, "sdiv {} {}", lhs, rhs)),
      ValueKind::URem(lhs, rhs) => try!(write!(f, "urem {} {}", lhs, rhs)),
      ValueKind::SRem(lhs, rhs) => try!(write!(f, "srem {} {}", lhs, rhs)),

      ValueKind::Add(lhs, rhs) => try!(write!(f, "add {} {}", lhs, rhs)),
      ValueKind::Sub(lhs, rhs) => try!(write!(f, "sub {} {}", lhs, rhs)),

      ValueKind::Shl(lhs, rhs) => try!(write!(f, "shl {} {}", lhs, rhs)),
      ValueKind::ZShr(lhs, rhs) => try!(write!(f, "zshr {} {}", lhs, rhs)), // zero-extend
      ValueKind::SShr(lhs, rhs) => try!(write!(f, "sshr {} {}", lhs, rhs)), // sign-extend

      ValueKind::And(lhs, rhs) => try!(write!(f, "and {} {}", lhs, rhs)),
      ValueKind::Xor(lhs, rhs) => try!(write!(f, "xor {} {}", lhs, rhs)),
      ValueKind::Or(lhs, rhs) => try!(write!(f, "or {} {}", lhs, rhs)),

      ValueKind::Eq(lhs, rhs) => try!(write!(f, "eq {} {}", lhs, rhs)),
      ValueKind::Neq(lhs, rhs) => try!(write!(f, "neq {} {}", lhs, rhs)),
      ValueKind::Lt(lhs, rhs) => try!(write!(f, "lt {} {}", lhs, rhs)),
      ValueKind::Gt(lhs, rhs) => try!(write!(f, "gt {} {}", lhs, rhs)),
      ValueKind::Lte(lhs, rhs) => try!(write!(f, "lte {} {}", lhs, rhs)),
      ValueKind::Gte(lhs, rhs) => try!(write!(f, "gte {} {}", lhs, rhs)),

      ValueKind::Parameter(_) => panic!("pcb_ice: Parameters should not be \
        displayed"),
    }
    Ok(())
  }
}

impl<'c> Display for Value<'c> {
  fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
    write!(f, "%{}", self.number)
  }
}