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.
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"
24 typedef double (*ModuloFunction)(double, double);
25 static ModuloFunction modulo_function = NULL;
26 // Defined in codegen-x64.cc.
27 ModuloFunction CreateModuloFunction();
29 void init_modulo_function() {
30 modulo_function = CreateModuloFunction();
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);
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)))) {
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);
61 return std::fmod(x, y);
64 #endif // defined(_WIN64)
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; \
72 double fast_##name(double x) { \
73 return (*fast_##name##_function)(x); \
76 UNARY_MATH_FUNCTION(exp, CreateExpFunction())
77 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
79 #undef UNARY_MATH_FUNCTION
82 void lazily_initialize_fast_exp() {
83 if (fast_exp_function == NULL) {
84 init_fast_exp_function();
89 #define __ ACCESS_MASM(masm_)
93 Comment::Comment(MacroAssembler* masm, const char* msg)
94 : masm_(masm), msg_(msg) {
95 __ RecordComment(msg);
100 if (msg_[0] == '[') __ RecordComment("]");
108 void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
109 bool print_source = false;
110 bool print_ast = false;
113 if (info->isolate()->bootstrapper()->IsActive()) {
114 print_source = FLAG_print_builtin_source;
115 print_ast = FLAG_print_builtin_ast;
118 print_source = FLAG_print_source;
119 print_ast = FLAG_print_ast;
120 ftype = "user-defined";
123 if (FLAG_trace_codegen || print_source || print_ast) {
124 PrintF("[generating %s code for %s function: ", kind, ftype);
125 if (info->IsStub()) {
127 CodeStub::MajorName(info->code_stub()->MajorKey(), true);
128 PrintF("%s", name == NULL ? "<unknown>" : name);
130 AllowDeferredHandleDereference allow_deference_for_trace;
131 PrintF("%s", info->function()->debug_name()->ToCString().get());
137 if (!info->IsStub() && print_source) {
138 PrintF("--- Source from AST ---\n%s\n",
139 PrettyPrinter(info->isolate(), info->zone())
140 .PrintProgram(info->function()));
143 if (!info->IsStub() && print_ast) {
144 PrintF("--- AST ---\n%s\n", AstPrinter(info->isolate(), info->zone())
145 .PrintProgram(info->function()));
151 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
153 CompilationInfo* info) {
154 Isolate* isolate = info->isolate();
156 // Allocate and install the code.
158 bool is_crankshafted =
159 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
161 masm->GetCode(&desc);
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());
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));
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;
189 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
190 OFStream os(tracing_scope.file());
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.
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());
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);
215 os << "--- Optimized code ---\n"
216 << "optimization_id = " << info->optimization_id() << "\n";
218 os << "--- Code ---\n";
221 os << "source_position = " << function->start_position() << "\n";
223 if (info->IsStub()) {
224 CodeStub::Major major_key = info->code_stub()->MajorKey();
225 code->Disassemble(CodeStub::MajorName(major_key, false), os);
227 code->Disassemble(function->debug_name()->ToCString().get(), os);
229 os << "--- End code ---\n";
231 #endif // ENABLE_DISASSEMBLER
235 bool CodeGenerator::RecordPositions(MacroAssembler* masm,
238 if (pos != RelocInfo::kNoPosition) {
239 masm->positions_recorder()->RecordStatementPosition(pos);
240 masm->positions_recorder()->RecordPosition(pos);
242 return masm->positions_recorder()->WriteRecordedPositions();
248 } } // namespace v8::internal