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