Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
| 28 | #ifndef V8_ARM_CODEGEN_ARM_H_ |
| 29 | #define V8_ARM_CODEGEN_ARM_H_ |
| 30 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 31 | #include "ic-inl.h" |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 32 | #include "ast.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | // Forward declarations |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 38 | class CompilationInfo; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | class DeferredCode; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 40 | class JumpTarget; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | class RegisterAllocator; |
| 42 | class RegisterFile; |
| 43 | |
| 44 | enum InitState { CONST_INIT, NOT_CONST_INIT }; |
| 45 | enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 46 | enum GenerateInlineSmi { DONT_GENERATE_INLINE_SMI, GENERATE_INLINE_SMI }; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 47 | enum WriteBarrierCharacter { UNLIKELY_SMI, LIKELY_SMI, NEVER_NEWSPACE }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | // ------------------------------------------------------------------------- |
| 51 | // Reference support |
| 52 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 53 | // A reference is a C++ stack-allocated object that puts a |
| 54 | // reference on the virtual frame. The reference may be consumed |
| 55 | // by GetValue, TakeValue, SetValue, and Codegen::UnloadReference. |
| 56 | // When the lifetime (scope) of a valid reference ends, it must have |
| 57 | // been consumed, and be in state UNLOADED. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | class Reference BASE_EMBEDDED { |
| 59 | public: |
| 60 | // The values of the types is important, see size(). |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 61 | enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 }; |
| 62 | Reference(CodeGenerator* cgen, |
| 63 | Expression* expression, |
| 64 | bool persist_after_get = false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | ~Reference(); |
| 66 | |
| 67 | Expression* expression() const { return expression_; } |
| 68 | Type type() const { return type_; } |
| 69 | void set_type(Type value) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 70 | ASSERT_EQ(ILLEGAL, type_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | type_ = value; |
| 72 | } |
| 73 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 74 | void set_unloaded() { |
| 75 | ASSERT_NE(ILLEGAL, type_); |
| 76 | ASSERT_NE(UNLOADED, type_); |
| 77 | type_ = UNLOADED; |
| 78 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | // The size the reference takes up on the stack. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 80 | int size() const { |
| 81 | return (type_ < SLOT) ? 0 : type_; |
| 82 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | |
| 84 | bool is_illegal() const { return type_ == ILLEGAL; } |
| 85 | bool is_slot() const { return type_ == SLOT; } |
| 86 | bool is_property() const { return type_ == NAMED || type_ == KEYED; } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 87 | bool is_unloaded() const { return type_ == UNLOADED; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
| 89 | // Return the name. Only valid for named property references. |
| 90 | Handle<String> GetName(); |
| 91 | |
| 92 | // Generate code to push the value of the reference on top of the |
| 93 | // expression stack. The reference is expected to be already on top of |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 94 | // the expression stack, and it is consumed by the call unless the |
| 95 | // reference is for a compound assignment. |
| 96 | // If the reference is not consumed, it is left in place under its value. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 97 | void GetValue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | // Generate code to store the value on top of the expression stack in the |
| 100 | // reference. The reference is expected to be immediately below the value |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 101 | // on the expression stack. The value is stored in the location specified |
| 102 | // by the reference, and is left on top of the stack, after the reference |
| 103 | // is popped from beneath it (unloaded). |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 104 | void SetValue(InitState init_state, WriteBarrierCharacter wb); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 106 | // This is in preparation for something that uses the reference on the stack. |
| 107 | // If we need this reference afterwards get then dup it now. Otherwise mark |
| 108 | // it as used. |
| 109 | inline void DupIfPersist(); |
| 110 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | private: |
| 112 | CodeGenerator* cgen_; |
| 113 | Expression* expression_; |
| 114 | Type type_; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 115 | // Keep the reference on the stack after get, so it can be used by set later. |
| 116 | bool persist_after_get_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | |
| 120 | // ------------------------------------------------------------------------- |
| 121 | // Code generation state |
| 122 | |
| 123 | // The state is passed down the AST by the code generator (and back up, in |
| 124 | // the form of the state of the label pair). It is threaded through the |
| 125 | // call stack. Constructing a state implicitly pushes it on the owning code |
| 126 | // generator's stack of states, and destroying one implicitly pops it. |
| 127 | |
| 128 | class CodeGenState BASE_EMBEDDED { |
| 129 | public: |
| 130 | // Create an initial code generator state. Destroying the initial state |
| 131 | // leaves the code generator with a NULL state. |
| 132 | explicit CodeGenState(CodeGenerator* owner); |
| 133 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | // Destroy a code generator state and restore the owning code generator's |
| 135 | // previous state. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 136 | virtual ~CodeGenState(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 138 | virtual JumpTarget* true_target() const { return NULL; } |
| 139 | virtual JumpTarget* false_target() const { return NULL; } |
| 140 | |
| 141 | protected: |
| 142 | inline CodeGenerator* owner() { return owner_; } |
| 143 | inline CodeGenState* previous() const { return previous_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | CodeGenerator* owner_; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 147 | CodeGenState* previous_; |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | class ConditionCodeGenState : public CodeGenState { |
| 152 | public: |
| 153 | // Create a code generator state based on a code generator's current |
| 154 | // state. The new state has its own pair of branch labels. |
| 155 | ConditionCodeGenState(CodeGenerator* owner, |
| 156 | JumpTarget* true_target, |
| 157 | JumpTarget* false_target); |
| 158 | |
| 159 | virtual JumpTarget* true_target() const { return true_target_; } |
| 160 | virtual JumpTarget* false_target() const { return false_target_; } |
| 161 | |
| 162 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | JumpTarget* true_target_; |
| 164 | JumpTarget* false_target_; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | |
| 168 | class TypeInfoCodeGenState : public CodeGenState { |
| 169 | public: |
| 170 | TypeInfoCodeGenState(CodeGenerator* owner, |
| 171 | Slot* slot_number, |
| 172 | TypeInfo info); |
| 173 | ~TypeInfoCodeGenState(); |
| 174 | |
| 175 | virtual JumpTarget* true_target() const { return previous()->true_target(); } |
| 176 | virtual JumpTarget* false_target() const { |
| 177 | return previous()->false_target(); |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | Slot* slot_; |
| 182 | TypeInfo old_type_info_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | |
| 186 | // ------------------------------------------------------------------------- |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 187 | // Arguments allocation mode |
| 188 | |
| 189 | enum ArgumentsAllocationMode { |
| 190 | NO_ARGUMENTS_ALLOCATION, |
| 191 | EAGER_ARGUMENTS_ALLOCATION, |
| 192 | LAZY_ARGUMENTS_ALLOCATION |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | // Different nop operations are used by the code generator to detect certain |
| 197 | // states of the generated code. |
| 198 | enum NopMarkerTypes { |
| 199 | NON_MARKING_NOP = 0, |
| 200 | PROPERTY_ACCESS_INLINED |
| 201 | }; |
| 202 | |
| 203 | |
| 204 | // ------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | // CodeGenerator |
| 206 | |
| 207 | class CodeGenerator: public AstVisitor { |
| 208 | public: |
| 209 | // Takes a function literal, generates code for it. This function should only |
| 210 | // be called by compiler.cc. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 211 | static Handle<Code> MakeCode(CompilationInfo* info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 213 | // Printing of AST, etc. as requested by flags. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 214 | static void MakeCodePrologue(CompilationInfo* info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 215 | |
| 216 | // Allocate and install the code. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 217 | static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 218 | Code::Flags flags, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 219 | CompilationInfo* info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 220 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 222 | static bool ShouldGenerateLog(Expression* type); |
| 223 | #endif |
| 224 | |
| 225 | static void SetFunctionInfo(Handle<JSFunction> fun, |
| 226 | FunctionLiteral* lit, |
| 227 | bool is_toplevel, |
| 228 | Handle<Script> script); |
| 229 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 230 | static bool RecordPositions(MacroAssembler* masm, |
| 231 | int pos, |
| 232 | bool right_here = false); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 233 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | // Accessors |
| 235 | MacroAssembler* masm() { return masm_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | VirtualFrame* frame() const { return frame_; } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 237 | inline Handle<Script> script(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | |
| 239 | bool has_valid_frame() const { return frame_ != NULL; } |
| 240 | |
| 241 | // Set the virtual frame to be new_frame, with non-frame register |
| 242 | // reference counts given by non_frame_registers. The non-frame |
| 243 | // register reference counts of the old frame are returned in |
| 244 | // non_frame_registers. |
| 245 | void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers); |
| 246 | |
| 247 | void DeleteFrame(); |
| 248 | |
| 249 | RegisterAllocator* allocator() const { return allocator_; } |
| 250 | |
| 251 | CodeGenState* state() { return state_; } |
| 252 | void set_state(CodeGenState* state) { state_ = state; } |
| 253 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 254 | TypeInfo type_info(Slot* slot) { |
| 255 | int index = NumberOfSlot(slot); |
| 256 | if (index == kInvalidSlotNumber) return TypeInfo::Unknown(); |
| 257 | return (*type_info_)[index]; |
| 258 | } |
| 259 | |
| 260 | TypeInfo set_type_info(Slot* slot, TypeInfo info) { |
| 261 | int index = NumberOfSlot(slot); |
| 262 | ASSERT(index >= kInvalidSlotNumber); |
| 263 | if (index != kInvalidSlotNumber) { |
| 264 | TypeInfo previous_value = (*type_info_)[index]; |
| 265 | (*type_info_)[index] = info; |
| 266 | return previous_value; |
| 267 | } |
| 268 | return TypeInfo::Unknown(); |
| 269 | } |
| 270 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | void AddDeferred(DeferredCode* code) { deferred_.Add(code); } |
| 272 | |
| 273 | static const int kUnknownIntValue = -1; |
| 274 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 275 | // If the name is an inline runtime function call return the number of |
| 276 | // expected arguments. Otherwise return -1. |
| 277 | static int InlineRuntimeCallArgumentsCount(Handle<String> name); |
| 278 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 279 | // Constants related to patching of inlined load/store. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 280 | static int GetInlinedKeyedLoadInstructionsAfterPatch() { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 281 | return FLAG_debug_code ? 32 : 13; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 282 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 283 | static const int kInlinedKeyedStoreInstructionsAfterPatch = 5; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 284 | static int GetInlinedNamedStoreInstructionsAfterPatch() { |
| 285 | ASSERT(inlined_write_barrier_size_ != -1); |
| 286 | return inlined_write_barrier_size_ + 4; |
| 287 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 288 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 289 | static MemOperand ContextOperand(Register context, int index) { |
| 290 | return MemOperand(context, Context::SlotOffset(index)); |
| 291 | } |
| 292 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | private: |
| 294 | // Construction/Destruction |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 295 | explicit CodeGenerator(MacroAssembler* masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | |
| 297 | // Accessors |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 298 | inline bool is_eval(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 299 | inline Scope* scope(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | |
| 301 | // Generating deferred code. |
| 302 | void ProcessDeferred(); |
| 303 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 304 | static const int kInvalidSlotNumber = -1; |
| 305 | |
| 306 | int NumberOfSlot(Slot* slot); |
| 307 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 308 | // State |
| 309 | bool has_cc() const { return cc_reg_ != al; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | JumpTarget* true_target() const { return state_->true_target(); } |
| 311 | JumpTarget* false_target() const { return state_->false_target(); } |
| 312 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 313 | // Track loop nesting level. |
| 314 | int loop_nesting() const { return loop_nesting_; } |
| 315 | void IncrementLoopNesting() { loop_nesting_++; } |
| 316 | void DecrementLoopNesting() { loop_nesting_--; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | |
| 318 | // Node visitors. |
| 319 | void VisitStatements(ZoneList<Statement*>* statements); |
| 320 | |
| 321 | #define DEF_VISIT(type) \ |
| 322 | void Visit##type(type* node); |
| 323 | AST_NODE_LIST(DEF_VISIT) |
| 324 | #undef DEF_VISIT |
| 325 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | // Main code generation function |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 327 | void Generate(CompilationInfo* info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 329 | // Generate the return sequence code. Should be called no more than |
| 330 | // once per compiled function, immediately after binding the return |
| 331 | // target (which can not be done more than once). The return value should |
| 332 | // be in r0. |
| 333 | void GenerateReturnSequence(); |
| 334 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 335 | // Returns the arguments allocation mode. |
| 336 | ArgumentsAllocationMode ArgumentsMode(); |
| 337 | |
| 338 | // Store the arguments object and allocate it if necessary. |
| 339 | void StoreArgumentsObject(bool initial); |
| 340 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 341 | // The following are used by class Reference. |
| 342 | void LoadReference(Reference* ref); |
| 343 | void UnloadReference(Reference* ref); |
| 344 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | MemOperand SlotOperand(Slot* slot, Register tmp); |
| 346 | |
| 347 | MemOperand ContextSlotOperandCheckExtensions(Slot* slot, |
| 348 | Register tmp, |
| 349 | Register tmp2, |
| 350 | JumpTarget* slow); |
| 351 | |
| 352 | // Expressions |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 353 | static MemOperand GlobalObject() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | return ContextOperand(cp, Context::GLOBAL_INDEX); |
| 355 | } |
| 356 | |
| 357 | void LoadCondition(Expression* x, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 358 | JumpTarget* true_target, |
| 359 | JumpTarget* false_target, |
| 360 | bool force_cc); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 361 | void Load(Expression* expr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | void LoadGlobal(); |
| 363 | void LoadGlobalReceiver(Register scratch); |
| 364 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | // Read a value from a slot and leave it on top of the expression stack. |
| 366 | void LoadFromSlot(Slot* slot, TypeofState typeof_state); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 367 | void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 368 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 369 | // Store the value on top of the stack to a slot. |
| 370 | void StoreToSlot(Slot* slot, InitState init_state); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 371 | |
| 372 | // Support for compiling assignment expressions. |
| 373 | void EmitSlotAssignment(Assignment* node); |
| 374 | void EmitNamedPropertyAssignment(Assignment* node); |
| 375 | void EmitKeyedPropertyAssignment(Assignment* node); |
| 376 | |
| 377 | // Load a named property, returning it in r0. The receiver is passed on the |
| 378 | // stack, and remains there. |
| 379 | void EmitNamedLoad(Handle<String> name, bool is_contextual); |
| 380 | |
| 381 | // Store to a named property. If the store is contextual, value is passed on |
| 382 | // the frame and consumed. Otherwise, receiver and value are passed on the |
| 383 | // frame and consumed. The result is returned in r0. |
| 384 | void EmitNamedStore(Handle<String> name, bool is_contextual); |
| 385 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 386 | // Load a keyed property, leaving it in r0. The receiver and key are |
| 387 | // passed on the stack, and remain there. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 388 | void EmitKeyedLoad(); |
| 389 | |
| 390 | // Store a keyed property. Key and receiver are on the stack and the value is |
| 391 | // in r0. Result is returned in r0. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 392 | void EmitKeyedStore(StaticType* key_type, WriteBarrierCharacter wb_info); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 393 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | void LoadFromGlobalSlotCheckExtensions(Slot* slot, |
| 395 | TypeofState typeof_state, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | JumpTarget* slow); |
| 397 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 398 | // Support for loading from local/global variables and arguments |
| 399 | // whose location is known unless they are shadowed by |
| 400 | // eval-introduced bindings. Generates no code for unsupported slot |
| 401 | // types and therefore expects to fall through to the slow jump target. |
| 402 | void EmitDynamicLoadFromSlotFastCase(Slot* slot, |
| 403 | TypeofState typeof_state, |
| 404 | JumpTarget* slow, |
| 405 | JumpTarget* done); |
| 406 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | // Special code for typeof expressions: Unfortunately, we must |
| 408 | // be careful when loading the expression in 'typeof' |
| 409 | // expressions. We are not allowed to throw reference errors for |
| 410 | // non-existing properties of the global object, so we must make it |
| 411 | // look like an explicit property access, instead of an access |
| 412 | // through the context chain. |
| 413 | void LoadTypeofExpression(Expression* x); |
| 414 | |
| 415 | void ToBoolean(JumpTarget* true_target, JumpTarget* false_target); |
| 416 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 417 | // Generate code that computes a shortcutting logical operation. |
| 418 | void GenerateLogicalBooleanOperation(BinaryOperation* node); |
| 419 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | void GenericBinaryOperation(Token::Value op, |
| 421 | OverwriteMode overwrite_mode, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 422 | GenerateInlineSmi inline_smi, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 423 | int known_rhs = kUnknownIntValue); |
| 424 | void Comparison(Condition cc, |
| 425 | Expression* left, |
| 426 | Expression* right, |
| 427 | bool strict = false); |
| 428 | |
| 429 | void SmiOperation(Token::Value op, |
| 430 | Handle<Object> value, |
| 431 | bool reversed, |
| 432 | OverwriteMode mode); |
| 433 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 434 | void CallWithArguments(ZoneList<Expression*>* arguments, |
| 435 | CallFunctionFlags flags, |
| 436 | int position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 438 | // An optimized implementation of expressions of the form |
| 439 | // x.apply(y, arguments). We call x the applicand and y the receiver. |
| 440 | // The optimization avoids allocating an arguments object if possible. |
| 441 | void CallApplyLazy(Expression* applicand, |
| 442 | Expression* receiver, |
| 443 | VariableProxy* arguments, |
| 444 | int position); |
| 445 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 446 | // Control flow |
| 447 | void Branch(bool if_true, JumpTarget* target); |
| 448 | void CheckStack(); |
| 449 | |
| 450 | struct InlineRuntimeLUT { |
| 451 | void (CodeGenerator::*method)(ZoneList<Expression*>*); |
| 452 | const char* name; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 453 | int nargs; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name); |
| 457 | bool CheckForInlineRuntimeCall(CallRuntime* node); |
| 458 | static bool PatchInlineRuntimeEntry(Handle<String> name, |
| 459 | const InlineRuntimeLUT& new_entry, |
| 460 | InlineRuntimeLUT* old_entry); |
| 461 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 462 | static Handle<Code> ComputeLazyCompile(int argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | void ProcessDeclarations(ZoneList<Declaration*>* declarations); |
| 464 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 465 | static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 466 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 467 | static Handle<Code> ComputeKeyedCallInitialize(int argc, InLoopFlag in_loop); |
| 468 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 469 | // Declare global variables and functions in the given array of |
| 470 | // name/value pairs. |
| 471 | void DeclareGlobals(Handle<FixedArray> pairs); |
| 472 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 473 | // Instantiate the function based on the shared function info. |
| 474 | void InstantiateFunction(Handle<SharedFunctionInfo> function_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 475 | |
| 476 | // Support for type checks. |
| 477 | void GenerateIsSmi(ZoneList<Expression*>* args); |
| 478 | void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args); |
| 479 | void GenerateIsArray(ZoneList<Expression*>* args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 480 | void GenerateIsRegExp(ZoneList<Expression*>* args); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 481 | void GenerateIsObject(ZoneList<Expression*>* args); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 482 | void GenerateIsSpecObject(ZoneList<Expression*>* args); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 483 | void GenerateIsFunction(ZoneList<Expression*>* args); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 484 | void GenerateIsUndetectableObject(ZoneList<Expression*>* args); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 485 | void GenerateIsStringWrapperSafeForDefaultValueOf( |
| 486 | ZoneList<Expression*>* args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 487 | |
| 488 | // Support for construct call checks. |
| 489 | void GenerateIsConstructCall(ZoneList<Expression*>* args); |
| 490 | |
| 491 | // Support for arguments.length and arguments[?]. |
| 492 | void GenerateArgumentsLength(ZoneList<Expression*>* args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 493 | void GenerateArguments(ZoneList<Expression*>* args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 494 | |
| 495 | // Support for accessing the class and value fields of an object. |
| 496 | void GenerateClassOf(ZoneList<Expression*>* args); |
| 497 | void GenerateValueOf(ZoneList<Expression*>* args); |
| 498 | void GenerateSetValueOf(ZoneList<Expression*>* args); |
| 499 | |
| 500 | // Fast support for charCodeAt(n). |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 501 | void GenerateStringCharCodeAt(ZoneList<Expression*>* args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 503 | // Fast support for string.charAt(n) and string[n]. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 504 | void GenerateStringCharFromCode(ZoneList<Expression*>* args); |
| 505 | |
| 506 | // Fast support for string.charAt(n) and string[n]. |
| 507 | void GenerateStringCharAt(ZoneList<Expression*>* args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 508 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 509 | // Fast support for object equality testing. |
| 510 | void GenerateObjectEquals(ZoneList<Expression*>* args); |
| 511 | |
| 512 | void GenerateLog(ZoneList<Expression*>* args); |
| 513 | |
| 514 | // Fast support for Math.random(). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 515 | void GenerateRandomHeapNumber(ZoneList<Expression*>* args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 516 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 517 | // Fast support for StringAdd. |
| 518 | void GenerateStringAdd(ZoneList<Expression*>* args); |
| 519 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 520 | // Fast support for SubString. |
| 521 | void GenerateSubString(ZoneList<Expression*>* args); |
| 522 | |
| 523 | // Fast support for StringCompare. |
| 524 | void GenerateStringCompare(ZoneList<Expression*>* args); |
| 525 | |
| 526 | // Support for direct calls from JavaScript to native RegExp code. |
| 527 | void GenerateRegExpExec(ZoneList<Expression*>* args); |
| 528 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 529 | void GenerateRegExpConstructResult(ZoneList<Expression*>* args); |
| 530 | |
| 531 | // Support for fast native caches. |
| 532 | void GenerateGetFromCache(ZoneList<Expression*>* args); |
| 533 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 534 | // Fast support for number to string. |
| 535 | void GenerateNumberToString(ZoneList<Expression*>* args); |
| 536 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 537 | // Fast swapping of elements. |
| 538 | void GenerateSwapElements(ZoneList<Expression*>* args); |
| 539 | |
| 540 | // Fast call for custom callbacks. |
| 541 | void GenerateCallFunction(ZoneList<Expression*>* args); |
| 542 | |
| 543 | // Fast call to math functions. |
| 544 | void GenerateMathPow(ZoneList<Expression*>* args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 545 | void GenerateMathSin(ZoneList<Expression*>* args); |
| 546 | void GenerateMathCos(ZoneList<Expression*>* args); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 547 | void GenerateMathSqrt(ZoneList<Expression*>* args); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 548 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 549 | void GenerateIsRegExpEquivalent(ZoneList<Expression*>* args); |
| 550 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 551 | // Simple condition analysis. |
| 552 | enum ConditionAnalysis { |
| 553 | ALWAYS_TRUE, |
| 554 | ALWAYS_FALSE, |
| 555 | DONT_KNOW |
| 556 | }; |
| 557 | ConditionAnalysis AnalyzeCondition(Expression* cond); |
| 558 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 559 | // Methods used to indicate which source code is generated for. Source |
| 560 | // positions are collected by the assembler and emitted with the relocation |
| 561 | // information. |
| 562 | void CodeForFunctionPosition(FunctionLiteral* fun); |
| 563 | void CodeForReturnPosition(FunctionLiteral* fun); |
| 564 | void CodeForStatementPosition(Statement* node); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 565 | void CodeForDoWhileConditionPosition(DoWhileStatement* stmt); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 566 | void CodeForSourcePosition(int pos); |
| 567 | |
| 568 | #ifdef DEBUG |
| 569 | // True if the registers are valid for entry to a block. |
| 570 | bool HasValidEntryRegisters(); |
| 571 | #endif |
| 572 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 573 | List<DeferredCode*> deferred_; |
| 574 | |
| 575 | // Assembler |
| 576 | MacroAssembler* masm_; // to generate code |
| 577 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 578 | CompilationInfo* info_; |
| 579 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 580 | // Code generation state |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 581 | VirtualFrame* frame_; |
| 582 | RegisterAllocator* allocator_; |
| 583 | Condition cc_reg_; |
| 584 | CodeGenState* state_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 585 | int loop_nesting_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 586 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 587 | Vector<TypeInfo>* type_info_; |
| 588 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 589 | // Jump targets |
| 590 | BreakTarget function_return_; |
| 591 | |
| 592 | // True if the function return is shadowed (ie, jumping to the target |
| 593 | // function_return_ does not jump to the true function return, but rather |
| 594 | // to some unlinking code). |
| 595 | bool function_return_is_shadowed_; |
| 596 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 597 | // Size of inlined write barriers generated by EmitNamedStore. |
| 598 | static int inlined_write_barrier_size_; |
| 599 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 600 | static InlineRuntimeLUT kInlineRuntimeLUT[]; |
| 601 | |
| 602 | friend class VirtualFrame; |
| 603 | friend class JumpTarget; |
| 604 | friend class Reference; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 605 | friend class FastCodeGenerator; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 606 | friend class FullCodeGenerator; |
| 607 | friend class FullCodeGenSyntaxChecker; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 608 | |
| 609 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 610 | }; |
| 611 | |
| 612 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 613 | // Compute a transcendental math function natively, or call the |
| 614 | // TranscendentalCache runtime function. |
| 615 | class TranscendentalCacheStub: public CodeStub { |
| 616 | public: |
| 617 | explicit TranscendentalCacheStub(TranscendentalCache::Type type) |
| 618 | : type_(type) {} |
| 619 | void Generate(MacroAssembler* masm); |
| 620 | private: |
| 621 | TranscendentalCache::Type type_; |
| 622 | Major MajorKey() { return TranscendentalCache; } |
| 623 | int MinorKey() { return type_; } |
| 624 | Runtime::FunctionId RuntimeFunction(); |
| 625 | }; |
| 626 | |
| 627 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 628 | class ToBooleanStub: public CodeStub { |
| 629 | public: |
| 630 | explicit ToBooleanStub(Register tos) : tos_(tos) { } |
| 631 | |
| 632 | void Generate(MacroAssembler* masm); |
| 633 | |
| 634 | private: |
| 635 | Register tos_; |
| 636 | Major MajorKey() { return ToBoolean; } |
| 637 | int MinorKey() { return tos_.code(); } |
| 638 | }; |
| 639 | |
| 640 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 641 | class GenericBinaryOpStub : public CodeStub { |
| 642 | public: |
| 643 | GenericBinaryOpStub(Token::Value op, |
| 644 | OverwriteMode mode, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 645 | Register lhs, |
| 646 | Register rhs, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 647 | int constant_rhs = CodeGenerator::kUnknownIntValue) |
| 648 | : op_(op), |
| 649 | mode_(mode), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 650 | lhs_(lhs), |
| 651 | rhs_(rhs), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 652 | constant_rhs_(constant_rhs), |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 653 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 654 | runtime_operands_type_(BinaryOpIC::DEFAULT), |
| 655 | name_(NULL) { } |
| 656 | |
| 657 | GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) |
| 658 | : op_(OpBits::decode(key)), |
| 659 | mode_(ModeBits::decode(key)), |
| 660 | lhs_(LhsRegister(RegisterBits::decode(key))), |
| 661 | rhs_(RhsRegister(RegisterBits::decode(key))), |
| 662 | constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))), |
| 663 | specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)), |
| 664 | runtime_operands_type_(type_info), |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 665 | name_(NULL) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 666 | |
| 667 | private: |
| 668 | Token::Value op_; |
| 669 | OverwriteMode mode_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 670 | Register lhs_; |
| 671 | Register rhs_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 672 | int constant_rhs_; |
| 673 | bool specialized_on_rhs_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 674 | BinaryOpIC::TypeInfo runtime_operands_type_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 675 | char* name_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 676 | |
| 677 | static const int kMaxKnownRhs = 0x40000000; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 678 | static const int kKnownRhsKeyBits = 6; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 679 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 680 | // Minor key encoding in 17 bits. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 681 | class ModeBits: public BitField<OverwriteMode, 0, 2> {}; |
| 682 | class OpBits: public BitField<Token::Value, 2, 6> {}; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 683 | class TypeInfoBits: public BitField<int, 8, 2> {}; |
| 684 | class RegisterBits: public BitField<bool, 10, 1> {}; |
| 685 | class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 686 | |
| 687 | Major MajorKey() { return GenericBinaryOp; } |
| 688 | int MinorKey() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 689 | ASSERT((lhs_.is(r0) && rhs_.is(r1)) || |
| 690 | (lhs_.is(r1) && rhs_.is(r0))); |
| 691 | // Encode the parameters in a unique 18 bit value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 692 | return OpBits::encode(op_) |
| 693 | | ModeBits::encode(mode_) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 694 | | KnownIntBits::encode(MinorKeyForKnownInt()) |
| 695 | | TypeInfoBits::encode(runtime_operands_type_) |
| 696 | | RegisterBits::encode(lhs_.is(r0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void Generate(MacroAssembler* masm); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 700 | void HandleNonSmiBitwiseOp(MacroAssembler* masm, |
| 701 | Register lhs, |
| 702 | Register rhs); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 703 | void HandleBinaryOpSlowCases(MacroAssembler* masm, |
| 704 | Label* not_smi, |
| 705 | Register lhs, |
| 706 | Register rhs, |
| 707 | const Builtins::JavaScript& builtin); |
| 708 | void GenerateTypeTransition(MacroAssembler* masm); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 709 | |
| 710 | static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) { |
| 711 | if (constant_rhs == CodeGenerator::kUnknownIntValue) return false; |
| 712 | if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3; |
| 713 | if (op == Token::MOD) { |
| 714 | if (constant_rhs <= 1) return false; |
| 715 | if (constant_rhs <= 10) return true; |
| 716 | if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true; |
| 717 | return false; |
| 718 | } |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | int MinorKeyForKnownInt() { |
| 723 | if (!specialized_on_rhs_) return 0; |
| 724 | if (constant_rhs_ <= 10) return constant_rhs_ + 1; |
| 725 | ASSERT(IsPowerOf2(constant_rhs_)); |
| 726 | int key = 12; |
| 727 | int d = constant_rhs_; |
| 728 | while ((d & 1) == 0) { |
| 729 | key++; |
| 730 | d >>= 1; |
| 731 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 732 | ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 733 | return key; |
| 734 | } |
| 735 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 736 | int KnownBitsForMinorKey(int key) { |
| 737 | if (!key) return 0; |
| 738 | if (key <= 11) return key - 1; |
| 739 | int d = 1; |
| 740 | while (key != 12) { |
| 741 | key--; |
| 742 | d <<= 1; |
| 743 | } |
| 744 | return d; |
| 745 | } |
| 746 | |
| 747 | Register LhsRegister(bool lhs_is_r0) { |
| 748 | return lhs_is_r0 ? r0 : r1; |
| 749 | } |
| 750 | |
| 751 | Register RhsRegister(bool lhs_is_r0) { |
| 752 | return lhs_is_r0 ? r1 : r0; |
| 753 | } |
| 754 | |
| 755 | bool ShouldGenerateSmiCode() { |
| 756 | return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) && |
| 757 | runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS && |
| 758 | runtime_operands_type_ != BinaryOpIC::STRINGS; |
| 759 | } |
| 760 | |
| 761 | bool ShouldGenerateFPCode() { |
| 762 | return runtime_operands_type_ != BinaryOpIC::STRINGS; |
| 763 | } |
| 764 | |
| 765 | virtual int GetCodeKind() { return Code::BINARY_OP_IC; } |
| 766 | |
| 767 | virtual InlineCacheState GetICState() { |
| 768 | return BinaryOpIC::ToState(runtime_operands_type_); |
| 769 | } |
| 770 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 771 | const char* GetName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 772 | |
| 773 | #ifdef DEBUG |
| 774 | void Print() { |
| 775 | if (!specialized_on_rhs_) { |
| 776 | PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); |
| 777 | } else { |
| 778 | PrintF("GenericBinaryOpStub (%s by %d)\n", |
| 779 | Token::String(op_), |
| 780 | constant_rhs_); |
| 781 | } |
| 782 | } |
| 783 | #endif |
| 784 | }; |
| 785 | |
| 786 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 787 | class StringHelper : public AllStatic { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 788 | public: |
| 789 | // Generate code for copying characters using a simple loop. This should only |
| 790 | // be used in places where the number of characters is small and the |
| 791 | // additional setup and checking in GenerateCopyCharactersLong adds too much |
| 792 | // overhead. Copying of overlapping regions is not supported. |
| 793 | // Dest register ends at the position after the last character written. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 794 | static void GenerateCopyCharacters(MacroAssembler* masm, |
| 795 | Register dest, |
| 796 | Register src, |
| 797 | Register count, |
| 798 | Register scratch, |
| 799 | bool ascii); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 800 | |
| 801 | // Generate code for copying a large number of characters. This function |
| 802 | // is allowed to spend extra time setting up conditions to make copying |
| 803 | // faster. Copying of overlapping regions is not supported. |
| 804 | // Dest register ends at the position after the last character written. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 805 | static void GenerateCopyCharactersLong(MacroAssembler* masm, |
| 806 | Register dest, |
| 807 | Register src, |
| 808 | Register count, |
| 809 | Register scratch1, |
| 810 | Register scratch2, |
| 811 | Register scratch3, |
| 812 | Register scratch4, |
| 813 | Register scratch5, |
| 814 | int flags); |
| 815 | |
| 816 | |
| 817 | // Probe the symbol table for a two character string. If the string is |
| 818 | // not found by probing a jump to the label not_found is performed. This jump |
| 819 | // does not guarantee that the string is not in the symbol table. If the |
| 820 | // string is found the code falls through with the string in register r0. |
| 821 | // Contents of both c1 and c2 registers are modified. At the exit c1 is |
| 822 | // guaranteed to contain halfword with low and high bytes equal to |
| 823 | // initial contents of c1 and c2 respectively. |
| 824 | static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm, |
| 825 | Register c1, |
| 826 | Register c2, |
| 827 | Register scratch1, |
| 828 | Register scratch2, |
| 829 | Register scratch3, |
| 830 | Register scratch4, |
| 831 | Register scratch5, |
| 832 | Label* not_found); |
| 833 | |
| 834 | // Generate string hash. |
| 835 | static void GenerateHashInit(MacroAssembler* masm, |
| 836 | Register hash, |
| 837 | Register character); |
| 838 | |
| 839 | static void GenerateHashAddCharacter(MacroAssembler* masm, |
| 840 | Register hash, |
| 841 | Register character); |
| 842 | |
| 843 | static void GenerateHashGetHash(MacroAssembler* masm, |
| 844 | Register hash); |
| 845 | |
| 846 | private: |
| 847 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 848 | }; |
| 849 | |
| 850 | |
| 851 | // Flag that indicates how to generate code for the stub StringAddStub. |
| 852 | enum StringAddFlags { |
| 853 | NO_STRING_ADD_FLAGS = 0, |
| 854 | NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub. |
| 855 | }; |
| 856 | |
| 857 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 858 | class StringAddStub: public CodeStub { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 859 | public: |
| 860 | explicit StringAddStub(StringAddFlags flags) { |
| 861 | string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0); |
| 862 | } |
| 863 | |
| 864 | private: |
| 865 | Major MajorKey() { return StringAdd; } |
| 866 | int MinorKey() { return string_check_ ? 0 : 1; } |
| 867 | |
| 868 | void Generate(MacroAssembler* masm); |
| 869 | |
| 870 | // Should the stub check whether arguments are strings? |
| 871 | bool string_check_; |
| 872 | }; |
| 873 | |
| 874 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 875 | class SubStringStub: public CodeStub { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 876 | public: |
| 877 | SubStringStub() {} |
| 878 | |
| 879 | private: |
| 880 | Major MajorKey() { return SubString; } |
| 881 | int MinorKey() { return 0; } |
| 882 | |
| 883 | void Generate(MacroAssembler* masm); |
| 884 | }; |
| 885 | |
| 886 | |
| 887 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 888 | class StringCompareStub: public CodeStub { |
| 889 | public: |
| 890 | StringCompareStub() { } |
| 891 | |
| 892 | // Compare two flat ASCII strings and returns result in r0. |
| 893 | // Does not use the stack. |
| 894 | static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm, |
| 895 | Register left, |
| 896 | Register right, |
| 897 | Register scratch1, |
| 898 | Register scratch2, |
| 899 | Register scratch3, |
| 900 | Register scratch4); |
| 901 | |
| 902 | private: |
| 903 | Major MajorKey() { return StringCompare; } |
| 904 | int MinorKey() { return 0; } |
| 905 | |
| 906 | void Generate(MacroAssembler* masm); |
| 907 | }; |
| 908 | |
| 909 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 910 | // This stub can do a fast mod operation without using fp. |
| 911 | // It is tail called from the GenericBinaryOpStub and it always |
| 912 | // returns an answer. It never causes GC so it doesn't need a real frame. |
| 913 | // |
| 914 | // The inputs are always positive Smis. This is never called |
| 915 | // where the denominator is a power of 2. We handle that separately. |
| 916 | // |
| 917 | // If we consider the denominator as an odd number multiplied by a power of 2, |
| 918 | // then: |
| 919 | // * The exponent (power of 2) is in the shift_distance register. |
| 920 | // * The odd number is in the odd_number register. It is always in the range |
| 921 | // of 3 to 25. |
| 922 | // * The bits from the numerator that are to be copied to the answer (there are |
| 923 | // shift_distance of them) are in the mask_bits register. |
| 924 | // * The other bits of the numerator have been shifted down and are in the lhs |
| 925 | // register. |
| 926 | class IntegerModStub : public CodeStub { |
| 927 | public: |
| 928 | IntegerModStub(Register result, |
| 929 | Register shift_distance, |
| 930 | Register odd_number, |
| 931 | Register mask_bits, |
| 932 | Register lhs, |
| 933 | Register scratch) |
| 934 | : result_(result), |
| 935 | shift_distance_(shift_distance), |
| 936 | odd_number_(odd_number), |
| 937 | mask_bits_(mask_bits), |
| 938 | lhs_(lhs), |
| 939 | scratch_(scratch) { |
| 940 | // We don't code these in the minor key, so they should always be the same. |
| 941 | // We don't really want to fix that since this stub is rather large and we |
| 942 | // don't want many copies of it. |
| 943 | ASSERT(shift_distance_.is(r9)); |
| 944 | ASSERT(odd_number_.is(r4)); |
| 945 | ASSERT(mask_bits_.is(r3)); |
| 946 | ASSERT(scratch_.is(r5)); |
| 947 | } |
| 948 | |
| 949 | private: |
| 950 | Register result_; |
| 951 | Register shift_distance_; |
| 952 | Register odd_number_; |
| 953 | Register mask_bits_; |
| 954 | Register lhs_; |
| 955 | Register scratch_; |
| 956 | |
| 957 | // Minor key encoding in 16 bits. |
| 958 | class ResultRegisterBits: public BitField<int, 0, 4> {}; |
| 959 | class LhsRegisterBits: public BitField<int, 4, 4> {}; |
| 960 | |
| 961 | Major MajorKey() { return IntegerMod; } |
| 962 | int MinorKey() { |
| 963 | // Encode the parameters in a unique 16 bit value. |
| 964 | return ResultRegisterBits::encode(result_.code()) |
| 965 | | LhsRegisterBits::encode(lhs_.code()); |
| 966 | } |
| 967 | |
| 968 | void Generate(MacroAssembler* masm); |
| 969 | |
| 970 | const char* GetName() { return "IntegerModStub"; } |
| 971 | |
| 972 | // Utility functions. |
| 973 | void DigitSum(MacroAssembler* masm, |
| 974 | Register lhs, |
| 975 | int mask, |
| 976 | int shift, |
| 977 | Label* entry); |
| 978 | void DigitSum(MacroAssembler* masm, |
| 979 | Register lhs, |
| 980 | Register scratch, |
| 981 | int mask, |
| 982 | int shift1, |
| 983 | int shift2, |
| 984 | Label* entry); |
| 985 | void ModGetInRangeBySubtraction(MacroAssembler* masm, |
| 986 | Register lhs, |
| 987 | int shift, |
| 988 | int rhs); |
| 989 | void ModReduce(MacroAssembler* masm, |
| 990 | Register lhs, |
| 991 | int max, |
| 992 | int denominator); |
| 993 | void ModAnswer(MacroAssembler* masm, |
| 994 | Register result, |
| 995 | Register shift_distance, |
| 996 | Register mask_bits, |
| 997 | Register sum_of_digits); |
| 998 | |
| 999 | |
| 1000 | #ifdef DEBUG |
| 1001 | void Print() { PrintF("IntegerModStub\n"); } |
| 1002 | #endif |
| 1003 | }; |
| 1004 | |
| 1005 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1006 | // This stub can convert a signed int32 to a heap number (double). It does |
| 1007 | // not work for int32s that are in Smi range! No GC occurs during this stub |
| 1008 | // so you don't have to set up the frame. |
| 1009 | class WriteInt32ToHeapNumberStub : public CodeStub { |
| 1010 | public: |
| 1011 | WriteInt32ToHeapNumberStub(Register the_int, |
| 1012 | Register the_heap_number, |
| 1013 | Register scratch) |
| 1014 | : the_int_(the_int), |
| 1015 | the_heap_number_(the_heap_number), |
| 1016 | scratch_(scratch) { } |
| 1017 | |
| 1018 | private: |
| 1019 | Register the_int_; |
| 1020 | Register the_heap_number_; |
| 1021 | Register scratch_; |
| 1022 | |
| 1023 | // Minor key encoding in 16 bits. |
| 1024 | class IntRegisterBits: public BitField<int, 0, 4> {}; |
| 1025 | class HeapNumberRegisterBits: public BitField<int, 4, 4> {}; |
| 1026 | class ScratchRegisterBits: public BitField<int, 8, 4> {}; |
| 1027 | |
| 1028 | Major MajorKey() { return WriteInt32ToHeapNumber; } |
| 1029 | int MinorKey() { |
| 1030 | // Encode the parameters in a unique 16 bit value. |
| 1031 | return IntRegisterBits::encode(the_int_.code()) |
| 1032 | | HeapNumberRegisterBits::encode(the_heap_number_.code()) |
| 1033 | | ScratchRegisterBits::encode(scratch_.code()); |
| 1034 | } |
| 1035 | |
| 1036 | void Generate(MacroAssembler* masm); |
| 1037 | |
| 1038 | const char* GetName() { return "WriteInt32ToHeapNumberStub"; } |
| 1039 | |
| 1040 | #ifdef DEBUG |
| 1041 | void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); } |
| 1042 | #endif |
| 1043 | }; |
| 1044 | |
| 1045 | |
| 1046 | class NumberToStringStub: public CodeStub { |
| 1047 | public: |
| 1048 | NumberToStringStub() { } |
| 1049 | |
| 1050 | // Generate code to do a lookup in the number string cache. If the number in |
| 1051 | // the register object is found in the cache the generated code falls through |
| 1052 | // with the result in the result register. The object and the result register |
| 1053 | // can be the same. If the number is not found in the cache the code jumps to |
| 1054 | // the label not_found with only the content of register object unchanged. |
| 1055 | static void GenerateLookupNumberStringCache(MacroAssembler* masm, |
| 1056 | Register object, |
| 1057 | Register result, |
| 1058 | Register scratch1, |
| 1059 | Register scratch2, |
| 1060 | Register scratch3, |
| 1061 | bool object_is_smi, |
| 1062 | Label* not_found); |
| 1063 | |
| 1064 | private: |
| 1065 | Major MajorKey() { return NumberToString; } |
| 1066 | int MinorKey() { return 0; } |
| 1067 | |
| 1068 | void Generate(MacroAssembler* masm); |
| 1069 | |
| 1070 | const char* GetName() { return "NumberToStringStub"; } |
| 1071 | |
| 1072 | #ifdef DEBUG |
| 1073 | void Print() { |
| 1074 | PrintF("NumberToStringStub\n"); |
| 1075 | } |
| 1076 | #endif |
| 1077 | }; |
| 1078 | |
| 1079 | |
| 1080 | class RecordWriteStub : public CodeStub { |
| 1081 | public: |
| 1082 | RecordWriteStub(Register object, Register offset, Register scratch) |
| 1083 | : object_(object), offset_(offset), scratch_(scratch) { } |
| 1084 | |
| 1085 | void Generate(MacroAssembler* masm); |
| 1086 | |
| 1087 | private: |
| 1088 | Register object_; |
| 1089 | Register offset_; |
| 1090 | Register scratch_; |
| 1091 | |
| 1092 | #ifdef DEBUG |
| 1093 | void Print() { |
| 1094 | PrintF("RecordWriteStub (object reg %d), (offset reg %d)," |
| 1095 | " (scratch reg %d)\n", |
| 1096 | object_.code(), offset_.code(), scratch_.code()); |
| 1097 | } |
| 1098 | #endif |
| 1099 | |
| 1100 | // Minor key encoding in 12 bits. 4 bits for each of the three |
| 1101 | // registers (object, offset and scratch) OOOOAAAASSSS. |
| 1102 | class ScratchBits: public BitField<uint32_t, 0, 4> {}; |
| 1103 | class OffsetBits: public BitField<uint32_t, 4, 4> {}; |
| 1104 | class ObjectBits: public BitField<uint32_t, 8, 4> {}; |
| 1105 | |
| 1106 | Major MajorKey() { return RecordWrite; } |
| 1107 | |
| 1108 | int MinorKey() { |
| 1109 | // Encode the registers. |
| 1110 | return ObjectBits::encode(object_.code()) | |
| 1111 | OffsetBits::encode(offset_.code()) | |
| 1112 | ScratchBits::encode(scratch_.code()); |
| 1113 | } |
| 1114 | }; |
| 1115 | |
| 1116 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1117 | } } // namespace v8::internal |
| 1118 | |
| 1119 | #endif // V8_ARM_CODEGEN_ARM_H_ |