forked from M-Labs/nac3
1
0
Fork 0

core/model: add GEP .set and .get & refactor

This commit is contained in:
lyken 2024-08-05 00:14:44 +08:00
parent 46110d2f6a
commit 876d1bbfe3
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
4 changed files with 41 additions and 13 deletions

View File

@ -599,11 +599,11 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
*self.exception_val.insert(exn)
});
exn.gep(self, |f| f.id).store(self, exn_id);
exn.gep(self, |f| f.msg).store(self, msg);
exn.set(self, |f| f.id, exn_id);
exn.set(self, |f| f.msg, msg);
for (i, param) in params.iter().enumerate() {
if let Some(param) = param {
exn.gep(self, |f| f.params[i]).store(self, *param);
exn.set(self, |f| f.params[i], *param);
}
}

View File

@ -166,4 +166,32 @@ impl<'ctx, S: StructKind<'ctx>> Ptr<'ctx, StructModel<S>> {
let ptr_model = PtrModel(field.model);
ptr_model.believe_value(ptr)
}
/// Convenience function equivalent to `.gep(...).load(...)`.
pub fn get<M, GetField>(
&self,
tyctx: TypeContext<'ctx>,
ctx: &CodeGenContext<'ctx, '_>,
get_field: GetField,
name: &str,
) -> Instance<'ctx, M>
where
M: Model<'ctx>,
GetField: FnOnce(S::Fields<GepFieldTraversal>) -> GepField<M>,
{
self.gep(ctx, get_field).load(tyctx, ctx, name)
}
/// Convenience function equivalent to `.gep(...).store(...)`.
pub fn set<M, GetField>(
&self,
ctx: &CodeGenContext<'ctx, '_>,
get_field: GetField,
value: Instance<'ctx, M>,
) where
M: Model<'ctx>,
GetField: FnOnce(S::Fields<GepFieldTraversal>) -> GepField<M>,
{
self.gep(ctx, get_field).store(ctx, value)
}
}

View File

@ -1274,17 +1274,17 @@ pub fn gen_raise<'ctx, G: CodeGenerator + ?Sized>(
let filename = loc.file.0;
let filename = ctx.gen_string(generator, &String::from(filename)).value;
let filename = cslice_model.check_value(type_context, ctx.ctx, filename).unwrap();
pexn.gep(ctx, |f| f.filename).store(ctx, filename);
pexn.set(ctx, |f| f.filename, filename);
let row = i32_model.constant(type_context, ctx.ctx, loc.row as u64);
pexn.gep(ctx, |f| f.line).store(ctx, row);
pexn.set(ctx, |f| f.line, row);
let column = i32_model.constant(type_context, ctx.ctx, loc.column as u64);
pexn.gep(ctx, |f| f.column).store(ctx, column);
pexn.set(ctx, |f| f.column, column);
let current_fn = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
let fn_name = ctx.gen_string(generator, current_fn.get_name().to_str().unwrap());
pexn.gep(ctx, |f| f.function).store(ctx, fn_name);
pexn.set(ctx, |f| f.function, fn_name);
let raise = get_builtins(generator, ctx, "__nac3_raise");
ctx.build_call_or_invoke(raise, &[pexn.value.into()], "raise");

View File

@ -95,11 +95,11 @@ impl<'ctx> NDArrayObject<'ctx> {
let shape = sizet_model.array_alloca(tyctx, ctx, ndims_val.value, "shape");
let strides = sizet_model.array_alloca(tyctx, ctx, ndims_val.value, "strides");
pndarray.gep(ctx, |f| f.data).store(ctx, data);
pndarray.gep(ctx, |f| f.itemsize).store(ctx, itemsize);
pndarray.gep(ctx, |f| f.ndims).store(ctx, ndims_val);
pndarray.gep(ctx, |f| f.shape).store(ctx, shape);
pndarray.gep(ctx, |f| f.strides).store(ctx, strides);
pndarray.set(ctx, |f| f.data, data);
pndarray.set(ctx, |f| f.itemsize, itemsize);
pndarray.set(ctx, |f| f.ndims, ndims_val);
pndarray.set(ctx, |f| f.shape, shape);
pndarray.set(ctx, |f| f.strides, strides);
NDArrayObject { dtype, ndims, value: pndarray }
}
@ -136,7 +136,7 @@ impl<'ctx> NDArrayObject<'ctx> {
let byte_model = IntModel(Byte);
let data = byte_model.array_alloca(tyctx, ctx, self.get_ndims(tyctx, ctx).value, "data");
self.value.gep(ctx, |f| f.data).store(ctx, data);
self.value.set(ctx, |f| f.data, data);
self.update_strides_by_shape(tyctx, ctx);
}