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 (*UnaryMathFunctionWithIsolate)(double x, Isolate* isolate);
93 
94 UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate);
95 UnaryMathFunctionWithIsolate CreateSqrtFunction(Isolate* isolate);
96 
97 
98 double modulo(double x, double y);
99 
100 // Custom implementation of math functions.
101 double fast_exp(double input, Isolate* isolate);
102 double fast_sqrt(double input, Isolate* isolate);
103 void lazily_initialize_fast_exp(Isolate* isolate);
104 void lazily_initialize_fast_sqrt(Isolate* isolate);
105 
106 
107 class ElementsTransitionGenerator : public AllStatic {
108  public:
109   // If |mode| is set to DONT_TRACK_ALLOCATION_SITE,
110   // |allocation_memento_found| may be NULL.
111   static void GenerateMapChangeElementsTransition(
112       MacroAssembler* masm,
113       Register receiver,
114       Register key,
115       Register value,
116       Register target_map,
117       AllocationSiteMode mode,
118       Label* allocation_memento_found);
119   static void GenerateSmiToDouble(
120       MacroAssembler* masm,
121       Register receiver,
122       Register key,
123       Register value,
124       Register target_map,
125       AllocationSiteMode mode,
126       Label* fail);
127   static void GenerateDoubleToObject(
128       MacroAssembler* masm,
129       Register receiver,
130       Register key,
131       Register value,
132       Register target_map,
133       AllocationSiteMode mode,
134       Label* fail);
135 
136  private:
137   DISALLOW_COPY_AND_ASSIGN(ElementsTransitionGenerator);
138 };
139 
140 static const int kNumberDictionaryProbes = 4;
141 
142 
143 class CodeAgingHelper {
144  public:
145   explicit CodeAgingHelper(Isolate* isolate);
146 
young_sequence_length()147   uint32_t young_sequence_length() const { return young_sequence_.length(); }
IsYoung(byte * candidate)148   bool IsYoung(byte* candidate) const {
149     return memcmp(candidate,
150                   young_sequence_.start(),
151                   young_sequence_.length()) == 0;
152   }
CopyYoungSequenceTo(byte * new_buffer)153   void CopyYoungSequenceTo(byte* new_buffer) const {
154     CopyBytes(new_buffer, young_sequence_.start(), young_sequence_.length());
155   }
156 
157 #ifdef DEBUG
158   bool IsOld(byte* candidate) const;
159 #endif
160 
161  protected:
162   const EmbeddedVector<byte, kNoCodeAgeSequenceLength> young_sequence_;
163 #ifdef DEBUG
164 #ifdef V8_TARGET_ARCH_ARM64
165   const EmbeddedVector<byte, kNoCodeAgeSequenceLength> old_sequence_;
166 #endif
167 #endif
168 };
169 
170 }  // namespace internal
171 }  // namespace v8
172 
173 #endif  // V8_CODEGEN_H_
174