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 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 31 | #include "ic-inl.h" |
| 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 35 | |
| 36 | // Forward declarations |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 37 | class CompilationInfo; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 38 | class DeferredCode; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 39 | class RegisterAllocator; |
| 40 | class RegisterFile; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 41 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 42 | enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 43 | enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 44 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 45 | |
| 46 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 47 | // Reference support |
| 48 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 49 | // A reference is a C++ stack-allocated object that puts a |
| 50 | // reference on the virtual frame. The reference may be consumed |
| 51 | // by GetValue, TakeValue, SetValue, and Codegen::UnloadReference. |
| 52 | // When the lifetime (scope) of a valid reference ends, it must have |
| 53 | // been consumed, and be in state UNLOADED. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 54 | class Reference BASE_EMBEDDED { |
| 55 | public: |
| 56 | // The values of the types is important, see size(). |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 57 | enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 58 | Reference(CodeGenerator* cgen, |
| 59 | Expression* expression, |
| 60 | bool persist_after_get = false); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 61 | ~Reference(); |
| 62 | |
| 63 | Expression* expression() const { return expression_; } |
| 64 | Type type() const { return type_; } |
| 65 | void set_type(Type value) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 66 | ASSERT_EQ(ILLEGAL, type_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 67 | type_ = value; |
| 68 | } |
| 69 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 70 | void set_unloaded() { |
| 71 | ASSERT_NE(ILLEGAL, type_); |
| 72 | ASSERT_NE(UNLOADED, type_); |
| 73 | type_ = UNLOADED; |
| 74 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 75 | // The size the reference takes up on the stack. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 76 | int size() const { |
| 77 | return (type_ < SLOT) ? 0 : type_; |
| 78 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 79 | |
| 80 | bool is_illegal() const { return type_ == ILLEGAL; } |
| 81 | bool is_slot() const { return type_ == SLOT; } |
| 82 | bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 83 | bool is_unloaded() const { return type_ == UNLOADED; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 84 | |
| 85 | // Return the name. Only valid for named property references. |
| 86 | Handle<String> GetName(); |
| 87 | |
| 88 | // Generate code to push the value of the reference on top of the |
| 89 | // 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] | 90 | // the expression stack, and it is consumed by the call unless the |
| 91 | // reference is for a compound assignment. |
| 92 | // 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] | 93 | void GetValue(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 94 | |
| 95 | // Generate code to store the value on top of the expression stack in the |
| 96 | // reference. The reference is expected to be immediately below the value |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 97 | // on the expression stack. The value is stored in the location specified |
| 98 | // by the reference, and is left on top of the stack, after the reference |
| 99 | // is popped from beneath it (unloaded). |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 100 | void SetValue(InitState init_state); |
| 101 | |
| 102 | private: |
| 103 | CodeGenerator* cgen_; |
| 104 | Expression* expression_; |
| 105 | Type type_; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 106 | // Keep the reference on the stack after get, so it can be used by set later. |
| 107 | bool persist_after_get_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | |
| 111 | // ------------------------------------------------------------------------- |
| 112 | // Code generation state |
| 113 | |
| 114 | // The state is passed down the AST by the code generator (and back up, in |
| 115 | // the form of the state of the label pair). It is threaded through the |
| 116 | // call stack. Constructing a state implicitly pushes it on the owning code |
| 117 | // generator's stack of states, and destroying one implicitly pops it. |
| 118 | |
| 119 | class CodeGenState BASE_EMBEDDED { |
| 120 | public: |
| 121 | // Create an initial code generator state. Destroying the initial state |
| 122 | // leaves the code generator with a NULL state. |
| 123 | explicit CodeGenState(CodeGenerator* owner); |
| 124 | |
| 125 | // 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] | 126 | // state. The new state has its own pair of branch labels. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 127 | CodeGenState(CodeGenerator* owner, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 128 | JumpTarget* true_target, |
| 129 | JumpTarget* false_target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 130 | |
| 131 | // Destroy a code generator state and restore the owning code generator's |
| 132 | // previous state. |
| 133 | ~CodeGenState(); |
| 134 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 135 | JumpTarget* true_target() const { return true_target_; } |
| 136 | JumpTarget* false_target() const { return false_target_; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | CodeGenerator* owner_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | JumpTarget* true_target_; |
| 141 | JumpTarget* false_target_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 142 | CodeGenState* previous_; |
| 143 | }; |
| 144 | |
| 145 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 146 | // ------------------------------------------------------------------------- |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 147 | // Arguments allocation mode |
| 148 | |
| 149 | enum ArgumentsAllocationMode { |
| 150 | NO_ARGUMENTS_ALLOCATION, |
| 151 | EAGER_ARGUMENTS_ALLOCATION, |
| 152 | LAZY_ARGUMENTS_ALLOCATION |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | // Different nop operations are used by the code generator to detect certain |
| 157 | // states of the generated code. |
| 158 | enum NopMarkerTypes { |
| 159 | NON_MARKING_NOP = 0, |
| 160 | NAMED_PROPERTY_LOAD_INLINED |
| 161 | }; |
| 162 | |
| 163 | |
| 164 | // ------------------------------------------------------------------------- |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 165 | // CodeGenerator |
| 166 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 167 | class CodeGenerator: public AstVisitor { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 168 | public: |
| 169 | // Takes a function literal, generates code for it. This function should only |
| 170 | // be called by compiler.cc. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 171 | static Handle<Code> MakeCode(CompilationInfo* info); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 172 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 173 | // Printing of AST, etc. as requested by flags. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 174 | static void MakeCodePrologue(CompilationInfo* info); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 175 | |
| 176 | // Allocate and install the code. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 177 | static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm, |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 178 | Code::Flags flags, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 179 | CompilationInfo* info); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 180 | |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 181 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 182 | static bool ShouldGenerateLog(Expression* type); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 183 | #endif |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 184 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 185 | static void SetFunctionInfo(Handle<JSFunction> fun, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 186 | FunctionLiteral* lit, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 187 | bool is_toplevel, |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 188 | Handle<Script> script); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 189 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 190 | static void RecordPositions(MacroAssembler* masm, int pos); |
| 191 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 192 | // Accessors |
| 193 | MacroAssembler* masm() { return masm_; } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 194 | VirtualFrame* frame() const { return frame_; } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 195 | inline Handle<Script> script(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 196 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 197 | bool has_valid_frame() const { return frame_ != NULL; } |
| 198 | |
| 199 | // Set the virtual frame to be new_frame, with non-frame register |
| 200 | // reference counts given by non_frame_registers. The non-frame |
| 201 | // register reference counts of the old frame are returned in |
| 202 | // non_frame_registers. |
| 203 | void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers); |
| 204 | |
| 205 | void DeleteFrame(); |
| 206 | |
| 207 | RegisterAllocator* allocator() const { return allocator_; } |
| 208 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 209 | CodeGenState* state() { return state_; } |
| 210 | void set_state(CodeGenState* state) { state_ = state; } |
| 211 | |
| 212 | void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 213 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 214 | static const int kUnknownIntValue = -1; |
| 215 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 216 | // If the name is an inline runtime function call return the number of |
| 217 | // expected arguments. Otherwise return -1. |
| 218 | static int InlineRuntimeCallArgumentsCount(Handle<String> name); |
| 219 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 220 | private: |
| 221 | // Construction/Destruction |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 222 | explicit CodeGenerator(MacroAssembler* masm); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 223 | |
| 224 | // Accessors |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 225 | inline bool is_eval(); |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 226 | inline Scope* scope(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 227 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 228 | // Generating deferred code. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 229 | void ProcessDeferred(); |
| 230 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 231 | // State |
| 232 | bool has_cc() const { return cc_reg_ != al; } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 233 | JumpTarget* true_target() const { return state_->true_target(); } |
| 234 | JumpTarget* false_target() const { return state_->false_target(); } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 235 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 236 | // Track loop nesting level. |
| 237 | int loop_nesting() const { return loop_nesting_; } |
| 238 | void IncrementLoopNesting() { loop_nesting_++; } |
| 239 | void DecrementLoopNesting() { loop_nesting_--; } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 240 | |
| 241 | // Node visitors. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 242 | void VisitStatements(ZoneList<Statement*>* statements); |
| 243 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 244 | #define DEF_VISIT(type) \ |
| 245 | void Visit##type(type* node); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 246 | AST_NODE_LIST(DEF_VISIT) |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 247 | #undef DEF_VISIT |
| 248 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 249 | // Visit a statement and then spill the virtual frame if control flow can |
| 250 | // reach the end of the statement (ie, it does not exit via break, |
| 251 | // continue, return, or throw). This function is used temporarily while |
| 252 | // the code generator is being transformed. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 253 | inline void VisitAndSpill(Statement* statement); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 254 | |
| 255 | // Visit a list of statements and then spill the virtual frame if control |
| 256 | // flow can reach the end of the list. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 257 | inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 258 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 259 | // Main code generation function |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 260 | void Generate(CompilationInfo* info); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 261 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 262 | // Returns the arguments allocation mode. |
| 263 | ArgumentsAllocationMode ArgumentsMode(); |
| 264 | |
| 265 | // Store the arguments object and allocate it if necessary. |
| 266 | void StoreArgumentsObject(bool initial); |
| 267 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 268 | // The following are used by class Reference. |
| 269 | void LoadReference(Reference* ref); |
| 270 | void UnloadReference(Reference* ref); |
| 271 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 272 | static MemOperand ContextOperand(Register context, int index) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 273 | return MemOperand(context, Context::SlotOffset(index)); |
| 274 | } |
| 275 | |
| 276 | MemOperand SlotOperand(Slot* slot, Register tmp); |
| 277 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 278 | MemOperand ContextSlotOperandCheckExtensions(Slot* slot, |
| 279 | Register tmp, |
| 280 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 281 | JumpTarget* slow); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 282 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 283 | // Expressions |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 284 | static MemOperand GlobalObject() { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 285 | return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 286 | } |
| 287 | |
| 288 | void LoadCondition(Expression* x, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 289 | JumpTarget* true_target, |
| 290 | JumpTarget* false_target, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 291 | bool force_cc); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 292 | void Load(Expression* expr); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 293 | void LoadGlobal(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 294 | void LoadGlobalReceiver(Register scratch); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 295 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 296 | // Generate code to push the value of an expression on top of the frame |
| 297 | // and then spill the frame fully to memory. This function is used |
| 298 | // temporarily while the code generator is being transformed. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 299 | inline void LoadAndSpill(Expression* expression); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 300 | |
| 301 | // Call LoadCondition and then spill the virtual frame unless control flow |
| 302 | // cannot reach the end of the expression (ie, by emitting only |
| 303 | // unconditional jumps to the control targets). |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 304 | inline void LoadConditionAndSpill(Expression* expression, |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 305 | JumpTarget* true_target, |
| 306 | JumpTarget* false_target, |
| 307 | bool force_control); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 308 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 309 | // Read a value from a slot and leave it on top of the expression stack. |
| 310 | void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 311 | void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 312 | // Store the value on top of the stack to a slot. |
| 313 | void StoreToSlot(Slot* slot, InitState init_state); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 314 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 315 | // Load a named property, leaving it in r0. The receiver is passed on the |
| 316 | // stack, and remains there. |
| 317 | void EmitNamedLoad(Handle<String> name, bool is_contextual); |
| 318 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 319 | // Load a keyed property, leaving it in r0. The receiver and key are |
| 320 | // passed on the stack, and remain there. |
| 321 | void EmitKeyedLoad(bool is_global); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 322 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 323 | void LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 324 | TypeofState typeof_state, |
| 325 | Register tmp, |
| 326 | Register tmp2, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 327 | JumpTarget* slow); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 328 | |
| 329 | // Special code for typeof expressions: Unfortunately, we must |
| 330 | // be careful when loading the expression in 'typeof' |
| 331 | // expressions. We are not allowed to throw reference errors for |
| 332 | // non-existing properties of the global object, so we must make it |
| 333 | // look like an explicit property access, instead of an access |
| 334 | // through the context chain. |
| 335 | void LoadTypeofExpression(Expression* x); |
| 336 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 337 | void ToBoolean(JumpTarget* true_target, JumpTarget* false_target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 338 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 339 | // Generate code that computes a shortcutting logical operation. |
| 340 | void GenerateLogicalBooleanOperation(BinaryOperation* node); |
| 341 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 342 | void GenericBinaryOperation(Token::Value op, |
| 343 | OverwriteMode overwrite_mode, |
| 344 | int known_rhs = kUnknownIntValue); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 345 | void VirtualFrameBinaryOperation(Token::Value op, |
| 346 | OverwriteMode overwrite_mode, |
| 347 | int known_rhs = kUnknownIntValue); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 348 | void Comparison(Condition cc, |
| 349 | Expression* left, |
| 350 | Expression* right, |
| 351 | bool strict = false); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 352 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 353 | void SmiOperation(Token::Value op, |
| 354 | Handle<Object> value, |
| 355 | bool reversed, |
| 356 | OverwriteMode mode); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 357 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 358 | void VirtualFrameSmiOperation(Token::Value op, |
| 359 | Handle<Object> value, |
| 360 | bool reversed, |
| 361 | OverwriteMode mode); |
| 362 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 363 | void CallWithArguments(ZoneList<Expression*>* arguments, |
| 364 | CallFunctionFlags flags, |
| 365 | int position); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 366 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame^] | 367 | // An optimized implementation of expressions of the form |
| 368 | // x.apply(y, arguments). We call x the applicand and y the receiver. |
| 369 | // The optimization avoids allocating an arguments object if possible. |
| 370 | void CallApplyLazy(Expression* applicand, |
| 371 | Expression* receiver, |
| 372 | VariableProxy* arguments, |
| 373 | int position); |
| 374 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 375 | // Control flow |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 376 | void Branch(bool if_true, JumpTarget* target); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 377 | void CheckStack(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 378 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 379 | struct InlineRuntimeLUT { |
| 380 | void (CodeGenerator::*method)(ZoneList<Expression*>*); |
| 381 | const char* name; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 382 | int nargs; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 386 | bool CheckForInlineRuntimeCall(CallRuntime* node); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 387 | static bool PatchInlineRuntimeEntry(Handle<String> name, |
| 388 | const InlineRuntimeLUT& new_entry, |
| 389 | InlineRuntimeLUT* old_entry); |
| 390 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 391 | static Handle<Code> ComputeLazyCompile(int argc); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 392 | void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 393 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 394 | static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 395 | |
| 396 | // Declare global variables and functions in the given array of |
| 397 | // name/value pairs. |
| 398 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 399 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 400 | // Instantiate the function based on the shared function info. |
| 401 | void InstantiateFunction(Handle<SharedFunctionInfo> function_info); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 402 | |
| 403 | // Support for type checks. |
| 404 | void GenerateIsSmi(ZoneList<Expression*>* args); |
| 405 | void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 406 | void GenerateIsArray(ZoneList<Expression*>* args); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 407 | void GenerateIsRegExp(ZoneList<Expression*>* args); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 408 | void GenerateIsObject(ZoneList<Expression*>* args); |
| 409 | void GenerateIsFunction(ZoneList<Expression*>* args); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 410 | void GenerateIsUndetectableObject(ZoneList<Expression*>* args); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 411 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 412 | // Support for construct call checks. |
| 413 | void GenerateIsConstructCall(ZoneList<Expression*>* args); |
| 414 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 415 | // Support for arguments.length and arguments[?]. |
| 416 | void GenerateArgumentsLength(ZoneList<Expression*>* args); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 417 | void GenerateArguments(ZoneList<Expression*>* args); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 418 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 419 | // Support for accessing the class and value fields of an object. |
| 420 | void GenerateClassOf(ZoneList<Expression*>* args); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 421 | void GenerateValueOf(ZoneList<Expression*>* args); |
| 422 | void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 423 | |
| 424 | // Fast support for charCodeAt(n). |
| 425 | void GenerateFastCharCodeAt(ZoneList<Expression*>* args); |
| 426 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 427 | // Fast support for string.charAt(n) and string[n]. |
| 428 | void GenerateCharFromCode(ZoneList<Expression*>* args); |
| 429 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 430 | // Fast support for object equality testing. |
| 431 | void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 432 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 433 | void GenerateLog(ZoneList<Expression*>* args); |
| 434 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 435 | // Fast support for Math.random(). |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 436 | void GenerateRandomHeapNumber(ZoneList<Expression*>* args); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 437 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 438 | // Fast support for StringAdd. |
| 439 | void GenerateStringAdd(ZoneList<Expression*>* args); |
| 440 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 441 | // Fast support for SubString. |
| 442 | void GenerateSubString(ZoneList<Expression*>* args); |
| 443 | |
| 444 | // Fast support for StringCompare. |
| 445 | void GenerateStringCompare(ZoneList<Expression*>* args); |
| 446 | |
| 447 | // Support for direct calls from JavaScript to native RegExp code. |
| 448 | void GenerateRegExpExec(ZoneList<Expression*>* args); |
| 449 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 450 | void GenerateRegExpConstructResult(ZoneList<Expression*>* args); |
| 451 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 452 | // Support for fast native caches. |
| 453 | void GenerateGetFromCache(ZoneList<Expression*>* args); |
| 454 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 455 | // Fast support for number to string. |
| 456 | void GenerateNumberToString(ZoneList<Expression*>* args); |
| 457 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 458 | // Fast call for custom callbacks. |
| 459 | void GenerateCallFunction(ZoneList<Expression*>* args); |
| 460 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 461 | // Fast call to math functions. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 462 | void GenerateMathPow(ZoneList<Expression*>* args); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 463 | void GenerateMathSin(ZoneList<Expression*>* args); |
| 464 | void GenerateMathCos(ZoneList<Expression*>* args); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 465 | void GenerateMathSqrt(ZoneList<Expression*>* args); |
| 466 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 467 | // Simple condition analysis. |
| 468 | enum ConditionAnalysis { |
| 469 | ALWAYS_TRUE, |
| 470 | ALWAYS_FALSE, |
| 471 | DONT_KNOW |
| 472 | }; |
| 473 | ConditionAnalysis AnalyzeCondition(Expression* cond); |
| 474 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 475 | // Methods used to indicate which source code is generated for. Source |
| 476 | // positions are collected by the assembler and emitted with the relocation |
| 477 | // information. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 478 | void CodeForFunctionPosition(FunctionLiteral* fun); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 479 | void CodeForReturnPosition(FunctionLiteral* fun); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 480 | void CodeForStatementPosition(Statement* node); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 481 | void CodeForDoWhileConditionPosition(DoWhileStatement* stmt); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 482 | void CodeForSourcePosition(int pos); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 483 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 484 | #ifdef DEBUG |
| 485 | // True if the registers are valid for entry to a block. |
| 486 | bool HasValidEntryRegisters(); |
| 487 | #endif |
| 488 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 489 | List<DeferredCode*> deferred_; |
| 490 | |
| 491 | // Assembler |
| 492 | MacroAssembler* masm_; // to generate code |
| 493 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 494 | CompilationInfo* info_; |
| 495 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 496 | // Code generation state |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 497 | VirtualFrame* frame_; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 498 | RegisterAllocator* allocator_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 499 | Condition cc_reg_; |
| 500 | CodeGenState* state_; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 501 | int loop_nesting_; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 502 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 503 | // Jump targets |
| 504 | BreakTarget function_return_; |
| 505 | |
| 506 | // True if the function return is shadowed (ie, jumping to the target |
| 507 | // function_return_ does not jump to the true function return, but rather |
| 508 | // to some unlinking code). |
| 509 | bool function_return_is_shadowed_; |
| 510 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 511 | static InlineRuntimeLUT kInlineRuntimeLUT[]; |
| 512 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 513 | friend class VirtualFrame; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 514 | friend class JumpTarget; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 515 | friend class Reference; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 516 | friend class FastCodeGenerator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 517 | friend class FullCodeGenerator; |
| 518 | friend class FullCodeGenSyntaxChecker; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 519 | |
| 520 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 521 | }; |
| 522 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 523 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 524 | class GenericBinaryOpStub : public CodeStub { |
| 525 | public: |
| 526 | GenericBinaryOpStub(Token::Value op, |
| 527 | OverwriteMode mode, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 528 | Register lhs, |
| 529 | Register rhs, |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 530 | int constant_rhs = CodeGenerator::kUnknownIntValue) |
| 531 | : op_(op), |
| 532 | mode_(mode), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 533 | lhs_(lhs), |
| 534 | rhs_(rhs), |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 535 | constant_rhs_(constant_rhs), |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 536 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 537 | runtime_operands_type_(BinaryOpIC::DEFAULT), |
| 538 | name_(NULL) { } |
| 539 | |
| 540 | GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) |
| 541 | : op_(OpBits::decode(key)), |
| 542 | mode_(ModeBits::decode(key)), |
| 543 | lhs_(LhsRegister(RegisterBits::decode(key))), |
| 544 | rhs_(RhsRegister(RegisterBits::decode(key))), |
| 545 | constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))), |
| 546 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)), |
| 547 | runtime_operands_type_(type_info), |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 548 | name_(NULL) { } |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 549 | |
| 550 | private: |
| 551 | Token::Value op_; |
| 552 | OverwriteMode mode_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 553 | Register lhs_; |
| 554 | Register rhs_; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 555 | int constant_rhs_; |
| 556 | bool specialized_on_rhs_; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 557 | BinaryOpIC::TypeInfo runtime_operands_type_; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 558 | char* name_; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 559 | |
| 560 | static const int kMaxKnownRhs = 0x40000000; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 561 | static const int kKnownRhsKeyBits = 6; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 562 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 563 | // Minor key encoding in 17 bits. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 564 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 565 | class OpBits: public BitField<Token::Value, 2, 6> {}; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 566 | class TypeInfoBits: public BitField<int, 8, 2> {}; |
| 567 | class RegisterBits: public BitField<bool, 10, 1> {}; |
| 568 | class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {}; |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 569 | |
| 570 | Major MajorKey() { return GenericBinaryOp; } |
| 571 | int MinorKey() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 572 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 573 | (lhs_.is(r1) && rhs_.is(r0))); |
| 574 | // Encode the parameters in a unique 18 bit value. |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 575 | return OpBits::encode(op_) |
| 576 | | ModeBits::encode(mode_) |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 577 | | KnownIntBits::encode(MinorKeyForKnownInt()) |
| 578 | | TypeInfoBits::encode(runtime_operands_type_) |
| 579 | | RegisterBits::encode(lhs_.is(r0)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void Generate(MacroAssembler* masm); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 583 | void HandleNonSmiBitwiseOp(MacroAssembler* masm, Register lhs, Register rhs); |
| 584 | void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 585 | Label* not_smi, |
| 586 | Register lhs, |
| 587 | Register rhs, |
| 588 | const Builtins::JavaScript& builtin); |
| 589 | void GenerateTypeTransition(MacroAssembler* masm); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 590 | |
| 591 | static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) { |
| 592 | if (constant_rhs == CodeGenerator::kUnknownIntValue) return false; |
| 593 | if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3; |
| 594 | if (op == Token::MOD) { |
| 595 | if (constant_rhs <= 1) return false; |
| 596 | if (constant_rhs <= 10) return true; |
| 597 | if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true; |
| 598 | return false; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | int MinorKeyForKnownInt() { |
| 604 | if (!specialized_on_rhs_) return 0; |
| 605 | if (constant_rhs_ <= 10) return constant_rhs_ + 1; |
| 606 | ASSERT(IsPowerOf2(constant_rhs_)); |
| 607 | int key = 12; |
| 608 | int d = constant_rhs_; |
| 609 | while ((d & 1) == 0) { |
| 610 | key++; |
| 611 | d >>= 1; |
| 612 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 613 | ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits)); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 614 | return key; |
| 615 | } |
| 616 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 617 | int KnownBitsForMinorKey(int key) { |
| 618 | if (!key) return 0; |
| 619 | if (key <= 11) return key - 1; |
| 620 | int d = 1; |
| 621 | while (key != 12) { |
| 622 | key--; |
| 623 | d <<= 1; |
| 624 | } |
| 625 | return d; |
| 626 | } |
| 627 | |
| 628 | Register LhsRegister(bool lhs_is_r0) { |
| 629 | return lhs_is_r0 ? r0 : r1; |
| 630 | } |
| 631 | |
| 632 | Register RhsRegister(bool lhs_is_r0) { |
| 633 | return lhs_is_r0 ? r1 : r0; |
| 634 | } |
| 635 | |
| 636 | bool ShouldGenerateSmiCode() { |
| 637 | return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) && |
| 638 | runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS && |
| 639 | runtime_operands_type_ != BinaryOpIC::STRINGS; |
| 640 | } |
| 641 | |
| 642 | bool ShouldGenerateFPCode() { |
| 643 | return runtime_operands_type_ != BinaryOpIC::STRINGS; |
| 644 | } |
| 645 | |
| 646 | virtual int GetCodeKind() { return Code::BINARY_OP_IC; } |
| 647 | |
| 648 | virtual InlineCacheState GetICState() { |
| 649 | return BinaryOpIC::ToState(runtime_operands_type_); |
| 650 | } |
| 651 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 652 | const char* GetName(); |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 653 | |
| 654 | #ifdef DEBUG |
| 655 | void Print() { |
| 656 | if (!specialized_on_rhs_) { |
| 657 | PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); |
| 658 | } else { |
| 659 | PrintF("GenericBinaryOpStub (%s by %d)\n", |
| 660 | Token::String(op_), |
| 661 | constant_rhs_); |
| 662 | } |
| 663 | } |
| 664 | #endif |
| 665 | }; |
| 666 | |
| 667 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 668 | class StringStubBase: public CodeStub { |
| 669 | public: |
| 670 | // Generate code for copying characters using a simple loop. This should only |
| 671 | // be used in places where the number of characters is small and the |
| 672 | // additional setup and checking in GenerateCopyCharactersLong adds too much |
| 673 | // overhead. Copying of overlapping regions is not supported. |
| 674 | // Dest register ends at the position after the last character written. |
| 675 | void GenerateCopyCharacters(MacroAssembler* masm, |
| 676 | Register dest, |
| 677 | Register src, |
| 678 | Register count, |
| 679 | Register scratch, |
| 680 | bool ascii); |
| 681 | |
| 682 | // Generate code for copying a large number of characters. This function |
| 683 | // is allowed to spend extra time setting up conditions to make copying |
| 684 | // faster. Copying of overlapping regions is not supported. |
| 685 | // Dest register ends at the position after the last character written. |
| 686 | void GenerateCopyCharactersLong(MacroAssembler* masm, |
| 687 | Register dest, |
| 688 | Register src, |
| 689 | Register count, |
| 690 | Register scratch1, |
| 691 | Register scratch2, |
| 692 | Register scratch3, |
| 693 | Register scratch4, |
| 694 | Register scratch5, |
| 695 | int flags); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 696 | |
| 697 | |
| 698 | // Probe the symbol table for a two character string. If the string is |
| 699 | // not found by probing a jump to the label not_found is performed. This jump |
| 700 | // does not guarantee that the string is not in the symbol table. If the |
| 701 | // string is found the code falls through with the string in register r0. |
| 702 | // Contents of both c1 and c2 registers are modified. At the exit c1 is |
| 703 | // guaranteed to contain halfword with low and high bytes equal to |
| 704 | // initial contents of c1 and c2 respectively. |
| 705 | void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 706 | Register c1, |
| 707 | Register c2, |
| 708 | Register scratch1, |
| 709 | Register scratch2, |
| 710 | Register scratch3, |
| 711 | Register scratch4, |
| 712 | Register scratch5, |
| 713 | Label* not_found); |
| 714 | |
| 715 | // Generate string hash. |
| 716 | void GenerateHashInit(MacroAssembler* masm, |
| 717 | Register hash, |
| 718 | Register character); |
| 719 | |
| 720 | void GenerateHashAddCharacter(MacroAssembler* masm, |
| 721 | Register hash, |
| 722 | Register character); |
| 723 | |
| 724 | void GenerateHashGetHash(MacroAssembler* masm, |
| 725 | Register hash); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | |
| 729 | // Flag that indicates how to generate code for the stub StringAddStub. |
| 730 | enum StringAddFlags { |
| 731 | NO_STRING_ADD_FLAGS = 0, |
| 732 | NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub. |
| 733 | }; |
| 734 | |
| 735 | |
| 736 | class StringAddStub: public StringStubBase { |
| 737 | public: |
| 738 | explicit StringAddStub(StringAddFlags flags) { |
| 739 | string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0); |
| 740 | } |
| 741 | |
| 742 | private: |
| 743 | Major MajorKey() { return StringAdd; } |
| 744 | int MinorKey() { return string_check_ ? 0 : 1; } |
| 745 | |
| 746 | void Generate(MacroAssembler* masm); |
| 747 | |
| 748 | // Should the stub check whether arguments are strings? |
| 749 | bool string_check_; |
| 750 | }; |
| 751 | |
| 752 | |
| 753 | class SubStringStub: public StringStubBase { |
| 754 | public: |
| 755 | SubStringStub() {} |
| 756 | |
| 757 | private: |
| 758 | Major MajorKey() { return SubString; } |
| 759 | int MinorKey() { return 0; } |
| 760 | |
| 761 | void Generate(MacroAssembler* masm); |
| 762 | }; |
| 763 | |
| 764 | |
| 765 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 766 | class StringCompareStub: public CodeStub { |
| 767 | public: |
| 768 | StringCompareStub() { } |
| 769 | |
| 770 | // Compare two flat ASCII strings and returns result in r0. |
| 771 | // Does not use the stack. |
| 772 | static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 773 | Register left, |
| 774 | Register right, |
| 775 | Register scratch1, |
| 776 | Register scratch2, |
| 777 | Register scratch3, |
| 778 | Register scratch4); |
| 779 | |
| 780 | private: |
| 781 | Major MajorKey() { return StringCompare; } |
| 782 | int MinorKey() { return 0; } |
| 783 | |
| 784 | void Generate(MacroAssembler* masm); |
| 785 | }; |
| 786 | |
| 787 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 788 | // This stub can convert a signed int32 to a heap number (double). It does |
| 789 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 790 | // so you don't have to set up the frame. |
| 791 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 792 | public: |
| 793 | WriteInt32ToHeapNumberStub(Register the_int, |
| 794 | Register the_heap_number, |
| 795 | Register scratch) |
| 796 | : the_int_(the_int), |
| 797 | the_heap_number_(the_heap_number), |
| 798 | scratch_(scratch) { } |
| 799 | |
| 800 | private: |
| 801 | Register the_int_; |
| 802 | Register the_heap_number_; |
| 803 | Register scratch_; |
| 804 | |
| 805 | // Minor key encoding in 16 bits. |
| 806 | class IntRegisterBits: public BitField<int, 0, 4> {}; |
| 807 | class HeapNumberRegisterBits: public BitField<int, 4, 4> {}; |
| 808 | class ScratchRegisterBits: public BitField<int, 8, 4> {}; |
| 809 | |
| 810 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 811 | int MinorKey() { |
| 812 | // Encode the parameters in a unique 16 bit value. |
| 813 | return IntRegisterBits::encode(the_int_.code()) |
| 814 | | HeapNumberRegisterBits::encode(the_heap_number_.code()) |
| 815 | | ScratchRegisterBits::encode(scratch_.code()); |
| 816 | } |
| 817 | |
| 818 | void Generate(MacroAssembler* masm); |
| 819 | |
| 820 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 821 | |
| 822 | #ifdef DEBUG |
| 823 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 824 | #endif |
| 825 | }; |
| 826 | |
| 827 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 828 | class NumberToStringStub: public CodeStub { |
| 829 | public: |
| 830 | NumberToStringStub() { } |
| 831 | |
| 832 | // Generate code to do a lookup in the number string cache. If the number in |
| 833 | // the register object is found in the cache the generated code falls through |
| 834 | // with the result in the result register. The object and the result register |
| 835 | // can be the same. If the number is not found in the cache the code jumps to |
| 836 | // the label not_found with only the content of register object unchanged. |
| 837 | static void GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 838 | Register object, |
| 839 | Register result, |
| 840 | Register scratch1, |
| 841 | Register scratch2, |
| 842 | bool object_is_smi, |
| 843 | Label* not_found); |
| 844 | |
| 845 | private: |
| 846 | Major MajorKey() { return NumberToString; } |
| 847 | int MinorKey() { return 0; } |
| 848 | |
| 849 | void Generate(MacroAssembler* masm); |
| 850 | |
| 851 | const char* GetName() { return "NumberToStringStub"; } |
| 852 | |
| 853 | #ifdef DEBUG |
| 854 | void Print() { |
| 855 | PrintF("NumberToStringStub\n"); |
| 856 | } |
| 857 | #endif |
| 858 | }; |
| 859 | |
| 860 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 861 | } } // namespace v8::internal |
| 862 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 863 | #endif // V8_ARM_CODEGEN_ARM_H_ |