Use the target-lexicon crate.

This switches from a custom list of architectures to use the
target-lexicon crate.

 - "set is_64bit=1; isa x86" is replaced with "target x86_64", and
   similar for other architectures, and the `is_64bit` flag is removed
   entirely.

 - The `is_compressed` flag is removed too; it's no longer being used to
   control REX prefixes on x86-64, ARM and Thumb are separate
   architectures in target-lexicon, and we can figure out how to
   select RISC-V compressed encodings when we're ready.
This commit is contained in:
Dan Gohman
2018-05-25 11:41:14 -07:00
parent 2f3008aa40
commit 4e67e08efd
131 changed files with 487 additions and 499 deletions

View File

@@ -9,7 +9,7 @@ use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, Modu
use faerie;
use failure::Error;
use std::fs::File;
use target;
use target_lexicon::BinaryFormat;
use traps::{FaerieTrapManifest, FaerieTrapSink};
#[derive(Debug)]
@@ -27,8 +27,7 @@ pub enum FaerieTrapCollection {
pub struct FaerieBuilder {
isa: Box<TargetIsa>,
name: String,
format: container::Format,
faerie_target: faerie::Target,
format: BinaryFormat,
collect_traps: FaerieTrapCollection,
libcall_names: Box<Fn(ir::LibCall) -> String>,
}
@@ -50,7 +49,7 @@ impl FaerieBuilder {
pub fn new(
isa: Box<TargetIsa>,
name: String,
format: container::Format,
format: BinaryFormat,
collect_traps: FaerieTrapCollection,
libcall_names: Box<Fn(ir::LibCall) -> String>,
) -> Result<Self, ModuleError> {
@@ -59,12 +58,10 @@ impl FaerieBuilder {
"faerie requires TargetIsa be PIC".to_owned(),
));
}
let faerie_target = target::translate(&*isa)?;
Ok(Self {
isa,
name,
format,
faerie_target,
collect_traps,
libcall_names,
})
@@ -92,7 +89,7 @@ impl FaerieBuilder {
pub struct FaerieBackend {
isa: Box<TargetIsa>,
artifact: faerie::Artifact,
format: container::Format,
format: BinaryFormat,
trap_manifest: Option<FaerieTrapManifest>,
libcall_names: Box<Fn(ir::LibCall) -> String>,
}
@@ -119,8 +116,8 @@ impl Backend for FaerieBackend {
/// Create a new `FaerieBackend` using the given Cretonne target.
fn new(builder: FaerieBuilder) -> Self {
Self {
artifact: faerie::Artifact::new(builder.isa.triple().clone(), builder.name),
isa: builder.isa,
artifact: faerie::Artifact::new(builder.faerie_target, builder.name),
format: builder.format,
trap_manifest: match builder.collect_traps {
FaerieTrapCollection::Enabled => Some(FaerieTrapManifest::new()),
@@ -290,7 +287,6 @@ impl Backend for FaerieBackend {
fn finish(self) -> FaerieProduct {
FaerieProduct {
artifact: self.artifact,
format: self.format,
trap_manifest: self.trap_manifest,
}
}
@@ -305,8 +301,6 @@ pub struct FaerieProduct {
/// Optional trap manifest. Contains `FaerieTrapManifest` when `FaerieBuilder.collect_traps` is
/// set to `FaerieTrapCollection::Enabled`.
pub trap_manifest: Option<FaerieTrapManifest>,
/// The format that the builder specified for output.
format: container::Format,
}
impl FaerieProduct {
@@ -317,18 +311,12 @@ impl FaerieProduct {
/// Call `emit` on the faerie `Artifact`, producing bytes in memory.
pub fn emit(&self) -> Result<Vec<u8>, Error> {
match self.format {
container::Format::ELF => self.artifact.emit::<faerie::Elf>(),
container::Format::MachO => self.artifact.emit::<faerie::Mach>(),
}
self.artifact.emit()
}
/// Call `write` on the faerie `Artifact`, writing to a file.
pub fn write(&self, sink: File) -> Result<(), Error> {
match self.format {
container::Format::ELF => self.artifact.write::<faerie::Elf>(sink),
container::Format::MachO => self.artifact.write::<faerie::Mach>(sink),
}
self.artifact.write(sink)
}
}
@@ -358,7 +346,7 @@ fn translate_data_linkage(linkage: Linkage, writable: bool) -> faerie::Decl {
}
struct FaerieRelocSink<'a> {
format: container::Format,
format: BinaryFormat,
artifact: &'a mut faerie::Artifact,
name: &'a str,
namespace: &'a ModuleNamespace<'a, FaerieBackend>,

View File

@@ -1,6 +1,7 @@
//! Utilities for working with Faerie container formats.
use cretonne_codegen::binemit::Reloc;
use target_lexicon::BinaryFormat;
/// An object file format.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -13,9 +14,9 @@ pub enum Format {
/// Translate from a Cretonne `Reloc` to a raw object-file-format-specific
/// relocation code.
pub fn raw_relocation(reloc: Reloc, format: Format) -> u32 {
pub fn raw_relocation(reloc: Reloc, format: BinaryFormat) -> u32 {
match format {
Format::ELF => {
BinaryFormat::Elf => {
use goblin::elf;
match reloc {
Reloc::Abs4 => elf::reloc::R_X86_64_32,
@@ -28,6 +29,7 @@ pub fn raw_relocation(reloc: Reloc, format: Format) -> u32 {
_ => unimplemented!(),
}
}
Format::MachO => unimplemented!(),
BinaryFormat::Macho => unimplemented!("macho relocations"),
_ => unimplemented!("unsupported format"),
}
}

View File

@@ -19,10 +19,10 @@ extern crate cretonne_module;
extern crate faerie;
extern crate failure;
extern crate goblin;
extern crate target_lexicon;
mod backend;
mod container;
mod target;
pub mod traps;
pub use backend::{FaerieBackend, FaerieBuilder, FaerieProduct, FaerieTrapCollection};

View File

@@ -1,21 +0,0 @@
use cretonne_codegen::isa;
use cretonne_module::ModuleError;
use faerie::Target;
/// Translate from a Cretonne `TargetIsa` to a Faerie `Target`.
pub fn translate(isa: &isa::TargetIsa) -> Result<Target, ModuleError> {
let name = isa.name();
match name {
"x86" => Ok(if isa.flags().is_64bit() {
Target::X86_64
} else {
Target::X86
}),
"arm32" => Ok(Target::ARMv7),
"arm64" => Ok(Target::ARM64),
_ => Err(ModuleError::Backend(format!(
"unsupported faerie isa: {}",
name
))),
}
}