Introduce a TargetFrontendConfig type. (#570)

* Introduce a `TargetFrontendConfig` type.

`TargetFrontendConfig` is information specific to the target which is
provided to frontends to allow them to produce Cranelift IR for the
target. Currently this includes the pointer size and the default calling
convention.

The default calling convention is now inferred from the target, rather
than being a setting. cranelift-native is now just a provider of target
information, rather than also being a provider of settings, which gives
it a clearer role.

And instead of having cranelift-frontend routines require the whole
`TargetIsa`, just require the `TargetFrontendConfig`, and add a way to
get the `TargetFrontendConfig` from a `Module`.

Fixes #529.
Fixes #555.
This commit is contained in:
Dan Gohman
2018-11-02 13:51:42 -07:00
committed by GitHub
parent 7094d9f470
commit d4f8eb7453
27 changed files with 297 additions and 168 deletions

View File

@@ -2,9 +2,8 @@
//! traits `FunctionEnvironment` and `ModuleEnvironment`.
use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir::{self, InstBuilder};
use cranelift_codegen::settings::Flags;
use cranelift_codegen::isa::TargetFrontendConfig;
use std::vec::Vec;
use target_lexicon::Triple;
use translation_utils::{
FuncIndex, Global, GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
};
@@ -89,22 +88,19 @@ pub enum ReturnMode {
/// IR. The function environment provides information about the WebAssembly module as well as the
/// runtime environment.
pub trait FuncEnvironment {
/// Get the triple for the current compilation.
fn triple(&self) -> &Triple;
/// Get the flags for the current compilation.
fn flags(&self) -> &Flags;
/// Get the information needed to produce Cranelift IR for the given target.
fn target_config(&self) -> TargetFrontendConfig;
/// Get the Cranelift integer type to use for native pointers.
///
/// This returns `I64` for 64-bit architectures and `I32` for 32-bit architectures.
fn pointer_type(&self) -> ir::Type {
ir::Type::int(u16::from(self.triple().pointer_width().unwrap().bits())).unwrap()
ir::Type::int(u16::from(self.target_config().pointer_bits())).unwrap()
}
/// Get the size of a native pointer, in bytes.
fn pointer_bytes(&self) -> u8 {
self.triple().pointer_width().unwrap().bytes()
self.target_config().pointer_bytes()
}
/// Set up the necessary preamble definitions in `func` to access the global variable
@@ -239,8 +235,8 @@ pub trait FuncEnvironment {
/// [`translate_module`](fn.translate_module.html) function. These methods should not be called
/// by the user, they are only for `cranelift-wasm` internal use.
pub trait ModuleEnvironment<'data> {
/// Get the flags for the current compilation.
fn flags(&self) -> &Flags;
/// Get the information needed to produce Cranelift IR for the current target.
fn target_config(&self) -> &TargetFrontendConfig;
/// Return the name for the given function index.
fn get_func_name(&self, func_index: FuncIndex) -> ir::ExternalName;