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