blob: 8a632a9c78cecd9d3efddaedb5d7ba74622004f2 [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
531 // Support for fast native caches.
532 void GenerateGetFromCache(ZoneList<Expression*>* args);
533
Andrei Popescu402d9372010-02-26 13:31:12 +0000534 // Fast support for number to string.
535 void GenerateNumberToString(ZoneList<Expression*>* args);
536
Steve Block6ded16b2010-05-10 14:33:55 +0100537 // 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 Popescu402d9372010-02-26 13:31:12 +0000545 void GenerateMathSin(ZoneList<Expression*>* args);
546 void GenerateMathCos(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100547 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000548
Ben Murdochbb769b22010-08-11 14:56:33 +0100549 void GenerateIsRegExpEquivalent(ZoneList<Expression*>* args);
550
Steve Block3ce2e202009-11-05 08:53:23 +0000551 // Simple condition analysis.
552 enum ConditionAnalysis {
553 ALWAYS_TRUE,
554 ALWAYS_FALSE,
555 DONT_KNOW
556 };
557 ConditionAnalysis AnalyzeCondition(Expression* cond);
558
Steve Blocka7e24c12009-10-30 11:49:00 +0000559 // 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 Blockd0582a62009-12-15 09:54:21 +0000565 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 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 Blocka7e24c12009-10-30 11:49:00 +0000573 List<DeferredCode*> deferred_;
574
575 // Assembler
576 MacroAssembler* masm_; // to generate code
577
Andrei Popescu31002712010-02-23 13:46:05 +0000578 CompilationInfo* info_;
579
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 VirtualFrame* frame_;
582 RegisterAllocator* allocator_;
583 Condition cc_reg_;
584 CodeGenState* state_;
Steve Block6ded16b2010-05-10 14:33:55 +0100585 int loop_nesting_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000586
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100587 Vector<TypeInfo>* type_info_;
588
Steve Blocka7e24c12009-10-30 11:49:00 +0000589 // 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 Monsen50ef84f2010-07-29 15:18:00 +0100597 // Size of inlined write barriers generated by EmitNamedStore.
598 static int inlined_write_barrier_size_;
599
Steve Blocka7e24c12009-10-30 11:49:00 +0000600 static InlineRuntimeLUT kInlineRuntimeLUT[];
601
602 friend class VirtualFrame;
603 friend class JumpTarget;
604 friend class Reference;
Leon Clarke4515c472010-02-03 11:58:03 +0000605 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000606 friend class FullCodeGenerator;
607 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000608
609 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
610};
611
612
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100613// Compute a transcendental math function natively, or call the
614// TranscendentalCache runtime function.
615class 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 Merrick75681382010-08-19 15:07:18 +0100628class 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 Blocka7e24c12009-10-30 11:49:00 +0000641class GenericBinaryOpStub : public CodeStub {
642 public:
643 GenericBinaryOpStub(Token::Value op,
644 OverwriteMode mode,
Steve Block6ded16b2010-05-10 14:33:55 +0100645 Register lhs,
646 Register rhs,
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 int constant_rhs = CodeGenerator::kUnknownIntValue)
648 : op_(op),
649 mode_(mode),
Steve Block6ded16b2010-05-10 14:33:55 +0100650 lhs_(lhs),
651 rhs_(rhs),
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 constant_rhs_(constant_rhs),
Leon Clarkee46be812010-01-19 14:06:41 +0000653 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
Steve Block6ded16b2010-05-10 14:33:55 +0100654 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 Clarkee46be812010-01-19 14:06:41 +0000665 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000666
667 private:
668 Token::Value op_;
669 OverwriteMode mode_;
Steve Block6ded16b2010-05-10 14:33:55 +0100670 Register lhs_;
671 Register rhs_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000672 int constant_rhs_;
673 bool specialized_on_rhs_;
Steve Block6ded16b2010-05-10 14:33:55 +0100674 BinaryOpIC::TypeInfo runtime_operands_type_;
Leon Clarkee46be812010-01-19 14:06:41 +0000675 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000676
677 static const int kMaxKnownRhs = 0x40000000;
Steve Block6ded16b2010-05-10 14:33:55 +0100678 static const int kKnownRhsKeyBits = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000679
Steve Block6ded16b2010-05-10 14:33:55 +0100680 // Minor key encoding in 17 bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000681 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
682 class OpBits: public BitField<Token::Value, 2, 6> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100683 class TypeInfoBits: public BitField<int, 8, 2> {};
684 class RegisterBits: public BitField<bool, 10, 1> {};
685 class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000686
687 Major MajorKey() { return GenericBinaryOp; }
688 int MinorKey() {
Steve Block6ded16b2010-05-10 14:33:55 +0100689 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 Blocka7e24c12009-10-30 11:49:00 +0000692 return OpBits::encode(op_)
693 | ModeBits::encode(mode_)
Steve Block6ded16b2010-05-10 14:33:55 +0100694 | KnownIntBits::encode(MinorKeyForKnownInt())
695 | TypeInfoBits::encode(runtime_operands_type_)
696 | RegisterBits::encode(lhs_.is(r0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 }
698
699 void Generate(MacroAssembler* masm);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100700 void HandleNonSmiBitwiseOp(MacroAssembler* masm,
701 Register lhs,
702 Register rhs);
Steve Block6ded16b2010-05-10 14:33:55 +0100703 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 Blocka7e24c12009-10-30 11:49:00 +0000709
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 Block6ded16b2010-05-10 14:33:55 +0100732 ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits));
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 return key;
734 }
735
Steve Block6ded16b2010-05-10 14:33:55 +0100736 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 Clarkee46be812010-01-19 14:06:41 +0000771 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000772
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 Block6ded16b2010-05-10 14:33:55 +0100787class StringHelper : public AllStatic {
Andrei Popescu31002712010-02-23 13:46:05 +0000788 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 Block6ded16b2010-05-10 14:33:55 +0100794 static void GenerateCopyCharacters(MacroAssembler* masm,
795 Register dest,
796 Register src,
797 Register count,
798 Register scratch,
799 bool ascii);
Andrei Popescu31002712010-02-23 13:46:05 +0000800
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 Block6ded16b2010-05-10 14:33:55 +0100805 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 Popescu31002712010-02-23 13:46:05 +0000848};
849
850
851// Flag that indicates how to generate code for the stub StringAddStub.
852enum StringAddFlags {
853 NO_STRING_ADD_FLAGS = 0,
854 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
855};
856
857
Steve Block6ded16b2010-05-10 14:33:55 +0100858class StringAddStub: public CodeStub {
Andrei Popescu31002712010-02-23 13:46:05 +0000859 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 Block6ded16b2010-05-10 14:33:55 +0100875class SubStringStub: public CodeStub {
Andrei Popescu31002712010-02-23 13:46:05 +0000876 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 Clarked91b9f72010-01-27 17:25:45 +0000888class 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 Block8defd9f2010-07-08 12:39:36 +0100910// 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.
926class 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 Block6ded16b2010-05-10 14:33:55 +01001006// 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.
1009class 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
1046class 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
1080class 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 Blocka7e24c12009-10-30 11:49:00 +00001117} } // namespace v8::internal
1118
1119#endif // V8_ARM_CODEGEN_ARM_H_