Implement indexing for arrays

This commit is contained in:
bjorn3 2018-08-08 12:22:16 +02:00
parent ece497cc84
commit efd203aa0b
4 changed files with 35 additions and 1 deletions

View file

@ -800,7 +800,8 @@ pub fn trans_place<'a, 'tcx: 'a>(
),
ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
ProjectionElem::Index(local) => {
unimplemented!("projection index {:?} {:?}", projection.base, local)
let index = fx.get_local_place(local).to_cvalue(fx).load_value(fx);
base.place_index(fx, index)
}
ProjectionElem::ConstantIndex {
offset,

View file

@ -297,6 +297,21 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
CPlace::Addr(field_ptr, field_layout)
}
pub fn place_index(self, fx: &mut FunctionCx<'a, 'tcx>, index: Value) -> CPlace<'tcx> {
let addr = self.expect_addr();
let layout = self.layout();
match layout.ty.sty {
TypeVariants::TyArray(elem_ty, _) => {
let elem_layout = fx.layout_of(elem_ty);
let size = fx.bcx.ins().iconst(types::I64, elem_layout.size.bytes() as i64);
let offset = fx.bcx.ins().imul(size, index);
CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
}
TypeVariants::TySlice(_elem_ty) => unimplemented!("place_index(TySlice)"),
_ => bug!("place_index({:?})", layout.ty),
}
}
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
match self {
CPlace::Var(var, _) => CPlace::Var(var, layout),