baseline.cc 3.46 KB
Newer Older
1 2 3 4 5 6
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/baseline/baseline.h"

7 8
#include "src/handles/maybe-handles.h"

9 10
// TODO(v8:11421): Remove #if once baseline compiler is ported to other
// architectures.
11 12 13
#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_ARM64 ||     \
    V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_RISCV64 || V8_TARGET_ARCH_MIPS64 || \
    V8_TARGET_ARCH_MIPS
14

15
#include "src/baseline/baseline-assembler-inl.h"
16
#include "src/baseline/baseline-compiler.h"
17
#include "src/debug/debug.h"
18
#include "src/heap/factory-inl.h"
19
#include "src/logging/runtime-call-stats-scope.h"
20 21 22 23 24 25
#include "src/objects/script-inl.h"
#include "src/objects/shared-function-info-inl.h"

namespace v8 {
namespace internal {

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
bool CanCompileWithBaseline(Isolate* isolate,
                            Handle<SharedFunctionInfo> shared) {
  // Check that baseline compiler is enabled.
  if (!FLAG_sparkplug) return false;

  // Check if we actually have bytecode.
  if (!shared->HasBytecodeArray()) return false;

  // Do not optimize when debugger needs to hook into every call.
  if (isolate->debug()->needs_check_on_function_call()) return false;

  // Functions with breakpoints have to stay interpreted.
  if (shared->HasBreakInfo()) return false;

  // Do not baseline compile if function doesn't pass sparkplug_filter.
  if (!shared->PassesFilter(FLAG_sparkplug_filter)) return false;

  return true;
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
namespace {
MaybeHandle<Code> GenerateOnHeapCode(Isolate* isolate,
                                     Handle<SharedFunctionInfo> shared,
                                     Handle<BytecodeArray> bytecode) {
  CodePageCollectionMemoryModificationScope code_allocation(isolate->heap());
  baseline::BaselineCompiler compiler(isolate, shared, bytecode,
                                      baseline::BaselineCompiler::kOnHeap);
  compiler.GenerateCode();
  return compiler.Build(isolate);
}

MaybeHandle<Code> GenerateOffHeapCode(Isolate* isolate,
                                      Handle<SharedFunctionInfo> shared,
                                      Handle<BytecodeArray> bytecode) {
  baseline::BaselineCompiler compiler(isolate, shared, bytecode);
  compiler.GenerateCode();
  return compiler.Build(isolate);
}

}  // namespace

67 68
MaybeHandle<Code> GenerateBaselineCode(Isolate* isolate,
                                       Handle<SharedFunctionInfo> shared) {
69
  RCS_SCOPE(isolate, RuntimeCallCounterId::kCompileBaseline);
70 71 72 73
  Handle<BytecodeArray> bytecode(shared->GetBytecodeArray(isolate), isolate);
  MaybeHandle<Code> code = FLAG_sparkplug_on_heap
                               ? GenerateOnHeapCode(isolate, shared, bytecode)
                               : GenerateOffHeapCode(isolate, shared, bytecode);
74 75
  if (FLAG_print_code && !code.is_null()) {
    code.ToHandleChecked()->Print();
76 77 78 79
  }
  return code;
}

80 81 82 83
void EmitReturnBaseline(MacroAssembler* masm) {
  baseline::BaselineAssembler::EmitReturn(masm);
}

84 85 86 87 88 89 90 91
}  // namespace internal
}  // namespace v8

#else

namespace v8 {
namespace internal {

92 93 94 95 96
bool CanCompileWithBaseline(Isolate* isolate,
                            Handle<SharedFunctionInfo> shared) {
  return false;
}

97 98
MaybeHandle<Code> GenerateBaselineCode(Isolate* isolate,
                                       Handle<SharedFunctionInfo> shared) {
99 100 101
  UNREACHABLE();
}

102 103
void EmitReturnBaseline(MacroAssembler* masm) { UNREACHABLE(); }

104 105 106 107
}  // namespace internal
}  // namespace v8

#endif