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/parser.h"
16 #include "src/prettyprinter.h"
17 #include "src/rewriter.h"
18 #include "src/runtime/runtime.h"
25 typedef double (*ModuloFunction)(double, double);
26 static ModuloFunction modulo_function = NULL;
27 // Defined in codegen-x64.cc.
28 ModuloFunction CreateModuloFunction();
30 void init_modulo_function() {
31 modulo_function = CreateModuloFunction();
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);
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)))) {
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);
62 return std::fmod(x, y);
65 #endif // defined(_WIN64)
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; \
73 double fast_##name(double x) { \
74 return (*fast_##name##_function)(x); \
77 UNARY_MATH_FUNCTION(exp, CreateExpFunction())
78 UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
80 #undef UNARY_MATH_FUNCTION
83 void lazily_initialize_fast_exp() {
84 if (fast_exp_function == NULL) {
85 init_fast_exp_function();
90 #define __ ACCESS_MASM(masm_)
94 Comment::Comment(MacroAssembler* masm, const char* msg)
95 : masm_(masm), msg_(msg) {
96 __ RecordComment(msg);
100 Comment::~Comment() {
101 if (msg_[0] == '[') __ RecordComment("]");
109 void CodeGenerator::MakeCodePrologue(CompilationInfo* info, const char* kind) {
110 bool print_source = false;
111 bool print_ast = false;
114 if (info->isolate()->bootstrapper()->IsActive()) {
115 print_source = FLAG_print_builtin_source;
116 print_ast = FLAG_print_builtin_ast;
119 print_source = FLAG_print_source;
120 print_ast = FLAG_print_ast;
121 ftype = "user-defined";
124 if (FLAG_trace_codegen || print_source || print_ast) {
125 PrintF("[generating %s code for %s function: ", kind, ftype);
126 if (info->IsStub()) {
128 CodeStub::MajorName(info->code_stub()->MajorKey(), true);
129 PrintF("%s", name == NULL ? "<unknown>" : name);
131 AllowDeferredHandleDereference allow_deference_for_trace;
132 PrintF("%s", info->function()->debug_name()->ToCString().get());
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()));
144 if (info->parse_info() && print_ast) {
145 PrintF("--- AST ---\n%s\n", AstPrinter(info->isolate(), info->zone())
146 .PrintProgram(info->function()));
152 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
154 CompilationInfo* info) {
155 Isolate* isolate = info->isolate();
157 // Allocate and install the code.
159 bool is_crankshafted =
160 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
162 masm->GetCode(&desc);
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());
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));
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);
192 info->parse_info()->function()->debug_name()->ToCString();
193 debug_name = debug_name_holder.get();
196 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
197 OFStream os(tracing_scope.file());
199 // Print the source code if available.
200 FunctionLiteral* function = nullptr;
202 info->parse_info() && (code->kind() == Code::OPTIMIZED_FUNCTION ||
203 code->kind() == Code::FUNCTION);
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.
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());
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);
228 os << "--- Optimized code ---\n"
229 << "optimization_id = " << info->optimization_id() << "\n";
231 os << "--- Code ---\n";
234 os << "source_position = " << function->start_position() << "\n";
236 code->Disassemble(debug_name, os);
237 os << "--- End code ---\n";
239 #endif // ENABLE_DISASSEMBLER
243 bool CodeGenerator::RecordPositions(MacroAssembler* masm,
246 if (pos != RelocInfo::kNoPosition) {
247 masm->positions_recorder()->RecordStatementPosition(pos);
248 masm->positions_recorder()->RecordPosition(pos);
250 return masm->positions_recorder()->WriteRecordedPositions();
256 } } // namespace v8::internal