Move IC code into a subdir and move ic-compilation related code from stub-cache into...
[platform/upstream/v8.git] / src / codegen.cc
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 #include "src/v8.h"
6
7 #include "src/bootstrapper.h"
8 #include "src/codegen.h"
9 #include "src/compiler.h"
10 #include "src/cpu-profiler.h"
11 #include "src/debug.h"
12 #include "src/prettyprinter.h"
13 #include "src/rewriter.h"
14 #include "src/runtime.h"
15
16 namespace v8 {
17 namespace internal {
18
19
20 #if defined(_WIN64)
21 typedef double (*ModuloFunction)(double, double);
22 static ModuloFunction modulo_function = NULL;
23 // Defined in codegen-x64.cc.
24 ModuloFunction CreateModuloFunction();
25
26 void init_modulo_function() {
27   modulo_function = CreateModuloFunction();
28 }
29
30
31 double modulo(double x, double y) {
32   // Note: here we rely on dependent reads being ordered. This is true
33   // on all architectures we currently support.
34   return (*modulo_function)(x, y);
35 }
36 #elif defined(_WIN32)
37
38 double modulo(double x, double y) {
39   // Workaround MS fmod bugs. ECMA-262 says:
40   // dividend is finite and divisor is an infinity => result equals dividend
41   // dividend is a zero and divisor is nonzero finite => result equals dividend
42   if (!(std::isfinite(x) && (!std::isfinite(y) && !std::isnan(y))) &&
43       !(x == 0 && (y != 0 && std::isfinite(y)))) {
44     x = fmod(x, y);
45   }
46   return x;
47 }
48 #else  // POSIX
49
50 double modulo(double x, double y) {
51   return std::fmod(x, y);
52 }
53 #endif  // defined(_WIN64)
54
55
56 #define UNARY_MATH_FUNCTION(name, generator)             \
57 static UnaryMathFunction fast_##name##_function = NULL;  \
58 void init_fast_##name##_function() {                     \
59   fast_##name##_function = generator;                    \
60 }                                                        \
61 double fast_##name(double x) {                           \
62   return (*fast_##name##_function)(x);                   \
63 }
64
65 UNARY_MATH_FUNCTION(exp, CreateExpFunction())
66 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
67
68 #undef UNARY_MATH_FUNCTION
69
70
71 void lazily_initialize_fast_exp() {
72   if (fast_exp_function == NULL) {
73     init_fast_exp_function();
74   }
75 }
76
77
78 #define __ ACCESS_MASM(masm_)
79
80 #ifdef DEBUG
81
82 Comment::Comment(MacroAssembler* masm, const char* msg)
83     : masm_(masm), msg_(msg) {
84   __ RecordComment(msg);
85 }
86
87
88 Comment::~Comment() {
89   if (msg_[0] == '[') __ RecordComment("]");
90 }
91
92 #endif  // DEBUG
93
94 #undef __
95
96
97 void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
98   bool print_source = false;
99   bool print_ast = false;
100   const char* ftype;
101
102   if (info->isolate()->bootstrapper()->IsActive()) {
103     print_source = FLAG_print_builtin_source;
104     print_ast = FLAG_print_builtin_ast;
105     ftype = "builtin";
106   } else {
107     print_source = FLAG_print_source;
108     print_ast = FLAG_print_ast;
109     ftype = "user-defined";
110   }
111
112   if (FLAG_trace_codegen || print_source || print_ast) {
113     PrintF("[generating %s code for %s function: ", kind, ftype);
114     if (info->IsStub()) {
115       const char* name =
116           CodeStub::MajorName(info->code_stub()->MajorKey(), true);
117       PrintF("%s", name == NULL ? "<unknown>" : name);
118     } else {
119       PrintF("%s", info->function()->debug_name()->ToCString().get());
120     }
121     PrintF("]\n");
122   }
123
124 #ifdef DEBUG
125   if (!info->IsStub() && print_source) {
126     PrintF("--- Source from AST ---\n%s\n",
127            PrettyPrinter(info->zone()).PrintProgram(info->function()));
128   }
129
130   if (!info->IsStub() && print_ast) {
131     PrintF("--- AST ---\n%s\n",
132            AstPrinter(info->zone()).PrintProgram(info->function()));
133   }
134 #endif  // DEBUG
135 }
136
137
138 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
139                                              Code::Flags flags,
140                                              CompilationInfo* info) {
141   Isolate* isolate = info->isolate();
142
143   // Allocate and install the code.
144   CodeDesc desc;
145   bool is_crankshafted =
146       Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
147       info->IsStub();
148   masm->GetCode(&desc);
149   Handle<Code> code =
150       isolate->factory()->NewCode(desc, flags, masm->CodeObject(),
151                                   false, is_crankshafted,
152                                   info->prologue_offset(),
153                                   info->is_debug() && !is_crankshafted);
154   isolate->counters()->total_compiled_code_size()->Increment(
155       code->instruction_size());
156   isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
157       code->instruction_size());
158   return code;
159 }
160
161
162 void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
163 #ifdef ENABLE_DISASSEMBLER
164   AllowDeferredHandleDereference allow_deference_for_print_code;
165   bool print_code = info->isolate()->bootstrapper()->IsActive()
166       ? FLAG_print_builtin_code
167       : (FLAG_print_code ||
168          (info->IsStub() && FLAG_print_code_stubs) ||
169          (info->IsOptimizing() && FLAG_print_opt_code));
170   if (print_code) {
171     // Print the source code if available.
172     FunctionLiteral* function = info->function();
173     bool print_source = code->kind() == Code::OPTIMIZED_FUNCTION ||
174         code->kind() == Code::FUNCTION;
175
176     CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
177     OFStream os(tracing_scope.file());
178     if (print_source) {
179       Handle<Script> script = info->script();
180       if (!script->IsUndefined() && !script->source()->IsUndefined()) {
181         os << "--- Raw source ---\n";
182         ConsStringIteratorOp op;
183         StringCharacterStream stream(String::cast(script->source()),
184                                      &op,
185                                      function->start_position());
186         // fun->end_position() points to the last character in the stream. We
187         // need to compensate by adding one to calculate the length.
188         int source_len =
189             function->end_position() - function->start_position() + 1;
190         for (int i = 0; i < source_len; i++) {
191           if (stream.HasMore()) {
192             os << AsUC16(stream.GetNext());
193           }
194         }
195         os << "\n\n";
196       }
197     }
198     if (info->IsOptimizing()) {
199       if (FLAG_print_unopt_code) {
200         os << "--- Unoptimized code ---\n";
201         info->closure()->shared()->code()->Disassemble(
202             function->debug_name()->ToCString().get(), os);
203       }
204       os << "--- Optimized code ---\n"
205          << "optimization_id = " << info->optimization_id() << "\n";
206     } else {
207       os << "--- Code ---\n";
208     }
209     if (print_source) {
210       os << "source_position = " << function->start_position() << "\n";
211     }
212     if (info->IsStub()) {
213       CodeStub::Major major_key = info->code_stub()->MajorKey();
214       code->Disassemble(CodeStub::MajorName(major_key, false), os);
215     } else {
216       code->Disassemble(function->debug_name()->ToCString().get(), os);
217     }
218     os << "--- End code ---\n";
219   }
220 #endif  // ENABLE_DISASSEMBLER
221 }
222
223
224 bool CodeGenerator::RecordPositions(MacroAssembler* masm,
225                                     int pos,
226                                     bool right_here) {
227   if (pos != RelocInfo::kNoPosition) {
228     masm->positions_recorder()->RecordStatementPosition(pos);
229     masm->positions_recorder()->RecordPosition(pos);
230     if (right_here) {
231       return masm->positions_recorder()->WriteRecordedPositions();
232     }
233   }
234   return false;
235 }
236
237
238 void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
239   switch (type_) {
240     case READ_ELEMENT:
241       GenerateReadElement(masm);
242       break;
243     case NEW_SLOPPY_FAST:
244       GenerateNewSloppyFast(masm);
245       break;
246     case NEW_SLOPPY_SLOW:
247       GenerateNewSloppySlow(masm);
248       break;
249     case NEW_STRICT:
250       GenerateNewStrict(masm);
251       break;
252   }
253 }
254
255
256 int CEntryStub::MinorKey() const {
257   int result = (save_doubles_ == kSaveFPRegs) ? 1 : 0;
258   DCHECK(result_size_ == 1 || result_size_ == 2);
259 #ifdef _WIN64
260   return result | ((result_size_ == 1) ? 0 : 2);
261 #else
262   return result;
263 #endif
264 }
265
266
267 } }  // namespace v8::internal