blob: 0d3fee5925f86b8678ac52663212c7172aa006f9 [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_IA32_CODEGEN_IA32_H_
29#define V8_IA32_CODEGEN_IA32_H_
30
Steve Block6ded16b2010-05-10 14:33:55 +010031#include "ic-inl.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
36// Forward declarations
Leon Clarke4515c472010-02-03 11:58:03 +000037class CompilationInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +000038class DeferredCode;
39class RegisterAllocator;
40class RegisterFile;
41
42enum InitState { CONST_INIT, NOT_CONST_INIT };
43enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
44
45
46// -------------------------------------------------------------------------
47// Reference support
48
Leon Clarked91b9f72010-01-27 17:25:45 +000049// A reference is a C++ stack-allocated object that puts a
50// reference on the virtual frame. The reference may be consumed
51// by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
52// When the lifetime (scope) of a valid reference ends, it must have
53// been consumed, and be in state UNLOADED.
Steve Blocka7e24c12009-10-30 11:49:00 +000054class Reference BASE_EMBEDDED {
55 public:
56 // The values of the types is important, see size().
Leon Clarked91b9f72010-01-27 17:25:45 +000057 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
58 Reference(CodeGenerator* cgen,
59 Expression* expression,
60 bool persist_after_get = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000061 ~Reference();
62
63 Expression* expression() const { return expression_; }
64 Type type() const { return type_; }
65 void set_type(Type value) {
Leon Clarked91b9f72010-01-27 17:25:45 +000066 ASSERT_EQ(ILLEGAL, type_);
Steve Blocka7e24c12009-10-30 11:49:00 +000067 type_ = value;
68 }
69
Leon Clarked91b9f72010-01-27 17:25:45 +000070 void set_unloaded() {
71 ASSERT_NE(ILLEGAL, type_);
72 ASSERT_NE(UNLOADED, type_);
73 type_ = UNLOADED;
74 }
Steve Blocka7e24c12009-10-30 11:49:00 +000075 // The size the reference takes up on the stack.
Leon Clarked91b9f72010-01-27 17:25:45 +000076 int size() const {
77 return (type_ < SLOT) ? 0 : type_;
78 }
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80 bool is_illegal() const { return type_ == ILLEGAL; }
81 bool is_slot() const { return type_ == SLOT; }
82 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
Leon Clarked91b9f72010-01-27 17:25:45 +000083 bool is_unloaded() const { return type_ == UNLOADED; }
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85 // Return the name. Only valid for named property references.
86 Handle<String> GetName();
87
88 // Generate code to push the value of the reference on top of the
89 // expression stack. The reference is expected to be already on top of
Leon Clarked91b9f72010-01-27 17:25:45 +000090 // the expression stack, and it is consumed by the call unless the
91 // reference is for a compound assignment.
92 // If the reference is not consumed, it is left in place under its value.
Steve Blockd0582a62009-12-15 09:54:21 +000093 void GetValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000094
95 // Like GetValue except that the slot is expected to be written to before
Leon Clarked91b9f72010-01-27 17:25:45 +000096 // being read from again. The value of the reference may be invalidated,
Steve Blocka7e24c12009-10-30 11:49:00 +000097 // causing subsequent attempts to read it to fail.
Steve Blockd0582a62009-12-15 09:54:21 +000098 void TakeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000099
100 // Generate code to store the value on top of the expression stack in the
101 // reference. The reference is expected to be immediately below the value
Leon Clarked91b9f72010-01-27 17:25:45 +0000102 // on the expression stack. The value is stored in the location specified
103 // by the reference, and is left on top of the stack, after the reference
104 // is popped from beneath it (unloaded).
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 void SetValue(InitState init_state);
106
107 private:
108 CodeGenerator* cgen_;
109 Expression* expression_;
110 Type type_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000111 // Keep the reference on the stack after get, so it can be used by set later.
112 bool persist_after_get_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000113};
114
115
116// -------------------------------------------------------------------------
117// Control destinations.
118
119// A control destination encapsulates a pair of jump targets and a
120// flag indicating which one is the preferred fall-through. The
121// preferred fall-through must be unbound, the other may be already
122// bound (ie, a backward target).
123//
124// The true and false targets may be jumped to unconditionally or
125// control may split conditionally. Unconditional jumping and
126// splitting should be emitted in tail position (as the last thing
127// when compiling an expression) because they can cause either label
128// to be bound or the non-fall through to be jumped to leaving an
129// invalid virtual frame.
130//
131// The labels in the control destination can be extracted and
132// manipulated normally without affecting the state of the
133// destination.
134
135class ControlDestination BASE_EMBEDDED {
136 public:
137 ControlDestination(JumpTarget* true_target,
138 JumpTarget* false_target,
139 bool true_is_fall_through)
140 : true_target_(true_target),
141 false_target_(false_target),
142 true_is_fall_through_(true_is_fall_through),
143 is_used_(false) {
144 ASSERT(true_is_fall_through ? !true_target->is_bound()
145 : !false_target->is_bound());
146 }
147
148 // Accessors for the jump targets. Directly jumping or branching to
149 // or binding the targets will not update the destination's state.
150 JumpTarget* true_target() const { return true_target_; }
151 JumpTarget* false_target() const { return false_target_; }
152
153 // True if the the destination has been jumped to unconditionally or
154 // control has been split to both targets. This predicate does not
155 // test whether the targets have been extracted and manipulated as
156 // raw jump targets.
157 bool is_used() const { return is_used_; }
158
159 // True if the destination is used and the true target (respectively
160 // false target) was the fall through. If the target is backward,
161 // "fall through" included jumping unconditionally to it.
162 bool true_was_fall_through() const {
163 return is_used_ && true_is_fall_through_;
164 }
165
166 bool false_was_fall_through() const {
167 return is_used_ && !true_is_fall_through_;
168 }
169
170 // Emit a branch to one of the true or false targets, and bind the
171 // other target. Because this binds the fall-through target, it
172 // should be emitted in tail position (as the last thing when
173 // compiling an expression).
174 void Split(Condition cc) {
175 ASSERT(!is_used_);
176 if (true_is_fall_through_) {
177 false_target_->Branch(NegateCondition(cc));
178 true_target_->Bind();
179 } else {
180 true_target_->Branch(cc);
181 false_target_->Bind();
182 }
183 is_used_ = true;
184 }
185
186 // Emit an unconditional jump in tail position, to the true target
187 // (if the argument is true) or the false target. The "jump" will
188 // actually bind the jump target if it is forward, jump to it if it
189 // is backward.
190 void Goto(bool where) {
191 ASSERT(!is_used_);
192 JumpTarget* target = where ? true_target_ : false_target_;
193 if (target->is_bound()) {
194 target->Jump();
195 } else {
196 target->Bind();
197 }
198 is_used_ = true;
199 true_is_fall_through_ = where;
200 }
201
202 // Mark this jump target as used as if Goto had been called, but
203 // without generating a jump or binding a label (the control effect
204 // should have already happened). This is used when the left
205 // subexpression of the short-circuit boolean operators are
206 // compiled.
207 void Use(bool where) {
208 ASSERT(!is_used_);
209 ASSERT((where ? true_target_ : false_target_)->is_bound());
210 is_used_ = true;
211 true_is_fall_through_ = where;
212 }
213
214 // Swap the true and false targets but keep the same actual label as
215 // the fall through. This is used when compiling negated
216 // expressions, where we want to swap the targets but preserve the
217 // state.
218 void Invert() {
219 JumpTarget* temp_target = true_target_;
220 true_target_ = false_target_;
221 false_target_ = temp_target;
222
223 true_is_fall_through_ = !true_is_fall_through_;
224 }
225
226 private:
227 // True and false jump targets.
228 JumpTarget* true_target_;
229 JumpTarget* false_target_;
230
231 // Before using the destination: true if the true target is the
232 // preferred fall through, false if the false target is. After
233 // using the destination: true if the true target was actually used
234 // as the fall through, false if the false target was.
235 bool true_is_fall_through_;
236
237 // True if the Split or Goto functions have been called.
238 bool is_used_;
239};
240
241
242// -------------------------------------------------------------------------
243// Code generation state
244
245// The state is passed down the AST by the code generator (and back up, in
246// the form of the state of the jump target pair). It is threaded through
247// the call stack. Constructing a state implicitly pushes it on the owning
248// code generator's stack of states, and destroying one implicitly pops it.
249//
250// The code generator state is only used for expressions, so statements have
251// the initial state.
252
253class CodeGenState BASE_EMBEDDED {
254 public:
255 // Create an initial code generator state. Destroying the initial state
256 // leaves the code generator with a NULL state.
257 explicit CodeGenState(CodeGenerator* owner);
258
259 // Create a code generator state based on a code generator's current
Steve Blockd0582a62009-12-15 09:54:21 +0000260 // state. The new state has its own control destination.
261 CodeGenState(CodeGenerator* owner, ControlDestination* destination);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262
263 // Destroy a code generator state and restore the owning code generator's
264 // previous state.
265 ~CodeGenState();
266
267 // Accessors for the state.
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 ControlDestination* destination() const { return destination_; }
269
270 private:
271 // The owning code generator.
272 CodeGenerator* owner_;
273
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 // A control destination in case the expression has a control-flow
275 // effect.
276 ControlDestination* destination_;
277
278 // The previous state of the owning code generator, restored when
279 // this state is destroyed.
280 CodeGenState* previous_;
281};
282
283
284// -------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000285// Arguments allocation mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287enum ArgumentsAllocationMode {
288 NO_ARGUMENTS_ALLOCATION,
289 EAGER_ARGUMENTS_ALLOCATION,
290 LAZY_ARGUMENTS_ALLOCATION
291};
292
293
294// -------------------------------------------------------------------------
295// CodeGenerator
296
297class CodeGenerator: public AstVisitor {
298 public:
299 // Takes a function literal, generates code for it. This function should only
300 // be called by compiler.cc.
Andrei Popescu31002712010-02-23 13:46:05 +0000301 static Handle<Code> MakeCode(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000302
Steve Block3ce2e202009-11-05 08:53:23 +0000303 // Printing of AST, etc. as requested by flags.
Andrei Popescu31002712010-02-23 13:46:05 +0000304 static void MakeCodePrologue(CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000305
306 // Allocate and install the code.
Andrei Popescu31002712010-02-23 13:46:05 +0000307 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +0000308 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +0000309 CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000310
Steve Blocka7e24c12009-10-30 11:49:00 +0000311#ifdef ENABLE_LOGGING_AND_PROFILING
312 static bool ShouldGenerateLog(Expression* type);
313#endif
314
Steve Block3ce2e202009-11-05 08:53:23 +0000315 static void RecordPositions(MacroAssembler* masm, int pos);
316
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 // Accessors
318 MacroAssembler* masm() { return masm_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 VirtualFrame* frame() const { return frame_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000320 inline Handle<Script> script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000321
322 bool has_valid_frame() const { return frame_ != NULL; }
323
324 // Set the virtual frame to be new_frame, with non-frame register
325 // reference counts given by non_frame_registers. The non-frame
326 // register reference counts of the old frame are returned in
327 // non_frame_registers.
328 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
329
330 void DeleteFrame();
331
332 RegisterAllocator* allocator() const { return allocator_; }
333
334 CodeGenState* state() { return state_; }
335 void set_state(CodeGenState* state) { state_ = state; }
336
337 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
338
339 bool in_spilled_code() const { return in_spilled_code_; }
340 void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
341
Steve Block6ded16b2010-05-10 14:33:55 +0100342 // If the name is an inline runtime function call return the number of
343 // expected arguments. Otherwise return -1.
344 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
345
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 private:
347 // Construction/Destruction
Andrei Popescu31002712010-02-23 13:46:05 +0000348 explicit CodeGenerator(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000349
350 // Accessors
Andrei Popescu31002712010-02-23 13:46:05 +0000351 inline bool is_eval();
Steve Block6ded16b2010-05-10 14:33:55 +0100352 inline Scope* scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000353
354 // Generating deferred code.
355 void ProcessDeferred();
356
357 // State
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 ControlDestination* destination() const { return state_->destination(); }
359
Steve Block6ded16b2010-05-10 14:33:55 +0100360 // Control of side-effect-free int32 expression compilation.
361 bool in_safe_int32_mode() { return in_safe_int32_mode_; }
362 void set_in_safe_int32_mode(bool value) { in_safe_int32_mode_ = value; }
363 bool safe_int32_mode_enabled() {
364 return FLAG_safe_int32_compiler && safe_int32_mode_enabled_;
365 }
366 void set_safe_int32_mode_enabled(bool value) {
367 safe_int32_mode_enabled_ = value;
368 }
369 void set_unsafe_bailout(BreakTarget* unsafe_bailout) {
370 unsafe_bailout_ = unsafe_bailout;
371 }
372
373 // Take the Result that is an untagged int32, and convert it to a tagged
374 // Smi or HeapNumber. Remove the untagged_int32 flag from the result.
375 void ConvertInt32ResultToNumber(Result* value);
376 void ConvertInt32ResultToSmi(Result* value);
377
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 // Track loop nesting level.
379 int loop_nesting() const { return loop_nesting_; }
380 void IncrementLoopNesting() { loop_nesting_++; }
381 void DecrementLoopNesting() { loop_nesting_--; }
382
383 // Node visitors.
384 void VisitStatements(ZoneList<Statement*>* statements);
385
386#define DEF_VISIT(type) \
387 void Visit##type(type* node);
388 AST_NODE_LIST(DEF_VISIT)
389#undef DEF_VISIT
390
391 // Visit a statement and then spill the virtual frame if control flow can
392 // reach the end of the statement (ie, it does not exit via break,
393 // continue, return, or throw). This function is used temporarily while
394 // the code generator is being transformed.
395 void VisitAndSpill(Statement* statement);
396
397 // Visit a list of statements and then spill the virtual frame if control
398 // flow can reach the end of the list.
399 void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
400
401 // Main code generation function
Andrei Popescu402d9372010-02-26 13:31:12 +0000402 void Generate(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000403
404 // Generate the return sequence code. Should be called no more than
405 // once per compiled function, immediately after binding the return
406 // target (which can not be done more than once).
407 void GenerateReturnSequence(Result* return_value);
408
409 // Returns the arguments allocation mode.
Andrei Popescu31002712010-02-23 13:46:05 +0000410 ArgumentsAllocationMode ArgumentsMode();
Steve Blocka7e24c12009-10-30 11:49:00 +0000411
412 // Store the arguments object and allocate it if necessary.
413 Result StoreArgumentsObject(bool initial);
414
415 // The following are used by class Reference.
416 void LoadReference(Reference* ref);
417 void UnloadReference(Reference* ref);
418
Steve Block3ce2e202009-11-05 08:53:23 +0000419 static Operand ContextOperand(Register context, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 return Operand(context, Context::SlotOffset(index));
421 }
422
423 Operand SlotOperand(Slot* slot, Register tmp);
424
425 Operand ContextSlotOperandCheckExtensions(Slot* slot,
426 Result tmp,
427 JumpTarget* slow);
428
429 // Expressions
Steve Block3ce2e202009-11-05 08:53:23 +0000430 static Operand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 return ContextOperand(esi, Context::GLOBAL_INDEX);
432 }
433
Steve Block6ded16b2010-05-10 14:33:55 +0100434 void LoadCondition(Expression* expr,
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 ControlDestination* destination,
436 bool force_control);
Steve Blockd0582a62009-12-15 09:54:21 +0000437 void Load(Expression* expr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 void LoadGlobal();
439 void LoadGlobalReceiver();
440
441 // Generate code to push the value of an expression on top of the frame
442 // and then spill the frame fully to memory. This function is used
443 // temporarily while the code generator is being transformed.
Steve Blockd0582a62009-12-15 09:54:21 +0000444 void LoadAndSpill(Expression* expression);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
Steve Block6ded16b2010-05-10 14:33:55 +0100446 // Evaluate an expression and place its value on top of the frame,
447 // using, or not using, the side-effect-free expression compiler.
448 void LoadInSafeInt32Mode(Expression* expr, BreakTarget* unsafe_bailout);
449 void LoadWithSafeInt32ModeDisabled(Expression* expr);
450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 // Read a value from a slot and leave it on top of the expression stack.
Andrei Popescu402d9372010-02-26 13:31:12 +0000452 Result LoadFromSlot(Slot* slot, TypeofState typeof_state);
453 Result LoadFromSlotCheckForArguments(Slot* slot, TypeofState typeof_state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 Result LoadFromGlobalSlotCheckExtensions(Slot* slot,
455 TypeofState typeof_state,
456 JumpTarget* slow);
457
458 // Store the value on top of the expression stack into a slot, leaving the
459 // value in place.
460 void StoreToSlot(Slot* slot, InitState init_state);
461
Andrei Popescu402d9372010-02-26 13:31:12 +0000462 // Support for compiling assignment expressions.
463 void EmitSlotAssignment(Assignment* node);
464 void EmitNamedPropertyAssignment(Assignment* node);
465 void EmitKeyedPropertyAssignment(Assignment* node);
466
467 // Receiver is passed on the frame and consumed.
468 Result EmitNamedLoad(Handle<String> name, bool is_contextual);
469
470 // If the store is contextual, value is passed on the frame and consumed.
471 // Otherwise, receiver and value are passed on the frame and consumed.
472 Result EmitNamedStore(Handle<String> name, bool is_contextual);
473
474 // Receiver and key are passed on the frame and consumed.
475 Result EmitKeyedLoad();
476
477 // Receiver, key, and value are passed on the frame and consumed.
478 Result EmitKeyedStore(StaticType* key_type);
Leon Clarked91b9f72010-01-27 17:25:45 +0000479
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 // Special code for typeof expressions: Unfortunately, we must
481 // be careful when loading the expression in 'typeof'
482 // expressions. We are not allowed to throw reference errors for
483 // non-existing properties of the global object, so we must make it
484 // look like an explicit property access, instead of an access
485 // through the context chain.
486 void LoadTypeofExpression(Expression* x);
487
488 // Translate the value on top of the frame into control flow to the
489 // control destination.
490 void ToBoolean(ControlDestination* destination);
491
Steve Block6ded16b2010-05-10 14:33:55 +0100492 // Generate code that computes a shortcutting logical operation.
493 void GenerateLogicalBooleanOperation(BinaryOperation* node);
494
495 void GenericBinaryOperation(BinaryOperation* expr,
496 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
498 // If possible, combine two constant smi values using op to produce
499 // a smi result, and push it on the virtual frame, all at compile time.
500 // Returns true if it succeeds. Otherwise it has no effect.
501 bool FoldConstantSmis(Token::Value op, int left, int right);
502
503 // Emit code to perform a binary operation on a constant
Steve Block6ded16b2010-05-10 14:33:55 +0100504 // smi and a likely smi. Consumes the Result operand.
505 Result ConstantSmiBinaryOperation(BinaryOperation* expr,
Leon Clarked91b9f72010-01-27 17:25:45 +0000506 Result* operand,
507 Handle<Object> constant_operand,
Leon Clarked91b9f72010-01-27 17:25:45 +0000508 bool reversed,
509 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
511 // Emit code to perform a binary operation on two likely smis.
512 // The code to handle smi arguments is produced inline.
Steve Block6ded16b2010-05-10 14:33:55 +0100513 // Consumes the Results left and right.
514 Result LikelySmiBinaryOperation(BinaryOperation* expr,
Leon Clarked91b9f72010-01-27 17:25:45 +0000515 Result* left,
516 Result* right,
517 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000518
Steve Block6ded16b2010-05-10 14:33:55 +0100519
520 // Emit code to perform a binary operation on two untagged int32 values.
521 // The values are on top of the frame, and the result is pushed on the frame.
522 void Int32BinaryOperation(BinaryOperation* node);
523
524
Leon Clarkee46be812010-01-19 14:06:41 +0000525 void Comparison(AstNode* node,
526 Condition cc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 bool strict,
528 ControlDestination* destination);
Steve Block6ded16b2010-05-10 14:33:55 +0100529 void GenerateInlineNumberComparison(Result* left_side,
530 Result* right_side,
531 Condition cc,
532 ControlDestination* dest);
Steve Blocka7e24c12009-10-30 11:49:00 +0000533
534 // To prevent long attacker-controlled byte sequences, integer constants
535 // from the JavaScript source are loaded in two parts if they are larger
Steve Block6ded16b2010-05-10 14:33:55 +0100536 // than 17 bits.
537 static const int kMaxSmiInlinedBits = 17;
Steve Blocka7e24c12009-10-30 11:49:00 +0000538 bool IsUnsafeSmi(Handle<Object> value);
Steve Blockd0582a62009-12-15 09:54:21 +0000539 // Load an integer constant x into a register target or into the stack using
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 // at most 16 bits of user-controlled data per assembly operation.
Steve Blockd0582a62009-12-15 09:54:21 +0000541 void MoveUnsafeSmi(Register target, Handle<Object> value);
542 void StoreUnsafeSmiToLocal(int offset, Handle<Object> value);
543 void PushUnsafeSmi(Handle<Object> value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000544
Leon Clarkee46be812010-01-19 14:06:41 +0000545 void CallWithArguments(ZoneList<Expression*>* arguments,
546 CallFunctionFlags flags,
547 int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000548
Leon Clarked91b9f72010-01-27 17:25:45 +0000549 // An optimized implementation of expressions of the form
550 // x.apply(y, arguments). We call x the applicand and y the receiver.
551 // The optimization avoids allocating an arguments object if possible.
552 void CallApplyLazy(Expression* applicand,
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 Expression* receiver,
554 VariableProxy* arguments,
555 int position);
556
557 void CheckStack();
558
559 struct InlineRuntimeLUT {
560 void (CodeGenerator::*method)(ZoneList<Expression*>*);
561 const char* name;
Steve Block6ded16b2010-05-10 14:33:55 +0100562 int nargs;
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 };
564
565 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
566 bool CheckForInlineRuntimeCall(CallRuntime* node);
567 static bool PatchInlineRuntimeEntry(Handle<String> name,
568 const InlineRuntimeLUT& new_entry,
569 InlineRuntimeLUT* old_entry);
570
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
572
Steve Block3ce2e202009-11-05 08:53:23 +0000573 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
575 // Declare global variables and functions in the given array of
576 // name/value pairs.
577 void DeclareGlobals(Handle<FixedArray> pairs);
578
Steve Block6ded16b2010-05-10 14:33:55 +0100579 // Instantiate the function based on the shared function info.
580 Result InstantiateFunction(Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000581
582 // Support for type checks.
583 void GenerateIsSmi(ZoneList<Expression*>* args);
584 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
585 void GenerateIsArray(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000586 void GenerateIsRegExp(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000587 void GenerateIsObject(ZoneList<Expression*>* args);
588 void GenerateIsFunction(ZoneList<Expression*>* args);
Leon Clarked91b9f72010-01-27 17:25:45 +0000589 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000590
591 // Support for construct call checks.
592 void GenerateIsConstructCall(ZoneList<Expression*>* args);
593
594 // Support for arguments.length and arguments[?].
595 void GenerateArgumentsLength(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100596 void GenerateArguments(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000597
598 // Support for accessing the class and value fields of an object.
599 void GenerateClassOf(ZoneList<Expression*>* args);
600 void GenerateValueOf(ZoneList<Expression*>* args);
601 void GenerateSetValueOf(ZoneList<Expression*>* args);
602
603 // Fast support for charCodeAt(n).
604 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
605
Steve Block6ded16b2010-05-10 14:33:55 +0100606 // Fast support for string.charAt(n) and string[n].
607 void GenerateCharFromCode(ZoneList<Expression*>* args);
608
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 // Fast support for object equality testing.
610 void GenerateObjectEquals(ZoneList<Expression*>* args);
611
612 void GenerateLog(ZoneList<Expression*>* args);
613
614 void GenerateGetFramePointer(ZoneList<Expression*>* args);
615
616 // Fast support for Math.random().
Steve Block6ded16b2010-05-10 14:33:55 +0100617 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000618
Steve Blockd0582a62009-12-15 09:54:21 +0000619 // Fast support for StringAdd.
620 void GenerateStringAdd(ZoneList<Expression*>* args);
621
Leon Clarkee46be812010-01-19 14:06:41 +0000622 // Fast support for SubString.
623 void GenerateSubString(ZoneList<Expression*>* args);
624
625 // Fast support for StringCompare.
626 void GenerateStringCompare(ZoneList<Expression*>* args);
627
628 // Support for direct calls from JavaScript to native RegExp code.
629 void GenerateRegExpExec(ZoneList<Expression*>* args);
630
Steve Block6ded16b2010-05-10 14:33:55 +0100631 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
632
633 // Support for fast native caches.
634 void GenerateGetFromCache(ZoneList<Expression*>* args);
635
Andrei Popescu402d9372010-02-26 13:31:12 +0000636 // Fast support for number to string.
637 void GenerateNumberToString(ZoneList<Expression*>* args);
638
Steve Block6ded16b2010-05-10 14:33:55 +0100639 // Fast swapping of elements. Takes three expressions, the object and two
640 // indices. This should only be used if the indices are known to be
641 // non-negative and within bounds of the elements array at the call site.
642 void GenerateSwapElements(ZoneList<Expression*>* args);
643
644 // Fast call for custom callbacks.
645 void GenerateCallFunction(ZoneList<Expression*>* args);
646
647 // Fast call to math functions.
648 void GenerateMathPow(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000649 void GenerateMathSin(ZoneList<Expression*>* args);
650 void GenerateMathCos(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100651 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000652
Steve Block3ce2e202009-11-05 08:53:23 +0000653 // Simple condition analysis.
654 enum ConditionAnalysis {
655 ALWAYS_TRUE,
656 ALWAYS_FALSE,
657 DONT_KNOW
658 };
659 ConditionAnalysis AnalyzeCondition(Expression* cond);
660
Steve Blocka7e24c12009-10-30 11:49:00 +0000661 // Methods used to indicate which source code is generated for. Source
662 // positions are collected by the assembler and emitted with the relocation
663 // information.
664 void CodeForFunctionPosition(FunctionLiteral* fun);
665 void CodeForReturnPosition(FunctionLiteral* fun);
666 void CodeForStatementPosition(Statement* stmt);
Steve Blockd0582a62009-12-15 09:54:21 +0000667 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 void CodeForSourcePosition(int pos);
669
Steve Block6ded16b2010-05-10 14:33:55 +0100670 void SetTypeForStackSlot(Slot* slot, TypeInfo info);
671
Steve Blocka7e24c12009-10-30 11:49:00 +0000672#ifdef DEBUG
673 // True if the registers are valid for entry to a block. There should
674 // be no frame-external references to (non-reserved) registers.
675 bool HasValidEntryRegisters();
676#endif
677
Steve Blocka7e24c12009-10-30 11:49:00 +0000678 ZoneList<DeferredCode*> deferred_;
679
680 // Assembler
681 MacroAssembler* masm_; // to generate code
682
Andrei Popescu31002712010-02-23 13:46:05 +0000683 CompilationInfo* info_;
684
Steve Blocka7e24c12009-10-30 11:49:00 +0000685 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000686 VirtualFrame* frame_;
687 RegisterAllocator* allocator_;
688 CodeGenState* state_;
689 int loop_nesting_;
Steve Block6ded16b2010-05-10 14:33:55 +0100690 bool in_safe_int32_mode_;
691 bool safe_int32_mode_enabled_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000692
693 // Jump targets.
694 // The target of the return from the function.
695 BreakTarget function_return_;
Steve Block6ded16b2010-05-10 14:33:55 +0100696 // The target of the bailout from a side-effect-free int32 subexpression.
697 BreakTarget* unsafe_bailout_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000698
699 // True if the function return is shadowed (ie, jumping to the target
700 // function_return_ does not jump to the true function return, but rather
701 // to some unlinking code).
702 bool function_return_is_shadowed_;
703
704 // True when we are in code that expects the virtual frame to be fully
705 // spilled. Some virtual frame function are disabled in DEBUG builds when
706 // called from spilled code, because they do not leave the virtual frame
707 // in a spilled state.
708 bool in_spilled_code_;
709
710 static InlineRuntimeLUT kInlineRuntimeLUT[];
711
712 friend class VirtualFrame;
713 friend class JumpTarget;
714 friend class Reference;
715 friend class Result;
Leon Clarke4515c472010-02-03 11:58:03 +0000716 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000717 friend class FullCodeGenerator;
718 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000719
720 friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc
721
722 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
723};
724
725
Andrei Popescu402d9372010-02-26 13:31:12 +0000726// Compute a transcendental math function natively, or call the
727// TranscendentalCache runtime function.
728class TranscendentalCacheStub: public CodeStub {
729 public:
730 explicit TranscendentalCacheStub(TranscendentalCache::Type type)
731 : type_(type) {}
732 void Generate(MacroAssembler* masm);
733 private:
734 TranscendentalCache::Type type_;
735 Major MajorKey() { return TranscendentalCache; }
736 int MinorKey() { return type_; }
737 Runtime::FunctionId RuntimeFunction();
738 void GenerateOperation(MacroAssembler* masm);
739};
740
741
Steve Blockd0582a62009-12-15 09:54:21 +0000742// Flag that indicates how to generate code for the stub GenericBinaryOpStub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000743enum GenericBinaryFlags {
Steve Block3ce2e202009-11-05 08:53:23 +0000744 NO_GENERIC_BINARY_FLAGS = 0,
745 NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000746};
747
748
749class GenericBinaryOpStub: public CodeStub {
750 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000751 GenericBinaryOpStub(Token::Value op,
Steve Blocka7e24c12009-10-30 11:49:00 +0000752 OverwriteMode mode,
Andrei Popescu402d9372010-02-26 13:31:12 +0000753 GenericBinaryFlags flags,
Steve Block6ded16b2010-05-10 14:33:55 +0100754 TypeInfo operands_type)
Steve Blockd0582a62009-12-15 09:54:21 +0000755 : op_(op),
Steve Block3ce2e202009-11-05 08:53:23 +0000756 mode_(mode),
757 flags_(flags),
758 args_in_registers_(false),
Leon Clarkee46be812010-01-19 14:06:41 +0000759 args_reversed_(false),
Steve Block6ded16b2010-05-10 14:33:55 +0100760 static_operands_type_(operands_type),
761 runtime_operands_type_(BinaryOpIC::DEFAULT),
762 name_(NULL) {
763 if (static_operands_type_.IsSmi()) {
764 mode_ = NO_OVERWRITE;
765 }
Steve Blockd0582a62009-12-15 09:54:21 +0000766 use_sse3_ = CpuFeatures::IsSupported(SSE3);
Steve Blocka7e24c12009-10-30 11:49:00 +0000767 ASSERT(OpBits::is_valid(Token::NUM_TOKENS));
768 }
769
Steve Block6ded16b2010-05-10 14:33:55 +0100770 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo runtime_operands_type)
771 : op_(OpBits::decode(key)),
772 mode_(ModeBits::decode(key)),
773 flags_(FlagBits::decode(key)),
774 args_in_registers_(ArgsInRegistersBits::decode(key)),
775 args_reversed_(ArgsReversedBits::decode(key)),
776 use_sse3_(SSE3Bits::decode(key)),
777 static_operands_type_(TypeInfo::ExpandedRepresentation(
778 StaticTypeInfoBits::decode(key))),
779 runtime_operands_type_(runtime_operands_type),
780 name_(NULL) {
781 }
782
Steve Block3ce2e202009-11-05 08:53:23 +0000783 // Generate code to call the stub with the supplied arguments. This will add
784 // code at the call site to prepare arguments either in registers or on the
785 // stack together with the actual call.
786 void GenerateCall(MacroAssembler* masm, Register left, Register right);
787 void GenerateCall(MacroAssembler* masm, Register left, Smi* right);
788 void GenerateCall(MacroAssembler* masm, Smi* left, Register right);
Steve Blocka7e24c12009-10-30 11:49:00 +0000789
Leon Clarked91b9f72010-01-27 17:25:45 +0000790 Result GenerateCall(MacroAssembler* masm,
791 VirtualFrame* frame,
792 Result* left,
793 Result* right);
794
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 private:
796 Token::Value op_;
797 OverwriteMode mode_;
798 GenericBinaryFlags flags_;
Steve Block3ce2e202009-11-05 08:53:23 +0000799 bool args_in_registers_; // Arguments passed in registers not on the stack.
800 bool args_reversed_; // Left and right argument are swapped.
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 bool use_sse3_;
Steve Block6ded16b2010-05-10 14:33:55 +0100802
803 // Number type information of operands, determined by code generator.
804 TypeInfo static_operands_type_;
805
806 // Operand type information determined at runtime.
807 BinaryOpIC::TypeInfo runtime_operands_type_;
808
Leon Clarkee46be812010-01-19 14:06:41 +0000809 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000810
811 const char* GetName();
812
813#ifdef DEBUG
814 void Print() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000815 PrintF("GenericBinaryOpStub %d (op %s), "
Steve Block6ded16b2010-05-10 14:33:55 +0100816 "(mode %d, flags %d, registers %d, reversed %d, type_info %s)\n",
Andrei Popescu402d9372010-02-26 13:31:12 +0000817 MinorKey(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000818 Token::String(op_),
819 static_cast<int>(mode_),
Steve Block3ce2e202009-11-05 08:53:23 +0000820 static_cast<int>(flags_),
821 static_cast<int>(args_in_registers_),
Andrei Popescu402d9372010-02-26 13:31:12 +0000822 static_cast<int>(args_reversed_),
Steve Block6ded16b2010-05-10 14:33:55 +0100823 static_operands_type_.ToString());
Steve Blocka7e24c12009-10-30 11:49:00 +0000824 }
825#endif
826
Steve Block6ded16b2010-05-10 14:33:55 +0100827 // Minor key encoding in 18 bits RRNNNFRASOOOOOOOMM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000829 class OpBits: public BitField<Token::Value, 2, 7> {};
830 class SSE3Bits: public BitField<bool, 9, 1> {};
831 class ArgsInRegistersBits: public BitField<bool, 10, 1> {};
832 class ArgsReversedBits: public BitField<bool, 11, 1> {};
833 class FlagBits: public BitField<GenericBinaryFlags, 12, 1> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100834 class StaticTypeInfoBits: public BitField<int, 13, 3> {};
835 class RuntimeTypeInfoBits: public BitField<BinaryOpIC::TypeInfo, 16, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000836
837 Major MajorKey() { return GenericBinaryOp; }
838 int MinorKey() {
Steve Block6ded16b2010-05-10 14:33:55 +0100839 // Encode the parameters in a unique 18 bit value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000840 return OpBits::encode(op_)
841 | ModeBits::encode(mode_)
842 | FlagBits::encode(flags_)
Steve Block3ce2e202009-11-05 08:53:23 +0000843 | SSE3Bits::encode(use_sse3_)
844 | ArgsInRegistersBits::encode(args_in_registers_)
Andrei Popescu402d9372010-02-26 13:31:12 +0000845 | ArgsReversedBits::encode(args_reversed_)
Steve Block6ded16b2010-05-10 14:33:55 +0100846 | StaticTypeInfoBits::encode(
847 static_operands_type_.ThreeBitRepresentation())
848 | RuntimeTypeInfoBits::encode(runtime_operands_type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000849 }
Steve Block3ce2e202009-11-05 08:53:23 +0000850
Steve Blocka7e24c12009-10-30 11:49:00 +0000851 void Generate(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000852 void GenerateSmiCode(MacroAssembler* masm, Label* slow);
853 void GenerateLoadArguments(MacroAssembler* masm);
854 void GenerateReturn(MacroAssembler* masm);
Leon Clarked91b9f72010-01-27 17:25:45 +0000855 void GenerateHeapResultAllocation(MacroAssembler* masm, Label* alloc_failure);
Steve Block6ded16b2010-05-10 14:33:55 +0100856 void GenerateRegisterArgsPush(MacroAssembler* masm);
857 void GenerateTypeTransition(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000858
859 bool ArgsInRegistersSupported() {
Leon Clarked91b9f72010-01-27 17:25:45 +0000860 return op_ == Token::ADD || op_ == Token::SUB
861 || op_ == Token::MUL || op_ == Token::DIV;
Steve Block3ce2e202009-11-05 08:53:23 +0000862 }
863 bool IsOperationCommutative() {
864 return (op_ == Token::ADD) || (op_ == Token::MUL);
865 }
866
867 void SetArgsInRegisters() { args_in_registers_ = true; }
868 void SetArgsReversed() { args_reversed_ = true; }
869 bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; }
Leon Clarked91b9f72010-01-27 17:25:45 +0000870 bool HasArgsInRegisters() { return args_in_registers_; }
871 bool HasArgsReversed() { return args_reversed_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100872
873 bool ShouldGenerateSmiCode() {
874 return HasSmiCodeInStub() &&
875 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
876 runtime_operands_type_ != BinaryOpIC::STRINGS;
877 }
878
879 bool ShouldGenerateFPCode() {
880 return runtime_operands_type_ != BinaryOpIC::STRINGS;
881 }
882
883 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
884
885 virtual InlineCacheState GetICState() {
886 return BinaryOpIC::ToState(runtime_operands_type_);
887 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000888};
889
890
Steve Block6ded16b2010-05-10 14:33:55 +0100891class StringHelper : public AllStatic {
Leon Clarkee46be812010-01-19 14:06:41 +0000892 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100893 // Generates fast code for getting a char code out of a string
894 // object at the given index. May bail out for four reasons (in the
895 // listed order):
896 // * Receiver is not a string (receiver_not_string label).
897 // * Index is not a smi (index_not_smi label).
898 // * Index is out of range (index_out_of_range).
899 // * Some other reason (slow_case label). In this case it's
900 // guaranteed that the above conditions are not violated,
901 // e.g. it's safe to assume the receiver is a string and the
902 // index is a non-negative smi < length.
903 // When successful, object, index, and scratch are clobbered.
904 // Otherwise, scratch and result are clobbered.
905 static void GenerateFastCharCodeAt(MacroAssembler* masm,
906 Register object,
907 Register index,
908 Register scratch,
909 Register result,
910 Label* receiver_not_string,
911 Label* index_not_smi,
912 Label* index_out_of_range,
913 Label* slow_case);
914
915 // Generates code for creating a one-char string from the given char
916 // code. May do a runtime call, so any register can be clobbered
917 // and, if the given invoke flag specifies a call, an internal frame
918 // is required. In tail call mode the result must be eax register.
919 static void GenerateCharFromCode(MacroAssembler* masm,
920 Register code,
921 Register result,
922 InvokeFlag flag);
923
Leon Clarkee46be812010-01-19 14:06:41 +0000924 // Generate code for copying characters using a simple loop. This should only
925 // be used in places where the number of characters is small and the
926 // additional setup and checking in GenerateCopyCharactersREP adds too much
927 // overhead. Copying of overlapping regions is not supported.
Steve Block6ded16b2010-05-10 14:33:55 +0100928 static void GenerateCopyCharacters(MacroAssembler* masm,
929 Register dest,
930 Register src,
931 Register count,
932 Register scratch,
933 bool ascii);
Leon Clarkee46be812010-01-19 14:06:41 +0000934
935 // Generate code for copying characters using the rep movs instruction.
936 // Copies ecx characters from esi to edi. Copying of overlapping regions is
937 // not supported.
Steve Block6ded16b2010-05-10 14:33:55 +0100938 static void GenerateCopyCharactersREP(MacroAssembler* masm,
939 Register dest, // Must be edi.
940 Register src, // Must be esi.
941 Register count, // Must be ecx.
942 Register scratch, // Neither of above.
943 bool ascii);
Andrei Popescu402d9372010-02-26 13:31:12 +0000944
945 // Probe the symbol table for a two character string. If the string is
946 // not found by probing a jump to the label not_found is performed. This jump
947 // does not guarantee that the string is not in the symbol table. If the
948 // string is found the code falls through with the string in register eax.
Steve Block6ded16b2010-05-10 14:33:55 +0100949 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
950 Register c1,
951 Register c2,
952 Register scratch1,
953 Register scratch2,
954 Register scratch3,
955 Label* not_found);
Andrei Popescu402d9372010-02-26 13:31:12 +0000956
957 // Generate string hash.
Steve Block6ded16b2010-05-10 14:33:55 +0100958 static void GenerateHashInit(MacroAssembler* masm,
959 Register hash,
960 Register character,
961 Register scratch);
962 static void GenerateHashAddCharacter(MacroAssembler* masm,
963 Register hash,
964 Register character,
965 Register scratch);
966 static void GenerateHashGetHash(MacroAssembler* masm,
967 Register hash,
968 Register scratch);
969
970 private:
971 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
Leon Clarkee46be812010-01-19 14:06:41 +0000972};
973
974
Andrei Popescu31002712010-02-23 13:46:05 +0000975// Flag that indicates how to generate code for the stub StringAddStub.
976enum StringAddFlags {
977 NO_STRING_ADD_FLAGS = 0,
978 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
979};
980
981
Steve Block6ded16b2010-05-10 14:33:55 +0100982class StringAddStub: public CodeStub {
Steve Blockd0582a62009-12-15 09:54:21 +0000983 public:
984 explicit StringAddStub(StringAddFlags flags) {
985 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
986 }
987
988 private:
989 Major MajorKey() { return StringAdd; }
990 int MinorKey() { return string_check_ ? 0 : 1; }
991
992 void Generate(MacroAssembler* masm);
993
Steve Blockd0582a62009-12-15 09:54:21 +0000994 // Should the stub check whether arguments are strings?
995 bool string_check_;
996};
997
998
Steve Block6ded16b2010-05-10 14:33:55 +0100999class SubStringStub: public CodeStub {
Leon Clarkee46be812010-01-19 14:06:41 +00001000 public:
1001 SubStringStub() {}
1002
1003 private:
1004 Major MajorKey() { return SubString; }
1005 int MinorKey() { return 0; }
1006
1007 void Generate(MacroAssembler* masm);
1008};
1009
1010
Steve Block6ded16b2010-05-10 14:33:55 +01001011class StringCompareStub: public CodeStub {
Leon Clarkee46be812010-01-19 14:06:41 +00001012 public:
1013 explicit StringCompareStub() {
1014 }
1015
1016 // Compare two flat ascii strings and returns result in eax after popping two
1017 // arguments from the stack.
1018 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
1019 Register left,
1020 Register right,
1021 Register scratch1,
1022 Register scratch2,
1023 Register scratch3);
1024
1025 private:
1026 Major MajorKey() { return StringCompare; }
1027 int MinorKey() { return 0; }
1028
1029 void Generate(MacroAssembler* masm);
1030};
1031
1032
Andrei Popescu402d9372010-02-26 13:31:12 +00001033class NumberToStringStub: public CodeStub {
1034 public:
1035 NumberToStringStub() { }
1036
1037 // Generate code to do a lookup in the number string cache. If the number in
1038 // the register object is found in the cache the generated code falls through
1039 // with the result in the result register. The object and the result register
1040 // can be the same. If the number is not found in the cache the code jumps to
1041 // the label not_found with only the content of register object unchanged.
1042 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
1043 Register object,
1044 Register result,
1045 Register scratch1,
1046 Register scratch2,
1047 bool object_is_smi,
1048 Label* not_found);
1049
1050 private:
1051 Major MajorKey() { return NumberToString; }
1052 int MinorKey() { return 0; }
1053
1054 void Generate(MacroAssembler* masm);
1055
1056 const char* GetName() { return "NumberToStringStub"; }
1057
1058#ifdef DEBUG
1059 void Print() {
1060 PrintF("NumberToStringStub\n");
1061 }
1062#endif
1063};
1064
1065
Steve Block6ded16b2010-05-10 14:33:55 +01001066class RecordWriteStub : public CodeStub {
1067 public:
1068 RecordWriteStub(Register object, Register addr, Register scratch)
1069 : object_(object), addr_(addr), scratch_(scratch) { }
1070
1071 void Generate(MacroAssembler* masm);
1072
1073 private:
1074 Register object_;
1075 Register addr_;
1076 Register scratch_;
1077
1078#ifdef DEBUG
1079 void Print() {
1080 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
1081 object_.code(), addr_.code(), scratch_.code());
1082 }
1083#endif
1084
1085 // Minor key encoding in 12 bits. 4 bits for each of the three
1086 // registers (object, address and scratch) OOOOAAAASSSS.
1087 class ScratchBits: public BitField<uint32_t, 0, 4> {};
1088 class AddressBits: public BitField<uint32_t, 4, 4> {};
1089 class ObjectBits: public BitField<uint32_t, 8, 4> {};
1090
1091 Major MajorKey() { return RecordWrite; }
1092
1093 int MinorKey() {
1094 // Encode the registers.
1095 return ObjectBits::encode(object_.code()) |
1096 AddressBits::encode(addr_.code()) |
1097 ScratchBits::encode(scratch_.code());
1098 }
1099};
1100
1101
Steve Blocka7e24c12009-10-30 11:49:00 +00001102} } // namespace v8::internal
1103
1104#endif // V8_IA32_CODEGEN_IA32_H_