sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 28 | #ifndef V8_ARM_CODEGEN_ARM_H_ |
| 29 | #define V8_ARM_CODEGEN_ARM_H_ |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 30 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 31 | namespace v8 { |
| 32 | namespace internal { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 33 | |
| 34 | // Forward declarations |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 35 | class CompilationInfo; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 36 | class DeferredCode; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 37 | class RegisterAllocator; |
| 38 | class RegisterFile; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 39 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 40 | enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 41 | enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 42 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 43 | |
| 44 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 45 | // Reference support |
| 46 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 47 | // A reference is a C++ stack-allocated object that puts a |
| 48 | // reference on the virtual frame. The reference may be consumed |
| 49 | // by GetValue, TakeValue, SetValue, and Codegen::UnloadReference. |
| 50 | // When the lifetime (scope) of a valid reference ends, it must have |
| 51 | // been consumed, and be in state UNLOADED. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 52 | class Reference BASE_EMBEDDED { |
| 53 | public: |
| 54 | // The values of the types is important, see size(). |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 55 | enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 56 | Reference(CodeGenerator* cgen, |
| 57 | Expression* expression, |
| 58 | bool persist_after_get = false); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 59 | ~Reference(); |
| 60 | |
| 61 | Expression* expression() const { return expression_; } |
| 62 | Type type() const { return type_; } |
| 63 | void set_type(Type value) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 64 | ASSERT_EQ(ILLEGAL, type_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 65 | type_ = value; |
| 66 | } |
| 67 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 68 | void set_unloaded() { |
| 69 | ASSERT_NE(ILLEGAL, type_); |
| 70 | ASSERT_NE(UNLOADED, type_); |
| 71 | type_ = UNLOADED; |
| 72 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 73 | // The size the reference takes up on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 74 | int size() const { |
| 75 | return (type_ < SLOT) ? 0 : type_; |
| 76 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 77 | |
| 78 | bool is_illegal() const { return type_ == ILLEGAL; } |
| 79 | bool is_slot() const { return type_ == SLOT; } |
| 80 | bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 81 | bool is_unloaded() const { return type_ == UNLOADED; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 82 | |
| 83 | // Return the name. Only valid for named property references. |
| 84 | Handle<String> GetName(); |
| 85 | |
| 86 | // Generate code to push the value of the reference on top of the |
| 87 | // expression stack. The reference is expected to be already on top of |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 88 | // the expression stack, and it is consumed by the call unless the |
| 89 | // reference is for a compound assignment. |
| 90 | // If the reference is not consumed, it is left in place under its value. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 91 | void GetValue(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 92 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 93 | // Generate code to pop a reference, push the value of the reference, |
| 94 | // and then spill the stack frame. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 95 | inline void GetValueAndSpill(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 96 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 97 | // Generate code to store the value on top of the expression stack in the |
| 98 | // reference. The reference is expected to be immediately below the value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 99 | // on the expression stack. The value is stored in the location specified |
| 100 | // by the reference, and is left on top of the stack, after the reference |
| 101 | // is popped from beneath it (unloaded). |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 102 | void SetValue(InitState init_state); |
| 103 | |
| 104 | private: |
| 105 | CodeGenerator* cgen_; |
| 106 | Expression* expression_; |
| 107 | Type type_; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 108 | // Keep the reference on the stack after get, so it can be used by set later. |
| 109 | bool persist_after_get_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | |
| 113 | // ------------------------------------------------------------------------- |
| 114 | // Code generation state |
| 115 | |
| 116 | // The state is passed down the AST by the code generator (and back up, in |
| 117 | // the form of the state of the label pair). It is threaded through the |
| 118 | // call stack. Constructing a state implicitly pushes it on the owning code |
| 119 | // generator's stack of states, and destroying one implicitly pops it. |
| 120 | |
| 121 | class CodeGenState BASE_EMBEDDED { |
| 122 | public: |
| 123 | // Create an initial code generator state. Destroying the initial state |
| 124 | // leaves the code generator with a NULL state. |
| 125 | explicit CodeGenState(CodeGenerator* owner); |
| 126 | |
| 127 | // Create a code generator state based on a code generator's current |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 128 | // state. The new state has its own pair of branch labels. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 129 | CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 130 | JumpTarget* true_target, |
| 131 | JumpTarget* false_target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 132 | |
| 133 | // Destroy a code generator state and restore the owning code generator's |
| 134 | // previous state. |
| 135 | ~CodeGenState(); |
| 136 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 137 | JumpTarget* true_target() const { return true_target_; } |
| 138 | JumpTarget* false_target() const { return false_target_; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | CodeGenerator* owner_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | JumpTarget* true_target_; |
| 143 | JumpTarget* false_target_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 144 | CodeGenState* previous_; |
| 145 | }; |
| 146 | |
| 147 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 148 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 149 | // CodeGenerator |
| 150 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 151 | class CodeGenerator: public AstVisitor { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 152 | public: |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 153 | // Compilation mode. Either the compiler is used as the primary |
| 154 | // compiler and needs to setup everything or the compiler is used as |
| 155 | // the secondary compiler for split compilation and has to handle |
| 156 | // bailouts. |
| 157 | enum Mode { |
| 158 | PRIMARY, |
| 159 | SECONDARY |
| 160 | }; |
| 161 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 162 | // Takes a function literal, generates code for it. This function should only |
| 163 | // be called by compiler.cc. |
| 164 | static Handle<Code> MakeCode(FunctionLiteral* fun, |
| 165 | Handle<Script> script, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 166 | bool is_eval, |
| 167 | CompilationInfo* info); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 168 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 169 | // Printing of AST, etc. as requested by flags. |
| 170 | static void MakeCodePrologue(FunctionLiteral* fun); |
| 171 | |
| 172 | // Allocate and install the code. |
| 173 | static Handle<Code> MakeCodeEpilogue(FunctionLiteral* fun, |
| 174 | MacroAssembler* masm, |
| 175 | Code::Flags flags, |
| 176 | Handle<Script> script); |
| 177 | |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 178 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 179 | static bool ShouldGenerateLog(Expression* type); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 180 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 181 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 182 | static void SetFunctionInfo(Handle<JSFunction> fun, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 183 | FunctionLiteral* lit, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 184 | bool is_toplevel, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 185 | Handle<Script> script); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 186 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 187 | static void RecordPositions(MacroAssembler* masm, int pos); |
| 188 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 189 | // Accessors |
| 190 | MacroAssembler* masm() { return masm_; } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 191 | VirtualFrame* frame() const { return frame_; } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 192 | Handle<Script> script() { return script_; } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 193 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 194 | bool has_valid_frame() const { return frame_ != NULL; } |
| 195 | |
| 196 | // Set the virtual frame to be new_frame, with non-frame register |
| 197 | // reference counts given by non_frame_registers. The non-frame |
| 198 | // register reference counts of the old frame are returned in |
| 199 | // non_frame_registers. |
| 200 | void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers); |
| 201 | |
| 202 | void DeleteFrame(); |
| 203 | |
| 204 | RegisterAllocator* allocator() const { return allocator_; } |
| 205 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 206 | CodeGenState* state() { return state_; } |
| 207 | void set_state(CodeGenState* state) { state_ = state; } |
| 208 | |
| 209 | void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 210 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 211 | static const int kUnknownIntValue = -1; |
| 212 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 213 | private: |
| 214 | // Construction/Destruction |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 215 | CodeGenerator(MacroAssembler* masm, Handle<Script> script, bool is_eval); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 216 | |
| 217 | // Accessors |
| 218 | Scope* scope() const { return scope_; } |
| 219 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 220 | // Generating deferred code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 221 | void ProcessDeferred(); |
| 222 | |
| 223 | bool is_eval() { return is_eval_; } |
| 224 | |
| 225 | // State |
| 226 | bool has_cc() const { return cc_reg_ != al; } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 227 | JumpTarget* true_target() const { return state_->true_target(); } |
| 228 | JumpTarget* false_target() const { return state_->false_target(); } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 229 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 230 | // We don't track loop nesting level on ARM yet. |
| 231 | int loop_nesting() const { return 0; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 232 | |
| 233 | // Node visitors. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 234 | void VisitStatements(ZoneList<Statement*>* statements); |
| 235 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 236 | #define DEF_VISIT(type) \ |
| 237 | void Visit##type(type* node); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 238 | AST_NODE_LIST(DEF_VISIT) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 239 | #undef DEF_VISIT |
| 240 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 241 | // Visit a statement and then spill the virtual frame if control flow can |
| 242 | // reach the end of the statement (ie, it does not exit via break, |
| 243 | // continue, return, or throw). This function is used temporarily while |
| 244 | // the code generator is being transformed. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 245 | inline void VisitAndSpill(Statement* statement); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 246 | |
| 247 | // Visit a list of statements and then spill the virtual frame if control |
| 248 | // flow can reach the end of the list. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 249 | inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 250 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 251 | // Main code generation function |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 252 | void Generate(FunctionLiteral* fun, Mode mode, CompilationInfo* info); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 253 | |
| 254 | // The following are used by class Reference. |
| 255 | void LoadReference(Reference* ref); |
| 256 | void UnloadReference(Reference* ref); |
| 257 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 258 | static MemOperand ContextOperand(Register context, int index) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 259 | return MemOperand(context, Context::SlotOffset(index)); |
| 260 | } |
| 261 | |
| 262 | MemOperand SlotOperand(Slot* slot, Register tmp); |
| 263 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 264 | MemOperand ContextSlotOperandCheckExtensions(Slot* slot, |
| 265 | Register tmp, |
| 266 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 267 | JumpTarget* slow); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 268 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 269 | // Expressions |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 270 | static MemOperand GlobalObject() { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 271 | return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 272 | } |
| 273 | |
| 274 | void LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 275 | JumpTarget* true_target, |
| 276 | JumpTarget* false_target, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 277 | bool force_cc); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 278 | void Load(Expression* expr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 279 | void LoadGlobal(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 280 | void LoadGlobalReceiver(Register scratch); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 281 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 282 | // Generate code to push the value of an expression on top of the frame |
| 283 | // and then spill the frame fully to memory. This function is used |
| 284 | // temporarily while the code generator is being transformed. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 285 | inline void LoadAndSpill(Expression* expression); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 286 | |
| 287 | // Call LoadCondition and then spill the virtual frame unless control flow |
| 288 | // cannot reach the end of the expression (ie, by emitting only |
| 289 | // unconditional jumps to the control targets). |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 290 | inline void LoadConditionAndSpill(Expression* expression, |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 291 | JumpTarget* true_target, |
| 292 | JumpTarget* false_target, |
| 293 | bool force_control); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 294 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 295 | // Read a value from a slot and leave it on top of the expression stack. |
| 296 | void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 297 | // Store the value on top of the stack to a slot. |
| 298 | void StoreToSlot(Slot* slot, InitState init_state); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 299 | // Load a keyed property, leaving it in r0. The receiver and key are |
| 300 | // passed on the stack, and remain there. |
| 301 | void EmitKeyedLoad(bool is_global); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 302 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 303 | void LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 304 | TypeofState typeof_state, |
| 305 | Register tmp, |
| 306 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 307 | JumpTarget* slow); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 308 | |
| 309 | // Special code for typeof expressions: Unfortunately, we must |
| 310 | // be careful when loading the expression in 'typeof' |
| 311 | // expressions. We are not allowed to throw reference errors for |
| 312 | // non-existing properties of the global object, so we must make it |
| 313 | // look like an explicit property access, instead of an access |
| 314 | // through the context chain. |
| 315 | void LoadTypeofExpression(Expression* x); |
| 316 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 317 | void ToBoolean(JumpTarget* true_target, JumpTarget* false_target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 318 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 319 | void GenericBinaryOperation(Token::Value op, |
| 320 | OverwriteMode overwrite_mode, |
| 321 | int known_rhs = kUnknownIntValue); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 322 | void Comparison(Condition cc, |
| 323 | Expression* left, |
| 324 | Expression* right, |
| 325 | bool strict = false); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 326 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 327 | void SmiOperation(Token::Value op, |
| 328 | Handle<Object> value, |
| 329 | bool reversed, |
| 330 | OverwriteMode mode); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 331 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 332 | void CallWithArguments(ZoneList<Expression*>* arguments, |
| 333 | CallFunctionFlags flags, |
| 334 | int position); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 335 | |
| 336 | // Control flow |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 337 | void Branch(bool if_true, JumpTarget* target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 338 | void CheckStack(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 339 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 340 | struct InlineRuntimeLUT { |
| 341 | void (CodeGenerator::*method)(ZoneList<Expression*>*); |
| 342 | const char* name; |
| 343 | }; |
| 344 | |
| 345 | static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 346 | bool CheckForInlineRuntimeCall(CallRuntime* node); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 347 | static bool PatchInlineRuntimeEntry(Handle<String> name, |
| 348 | const InlineRuntimeLUT& new_entry, |
| 349 | InlineRuntimeLUT* old_entry); |
| 350 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 351 | static Handle<Code> ComputeLazyCompile(int argc); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 352 | void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 353 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 354 | static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 355 | |
| 356 | // Declare global variables and functions in the given array of |
| 357 | // name/value pairs. |
| 358 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 359 | |
| 360 | // Instantiate the function boilerplate. |
| 361 | void InstantiateBoilerplate(Handle<JSFunction> boilerplate); |
| 362 | |
| 363 | // Support for type checks. |
| 364 | void GenerateIsSmi(ZoneList<Expression*>* args); |
| 365 | void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 366 | void GenerateIsArray(ZoneList<Expression*>* args); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 367 | void GenerateIsObject(ZoneList<Expression*>* args); |
| 368 | void GenerateIsFunction(ZoneList<Expression*>* args); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 369 | void GenerateIsUndetectableObject(ZoneList<Expression*>* args); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 370 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 371 | // Support for construct call checks. |
| 372 | void GenerateIsConstructCall(ZoneList<Expression*>* args); |
| 373 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 374 | // Support for arguments.length and arguments[?]. |
| 375 | void GenerateArgumentsLength(ZoneList<Expression*>* args); |
| 376 | void GenerateArgumentsAccess(ZoneList<Expression*>* args); |
| 377 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 378 | // Support for accessing the class and value fields of an object. |
| 379 | void GenerateClassOf(ZoneList<Expression*>* args); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 380 | void GenerateValueOf(ZoneList<Expression*>* args); |
| 381 | void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 382 | |
| 383 | // Fast support for charCodeAt(n). |
| 384 | void GenerateFastCharCodeAt(ZoneList<Expression*>* args); |
| 385 | |
| 386 | // Fast support for object equality testing. |
| 387 | void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 388 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 389 | void GenerateLog(ZoneList<Expression*>* args); |
| 390 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 391 | // Fast support for Math.random(). |
| 392 | void GenerateRandomPositiveSmi(ZoneList<Expression*>* args); |
| 393 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 394 | // Fast support for StringAdd. |
| 395 | void GenerateStringAdd(ZoneList<Expression*>* args); |
| 396 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 397 | // Fast support for SubString. |
| 398 | void GenerateSubString(ZoneList<Expression*>* args); |
| 399 | |
| 400 | // Fast support for StringCompare. |
| 401 | void GenerateStringCompare(ZoneList<Expression*>* args); |
| 402 | |
| 403 | // Support for direct calls from JavaScript to native RegExp code. |
| 404 | void GenerateRegExpExec(ZoneList<Expression*>* args); |
| 405 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 406 | // Simple condition analysis. |
| 407 | enum ConditionAnalysis { |
| 408 | ALWAYS_TRUE, |
| 409 | ALWAYS_FALSE, |
| 410 | DONT_KNOW |
| 411 | }; |
| 412 | ConditionAnalysis AnalyzeCondition(Expression* cond); |
| 413 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 414 | // Methods used to indicate which source code is generated for. Source |
| 415 | // positions are collected by the assembler and emitted with the relocation |
| 416 | // information. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 417 | void CodeForFunctionPosition(FunctionLiteral* fun); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 418 | void CodeForReturnPosition(FunctionLiteral* fun); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 419 | void CodeForStatementPosition(Statement* node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 420 | void CodeForDoWhileConditionPosition(DoWhileStatement* stmt); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 421 | void CodeForSourcePosition(int pos); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 422 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 423 | #ifdef DEBUG |
| 424 | // True if the registers are valid for entry to a block. |
| 425 | bool HasValidEntryRegisters(); |
| 426 | #endif |
| 427 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 428 | bool is_eval_; // Tells whether code is generated for eval. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 429 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 430 | Handle<Script> script_; |
| 431 | List<DeferredCode*> deferred_; |
| 432 | |
| 433 | // Assembler |
| 434 | MacroAssembler* masm_; // to generate code |
| 435 | |
| 436 | // Code generation state |
| 437 | Scope* scope_; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 438 | VirtualFrame* frame_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 439 | RegisterAllocator* allocator_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 440 | Condition cc_reg_; |
| 441 | CodeGenState* state_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 442 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 443 | // Jump targets |
| 444 | BreakTarget function_return_; |
| 445 | |
| 446 | // True if the function return is shadowed (ie, jumping to the target |
| 447 | // function_return_ does not jump to the true function return, but rather |
| 448 | // to some unlinking code). |
| 449 | bool function_return_is_shadowed_; |
| 450 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 451 | static InlineRuntimeLUT kInlineRuntimeLUT[]; |
| 452 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 453 | friend class VirtualFrame; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 454 | friend class JumpTarget; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 455 | friend class Reference; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 456 | friend class FastCodeGenerator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 457 | friend class FullCodeGenerator; |
| 458 | friend class FullCodeGenSyntaxChecker; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 459 | |
| 460 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 461 | }; |
| 462 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 463 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 464 | class GenericBinaryOpStub : public CodeStub { |
| 465 | public: |
| 466 | GenericBinaryOpStub(Token::Value op, |
| 467 | OverwriteMode mode, |
| 468 | int constant_rhs = CodeGenerator::kUnknownIntValue) |
| 469 | : op_(op), |
| 470 | mode_(mode), |
| 471 | constant_rhs_(constant_rhs), |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 472 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)), |
| 473 | name_(NULL) { } |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 474 | |
| 475 | private: |
| 476 | Token::Value op_; |
| 477 | OverwriteMode mode_; |
| 478 | int constant_rhs_; |
| 479 | bool specialized_on_rhs_; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 480 | char* name_; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 481 | |
| 482 | static const int kMaxKnownRhs = 0x40000000; |
| 483 | |
| 484 | // Minor key encoding in 16 bits. |
| 485 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 486 | class OpBits: public BitField<Token::Value, 2, 6> {}; |
| 487 | class KnownIntBits: public BitField<int, 8, 8> {}; |
| 488 | |
| 489 | Major MajorKey() { return GenericBinaryOp; } |
| 490 | int MinorKey() { |
| 491 | // Encode the parameters in a unique 16 bit value. |
| 492 | return OpBits::encode(op_) |
| 493 | | ModeBits::encode(mode_) |
| 494 | | KnownIntBits::encode(MinorKeyForKnownInt()); |
| 495 | } |
| 496 | |
| 497 | void Generate(MacroAssembler* masm); |
| 498 | void HandleNonSmiBitwiseOp(MacroAssembler* masm); |
| 499 | |
| 500 | static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) { |
| 501 | if (constant_rhs == CodeGenerator::kUnknownIntValue) return false; |
| 502 | if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3; |
| 503 | if (op == Token::MOD) { |
| 504 | if (constant_rhs <= 1) return false; |
| 505 | if (constant_rhs <= 10) return true; |
| 506 | if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true; |
| 507 | return false; |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | int MinorKeyForKnownInt() { |
| 513 | if (!specialized_on_rhs_) return 0; |
| 514 | if (constant_rhs_ <= 10) return constant_rhs_ + 1; |
| 515 | ASSERT(IsPowerOf2(constant_rhs_)); |
| 516 | int key = 12; |
| 517 | int d = constant_rhs_; |
| 518 | while ((d & 1) == 0) { |
| 519 | key++; |
| 520 | d >>= 1; |
| 521 | } |
| 522 | return key; |
| 523 | } |
| 524 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 525 | const char* GetName(); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 526 | |
| 527 | #ifdef DEBUG |
| 528 | void Print() { |
| 529 | if (!specialized_on_rhs_) { |
| 530 | PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); |
| 531 | } else { |
| 532 | PrintF("GenericBinaryOpStub (%s by %d)\n", |
| 533 | Token::String(op_), |
| 534 | constant_rhs_); |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | }; |
| 539 | |
| 540 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame^] | 541 | class StringCompareStub: public CodeStub { |
| 542 | public: |
| 543 | StringCompareStub() { } |
| 544 | |
| 545 | // Compare two flat ASCII strings and returns result in r0. |
| 546 | // Does not use the stack. |
| 547 | static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 548 | Register left, |
| 549 | Register right, |
| 550 | Register scratch1, |
| 551 | Register scratch2, |
| 552 | Register scratch3, |
| 553 | Register scratch4); |
| 554 | |
| 555 | private: |
| 556 | Major MajorKey() { return StringCompare; } |
| 557 | int MinorKey() { return 0; } |
| 558 | |
| 559 | void Generate(MacroAssembler* masm); |
| 560 | }; |
| 561 | |
| 562 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 563 | } } // namespace v8::internal |
| 564 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 565 | #endif // V8_ARM_CODEGEN_ARM_H_ |