1 // Copyright 2010 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #ifndef V8_FAST_CODEGEN_IA32_H_ 29 #define V8_FAST_CODEGEN_IA32_H_ 30 31 #include "v8.h" 32 33 #include "ast.h" 34 #include "compiler.h" 35 #include "list.h" 36 37 namespace v8 { 38 namespace internal { 39 40 class FastCodeGenSyntaxChecker: public AstVisitor { 41 public: 42 explicit FastCodeGenSyntaxChecker() 43 : info_(NULL), has_supported_syntax_(true) { 44 } 45 46 void Check(CompilationInfo* info); 47 48 CompilationInfo* info() { return info_; } 49 bool has_supported_syntax() { return has_supported_syntax_; } 50 51 private: 52 void VisitDeclarations(ZoneList<Declaration*>* decls); 53 void VisitStatements(ZoneList<Statement*>* stmts); 54 55 // AST node visit functions. 56 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); 57 AST_NODE_LIST(DECLARE_VISIT) 58 #undef DECLARE_VISIT 59 60 CompilationInfo* info_; 61 bool has_supported_syntax_; 62 63 DISALLOW_COPY_AND_ASSIGN(FastCodeGenSyntaxChecker); 64 }; 65 66 67 class FastCodeGenerator: public AstVisitor { 68 public: 69 explicit FastCodeGenerator(MacroAssembler* masm) 70 : masm_(masm), info_(NULL), destination_(no_reg), smi_bits_(0) { 71 } 72 73 static Handle<Code> MakeCode(CompilationInfo* info); 74 75 void Generate(CompilationInfo* compilation_info); 76 77 private: 78 MacroAssembler* masm() { return masm_; } 79 CompilationInfo* info() { return info_; } 80 81 Register destination() { return destination_; } 82 void set_destination(Register reg) { destination_ = reg; } 83 84 FunctionLiteral* function() { return info_->function(); } 85 Scope* scope() { return info_->scope(); } 86 87 // Platform-specific fixed registers, all guaranteed distinct. 88 Register accumulator0(); 89 Register accumulator1(); 90 Register scratch0(); 91 Register scratch1(); 92 Register receiver_reg(); 93 Register context_reg(); 94 95 Register other_accumulator(Register reg) { 96 ASSERT(reg.is(accumulator0()) || reg.is(accumulator1())); 97 return (reg.is(accumulator0())) ? accumulator1() : accumulator0(); 98 } 99 100 // Flags are true if the respective register is statically known to hold a 101 // smi. We do not track every register, only the accumulator registers. 102 bool is_smi(Register reg) { 103 ASSERT(!reg.is(no_reg)); 104 return (smi_bits_ & reg.bit()) != 0; 105 } 106 void set_as_smi(Register reg) { 107 ASSERT(!reg.is(no_reg)); 108 smi_bits_ = smi_bits_ | reg.bit(); 109 } 110 void clear_as_smi(Register reg) { 111 ASSERT(!reg.is(no_reg)); 112 smi_bits_ = smi_bits_ & ~reg.bit(); 113 } 114 115 // AST node visit functions. 116 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); 117 AST_NODE_LIST(DECLARE_VISIT) 118 #undef DECLARE_VISIT 119 120 // Emit code to load the receiver from the stack into receiver_reg. 121 void EmitLoadReceiver(); 122 123 // Emit code to load a global variable directly from a global property 124 // cell into the destination register. 125 void EmitGlobalVariableLoad(Handle<Object> cell); 126 127 // Emit a store to an own property of this. The stored value is expected 128 // in accumulator0 and the receiver in receiver_reg. The receiver 129 // register is preserved and the result (the stored value) is left in the 130 // destination register. 131 void EmitThisPropertyStore(Handle<String> name); 132 133 // Emit a load from an own property of this. The receiver is expected in 134 // receiver_reg. The receiver register is preserved and the result is 135 // left in the destination register. 136 void EmitThisPropertyLoad(Handle<String> name); 137 138 // Emit a bitwise or operation. The left operand is in accumulator1 and 139 // the right is in accumulator0. The result should be left in the 140 // destination register. 141 void EmitBitOr(); 142 143 MacroAssembler* masm_; 144 CompilationInfo* info_; 145 146 Register destination_; 147 uint32_t smi_bits_; 148 149 DISALLOW_COPY_AND_ASSIGN(FastCodeGenerator); 150 }; 151 152 153 } } // namespace v8::internal 154 155 #endif // V8_FAST_CODEGEN_IA32_H_ 156