forked from M-Labs/nac3
[artiq] Add support for globals in nac3artiq
This commit is contained in:
parent
8baf111734
commit
5076eccb6c
@ -228,6 +228,7 @@ impl Nac3 {
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::AnnAssign { .. } => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
@ -544,6 +545,12 @@ impl Nac3 {
|
||||
pyid_to_ty.insert(id, ty);
|
||||
}
|
||||
}
|
||||
if let StmtKind::AnnAssign { target, .. } = &stmt.node {
|
||||
let ExprKind::Name { id: name, .. } = target.node else { unreachable!() };
|
||||
global_value_ids
|
||||
.write()
|
||||
.insert(id, py_module.getattr(name.to_string().as_str()).unwrap().into());
|
||||
}
|
||||
}
|
||||
|
||||
let id_fun = PyModule::import(py, "builtins")?.getattr("id")?;
|
||||
|
@ -23,7 +23,7 @@ use nac3core::{
|
||||
inkwell::{
|
||||
module::Linkage,
|
||||
types::{BasicType, BasicTypeEnum},
|
||||
values::BasicValueEnum,
|
||||
values::{BasicValue, BasicValueEnum},
|
||||
AddressSpace,
|
||||
},
|
||||
nac3parser::ast::{self, StrRef},
|
||||
@ -1555,9 +1555,39 @@ impl SymbolResolver for Resolver {
|
||||
fn get_symbol_value<'ctx>(
|
||||
&self,
|
||||
id: StrRef,
|
||||
_: &mut CodeGenContext<'ctx, '_>,
|
||||
_: &mut dyn CodeGenerator,
|
||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||
generator: &mut dyn CodeGenerator,
|
||||
) -> Option<ValueEnum<'ctx>> {
|
||||
if let Some(global_value) = self
|
||||
.0
|
||||
.name_to_pyid
|
||||
.get(&id)
|
||||
.and_then(|pyid| self.0.global_value_ids.read().get(pyid).cloned())
|
||||
{
|
||||
let val = ctx.module.get_global(id.to_string().as_str()).unwrap_or_else(|| {
|
||||
let v = Python::with_gil(|py| -> PyResult<SymbolValue> {
|
||||
Ok(self
|
||||
.0
|
||||
.get_default_param_obj_value(py, global_value.as_ref(py))
|
||||
.unwrap()
|
||||
.unwrap())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let ty = v.get_type(&ctx.primitives, &mut ctx.unifier);
|
||||
|
||||
let init_val = ctx.gen_symbol_val(generator, &v, ty);
|
||||
let llvm_ty = init_val.get_type();
|
||||
|
||||
let global = ctx.module.add_global(llvm_ty, None, &id.to_string());
|
||||
global.set_linkage(Linkage::LinkOnceAny);
|
||||
global.set_initializer(&init_val);
|
||||
|
||||
global
|
||||
});
|
||||
return Some(val.as_basic_value_enum().into());
|
||||
}
|
||||
|
||||
let sym_value = {
|
||||
let id_to_val = self.0.id_to_pyval.read();
|
||||
id_to_val.get(&id).cloned()
|
||||
|
Loading…
Reference in New Issue
Block a user