blob: e550a62c33e26a7d2e22f245b6030979dd98f4eb [file] [log] [blame]
Leon Clarked91b9f72010-01-27 17:25:45 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Block6ded16b2010-05-10 14:33:55 +010031#include "ic-inl.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010032#include "ast.h"
Steve Block6ded16b2010-05-10 14:33:55 +010033
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
37// Forward declarations
Leon Clarke4515c472010-02-03 11:58:03 +000038class CompilationInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +000039class DeferredCode;
Kristian Monsen25f61362010-05-21 11:50:48 +010040class JumpTarget;
Steve Blocka7e24c12009-10-30 11:49:00 +000041class RegisterAllocator;
42class RegisterFile;
43
44enum InitState { CONST_INIT, NOT_CONST_INIT };
45enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010046enum GenerateInlineSmi { DONT_GENERATE_INLINE_SMI, GENERATE_INLINE_SMI };
Steve Block8defd9f2010-07-08 12:39:36 +010047enum WriteBarrierCharacter { UNLIKELY_SMI, LIKELY_SMI, NEVER_NEWSPACE };
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49
50// -------------------------------------------------------------------------
51// Reference support
52
Leon Clarked91b9f72010-01-27 17:25:45 +000053// 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 Blocka7e24c12009-10-30 11:49:00 +000058class Reference BASE_EMBEDDED {
59 public:
60 // The values of the types is important, see size().
Leon Clarked91b9f72010-01-27 17:25:45 +000061 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 Blocka7e24c12009-10-30 11:49:00 +000065 ~Reference();
66
67 Expression* expression() const { return expression_; }
68 Type type() const { return type_; }
69 void set_type(Type value) {
Leon Clarked91b9f72010-01-27 17:25:45 +000070 ASSERT_EQ(ILLEGAL, type_);
Steve Blocka7e24c12009-10-30 11:49:00 +000071 type_ = value;
72 }
73
Leon Clarked91b9f72010-01-27 17:25:45 +000074 void set_unloaded() {
75 ASSERT_NE(ILLEGAL, type_);
76 ASSERT_NE(UNLOADED, type_);
77 type_ = UNLOADED;
78 }
Steve Blocka7e24c12009-10-30 11:49:00 +000079 // The size the reference takes up on the stack.
Leon Clarked91b9f72010-01-27 17:25:45 +000080 int size() const {
81 return (type_ < SLOT) ? 0 : type_;
82 }
Steve Blocka7e24c12009-10-30 11:49:00 +000083
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 Clarked91b9f72010-01-27 17:25:45 +000087 bool is_unloaded() const { return type_ == UNLOADED; }
Steve Blocka7e24c12009-10-30 11:49:00 +000088
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 Clarked91b9f72010-01-27 17:25:45 +000094 // 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 Blockd0582a62009-12-15 09:54:21 +000097 void GetValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000098
Steve Blocka7e24c12009-10-30 11:49:00 +000099 // 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 Clarked91b9f72010-01-27 17:25:45 +0000101 // 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 Block8defd9f2010-07-08 12:39:36 +0100104 void SetValue(InitState init_state, WriteBarrierCharacter wb);
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100106 // 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 Blocka7e24c12009-10-30 11:49:00 +0000111 private:
112 CodeGenerator* cgen_;
113 Expression* expression_;
114 Type type_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000115 // Keep the reference on the stack after get, so it can be used by set later.
116 bool persist_after_get_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000117};
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
128class 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 Blocka7e24c12009-10-30 11:49:00 +0000134 // Destroy a code generator state and restore the owning code generator's
135 // previous state.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100136 virtual ~CodeGenState();
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100138 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 Blocka7e24c12009-10-30 11:49:00 +0000144
145 private:
146 CodeGenerator* owner_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100147 CodeGenState* previous_;
148};
149
150
151class 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 Blocka7e24c12009-10-30 11:49:00 +0000163 JumpTarget* true_target_;
164 JumpTarget* false_target_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100165};
166
167
168class 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 Blocka7e24c12009-10-30 11:49:00 +0000183};
184
185
186// -------------------------------------------------------------------------
Steve Block6ded16b2010-05-10 14:33:55 +0100187// Arguments allocation mode
188
189enum 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.
198enum NopMarkerTypes {
199 NON_MARKING_NOP = 0,
200 PROPERTY_ACCESS_INLINED
201};
202
203
204// -------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000205// CodeGenerator
206
207class 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 Popescu31002712010-02-23 13:46:05 +0000211 static Handle<Code> MakeCode(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000212
Steve Block3ce2e202009-11-05 08:53:23 +0000213 // Printing of AST, etc. as requested by flags.
Andrei Popescu31002712010-02-23 13:46:05 +0000214 static void MakeCodePrologue(CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000215
216 // Allocate and install the code.
Andrei Popescu31002712010-02-23 13:46:05 +0000217 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +0000218 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +0000219 CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000220
Steve Blocka7e24c12009-10-30 11:49:00 +0000221#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 Murdoch7f4d5bd2010-06-15 11:15:29 +0100230 static bool RecordPositions(MacroAssembler* masm,
231 int pos,
232 bool right_here = false);
Steve Block3ce2e202009-11-05 08:53:23 +0000233
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 // Accessors
235 MacroAssembler* masm() { return masm_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 VirtualFrame* frame() const { return frame_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000237 inline Handle<Script> script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000238
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 Murdoch7f4d5bd2010-06-15 11:15:29 +0100254 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 Blocka7e24c12009-10-30 11:49:00 +0000271 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
272
273 static const int kUnknownIntValue = -1;
274
Steve Block6ded16b2010-05-10 14:33:55 +0100275 // 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 Monsen25f61362010-05-21 11:50:48 +0100279 // Constants related to patching of inlined load/store.
Steve Block8defd9f2010-07-08 12:39:36 +0100280 static int GetInlinedKeyedLoadInstructionsAfterPatch() {
Iain Merrick75681382010-08-19 15:07:18 +0100281 return FLAG_debug_code ? 32 : 13;
Steve Block8defd9f2010-07-08 12:39:36 +0100282 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100283 static const int kInlinedKeyedStoreInstructionsAfterPatch = 5;
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100284 static int GetInlinedNamedStoreInstructionsAfterPatch() {
285 ASSERT(inlined_write_barrier_size_ != -1);
286 return inlined_write_barrier_size_ + 4;
287 }
Kristian Monsen25f61362010-05-21 11:50:48 +0100288
Iain Merrick75681382010-08-19 15:07:18 +0100289 static MemOperand ContextOperand(Register context, int index) {
290 return MemOperand(context, Context::SlotOffset(index));
291 }
292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 private:
294 // Construction/Destruction
Andrei Popescu31002712010-02-23 13:46:05 +0000295 explicit CodeGenerator(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000296
297 // Accessors
Andrei Popescu31002712010-02-23 13:46:05 +0000298 inline bool is_eval();
Steve Block6ded16b2010-05-10 14:33:55 +0100299 inline Scope* scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000300
301 // Generating deferred code.
302 void ProcessDeferred();
303
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100304 static const int kInvalidSlotNumber = -1;
305
306 int NumberOfSlot(Slot* slot);
307
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 // State
309 bool has_cc() const { return cc_reg_ != al; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 JumpTarget* true_target() const { return state_->true_target(); }
311 JumpTarget* false_target() const { return state_->false_target(); }
312
Steve Block6ded16b2010-05-10 14:33:55 +0100313 // Track loop nesting level.
314 int loop_nesting() const { return loop_nesting_; }
315 void IncrementLoopNesting() { loop_nesting_++; }
316 void DecrementLoopNesting() { loop_nesting_--; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000317
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 Blocka7e24c12009-10-30 11:49:00 +0000326 // Main code generation function
Andrei Popescu402d9372010-02-26 13:31:12 +0000327 void Generate(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000328
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100329 // 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 Block6ded16b2010-05-10 14:33:55 +0100335 // 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 Blocka7e24c12009-10-30 11:49:00 +0000341 // The following are used by class Reference.
342 void LoadReference(Reference* ref);
343 void UnloadReference(Reference* ref);
344
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 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 Block3ce2e202009-11-05 08:53:23 +0000353 static MemOperand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 return ContextOperand(cp, Context::GLOBAL_INDEX);
355 }
356
357 void LoadCondition(Expression* x,
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 JumpTarget* true_target,
359 JumpTarget* false_target,
360 bool force_cc);
Steve Blockd0582a62009-12-15 09:54:21 +0000361 void Load(Expression* expr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 void LoadGlobal();
363 void LoadGlobalReceiver(Register scratch);
364
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 // Read a value from a slot and leave it on top of the expression stack.
366 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
Steve Block6ded16b2010-05-10 14:33:55 +0100367 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
Kristian Monsen25f61362010-05-21 11:50:48 +0100368
Leon Clarkee46be812010-01-19 14:06:41 +0000369 // Store the value on top of the stack to a slot.
370 void StoreToSlot(Slot* slot, InitState init_state);
Steve Block6ded16b2010-05-10 14:33:55 +0100371
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 Clarked91b9f72010-01-27 17:25:45 +0000386 // Load a keyed property, leaving it in r0. The receiver and key are
387 // passed on the stack, and remain there.
Steve Block6ded16b2010-05-10 14:33:55 +0100388 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 Block8defd9f2010-07-08 12:39:36 +0100392 void EmitKeyedStore(StaticType* key_type, WriteBarrierCharacter wb_info);
Leon Clarkee46be812010-01-19 14:06:41 +0000393
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 void LoadFromGlobalSlotCheckExtensions(Slot* slot,
395 TypeofState typeof_state,
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 JumpTarget* slow);
397
Kristian Monsen25f61362010-05-21 11:50:48 +0100398 // 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 Blocka7e24c12009-10-30 11:49:00 +0000407 // 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 Block6ded16b2010-05-10 14:33:55 +0100417 // Generate code that computes a shortcutting logical operation.
418 void GenerateLogicalBooleanOperation(BinaryOperation* node);
419
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 void GenericBinaryOperation(Token::Value op,
421 OverwriteMode overwrite_mode,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100422 GenerateInlineSmi inline_smi,
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 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 Clarkee46be812010-01-19 14:06:41 +0000434 void CallWithArguments(ZoneList<Expression*>* arguments,
435 CallFunctionFlags flags,
436 int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
Steve Block6ded16b2010-05-10 14:33:55 +0100438 // 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 Blocka7e24c12009-10-30 11:49:00 +0000446 // 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 Block6ded16b2010-05-10 14:33:55 +0100453 int nargs;
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 };
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 Block3ce2e202009-11-05 08:53:23 +0000462 static Handle<Code> ComputeLazyCompile(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
464
Steve Block3ce2e202009-11-05 08:53:23 +0000465 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000466
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100467 static Handle<Code> ComputeKeyedCallInitialize(int argc, InLoopFlag in_loop);
468
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 // Declare global variables and functions in the given array of
470 // name/value pairs.
471 void DeclareGlobals(Handle<FixedArray> pairs);
472
Steve Block6ded16b2010-05-10 14:33:55 +0100473 // Instantiate the function based on the shared function info.
474 void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000475
476 // Support for type checks.
477 void GenerateIsSmi(ZoneList<Expression*>* args);
478 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
479 void GenerateIsArray(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000480 void GenerateIsRegExp(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000481 void GenerateIsObject(ZoneList<Expression*>* args);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100482 void GenerateIsSpecObject(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000483 void GenerateIsFunction(ZoneList<Expression*>* args);
Leon Clarked91b9f72010-01-27 17:25:45 +0000484 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
Iain Merrick75681382010-08-19 15:07:18 +0100485 void GenerateIsStringWrapperSafeForDefaultValueOf(
486 ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000487
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 Block6ded16b2010-05-10 14:33:55 +0100493 void GenerateArguments(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000494
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 Murdoch7f4d5bd2010-06-15 11:15:29 +0100501 void GenerateStringCharCodeAt(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000502
Steve Block6ded16b2010-05-10 14:33:55 +0100503 // Fast support for string.charAt(n) and string[n].
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100504 void GenerateStringCharFromCode(ZoneList<Expression*>* args);
505
506 // Fast support for string.charAt(n) and string[n].
507 void GenerateStringCharAt(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100508
Steve Blocka7e24c12009-10-30 11:49:00 +0000509 // 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 Block6ded16b2010-05-10 14:33:55 +0100515 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000516
Steve Blockd0582a62009-12-15 09:54:21 +0000517 // Fast support for StringAdd.
518 void GenerateStringAdd(ZoneList<Expression*>* args);
519
Leon Clarkee46be812010-01-19 14:06:41 +0000520 // 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 Block6ded16b2010-05-10 14:33:55 +0100529 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
530
Steve Block791712a2010-08-27 10:21:07 +0100531 void GenerateRegExpCloneResult(ZoneList<Expression*>* args);
532
Steve Block6ded16b2010-05-10 14:33:55 +0100533 // Support for fast native caches.
534 void GenerateGetFromCache(ZoneList<Expression*>* args);
535
Andrei Popescu402d9372010-02-26 13:31:12 +0000536 // Fast support for number to string.
537 void GenerateNumberToString(ZoneList<Expression*>* args);
538
Steve Block6ded16b2010-05-10 14:33:55 +0100539 // Fast swapping of elements.
540 void GenerateSwapElements(ZoneList<Expression*>* args);
541
542 // Fast call for custom callbacks.
543 void GenerateCallFunction(ZoneList<Expression*>* args);
544
545 // Fast call to math functions.
546 void GenerateMathPow(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000547 void GenerateMathSin(ZoneList<Expression*>* args);
548 void GenerateMathCos(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100549 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000550
Ben Murdochbb769b22010-08-11 14:56:33 +0100551 void GenerateIsRegExpEquivalent(ZoneList<Expression*>* args);
552
Steve Block3ce2e202009-11-05 08:53:23 +0000553 // Simple condition analysis.
554 enum ConditionAnalysis {
555 ALWAYS_TRUE,
556 ALWAYS_FALSE,
557 DONT_KNOW
558 };
559 ConditionAnalysis AnalyzeCondition(Expression* cond);
560
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 // Methods used to indicate which source code is generated for. Source
562 // positions are collected by the assembler and emitted with the relocation
563 // information.
564 void CodeForFunctionPosition(FunctionLiteral* fun);
565 void CodeForReturnPosition(FunctionLiteral* fun);
566 void CodeForStatementPosition(Statement* node);
Steve Blockd0582a62009-12-15 09:54:21 +0000567 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 void CodeForSourcePosition(int pos);
569
570#ifdef DEBUG
571 // True if the registers are valid for entry to a block.
572 bool HasValidEntryRegisters();
573#endif
574
Steve Blocka7e24c12009-10-30 11:49:00 +0000575 List<DeferredCode*> deferred_;
576
577 // Assembler
578 MacroAssembler* masm_; // to generate code
579
Andrei Popescu31002712010-02-23 13:46:05 +0000580 CompilationInfo* info_;
581
Steve Blocka7e24c12009-10-30 11:49:00 +0000582 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 VirtualFrame* frame_;
584 RegisterAllocator* allocator_;
585 Condition cc_reg_;
586 CodeGenState* state_;
Steve Block6ded16b2010-05-10 14:33:55 +0100587 int loop_nesting_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000588
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100589 Vector<TypeInfo>* type_info_;
590
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 // Jump targets
592 BreakTarget function_return_;
593
594 // True if the function return is shadowed (ie, jumping to the target
595 // function_return_ does not jump to the true function return, but rather
596 // to some unlinking code).
597 bool function_return_is_shadowed_;
598
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100599 // Size of inlined write barriers generated by EmitNamedStore.
600 static int inlined_write_barrier_size_;
601
Steve Blocka7e24c12009-10-30 11:49:00 +0000602 static InlineRuntimeLUT kInlineRuntimeLUT[];
603
604 friend class VirtualFrame;
605 friend class JumpTarget;
606 friend class Reference;
Leon Clarke4515c472010-02-03 11:58:03 +0000607 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000608 friend class FullCodeGenerator;
609 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000610
611 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
612};
613
614
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100615// Compute a transcendental math function natively, or call the
616// TranscendentalCache runtime function.
617class TranscendentalCacheStub: public CodeStub {
618 public:
619 explicit TranscendentalCacheStub(TranscendentalCache::Type type)
620 : type_(type) {}
621 void Generate(MacroAssembler* masm);
622 private:
623 TranscendentalCache::Type type_;
624 Major MajorKey() { return TranscendentalCache; }
625 int MinorKey() { return type_; }
626 Runtime::FunctionId RuntimeFunction();
627};
628
629
Iain Merrick75681382010-08-19 15:07:18 +0100630class ToBooleanStub: public CodeStub {
631 public:
632 explicit ToBooleanStub(Register tos) : tos_(tos) { }
633
634 void Generate(MacroAssembler* masm);
635
636 private:
637 Register tos_;
638 Major MajorKey() { return ToBoolean; }
639 int MinorKey() { return tos_.code(); }
640};
641
642
Steve Blocka7e24c12009-10-30 11:49:00 +0000643class GenericBinaryOpStub : public CodeStub {
644 public:
645 GenericBinaryOpStub(Token::Value op,
646 OverwriteMode mode,
Steve Block6ded16b2010-05-10 14:33:55 +0100647 Register lhs,
648 Register rhs,
Steve Blocka7e24c12009-10-30 11:49:00 +0000649 int constant_rhs = CodeGenerator::kUnknownIntValue)
650 : op_(op),
651 mode_(mode),
Steve Block6ded16b2010-05-10 14:33:55 +0100652 lhs_(lhs),
653 rhs_(rhs),
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 constant_rhs_(constant_rhs),
Leon Clarkee46be812010-01-19 14:06:41 +0000655 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
Steve Block6ded16b2010-05-10 14:33:55 +0100656 runtime_operands_type_(BinaryOpIC::DEFAULT),
657 name_(NULL) { }
658
659 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info)
660 : op_(OpBits::decode(key)),
661 mode_(ModeBits::decode(key)),
662 lhs_(LhsRegister(RegisterBits::decode(key))),
663 rhs_(RhsRegister(RegisterBits::decode(key))),
664 constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))),
665 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)),
666 runtime_operands_type_(type_info),
Leon Clarkee46be812010-01-19 14:06:41 +0000667 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000668
669 private:
670 Token::Value op_;
671 OverwriteMode mode_;
Steve Block6ded16b2010-05-10 14:33:55 +0100672 Register lhs_;
673 Register rhs_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000674 int constant_rhs_;
675 bool specialized_on_rhs_;
Steve Block6ded16b2010-05-10 14:33:55 +0100676 BinaryOpIC::TypeInfo runtime_operands_type_;
Leon Clarkee46be812010-01-19 14:06:41 +0000677 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000678
679 static const int kMaxKnownRhs = 0x40000000;
Steve Block6ded16b2010-05-10 14:33:55 +0100680 static const int kKnownRhsKeyBits = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000681
Steve Block6ded16b2010-05-10 14:33:55 +0100682 // Minor key encoding in 17 bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000683 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
684 class OpBits: public BitField<Token::Value, 2, 6> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100685 class TypeInfoBits: public BitField<int, 8, 2> {};
686 class RegisterBits: public BitField<bool, 10, 1> {};
687 class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000688
689 Major MajorKey() { return GenericBinaryOp; }
690 int MinorKey() {
Steve Block6ded16b2010-05-10 14:33:55 +0100691 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
692 (lhs_.is(r1) && rhs_.is(r0)));
693 // Encode the parameters in a unique 18 bit value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 return OpBits::encode(op_)
695 | ModeBits::encode(mode_)
Steve Block6ded16b2010-05-10 14:33:55 +0100696 | KnownIntBits::encode(MinorKeyForKnownInt())
697 | TypeInfoBits::encode(runtime_operands_type_)
698 | RegisterBits::encode(lhs_.is(r0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 }
700
701 void Generate(MacroAssembler* masm);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100702 void HandleNonSmiBitwiseOp(MacroAssembler* masm,
703 Register lhs,
704 Register rhs);
Steve Block6ded16b2010-05-10 14:33:55 +0100705 void HandleBinaryOpSlowCases(MacroAssembler* masm,
706 Label* not_smi,
707 Register lhs,
708 Register rhs,
709 const Builtins::JavaScript& builtin);
710 void GenerateTypeTransition(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000711
712 static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
713 if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
714 if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
715 if (op == Token::MOD) {
716 if (constant_rhs <= 1) return false;
717 if (constant_rhs <= 10) return true;
718 if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
719 return false;
720 }
721 return false;
722 }
723
724 int MinorKeyForKnownInt() {
725 if (!specialized_on_rhs_) return 0;
726 if (constant_rhs_ <= 10) return constant_rhs_ + 1;
727 ASSERT(IsPowerOf2(constant_rhs_));
728 int key = 12;
729 int d = constant_rhs_;
730 while ((d & 1) == 0) {
731 key++;
732 d >>= 1;
733 }
Steve Block6ded16b2010-05-10 14:33:55 +0100734 ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits));
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 return key;
736 }
737
Steve Block6ded16b2010-05-10 14:33:55 +0100738 int KnownBitsForMinorKey(int key) {
739 if (!key) return 0;
740 if (key <= 11) return key - 1;
741 int d = 1;
742 while (key != 12) {
743 key--;
744 d <<= 1;
745 }
746 return d;
747 }
748
749 Register LhsRegister(bool lhs_is_r0) {
750 return lhs_is_r0 ? r0 : r1;
751 }
752
753 Register RhsRegister(bool lhs_is_r0) {
754 return lhs_is_r0 ? r1 : r0;
755 }
756
757 bool ShouldGenerateSmiCode() {
758 return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) &&
759 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
760 runtime_operands_type_ != BinaryOpIC::STRINGS;
761 }
762
763 bool ShouldGenerateFPCode() {
764 return runtime_operands_type_ != BinaryOpIC::STRINGS;
765 }
766
767 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
768
769 virtual InlineCacheState GetICState() {
770 return BinaryOpIC::ToState(runtime_operands_type_);
771 }
772
Leon Clarkee46be812010-01-19 14:06:41 +0000773 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000774
775#ifdef DEBUG
776 void Print() {
777 if (!specialized_on_rhs_) {
778 PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
779 } else {
780 PrintF("GenericBinaryOpStub (%s by %d)\n",
781 Token::String(op_),
782 constant_rhs_);
783 }
784 }
785#endif
786};
787
788
Steve Block6ded16b2010-05-10 14:33:55 +0100789class StringHelper : public AllStatic {
Andrei Popescu31002712010-02-23 13:46:05 +0000790 public:
791 // Generate code for copying characters using a simple loop. This should only
792 // be used in places where the number of characters is small and the
793 // additional setup and checking in GenerateCopyCharactersLong adds too much
794 // overhead. Copying of overlapping regions is not supported.
795 // Dest register ends at the position after the last character written.
Steve Block6ded16b2010-05-10 14:33:55 +0100796 static void GenerateCopyCharacters(MacroAssembler* masm,
797 Register dest,
798 Register src,
799 Register count,
800 Register scratch,
801 bool ascii);
Andrei Popescu31002712010-02-23 13:46:05 +0000802
803 // Generate code for copying a large number of characters. This function
804 // is allowed to spend extra time setting up conditions to make copying
805 // faster. Copying of overlapping regions is not supported.
806 // Dest register ends at the position after the last character written.
Steve Block6ded16b2010-05-10 14:33:55 +0100807 static void GenerateCopyCharactersLong(MacroAssembler* masm,
808 Register dest,
809 Register src,
810 Register count,
811 Register scratch1,
812 Register scratch2,
813 Register scratch3,
814 Register scratch4,
815 Register scratch5,
816 int flags);
817
818
819 // Probe the symbol table for a two character string. If the string is
820 // not found by probing a jump to the label not_found is performed. This jump
821 // does not guarantee that the string is not in the symbol table. If the
822 // string is found the code falls through with the string in register r0.
823 // Contents of both c1 and c2 registers are modified. At the exit c1 is
824 // guaranteed to contain halfword with low and high bytes equal to
825 // initial contents of c1 and c2 respectively.
826 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
827 Register c1,
828 Register c2,
829 Register scratch1,
830 Register scratch2,
831 Register scratch3,
832 Register scratch4,
833 Register scratch5,
834 Label* not_found);
835
836 // Generate string hash.
837 static void GenerateHashInit(MacroAssembler* masm,
838 Register hash,
839 Register character);
840
841 static void GenerateHashAddCharacter(MacroAssembler* masm,
842 Register hash,
843 Register character);
844
845 static void GenerateHashGetHash(MacroAssembler* masm,
846 Register hash);
847
848 private:
849 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
Andrei Popescu31002712010-02-23 13:46:05 +0000850};
851
852
853// Flag that indicates how to generate code for the stub StringAddStub.
854enum StringAddFlags {
855 NO_STRING_ADD_FLAGS = 0,
856 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
857};
858
859
Steve Block6ded16b2010-05-10 14:33:55 +0100860class StringAddStub: public CodeStub {
Andrei Popescu31002712010-02-23 13:46:05 +0000861 public:
862 explicit StringAddStub(StringAddFlags flags) {
863 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
864 }
865
866 private:
867 Major MajorKey() { return StringAdd; }
868 int MinorKey() { return string_check_ ? 0 : 1; }
869
870 void Generate(MacroAssembler* masm);
871
872 // Should the stub check whether arguments are strings?
873 bool string_check_;
874};
875
876
Steve Block6ded16b2010-05-10 14:33:55 +0100877class SubStringStub: public CodeStub {
Andrei Popescu31002712010-02-23 13:46:05 +0000878 public:
879 SubStringStub() {}
880
881 private:
882 Major MajorKey() { return SubString; }
883 int MinorKey() { return 0; }
884
885 void Generate(MacroAssembler* masm);
886};
887
888
889
Leon Clarked91b9f72010-01-27 17:25:45 +0000890class StringCompareStub: public CodeStub {
891 public:
892 StringCompareStub() { }
893
894 // Compare two flat ASCII strings and returns result in r0.
895 // Does not use the stack.
896 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
897 Register left,
898 Register right,
899 Register scratch1,
900 Register scratch2,
901 Register scratch3,
902 Register scratch4);
903
904 private:
905 Major MajorKey() { return StringCompare; }
906 int MinorKey() { return 0; }
907
908 void Generate(MacroAssembler* masm);
909};
910
911
Steve Block8defd9f2010-07-08 12:39:36 +0100912// This stub can do a fast mod operation without using fp.
913// It is tail called from the GenericBinaryOpStub and it always
914// returns an answer. It never causes GC so it doesn't need a real frame.
915//
916// The inputs are always positive Smis. This is never called
917// where the denominator is a power of 2. We handle that separately.
918//
919// If we consider the denominator as an odd number multiplied by a power of 2,
920// then:
921// * The exponent (power of 2) is in the shift_distance register.
922// * The odd number is in the odd_number register. It is always in the range
923// of 3 to 25.
924// * The bits from the numerator that are to be copied to the answer (there are
925// shift_distance of them) are in the mask_bits register.
926// * The other bits of the numerator have been shifted down and are in the lhs
927// register.
928class IntegerModStub : public CodeStub {
929 public:
930 IntegerModStub(Register result,
931 Register shift_distance,
932 Register odd_number,
933 Register mask_bits,
934 Register lhs,
935 Register scratch)
936 : result_(result),
937 shift_distance_(shift_distance),
938 odd_number_(odd_number),
939 mask_bits_(mask_bits),
940 lhs_(lhs),
941 scratch_(scratch) {
942 // We don't code these in the minor key, so they should always be the same.
943 // We don't really want to fix that since this stub is rather large and we
944 // don't want many copies of it.
945 ASSERT(shift_distance_.is(r9));
946 ASSERT(odd_number_.is(r4));
947 ASSERT(mask_bits_.is(r3));
948 ASSERT(scratch_.is(r5));
949 }
950
951 private:
952 Register result_;
953 Register shift_distance_;
954 Register odd_number_;
955 Register mask_bits_;
956 Register lhs_;
957 Register scratch_;
958
959 // Minor key encoding in 16 bits.
960 class ResultRegisterBits: public BitField<int, 0, 4> {};
961 class LhsRegisterBits: public BitField<int, 4, 4> {};
962
963 Major MajorKey() { return IntegerMod; }
964 int MinorKey() {
965 // Encode the parameters in a unique 16 bit value.
966 return ResultRegisterBits::encode(result_.code())
967 | LhsRegisterBits::encode(lhs_.code());
968 }
969
970 void Generate(MacroAssembler* masm);
971
972 const char* GetName() { return "IntegerModStub"; }
973
974 // Utility functions.
975 void DigitSum(MacroAssembler* masm,
976 Register lhs,
977 int mask,
978 int shift,
979 Label* entry);
980 void DigitSum(MacroAssembler* masm,
981 Register lhs,
982 Register scratch,
983 int mask,
984 int shift1,
985 int shift2,
986 Label* entry);
987 void ModGetInRangeBySubtraction(MacroAssembler* masm,
988 Register lhs,
989 int shift,
990 int rhs);
991 void ModReduce(MacroAssembler* masm,
992 Register lhs,
993 int max,
994 int denominator);
995 void ModAnswer(MacroAssembler* masm,
996 Register result,
997 Register shift_distance,
998 Register mask_bits,
999 Register sum_of_digits);
1000
1001
1002#ifdef DEBUG
1003 void Print() { PrintF("IntegerModStub\n"); }
1004#endif
1005};
1006
1007
Steve Block6ded16b2010-05-10 14:33:55 +01001008// This stub can convert a signed int32 to a heap number (double). It does
1009// not work for int32s that are in Smi range! No GC occurs during this stub
1010// so you don't have to set up the frame.
1011class WriteInt32ToHeapNumberStub : public CodeStub {
1012 public:
1013 WriteInt32ToHeapNumberStub(Register the_int,
1014 Register the_heap_number,
1015 Register scratch)
1016 : the_int_(the_int),
1017 the_heap_number_(the_heap_number),
1018 scratch_(scratch) { }
1019
1020 private:
1021 Register the_int_;
1022 Register the_heap_number_;
1023 Register scratch_;
1024
1025 // Minor key encoding in 16 bits.
1026 class IntRegisterBits: public BitField<int, 0, 4> {};
1027 class HeapNumberRegisterBits: public BitField<int, 4, 4> {};
1028 class ScratchRegisterBits: public BitField<int, 8, 4> {};
1029
1030 Major MajorKey() { return WriteInt32ToHeapNumber; }
1031 int MinorKey() {
1032 // Encode the parameters in a unique 16 bit value.
1033 return IntRegisterBits::encode(the_int_.code())
1034 | HeapNumberRegisterBits::encode(the_heap_number_.code())
1035 | ScratchRegisterBits::encode(scratch_.code());
1036 }
1037
1038 void Generate(MacroAssembler* masm);
1039
1040 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
1041
1042#ifdef DEBUG
1043 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
1044#endif
1045};
1046
1047
1048class NumberToStringStub: public CodeStub {
1049 public:
1050 NumberToStringStub() { }
1051
1052 // Generate code to do a lookup in the number string cache. If the number in
1053 // the register object is found in the cache the generated code falls through
1054 // with the result in the result register. The object and the result register
1055 // can be the same. If the number is not found in the cache the code jumps to
1056 // the label not_found with only the content of register object unchanged.
1057 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
1058 Register object,
1059 Register result,
1060 Register scratch1,
1061 Register scratch2,
1062 Register scratch3,
1063 bool object_is_smi,
1064 Label* not_found);
1065
1066 private:
1067 Major MajorKey() { return NumberToString; }
1068 int MinorKey() { return 0; }
1069
1070 void Generate(MacroAssembler* masm);
1071
1072 const char* GetName() { return "NumberToStringStub"; }
1073
1074#ifdef DEBUG
1075 void Print() {
1076 PrintF("NumberToStringStub\n");
1077 }
1078#endif
1079};
1080
1081
1082class RecordWriteStub : public CodeStub {
1083 public:
1084 RecordWriteStub(Register object, Register offset, Register scratch)
1085 : object_(object), offset_(offset), scratch_(scratch) { }
1086
1087 void Generate(MacroAssembler* masm);
1088
1089 private:
1090 Register object_;
1091 Register offset_;
1092 Register scratch_;
1093
1094#ifdef DEBUG
1095 void Print() {
1096 PrintF("RecordWriteStub (object reg %d), (offset reg %d),"
1097 " (scratch reg %d)\n",
1098 object_.code(), offset_.code(), scratch_.code());
1099 }
1100#endif
1101
1102 // Minor key encoding in 12 bits. 4 bits for each of the three
1103 // registers (object, offset and scratch) OOOOAAAASSSS.
1104 class ScratchBits: public BitField<uint32_t, 0, 4> {};
1105 class OffsetBits: public BitField<uint32_t, 4, 4> {};
1106 class ObjectBits: public BitField<uint32_t, 8, 4> {};
1107
1108 Major MajorKey() { return RecordWrite; }
1109
1110 int MinorKey() {
1111 // Encode the registers.
1112 return ObjectBits::encode(object_.code()) |
1113 OffsetBits::encode(offset_.code()) |
1114 ScratchBits::encode(scratch_.code());
1115 }
1116};
1117
1118
Steve Blocka7e24c12009-10-30 11:49:00 +00001119} } // namespace v8::internal
1120
1121#endif // V8_ARM_CODEGEN_ARM_H_