diff --git a/src/IR/module.rs b/src/IR/module.rs index deef2b10..1ea9121e 100644 --- a/src/IR/module.rs +++ b/src/IR/module.rs @@ -1,22 +1,22 @@ use crate::{prelude::Triple, Obj::{Decl, Linkage, ObjectBuilder}, Optimizations::PassManager, Support::{ColorClass, ColorProfile}, Target::TargetRegistry}; use super::{func::FunctionType, Const, Function, VerifyError}; -use std::{collections::HashMap, error::Error, fs::OpenOptions, io::Write, path::Path}; +use std::{collections::BTreeMap, error::Error, fs::OpenOptions, io::Write, path::Path}; /// ## The Module /// The main class for handeling functions #[derive(Debug, Clone)] pub struct Module { - pub(crate) funcs: HashMap, - pub(crate) consts: HashMap, + pub(crate) funcs: BTreeMap, + pub(crate) consts: BTreeMap, } impl Module { /// Creates a new module pub fn new() -> Self { Self { - funcs: HashMap::new(), - consts: HashMap::new(), + funcs: BTreeMap::new(), + consts: BTreeMap::new(), } } diff --git a/src/Obj/wrapper.rs b/src/Obj/wrapper.rs index 1fcfe32b..e8dba5b1 100644 --- a/src/Obj/wrapper.rs +++ b/src/Obj/wrapper.rs @@ -3,7 +3,7 @@ use object::{Architecture, BinaryFormat, Endianness, RelocationEncoding, Relocat use crate::prelude::Triple; use crate::Target::{self, Arch, CallConv}; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::fs::File; use std::error::Error; @@ -63,7 +63,7 @@ pub enum Linkage { /// It also supports debugging information #[derive(Debug, Clone, PartialEq, Eq)] pub struct ObjectBuilder { - defines: HashMap>, + defines: BTreeMap>, links: Vec, decls: Vec<(String, Decl, Linkage)>, @@ -75,7 +75,7 @@ impl ObjectBuilder { /// Creates an new object builder pub fn new(triple: Triple) -> Self { Self { - defines: HashMap::new(), + defines: BTreeMap::new(), links: vec![], @@ -109,6 +109,9 @@ impl ObjectBuilder { /// Writes the object file into the the specified file pub fn emit(&self, file: File) -> Result<(), Box> { + + let align = 1; + let mut obj = object::write::Object::new({ match self.triple.bin { Target::ObjFormat::Unknown => BinaryFormat::native_object(), @@ -169,7 +172,7 @@ impl ObjectBuilder { let secData = obj.add_section(vec![], ".data".as_bytes().to_vec(), SectionKind::Data); let secConsts = obj.add_section(vec![], ".rodata".as_bytes().to_vec(), SectionKind::ReadOnlyData); - let mut syms: HashMap, Option, SymbolId)> = HashMap::new(); + let mut syms: BTreeMap, Option, SymbolId)> = BTreeMap::new(); for (name, data) in &self.defines { let name = name.to_owned(); @@ -216,7 +219,17 @@ impl ObjectBuilder { } }, weak: false, - section: SymbolSection::Undefined, + section: { + if *link != Linkage::Extern { + match decl { + Decl::Function => SymbolSection::Section(secText), + Decl::Data => SymbolSection::Section(secData), + Decl::Constant => SymbolSection::Section(secConsts), + } + } else { + SymbolSection::Undefined + } + }, flags: SymbolFlags::None, }); @@ -228,19 +241,10 @@ impl ObjectBuilder { if *link != Linkage::Extern { - - if *decl == Decl::Function { - eprint!("{}: 0x", name); - for byte in &data { - eprint!("{:02x?}", *byte); - } - eprintln!(""); - } - let def_offset = match decl { - Decl::Function => obj.add_symbol_data(sym, secText, &data, 1), - Decl::Data => obj.add_symbol_data(sym, secData, &data, 1), - Decl::Constant => obj.add_symbol_data(sym, secConsts, &data, 1), + Decl::Function => obj.add_symbol_data(sym, secText, &data, align), + Decl::Data => obj.add_symbol_data(sym, secData, &data, align), + Decl::Constant => obj.add_symbol_data(sym, secConsts, &data, align), }; syms.insert(name.clone(), (None, Some(def_offset), sym)); @@ -264,10 +268,12 @@ impl ObjectBuilder { offset = -4; } + eprintln!("{:?}", link); + obj.add_relocation(secText, Relocation { - offset: (link.at as i64 + offset) as u64, + offset: (link.at as i64 + offset) as u64 + {if let Some(off) = off { *off } else { 0 }}, symbol: to_sym.to_owned(), - addend: link.addend + addend + {if let Some(off) = off { *off as i64} else { 0 }}, + addend: link.addend + addend, flags: RelocationFlags::Generic { kind: RelocationKind::PltRelative, encoding: { diff --git a/test.bat b/test.bat deleted file mode 100644 index ed69623e..00000000 --- a/test.bat +++ /dev/null @@ -1,4 +0,0 @@ -@echo off -cargo run -p simplelang -- -in="tools/simplelang/example.sl" -o="out.o" -asm > out.asm -llvm-objdump -d out.o --x86-asm-syntax=intel > dump.asm -gcc out.o \ No newline at end of file diff --git a/tools/simplelang/example.sl b/tools/simplelang/example.sl index ee182e84..02a46f36 100644 --- a/tools/simplelang/example.sl +++ b/tools/simplelang/example.sl @@ -31,4 +31,8 @@ extern with () main: { printf("add(%d, %d) = %d # expected: %d\n", a, b, add(a, b), expected); return 0; +} + +extern with() aaa: { + return 5; } \ No newline at end of file