blob: ba99a404a38a61b201f792c38539c32652c0acd7 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_CODEGEN_H_
6#define V8_CODEGEN_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/code-stubs.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000010
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
Steve Block3ce2e202009-11-05 08:53:23 +000015// MakeCodePrologue
16// MakeCodeEpilogue
Steve Blocka7e24c12009-10-30 11:49:00 +000017// masm
18// frame
Steve Blockd0582a62009-12-15 09:54:21 +000019// script
Steve Blocka7e24c12009-10-30 11:49:00 +000020// has_valid_frame
21// SetFrame
22// DeleteFrame
23// allocator
24// AddDeferred
25// in_spilled_code
26// set_in_spilled_code
Steve Block3ce2e202009-11-05 08:53:23 +000027// RecordPositions
Steve Blocka7e24c12009-10-30 11:49:00 +000028//
29// These methods are either used privately by the shared code or implemented as
30// shared code:
31// CodeGenerator
32// ~CodeGenerator
Leon Clarke4515c472010-02-03 11:58:03 +000033// Generate
Steve Block3ce2e202009-11-05 08:53:23 +000034// ComputeLazyCompile
Steve Block6ded16b2010-05-10 14:33:55 +010035// BuildFunctionInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000036// ProcessDeclarations
37// DeclareGlobals
Steve Blocka7e24c12009-10-30 11:49:00 +000038// CheckForInlineRuntimeCall
Steve Block3ce2e202009-11-05 08:53:23 +000039// AnalyzeCondition
Steve Blocka7e24c12009-10-30 11:49:00 +000040// CodeForFunctionPosition
41// CodeForReturnPosition
42// CodeForStatementPosition
Steve Blockd0582a62009-12-15 09:54:21 +000043// CodeForDoWhileConditionPosition
Steve Blocka7e24c12009-10-30 11:49:00 +000044// CodeForSourcePosition
45
Ben Murdochb0fe1622011-05-05 13:52:32 +010046enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
47
Ben Murdochbb769b22010-08-11 14:56:33 +010048#if V8_TARGET_ARCH_IA32
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049#include "src/ia32/codegen-ia32.h" // NOLINT
Ben Murdochbb769b22010-08-11 14:56:33 +010050#elif V8_TARGET_ARCH_X64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051#include "src/x64/codegen-x64.h" // NOLINT
52#elif V8_TARGET_ARCH_ARM64
53#include "src/arm64/codegen-arm64.h" // NOLINT
Ben Murdochbb769b22010-08-11 14:56:33 +010054#elif V8_TARGET_ARCH_ARM
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055#include "src/arm/codegen-arm.h" // NOLINT
Ben Murdochbb769b22010-08-11 14:56:33 +010056#elif V8_TARGET_ARCH_MIPS
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057#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_X87
61#include "src/x87/codegen-x87.h" // NOLINT
Ben Murdochbb769b22010-08-11 14:56:33 +010062#else
63#error Unsupported target architecture.
64#endif
65
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066namespace v8 {
67namespace internal {
68
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069
70class CompilationInfo;
71
72
73class CodeGenerator {
74 public:
75 // Printing of AST, etc. as requested by flags.
76 static void MakeCodePrologue(CompilationInfo* info, const char* kind);
77
78 // Allocate and install the code.
79 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
80 Code::Flags flags,
81 CompilationInfo* info);
82
83 // Print the code after compiling it.
84 static void PrintCode(Handle<Code> code, CompilationInfo* info);
85
86 static bool RecordPositions(MacroAssembler* masm,
87 int pos,
88 bool right_here = false);
89
90 private:
91 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
92};
93
94
Ben Murdoch3ef787d2012-04-12 10:51:47 +010095// Results of the library implementation of transcendental functions may differ
96// from the one we use in our generated code. Therefore we use the same
97// generated code both in runtime and compiled code.
98typedef double (*UnaryMathFunction)(double x);
99
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100UnaryMathFunction CreateExpFunction();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100101UnaryMathFunction CreateSqrtFunction();
102
103
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104double modulo(double x, double y);
105
106// Custom implementation of math functions.
107double fast_exp(double input);
108double fast_sqrt(double input);
109#ifdef _WIN64
110void init_modulo_function();
111#endif
112void lazily_initialize_fast_exp();
113void init_fast_sqrt_function();
114
115
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100116class ElementsTransitionGenerator : public AllStatic {
117 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 // If |mode| is set to DONT_TRACK_ALLOCATION_SITE,
119 // |allocation_memento_found| may be NULL.
120 static void GenerateMapChangeElementsTransition(
121 MacroAssembler* masm,
122 Register receiver,
123 Register key,
124 Register value,
125 Register target_map,
126 AllocationSiteMode mode,
127 Label* allocation_memento_found);
128 static void GenerateSmiToDouble(
129 MacroAssembler* masm,
130 Register receiver,
131 Register key,
132 Register value,
133 Register target_map,
134 AllocationSiteMode mode,
135 Label* fail);
136 static void GenerateDoubleToObject(
137 MacroAssembler* masm,
138 Register receiver,
139 Register key,
140 Register value,
141 Register target_map,
142 AllocationSiteMode mode,
143 Label* fail);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100144
145 private:
146 DISALLOW_COPY_AND_ASSIGN(ElementsTransitionGenerator);
147};
148
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149static const int kNumberDictionaryProbes = 4;
150
151
152class CodeAgingHelper {
153 public:
154 CodeAgingHelper();
155
156 uint32_t young_sequence_length() const { return young_sequence_.length(); }
157 bool IsYoung(byte* candidate) const {
158 return memcmp(candidate,
159 young_sequence_.start(),
160 young_sequence_.length()) == 0;
161 }
162 void CopyYoungSequenceTo(byte* new_buffer) const {
163 CopyBytes(new_buffer, young_sequence_.start(), young_sequence_.length());
164 }
165
166#ifdef DEBUG
167 bool IsOld(byte* candidate) const;
168#endif
169
170 protected:
171 const EmbeddedVector<byte, kNoCodeAgeSequenceLength> young_sequence_;
172#ifdef DEBUG
173#ifdef V8_TARGET_ARCH_ARM64
174 const EmbeddedVector<byte, kNoCodeAgeSequenceLength> old_sequence_;
175#endif
176#endif
177};
178
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100179} } // namespace v8::internal
180
Steve Blocka7e24c12009-10-30 11:49:00 +0000181#endif // V8_CODEGEN_H_