codegen.h revision 1b268ca467c924004286c97bac133db489cf43d0
1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_CODEGEN_H_
6#define V8_CODEGEN_H_
7
8#include "src/code-stubs.h"
9#include "src/runtime/runtime.h"
10
11// Include the declaration of the architecture defined class CodeGenerator.
12// The contract  to the shared code is that the the CodeGenerator is a subclass
13// of Visitor and that the following methods are available publicly:
14//   MakeCode
15//   MakeCodePrologue
16//   MakeCodeEpilogue
17//   masm
18//   frame
19//   script
20//   has_valid_frame
21//   SetFrame
22//   DeleteFrame
23//   allocator
24//   AddDeferred
25//   in_spilled_code
26//   set_in_spilled_code
27//   RecordPositions
28//
29// These methods are either used privately by the shared code or implemented as
30// shared code:
31//   CodeGenerator
32//   ~CodeGenerator
33//   Generate
34//   ComputeLazyCompile
35//   ProcessDeclarations
36//   DeclareGlobals
37//   CheckForInlineRuntimeCall
38//   AnalyzeCondition
39//   CodeForFunctionPosition
40//   CodeForReturnPosition
41//   CodeForStatementPosition
42//   CodeForDoWhileConditionPosition
43//   CodeForSourcePosition
44
45#if V8_TARGET_ARCH_IA32
46#include "src/ia32/codegen-ia32.h"  // NOLINT
47#elif V8_TARGET_ARCH_X64
48#include "src/x64/codegen-x64.h"  // NOLINT
49#elif V8_TARGET_ARCH_ARM64
50#include "src/arm64/codegen-arm64.h"  // NOLINT
51#elif V8_TARGET_ARCH_ARM
52#include "src/arm/codegen-arm.h"  // NOLINT
53#elif V8_TARGET_ARCH_PPC
54#include "src/ppc/codegen-ppc.h"  // NOLINT
55#elif V8_TARGET_ARCH_MIPS
56#include "src/mips/codegen-mips.h"  // NOLINT
57#elif V8_TARGET_ARCH_MIPS64
58#include "src/mips64/codegen-mips64.h"  // NOLINT
59#elif V8_TARGET_ARCH_S390
60#include "src/s390/codegen-s390.h"  // NOLINT
61#elif V8_TARGET_ARCH_X87
62#include "src/x87/codegen-x87.h"  // NOLINT
63#else
64#error Unsupported target architecture.
65#endif
66
67namespace v8 {
68namespace internal {
69
70
71class CompilationInfo;
72
73
74class CodeGenerator {
75 public:
76  // Printing of AST, etc. as requested by flags.
77  static void MakeCodePrologue(CompilationInfo* info, const char* kind);
78
79  // Allocate and install the code.
80  static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
81                                       CompilationInfo* info);
82
83  // Print the code after compiling it.
84  static void PrintCode(Handle<Code> code, CompilationInfo* info);
85
86 private:
87  DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
88};
89
90
91// Results of the library implementation of transcendental functions may differ
92// from the one we use in our generated code.  Therefore we use the same
93// generated code both in runtime and compiled code.
94typedef double (*UnaryMathFunctionWithIsolate)(double x, Isolate* isolate);
95
96UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate);
97UnaryMathFunctionWithIsolate CreateSqrtFunction(Isolate* isolate);
98
99
100double modulo(double x, double y);
101
102// Custom implementation of math functions.
103double fast_exp(double input, Isolate* isolate);
104double fast_sqrt(double input, Isolate* isolate);
105void lazily_initialize_fast_exp(Isolate* isolate);
106void lazily_initialize_fast_sqrt(Isolate* isolate);
107
108
109class ElementsTransitionGenerator : public AllStatic {
110 public:
111  // If |mode| is set to DONT_TRACK_ALLOCATION_SITE,
112  // |allocation_memento_found| may be NULL.
113  static void GenerateMapChangeElementsTransition(
114      MacroAssembler* masm,
115      Register receiver,
116      Register key,
117      Register value,
118      Register target_map,
119      AllocationSiteMode mode,
120      Label* allocation_memento_found);
121  static void GenerateSmiToDouble(
122      MacroAssembler* masm,
123      Register receiver,
124      Register key,
125      Register value,
126      Register target_map,
127      AllocationSiteMode mode,
128      Label* fail);
129  static void GenerateDoubleToObject(
130      MacroAssembler* masm,
131      Register receiver,
132      Register key,
133      Register value,
134      Register target_map,
135      AllocationSiteMode mode,
136      Label* fail);
137
138 private:
139  DISALLOW_COPY_AND_ASSIGN(ElementsTransitionGenerator);
140};
141
142static const int kNumberDictionaryProbes = 4;
143
144
145class CodeAgingHelper {
146 public:
147  explicit CodeAgingHelper(Isolate* isolate);
148
149  uint32_t young_sequence_length() const { return young_sequence_.length(); }
150  bool IsYoung(byte* candidate) const {
151    return memcmp(candidate,
152                  young_sequence_.start(),
153                  young_sequence_.length()) == 0;
154  }
155  void CopyYoungSequenceTo(byte* new_buffer) const {
156    CopyBytes(new_buffer, young_sequence_.start(), young_sequence_.length());
157  }
158
159#ifdef DEBUG
160  bool IsOld(byte* candidate) const;
161#endif
162
163 protected:
164  const EmbeddedVector<byte, kNoCodeAgeSequenceLength> young_sequence_;
165#ifdef DEBUG
166#ifdef V8_TARGET_ARCH_ARM64
167  const EmbeddedVector<byte, kNoCodeAgeSequenceLength> old_sequence_;
168#endif
169#endif
170};
171
172}  // namespace internal
173}  // namespace v8
174
175#endif  // V8_CODEGEN_H_
176