04f130999eec309cbdcd5aa539c5f33c67b7c9c8
[platform/upstream/v8.git] / src / codegen.h
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_X87
60 #include "src/x87/codegen-x87.h"  // NOLINT
61 #else
62 #error Unsupported target architecture.
63 #endif
64
65 namespace v8 {
66 namespace internal {
67
68
69 class CompilationInfo;
70
71
72 class CodeGenerator {
73  public:
74   // Printing of AST, etc. as requested by flags.
75   static void MakeCodePrologue(CompilationInfo* info, const char* kind);
76
77   // Allocate and install the code.
78   static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
79                                        CompilationInfo* info);
80
81   // Print the code after compiling it.
82   static void PrintCode(Handle<Code> code, CompilationInfo* info);
83
84  private:
85   DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
86 };
87
88
89 // Results of the library implementation of transcendental functions may differ
90 // from the one we use in our generated code.  Therefore we use the same
91 // generated code both in runtime and compiled code.
92 typedef double (*UnaryMathFunction)(double x);
93
94 UnaryMathFunction CreateExpFunction();
95 UnaryMathFunction CreateSqrtFunction();
96
97
98 double modulo(double x, double y);
99
100 // Custom implementation of math functions.
101 double fast_exp(double input);
102 double fast_sqrt(double input);
103 #ifdef _WIN64
104 void init_modulo_function();
105 #endif
106 void lazily_initialize_fast_exp();
107 void init_fast_sqrt_function();
108
109
110 class ElementsTransitionGenerator : public AllStatic {
111  public:
112   // If |mode| is set to DONT_TRACK_ALLOCATION_SITE,
113   // |allocation_memento_found| may be NULL.
114   static void GenerateMapChangeElementsTransition(
115       MacroAssembler* masm,
116       Register receiver,
117       Register key,
118       Register value,
119       Register target_map,
120       AllocationSiteMode mode,
121       Label* allocation_memento_found);
122   static void GenerateSmiToDouble(
123       MacroAssembler* masm,
124       Register receiver,
125       Register key,
126       Register value,
127       Register target_map,
128       AllocationSiteMode mode,
129       Label* fail);
130   static void GenerateDoubleToObject(
131       MacroAssembler* masm,
132       Register receiver,
133       Register key,
134       Register value,
135       Register target_map,
136       AllocationSiteMode mode,
137       Label* fail);
138
139  private:
140   DISALLOW_COPY_AND_ASSIGN(ElementsTransitionGenerator);
141 };
142
143 static const int kNumberDictionaryProbes = 4;
144
145
146 class CodeAgingHelper {
147  public:
148   CodeAgingHelper();
149
150   uint32_t young_sequence_length() const { return young_sequence_.length(); }
151   bool IsYoung(byte* candidate) const {
152     return memcmp(candidate,
153                   young_sequence_.start(),
154                   young_sequence_.length()) == 0;
155   }
156   void CopyYoungSequenceTo(byte* new_buffer) const {
157     CopyBytes(new_buffer, young_sequence_.start(), young_sequence_.length());
158   }
159
160 #ifdef DEBUG
161   bool IsOld(byte* candidate) const;
162 #endif
163
164  protected:
165   const EmbeddedVector<byte, kNoCodeAgeSequenceLength> young_sequence_;
166 #ifdef DEBUG
167 #ifdef V8_TARGET_ARCH_ARM64
168   const EmbeddedVector<byte, kNoCodeAgeSequenceLength> old_sequence_;
169 #endif
170 #endif
171 };
172
173 } }  // namespace v8::internal
174
175 #endif  // V8_CODEGEN_H_