v8/src/fast-codegen.cc

275 lines
6.4 KiB
C++
Raw Normal View History

Initial infrastructure for fast compilation of top-level code. The fast code generator is optimized for compilation time and code size. Currently it is only implemented on IA32. It is potentially triggered for any code in the global scope (including code eval'd in the global scope). It performs a syntactic check and chooses to compile in fast mode if the AST contains only supported constructs and matches some other constraints. Initially supported constructs are * ExpressionStatement, * ReturnStatement, * VariableProxy (variable references) to parameters and stack-allocated locals, * Assignment with lhs a parameter or stack-allocated local, and * Literal This allows compilation of literals at the top level and not much else. All intermediate values are allocated to temporaries and the stack is used for all temporaries. The extra memory traffic is a known issue. The code generated for 'true' is: 0 push ebp 1 mov ebp,esp 3 push esi 4 push edi 5 push 0xf5cca135 ;; object: 0xf5cca135 <undefined> 10 cmp esp,[0x8277efc] 16 jnc 27 (0xf5cbbb1b) 22 call 0xf5cac960 ;; code: STUB, StackCheck, minor: 0 27 push 0xf5cca161 ;; object: 0xf5cca161 <true> 32 mov eax,[esp] 35 mov [ebp+0xf4],eax 38 pop eax 39 mov eax,[ebp+0xf4] 42 mov esp,ebp ;; js return 44 pop ebp 45 ret 0x4 48 mov eax,0xf5cca135 ;; object: 0xf5cca135 <undefined> 53 mov esp,ebp ;; js return 55 pop ebp 56 ret 0x4 Review URL: http://codereview.chromium.org/273050 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3067 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2009-10-14 19:30:50 +00:00
// Copyright 2009 the V8 project authors. All rights reserved.
// 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"
#include "codegen-inl.h"
#include "fast-codegen.h"
namespace v8 {
namespace internal {
Handle<Code> FastCodeGenerator::MakeCode(FunctionLiteral* fun,
Handle<Script> script) {
CodeGenerator::MakeCodePrologue(fun);
const int kInitialBufferSize = 4 * KB;
MacroAssembler masm(NULL, kInitialBufferSize);
FastCodeGenerator cgen(&masm);
cgen.Generate(fun);
if (cgen.HasStackOverflow()) {
ASSERT(!Top::has_pending_exception());
return Handle<Code>::null();
}
Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
return CodeGenerator::MakeCodeEpilogue(fun, &masm, flags, script);
}
int FastCodeGenerator::SlotOffset(Slot* slot) {
// Offset is negative because higher indexes are at lower addresses.
int offset = -slot->index() * kPointerSize;
// Adjust by a (parameter or local) base offset.
switch (slot->type()) {
case Slot::PARAMETER:
offset += (function_->scope()->num_parameters() + 1) * kPointerSize;
break;
case Slot::LOCAL:
offset += JavaScriptFrameConstants::kLocal0Offset;
break;
default:
UNREACHABLE();
}
return offset;
}
void FastCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
if (FLAG_debug_info) {
CodeGenerator::RecordPositions(masm_, fun->start_position());
}
}
void FastCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
if (FLAG_debug_info) {
CodeGenerator::RecordPositions(masm_, fun->end_position());
}
}
void FastCodeGenerator::SetStatementPosition(Statement* stmt) {
if (FLAG_debug_info) {
CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
}
}
void FastCodeGenerator::SetSourcePosition(int pos) {
if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
masm_->RecordPosition(pos);
}
}
Initial infrastructure for fast compilation of top-level code. The fast code generator is optimized for compilation time and code size. Currently it is only implemented on IA32. It is potentially triggered for any code in the global scope (including code eval'd in the global scope). It performs a syntactic check and chooses to compile in fast mode if the AST contains only supported constructs and matches some other constraints. Initially supported constructs are * ExpressionStatement, * ReturnStatement, * VariableProxy (variable references) to parameters and stack-allocated locals, * Assignment with lhs a parameter or stack-allocated local, and * Literal This allows compilation of literals at the top level and not much else. All intermediate values are allocated to temporaries and the stack is used for all temporaries. The extra memory traffic is a known issue. The code generated for 'true' is: 0 push ebp 1 mov ebp,esp 3 push esi 4 push edi 5 push 0xf5cca135 ;; object: 0xf5cca135 <undefined> 10 cmp esp,[0x8277efc] 16 jnc 27 (0xf5cbbb1b) 22 call 0xf5cac960 ;; code: STUB, StackCheck, minor: 0 27 push 0xf5cca161 ;; object: 0xf5cca161 <true> 32 mov eax,[esp] 35 mov [ebp+0xf4],eax 38 pop eax 39 mov eax,[ebp+0xf4] 42 mov esp,ebp ;; js return 44 pop ebp 45 ret 0x4 48 mov eax,0xf5cca135 ;; object: 0xf5cca135 <undefined> 53 mov esp,ebp ;; js return 55 pop ebp 56 ret 0x4 Review URL: http://codereview.chromium.org/273050 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3067 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2009-10-14 19:30:50 +00:00
void FastCodeGenerator::VisitDeclaration(Declaration* decl) {
UNREACHABLE();
}
void FastCodeGenerator::VisitBlock(Block* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitIfStatement(IfStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitForStatement(ForStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
UNREACHABLE();
}
void FastCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitFunctionBoilerplateLiteral(
FunctionBoilerplateLiteral* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitConditional(Conditional* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitSlot(Slot* expr) {
// Slots do not appear directly in the AST.
UNREACHABLE();
}
void FastCodeGenerator::VisitLiteral(Literal* expr) {
// No code is emitted (here) for simple literals.
}
Initial infrastructure for fast compilation of top-level code. The fast code generator is optimized for compilation time and code size. Currently it is only implemented on IA32. It is potentially triggered for any code in the global scope (including code eval'd in the global scope). It performs a syntactic check and chooses to compile in fast mode if the AST contains only supported constructs and matches some other constraints. Initially supported constructs are * ExpressionStatement, * ReturnStatement, * VariableProxy (variable references) to parameters and stack-allocated locals, * Assignment with lhs a parameter or stack-allocated local, and * Literal This allows compilation of literals at the top level and not much else. All intermediate values are allocated to temporaries and the stack is used for all temporaries. The extra memory traffic is a known issue. The code generated for 'true' is: 0 push ebp 1 mov ebp,esp 3 push esi 4 push edi 5 push 0xf5cca135 ;; object: 0xf5cca135 <undefined> 10 cmp esp,[0x8277efc] 16 jnc 27 (0xf5cbbb1b) 22 call 0xf5cac960 ;; code: STUB, StackCheck, minor: 0 27 push 0xf5cca161 ;; object: 0xf5cca161 <true> 32 mov eax,[esp] 35 mov [ebp+0xf4],eax 38 pop eax 39 mov eax,[ebp+0xf4] 42 mov esp,ebp ;; js return 44 pop ebp 45 ret 0x4 48 mov eax,0xf5cca135 ;; object: 0xf5cca135 <undefined> 53 mov esp,ebp ;; js return 55 pop ebp 56 ret 0x4 Review URL: http://codereview.chromium.org/273050 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3067 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2009-10-14 19:30:50 +00:00
void FastCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitThrow(Throw* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitProperty(Property* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitCall(Call* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitCallNew(CallNew* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitCountOperation(CountOperation* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
UNREACHABLE();
}
void FastCodeGenerator::VisitThisFunction(ThisFunction* expr) {
UNREACHABLE();
}
} } // namespace v8::internal