add import statement

ltcc: map all outputted bytecode such that absolute addresses become relative to the file

typing: rename scope to super_scope
This commit is contained in:
Michael Sippel 2024-10-21 12:00:13 +02:00
parent a948b53d9a
commit 4bc7fd1788
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 122 additions and 46 deletions
ltcc/src

View file

@ -54,12 +54,11 @@ fn main() {
.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 {
if let Some(addr)= runtime_linker.get_link_addr(&symbol) {
Some((symbol, addr))
} else {
Some((symbol.clone(), runtime_linker.get_link_addr(&symbol).unwrap_or(-1)))
None
}
} else {
None
@ -168,18 +167,6 @@ fn main() {
}
eprintln!("{} {}\n{}", "Compiled".green(), path.bold(), "---------------".green());
for (name, def) in exports.iter() {
eprintln!("export {}:", name.yellow().bold());
let mut t = def.get_type(&mut main_scope);
t = t.normalize();
t = t.param_normalize();
let mut tc = main_scope.clone();
eprintln!( "{}", t.sugar(&mut tc).pretty(&tc,0) );
}
main_scope.write().unwrap().import(
exports
);
/* link assembly-program to symbols
*/
@ -212,10 +199,10 @@ fn main() {
.filter_map(|(symbol, def)| match def {
ltcore::symbols::SymbolDef::StaticRef { typ, link_addr, export } => {
if export {
if let Some(addr)= link_addr {
if let Some(addr) = linker.get_link_addr(&symbol) {
Some((symbol.clone(), addr))
} else {
Some((symbol.clone(), linker.get_link_addr(&symbol).unwrap_or(-1)))
None
}
} else {
None
@ -229,9 +216,12 @@ fn main() {
Some(( symbol, w ))
}
tisc::LinkAddr::Relative{ symbol: b, offset } => {
let addr = linker.get_link_addr(&b).unwrap_or(-1);
// eprintln!("relative symbol {} -> {}({})+{}", symbol, b, addr, offset);
Some((symbol, addr + offset ))
if let Some(addr) = linker.get_link_addr(&b) {
// eprintln!("relative symbol {} -> {}({})+{}", symbol, b, addr, offset);
Some((symbol, addr + offset ))
} else {
None
}
}
}
} else {
@ -243,6 +233,22 @@ fn main() {
.collect(),
code: linker.link_partial().expect("Link error:")
.into_iter()
.map(|w| match w {
tisc::assembler::AssemblyWord::Symbol(
tisc::LinkAddr::Absolute(a)
) => {
tisc::assembler::AssemblyWord::Symbol(
tisc::LinkAddr::Relative{
symbol: args.output.clone().into(),
offset: a
}
)
},
w => w
})
.collect()
};
let mut output = std::io::BufWriter::new(