LCOV - code coverage report
Current view: top level - src - assembler.h (source / functions) Hit Total Coverage
Test: app.info Lines: 24 25 96.0 %
Date: 2019-01-20 Functions: 4 7 57.1 %

          Line data    Source code
       1             : // Copyright (c) 1994-2006 Sun Microsystems Inc.
       2             : // All Rights Reserved.
       3             : //
       4             : // Redistribution and use in source and binary forms, with or without
       5             : // modification, are permitted provided that the following conditions are
       6             : // met:
       7             : //
       8             : // - Redistributions of source code must retain the above copyright notice,
       9             : // this list of conditions and the following disclaimer.
      10             : //
      11             : // - Redistribution in binary form must reproduce the above copyright
      12             : // notice, this list of conditions and the following disclaimer in the
      13             : // documentation and/or other materials provided with the distribution.
      14             : //
      15             : // - Neither the name of Sun Microsystems or the names of contributors may
      16             : // be used to endorse or promote products derived from this software without
      17             : // specific prior written permission.
      18             : //
      19             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
      20             : // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
      21             : // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
      22             : // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
      23             : // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      24             : // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      25             : // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
      26             : // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      27             : // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      28             : // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      29             : // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30             : 
      31             : // The original source code covered by the above license above has been
      32             : // modified significantly by Google Inc.
      33             : // Copyright 2012 the V8 project authors. All rights reserved.
      34             : 
      35             : #ifndef V8_ASSEMBLER_H_
      36             : #define V8_ASSEMBLER_H_
      37             : 
      38             : #include <forward_list>
      39             : 
      40             : #include "src/code-comments.h"
      41             : #include "src/cpu-features.h"
      42             : #include "src/deoptimize-reason.h"
      43             : #include "src/external-reference.h"
      44             : #include "src/flags.h"
      45             : #include "src/globals.h"
      46             : #include "src/handles.h"
      47             : #include "src/objects.h"
      48             : #include "src/reglist.h"
      49             : #include "src/reloc-info.h"
      50             : 
      51             : namespace v8 {
      52             : 
      53             : // Forward declarations.
      54             : class ApiFunction;
      55             : 
      56             : namespace internal {
      57             : 
      58             : // Forward declarations.
      59             : class EmbeddedData;
      60             : class InstructionStream;
      61             : class Isolate;
      62             : class SCTableReference;
      63             : class SourcePosition;
      64             : class StatsCounter;
      65             : class StringConstantBase;
      66             : 
      67             : // -----------------------------------------------------------------------------
      68             : // Optimization for far-jmp like instructions that can be replaced by shorter.
      69             : 
      70      133830 : class JumpOptimizationInfo {
      71             :  public:
      72             :   bool is_collecting() const { return stage_ == kCollection; }
      73             :   bool is_optimizing() const { return stage_ == kOptimization; }
      74       55608 :   void set_optimizing() { stage_ = kOptimization; }
      75             : 
      76             :   bool is_optimizable() const { return optimizable_; }
      77       55608 :   void set_optimizable() { optimizable_ = true; }
      78             : 
      79             :   // Used to verify the instruction sequence is always the same in two stages.
      80             :   size_t hash_code() const { return hash_code_; }
      81       64960 :   void set_hash_code(size_t hash_code) { hash_code_ = hash_code; }
      82             : 
      83             :   std::vector<uint32_t>& farjmp_bitmap() { return farjmp_bitmap_; }
      84             : 
      85             :  private:
      86             :   enum { kCollection, kOptimization } stage_ = kCollection;
      87             :   bool optimizable_ = false;
      88             :   std::vector<uint32_t> farjmp_bitmap_;
      89             :   size_t hash_code_ = 0u;
      90             : };
      91             : 
      92             : class HeapObjectRequest {
      93             :  public:
      94             :   explicit HeapObjectRequest(double heap_number, int offset = -1);
      95             :   explicit HeapObjectRequest(const StringConstantBase* string, int offset = -1);
      96             : 
      97             :   enum Kind { kHeapNumber, kStringConstant };
      98             :   Kind kind() const { return kind_; }
      99             : 
     100             :   double heap_number() const {
     101             :     DCHECK_EQ(kind(), kHeapNumber);
     102             :     return value_.heap_number;
     103             :   }
     104             : 
     105             :   const StringConstantBase* string() const {
     106             :     DCHECK_EQ(kind(), kStringConstant);
     107             :     return value_.string;
     108             :   }
     109             : 
     110             :   // The code buffer offset at the time of the request.
     111             :   int offset() const {
     112             :     DCHECK_GE(offset_, 0);
     113             :     return offset_;
     114             :   }
     115             :   void set_offset(int offset) {
     116             :     DCHECK_LT(offset_, 0);
     117       46435 :     offset_ = offset;
     118             :     DCHECK_GE(offset_, 0);
     119             :   }
     120             : 
     121             :  private:
     122             :   Kind kind_;
     123             : 
     124             :   union {
     125             :     double heap_number;
     126             :     const StringConstantBase* string;
     127             :   } value_;
     128             : 
     129             :   int offset_;
     130             : };
     131             : 
     132             : // -----------------------------------------------------------------------------
     133             : // Platform independent assembler base class.
     134             : 
     135             : enum class CodeObjectRequired { kNo, kYes };
     136             : 
     137     1400743 : struct V8_EXPORT_PRIVATE AssemblerOptions {
     138             :   // Prohibits using any V8-specific features of assembler like (isolates,
     139             :   // heap objects, external references, etc.).
     140             :   bool v8_agnostic_code = false;
     141             :   // Recording reloc info for external references and off-heap targets is
     142             :   // needed whenever code is serialized, e.g. into the snapshot or as a WASM
     143             :   // module. This flag allows this reloc info to be disabled for code that
     144             :   // will not survive process destruction.
     145             :   bool record_reloc_info_for_serialization = true;
     146             :   // Recording reloc info can be disabled wholesale. This is needed when the
     147             :   // assembler is used on existing code directly (e.g. JumpTableAssembler)
     148             :   // without any buffer to hold reloc information.
     149             :   bool disable_reloc_info_for_patching = false;
     150             :   // Enables access to exrefs by computing a delta from the root array.
     151             :   // Only valid if code will not survive the process.
     152             :   bool enable_root_array_delta_access = false;
     153             :   // Enables specific assembler sequences only used for the simulator.
     154             :   bool enable_simulator_code = false;
     155             :   // Enables use of isolate-independent constants, indirected through the
     156             :   // root array.
     157             :   // (macro assembler feature).
     158             :   bool isolate_independent_code = false;
     159             :   // Enables the use of isolate-independent builtins through an off-heap
     160             :   // trampoline. (macro assembler feature).
     161             :   bool inline_offheap_trampolines = false;
     162             :   // On some platforms, all code is within a given range in the process,
     163             :   // and the start of this range is configured here.
     164             :   Address code_range_start = 0;
     165             :   // Enable pc-relative calls/jumps on platforms that support it. When setting
     166             :   // this flag, the code range must be small enough to fit all offsets into
     167             :   // the instruction immediates.
     168             :   bool use_pc_relative_calls_and_jumps = false;
     169             : 
     170             :   // Constructs V8-agnostic set of options from current state.
     171             :   AssemblerOptions EnableV8AgnosticCode() const;
     172             : 
     173             :   static AssemblerOptions Default(
     174             :       Isolate* isolate, bool explicitly_support_serialization = false);
     175             : };
     176             : 
     177     6520519 : class AssemblerBuffer {
     178             :  public:
     179     6520358 :   virtual ~AssemblerBuffer() = default;
     180             :   virtual byte* start() const = 0;
     181             :   virtual int size() const = 0;
     182             :   // Return a grown copy of this buffer. The contained data is uninitialized.
     183             :   // The data in {this} will still be read afterwards (until {this} is
     184             :   // destructed), but not written.
     185             :   virtual std::unique_ptr<AssemblerBuffer> Grow(int new_size)
     186             :       V8_WARN_UNUSED_RESULT = 0;
     187             : };
     188             : 
     189             : // Allocate an AssemblerBuffer which uses an existing buffer. This buffer cannot
     190             : // grow, so it must be large enough for all code emitted by the Assembler.
     191             : V8_EXPORT_PRIVATE
     192             : std::unique_ptr<AssemblerBuffer> ExternalAssemblerBuffer(void* buffer,
     193             :                                                          int size);
     194             : 
     195             : // Allocate a new growable AssemblerBuffer with a given initial size.
     196             : V8_EXPORT_PRIVATE
     197             : std::unique_ptr<AssemblerBuffer> NewAssemblerBuffer(int size);
     198             : 
     199    12956689 : class V8_EXPORT_PRIVATE AssemblerBase : public Malloced {
     200             :  public:
     201             :   AssemblerBase(const AssemblerOptions& options,
     202             :                 std::unique_ptr<AssemblerBuffer>);
     203             :   virtual ~AssemblerBase();
     204             : 
     205             :   const AssemblerOptions& options() const { return options_; }
     206             : 
     207             :   bool emit_debug_code() const { return emit_debug_code_; }
     208       45129 :   void set_emit_debug_code(bool value) { emit_debug_code_ = value; }
     209             : 
     210             :   bool predictable_code_size() const { return predictable_code_size_; }
     211           0 :   void set_predictable_code_size(bool value) { predictable_code_size_ = value; }
     212             : 
     213             :   uint64_t enabled_cpu_features() const { return enabled_cpu_features_; }
     214             :   void set_enabled_cpu_features(uint64_t features) {
     215             :     enabled_cpu_features_ = features;
     216             :   }
     217             :   // Features are usually enabled by CpuFeatureScope, which also asserts that
     218             :   // the features are supported before they are enabled.
     219             :   bool IsEnabled(CpuFeature f) {
     220             :     return (enabled_cpu_features_ & (static_cast<uint64_t>(1) << f)) != 0;
     221             :   }
     222             :   void EnableCpuFeature(CpuFeature f) {
     223     6472701 :     enabled_cpu_features_ |= (static_cast<uint64_t>(1) << f);
     224             :   }
     225             : 
     226             :   bool is_constant_pool_available() const {
     227             :     if (FLAG_enable_embedded_constant_pool) {
     228             :       return constant_pool_available_;
     229             :     } else {
     230             :       // Embedded constant pool not supported on this architecture.
     231             :       UNREACHABLE();
     232             :     }
     233             :   }
     234             : 
     235             :   JumpOptimizationInfo* jump_optimization_info() {
     236             :     return jump_optimization_info_;
     237             :   }
     238             :   void set_jump_optimization_info(JumpOptimizationInfo* jump_opt) {
     239     2949430 :     jump_optimization_info_ = jump_opt;
     240             :   }
     241             : 
     242             :   // Overwrite a host NaN with a quiet target NaN.  Used by mksnapshot for
     243             :   // cross-snapshotting.
     244             :   static void QuietNaN(HeapObject nan) {}
     245             : 
     246   158355404 :   int pc_offset() const { return static_cast<int>(pc_ - buffer_start_); }
     247             : 
     248             :   // This function is called when code generation is aborted, so that
     249             :   // the assembler could clean up internal data structures.
     250           9 :   virtual void AbortedCodeGeneration() { }
     251             : 
     252             :   // Debugging
     253             :   void Print(Isolate* isolate);
     254             : 
     255             :   // Record an inline code comment that can be used by a disassembler.
     256             :   // Use --code-comments to enable.
     257     4294462 :   void RecordComment(const char* msg) {
     258     4290137 :     if (FLAG_code_comments) {
     259       12975 :       code_comments_writer_.Add(pc_offset(), std::string(msg));
     260             :     }
     261     4290137 :   }
     262             : 
     263             :   static const int kMinimalBufferSize = 4*KB;
     264             : 
     265             :   static void FlushICache(void* start, size_t size);
     266      447387 :   static void FlushICache(Address start, size_t size) {
     267    11678950 :     return FlushICache(reinterpret_cast<void*>(start), size);
     268             :   }
     269             : 
     270             :  protected:
     271             :   // Add 'target' to the {code_targets_} vector, if necessary, and return the
     272             :   // offset at which it is stored.
     273             :   int AddCodeTarget(Handle<Code> target);
     274             :   Handle<Code> GetCodeTarget(intptr_t code_target_index) const;
     275             :   // Update to the code target at {code_target_index} to {target}.
     276             :   void UpdateCodeTarget(intptr_t code_target_index, Handle<Code> target);
     277             :   // Reserves space in the code target vector.
     278             :   void ReserveCodeTargetSpace(size_t num_of_code_targets);
     279             : 
     280             :   // The buffer into which code and relocation info are generated.
     281             :   std::unique_ptr<AssemblerBuffer> buffer_;
     282             :   // Cached from {buffer_->start()}, for faster access.
     283             :   byte* buffer_start_;
     284             :   std::forward_list<HeapObjectRequest> heap_object_requests_;
     285             :   // The program counter, which points into the buffer above and moves forward.
     286             :   // TODO(jkummerow): This should probably have type {Address}.
     287             :   byte* pc_;
     288             : 
     289             :   void set_constant_pool_available(bool available) {
     290             :     if (FLAG_enable_embedded_constant_pool) {
     291             :       constant_pool_available_ = available;
     292             :     } else {
     293             :       // Embedded constant pool not supported on this architecture.
     294             :       UNREACHABLE();
     295             :     }
     296             :   }
     297             : 
     298             :   // {RequestHeapObject} records the need for a future heap number allocation,
     299             :   // code stub generation or string allocation. After code assembly, each
     300             :   // platform's {Assembler::AllocateAndInstallRequestedHeapObjects} will
     301             :   // allocate these objects and place them where they are expected (determined
     302             :   // by the pc offset associated with each request).
     303             :   void RequestHeapObject(HeapObjectRequest request);
     304             : 
     305     5901906 :   bool ShouldRecordRelocInfo(RelocInfo::Mode rmode) const {
     306             :     DCHECK(!RelocInfo::IsNone(rmode));
     307    17595633 :     if (options().disable_reloc_info_for_patching) return false;
     308    23743613 :     if (RelocInfo::IsOnlyForSerializer(rmode) &&
     309    23497430 :         !options().record_reloc_info_for_serialization && !emit_debug_code()) {
     310             :       return false;
     311             :     }
     312             :     return true;
     313             :   }
     314             : 
     315             :   CodeCommentsWriter code_comments_writer_;
     316             : 
     317             :  private:
     318             :   // Before we copy code into the code space, we sometimes cannot encode
     319             :   // call/jump code targets as we normally would, as the difference between the
     320             :   // instruction's location in the temporary buffer and the call target is not
     321             :   // guaranteed to fit in the instruction's offset field. We keep track of the
     322             :   // code handles we encounter in calls in this vector, and encode the index of
     323             :   // the code handle in the vector instead.
     324             :   std::vector<Handle<Code>> code_targets_;
     325             : 
     326             :   const AssemblerOptions options_;
     327             :   uint64_t enabled_cpu_features_;
     328             :   bool emit_debug_code_;
     329             :   bool predictable_code_size_;
     330             : 
     331             :   // Indicates whether the constant pool can be accessed, which is only possible
     332             :   // if the pp register points to the current code object's constant pool.
     333             :   bool constant_pool_available_;
     334             : 
     335             :   JumpOptimizationInfo* jump_optimization_info_;
     336             : 
     337             :   // Constant pool.
     338             :   friend class FrameAndConstantPoolScope;
     339             :   friend class ConstantPoolUnavailableScope;
     340             : };
     341             : 
     342             : // Avoids emitting debug code during the lifetime of this scope object.
     343             : class DontEmitDebugCodeScope {
     344             :  public:
     345             :   explicit DontEmitDebugCodeScope(AssemblerBase* assembler)
     346             :       : assembler_(assembler), old_value_(assembler->emit_debug_code()) {
     347             :     assembler_->set_emit_debug_code(false);
     348             :   }
     349             :   ~DontEmitDebugCodeScope() {
     350             :     assembler_->set_emit_debug_code(old_value_);
     351             :   }
     352             :  private:
     353             :   AssemblerBase* assembler_;
     354             :   bool old_value_;
     355             : };
     356             : 
     357             : 
     358             : // Avoids using instructions that vary in size in unpredictable ways between the
     359             : // snapshot and the running VM.
     360             : class PredictableCodeSizeScope {
     361             :  public:
     362             :   PredictableCodeSizeScope(AssemblerBase* assembler, int expected_size);
     363             :   ~PredictableCodeSizeScope();
     364             : 
     365             :  private:
     366             :   AssemblerBase* const assembler_;
     367             :   int const expected_size_;
     368             :   int const start_offset_;
     369             :   bool const old_value_;
     370             : };
     371             : 
     372             : 
     373             : // Enable a specified feature within a scope.
     374             : class CpuFeatureScope {
     375             :  public:
     376             :   enum CheckPolicy {
     377             :     kCheckSupported,
     378             :     kDontCheckSupported,
     379             :   };
     380             : 
     381             : #ifdef DEBUG
     382             :   CpuFeatureScope(AssemblerBase* assembler, CpuFeature f,
     383             :                   CheckPolicy check = kCheckSupported);
     384             :   ~CpuFeatureScope();
     385             : 
     386             :  private:
     387             :   AssemblerBase* assembler_;
     388             :   uint64_t old_enabled_;
     389             : #else
     390             :   CpuFeatureScope(AssemblerBase* assembler, CpuFeature f,
     391             :                   CheckPolicy check = kCheckSupported) {}
     392             :   ~CpuFeatureScope() {  // NOLINT (modernize-use-equals-default)
     393             :     // Define a destructor to avoid unused variable warnings.
     394             :   }
     395             : #endif
     396             : };
     397             : 
     398             : }  // namespace internal
     399             : }  // namespace v8
     400             : #endif  // V8_ASSEMBLER_H_

Generated by: LCOV version 1.10