2009-03-31 09:02:40 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2008-10-10 00:00:52 +00:00
|
|
|
#include "bootstrapper.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "codegen-inl.h"
|
|
|
|
#include "debug.h"
|
2009-03-31 09:02:40 +00:00
|
|
|
#include "oprofile-agent.h"
|
2008-10-10 00:00:52 +00:00
|
|
|
#include "prettyprinter.h"
|
2009-03-20 12:58:14 +00:00
|
|
|
#include "register-allocator-inl.h"
|
2009-03-31 09:02:40 +00:00
|
|
|
#include "rewriter.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "runtime.h"
|
2009-03-20 09:35:31 +00:00
|
|
|
#include "scopeinfo.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "stub-cache.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-15 11:09:51 +00:00
|
|
|
|
|
|
|
CodeGenerator* CodeGeneratorScope::top_ = NULL;
|
|
|
|
|
|
|
|
|
2009-06-05 14:16:32 +00:00
|
|
|
DeferredCode::DeferredCode()
|
|
|
|
: masm_(CodeGeneratorScope::Current()->masm()),
|
|
|
|
statement_position_(masm_->current_statement_position()),
|
|
|
|
position_(masm_->current_position()) {
|
2008-12-17 08:45:42 +00:00
|
|
|
ASSERT(statement_position_ != RelocInfo::kNoPosition);
|
|
|
|
ASSERT(position_ != RelocInfo::kNoPosition);
|
2009-05-27 15:13:00 +00:00
|
|
|
|
2009-06-05 14:16:32 +00:00
|
|
|
CodeGeneratorScope::Current()->AddDeferred(this);
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
comment_ = "";
|
|
|
|
#endif
|
2009-06-05 14:16:32 +00:00
|
|
|
|
|
|
|
// Copy the register locations from the code generator's frame.
|
|
|
|
// These are the registers that will be spilled on entry to the
|
|
|
|
// deferred code and restored on exit.
|
|
|
|
VirtualFrame* frame = CodeGeneratorScope::Current()->frame();
|
|
|
|
int sp_offset = frame->fp_relative(frame->stack_pointer_);
|
|
|
|
for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
|
|
|
|
int loc = frame->register_location(i);
|
|
|
|
if (loc == VirtualFrame::kIllegalIndex) {
|
|
|
|
registers_[i] = kIgnore;
|
|
|
|
} else if (frame->elements_[loc].is_synced()) {
|
|
|
|
// Needs to be restored on exit but not saved on entry.
|
|
|
|
registers_[i] = frame->fp_relative(loc) | kSyncedFlag;
|
|
|
|
} else {
|
|
|
|
int offset = frame->fp_relative(loc);
|
|
|
|
registers_[i] = (offset < sp_offset) ? kPush : offset;
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::ProcessDeferred() {
|
|
|
|
while (!deferred_.is_empty()) {
|
|
|
|
DeferredCode* code = deferred_.RemoveLast();
|
2009-06-05 14:16:32 +00:00
|
|
|
ASSERT(masm_ == code->masm());
|
2008-07-03 15:10:15 +00:00
|
|
|
// Record position of deferred code stub.
|
2009-06-05 14:16:32 +00:00
|
|
|
masm_->RecordStatementPosition(code->statement_position());
|
2008-09-22 13:57:03 +00:00
|
|
|
if (code->position() != RelocInfo::kNoPosition) {
|
2009-06-05 14:16:32 +00:00
|
|
|
masm_->RecordPosition(code->position());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-02-27 13:00:32 +00:00
|
|
|
// Generate the code.
|
2009-06-05 14:16:32 +00:00
|
|
|
Comment cmnt(masm_, code->comment());
|
|
|
|
masm_->bind(code->entry_label());
|
|
|
|
code->SaveRegisters();
|
2008-07-03 15:10:15 +00:00
|
|
|
code->Generate();
|
2009-06-05 14:16:32 +00:00
|
|
|
code->RestoreRegisters();
|
|
|
|
masm_->jmp(code->exit_label());
|
2009-02-27 13:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::SetFrame(VirtualFrame* new_frame,
|
|
|
|
RegisterFile* non_frame_registers) {
|
|
|
|
RegisterFile saved_counts;
|
|
|
|
if (has_valid_frame()) {
|
|
|
|
frame_->DetachFromCodeGenerator();
|
|
|
|
// The remaining register reference counts are the non-frame ones.
|
|
|
|
allocator_->SaveTo(&saved_counts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_frame != NULL) {
|
|
|
|
// Restore the non-frame register references that go with the new frame.
|
|
|
|
allocator_->RestoreFrom(non_frame_registers);
|
|
|
|
new_frame->AttachToCodeGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
frame_ = new_frame;
|
|
|
|
saved_counts.CopyTo(non_frame_registers);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::DeleteFrame() {
|
|
|
|
if (has_valid_frame()) {
|
|
|
|
frame_->DetachFromCodeGenerator();
|
|
|
|
frame_ = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 00:00:52 +00:00
|
|
|
// Generate the code. Takes a function literal, generates code for it, assemble
|
|
|
|
// all the pieces into a Code object. This function is only to be called by
|
|
|
|
// the compiler.cc code.
|
|
|
|
Handle<Code> CodeGenerator::MakeCode(FunctionLiteral* flit,
|
|
|
|
Handle<Script> script,
|
|
|
|
bool is_eval) {
|
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
2008-12-04 08:26:08 +00:00
|
|
|
bool print_code = Bootstrapper::IsActive()
|
|
|
|
? FLAG_print_builtin_code
|
|
|
|
: FLAG_print_code;
|
2008-10-10 00:00:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool print_source = false;
|
|
|
|
bool print_ast = false;
|
|
|
|
const char* ftype;
|
|
|
|
|
|
|
|
if (Bootstrapper::IsActive()) {
|
|
|
|
print_source = FLAG_print_builtin_source;
|
|
|
|
print_ast = FLAG_print_builtin_ast;
|
|
|
|
ftype = "builtin";
|
|
|
|
} else {
|
|
|
|
print_source = FLAG_print_source;
|
|
|
|
print_ast = FLAG_print_ast;
|
|
|
|
ftype = "user-defined";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAG_trace_codegen || print_source || print_ast) {
|
|
|
|
PrintF("*** Generate code for %s function: ", ftype);
|
|
|
|
flit->name()->ShortPrint();
|
|
|
|
PrintF(" ***\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_source) {
|
|
|
|
PrintF("--- Source from AST ---\n%s\n", PrettyPrinter().PrintProgram(flit));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_ast) {
|
|
|
|
PrintF("--- AST ---\n%s\n", AstPrinter().PrintProgram(flit));
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
// Generate code.
|
|
|
|
const int initial_buffer_size = 4 * KB;
|
|
|
|
CodeGenerator cgen(initial_buffer_size, script, is_eval);
|
2009-05-15 11:09:51 +00:00
|
|
|
CodeGeneratorScope scope(&cgen);
|
2008-10-10 00:00:52 +00:00
|
|
|
cgen.GenCode(flit);
|
|
|
|
if (cgen.HasStackOverflow()) {
|
|
|
|
ASSERT(!Top::has_pending_exception());
|
|
|
|
return Handle<Code>::null();
|
|
|
|
}
|
|
|
|
|
2009-05-14 10:29:48 +00:00
|
|
|
// Allocate and install the code. Time the rest of this function as
|
|
|
|
// code creation.
|
|
|
|
HistogramTimerScope timer(&Counters::code_creation);
|
2008-10-10 00:00:52 +00:00
|
|
|
CodeDesc desc;
|
|
|
|
cgen.masm()->GetCode(&desc);
|
2009-05-18 09:41:16 +00:00
|
|
|
ZoneScopeInfo sinfo(flit->scope());
|
2009-05-25 18:29:02 +00:00
|
|
|
InLoopFlag in_loop = (cgen.loop_nesting() != 0) ? IN_LOOP : NOT_IN_LOOP;
|
|
|
|
Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, in_loop);
|
2009-02-25 16:52:15 +00:00
|
|
|
Handle<Code> code = Factory::NewCode(desc,
|
|
|
|
&sinfo,
|
|
|
|
flags,
|
|
|
|
cgen.masm()->CodeObject());
|
2008-10-10 00:00:52 +00:00
|
|
|
|
|
|
|
// Add unresolved entries in the code to the fixup list.
|
|
|
|
Bootstrapper::AddFixup(*code, cgen.masm());
|
|
|
|
|
|
|
|
#ifdef ENABLE_DISASSEMBLER
|
|
|
|
if (print_code) {
|
|
|
|
// Print the source code if available.
|
|
|
|
if (!script->IsUndefined() && !script->source()->IsUndefined()) {
|
|
|
|
PrintF("--- Raw source ---\n");
|
|
|
|
StringInputBuffer stream(String::cast(script->source()));
|
|
|
|
stream.Seek(flit->start_position());
|
|
|
|
// flit->end_position() points to the last character in the stream. We
|
|
|
|
// need to compensate by adding one to calculate the length.
|
|
|
|
int source_len = flit->end_position() - flit->start_position() + 1;
|
|
|
|
for (int i = 0; i < source_len; i++) {
|
|
|
|
if (stream.has_more()) PrintF("%c", stream.GetNext());
|
|
|
|
}
|
|
|
|
PrintF("\n\n");
|
|
|
|
}
|
|
|
|
PrintF("--- Code ---\n");
|
2009-03-04 06:14:34 +00:00
|
|
|
code->Disassemble(*flit->name()->ToCString());
|
2008-10-10 00:00:52 +00:00
|
|
|
}
|
|
|
|
#endif // ENABLE_DISASSEMBLER
|
|
|
|
|
|
|
|
if (!code.is_null()) {
|
|
|
|
Counters::total_compiled_code_size.Increment(code->instruction_size());
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-07 14:24:08 +00:00
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
|
|
|
|
2009-01-06 13:24:52 +00:00
|
|
|
bool CodeGenerator::ShouldGenerateLog(Expression* type) {
|
|
|
|
ASSERT(type != NULL);
|
2009-06-24 13:09:34 +00:00
|
|
|
if (!Logger::is_logging()) return false;
|
2009-01-06 13:24:52 +00:00
|
|
|
Handle<String> name = Handle<String>::cast(type->AsLiteral()->handle());
|
|
|
|
if (FLAG_log_regexp) {
|
|
|
|
static Vector<const char> kRegexp = CStrVector("regexp");
|
|
|
|
if (name->IsEqualTo(kRegexp))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-07 14:24:08 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-06 13:24:52 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Sets the function info on a function.
|
|
|
|
// The start_position points to the first '(' character after the function name
|
|
|
|
// in the full script source. When counting characters in the script source the
|
|
|
|
// the first character is number 0 (not 1).
|
|
|
|
void CodeGenerator::SetFunctionInfo(Handle<JSFunction> fun,
|
|
|
|
int length,
|
|
|
|
int function_token_position,
|
|
|
|
int start_position,
|
|
|
|
int end_position,
|
|
|
|
bool is_expression,
|
|
|
|
bool is_toplevel,
|
2009-04-14 00:51:59 +00:00
|
|
|
Handle<Script> script,
|
|
|
|
Handle<String> inferred_name) {
|
2008-07-03 15:10:15 +00:00
|
|
|
fun->shared()->set_length(length);
|
|
|
|
fun->shared()->set_formal_parameter_count(length);
|
|
|
|
fun->shared()->set_script(*script);
|
|
|
|
fun->shared()->set_function_token_position(function_token_position);
|
|
|
|
fun->shared()->set_start_position(start_position);
|
|
|
|
fun->shared()->set_end_position(end_position);
|
|
|
|
fun->shared()->set_is_expression(is_expression);
|
|
|
|
fun->shared()->set_is_toplevel(is_toplevel);
|
2009-04-14 00:51:59 +00:00
|
|
|
fun->shared()->set_inferred_name(*inferred_name);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<Code> ComputeLazyCompile(int argc) {
|
|
|
|
CALL_HEAP_FUNCTION(StubCache::ComputeLazyCompile(argc), Code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> CodeGenerator::BuildBoilerplate(FunctionLiteral* node) {
|
2009-03-09 14:48:34 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// We should not try to compile the same function literal more than
|
|
|
|
// once.
|
|
|
|
node->mark_as_compiled();
|
|
|
|
#endif
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Determine if the function can be lazily compiled. This is
|
|
|
|
// necessary to allow some of our builtin JS files to be lazily
|
|
|
|
// compiled. These builtins cannot be handled lazily by the parser,
|
|
|
|
// since we have to know if a function uses the special natives
|
|
|
|
// syntax, which is something the parser records.
|
|
|
|
bool allow_lazy = node->AllowsLazyCompilation();
|
|
|
|
|
|
|
|
// Generate code
|
|
|
|
Handle<Code> code;
|
|
|
|
if (FLAG_lazy && allow_lazy) {
|
|
|
|
code = ComputeLazyCompile(node->num_parameters());
|
|
|
|
} else {
|
2009-03-31 09:02:40 +00:00
|
|
|
// The bodies of function literals have not yet been visited by
|
|
|
|
// the AST optimizer/analyzer.
|
|
|
|
if (!Rewriter::Optimize(node)) {
|
|
|
|
return Handle<JSFunction>::null();
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
code = MakeCode(node, script_, false);
|
|
|
|
|
2008-07-16 07:07:30 +00:00
|
|
|
// Check for stack-overflow exception.
|
|
|
|
if (code.is_null()) {
|
|
|
|
SetStackOverflow();
|
|
|
|
return Handle<JSFunction>::null();
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Function compilation complete.
|
2009-06-08 13:39:48 +00:00
|
|
|
LOG(CodeCreateEvent(Logger::FUNCTION_TAG, *code, *node->name()));
|
2009-03-31 09:02:40 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_OPROFILE_AGENT
|
|
|
|
OProfileAgent::CreateNativeCodeRegion(*node->name(),
|
2009-06-15 16:33:28 +00:00
|
|
|
code->instruction_start(),
|
|
|
|
code->instruction_size());
|
2009-03-31 09:02:40 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a boilerplate function.
|
|
|
|
Handle<JSFunction> function =
|
|
|
|
Factory::NewFunctionBoilerplate(node->name(),
|
|
|
|
node->materialized_literal_count(),
|
2008-08-22 13:33:59 +00:00
|
|
|
node->contains_array_literal(),
|
2008-07-03 15:10:15 +00:00
|
|
|
code);
|
|
|
|
CodeGenerator::SetFunctionInfo(function, node->num_parameters(),
|
|
|
|
node->function_token_position(),
|
|
|
|
node->start_position(), node->end_position(),
|
2009-04-14 00:51:59 +00:00
|
|
|
node->is_expression(), false, script_,
|
|
|
|
node->inferred_name());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2008-07-03 15:10:15 +00:00
|
|
|
// Notify debugger that a new function has been added.
|
|
|
|
Debugger::OnNewFunction(function);
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Set the expected number of properties for instances and return
|
|
|
|
// the resulting function.
|
|
|
|
SetExpectedNofPropertiesFromEstimate(function,
|
|
|
|
node->expected_property_count());
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-25 18:29:02 +00:00
|
|
|
Handle<Code> CodeGenerator::ComputeCallInitialize(
|
|
|
|
int argc,
|
|
|
|
InLoopFlag in_loop) {
|
|
|
|
if (in_loop == IN_LOOP) {
|
|
|
|
// Force the creation of the corresponding stub outside loops,
|
|
|
|
// because it may be used when clearing the ICs later - it is
|
|
|
|
// possible for a series of IC transitions to lose the in-loop
|
|
|
|
// information, and the IC clearing code can't generate a stub
|
|
|
|
// that it needs so we need to ensure it is generated already.
|
|
|
|
ComputeCallInitialize(argc, NOT_IN_LOOP);
|
|
|
|
}
|
|
|
|
CALL_HEAP_FUNCTION(StubCache::ComputeCallInitialize(argc, in_loop), Code);
|
2008-11-17 05:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void CodeGenerator::ProcessDeclarations(ZoneList<Declaration*>* declarations) {
|
|
|
|
int length = declarations->length();
|
|
|
|
int globals = 0;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Declaration* node = declarations->at(i);
|
|
|
|
Variable* var = node->proxy()->var();
|
|
|
|
Slot* slot = var->slot();
|
|
|
|
|
|
|
|
// If it was not possible to allocate the variable at compile
|
|
|
|
// time, we need to "declare" it at runtime to make sure it
|
|
|
|
// actually exists in the local context.
|
|
|
|
if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
|
|
|
|
VisitDeclaration(node);
|
|
|
|
} else {
|
|
|
|
// Count global variables and functions for later processing
|
|
|
|
globals++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return in case of no declared global functions or variables.
|
|
|
|
if (globals == 0) return;
|
|
|
|
|
|
|
|
// Compute array of global variable and function declarations.
|
|
|
|
Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED);
|
|
|
|
for (int j = 0, i = 0; i < length; i++) {
|
|
|
|
Declaration* node = declarations->at(i);
|
|
|
|
Variable* var = node->proxy()->var();
|
|
|
|
Slot* slot = var->slot();
|
|
|
|
|
|
|
|
if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
|
|
|
|
// Skip - already processed.
|
|
|
|
} else {
|
|
|
|
array->set(j++, *(var->name()));
|
|
|
|
if (node->fun() == NULL) {
|
|
|
|
if (var->mode() == Variable::CONST) {
|
|
|
|
// In case this is const property use the hole.
|
|
|
|
array->set_the_hole(j++);
|
|
|
|
} else {
|
|
|
|
array->set_undefined(j++);
|
|
|
|
}
|
|
|
|
} else {
|
2008-07-16 07:07:30 +00:00
|
|
|
Handle<JSFunction> function = BuildBoilerplate(node->fun());
|
|
|
|
// Check for stack-overflow exception.
|
|
|
|
if (HasStackOverflow()) return;
|
|
|
|
array->set(j++, *function);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke the platform-dependent code generator to do the actual
|
|
|
|
// declaration the global variables and functions.
|
|
|
|
DeclareGlobals(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
|
|
|
|
// Special cases: These 'runtime calls' manipulate the current
|
|
|
|
// frame and are only used 1 or two places, so we generate them
|
|
|
|
// inline instead of generating calls to them. They are used
|
|
|
|
// for implementing Function.prototype.call() and
|
|
|
|
// Function.prototype.apply().
|
|
|
|
CodeGenerator::InlineRuntimeLUT CodeGenerator::kInlineRuntimeLUT[] = {
|
|
|
|
{&CodeGenerator::GenerateIsSmi, "_IsSmi"},
|
|
|
|
{&CodeGenerator::GenerateIsNonNegativeSmi, "_IsNonNegativeSmi"},
|
|
|
|
{&CodeGenerator::GenerateIsArray, "_IsArray"},
|
|
|
|
{&CodeGenerator::GenerateArgumentsLength, "_ArgumentsLength"},
|
|
|
|
{&CodeGenerator::GenerateArgumentsAccess, "_Arguments"},
|
|
|
|
{&CodeGenerator::GenerateValueOf, "_ValueOf"},
|
|
|
|
{&CodeGenerator::GenerateSetValueOf, "_SetValueOf"},
|
|
|
|
{&CodeGenerator::GenerateFastCharCodeAt, "_FastCharCodeAt"},
|
|
|
|
{&CodeGenerator::GenerateObjectEquals, "_ObjectEquals"},
|
2009-06-15 08:04:47 +00:00
|
|
|
{&CodeGenerator::GenerateLog, "_Log"},
|
2009-06-15 12:06:48 +00:00
|
|
|
{&CodeGenerator::GenerateRandomPositiveSmi, "_RandomPositiveSmi"},
|
|
|
|
{&CodeGenerator::GenerateMathSin, "_Math_sin"},
|
|
|
|
{&CodeGenerator::GenerateMathCos, "_Math_cos"}
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
CodeGenerator::InlineRuntimeLUT* CodeGenerator::FindInlineRuntimeLUT(
|
|
|
|
Handle<String> name) {
|
|
|
|
const int entries_count =
|
|
|
|
sizeof(kInlineRuntimeLUT) / sizeof(InlineRuntimeLUT);
|
|
|
|
for (int i = 0; i < entries_count; i++) {
|
|
|
|
InlineRuntimeLUT* entry = &kInlineRuntimeLUT[i];
|
|
|
|
if (name->IsEqualTo(CStrVector(entry->name))) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
bool CodeGenerator::CheckForInlineRuntimeCall(CallRuntime* node) {
|
|
|
|
ZoneList<Expression*>* args = node->arguments();
|
2008-11-03 10:16:05 +00:00
|
|
|
Handle<String> name = node->name();
|
2009-03-17 09:33:06 +00:00
|
|
|
if (name->length() > 0 && name->Get(0) == '_') {
|
2009-05-07 09:24:43 +00:00
|
|
|
InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name);
|
|
|
|
if (entry != NULL) {
|
|
|
|
((*this).*(entry->method))(args);
|
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-07 09:24:43 +00:00
|
|
|
bool CodeGenerator::PatchInlineRuntimeEntry(Handle<String> name,
|
|
|
|
const CodeGenerator::InlineRuntimeLUT& new_entry,
|
|
|
|
CodeGenerator::InlineRuntimeLUT* old_entry) {
|
|
|
|
InlineRuntimeLUT* entry = FindInlineRuntimeLUT(name);
|
|
|
|
if (entry == NULL) return false;
|
|
|
|
if (old_entry != NULL) {
|
|
|
|
old_entry->name = entry->name;
|
|
|
|
old_entry->method = entry->method;
|
|
|
|
}
|
|
|
|
entry->name = new_entry.name;
|
|
|
|
entry->method = new_entry.method;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-27 13:00:32 +00:00
|
|
|
void CodeGenerator::CodeForFunctionPosition(FunctionLiteral* fun) {
|
|
|
|
if (FLAG_debug_info) {
|
|
|
|
int pos = fun->start_position();
|
|
|
|
if (pos != RelocInfo::kNoPosition) {
|
|
|
|
masm()->RecordStatementPosition(pos);
|
|
|
|
masm()->RecordPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-02 08:58:18 +00:00
|
|
|
void CodeGenerator::CodeForReturnPosition(FunctionLiteral* fun) {
|
|
|
|
if (FLAG_debug_info) {
|
2009-04-14 14:56:03 +00:00
|
|
|
int pos = fun->end_position();
|
2009-04-02 08:58:18 +00:00
|
|
|
if (pos != RelocInfo::kNoPosition) {
|
|
|
|
masm()->RecordStatementPosition(pos);
|
|
|
|
masm()->RecordPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-27 13:00:32 +00:00
|
|
|
void CodeGenerator::CodeForStatementPosition(Node* node) {
|
2008-12-17 08:45:42 +00:00
|
|
|
if (FLAG_debug_info) {
|
|
|
|
int pos = node->statement_pos();
|
|
|
|
if (pos != RelocInfo::kNoPosition) {
|
|
|
|
masm()->RecordStatementPosition(pos);
|
2009-04-02 08:58:18 +00:00
|
|
|
masm()->RecordPosition(pos);
|
2008-12-17 08:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeGenerator::CodeForSourcePosition(int pos) {
|
|
|
|
if (FLAG_debug_info) {
|
|
|
|
if (pos != RelocInfo::kNoPosition) {
|
|
|
|
masm()->RecordPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
const char* RuntimeStub::GetName() {
|
|
|
|
return Runtime::FunctionForId(id_)->stub_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RuntimeStub::Generate(MacroAssembler* masm) {
|
2008-08-13 09:32:07 +00:00
|
|
|
masm->TailCallRuntime(ExternalReference(id_), num_arguments_);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-06 11:04:03 +00:00
|
|
|
void ArgumentsAccessStub::Generate(MacroAssembler* masm) {
|
|
|
|
switch (type_) {
|
|
|
|
case READ_LENGTH: GenerateReadLength(masm); break;
|
|
|
|
case READ_ELEMENT: GenerateReadElement(masm); break;
|
|
|
|
case NEW_OBJECT: GenerateNewObject(masm); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|