tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WasmCompile.h (4667B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 *
      4 * Copyright 2015 Mozilla Foundation
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *     http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 #ifndef wasm_compile_h
     20 #define wasm_compile_h
     21 
     22 #include "vm/Runtime.h"
     23 #include "wasm/WasmModule.h"
     24 
     25 namespace JS {
     26 class OptimizedEncodingListener;
     27 }
     28 
     29 namespace js {
     30 namespace wasm {
     31 
     32 class Code;
     33 
     34 // Return a uint32_t which captures the observed properties of the CPU that
     35 // affect compilation. If code compiled now is to be serialized and executed
     36 // later, the ObservedCPUFeatures() must be ensured to be the same.
     37 
     38 uint32_t ObservedCPUFeatures();
     39 
     40 // Return the estimated compiled (machine) code size for the given bytecode size
     41 // compiled at the given tier.
     42 
     43 double EstimateCompiledCodeSize(Tier tier, size_t bytecodeSize);
     44 
     45 // Compile the given WebAssembly bytecode with the given arguments into a
     46 // wasm::Module. On success, the Module is returned. On failure, the returned
     47 // SharedModule pointer is null and either:
     48 //  - *error points to a string description of the error
     49 //  - *error is null and the caller should report out-of-memory.
     50 
     51 SharedModule CompileBuffer(const CompileArgs& args,
     52                           const BytecodeBufferOrSource& bytecode,
     53                           UniqueChars* error, UniqueCharsVector* warnings,
     54                           JS::OptimizedEncodingListener* listener = nullptr);
     55 
     56 // Attempt to compile the second tier of the given wasm::Module.
     57 
     58 bool CompileCompleteTier2(const ShareableBytes* codeSection,
     59                          const Module& module, UniqueChars* error,
     60                          UniqueCharsVector* warnings,
     61                          mozilla::Atomic<bool>* cancelled);
     62 
     63 // Attempt to compile the second tier for the given functions of a wasm::Module.
     64 
     65 bool CompilePartialTier2(const Code& code, uint32_t funcIndex,
     66                         UniqueChars* error, UniqueCharsVector* warnings,
     67                         mozilla::Atomic<bool>* cancelled);
     68 
     69 // Compile the given WebAssembly module which has been broken into three
     70 // partitions:
     71 //  - envBytes contains a complete ModuleMetadata that has already been
     72 //    copied in from the stream.
     73 //  - codeBytes is pre-sized to hold the complete code section when the stream
     74 //    completes.
     75 //  - The range [codeBytes.begin(), codeBytesEnd) contains the bytes currently
     76 //    read from the stream and codeBytesEnd will advance until either
     77 //    the stream is cancelled or codeBytesEnd == codeBytes.end().
     78 //  - streamEnd contains the final information received after the code section:
     79 //    the remaining module bytecodes and maybe a JS::OptimizedEncodingListener.
     80 //    When the stream is successfully closed, streamEnd.reached is set.
     81 // The ExclusiveWaitableData are notified when CompileStreaming() can make
     82 // progress (i.e., codeBytesEnd advances or streamEnd.reached is set).
     83 // If cancelled is set to true, compilation aborts and returns null. After
     84 // cancellation is set, both ExclusiveWaitableData will be notified and so every
     85 // wait() loop must check cancelled.
     86 
     87 using ExclusiveBytesPtr = ExclusiveWaitableData<const uint8_t*>;
     88 
     89 struct StreamEndData {
     90  bool reached;
     91  const ShareableBytes* tailBytes;
     92  CompleteTier2Listener completeTier2Listener;
     93 
     94  StreamEndData() : reached(false), tailBytes(nullptr) {}
     95 };
     96 using ExclusiveStreamEndData = ExclusiveWaitableData<StreamEndData>;
     97 
     98 SharedModule CompileStreaming(const CompileArgs& args,
     99                              const ShareableBytes& envBytes,
    100                              const ShareableBytes& codeBytes,
    101                              const ExclusiveBytesPtr& codeBytesEnd,
    102                              const ExclusiveStreamEndData& streamEnd,
    103                              const mozilla::Atomic<bool>& cancelled,
    104                              UniqueChars* error, UniqueCharsVector* warnings);
    105 
    106 bool DumpIonFunctionInModule(const ShareableBytes& bytecode,
    107                             uint32_t targetFuncIndex, GenericPrinter& out,
    108                             UniqueChars* error);
    109 
    110 }  // namespace wasm
    111 }  // namespace js
    112 
    113 #endif  // namespace wasm_compile_h