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.
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/runtime.h"
21 typedef double (*ModuloFunction)(double, double);
22 static ModuloFunction modulo_function = NULL;
23 // Defined in codegen-x64.cc.
24 ModuloFunction CreateModuloFunction();
26 void init_modulo_function() {
27 modulo_function = CreateModuloFunction();
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);
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)))) {
50 double modulo(double x, double y) {
51 return std::fmod(x, y);
53 #endif // defined(_WIN64)
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; \
61 double fast_##name(double x) { \
62 return (*fast_##name##_function)(x); \
65 UNARY_MATH_FUNCTION(exp, CreateExpFunction())
66 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
68 #undef UNARY_MATH_FUNCTION
71 void lazily_initialize_fast_exp() {
72 if (fast_exp_function == NULL) {
73 init_fast_exp_function();
78 #define __ ACCESS_MASM(masm_)
82 Comment::Comment(MacroAssembler* masm, const char* msg)
83 : masm_(masm), msg_(msg) {
84 __ RecordComment(msg);
89 if (msg_[0] == '[') __ RecordComment("]");
97 void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
98 bool print_source = false;
99 bool print_ast = false;
102 if (info->isolate()->bootstrapper()->IsActive()) {
103 print_source = FLAG_print_builtin_source;
104 print_ast = FLAG_print_builtin_ast;
107 print_source = FLAG_print_source;
108 print_ast = FLAG_print_ast;
109 ftype = "user-defined";
112 if (FLAG_trace_codegen || print_source || print_ast) {
113 PrintF("[generating %s code for %s function: ", kind, ftype);
114 if (info->IsStub()) {
116 CodeStub::MajorName(info->code_stub()->MajorKey(), true);
117 PrintF("%s", name == NULL ? "<unknown>" : name);
119 AllowDeferredHandleDereference allow_deference_for_trace;
120 PrintF("%s", info->function()->debug_name()->ToCString().get());
126 if (!info->IsStub() && print_source) {
127 PrintF("--- Source from AST ---\n%s\n",
128 PrettyPrinter(info->zone()).PrintProgram(info->function()));
131 if (!info->IsStub() && print_ast) {
132 PrintF("--- AST ---\n%s\n",
133 AstPrinter(info->zone()).PrintProgram(info->function()));
139 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
141 CompilationInfo* info) {
142 Isolate* isolate = info->isolate();
144 // Allocate and install the code.
146 bool is_crankshafted =
147 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
149 masm->GetCode(&desc);
151 isolate->factory()->NewCode(desc, flags, masm->CodeObject(),
152 false, is_crankshafted,
153 info->prologue_offset(),
154 info->is_debug() && !is_crankshafted);
155 isolate->counters()->total_compiled_code_size()->Increment(
156 code->instruction_size());
157 isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
158 code->instruction_size());
163 void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
164 #ifdef ENABLE_DISASSEMBLER
165 AllowDeferredHandleDereference allow_deference_for_print_code;
166 bool print_code = info->isolate()->bootstrapper()->IsActive()
167 ? FLAG_print_builtin_code
168 : (FLAG_print_code ||
169 (info->IsStub() && FLAG_print_code_stubs) ||
170 (info->IsOptimizing() && FLAG_print_opt_code));
172 // Print the source code if available.
173 FunctionLiteral* function = info->function();
174 bool print_source = code->kind() == Code::OPTIMIZED_FUNCTION ||
175 code->kind() == Code::FUNCTION;
177 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
178 OFStream os(tracing_scope.file());
180 Handle<Script> script = info->script();
181 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
182 os << "--- Raw source ---\n";
183 StringCharacterStream stream(String::cast(script->source()),
184 function->start_position());
185 // fun->end_position() points to the last character in the stream. We
186 // need to compensate by adding one to calculate the length.
188 function->end_position() - function->start_position() + 1;
189 for (int i = 0; i < source_len; i++) {
190 if (stream.HasMore()) {
191 os << AsReversiblyEscapedUC16(stream.GetNext());
197 if (info->IsOptimizing()) {
198 if (FLAG_print_unopt_code) {
199 os << "--- Unoptimized code ---\n";
200 info->closure()->shared()->code()->Disassemble(
201 function->debug_name()->ToCString().get(), os);
203 os << "--- Optimized code ---\n"
204 << "optimization_id = " << info->optimization_id() << "\n";
206 os << "--- Code ---\n";
209 os << "source_position = " << function->start_position() << "\n";
211 if (info->IsStub()) {
212 CodeStub::Major major_key = info->code_stub()->MajorKey();
213 code->Disassemble(CodeStub::MajorName(major_key, false), os);
215 code->Disassemble(function->debug_name()->ToCString().get(), os);
217 os << "--- End code ---\n";
219 #endif // ENABLE_DISASSEMBLER
223 bool CodeGenerator::RecordPositions(MacroAssembler* masm,
226 if (pos != RelocInfo::kNoPosition) {
227 masm->positions_recorder()->RecordStatementPosition(pos);
228 masm->positions_recorder()->RecordPosition(pos);
230 return masm->positions_recorder()->WriteRecordedPositions();
236 } } // namespace v8::internal