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.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 // BuildFunctionInfo 36 // ProcessDeclarations 37 // DeclareGlobals 38 // CheckForInlineRuntimeCall 39 // AnalyzeCondition 40 // CodeForFunctionPosition 41 // CodeForReturnPosition 42 // CodeForStatementPosition 43 // CodeForDoWhileConditionPosition 44 // CodeForSourcePosition 45 46 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; 47 48 #if V8_TARGET_ARCH_IA32 49 #include "src/ia32/codegen-ia32.h" 50 #elif V8_TARGET_ARCH_X64 51 #include "src/x64/codegen-x64.h" 52 #elif V8_TARGET_ARCH_ARM64 53 #include "src/arm64/codegen-arm64.h" 54 #elif V8_TARGET_ARCH_ARM 55 #include "src/arm/codegen-arm.h" 56 #elif V8_TARGET_ARCH_MIPS 57 #include "src/mips/codegen-mips.h" 58 #elif V8_TARGET_ARCH_X87 59 #include "src/x87/codegen-x87.h" 60 #else 61 #error Unsupported target architecture. 62 #endif 63 64 namespace v8 { 65 namespace internal { 66 67 68 class CompilationInfo; 69 70 71 class CodeGenerator { 72 public: 73 // Printing of AST, etc. as requested by flags. 74 static void MakeCodePrologue(CompilationInfo* info, const char* kind); 75 76 // Allocate and install the code. 77 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm, 78 Code::Flags flags, 79 CompilationInfo* info); 80 81 // Print the code after compiling it. 82 static void PrintCode(Handle<Code> code, CompilationInfo* info); 83 84 static bool RecordPositions(MacroAssembler* masm, 85 int pos, 86 bool right_here = false); 87 88 private: 89 DISALLOW_COPY_AND_ASSIGN(CodeGenerator); 90 }; 91 92 93 // Results of the library implementation of transcendental functions may differ 94 // from the one we use in our generated code. Therefore we use the same 95 // generated code both in runtime and compiled code. 96 typedef double (*UnaryMathFunction)(double x); 97 98 UnaryMathFunction CreateExpFunction(); 99 UnaryMathFunction CreateSqrtFunction(); 100 101 102 double modulo(double x, double y); 103 104 // Custom implementation of math functions. 105 double fast_exp(double input); 106 double fast_sqrt(double input); 107 #ifdef _WIN64 108 void init_modulo_function(); 109 #endif 110 void lazily_initialize_fast_exp(); 111 void init_fast_sqrt_function(); 112 113 114 class ElementsTransitionGenerator : public AllStatic { 115 public: 116 // If |mode| is set to DONT_TRACK_ALLOCATION_SITE, 117 // |allocation_memento_found| may be NULL. 118 static void GenerateMapChangeElementsTransition(MacroAssembler* masm, 119 AllocationSiteMode mode, 120 Label* allocation_memento_found); 121 static void GenerateSmiToDouble(MacroAssembler* masm, 122 AllocationSiteMode mode, 123 Label* fail); 124 static void GenerateDoubleToObject(MacroAssembler* masm, 125 AllocationSiteMode mode, 126 Label* fail); 127 128 private: 129 DISALLOW_COPY_AND_ASSIGN(ElementsTransitionGenerator); 130 }; 131 132 static const int kNumberDictionaryProbes = 4; 133 134 135 class CodeAgingHelper { 136 public: 137 CodeAgingHelper(); 138 139 uint32_t young_sequence_length() const { return young_sequence_.length(); } 140 bool IsYoung(byte* candidate) const { 141 return memcmp(candidate, 142 young_sequence_.start(), 143 young_sequence_.length()) == 0; 144 } 145 void CopyYoungSequenceTo(byte* new_buffer) const { 146 CopyBytes(new_buffer, young_sequence_.start(), young_sequence_.length()); 147 } 148 149 #ifdef DEBUG 150 bool IsOld(byte* candidate) const; 151 #endif 152 153 protected: 154 const EmbeddedVector<byte, kNoCodeAgeSequenceLength> young_sequence_; 155 #ifdef DEBUG 156 #ifdef V8_TARGET_ARCH_ARM64 157 const EmbeddedVector<byte, kNoCodeAgeSequenceLength> old_sequence_; 158 #endif 159 #endif 160 }; 161 162 } } // namespace v8::internal 163 164 #endif // V8_CODEGEN_H_ 165