fixup symbol export

This commit is contained in:
Michael Sippel 2024-10-21 00:20:55 +02:00
parent 489d9886e3
commit 45869fdd30
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 69 additions and 19 deletions

View file

@ -77,14 +77,13 @@ impl ProcedureCompiler {
self.linker.link_relative(&self.proc_symbol).expect("link error");
let mut export_symbols = self.scope.read().unwrap().export();
//eprintln!("proc compiler export = {:?}\ncode= {:?}", export_symbols, code);
(
export_symbols,
code
)
}
pub fn compile_statement(mut self, statement: &Statement, enable_export: bool) -> Self {
pub fn compile_statement(mut self, statement: &Statement) -> Self {
match statement {
Statement::Assignment { name_region, var_id, val_expr } => {
self = self.compile_expr(val_expr, true);
@ -93,7 +92,7 @@ impl ProcedureCompiler {
Some(SymbolDef::FrameRef { typ, stack_ref }) => {
self.asm = self.asm.lit(stack_ref).call("data-frame-set");
}
Some(SymbolDef::StaticRef { typ, link_addr }) => {
Some(SymbolDef::StaticRef { typ, link_addr, export }) => {
self.asm = self
.asm
.static_ref(var_id.as_str())
@ -146,7 +145,7 @@ impl ProcedureCompiler {
name_region: *name_region,
var_id: var_id.clone(),
val_expr: val_expr.clone(),
}, false);
});
self.frame_size += 1;
}
@ -173,7 +172,7 @@ impl ProcedureCompiler {
Some(SymbolDef::FrameRef { typ, stack_ref }) => {
self.asm = self.asm.lit(stack_ref).call("data-frame-get");
}
Some(SymbolDef::StaticRef { typ, link_addr }) => {
Some(SymbolDef::StaticRef { typ, link_addr, export }) => {
self.asm = self.asm.static_ref(symbol.as_str());
}
Some(SymbolDef::Procedure {
@ -280,19 +279,18 @@ impl ProcedureCompiler {
self.linker.add_procedure( &abs_symbol, abs_code );
self.asm = self.asm.call( &abs_symbol );
}
LTExpr::Block { region, scope, statements } => {
LTExpr::ExportBlock{ region, scope, statements }
| LTExpr::Block { region, scope, statements } => {
let block_symbol = self.make_subroutine_symbol();
let mut block_compiler = ProcedureCompiler::new( block_symbol.clone(), scope.clone() );
for stmnt in statements.iter() {
block_compiler = block_compiler.compile_statement( stmnt, true );
block_compiler = block_compiler.compile_statement( stmnt );
}
let (block_export_symbols, mut block_code) = block_compiler.get_bytecode();
self.scope.write().unwrap().import( block_export_symbols );
self.linker.add_procedure( &block_symbol, block_code.clone() );
self.asm = self.asm.call( &block_symbol );
}
LTExpr::ExportBlock{ region, scope, statements } => {
}
}
self
}

View file

@ -20,6 +20,7 @@ pub enum SymbolDef {
StaticRef {
typ: laddertypes::TypeTerm,
link_addr: Option<tisc::VM_Word>,
export: bool
},
Procedure {
in_types: Vec<laddertypes::TypeTerm>,
@ -36,7 +37,7 @@ impl SymbolDef {
) -> laddertypes::TypeTerm {
match self {
SymbolDef::FrameRef { typ, stack_ref: _ } => typ.clone(),
SymbolDef::StaticRef { typ, link_addr: _ } => typ.clone(),
SymbolDef::StaticRef { typ, link_addr: _, export: _ } => typ.clone(),
SymbolDef::Procedure {
in_types,
out_types,
@ -153,6 +154,7 @@ impl Scope {
.iter()
.filter(|(name, def)|
match def {
SymbolDef::StaticRef { typ:_, link_addr:_, export } => *export,
SymbolDef::Procedure { in_types:_, out_types:_, link_addr:_, export } => *export,
_ => false
}
@ -215,8 +217,7 @@ impl Scope {
export:_
} => {
if let Some(offset) = linker.get_link_addr( name ) {
eprintln!("update link addr {} @ {} + {}", name, base_symbol, offset);
// eprintln!("update link addr {} @ {} + {}", name, base_symbol, offset);
*link_addr = LinkAddr::Relative{
symbol: base_symbol.clone(),
offset
@ -319,15 +320,17 @@ impl Scope {
let typ = self
.parse(typ)
.expect("parse typeterm");
self.declare_static(String::from(name), typ);
self.declare_static(String::from(name), typ, true);
}
pub fn declare_static(&mut self, name: String, typ: laddertypes::TypeTerm) {
pub fn declare_static(&mut self, name: String, typ: laddertypes::TypeTerm, export: bool) {
eprintln!("add {} export {}", name, export);
self.symbols.insert(
name,
SymbolDef::StaticRef {
typ,
link_addr: None,
export
},
);
}

View file

@ -23,6 +23,21 @@ impl LTExpr {
Err(TypeError::Todo)
}*/
pub fn export_symbols(&self) -> Vec< (String, SymbolDef) > {
match self {
LTExpr::Block{ region, scope, statements }
| LTExpr::ExportBlock{
region,
scope,
statements
} => {
scope.read().unwrap().export()
}
_ => vec![]
}
}
pub fn infer_type(&self, scope: &Arc<RwLock<Scope>>) -> TypeTag
{
match self {
@ -230,8 +245,15 @@ impl LTExpr {
LTExpr::Block{ region, scope, statements } => {
let mut types = Vec::new();
let enable_export =
match self {
LTExpr::ExportBlock{ region:_, scope:_, statements:_ } => true,
_ => false
};
eprintln!(" .. in block (export={}) ", enable_export);
for s in statements {
match s.infer_type(scope) {
match s.infer_type(scope, enable_export) {
Ok(Some(t)) => {
if !t.is_empty() {
types.insert(0, t);
@ -255,7 +277,7 @@ impl LTExpr {
}
impl Statement {
pub fn infer_type(&self, scope: &Arc<RwLock<Scope>>) -> Result< Option<laddertypes::SugaredTypeTerm> , Vec<TypeError> > {
pub fn infer_type(&self, scope: &Arc<RwLock<Scope>>, enable_export: bool) -> Result< Option<laddertypes::SugaredTypeTerm> , Vec<TypeError> > {
match self {
Statement::LetAssign{ name_region, typ, var_id, val_expr } => {
let typ = val_expr.infer_type( scope )?;
@ -271,18 +293,18 @@ impl Statement {
let in_types = args.into_iter().map(|t| t.desugar(&mut scope.clone())).collect();
scope.write().unwrap()
.declare_proc(
.declare_proc(
var_id.clone(),
in_types,
out_types,
true
enable_export
);
return Ok(None);
}
_ => {
let id = scope.write().unwrap().declare_var(var_id.clone(), typ);
eprintln!("TYPING declare var = {}", id);
eprintln!("TYPING declare var ({}) = {}, export ={}", var_id, id, enable_export);
Ok(None)
}
}
@ -291,6 +313,10 @@ impl Statement {
Statement::Expr(expr) => {
let t = expr.infer_type(scope)?;
let symb = expr.export_symbols();
eprintln!("expr statement: import symbols from expr {:?}", symb);
scope.write().unwrap().import( symb );
if t != laddertypes::TypeTerm::App(vec![]) {
let st = t.sugar(&mut scope.clone());
Ok(Some(st))

View file

@ -53,6 +53,18 @@ fn main() {
.export()
.into_iter()
.filter_map(|(symbol, def)| match def {
ltcore::symbols::SymbolDef::StaticRef { typ, link_addr, export } => {
eprintln!("runtime export static REF {}", export);
if export {
if let Some(addr)= link_addr {
Some((symbol, addr))
} else {
Some((symbol.clone(), runtime_linker.get_link_addr(&symbol).unwrap_or(-1)))
}
} else {
None
}
}
ltcore::symbols::SymbolDef::Procedure { in_types:_, out_types:_, link_addr, export } => {
if true {
match link_addr {
@ -198,6 +210,17 @@ fn main() {
.export()
.into_iter()
.filter_map(|(symbol, def)| match def {
ltcore::symbols::SymbolDef::StaticRef { typ, link_addr, export } => {
if export {
if let Some(addr)= link_addr {
Some((symbol.clone(), addr))
} else {
Some((symbol.clone(), linker.get_link_addr(&symbol).unwrap_or(-1)))
}
} else {
None
}
}
ltcore::symbols::SymbolDef::Procedure { in_types:_, out_types:_, link_addr, export } => {
if export {
match link_addr {