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