1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 #ifndef V8_ARM_CODEGEN_ARM_H_
29 #define V8_ARM_CODEGEN_ARM_H_
37 // Forward declarations
38 class CompilationInfo;
41 class RegisterAllocator;
44 enum InitState { CONST_INIT, NOT_CONST_INIT };
45 enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
46 enum GenerateInlineSmi { DONT_GENERATE_INLINE_SMI, GENERATE_INLINE_SMI };
47 enum WriteBarrierCharacter { UNLIKELY_SMI, LIKELY_SMI, NEVER_NEWSPACE };
50 // -------------------------------------------------------------------------
53 // A reference is a C++ stack-allocated object that puts a
54 // reference on the virtual frame. The reference may be consumed
55 // by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
56 // When the lifetime (scope) of a valid reference ends, it must have
57 // been consumed, and be in state UNLOADED.
58 class Reference BASE_EMBEDDED {
60 // The values of the types is important, see size().
61 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
62 Reference(CodeGenerator* cgen,
63 Expression* expression,
64 bool persist_after_get = false);
67 Expression* expression() const { return expression_; }
68 Type type() const { return type_; }
69 void set_type(Type value) {
70 ASSERT_EQ(ILLEGAL, type_);
75 ASSERT_NE(ILLEGAL, type_);
76 ASSERT_NE(UNLOADED, type_);
79 // The size the reference takes up on the stack.
81 return (type_ < SLOT) ? 0 : type_;
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; }
87 bool is_unloaded() const { return type_ == UNLOADED; }
89 // Return the name. Only valid for named property references.
90 Handle<String> GetName();
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
94 // the expression stack, and it is consumed by the call unless the
95 // reference is for a compound assignment.
96 // If the reference is not consumed, it is left in place under its value.
99 // Generate code to store the value on top of the expression stack in the
100 // reference. The reference is expected to be immediately below the value
101 // on the expression stack. The value is stored in the location specified
102 // by the reference, and is left on top of the stack, after the reference
103 // is popped from beneath it (unloaded).
104 void SetValue(InitState init_state, WriteBarrierCharacter wb);
106 // This is in preparation for something that uses the reference on the stack.
107 // If we need this reference afterwards get then dup it now. Otherwise mark
109 inline void DupIfPersist();
112 CodeGenerator* cgen_;
113 Expression* expression_;
115 // Keep the reference on the stack after get, so it can be used by set later.
116 bool persist_after_get_;
120 // -------------------------------------------------------------------------
121 // Code generation state
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.
128 class CodeGenState BASE_EMBEDDED {
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);
134 // Destroy a code generator state and restore the owning code generator's
136 virtual ~CodeGenState();
138 virtual JumpTarget* true_target() const { return NULL; }
139 virtual JumpTarget* false_target() const { return NULL; }
142 inline CodeGenerator* owner() { return owner_; }
143 inline CodeGenState* previous() const { return previous_; }
146 CodeGenerator* owner_;
147 CodeGenState* previous_;
151 class ConditionCodeGenState : public CodeGenState {
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);
159 virtual JumpTarget* true_target() const { return true_target_; }
160 virtual JumpTarget* false_target() const { return false_target_; }
163 JumpTarget* true_target_;
164 JumpTarget* false_target_;
168 class TypeInfoCodeGenState : public CodeGenState {
170 TypeInfoCodeGenState(CodeGenerator* owner,
173 ~TypeInfoCodeGenState();
175 virtual JumpTarget* true_target() const { return previous()->true_target(); }
176 virtual JumpTarget* false_target() const {
177 return previous()->false_target();
182 TypeInfo old_type_info_;
186 // -------------------------------------------------------------------------
187 // Arguments allocation mode
189 enum ArgumentsAllocationMode {
190 NO_ARGUMENTS_ALLOCATION,
191 EAGER_ARGUMENTS_ALLOCATION,
192 LAZY_ARGUMENTS_ALLOCATION
196 // Different nop operations are used by the code generator to detect certain
197 // states of the generated code.
198 enum NopMarkerTypes {
200 PROPERTY_ACCESS_INLINED
204 // -------------------------------------------------------------------------
207 class CodeGenerator: public AstVisitor {
209 // Takes a function literal, generates code for it. This function should only
210 // be called by compiler.cc.
211 static Handle<Code> MakeCode(CompilationInfo* info);
213 // Printing of AST, etc. as requested by flags.
214 static void MakeCodePrologue(CompilationInfo* info);
216 // Allocate and install the code.
217 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
219 CompilationInfo* info);
221 #ifdef ENABLE_LOGGING_AND_PROFILING
222 static bool ShouldGenerateLog(Expression* type);
225 static void SetFunctionInfo(Handle<JSFunction> fun,
226 FunctionLiteral* lit,
228 Handle<Script> script);
230 static bool RecordPositions(MacroAssembler* masm,
232 bool right_here = false);
235 MacroAssembler* masm() { return masm_; }
236 VirtualFrame* frame() const { return frame_; }
237 inline Handle<Script> script();
239 bool has_valid_frame() const { return frame_ != NULL; }
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);
249 RegisterAllocator* allocator() const { return allocator_; }
251 CodeGenState* state() { return state_; }
252 void set_state(CodeGenState* state) { state_ = state; }
254 TypeInfo type_info(Slot* slot) {
255 int index = NumberOfSlot(slot);
256 if (index == kInvalidSlotNumber) return TypeInfo::Unknown();
257 return (*type_info_)[index];
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;
268 return TypeInfo::Unknown();
271 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
273 static const int kUnknownIntValue = -1;
275 // If the name is an inline runtime function call return the number of
276 // expected arguments. Otherwise return -1.
277 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
279 // Constants related to patching of inlined load/store.
280 static int GetInlinedKeyedLoadInstructionsAfterPatch() {
281 return FLAG_debug_code ? 27 : 13;
283 static const int kInlinedKeyedStoreInstructionsAfterPatch = 5;
284 static int GetInlinedNamedStoreInstructionsAfterPatch() {
285 return FLAG_debug_code ? 33 : 14;
289 // Construction/Destruction
290 explicit CodeGenerator(MacroAssembler* masm);
293 inline bool is_eval();
294 inline Scope* scope();
296 // Generating deferred code.
297 void ProcessDeferred();
299 static const int kInvalidSlotNumber = -1;
301 int NumberOfSlot(Slot* slot);
304 bool has_cc() const { return cc_reg_ != al; }
305 JumpTarget* true_target() const { return state_->true_target(); }
306 JumpTarget* false_target() const { return state_->false_target(); }
308 // Track loop nesting level.
309 int loop_nesting() const { return loop_nesting_; }
310 void IncrementLoopNesting() { loop_nesting_++; }
311 void DecrementLoopNesting() { loop_nesting_--; }
314 void VisitStatements(ZoneList<Statement*>* statements);
316 #define DEF_VISIT(type) \
317 void Visit##type(type* node);
318 AST_NODE_LIST(DEF_VISIT)
321 // Main code generation function
322 void Generate(CompilationInfo* info);
324 // Generate the return sequence code. Should be called no more than
325 // once per compiled function, immediately after binding the return
326 // target (which can not be done more than once). The return value should
328 void GenerateReturnSequence();
330 // Returns the arguments allocation mode.
331 ArgumentsAllocationMode ArgumentsMode();
333 // Store the arguments object and allocate it if necessary.
334 void StoreArgumentsObject(bool initial);
336 // The following are used by class Reference.
337 void LoadReference(Reference* ref);
338 void UnloadReference(Reference* ref);
340 static MemOperand ContextOperand(Register context, int index) {
341 return MemOperand(context, Context::SlotOffset(index));
344 MemOperand SlotOperand(Slot* slot, Register tmp);
346 MemOperand ContextSlotOperandCheckExtensions(Slot* slot,
352 static MemOperand GlobalObject() {
353 return ContextOperand(cp, Context::GLOBAL_INDEX);
356 void LoadCondition(Expression* x,
357 JumpTarget* true_target,
358 JumpTarget* false_target,
360 void Load(Expression* expr);
362 void LoadGlobalReceiver(Register scratch);
364 // Read a value from a slot and leave it on top of the expression stack.
365 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
366 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
368 // Store the value on top of the stack to a slot.
369 void StoreToSlot(Slot* slot, InitState init_state);
371 // Support for compiling assignment expressions.
372 void EmitSlotAssignment(Assignment* node);
373 void EmitNamedPropertyAssignment(Assignment* node);
374 void EmitKeyedPropertyAssignment(Assignment* node);
376 // Load a named property, returning it in r0. The receiver is passed on the
377 // stack, and remains there.
378 void EmitNamedLoad(Handle<String> name, bool is_contextual);
380 // Store to a named property. If the store is contextual, value is passed on
381 // the frame and consumed. Otherwise, receiver and value are passed on the
382 // frame and consumed. The result is returned in r0.
383 void EmitNamedStore(Handle<String> name, bool is_contextual);
385 // Load a keyed property, leaving it in r0. The receiver and key are
386 // passed on the stack, and remain there.
387 void EmitKeyedLoad();
389 // Store a keyed property. Key and receiver are on the stack and the value is
390 // in r0. Result is returned in r0.
391 void EmitKeyedStore(StaticType* key_type, WriteBarrierCharacter wb_info);
393 void LoadFromGlobalSlotCheckExtensions(Slot* slot,
394 TypeofState typeof_state,
397 // Support for loading from local/global variables and arguments
398 // whose location is known unless they are shadowed by
399 // eval-introduced bindings. Generates no code for unsupported slot
400 // types and therefore expects to fall through to the slow jump target.
401 void EmitDynamicLoadFromSlotFastCase(Slot* slot,
402 TypeofState typeof_state,
406 // Special code for typeof expressions: Unfortunately, we must
407 // be careful when loading the expression in 'typeof'
408 // expressions. We are not allowed to throw reference errors for
409 // non-existing properties of the global object, so we must make it
410 // look like an explicit property access, instead of an access
411 // through the context chain.
412 void LoadTypeofExpression(Expression* x);
414 void ToBoolean(JumpTarget* true_target, JumpTarget* false_target);
416 // Generate code that computes a shortcutting logical operation.
417 void GenerateLogicalBooleanOperation(BinaryOperation* node);
419 void GenericBinaryOperation(Token::Value op,
420 OverwriteMode overwrite_mode,
421 GenerateInlineSmi inline_smi,
422 int known_rhs = kUnknownIntValue);
423 void Comparison(Condition cc,
426 bool strict = false);
428 void SmiOperation(Token::Value op,
429 Handle<Object> value,
433 void CallWithArguments(ZoneList<Expression*>* arguments,
434 CallFunctionFlags flags,
437 // An optimized implementation of expressions of the form
438 // x.apply(y, arguments). We call x the applicand and y the receiver.
439 // The optimization avoids allocating an arguments object if possible.
440 void CallApplyLazy(Expression* applicand,
441 Expression* receiver,
442 VariableProxy* arguments,
446 void Branch(bool if_true, JumpTarget* target);
449 struct InlineRuntimeLUT {
450 void (CodeGenerator::*method)(ZoneList<Expression*>*);
455 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
456 bool CheckForInlineRuntimeCall(CallRuntime* node);
457 static bool PatchInlineRuntimeEntry(Handle<String> name,
458 const InlineRuntimeLUT& new_entry,
459 InlineRuntimeLUT* old_entry);
461 static Handle<Code> ComputeLazyCompile(int argc);
462 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
464 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
466 static Handle<Code> ComputeKeyedCallInitialize(int argc, InLoopFlag in_loop);
468 // Declare global variables and functions in the given array of
470 void DeclareGlobals(Handle<FixedArray> pairs);
472 // Instantiate the function based on the shared function info.
473 void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
475 // Support for type checks.
476 void GenerateIsSmi(ZoneList<Expression*>* args);
477 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
478 void GenerateIsArray(ZoneList<Expression*>* args);
479 void GenerateIsRegExp(ZoneList<Expression*>* args);
480 void GenerateIsObject(ZoneList<Expression*>* args);
481 void GenerateIsSpecObject(ZoneList<Expression*>* args);
482 void GenerateIsFunction(ZoneList<Expression*>* args);
483 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
485 // Support for construct call checks.
486 void GenerateIsConstructCall(ZoneList<Expression*>* args);
488 // Support for arguments.length and arguments[?].
489 void GenerateArgumentsLength(ZoneList<Expression*>* args);
490 void GenerateArguments(ZoneList<Expression*>* args);
492 // Support for accessing the class and value fields of an object.
493 void GenerateClassOf(ZoneList<Expression*>* args);
494 void GenerateValueOf(ZoneList<Expression*>* args);
495 void GenerateSetValueOf(ZoneList<Expression*>* args);
497 // Fast support for charCodeAt(n).
498 void GenerateStringCharCodeAt(ZoneList<Expression*>* args);
500 // Fast support for string.charAt(n) and string[n].
501 void GenerateStringCharFromCode(ZoneList<Expression*>* args);
503 // Fast support for string.charAt(n) and string[n].
504 void GenerateStringCharAt(ZoneList<Expression*>* args);
506 // Fast support for object equality testing.
507 void GenerateObjectEquals(ZoneList<Expression*>* args);
509 void GenerateLog(ZoneList<Expression*>* args);
511 // Fast support for Math.random().
512 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
514 // Fast support for StringAdd.
515 void GenerateStringAdd(ZoneList<Expression*>* args);
517 // Fast support for SubString.
518 void GenerateSubString(ZoneList<Expression*>* args);
520 // Fast support for StringCompare.
521 void GenerateStringCompare(ZoneList<Expression*>* args);
523 // Support for direct calls from JavaScript to native RegExp code.
524 void GenerateRegExpExec(ZoneList<Expression*>* args);
526 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
528 // Support for fast native caches.
529 void GenerateGetFromCache(ZoneList<Expression*>* args);
531 // Fast support for number to string.
532 void GenerateNumberToString(ZoneList<Expression*>* args);
534 // Fast swapping of elements.
535 void GenerateSwapElements(ZoneList<Expression*>* args);
537 // Fast call for custom callbacks.
538 void GenerateCallFunction(ZoneList<Expression*>* args);
540 // Fast call to math functions.
541 void GenerateMathPow(ZoneList<Expression*>* args);
542 void GenerateMathSin(ZoneList<Expression*>* args);
543 void GenerateMathCos(ZoneList<Expression*>* args);
544 void GenerateMathSqrt(ZoneList<Expression*>* args);
546 // Simple condition analysis.
547 enum ConditionAnalysis {
552 ConditionAnalysis AnalyzeCondition(Expression* cond);
554 // Methods used to indicate which source code is generated for. Source
555 // positions are collected by the assembler and emitted with the relocation
557 void CodeForFunctionPosition(FunctionLiteral* fun);
558 void CodeForReturnPosition(FunctionLiteral* fun);
559 void CodeForStatementPosition(Statement* node);
560 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
561 void CodeForSourcePosition(int pos);
564 // True if the registers are valid for entry to a block.
565 bool HasValidEntryRegisters();
568 List<DeferredCode*> deferred_;
571 MacroAssembler* masm_; // to generate code
573 CompilationInfo* info_;
575 // Code generation state
576 VirtualFrame* frame_;
577 RegisterAllocator* allocator_;
579 CodeGenState* state_;
582 Vector<TypeInfo>* type_info_;
585 BreakTarget function_return_;
587 // True if the function return is shadowed (ie, jumping to the target
588 // function_return_ does not jump to the true function return, but rather
589 // to some unlinking code).
590 bool function_return_is_shadowed_;
592 static InlineRuntimeLUT kInlineRuntimeLUT[];
594 friend class VirtualFrame;
595 friend class JumpTarget;
596 friend class Reference;
597 friend class FastCodeGenerator;
598 friend class FullCodeGenerator;
599 friend class FullCodeGenSyntaxChecker;
601 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
605 // Compute a transcendental math function natively, or call the
606 // TranscendentalCache runtime function.
607 class TranscendentalCacheStub: public CodeStub {
609 explicit TranscendentalCacheStub(TranscendentalCache::Type type)
611 void Generate(MacroAssembler* masm);
613 TranscendentalCache::Type type_;
614 Major MajorKey() { return TranscendentalCache; }
615 int MinorKey() { return type_; }
616 Runtime::FunctionId RuntimeFunction();
620 class GenericBinaryOpStub : public CodeStub {
622 GenericBinaryOpStub(Token::Value op,
626 int constant_rhs = CodeGenerator::kUnknownIntValue)
631 constant_rhs_(constant_rhs),
632 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
633 runtime_operands_type_(BinaryOpIC::DEFAULT),
636 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info)
637 : op_(OpBits::decode(key)),
638 mode_(ModeBits::decode(key)),
639 lhs_(LhsRegister(RegisterBits::decode(key))),
640 rhs_(RhsRegister(RegisterBits::decode(key))),
641 constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))),
642 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)),
643 runtime_operands_type_(type_info),
652 bool specialized_on_rhs_;
653 BinaryOpIC::TypeInfo runtime_operands_type_;
656 static const int kMaxKnownRhs = 0x40000000;
657 static const int kKnownRhsKeyBits = 6;
659 // Minor key encoding in 17 bits.
660 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
661 class OpBits: public BitField<Token::Value, 2, 6> {};
662 class TypeInfoBits: public BitField<int, 8, 2> {};
663 class RegisterBits: public BitField<bool, 10, 1> {};
664 class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {};
666 Major MajorKey() { return GenericBinaryOp; }
668 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
669 (lhs_.is(r1) && rhs_.is(r0)));
670 // Encode the parameters in a unique 18 bit value.
671 return OpBits::encode(op_)
672 | ModeBits::encode(mode_)
673 | KnownIntBits::encode(MinorKeyForKnownInt())
674 | TypeInfoBits::encode(runtime_operands_type_)
675 | RegisterBits::encode(lhs_.is(r0));
678 void Generate(MacroAssembler* masm);
679 void HandleNonSmiBitwiseOp(MacroAssembler* masm,
682 void HandleBinaryOpSlowCases(MacroAssembler* masm,
686 const Builtins::JavaScript& builtin);
687 void GenerateTypeTransition(MacroAssembler* masm);
689 static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
690 if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
691 if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
692 if (op == Token::MOD) {
693 if (constant_rhs <= 1) return false;
694 if (constant_rhs <= 10) return true;
695 if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
701 int MinorKeyForKnownInt() {
702 if (!specialized_on_rhs_) return 0;
703 if (constant_rhs_ <= 10) return constant_rhs_ + 1;
704 ASSERT(IsPowerOf2(constant_rhs_));
706 int d = constant_rhs_;
707 while ((d & 1) == 0) {
711 ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits));
715 int KnownBitsForMinorKey(int key) {
717 if (key <= 11) return key - 1;
726 Register LhsRegister(bool lhs_is_r0) {
727 return lhs_is_r0 ? r0 : r1;
730 Register RhsRegister(bool lhs_is_r0) {
731 return lhs_is_r0 ? r1 : r0;
734 bool ShouldGenerateSmiCode() {
735 return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) &&
736 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
737 runtime_operands_type_ != BinaryOpIC::STRINGS;
740 bool ShouldGenerateFPCode() {
741 return runtime_operands_type_ != BinaryOpIC::STRINGS;
744 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
746 virtual InlineCacheState GetICState() {
747 return BinaryOpIC::ToState(runtime_operands_type_);
750 const char* GetName();
754 if (!specialized_on_rhs_) {
755 PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
757 PrintF("GenericBinaryOpStub (%s by %d)\n",
766 class StringHelper : public AllStatic {
768 // Generate code for copying characters using a simple loop. This should only
769 // be used in places where the number of characters is small and the
770 // additional setup and checking in GenerateCopyCharactersLong adds too much
771 // overhead. Copying of overlapping regions is not supported.
772 // Dest register ends at the position after the last character written.
773 static void GenerateCopyCharacters(MacroAssembler* masm,
780 // Generate code for copying a large number of characters. This function
781 // is allowed to spend extra time setting up conditions to make copying
782 // faster. Copying of overlapping regions is not supported.
783 // Dest register ends at the position after the last character written.
784 static void GenerateCopyCharactersLong(MacroAssembler* masm,
796 // Probe the symbol table for a two character string. If the string is
797 // not found by probing a jump to the label not_found is performed. This jump
798 // does not guarantee that the string is not in the symbol table. If the
799 // string is found the code falls through with the string in register r0.
800 // Contents of both c1 and c2 registers are modified. At the exit c1 is
801 // guaranteed to contain halfword with low and high bytes equal to
802 // initial contents of c1 and c2 respectively.
803 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
813 // Generate string hash.
814 static void GenerateHashInit(MacroAssembler* masm,
818 static void GenerateHashAddCharacter(MacroAssembler* masm,
822 static void GenerateHashGetHash(MacroAssembler* masm,
826 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
830 // Flag that indicates how to generate code for the stub StringAddStub.
831 enum StringAddFlags {
832 NO_STRING_ADD_FLAGS = 0,
833 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
837 class StringAddStub: public CodeStub {
839 explicit StringAddStub(StringAddFlags flags) {
840 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
844 Major MajorKey() { return StringAdd; }
845 int MinorKey() { return string_check_ ? 0 : 1; }
847 void Generate(MacroAssembler* masm);
849 // Should the stub check whether arguments are strings?
854 class SubStringStub: public CodeStub {
859 Major MajorKey() { return SubString; }
860 int MinorKey() { return 0; }
862 void Generate(MacroAssembler* masm);
867 class StringCompareStub: public CodeStub {
869 StringCompareStub() { }
871 // Compare two flat ASCII strings and returns result in r0.
872 // Does not use the stack.
873 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
882 Major MajorKey() { return StringCompare; }
883 int MinorKey() { return 0; }
885 void Generate(MacroAssembler* masm);
889 // This stub can do a fast mod operation without using fp.
890 // It is tail called from the GenericBinaryOpStub and it always
891 // returns an answer. It never causes GC so it doesn't need a real frame.
893 // The inputs are always positive Smis. This is never called
894 // where the denominator is a power of 2. We handle that separately.
896 // If we consider the denominator as an odd number multiplied by a power of 2,
898 // * The exponent (power of 2) is in the shift_distance register.
899 // * The odd number is in the odd_number register. It is always in the range
901 // * The bits from the numerator that are to be copied to the answer (there are
902 // shift_distance of them) are in the mask_bits register.
903 // * The other bits of the numerator have been shifted down and are in the lhs
905 class IntegerModStub : public CodeStub {
907 IntegerModStub(Register result,
908 Register shift_distance,
914 shift_distance_(shift_distance),
915 odd_number_(odd_number),
916 mask_bits_(mask_bits),
919 // We don't code these in the minor key, so they should always be the same.
920 // We don't really want to fix that since this stub is rather large and we
921 // don't want many copies of it.
922 ASSERT(shift_distance_.is(r9));
923 ASSERT(odd_number_.is(r4));
924 ASSERT(mask_bits_.is(r3));
925 ASSERT(scratch_.is(r5));
930 Register shift_distance_;
931 Register odd_number_;
936 // Minor key encoding in 16 bits.
937 class ResultRegisterBits: public BitField<int, 0, 4> {};
938 class LhsRegisterBits: public BitField<int, 4, 4> {};
940 Major MajorKey() { return IntegerMod; }
942 // Encode the parameters in a unique 16 bit value.
943 return ResultRegisterBits::encode(result_.code())
944 | LhsRegisterBits::encode(lhs_.code());
947 void Generate(MacroAssembler* masm);
949 const char* GetName() { return "IntegerModStub"; }
951 // Utility functions.
952 void DigitSum(MacroAssembler* masm,
957 void DigitSum(MacroAssembler* masm,
964 void ModGetInRangeBySubtraction(MacroAssembler* masm,
968 void ModReduce(MacroAssembler* masm,
972 void ModAnswer(MacroAssembler* masm,
974 Register shift_distance,
976 Register sum_of_digits);
980 void Print() { PrintF("IntegerModStub\n"); }
985 // This stub can convert a signed int32 to a heap number (double). It does
986 // not work for int32s that are in Smi range! No GC occurs during this stub
987 // so you don't have to set up the frame.
988 class WriteInt32ToHeapNumberStub : public CodeStub {
990 WriteInt32ToHeapNumberStub(Register the_int,
991 Register the_heap_number,
994 the_heap_number_(the_heap_number),
995 scratch_(scratch) { }
999 Register the_heap_number_;
1002 // Minor key encoding in 16 bits.
1003 class IntRegisterBits: public BitField<int, 0, 4> {};
1004 class HeapNumberRegisterBits: public BitField<int, 4, 4> {};
1005 class ScratchRegisterBits: public BitField<int, 8, 4> {};
1007 Major MajorKey() { return WriteInt32ToHeapNumber; }
1009 // Encode the parameters in a unique 16 bit value.
1010 return IntRegisterBits::encode(the_int_.code())
1011 | HeapNumberRegisterBits::encode(the_heap_number_.code())
1012 | ScratchRegisterBits::encode(scratch_.code());
1015 void Generate(MacroAssembler* masm);
1017 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
1020 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
1025 class NumberToStringStub: public CodeStub {
1027 NumberToStringStub() { }
1029 // Generate code to do a lookup in the number string cache. If the number in
1030 // the register object is found in the cache the generated code falls through
1031 // with the result in the result register. The object and the result register
1032 // can be the same. If the number is not found in the cache the code jumps to
1033 // the label not_found with only the content of register object unchanged.
1034 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
1044 Major MajorKey() { return NumberToString; }
1045 int MinorKey() { return 0; }
1047 void Generate(MacroAssembler* masm);
1049 const char* GetName() { return "NumberToStringStub"; }
1053 PrintF("NumberToStringStub\n");
1059 class RecordWriteStub : public CodeStub {
1061 RecordWriteStub(Register object, Register offset, Register scratch)
1062 : object_(object), offset_(offset), scratch_(scratch) { }
1064 void Generate(MacroAssembler* masm);
1073 PrintF("RecordWriteStub (object reg %d), (offset reg %d),"
1074 " (scratch reg %d)\n",
1075 object_.code(), offset_.code(), scratch_.code());
1079 // Minor key encoding in 12 bits. 4 bits for each of the three
1080 // registers (object, offset and scratch) OOOOAAAASSSS.
1081 class ScratchBits: public BitField<uint32_t, 0, 4> {};
1082 class OffsetBits: public BitField<uint32_t, 4, 4> {};
1083 class ObjectBits: public BitField<uint32_t, 8, 4> {};
1085 Major MajorKey() { return RecordWrite; }
1088 // Encode the registers.
1089 return ObjectBits::encode(object_.code()) |
1090 OffsetBits::encode(offset_.code()) |
1091 ScratchBits::encode(scratch_.code());
1096 } } // namespace v8::internal
1098 #endif // V8_ARM_CODEGEN_ARM_H_