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.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 PrintF("%s", info->function()->debug_name()->ToCString().get());
125 if (!info->IsStub() && print_source) {
126 PrintF("--- Source from AST ---\n%s\n",
127 PrettyPrinter(info->zone()).PrintProgram(info->function()));
130 if (!info->IsStub() && print_ast) {
131 PrintF("--- AST ---\n%s\n",
132 AstPrinter(info->zone()).PrintProgram(info->function()));
138 Handle<Code> CodeGenerator::MakeCodeEpilogue(MacroAssembler* masm,
140 CompilationInfo* info) {
141 Isolate* isolate = info->isolate();
143 // Allocate and install the code.
145 bool is_crankshafted =
146 Code::ExtractKindFromFlags(flags) == Code::OPTIMIZED_FUNCTION ||
148 masm->GetCode(&desc);
150 isolate->factory()->NewCode(desc, flags, masm->CodeObject(),
151 false, is_crankshafted,
152 info->prologue_offset(),
153 info->is_debug() && !is_crankshafted);
154 isolate->counters()->total_compiled_code_size()->Increment(
155 code->instruction_size());
156 isolate->heap()->IncrementCodeGeneratedBytes(is_crankshafted,
157 code->instruction_size());
162 void CodeGenerator::PrintCode(Handle<Code> code, CompilationInfo* info) {
163 #ifdef ENABLE_DISASSEMBLER
164 AllowDeferredHandleDereference allow_deference_for_print_code;
165 bool print_code = info->isolate()->bootstrapper()->IsActive()
166 ? FLAG_print_builtin_code
167 : (FLAG_print_code ||
168 (info->IsStub() && FLAG_print_code_stubs) ||
169 (info->IsOptimizing() && FLAG_print_opt_code));
171 // Print the source code if available.
172 FunctionLiteral* function = info->function();
173 bool print_source = code->kind() == Code::OPTIMIZED_FUNCTION ||
174 code->kind() == Code::FUNCTION;
176 CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer());
177 OFStream os(tracing_scope.file());
179 Handle<Script> script = info->script();
180 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
181 os << "--- Raw source ---\n";
182 ConsStringIteratorOp op;
183 StringCharacterStream stream(String::cast(script->source()),
185 function->start_position());
186 // fun->end_position() points to the last character in the stream. We
187 // need to compensate by adding one to calculate the length.
189 function->end_position() - function->start_position() + 1;
190 for (int i = 0; i < source_len; i++) {
191 if (stream.HasMore()) {
192 os << AsUC16(stream.GetNext());
198 if (info->IsOptimizing()) {
199 if (FLAG_print_unopt_code) {
200 os << "--- Unoptimized code ---\n";
201 info->closure()->shared()->code()->Disassemble(
202 function->debug_name()->ToCString().get(), os);
204 os << "--- Optimized code ---\n"
205 << "optimization_id = " << info->optimization_id() << "\n";
207 os << "--- Code ---\n";
210 os << "source_position = " << function->start_position() << "\n";
212 if (info->IsStub()) {
213 CodeStub::Major major_key = info->code_stub()->MajorKey();
214 code->Disassemble(CodeStub::MajorName(major_key, false), os);
216 code->Disassemble(function->debug_name()->ToCString().get(), os);
218 os << "--- End code ---\n";
220 #endif // ENABLE_DISASSEMBLER
224 bool CodeGenerator::RecordPositions(MacroAssembler* masm,
227 if (pos != RelocInfo::kNoPosition) {
228 masm->positions_recorder()->RecordStatementPosition(pos);
229 masm->positions_recorder()->RecordPosition(pos);
231 return masm->positions_recorder()->WriteRecordedPositions();
238 void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
241 GenerateReadElement(masm);
243 case NEW_SLOPPY_FAST:
244 GenerateNewSloppyFast(masm);
246 case NEW_SLOPPY_SLOW:
247 GenerateNewSloppySlow(masm);
250 GenerateNewStrict(masm);
256 int CEntryStub::MinorKey() const {
257 int result = (save_doubles_ == kSaveFPRegs) ? 1 : 0;
258 DCHECK(result_size_ == 1 || result_size_ == 2);
260 return result | ((result_size_ == 1) ? 0 : 2);
267 } } // namespace v8::internal