Fix typedef issue with modules
This commit is contained in:
parent
f35f1ef701
commit
78a1e9f06b
@ -51,6 +51,7 @@ pub struct ModuleData {
|
|||||||
is_main: bool,
|
is_main: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Module<'ctx> {
|
pub struct Module<'ctx> {
|
||||||
phantom: PhantomData<&'ctx ()>,
|
phantom: PhantomData<&'ctx ()>,
|
||||||
builder: Builder,
|
builder: Builder,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{array, collections::HashMap, mem};
|
use std::{array, collections::HashMap, hash::Hash, mem};
|
||||||
|
|
||||||
use reid_lib::{
|
use reid_lib::{
|
||||||
builder::{InstructionValue, TypeValue},
|
builder::{InstructionValue, ModuleValue, TypeValue},
|
||||||
compile::CompiledModule,
|
compile::CompiledModule,
|
||||||
debug_information::{
|
debug_information::{
|
||||||
DebugArrayType, DebugBasicType, DebugFieldType, DebugFileData, DebugInformation,
|
DebugArrayType, DebugBasicType, DebugFieldType, DebugFileData, DebugInformation,
|
||||||
@ -18,8 +18,9 @@ use crate::{
|
|||||||
error_raporting::ErrorModules,
|
error_raporting::ErrorModules,
|
||||||
lexer::{FullToken, Position},
|
lexer::{FullToken, Position},
|
||||||
mir::{
|
mir::{
|
||||||
self, implement::TypeCategory, Metadata, NamedVariableRef, SourceModuleId, StructField,
|
self, implement::TypeCategory, Metadata, ModuleMap, NamedVariableRef, SourceModuleId,
|
||||||
StructType, TypeDefinition, TypeDefinitionKind, TypeKey, TypeKind, VagueLiteral,
|
StructField, StructType, TypeDefinition, TypeDefinitionKind, TypeKey, TypeKind,
|
||||||
|
VagueLiteral,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -41,16 +42,22 @@ impl<'ctx> CodegenContext<'ctx> {
|
|||||||
impl mir::Context {
|
impl mir::Context {
|
||||||
/// Compile MIR [`Context`] into [`CodegenContext`] containing LLIR.
|
/// Compile MIR [`Context`] into [`CodegenContext`] containing LLIR.
|
||||||
pub fn codegen<'ctx>(&self, context: &'ctx Context) -> CodegenContext<'ctx> {
|
pub fn codegen<'ctx>(&self, context: &'ctx Context) -> CodegenContext<'ctx> {
|
||||||
let mut modules = Vec::new();
|
let mut modules = HashMap::new();
|
||||||
for (_, module) in &self.modules {
|
let mut modules_sorted = self.modules.iter().map(|(_, m)| m).collect::<Vec<_>>();
|
||||||
modules.push(module.codegen(context));
|
modules_sorted.sort_by(|m1, m2| m2.module_id.cmp(&m1.module_id));
|
||||||
|
|
||||||
|
for module in &modules_sorted {
|
||||||
|
let codegen = module.codegen(context, modules.clone());
|
||||||
|
modules.insert(module.module_id, codegen);
|
||||||
}
|
}
|
||||||
CodegenContext { context }
|
CodegenContext { context }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct ModuleCodegen<'ctx> {
|
struct ModuleCodegen<'ctx> {
|
||||||
module: Module<'ctx>,
|
module: Module<'ctx>,
|
||||||
|
type_values: HashMap<TypeKey, TypeValue>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx> std::fmt::Debug for ModuleCodegen<'ctx> {
|
impl<'ctx> std::fmt::Debug for ModuleCodegen<'ctx> {
|
||||||
@ -59,16 +66,17 @@ impl<'ctx> std::fmt::Debug for ModuleCodegen<'ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Scope<'ctx, 'a> {
|
pub struct Scope<'ctx, 'scope> {
|
||||||
context: &'ctx Context,
|
context: &'ctx Context,
|
||||||
|
modules: &'scope HashMap<SourceModuleId, ModuleCodegen<'ctx>>,
|
||||||
tokens: &'ctx Vec<FullToken>,
|
tokens: &'ctx Vec<FullToken>,
|
||||||
module: &'ctx Module<'ctx>,
|
module: &'ctx Module<'ctx>,
|
||||||
module_id: SourceModuleId,
|
module_id: SourceModuleId,
|
||||||
function: &'ctx StackFunction<'ctx>,
|
function: &'ctx StackFunction<'ctx>,
|
||||||
block: Block<'ctx>,
|
block: Block<'ctx>,
|
||||||
types: &'a HashMap<TypeValue, TypeDefinition>,
|
types: &'scope HashMap<TypeValue, TypeDefinition>,
|
||||||
type_values: &'a HashMap<TypeKey, TypeValue>,
|
type_values: &'scope HashMap<TypeKey, TypeValue>,
|
||||||
functions: &'a HashMap<String, StackFunction<'ctx>>,
|
functions: &'scope HashMap<String, StackFunction<'ctx>>,
|
||||||
stack_values: HashMap<String, StackValue>,
|
stack_values: HashMap<String, StackValue>,
|
||||||
debug: Option<Debug<'ctx>>,
|
debug: Option<Debug<'ctx>>,
|
||||||
}
|
}
|
||||||
@ -137,6 +145,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
|
|||||||
fn with_block(&self, block: Block<'ctx>) -> Scope<'ctx, 'a> {
|
fn with_block(&self, block: Block<'ctx>) -> Scope<'ctx, 'a> {
|
||||||
Scope {
|
Scope {
|
||||||
block,
|
block,
|
||||||
|
modules: self.modules,
|
||||||
tokens: self.tokens,
|
tokens: self.tokens,
|
||||||
function: self.function,
|
function: self.function,
|
||||||
context: self.context,
|
context: self.context,
|
||||||
@ -184,7 +193,11 @@ impl Default for State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl mir::Module {
|
impl mir::Module {
|
||||||
fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> {
|
fn codegen<'ctx>(
|
||||||
|
&self,
|
||||||
|
context: &'ctx Context,
|
||||||
|
modules: HashMap<SourceModuleId, ModuleCodegen<'ctx>>,
|
||||||
|
) -> ModuleCodegen<'ctx> {
|
||||||
let mut module = context.module(&self.name, self.is_main);
|
let mut module = context.module(&self.name, self.is_main);
|
||||||
let tokens = &self.tokens;
|
let tokens = &self.tokens;
|
||||||
|
|
||||||
@ -236,9 +249,13 @@ impl mir::Module {
|
|||||||
|
|
||||||
for typedef in &self.typedefs {
|
for typedef in &self.typedefs {
|
||||||
let type_key = TypeKey(typedef.name.clone(), typedef.source_module);
|
let type_key = TypeKey(typedef.name.clone(), typedef.source_module);
|
||||||
|
|
||||||
let type_value = if typedef.source_module != self.module_id {
|
let type_value = if typedef.source_module != self.module_id {
|
||||||
todo!()
|
*modules
|
||||||
|
.get(&typedef.source_module)
|
||||||
|
.unwrap()
|
||||||
|
.type_values
|
||||||
|
.get(&type_key)
|
||||||
|
.unwrap()
|
||||||
} else {
|
} else {
|
||||||
match &typedef.kind {
|
match &typedef.kind {
|
||||||
TypeDefinitionKind::Struct(StructType(fields)) => {
|
TypeDefinitionKind::Struct(StructType(fields)) => {
|
||||||
@ -392,6 +409,7 @@ impl mir::Module {
|
|||||||
|
|
||||||
let mut scope = Scope {
|
let mut scope = Scope {
|
||||||
context,
|
context,
|
||||||
|
modules: &modules,
|
||||||
tokens,
|
tokens,
|
||||||
module: &module,
|
module: &module,
|
||||||
module_id: self.module_id,
|
module_id: self.module_id,
|
||||||
@ -434,7 +452,10 @@ impl mir::Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ModuleCodegen { module }
|
ModuleCodegen {
|
||||||
|
module,
|
||||||
|
type_values,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -879,7 +900,7 @@ impl mir::Expression {
|
|||||||
mir::ExprKind::Accessed(expression, type_kind, field) => {
|
mir::ExprKind::Accessed(expression, type_kind, field) => {
|
||||||
let struct_val = expression.codegen(scope, &state.load(false)).unwrap();
|
let struct_val = expression.codegen(scope, &state.load(false)).unwrap();
|
||||||
|
|
||||||
dbg!(&expression);
|
dbg!(&expression, &struct_val);
|
||||||
let TypeKind::CodegenPtr(inner) = &struct_val.1 else {
|
let TypeKind::CodegenPtr(inner) = &struct_val.1 else {
|
||||||
panic!("tried accessing non-pointer");
|
panic!("tried accessing non-pointer");
|
||||||
};
|
};
|
||||||
|
@ -21,6 +21,7 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub static STD_SOURCE: &str = include_str!("../../lib/std.reid");
|
pub static STD_SOURCE: &str = include_str!("../../lib/std.reid");
|
||||||
|
pub static STD_NAME: &str = "std";
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum ErrorKind {
|
pub enum ErrorKind {
|
||||||
@ -47,7 +48,7 @@ pub enum ErrorKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile_std(module_map: &mut ErrorModules) -> Result<Module, ReidError> {
|
pub fn compile_std(module_map: &mut ErrorModules) -> Result<Module, ReidError> {
|
||||||
let (id, tokens) = parse_module(STD_SOURCE, "standard_library", module_map)?;
|
let (id, tokens) = parse_module(STD_SOURCE, STD_NAME, module_map)?;
|
||||||
let module = compile_module(id, tokens, module_map, None, false)?;
|
let module = compile_module(id, tokens, module_map, None, false)?;
|
||||||
|
|
||||||
let module_id = module.module_id;
|
let module_id = module.module_id;
|
||||||
@ -70,8 +71,9 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
type Data = ();
|
type Data = ();
|
||||||
type TError = ErrorKind;
|
type TError = ErrorKind;
|
||||||
fn context(&mut self, context: &mut Context, mut state: LinkerPassState) -> PassResult {
|
fn context(&mut self, context: &mut Context, mut state: LinkerPassState) -> PassResult {
|
||||||
let mains = (&mut context.modules)
|
let mains = context
|
||||||
.into_iter()
|
.modules
|
||||||
|
.iter_mut()
|
||||||
.filter(|(_, module)| module.is_main)
|
.filter(|(_, module)| module.is_main)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if mains.len() > 1 {
|
if mains.len() > 1 {
|
||||||
@ -115,11 +117,9 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
|
|
||||||
let module_name = unsafe { path.get_unchecked(0) };
|
let module_name = unsafe { path.get_unchecked(0) };
|
||||||
|
|
||||||
let mut imported = if let Some(module) =
|
let mut imported = if let Some(mod_id) = module_ids.get(module_name) {
|
||||||
modules.get_mut(module_ids.get(module_name).unwrap())
|
modules.get(mod_id).unwrap()
|
||||||
{
|
} else if module_name == STD_NAME {
|
||||||
module
|
|
||||||
} else if module_name == "std" {
|
|
||||||
let std = compile_std(&mut self.module_map)?;
|
let std = compile_std(&mut self.module_map)?;
|
||||||
modules.insert(
|
modules.insert(
|
||||||
std.module_id,
|
std.module_id,
|
||||||
@ -324,11 +324,15 @@ impl<'map> Pass for LinkerPass<'map> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let modules: Vec<Module> = modules
|
let mut modules: Vec<Module> = modules
|
||||||
.into_values()
|
.into_values()
|
||||||
.map(|v| Rc::into_inner(v).unwrap().into_inner())
|
.map(|v| Rc::into_inner(v).unwrap().into_inner())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
for module in modules.drain(..) {
|
||||||
|
context.modules.insert(module.module_id, module);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,7 +277,7 @@ impl Context {
|
|||||||
let mut state = State::new();
|
let mut state = State::new();
|
||||||
let mut scope = Scope::default();
|
let mut scope = Scope::default();
|
||||||
pass.context(self, PassState::from(&mut state, &mut scope, None))?;
|
pass.context(self, PassState::from(&mut state, &mut scope, None))?;
|
||||||
for (_, module) in (&mut self.modules).into_iter() {
|
for (_, module) in &mut self.modules {
|
||||||
module.pass(pass, &mut state, &mut scope.inner())?;
|
module.pass(pass, &mut state, &mut scope.inner())?;
|
||||||
}
|
}
|
||||||
Ok(state)
|
Ok(state)
|
||||||
|
Loading…
Reference in New Issue
Block a user