blob: f694ddeb869be568c6285be894896d9657f362ea [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_X64_CODEGEN_X64_H_
29#define V8_X64_CODEGEN_X64_H_
30
Kristian Monsen25f61362010-05-21 11:50:48 +010031#include "ast.h"
Steve Block6ded16b2010-05-10 14:33:55 +010032#include "ic-inl.h"
Kristian Monsen25f61362010-05-21 11:50:48 +010033#include "jump-target-heavy.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034
Steve Blocka7e24c12009-10-30 11:49:00 +000035namespace v8 {
36namespace internal {
37
38// Forward declarations
Leon Clarke4515c472010-02-03 11:58:03 +000039class CompilationInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +000040class DeferredCode;
41class RegisterAllocator;
42class RegisterFile;
43
44enum InitState { CONST_INIT, NOT_CONST_INIT };
45enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
46
47
48// -------------------------------------------------------------------------
49// Reference support
50
Leon Clarked91b9f72010-01-27 17:25:45 +000051// A reference is a C++ stack-allocated object that puts a
52// reference on the virtual frame. The reference may be consumed
53// by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
54// When the lifetime (scope) of a valid reference ends, it must have
55// been consumed, and be in state UNLOADED.
Steve Blocka7e24c12009-10-30 11:49:00 +000056class Reference BASE_EMBEDDED {
57 public:
58 // The values of the types is important, see size().
Leon Clarked91b9f72010-01-27 17:25:45 +000059 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
60
61 Reference(CodeGenerator* cgen,
62 Expression* expression,
63 bool persist_after_get = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000064 ~Reference();
65
66 Expression* expression() const { return expression_; }
67 Type type() const { return type_; }
68 void set_type(Type value) {
Leon Clarked91b9f72010-01-27 17:25:45 +000069 ASSERT_EQ(ILLEGAL, type_);
Steve Blocka7e24c12009-10-30 11:49:00 +000070 type_ = value;
71 }
72
Leon Clarked91b9f72010-01-27 17:25:45 +000073 void set_unloaded() {
74 ASSERT_NE(ILLEGAL, type_);
75 ASSERT_NE(UNLOADED, type_);
76 type_ = UNLOADED;
77 }
Steve Blocka7e24c12009-10-30 11:49:00 +000078 // The size the reference takes up on the stack.
Leon Clarked91b9f72010-01-27 17:25:45 +000079 int size() const {
80 return (type_ < SLOT) ? 0 : type_;
81 }
Steve Blocka7e24c12009-10-30 11:49:00 +000082
83 bool is_illegal() const { return type_ == ILLEGAL; }
84 bool is_slot() const { return type_ == SLOT; }
85 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
Leon Clarked91b9f72010-01-27 17:25:45 +000086 bool is_unloaded() const { return type_ == UNLOADED; }
Steve Blocka7e24c12009-10-30 11:49:00 +000087
88 // Return the name. Only valid for named property references.
89 Handle<String> GetName();
90
91 // Generate code to push the value of the reference on top of the
92 // expression stack. The reference is expected to be already on top of
Leon Clarked91b9f72010-01-27 17:25:45 +000093 // the expression stack, and it is consumed by the call unless the
94 // reference is for a compound assignment.
95 // If the reference is not consumed, it is left in place under its value.
Steve Blockd0582a62009-12-15 09:54:21 +000096 void GetValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000097
98 // Like GetValue except that the slot is expected to be written to before
Leon Clarked91b9f72010-01-27 17:25:45 +000099 // being read from again. The value of the reference may be invalidated,
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 // causing subsequent attempts to read it to fail.
Steve Blockd0582a62009-12-15 09:54:21 +0000101 void TakeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
103 // Generate code to store the value on top of the expression stack in the
104 // reference. The reference is expected to be immediately below the value
Leon Clarked91b9f72010-01-27 17:25:45 +0000105 // on the expression stack. The value is stored in the location specified
106 // by the reference, and is left on top of the stack, after the reference
107 // is popped from beneath it (unloaded).
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 void SetValue(InitState init_state);
109
110 private:
111 CodeGenerator* cgen_;
112 Expression* expression_;
113 Type type_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000114 bool persist_after_get_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000115};
116
117
118// -------------------------------------------------------------------------
119// Control destinations.
120
121// A control destination encapsulates a pair of jump targets and a
122// flag indicating which one is the preferred fall-through. The
123// preferred fall-through must be unbound, the other may be already
124// bound (ie, a backward target).
125//
126// The true and false targets may be jumped to unconditionally or
127// control may split conditionally. Unconditional jumping and
128// splitting should be emitted in tail position (as the last thing
129// when compiling an expression) because they can cause either label
130// to be bound or the non-fall through to be jumped to leaving an
131// invalid virtual frame.
132//
133// The labels in the control destination can be extracted and
134// manipulated normally without affecting the state of the
135// destination.
136
137class ControlDestination BASE_EMBEDDED {
138 public:
139 ControlDestination(JumpTarget* true_target,
140 JumpTarget* false_target,
141 bool true_is_fall_through)
142 : true_target_(true_target),
143 false_target_(false_target),
144 true_is_fall_through_(true_is_fall_through),
145 is_used_(false) {
146 ASSERT(true_is_fall_through ? !true_target->is_bound()
147 : !false_target->is_bound());
148 }
149
150 // Accessors for the jump targets. Directly jumping or branching to
151 // or binding the targets will not update the destination's state.
152 JumpTarget* true_target() const { return true_target_; }
153 JumpTarget* false_target() const { return false_target_; }
154
155 // True if the the destination has been jumped to unconditionally or
156 // control has been split to both targets. This predicate does not
157 // test whether the targets have been extracted and manipulated as
158 // raw jump targets.
159 bool is_used() const { return is_used_; }
160
161 // True if the destination is used and the true target (respectively
162 // false target) was the fall through. If the target is backward,
163 // "fall through" included jumping unconditionally to it.
164 bool true_was_fall_through() const {
165 return is_used_ && true_is_fall_through_;
166 }
167
168 bool false_was_fall_through() const {
169 return is_used_ && !true_is_fall_through_;
170 }
171
172 // Emit a branch to one of the true or false targets, and bind the
173 // other target. Because this binds the fall-through target, it
174 // should be emitted in tail position (as the last thing when
175 // compiling an expression).
176 void Split(Condition cc) {
177 ASSERT(!is_used_);
178 if (true_is_fall_through_) {
179 false_target_->Branch(NegateCondition(cc));
180 true_target_->Bind();
181 } else {
182 true_target_->Branch(cc);
183 false_target_->Bind();
184 }
185 is_used_ = true;
186 }
187
188 // Emit an unconditional jump in tail position, to the true target
189 // (if the argument is true) or the false target. The "jump" will
190 // actually bind the jump target if it is forward, jump to it if it
191 // is backward.
192 void Goto(bool where) {
193 ASSERT(!is_used_);
194 JumpTarget* target = where ? true_target_ : false_target_;
195 if (target->is_bound()) {
196 target->Jump();
197 } else {
198 target->Bind();
199 }
200 is_used_ = true;
201 true_is_fall_through_ = where;
202 }
203
204 // Mark this jump target as used as if Goto had been called, but
205 // without generating a jump or binding a label (the control effect
206 // should have already happened). This is used when the left
207 // subexpression of the short-circuit boolean operators are
208 // compiled.
209 void Use(bool where) {
210 ASSERT(!is_used_);
211 ASSERT((where ? true_target_ : false_target_)->is_bound());
212 is_used_ = true;
213 true_is_fall_through_ = where;
214 }
215
216 // Swap the true and false targets but keep the same actual label as
217 // the fall through. This is used when compiling negated
218 // expressions, where we want to swap the targets but preserve the
219 // state.
220 void Invert() {
221 JumpTarget* temp_target = true_target_;
222 true_target_ = false_target_;
223 false_target_ = temp_target;
224
225 true_is_fall_through_ = !true_is_fall_through_;
226 }
227
228 private:
229 // True and false jump targets.
230 JumpTarget* true_target_;
231 JumpTarget* false_target_;
232
233 // Before using the destination: true if the true target is the
234 // preferred fall through, false if the false target is. After
235 // using the destination: true if the true target was actually used
236 // as the fall through, false if the false target was.
237 bool true_is_fall_through_;
238
239 // True if the Split or Goto functions have been called.
240 bool is_used_;
241};
242
243
244// -------------------------------------------------------------------------
245// Code generation state
246
247// The state is passed down the AST by the code generator (and back up, in
248// the form of the state of the jump target pair). It is threaded through
249// the call stack. Constructing a state implicitly pushes it on the owning
250// code generator's stack of states, and destroying one implicitly pops it.
251//
252// The code generator state is only used for expressions, so statements have
253// the initial state.
254
255class CodeGenState BASE_EMBEDDED {
256 public:
257 // Create an initial code generator state. Destroying the initial state
258 // leaves the code generator with a NULL state.
259 explicit CodeGenState(CodeGenerator* owner);
260
261 // Create a code generator state based on a code generator's current
Steve Blockd0582a62009-12-15 09:54:21 +0000262 // state. The new state has its own control destination.
263 CodeGenState(CodeGenerator* owner, ControlDestination* destination);
Steve Blocka7e24c12009-10-30 11:49:00 +0000264
265 // Destroy a code generator state and restore the owning code generator's
266 // previous state.
267 ~CodeGenState();
268
269 // Accessors for the state.
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 ControlDestination* destination() const { return destination_; }
271
272 private:
273 // The owning code generator.
274 CodeGenerator* owner_;
275
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 // A control destination in case the expression has a control-flow
277 // effect.
278 ControlDestination* destination_;
279
280 // The previous state of the owning code generator, restored when
281 // this state is destroyed.
282 CodeGenState* previous_;
283};
284
285
286// -------------------------------------------------------------------------
287// Arguments allocation mode
288
289enum ArgumentsAllocationMode {
290 NO_ARGUMENTS_ALLOCATION,
291 EAGER_ARGUMENTS_ALLOCATION,
292 LAZY_ARGUMENTS_ALLOCATION
293};
294
295
296// -------------------------------------------------------------------------
297// CodeGenerator
298
299class CodeGenerator: public AstVisitor {
300 public:
301 // Takes a function literal, generates code for it. This function should only
302 // be called by compiler.cc.
Andrei Popescu31002712010-02-23 13:46:05 +0000303 static Handle<Code> MakeCode(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304
Steve Block3ce2e202009-11-05 08:53:23 +0000305 // Printing of AST, etc. as requested by flags.
Andrei Popescu31002712010-02-23 13:46:05 +0000306 static void MakeCodePrologue(CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000307
308 // Allocate and install the code.
Andrei Popescu31002712010-02-23 13:46:05 +0000309 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +0000310 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +0000311 CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313#ifdef ENABLE_LOGGING_AND_PROFILING
314 static bool ShouldGenerateLog(Expression* type);
315#endif
316
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100317 static bool RecordPositions(MacroAssembler* masm,
318 int pos,
319 bool right_here = false);
Steve Block3ce2e202009-11-05 08:53:23 +0000320
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 // Accessors
322 MacroAssembler* masm() { return masm_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 VirtualFrame* frame() const { return frame_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000324 inline Handle<Script> script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000325
326 bool has_valid_frame() const { return frame_ != NULL; }
327
328 // Set the virtual frame to be new_frame, with non-frame register
329 // reference counts given by non_frame_registers. The non-frame
330 // register reference counts of the old frame are returned in
331 // non_frame_registers.
332 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
333
334 void DeleteFrame();
335
336 RegisterAllocator* allocator() const { return allocator_; }
337
338 CodeGenState* state() { return state_; }
339 void set_state(CodeGenState* state) { state_ = state; }
340
341 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
342
343 bool in_spilled_code() const { return in_spilled_code_; }
344 void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
345
Steve Block6ded16b2010-05-10 14:33:55 +0100346 // If the name is an inline runtime function call return the number of
347 // expected arguments. Otherwise return -1.
348 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
349
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 private:
351 // Construction/Destruction
Andrei Popescu31002712010-02-23 13:46:05 +0000352 explicit CodeGenerator(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000353
354 // Accessors
Andrei Popescu31002712010-02-23 13:46:05 +0000355 inline bool is_eval();
Steve Block6ded16b2010-05-10 14:33:55 +0100356 inline Scope* scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000357
358 // Generating deferred code.
359 void ProcessDeferred();
360
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 // State
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 ControlDestination* destination() const { return state_->destination(); }
363
364 // Track loop nesting level.
365 int loop_nesting() const { return loop_nesting_; }
366 void IncrementLoopNesting() { loop_nesting_++; }
367 void DecrementLoopNesting() { loop_nesting_--; }
368
369
370 // Node visitors.
371 void VisitStatements(ZoneList<Statement*>* statements);
372
373#define DEF_VISIT(type) \
374 void Visit##type(type* node);
375 AST_NODE_LIST(DEF_VISIT)
376#undef DEF_VISIT
377
378 // Visit a statement and then spill the virtual frame if control flow can
379 // reach the end of the statement (ie, it does not exit via break,
380 // continue, return, or throw). This function is used temporarily while
381 // the code generator is being transformed.
382 void VisitAndSpill(Statement* statement);
383
384 // Visit a list of statements and then spill the virtual frame if control
385 // flow can reach the end of the list.
386 void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
387
388 // Main code generation function
Andrei Popescu402d9372010-02-26 13:31:12 +0000389 void Generate(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390
391 // Generate the return sequence code. Should be called no more than
392 // once per compiled function, immediately after binding the return
393 // target (which can not be done more than once).
394 void GenerateReturnSequence(Result* return_value);
395
Steve Block8defd9f2010-07-08 12:39:36 +0100396 // Generate code for a fast smi loop.
397 void GenerateFastSmiLoop(ForStatement* node);
398
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 // Returns the arguments allocation mode.
Andrei Popescu31002712010-02-23 13:46:05 +0000400 ArgumentsAllocationMode ArgumentsMode();
Steve Blocka7e24c12009-10-30 11:49:00 +0000401
402 // Store the arguments object and allocate it if necessary.
403 Result StoreArgumentsObject(bool initial);
404
405 // The following are used by class Reference.
406 void LoadReference(Reference* ref);
407 void UnloadReference(Reference* ref);
408
Steve Block3ce2e202009-11-05 08:53:23 +0000409 static Operand ContextOperand(Register context, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000410 return Operand(context, Context::SlotOffset(index));
411 }
412
413 Operand SlotOperand(Slot* slot, Register tmp);
414
415 Operand ContextSlotOperandCheckExtensions(Slot* slot,
416 Result tmp,
417 JumpTarget* slow);
418
419 // Expressions
Steve Block3ce2e202009-11-05 08:53:23 +0000420 static Operand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 return ContextOperand(rsi, Context::GLOBAL_INDEX);
422 }
423
424 void LoadCondition(Expression* x,
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 ControlDestination* destination,
426 bool force_control);
Steve Blockd0582a62009-12-15 09:54:21 +0000427 void Load(Expression* expr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 void LoadGlobal();
429 void LoadGlobalReceiver();
430
431 // Generate code to push the value of an expression on top of the frame
432 // and then spill the frame fully to memory. This function is used
433 // temporarily while the code generator is being transformed.
Steve Blockd0582a62009-12-15 09:54:21 +0000434 void LoadAndSpill(Expression* expression);
Steve Blocka7e24c12009-10-30 11:49:00 +0000435
436 // Read a value from a slot and leave it on top of the expression stack.
437 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
438 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
439 Result LoadFromGlobalSlotCheckExtensions(Slot* slot,
440 TypeofState typeof_state,
441 JumpTarget* slow);
442
Kristian Monsen25f61362010-05-21 11:50:48 +0100443 // Support for loading from local/global variables and arguments
444 // whose location is known unless they are shadowed by
445 // eval-introduced bindings. Generates no code for unsupported slot
446 // types and therefore expects to fall through to the slow jump target.
447 void EmitDynamicLoadFromSlotFastCase(Slot* slot,
448 TypeofState typeof_state,
449 Result* result,
450 JumpTarget* slow,
451 JumpTarget* done);
452
Steve Blocka7e24c12009-10-30 11:49:00 +0000453 // Store the value on top of the expression stack into a slot, leaving the
454 // value in place.
455 void StoreToSlot(Slot* slot, InitState init_state);
456
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100457 // Support for compiling assignment expressions.
458 void EmitSlotAssignment(Assignment* node);
459 void EmitNamedPropertyAssignment(Assignment* node);
460 void EmitKeyedPropertyAssignment(Assignment* node);
461
Leon Clarkef7060e22010-06-03 12:02:55 +0100462 // Receiver is passed on the frame and not consumed.
463 Result EmitNamedLoad(Handle<String> name, bool is_contextual);
464
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100465 // If the store is contextual, value is passed on the frame and consumed.
466 // Otherwise, receiver and value are passed on the frame and consumed.
467 Result EmitNamedStore(Handle<String> name, bool is_contextual);
468
Leon Clarked91b9f72010-01-27 17:25:45 +0000469 // Load a property of an object, returning it in a Result.
470 // The object and the property name are passed on the stack, and
471 // not changed.
Leon Clarkef7060e22010-06-03 12:02:55 +0100472 Result EmitKeyedLoad();
Leon Clarked91b9f72010-01-27 17:25:45 +0000473
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100474 // Receiver, key, and value are passed on the frame and consumed.
475 Result EmitKeyedStore(StaticType* key_type);
476
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 // Special code for typeof expressions: Unfortunately, we must
478 // be careful when loading the expression in 'typeof'
479 // expressions. We are not allowed to throw reference errors for
480 // non-existing properties of the global object, so we must make it
481 // look like an explicit property access, instead of an access
482 // through the context chain.
483 void LoadTypeofExpression(Expression* x);
484
485 // Translate the value on top of the frame into control flow to the
486 // control destination.
487 void ToBoolean(ControlDestination* destination);
488
Steve Block6ded16b2010-05-10 14:33:55 +0100489 // Generate code that computes a shortcutting logical operation.
490 void GenerateLogicalBooleanOperation(BinaryOperation* node);
491
492 void GenericBinaryOperation(BinaryOperation* expr,
493 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000494
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100495 // Emits code sequence that jumps to a JumpTarget if the inputs
496 // are both smis. Cannot be in MacroAssembler because it takes
497 // advantage of TypeInfo to skip unneeded checks.
498 void JumpIfBothSmiUsingTypeInfo(Result* left,
499 Result* right,
500 JumpTarget* both_smi);
501
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100502 // Emits code sequence that jumps to deferred code if the input
503 // is not a smi. Cannot be in MacroAssembler because it takes
504 // advantage of TypeInfo to skip unneeded checks.
505 void JumpIfNotSmiUsingTypeInfo(Register reg,
506 TypeInfo type,
507 DeferredCode* deferred);
508
509 // Emits code sequence that jumps to deferred code if the inputs
510 // are not both smis. Cannot be in MacroAssembler because it takes
511 // advantage of TypeInfo to skip unneeded checks.
512 void JumpIfNotBothSmiUsingTypeInfo(Register left,
513 Register right,
514 TypeInfo left_info,
515 TypeInfo right_info,
516 DeferredCode* deferred);
517
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 // If possible, combine two constant smi values using op to produce
519 // a smi result, and push it on the virtual frame, all at compile time.
520 // Returns true if it succeeds. Otherwise it has no effect.
521 bool FoldConstantSmis(Token::Value op, int left, int right);
522
523 // Emit code to perform a binary operation on a constant
524 // smi and a likely smi. Consumes the Result *operand.
Steve Block6ded16b2010-05-10 14:33:55 +0100525 Result ConstantSmiBinaryOperation(BinaryOperation* expr,
Leon Clarked91b9f72010-01-27 17:25:45 +0000526 Result* operand,
527 Handle<Object> constant_operand,
Leon Clarked91b9f72010-01-27 17:25:45 +0000528 bool reversed,
529 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000530
531 // Emit code to perform a binary operation on two likely smis.
532 // The code to handle smi arguments is produced inline.
533 // Consumes the Results *left and *right.
Steve Block6ded16b2010-05-10 14:33:55 +0100534 Result LikelySmiBinaryOperation(BinaryOperation* expr,
Leon Clarked91b9f72010-01-27 17:25:45 +0000535 Result* left,
536 Result* right,
537 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000538
Andrei Popescu402d9372010-02-26 13:31:12 +0000539 void Comparison(AstNode* node,
540 Condition cc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 bool strict,
542 ControlDestination* destination);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100543
544 // If at least one of the sides is a constant smi, generate optimized code.
545 void ConstantSmiComparison(Condition cc,
546 bool strict,
547 ControlDestination* destination,
548 Result* left_side,
549 Result* right_side,
550 bool left_side_constant_smi,
551 bool right_side_constant_smi,
552 bool is_loop_condition);
553
Steve Block6ded16b2010-05-10 14:33:55 +0100554 void GenerateInlineNumberComparison(Result* left_side,
555 Result* right_side,
556 Condition cc,
557 ControlDestination* dest);
Steve Blocka7e24c12009-10-30 11:49:00 +0000558
559 // To prevent long attacker-controlled byte sequences, integer constants
560 // from the JavaScript source are loaded in two parts if they are larger
561 // than 16 bits.
562 static const int kMaxSmiInlinedBits = 16;
563 bool IsUnsafeSmi(Handle<Object> value);
564 // Load an integer constant x into a register target using
565 // at most 16 bits of user-controlled data per assembly operation.
566 void LoadUnsafeSmi(Register target, Handle<Object> value);
567
Leon Clarkee46be812010-01-19 14:06:41 +0000568 void CallWithArguments(ZoneList<Expression*>* arguments,
569 CallFunctionFlags flags,
570 int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000571
Leon Clarked91b9f72010-01-27 17:25:45 +0000572 // An optimized implementation of expressions of the form
573 // x.apply(y, arguments). We call x the applicand and y the receiver.
574 // The optimization avoids allocating an arguments object if possible.
575 void CallApplyLazy(Expression* applicand,
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 Expression* receiver,
577 VariableProxy* arguments,
578 int position);
579
580 void CheckStack();
581
582 struct InlineRuntimeLUT {
583 void (CodeGenerator::*method)(ZoneList<Expression*>*);
584 const char* name;
Steve Block6ded16b2010-05-10 14:33:55 +0100585 int nargs;
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 };
587 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
588 bool CheckForInlineRuntimeCall(CallRuntime* node);
589 static bool PatchInlineRuntimeEntry(Handle<String> name,
590 const InlineRuntimeLUT& new_entry,
591 InlineRuntimeLUT* old_entry);
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
593
Steve Block3ce2e202009-11-05 08:53:23 +0000594 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000595
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100596 static Handle<Code> ComputeKeyedCallInitialize(int argc, InLoopFlag in_loop);
597
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 // Declare global variables and functions in the given array of
599 // name/value pairs.
600 void DeclareGlobals(Handle<FixedArray> pairs);
601
Steve Block6ded16b2010-05-10 14:33:55 +0100602 // Instantiate the function based on the shared function info.
603 void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000604
605 // Support for type checks.
606 void GenerateIsSmi(ZoneList<Expression*>* args);
607 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
608 void GenerateIsArray(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000609 void GenerateIsRegExp(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000610 void GenerateIsObject(ZoneList<Expression*>* args);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100611 void GenerateIsSpecObject(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000612 void GenerateIsFunction(ZoneList<Expression*>* args);
Leon Clarked91b9f72010-01-27 17:25:45 +0000613 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000614
615 // Support for construct call checks.
616 void GenerateIsConstructCall(ZoneList<Expression*>* args);
617
618 // Support for arguments.length and arguments[?].
619 void GenerateArgumentsLength(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100620 void GenerateArguments(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000621
622 // Support for accessing the class and value fields of an object.
623 void GenerateClassOf(ZoneList<Expression*>* args);
624 void GenerateValueOf(ZoneList<Expression*>* args);
625 void GenerateSetValueOf(ZoneList<Expression*>* args);
626
627 // Fast support for charCodeAt(n).
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100628 void GenerateStringCharCodeAt(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000629
Steve Block6ded16b2010-05-10 14:33:55 +0100630 // Fast support for string.charAt(n) and string[n].
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100631 void GenerateStringCharFromCode(ZoneList<Expression*>* args);
632
633 // Fast support for string.charAt(n) and string[n].
634 void GenerateStringCharAt(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100635
Steve Blocka7e24c12009-10-30 11:49:00 +0000636 // Fast support for object equality testing.
637 void GenerateObjectEquals(ZoneList<Expression*>* args);
638
639 void GenerateLog(ZoneList<Expression*>* args);
640
641 void GenerateGetFramePointer(ZoneList<Expression*>* args);
642
643 // Fast support for Math.random().
Steve Block6ded16b2010-05-10 14:33:55 +0100644 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000645
Steve Blockd0582a62009-12-15 09:54:21 +0000646 // Fast support for StringAdd.
647 void GenerateStringAdd(ZoneList<Expression*>* args);
648
Leon Clarkee46be812010-01-19 14:06:41 +0000649 // Fast support for SubString.
650 void GenerateSubString(ZoneList<Expression*>* args);
651
652 // Fast support for StringCompare.
653 void GenerateStringCompare(ZoneList<Expression*>* args);
654
655 // Support for direct calls from JavaScript to native RegExp code.
656 void GenerateRegExpExec(ZoneList<Expression*>* args);
657
Steve Block6ded16b2010-05-10 14:33:55 +0100658 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
659
660 // Support for fast native caches.
661 void GenerateGetFromCache(ZoneList<Expression*>* args);
662
Andrei Popescu402d9372010-02-26 13:31:12 +0000663 // Fast support for number to string.
664 void GenerateNumberToString(ZoneList<Expression*>* args);
665
Steve Block6ded16b2010-05-10 14:33:55 +0100666 // Fast swapping of elements. Takes three expressions, the object and two
667 // indices. This should only be used if the indices are known to be
668 // non-negative and within bounds of the elements array at the call site.
669 void GenerateSwapElements(ZoneList<Expression*>* args);
670
671 // Fast call for custom callbacks.
672 void GenerateCallFunction(ZoneList<Expression*>* args);
673
Andrei Popescu402d9372010-02-26 13:31:12 +0000674 // Fast call to math functions.
Steve Block6ded16b2010-05-10 14:33:55 +0100675 void GenerateMathPow(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000676 void GenerateMathSin(ZoneList<Expression*>* args);
677 void GenerateMathCos(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100678 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000679
680// Simple condition analysis.
Steve Block3ce2e202009-11-05 08:53:23 +0000681 enum ConditionAnalysis {
682 ALWAYS_TRUE,
683 ALWAYS_FALSE,
684 DONT_KNOW
685 };
686 ConditionAnalysis AnalyzeCondition(Expression* cond);
687
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 // Methods used to indicate which source code is generated for. Source
689 // positions are collected by the assembler and emitted with the relocation
690 // information.
691 void CodeForFunctionPosition(FunctionLiteral* fun);
692 void CodeForReturnPosition(FunctionLiteral* fun);
693 void CodeForStatementPosition(Statement* node);
Steve Blockd0582a62009-12-15 09:54:21 +0000694 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000695 void CodeForSourcePosition(int pos);
696
Steve Block6ded16b2010-05-10 14:33:55 +0100697 void SetTypeForStackSlot(Slot* slot, TypeInfo info);
698
Steve Blocka7e24c12009-10-30 11:49:00 +0000699#ifdef DEBUG
700 // True if the registers are valid for entry to a block. There should
701 // be no frame-external references to (non-reserved) registers.
702 bool HasValidEntryRegisters();
703#endif
704
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 ZoneList<DeferredCode*> deferred_;
706
707 // Assembler
708 MacroAssembler* masm_; // to generate code
709
Andrei Popescu31002712010-02-23 13:46:05 +0000710 CompilationInfo* info_;
711
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 VirtualFrame* frame_;
714 RegisterAllocator* allocator_;
715 CodeGenState* state_;
716 int loop_nesting_;
717
718 // Jump targets.
719 // The target of the return from the function.
720 BreakTarget function_return_;
721
722 // True if the function return is shadowed (ie, jumping to the target
723 // function_return_ does not jump to the true function return, but rather
724 // to some unlinking code).
725 bool function_return_is_shadowed_;
726
727 // True when we are in code that expects the virtual frame to be fully
728 // spilled. Some virtual frame function are disabled in DEBUG builds when
729 // called from spilled code, because they do not leave the virtual frame
730 // in a spilled state.
731 bool in_spilled_code_;
732
733 static InlineRuntimeLUT kInlineRuntimeLUT[];
734
735 friend class VirtualFrame;
736 friend class JumpTarget;
737 friend class Reference;
738 friend class Result;
Leon Clarke4515c472010-02-03 11:58:03 +0000739 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000740 friend class FullCodeGenerator;
741 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000742
743 friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc
744
745 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
746};
747
748
Steve Block6ded16b2010-05-10 14:33:55 +0100749// Compute a transcendental math function natively, or call the
750// TranscendentalCache runtime function.
751class TranscendentalCacheStub: public CodeStub {
752 public:
753 explicit TranscendentalCacheStub(TranscendentalCache::Type type)
754 : type_(type) {}
755 void Generate(MacroAssembler* masm);
756 private:
757 TranscendentalCache::Type type_;
758 Major MajorKey() { return TranscendentalCache; }
759 int MinorKey() { return type_; }
760 Runtime::FunctionId RuntimeFunction();
761 void GenerateOperation(MacroAssembler* masm, Label* on_nan_result);
762};
763
764
Steve Blockd0582a62009-12-15 09:54:21 +0000765// Flag that indicates how to generate code for the stub GenericBinaryOpStub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000766enum GenericBinaryFlags {
Steve Blockd0582a62009-12-15 09:54:21 +0000767 NO_GENERIC_BINARY_FLAGS = 0,
768 NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000769};
770
771
772class GenericBinaryOpStub: public CodeStub {
773 public:
774 GenericBinaryOpStub(Token::Value op,
775 OverwriteMode mode,
Andrei Popescu402d9372010-02-26 13:31:12 +0000776 GenericBinaryFlags flags,
Steve Block6ded16b2010-05-10 14:33:55 +0100777 TypeInfo operands_type = TypeInfo::Unknown())
Steve Blockd0582a62009-12-15 09:54:21 +0000778 : op_(op),
779 mode_(mode),
780 flags_(flags),
781 args_in_registers_(false),
Leon Clarkee46be812010-01-19 14:06:41 +0000782 args_reversed_(false),
Steve Block6ded16b2010-05-10 14:33:55 +0100783 static_operands_type_(operands_type),
784 runtime_operands_type_(BinaryOpIC::DEFAULT),
785 name_(NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000786 ASSERT(OpBits::is_valid(Token::NUM_TOKENS));
787 }
788
Steve Block6ded16b2010-05-10 14:33:55 +0100789 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info)
790 : op_(OpBits::decode(key)),
791 mode_(ModeBits::decode(key)),
792 flags_(FlagBits::decode(key)),
793 args_in_registers_(ArgsInRegistersBits::decode(key)),
794 args_reversed_(ArgsReversedBits::decode(key)),
Steve Block6ded16b2010-05-10 14:33:55 +0100795 static_operands_type_(TypeInfo::ExpandedRepresentation(
796 StaticTypeInfoBits::decode(key))),
797 runtime_operands_type_(type_info),
798 name_(NULL) {
799 }
800
Steve Blockd0582a62009-12-15 09:54:21 +0000801 // Generate code to call the stub with the supplied arguments. This will add
802 // code at the call site to prepare arguments either in registers or on the
803 // stack together with the actual call.
804 void GenerateCall(MacroAssembler* masm, Register left, Register right);
805 void GenerateCall(MacroAssembler* masm, Register left, Smi* right);
806 void GenerateCall(MacroAssembler* masm, Smi* left, Register right);
Steve Blocka7e24c12009-10-30 11:49:00 +0000807
Leon Clarke4515c472010-02-03 11:58:03 +0000808 Result GenerateCall(MacroAssembler* masm,
809 VirtualFrame* frame,
810 Result* left,
811 Result* right);
812
Steve Blocka7e24c12009-10-30 11:49:00 +0000813 private:
814 Token::Value op_;
815 OverwriteMode mode_;
816 GenericBinaryFlags flags_;
Steve Blockd0582a62009-12-15 09:54:21 +0000817 bool args_in_registers_; // Arguments passed in registers not on the stack.
818 bool args_reversed_; // Left and right argument are swapped.
Steve Block6ded16b2010-05-10 14:33:55 +0100819
820 // Number type information of operands, determined by code generator.
821 TypeInfo static_operands_type_;
822
823 // Operand type information determined at runtime.
824 BinaryOpIC::TypeInfo runtime_operands_type_;
825
Leon Clarkee46be812010-01-19 14:06:41 +0000826 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000827
828 const char* GetName();
829
830#ifdef DEBUG
831 void Print() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000832 PrintF("GenericBinaryOpStub %d (op %s), "
833 "(mode %d, flags %d, registers %d, reversed %d, only_numbers %s)\n",
834 MinorKey(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 Token::String(op_),
836 static_cast<int>(mode_),
Steve Blockd0582a62009-12-15 09:54:21 +0000837 static_cast<int>(flags_),
838 static_cast<int>(args_in_registers_),
Andrei Popescu402d9372010-02-26 13:31:12 +0000839 static_cast<int>(args_reversed_),
Steve Block6ded16b2010-05-10 14:33:55 +0100840 static_operands_type_.ToString());
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 }
842#endif
843
Kristian Monsen25f61362010-05-21 11:50:48 +0100844 // Minor key encoding in 17 bits TTNNNFRAOOOOOOOMM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000846 class OpBits: public BitField<Token::Value, 2, 7> {};
Kristian Monsen25f61362010-05-21 11:50:48 +0100847 class ArgsInRegistersBits: public BitField<bool, 9, 1> {};
848 class ArgsReversedBits: public BitField<bool, 10, 1> {};
849 class FlagBits: public BitField<GenericBinaryFlags, 11, 1> {};
850 class StaticTypeInfoBits: public BitField<int, 12, 3> {};
851 class RuntimeTypeInfoBits: public BitField<BinaryOpIC::TypeInfo, 15, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000852
853 Major MajorKey() { return GenericBinaryOp; }
854 int MinorKey() {
Steve Block6ded16b2010-05-10 14:33:55 +0100855 // Encode the parameters in a unique 18 bit value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 return OpBits::encode(op_)
Steve Blockd0582a62009-12-15 09:54:21 +0000857 | ModeBits::encode(mode_)
858 | FlagBits::encode(flags_)
Steve Blockd0582a62009-12-15 09:54:21 +0000859 | ArgsInRegistersBits::encode(args_in_registers_)
Andrei Popescu402d9372010-02-26 13:31:12 +0000860 | ArgsReversedBits::encode(args_reversed_)
Steve Block6ded16b2010-05-10 14:33:55 +0100861 | StaticTypeInfoBits::encode(
862 static_operands_type_.ThreeBitRepresentation())
863 | RuntimeTypeInfoBits::encode(runtime_operands_type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000864 }
Steve Blockd0582a62009-12-15 09:54:21 +0000865
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 void Generate(MacroAssembler* masm);
Steve Blockd0582a62009-12-15 09:54:21 +0000867 void GenerateSmiCode(MacroAssembler* masm, Label* slow);
868 void GenerateLoadArguments(MacroAssembler* masm);
869 void GenerateReturn(MacroAssembler* masm);
Steve Block6ded16b2010-05-10 14:33:55 +0100870 void GenerateRegisterArgsPush(MacroAssembler* masm);
871 void GenerateTypeTransition(MacroAssembler* masm);
Steve Blockd0582a62009-12-15 09:54:21 +0000872
873 bool ArgsInRegistersSupported() {
Leon Clarke4515c472010-02-03 11:58:03 +0000874 return (op_ == Token::ADD) || (op_ == Token::SUB)
875 || (op_ == Token::MUL) || (op_ == Token::DIV);
Steve Blockd0582a62009-12-15 09:54:21 +0000876 }
877 bool IsOperationCommutative() {
878 return (op_ == Token::ADD) || (op_ == Token::MUL);
879 }
880
881 void SetArgsInRegisters() { args_in_registers_ = true; }
882 void SetArgsReversed() { args_reversed_ = true; }
883 bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; }
Leon Clarke4515c472010-02-03 11:58:03 +0000884 bool HasArgsInRegisters() { return args_in_registers_; }
885 bool HasArgsReversed() { return args_reversed_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100886
887 bool ShouldGenerateSmiCode() {
888 return HasSmiCodeInStub() &&
889 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
890 runtime_operands_type_ != BinaryOpIC::STRINGS;
891 }
892
893 bool ShouldGenerateFPCode() {
894 return runtime_operands_type_ != BinaryOpIC::STRINGS;
895 }
896
897 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
898
899 virtual InlineCacheState GetICState() {
900 return BinaryOpIC::ToState(runtime_operands_type_);
901 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000902};
903
Steve Block6ded16b2010-05-10 14:33:55 +0100904class StringHelper : public AllStatic {
Leon Clarked91b9f72010-01-27 17:25:45 +0000905 public:
906 // Generate code for copying characters using a simple loop. This should only
907 // be used in places where the number of characters is small and the
908 // additional setup and checking in GenerateCopyCharactersREP adds too much
909 // overhead. Copying of overlapping regions is not supported.
Steve Block6ded16b2010-05-10 14:33:55 +0100910 static void GenerateCopyCharacters(MacroAssembler* masm,
911 Register dest,
912 Register src,
913 Register count,
914 bool ascii);
Leon Clarked91b9f72010-01-27 17:25:45 +0000915
916 // Generate code for copying characters using the rep movs instruction.
917 // Copies rcx characters from rsi to rdi. Copying of overlapping regions is
918 // not supported.
Steve Block6ded16b2010-05-10 14:33:55 +0100919 static void GenerateCopyCharactersREP(MacroAssembler* masm,
920 Register dest, // Must be rdi.
921 Register src, // Must be rsi.
922 Register count, // Must be rcx.
923 bool ascii);
924
925
926 // Probe the symbol table for a two character string. If the string is
927 // not found by probing a jump to the label not_found is performed. This jump
928 // does not guarantee that the string is not in the symbol table. If the
929 // string is found the code falls through with the string in register rax.
930 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
931 Register c1,
932 Register c2,
933 Register scratch1,
934 Register scratch2,
935 Register scratch3,
936 Register scratch4,
937 Label* not_found);
938
939 // Generate string hash.
940 static void GenerateHashInit(MacroAssembler* masm,
941 Register hash,
942 Register character,
943 Register scratch);
944 static void GenerateHashAddCharacter(MacroAssembler* masm,
945 Register hash,
946 Register character,
947 Register scratch);
948 static void GenerateHashGetHash(MacroAssembler* masm,
949 Register hash,
950 Register scratch);
951
952 private:
953 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
Leon Clarked91b9f72010-01-27 17:25:45 +0000954};
955
956
Leon Clarkee46be812010-01-19 14:06:41 +0000957// Flag that indicates how to generate code for the stub StringAddStub.
958enum StringAddFlags {
959 NO_STRING_ADD_FLAGS = 0,
960 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
961};
962
963
Steve Block6ded16b2010-05-10 14:33:55 +0100964class StringAddStub: public CodeStub {
Leon Clarkee46be812010-01-19 14:06:41 +0000965 public:
966 explicit StringAddStub(StringAddFlags flags) {
967 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
968 }
969
970 private:
971 Major MajorKey() { return StringAdd; }
972 int MinorKey() { return string_check_ ? 0 : 1; }
973
974 void Generate(MacroAssembler* masm);
975
Leon Clarkee46be812010-01-19 14:06:41 +0000976 // Should the stub check whether arguments are strings?
977 bool string_check_;
978};
979
980
Steve Block6ded16b2010-05-10 14:33:55 +0100981class SubStringStub: public CodeStub {
Leon Clarked91b9f72010-01-27 17:25:45 +0000982 public:
983 SubStringStub() {}
984
985 private:
986 Major MajorKey() { return SubString; }
987 int MinorKey() { return 0; }
988
989 void Generate(MacroAssembler* masm);
990};
991
992
Leon Clarkee46be812010-01-19 14:06:41 +0000993class StringCompareStub: public CodeStub {
994 public:
995 explicit StringCompareStub() {}
996
997 // Compare two flat ascii strings and returns result in rax after popping two
998 // arguments from the stack.
999 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
1000 Register left,
1001 Register right,
1002 Register scratch1,
1003 Register scratch2,
1004 Register scratch3,
1005 Register scratch4);
1006
1007 private:
1008 Major MajorKey() { return StringCompare; }
1009 int MinorKey() { return 0; }
1010
1011 void Generate(MacroAssembler* masm);
1012};
1013
1014
Steve Block6ded16b2010-05-10 14:33:55 +01001015class NumberToStringStub: public CodeStub {
1016 public:
1017 NumberToStringStub() { }
1018
1019 // Generate code to do a lookup in the number string cache. If the number in
1020 // the register object is found in the cache the generated code falls through
1021 // with the result in the result register. The object and the result register
1022 // can be the same. If the number is not found in the cache the code jumps to
1023 // the label not_found with only the content of register object unchanged.
1024 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
1025 Register object,
1026 Register result,
1027 Register scratch1,
1028 Register scratch2,
1029 bool object_is_smi,
1030 Label* not_found);
1031
1032 private:
1033 static void GenerateConvertHashCodeToIndex(MacroAssembler* masm,
1034 Register hash,
1035 Register mask);
1036
1037 Major MajorKey() { return NumberToString; }
1038 int MinorKey() { return 0; }
1039
1040 void Generate(MacroAssembler* masm);
1041
1042 const char* GetName() { return "NumberToStringStub"; }
1043
1044#ifdef DEBUG
1045 void Print() {
1046 PrintF("NumberToStringStub\n");
1047 }
1048#endif
1049};
1050
1051
1052class RecordWriteStub : public CodeStub {
1053 public:
1054 RecordWriteStub(Register object, Register addr, Register scratch)
1055 : object_(object), addr_(addr), scratch_(scratch) { }
1056
1057 void Generate(MacroAssembler* masm);
1058
1059 private:
1060 Register object_;
1061 Register addr_;
1062 Register scratch_;
1063
1064#ifdef DEBUG
1065 void Print() {
1066 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
1067 object_.code(), addr_.code(), scratch_.code());
1068 }
1069#endif
1070
1071 // Minor key encoding in 12 bits. 4 bits for each of the three
1072 // registers (object, address and scratch) OOOOAAAASSSS.
1073 class ScratchBits : public BitField<uint32_t, 0, 4> {};
1074 class AddressBits : public BitField<uint32_t, 4, 4> {};
1075 class ObjectBits : public BitField<uint32_t, 8, 4> {};
1076
1077 Major MajorKey() { return RecordWrite; }
1078
1079 int MinorKey() {
1080 // Encode the registers.
1081 return ObjectBits::encode(object_.code()) |
1082 AddressBits::encode(addr_.code()) |
1083 ScratchBits::encode(scratch_.code());
1084 }
1085};
1086
1087
Steve Blocka7e24c12009-10-30 11:49:00 +00001088} } // namespace v8::internal
1089
1090#endif // V8_X64_CODEGEN_X64_H_