Commit Graph

80 Commits

Author SHA1 Message Date
Dan Gohman
2c9d03f9bd Let the runtime provide the number of imported functions.
This obviates the need to keep a separate running total of the number of
functions seen.
2017-10-09 17:29:39 -07:00
Dan Gohman
e74bc06380 Move start_index out of TranslationResult and into the WasmRuntime.
This makes it more consistent with how all the rest of the content of
a wasm module is handled. And, now TranslationResult just has a Vec
of translated functions, which will make it easier to refactor further.
2017-10-09 17:29:39 -07:00
Dan Gohman
ef5ad630c8 Use the WasmRuntime's global list rather than keeping a separate list. 2017-10-09 17:29:39 -07:00
Dan Gohman
3841552b7c Use the WasmRuntime's type list rather than keeping a separate list. 2017-10-09 17:29:39 -07:00
Dan Gohman
d1766f0ba4 Simplify code by removing unnecessary Options. 2017-10-06 15:59:30 -07:00
Dan Gohman
7410ddfe08 Use the WasmRuntime's signature list rather than keeping a separate list.
This way, if the runtime modifies the signature, such as to add special
arguments, they are reflected in the resulting function defintions.
2017-10-04 16:57:39 -07:00
Dan Gohman
25b8b45a41 Avoid unnecessary BufReaders. 2017-10-03 09:34:41 -07:00
Jakob Stoklund Olesen
3a34c35f95 Don't swap the arguments to the select instruction.
Both WebAssembly and Cretonne use true-value, false-value.
2017-10-02 14:43:30 -07:00
Jakob Stoklund Olesen
53404a9387 Check for invalid special type constraints.
The extend and reduce instructions have additional type constraints.

Stop inserting sextend instructions after ctz, clz, and popcnt when
translating from WebAssembly. The Cretonne instructions have the same
signature as the WebAssembly equivalents.
2017-09-28 16:30:19 -07:00
Dan Gohman
d13f29cfe4 Update to wasmparser 0.11.2. 2017-09-26 14:58:50 -07:00
Dan Gohman
36585ddc4f Wasm control stack entries only need the number of return types.
This eliminates heap-allocated vectors which stored the actual types.
2017-09-25 13:05:29 -07:00
Dan Gohman
55e48ce7aa Simplify translate_type's return type. 2017-09-25 13:05:29 -07:00
Jakob Stoklund Olesen
85e4e9f511 Assign source locations when translating WebAssembly to Cretonne.
The source locations are byte code offsets relative to the beginning of
the function.
2017-09-21 14:37:54 -07:00
Jakob Stoklund Olesen
e8723be33f Add trap codes to the Cretonne IL.
The trap and trapz/trapnz instructions now take a trap code immediate
operand which indicates the reason for trapping.
2017-09-20 15:50:02 -07:00
Dan Gohman
d3f3fdf5af Remove a redundant .gitignore file. 2017-09-20 15:35:26 -07:00
Jakob Stoklund Olesen
da4cde8117 Always insert the entry EBB before translating any WASM.
The FuncEnvironment callbacks (make_global in particular) may need to
insert code in the entry EBB. We need to make sure the entry EBB has
been inserted in the layout before making those callbacks.
2017-09-18 11:53:32 -07:00
Jakob Stoklund Olesen
1349a6bdbc Always require a Flags reference for verifying functions.
Add a settings::FlagsOrIsa struct which represents a flags reference and
optionally the ISA it belongs to. Use this for passing flags/isa
information to the verifier.

The verify_function() and verify_context() functions are now generic so
they accept either a &Flags or a &TargetISa argument.

Fix the return_at_end verifier tests which no longer require an ISA
specified. The signle "set return_at_end" flag setting now makes it to
the verifier even when no ISA is present to carry it.
2017-09-14 17:51:15 -07:00
Dan Gohman
bbe056bf9d Make passes assert their dependencies consistently. (#156)
* Make passes assert their dependencies consistently.

This avoids ambiguity about whose responsibility it is to run
to compute cfg, domtree, and loop_analysis data.

* Reset the `valid` flag in DominatorTree's `clear()`.

* Remove the redundant assert from DominatorTree::with_function.

* Remove the message strings from obvious asserts.

This avoids having them spill out into multiple lines.

* Refactor calls to `compute` on `Context` objects into helper functions.
2017-09-14 14:38:53 -07:00
Jakob Stoklund Olesen
39992014e0 Make FuncEnvironment callbacks take an &mut self parameter.
The WasmRuntime trait already does this, and nothing in the function
translator requires the environment trait object to be non-mutable.
2017-09-14 12:25:59 -07:00
Dan Gohman
c7b1bb5f9e Simplify using map_err and expect. 2017-09-12 13:40:19 -07:00
Dan Gohman
1ab207b93c Add support for emitting code with a single return at the end. (#153)
This also enables testing of the wasmtests tests.

This also updates for wabt updating to the official "wat" filename
extension, as opposed to "wast".
2017-09-12 13:27:36 -07:00
Dan Gohman
de27abcb2b Change translate_module to use FuncTranslator. 2017-09-11 18:01:50 -07:00
Dan Gohman
95e2566d6f Use TranslationState's popn/peekn functions. (#151)
This entailed reorganizing surrounding code to minimize the extent of
mutable borrows of the control-flow stack.
2017-09-11 13:58:49 -07:00
Dan Gohman
9bce21c17c Update to wasmparser 0.9.4 for a bugfix in create_binary_reader. 2017-09-11 11:34:39 -07:00
Dan Gohman
f8f6878b2c Switch to wasmparser's create_binary_reader() API for reading function bodies.
A subtle difference here is that the `end` at the end of a function body
now gets handled by translate_operator/translate_unreachable_operator, so we
no longer have to do as much special-case cleanup at the end of the function
body.

This is a preperatory step for converting module_translator.rs to use
the new FuncTranslator mechanism.
2017-09-11 11:27:02 -07:00
Dan Gohman
7bf2747e1e Replace a match with a ?. 2017-09-11 08:47:41 -07:00
Dan Gohman
a2542ed71d Replace a match with a .unwrap_or_default(). 2017-09-11 08:47:38 -07:00
Dan Gohman
47bc963ba5 Add a JumpTableData::with_capacity and use it.
As with Vec::with_capacity, this helps reduce intermediate heap allocation.
2017-09-11 08:47:28 -07:00
Dan Gohman
620f1f49e2 Move several functions from FunctionBuilder to Function.
With FuncEnvironment using FuncCursors in place of full
FunctionBuilders, it's useful to move several of these convenience
functions from FunctionBuilder to Function.
2017-09-11 08:40:50 -07:00
Dan Gohman
2fa0a7a3a4 Fix a confusion between EndDataSectionEntry and EndDataSectionEntryBody. 2017-09-08 17:12:32 -07:00
Dan Gohman
3775fa4867 Update to wasmparser 0.9.3.
wasmparser's API is changing in anticipation of streaming decoding, so
it will now hand large data section initializers back in chunks rather
than all at once.
2017-09-08 16:17:31 -07:00
Jakob Stoklund Olesen
439a40e5e4 Add a WebAssembly function translator.
The new FuncTranslator type can be used to translate binary WebAssembly
functions to Cretonne IL one at a time. It is independent of the
module-level parser also present in the cretonne-wasm crate.
2017-09-07 16:12:48 -07:00
Dan Gohman
c0f3eaafbc Publically declare GlobalValue.
It's used in the publically declared FuncEnvironment trait, so library
users should be able to name it.
2017-09-07 10:36:37 -07:00
Dan Gohman
388a96421b Add documentation comments to GlobalValue's fields. 2017-09-07 10:34:26 -07:00
Jakob Stoklund Olesen
d6f6af104b Make translate_operator() generic on FuncEnvironment.
This makes it clear that this function only uses the FuncEnvironment
trait and not WasmRuntime.

Also make the translate_{grow,current}_memory() methods take &self
instead of &mut self. The &mut was left on there by accident.
2017-09-06 16:24:55 -07:00
Jakob Stoklund Olesen
062dd41c93 Don't export the 'Local' type from cton_wasm.
This type is not longer used in any public interface, it has become an
internal implementation detail.

Also remove some unused exported types from the crate.
2017-09-06 15:56:39 -07:00
Jakob Stoklund Olesen
b10faca534 Move translate_grow_memory and translate_current_memory too.
This moves the last instruction-level callbacks into FuncEnvironment
such that the trait has all the information required to translate a
whole function.

Change the position argument to a FuncCursor. This eliminates all
exposure of FunctionBuilder<Local>, so its use properly becomes an
implementation detail.
2017-09-06 15:44:23 -07:00
Jakob Stoklund Olesen
26048c2ecc Move WasmRuntime::translate_call_indirect() into FuncEnvironment.
Add two new arguments:

- table_index is the WebAssembly table referenced in the indirect call.
- sig_index is the WebAssembly signature index. We still have the SigRef
  that was created by make_indirect_sig(), but the WebAssembly signature
  index may be needed for detecting type mismatches at runtime.

Change the insertion location to a plain FuncCursor rather than a
FunctionBuilder<Local>. The fact that cretonne-wasm uses FunctionBuilder
should be an implementation detail, and the callbacks don't need to
access WebAssembly locals, so they don't need the extended interface.

Add a FunctionBuilder::cursor() method which creates a FuncCursor for
inserting instructions in the current EBB.

Also add a FuncEnvironment::translate_call() method which allows the
environment to override direct calls the same way as indirect calls.
2017-09-06 15:18:55 -07:00
Jakob Stoklund Olesen
dc2bee9cef Add a FuncEnvironment::make_direct_func() callback.
This allows the environment to control the signatures used for direct
function calls. The signature and calling convention may depend on
whether the function is imported or local.

Also add WasmRuntime::declare_func_{import,type} to notify the runtime
about imported and local functions. This is necessary so the runtime
knows what function indexes are referring to .

Since imported and local functions are now declared to the runtime, it
is no longer necessary to return hashes mapping between WebAssembly
indexes and Cretonne entities.

Also stop return null entries for the imported functions in the
TranslationResult. Just return a vector of local functions.
2017-09-06 12:36:19 -07:00
Jakob Stoklund Olesen
27e9e16077 Add a FuncEnvironment::make_indirect_sig() callback.
The function environment is now expected to keep track of the function
signatures in the module, and it is asked to generate Cretonne
signatures to be used for indirect calls.

The combination of make_indirect_sig() and translate_call_indirect()
callbacks allow the runtime to insert additional function arguments for
indirect calls such as vmctx pointers and CFI-style signature identifiers.
2017-09-06 10:28:11 -07:00
Dan Gohman
320c88f365 Rename cretonne_wasm to cton_wasm, for consistency with the other libraries. 2017-09-05 16:30:44 -07:00
Jakob Stoklund Olesen
0ac1d0dd94 Add FuncEnvironment trait.
This trait is used to provide the environment necessary to translate a
single WebAssembly function without having other global data structures
for the WebAssembly module.

The WasmRuntime trait extends the FuncEnvironment trait for those uses
that want to parse a whole WebAssembly module.

- Change the handling of WebAssembly globals to use the FuncEnvironment
  trait as well as the new GlobalVar infrastructure in Cretonne. The
  runtime is not consulted on the translation of each
  get_global/get_global instruction. Instead it gets to create the
  GlobalVar declaration in the function preamble the first time the
  global is used.

- Change the handling of heap load/store instructions to use the new
  Heap infrastructure in Cretonne. The runtime is called to create the
  Heap declaration in the preamble. It is not involved in individual
  load/store instructions.
2017-09-05 16:17:52 -07:00
Jakob Stoklund Olesen
19c8ba5021 Eliminate the sig argument to translate_operator.
The current function's return types are pushed as the first control
stack frame.
2017-09-05 11:51:32 -07:00
Jakob Stoklund Olesen
2671cbb092 Add TranslationState::in_unreachable_code().
Move an unreachable code test and sanity check into this method.
2017-09-05 11:46:08 -07:00
Jakob Stoklund Olesen
6f864f2926 Move translation state initialization into a method. 2017-09-05 11:00:40 -07:00
Dan Gohman
ef3ea72422 Avoid calling Vec::split_off, avoiding more heap allocations. 2017-09-05 09:23:20 -07:00
Jakob Stoklund Olesen
03939e4f9f Move TranslationState into its own module.
Add some convenience methods for common state access patterns.
2017-09-01 15:55:02 -07:00
Jakob Stoklund Olesen
6762194801 Move stack and control_stack into TranslationState.
This reduces the number of function arguments passed around and it keeps
related information together.
2017-09-01 14:29:35 -07:00
Dan Gohman
fe12fe0e63 Avoid unneeded calls to .as_slice(). 2017-09-01 12:15:14 -07:00
Dan Gohman
9d2fbdae62 Avoid cloning a jump argument vector to the heap. 2017-09-01 12:15:14 -07:00