Use zeroinit API for faerie and object (#1209)

* use new zeroinit API for faerie

* use bss for cranelift-object

* don't crash when initializing bss

* fix formatting

* Improve code locality

Co-Authored-By: Philip Craig <philipjcraig@gmail.com>

* use `as` instead of try_into() for usize -> u64

* don't allocate unnecessarily in `faerie`

Co-authored-by: Philip Craig <philipjcraig@gmail.com>
This commit is contained in:
Joshua Nelson
2020-02-13 16:25:41 -05:00
committed by GitHub
parent 58e5a62cde
commit 4d8cf563f3
2 changed files with 34 additions and 36 deletions

View File

@@ -217,20 +217,6 @@ impl Backend for FaerieBackend {
ref data_relocs,
} = data_ctx.description();
let size = init.size();
let mut bytes = Vec::with_capacity(size);
match *init {
Init::Uninitialized => {
panic!("data is not initialized yet");
}
Init::Zeros { .. } => {
bytes.resize(size, 0);
}
Init::Bytes { ref contents } => {
bytes.extend_from_slice(contents);
}
}
for &(offset, id) in function_relocs {
let to = &namespace.get_function_decl(&function_decls[id]).name;
self.artifact
@@ -256,9 +242,22 @@ impl Backend for FaerieBackend {
.map_err(|e| ModuleError::Backend(e.to_string()))?;
}
self.artifact
.define(name, bytes)
.expect("inconsistent declaration");
match *init {
Init::Uninitialized => {
panic!("data is not initialized yet");
}
Init::Zeros { size } => {
self.artifact
.define_zero_init(name, size)
.expect("inconsistent declaration");
}
Init::Bytes { ref contents } => {
self.artifact
.define(name, contents.to_vec())
.expect("inconsistent declaration");
}
}
Ok(FaerieCompiledData {})
}

View File

@@ -243,20 +243,6 @@ impl Backend for ObjectBackend {
ref data_relocs,
} = data_ctx.description();
let size = init.size();
let mut data = Vec::with_capacity(size);
match *init {
Init::Uninitialized => {
panic!("data is not initialized yet");
}
Init::Zeros { .. } => {
data.resize(size, 0);
}
Init::Bytes { ref contents } => {
data.extend_from_slice(contents);
}
}
let reloc_size = match self.isa.triple().pointer_width().unwrap() {
PointerWidth::U16 => 16,
PointerWidth::U32 => 32,
@@ -285,16 +271,29 @@ impl Backend for ObjectBackend {
}
let symbol = self.data_objects[data_id].unwrap();
let section = self.object.section_id(if writable {
let section_kind = if let Init::Zeros { .. } = *init {
StandardSection::UninitializedData
} else if writable {
StandardSection::Data
} else if relocs.is_empty() {
StandardSection::ReadOnlyData
} else {
StandardSection::ReadOnlyDataWithRel
});
let offset =
self.object
.add_symbol_data(symbol, section, &data, u64::from(align.unwrap_or(1)));
};
let section = self.object.section_id(section_kind);
let align = u64::from(align.unwrap_or(1));
let offset = match *init {
Init::Uninitialized => {
panic!("data is not initialized yet");
}
Init::Zeros { size } => self
.object
.add_symbol_bss(symbol, section, size as u64, align),
Init::Bytes { ref contents } => self
.object
.add_symbol_data(symbol, section, &contents, align),
};
if !relocs.is_empty() {
self.relocs.push(SymbolRelocs {
section,