diff --git a/hercules_ir/src/build.rs b/hercules_ir/src/build.rs index cfc59a2b4ce5283fdc709371cba21ec17c2e0f0c..50a38ef8a0ba54c00ddd9e76b84333d537c6d383 100644 --- a/hercules_ir/src/build.rs +++ b/hercules_ir/src/build.rs @@ -354,14 +354,41 @@ impl<'a> Builder<'a> { pub fn create_constant_array( &mut self, elem_ty: TypeID, - extents: Box<[u32]>, - ) -> BuilderResult<ConstantID> { - let extents = extents - .iter() - .map(|extent| self.create_dynamic_constant_constant(*extent as usize)) - .collect(); + extents: Box<[DynamicConstantID]>, + ) -> ConstantID { let ty = self.create_type_array(elem_ty, extents); - Ok(self.intern_constant(Constant::Array(ty), ty)) + self.intern_constant(Constant::Array(ty), ty) + } + + pub fn create_constant_zero(&mut self, typ : TypeID) -> ConstantID { + match &self.module.types[typ.idx()] { + Type::Control(_) => panic!("Cannot create constant for control types"), + Type::Boolean => self.create_constant_bool(false), + Type::Integer8 => self.create_constant_i8(0), + Type::Integer16 => self.create_constant_i16(0), + Type::Integer32 => self.create_constant_i32(0), + Type::Integer64 => self.create_constant_i64(0), + Type::UnsignedInteger8 => self.create_constant_u8(0), + Type::UnsignedInteger16 => self.create_constant_u16(0), + Type::UnsignedInteger32 => self.create_constant_u32(0), + Type::UnsignedInteger64 => self.create_constant_u64(0), + Type::Float32 => self.create_constant_f32(0.0), + Type::Float64 => self.create_constant_f64(0.0), + Type::Product(fs) => { + let mut cs = vec![]; + for t in fs.clone() { + cs.push(self.create_constant_zero(t)); + } + self.create_constant_prod(cs.into()) + }, + Type::Summation(cs) => { + assert!(cs.len() >= 1, "Cannot create zero for empty summation"); + let c = self.create_constant_zero(cs[0]); + self.create_constant_sum(typ, 0, c) + .expect("Exists and well typed by construction") + }, + Type::Array(t, dims) => self.create_constant_array(*t, dims.clone()), + } } pub fn create_dynamic_constant_constant(&mut self, val: usize) -> DynamicConstantID {