blob: a098dc3859ebe730243e9e7421cf4611fa4d6764 [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
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
Kristian Monsen25f61362010-05-21 11:50:48 +010053// by GetValue, TakeValue and SetValue.
Leon Clarked91b9f72010-01-27 17:25:45 +000054// 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 Reference(CodeGenerator* cgen,
61 Expression* expression,
62 bool persist_after_get = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000063 ~Reference();
64
65 Expression* expression() const { return expression_; }
66 Type type() const { return type_; }
67 void set_type(Type value) {
Leon Clarked91b9f72010-01-27 17:25:45 +000068 ASSERT_EQ(ILLEGAL, type_);
Steve Blocka7e24c12009-10-30 11:49:00 +000069 type_ = value;
70 }
71
Leon Clarked91b9f72010-01-27 17:25:45 +000072 void set_unloaded() {
73 ASSERT_NE(ILLEGAL, type_);
74 ASSERT_NE(UNLOADED, type_);
75 type_ = UNLOADED;
76 }
Steve Blocka7e24c12009-10-30 11:49:00 +000077 // The size the reference takes up on the stack.
Leon Clarked91b9f72010-01-27 17:25:45 +000078 int size() const {
79 return (type_ < SLOT) ? 0 : type_;
80 }
Steve Blocka7e24c12009-10-30 11:49:00 +000081
82 bool is_illegal() const { return type_ == ILLEGAL; }
83 bool is_slot() const { return type_ == SLOT; }
84 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
Leon Clarked91b9f72010-01-27 17:25:45 +000085 bool is_unloaded() const { return type_ == UNLOADED; }
Steve Blocka7e24c12009-10-30 11:49:00 +000086
87 // Return the name. Only valid for named property references.
88 Handle<String> GetName();
89
90 // Generate code to push the value of the reference on top of the
91 // expression stack. The reference is expected to be already on top of
Leon Clarked91b9f72010-01-27 17:25:45 +000092 // the expression stack, and it is consumed by the call unless the
93 // reference is for a compound assignment.
94 // If the reference is not consumed, it is left in place under its value.
Steve Blockd0582a62009-12-15 09:54:21 +000095 void GetValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000096
97 // Like GetValue except that the slot is expected to be written to before
Leon Clarked91b9f72010-01-27 17:25:45 +000098 // being read from again. The value of the reference may be invalidated,
Steve Blocka7e24c12009-10-30 11:49:00 +000099 // causing subsequent attempts to read it to fail.
Steve Blockd0582a62009-12-15 09:54:21 +0000100 void TakeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
102 // Generate code to store the value on top of the expression stack in the
103 // reference. The reference is expected to be immediately below the value
Leon Clarked91b9f72010-01-27 17:25:45 +0000104 // on the expression stack. The value is stored in the location specified
105 // by the reference, and is left on top of the stack, after the reference
106 // is popped from beneath it (unloaded).
Steve Blocka7e24c12009-10-30 11:49:00 +0000107 void SetValue(InitState init_state);
108
109 private:
110 CodeGenerator* cgen_;
111 Expression* expression_;
112 Type type_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000113 // Keep the reference on the stack after get, so it can be used by set later.
114 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// -------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000287// Arguments allocation mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000288
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
Steve Block3ce2e202009-11-05 08:53:23 +0000317 static void RecordPositions(MacroAssembler* masm, int pos);
318
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 // Accessors
320 MacroAssembler* masm() { return masm_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 VirtualFrame* frame() const { return frame_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000322 inline Handle<Script> script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
324 bool has_valid_frame() const { return frame_ != NULL; }
325
326 // Set the virtual frame to be new_frame, with non-frame register
327 // reference counts given by non_frame_registers. The non-frame
328 // register reference counts of the old frame are returned in
329 // non_frame_registers.
330 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
331
332 void DeleteFrame();
333
334 RegisterAllocator* allocator() const { return allocator_; }
335
336 CodeGenState* state() { return state_; }
337 void set_state(CodeGenState* state) { state_ = state; }
338
339 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
340
341 bool in_spilled_code() const { return in_spilled_code_; }
342 void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
343
Steve Block6ded16b2010-05-10 14:33:55 +0100344 // If the name is an inline runtime function call return the number of
345 // expected arguments. Otherwise return -1.
346 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
347
Kristian Monsen25f61362010-05-21 11:50:48 +0100348 // Return a position of the element at |index_as_smi| + |additional_offset|
349 // in FixedArray pointer to which is held in |array|. |index_as_smi| is Smi.
350 static Operand FixedArrayElementOperand(Register array,
351 Register index_as_smi,
352 int additional_offset = 0) {
353 int offset = FixedArray::kHeaderSize + additional_offset * kPointerSize;
354 return FieldOperand(array, index_as_smi, times_half_pointer_size, offset);
355 }
356
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 private:
358 // Construction/Destruction
Andrei Popescu31002712010-02-23 13:46:05 +0000359 explicit CodeGenerator(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360
361 // Accessors
Andrei Popescu31002712010-02-23 13:46:05 +0000362 inline bool is_eval();
Steve Block6ded16b2010-05-10 14:33:55 +0100363 inline Scope* scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000364
365 // Generating deferred code.
366 void ProcessDeferred();
367
368 // State
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 ControlDestination* destination() const { return state_->destination(); }
370
Steve Block6ded16b2010-05-10 14:33:55 +0100371 // Control of side-effect-free int32 expression compilation.
372 bool in_safe_int32_mode() { return in_safe_int32_mode_; }
373 void set_in_safe_int32_mode(bool value) { in_safe_int32_mode_ = value; }
374 bool safe_int32_mode_enabled() {
375 return FLAG_safe_int32_compiler && safe_int32_mode_enabled_;
376 }
377 void set_safe_int32_mode_enabled(bool value) {
378 safe_int32_mode_enabled_ = value;
379 }
380 void set_unsafe_bailout(BreakTarget* unsafe_bailout) {
381 unsafe_bailout_ = unsafe_bailout;
382 }
383
384 // Take the Result that is an untagged int32, and convert it to a tagged
385 // Smi or HeapNumber. Remove the untagged_int32 flag from the result.
386 void ConvertInt32ResultToNumber(Result* value);
387 void ConvertInt32ResultToSmi(Result* value);
388
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 // Track loop nesting level.
390 int loop_nesting() const { return loop_nesting_; }
391 void IncrementLoopNesting() { loop_nesting_++; }
392 void DecrementLoopNesting() { loop_nesting_--; }
393
394 // Node visitors.
395 void VisitStatements(ZoneList<Statement*>* statements);
396
397#define DEF_VISIT(type) \
398 void Visit##type(type* node);
399 AST_NODE_LIST(DEF_VISIT)
400#undef DEF_VISIT
401
402 // Visit a statement and then spill the virtual frame if control flow can
403 // reach the end of the statement (ie, it does not exit via break,
404 // continue, return, or throw). This function is used temporarily while
405 // the code generator is being transformed.
406 void VisitAndSpill(Statement* statement);
407
408 // Visit a list of statements and then spill the virtual frame if control
409 // flow can reach the end of the list.
410 void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
411
412 // Main code generation function
Andrei Popescu402d9372010-02-26 13:31:12 +0000413 void Generate(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000414
415 // Generate the return sequence code. Should be called no more than
416 // once per compiled function, immediately after binding the return
417 // target (which can not be done more than once).
418 void GenerateReturnSequence(Result* return_value);
419
420 // Returns the arguments allocation mode.
Andrei Popescu31002712010-02-23 13:46:05 +0000421 ArgumentsAllocationMode ArgumentsMode();
Steve Blocka7e24c12009-10-30 11:49:00 +0000422
423 // Store the arguments object and allocate it if necessary.
424 Result StoreArgumentsObject(bool initial);
425
426 // The following are used by class Reference.
427 void LoadReference(Reference* ref);
Steve Blocka7e24c12009-10-30 11:49:00 +0000428
Steve Block3ce2e202009-11-05 08:53:23 +0000429 static Operand ContextOperand(Register context, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 return Operand(context, Context::SlotOffset(index));
431 }
432
433 Operand SlotOperand(Slot* slot, Register tmp);
434
435 Operand ContextSlotOperandCheckExtensions(Slot* slot,
436 Result tmp,
437 JumpTarget* slow);
438
439 // Expressions
Steve Block3ce2e202009-11-05 08:53:23 +0000440 static Operand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 return ContextOperand(esi, Context::GLOBAL_INDEX);
442 }
443
Steve Block6ded16b2010-05-10 14:33:55 +0100444 void LoadCondition(Expression* expr,
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 ControlDestination* destination,
446 bool force_control);
Steve Blockd0582a62009-12-15 09:54:21 +0000447 void Load(Expression* expr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 void LoadGlobal();
449 void LoadGlobalReceiver();
450
451 // Generate code to push the value of an expression on top of the frame
452 // and then spill the frame fully to memory. This function is used
453 // temporarily while the code generator is being transformed.
Steve Blockd0582a62009-12-15 09:54:21 +0000454 void LoadAndSpill(Expression* expression);
Steve Blocka7e24c12009-10-30 11:49:00 +0000455
Steve Block6ded16b2010-05-10 14:33:55 +0100456 // Evaluate an expression and place its value on top of the frame,
457 // using, or not using, the side-effect-free expression compiler.
458 void LoadInSafeInt32Mode(Expression* expr, BreakTarget* unsafe_bailout);
459 void LoadWithSafeInt32ModeDisabled(Expression* expr);
460
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 // Read a value from a slot and leave it on top of the expression stack.
Leon Clarkef7060e22010-06-03 12:02:55 +0100462 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
463 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState typeof_state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 Result LoadFromGlobalSlotCheckExtensions(Slot* slot,
465 TypeofState typeof_state,
466 JumpTarget* slow);
467
Kristian Monsen25f61362010-05-21 11:50:48 +0100468 // Support for loading from local/global variables and arguments
469 // whose location is known unless they are shadowed by
470 // eval-introduced bindings. Generates no code for unsupported slot
471 // types and therefore expects to fall through to the slow jump target.
472 void EmitDynamicLoadFromSlotFastCase(Slot* slot,
473 TypeofState typeof_state,
474 Result* result,
475 JumpTarget* slow,
476 JumpTarget* done);
477
Steve Blocka7e24c12009-10-30 11:49:00 +0000478 // Store the value on top of the expression stack into a slot, leaving the
479 // value in place.
480 void StoreToSlot(Slot* slot, InitState init_state);
481
Andrei Popescu402d9372010-02-26 13:31:12 +0000482 // Support for compiling assignment expressions.
483 void EmitSlotAssignment(Assignment* node);
484 void EmitNamedPropertyAssignment(Assignment* node);
485 void EmitKeyedPropertyAssignment(Assignment* node);
486
487 // Receiver is passed on the frame and consumed.
488 Result EmitNamedLoad(Handle<String> name, bool is_contextual);
489
490 // If the store is contextual, value is passed on the frame and consumed.
491 // Otherwise, receiver and value are passed on the frame and consumed.
492 Result EmitNamedStore(Handle<String> name, bool is_contextual);
493
494 // Receiver and key are passed on the frame and consumed.
495 Result EmitKeyedLoad();
496
497 // Receiver, key, and value are passed on the frame and consumed.
498 Result EmitKeyedStore(StaticType* key_type);
Leon Clarked91b9f72010-01-27 17:25:45 +0000499
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 // Special code for typeof expressions: Unfortunately, we must
501 // be careful when loading the expression in 'typeof'
502 // expressions. We are not allowed to throw reference errors for
503 // non-existing properties of the global object, so we must make it
504 // look like an explicit property access, instead of an access
505 // through the context chain.
506 void LoadTypeofExpression(Expression* x);
507
508 // Translate the value on top of the frame into control flow to the
509 // control destination.
510 void ToBoolean(ControlDestination* destination);
511
Steve Block6ded16b2010-05-10 14:33:55 +0100512 // Generate code that computes a shortcutting logical operation.
513 void GenerateLogicalBooleanOperation(BinaryOperation* node);
514
515 void GenericBinaryOperation(BinaryOperation* expr,
516 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000517
518 // 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
Steve Block6ded16b2010-05-10 14:33:55 +0100524 // smi and a likely smi. Consumes the Result operand.
525 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.
Steve Block6ded16b2010-05-10 14:33:55 +0100533 // Consumes the Results left and right.
534 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
Steve Block6ded16b2010-05-10 14:33:55 +0100539
540 // Emit code to perform a binary operation on two untagged int32 values.
541 // The values are on top of the frame, and the result is pushed on the frame.
542 void Int32BinaryOperation(BinaryOperation* node);
543
544
Leon Clarkee46be812010-01-19 14:06:41 +0000545 void Comparison(AstNode* node,
546 Condition cc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000547 bool strict,
548 ControlDestination* destination);
Steve Block6ded16b2010-05-10 14:33:55 +0100549 void GenerateInlineNumberComparison(Result* left_side,
550 Result* right_side,
551 Condition cc,
552 ControlDestination* dest);
Steve Blocka7e24c12009-10-30 11:49:00 +0000553
554 // To prevent long attacker-controlled byte sequences, integer constants
555 // from the JavaScript source are loaded in two parts if they are larger
Steve Block6ded16b2010-05-10 14:33:55 +0100556 // than 17 bits.
557 static const int kMaxSmiInlinedBits = 17;
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 bool IsUnsafeSmi(Handle<Object> value);
Steve Blockd0582a62009-12-15 09:54:21 +0000559 // Load an integer constant x into a register target or into the stack using
Steve Blocka7e24c12009-10-30 11:49:00 +0000560 // at most 16 bits of user-controlled data per assembly operation.
Steve Blockd0582a62009-12-15 09:54:21 +0000561 void MoveUnsafeSmi(Register target, Handle<Object> value);
562 void StoreUnsafeSmiToLocal(int offset, Handle<Object> value);
563 void PushUnsafeSmi(Handle<Object> value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000564
Leon Clarkee46be812010-01-19 14:06:41 +0000565 void CallWithArguments(ZoneList<Expression*>* arguments,
566 CallFunctionFlags flags,
567 int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000568
Leon Clarked91b9f72010-01-27 17:25:45 +0000569 // An optimized implementation of expressions of the form
570 // x.apply(y, arguments). We call x the applicand and y the receiver.
571 // The optimization avoids allocating an arguments object if possible.
572 void CallApplyLazy(Expression* applicand,
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 Expression* receiver,
574 VariableProxy* arguments,
575 int position);
576
577 void CheckStack();
578
579 struct InlineRuntimeLUT {
580 void (CodeGenerator::*method)(ZoneList<Expression*>*);
581 const char* name;
Steve Block6ded16b2010-05-10 14:33:55 +0100582 int nargs;
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 };
584
585 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
586 bool CheckForInlineRuntimeCall(CallRuntime* node);
587 static bool PatchInlineRuntimeEntry(Handle<String> name,
588 const InlineRuntimeLUT& new_entry,
589 InlineRuntimeLUT* old_entry);
590
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
592
Steve Block3ce2e202009-11-05 08:53:23 +0000593 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
595 // Declare global variables and functions in the given array of
596 // name/value pairs.
597 void DeclareGlobals(Handle<FixedArray> pairs);
598
Steve Block6ded16b2010-05-10 14:33:55 +0100599 // Instantiate the function based on the shared function info.
600 Result InstantiateFunction(Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000601
602 // Support for type checks.
603 void GenerateIsSmi(ZoneList<Expression*>* args);
604 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
605 void GenerateIsArray(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000606 void GenerateIsRegExp(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000607 void GenerateIsObject(ZoneList<Expression*>* args);
608 void GenerateIsFunction(ZoneList<Expression*>* args);
Leon Clarked91b9f72010-01-27 17:25:45 +0000609 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000610
611 // Support for construct call checks.
612 void GenerateIsConstructCall(ZoneList<Expression*>* args);
613
614 // Support for arguments.length and arguments[?].
615 void GenerateArgumentsLength(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100616 void GenerateArguments(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000617
618 // Support for accessing the class and value fields of an object.
619 void GenerateClassOf(ZoneList<Expression*>* args);
620 void GenerateValueOf(ZoneList<Expression*>* args);
621 void GenerateSetValueOf(ZoneList<Expression*>* args);
622
623 // Fast support for charCodeAt(n).
624 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
625
Steve Block6ded16b2010-05-10 14:33:55 +0100626 // Fast support for string.charAt(n) and string[n].
627 void GenerateCharFromCode(ZoneList<Expression*>* args);
628
Steve Blocka7e24c12009-10-30 11:49:00 +0000629 // Fast support for object equality testing.
630 void GenerateObjectEquals(ZoneList<Expression*>* args);
631
632 void GenerateLog(ZoneList<Expression*>* args);
633
634 void GenerateGetFramePointer(ZoneList<Expression*>* args);
635
636 // Fast support for Math.random().
Steve Block6ded16b2010-05-10 14:33:55 +0100637 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
Steve Blockd0582a62009-12-15 09:54:21 +0000639 // Fast support for StringAdd.
640 void GenerateStringAdd(ZoneList<Expression*>* args);
641
Leon Clarkee46be812010-01-19 14:06:41 +0000642 // Fast support for SubString.
643 void GenerateSubString(ZoneList<Expression*>* args);
644
645 // Fast support for StringCompare.
646 void GenerateStringCompare(ZoneList<Expression*>* args);
647
648 // Support for direct calls from JavaScript to native RegExp code.
649 void GenerateRegExpExec(ZoneList<Expression*>* args);
650
Steve Block6ded16b2010-05-10 14:33:55 +0100651 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
652
653 // Support for fast native caches.
654 void GenerateGetFromCache(ZoneList<Expression*>* args);
655
Andrei Popescu402d9372010-02-26 13:31:12 +0000656 // Fast support for number to string.
657 void GenerateNumberToString(ZoneList<Expression*>* args);
658
Steve Block6ded16b2010-05-10 14:33:55 +0100659 // Fast swapping of elements. Takes three expressions, the object and two
660 // indices. This should only be used if the indices are known to be
661 // non-negative and within bounds of the elements array at the call site.
662 void GenerateSwapElements(ZoneList<Expression*>* args);
663
664 // Fast call for custom callbacks.
665 void GenerateCallFunction(ZoneList<Expression*>* args);
666
667 // Fast call to math functions.
668 void GenerateMathPow(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000669 void GenerateMathSin(ZoneList<Expression*>* args);
670 void GenerateMathCos(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100671 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000672
Steve Block3ce2e202009-11-05 08:53:23 +0000673 // Simple condition analysis.
674 enum ConditionAnalysis {
675 ALWAYS_TRUE,
676 ALWAYS_FALSE,
677 DONT_KNOW
678 };
679 ConditionAnalysis AnalyzeCondition(Expression* cond);
680
Steve Blocka7e24c12009-10-30 11:49:00 +0000681 // Methods used to indicate which source code is generated for. Source
682 // positions are collected by the assembler and emitted with the relocation
683 // information.
684 void CodeForFunctionPosition(FunctionLiteral* fun);
685 void CodeForReturnPosition(FunctionLiteral* fun);
686 void CodeForStatementPosition(Statement* stmt);
Steve Blockd0582a62009-12-15 09:54:21 +0000687 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 void CodeForSourcePosition(int pos);
689
Steve Block6ded16b2010-05-10 14:33:55 +0100690 void SetTypeForStackSlot(Slot* slot, TypeInfo info);
691
Steve Blocka7e24c12009-10-30 11:49:00 +0000692#ifdef DEBUG
693 // True if the registers are valid for entry to a block. There should
694 // be no frame-external references to (non-reserved) registers.
695 bool HasValidEntryRegisters();
696#endif
697
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 ZoneList<DeferredCode*> deferred_;
699
700 // Assembler
701 MacroAssembler* masm_; // to generate code
702
Andrei Popescu31002712010-02-23 13:46:05 +0000703 CompilationInfo* info_;
704
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000706 VirtualFrame* frame_;
707 RegisterAllocator* allocator_;
708 CodeGenState* state_;
709 int loop_nesting_;
Steve Block6ded16b2010-05-10 14:33:55 +0100710 bool in_safe_int32_mode_;
711 bool safe_int32_mode_enabled_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000712
713 // Jump targets.
714 // The target of the return from the function.
715 BreakTarget function_return_;
Steve Block6ded16b2010-05-10 14:33:55 +0100716 // The target of the bailout from a side-effect-free int32 subexpression.
717 BreakTarget* unsafe_bailout_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000718
719 // True if the function return is shadowed (ie, jumping to the target
720 // function_return_ does not jump to the true function return, but rather
721 // to some unlinking code).
722 bool function_return_is_shadowed_;
723
724 // True when we are in code that expects the virtual frame to be fully
725 // spilled. Some virtual frame function are disabled in DEBUG builds when
726 // called from spilled code, because they do not leave the virtual frame
727 // in a spilled state.
728 bool in_spilled_code_;
729
730 static InlineRuntimeLUT kInlineRuntimeLUT[];
731
732 friend class VirtualFrame;
733 friend class JumpTarget;
734 friend class Reference;
735 friend class Result;
Leon Clarke4515c472010-02-03 11:58:03 +0000736 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000737 friend class FullCodeGenerator;
738 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000739
740 friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc
741
742 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
743};
744
745
Andrei Popescu402d9372010-02-26 13:31:12 +0000746// Compute a transcendental math function natively, or call the
747// TranscendentalCache runtime function.
748class TranscendentalCacheStub: public CodeStub {
749 public:
750 explicit TranscendentalCacheStub(TranscendentalCache::Type type)
751 : type_(type) {}
752 void Generate(MacroAssembler* masm);
753 private:
754 TranscendentalCache::Type type_;
755 Major MajorKey() { return TranscendentalCache; }
756 int MinorKey() { return type_; }
757 Runtime::FunctionId RuntimeFunction();
758 void GenerateOperation(MacroAssembler* masm);
759};
760
761
Steve Blockd0582a62009-12-15 09:54:21 +0000762// Flag that indicates how to generate code for the stub GenericBinaryOpStub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000763enum GenericBinaryFlags {
Steve Block3ce2e202009-11-05 08:53:23 +0000764 NO_GENERIC_BINARY_FLAGS = 0,
765 NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000766};
767
768
769class GenericBinaryOpStub: public CodeStub {
770 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000771 GenericBinaryOpStub(Token::Value op,
Steve Blocka7e24c12009-10-30 11:49:00 +0000772 OverwriteMode mode,
Andrei Popescu402d9372010-02-26 13:31:12 +0000773 GenericBinaryFlags flags,
Steve Block6ded16b2010-05-10 14:33:55 +0100774 TypeInfo operands_type)
Steve Blockd0582a62009-12-15 09:54:21 +0000775 : op_(op),
Steve Block3ce2e202009-11-05 08:53:23 +0000776 mode_(mode),
777 flags_(flags),
778 args_in_registers_(false),
Leon Clarkee46be812010-01-19 14:06:41 +0000779 args_reversed_(false),
Steve Block6ded16b2010-05-10 14:33:55 +0100780 static_operands_type_(operands_type),
781 runtime_operands_type_(BinaryOpIC::DEFAULT),
782 name_(NULL) {
783 if (static_operands_type_.IsSmi()) {
784 mode_ = NO_OVERWRITE;
785 }
Steve Blockd0582a62009-12-15 09:54:21 +0000786 use_sse3_ = CpuFeatures::IsSupported(SSE3);
Steve Blocka7e24c12009-10-30 11:49:00 +0000787 ASSERT(OpBits::is_valid(Token::NUM_TOKENS));
788 }
789
Steve Block6ded16b2010-05-10 14:33:55 +0100790 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo runtime_operands_type)
791 : op_(OpBits::decode(key)),
792 mode_(ModeBits::decode(key)),
793 flags_(FlagBits::decode(key)),
794 args_in_registers_(ArgsInRegistersBits::decode(key)),
795 args_reversed_(ArgsReversedBits::decode(key)),
796 use_sse3_(SSE3Bits::decode(key)),
797 static_operands_type_(TypeInfo::ExpandedRepresentation(
798 StaticTypeInfoBits::decode(key))),
799 runtime_operands_type_(runtime_operands_type),
800 name_(NULL) {
801 }
802
Steve Block3ce2e202009-11-05 08:53:23 +0000803 // Generate code to call the stub with the supplied arguments. This will add
804 // code at the call site to prepare arguments either in registers or on the
805 // stack together with the actual call.
806 void GenerateCall(MacroAssembler* masm, Register left, Register right);
807 void GenerateCall(MacroAssembler* masm, Register left, Smi* right);
808 void GenerateCall(MacroAssembler* masm, Smi* left, Register right);
Steve Blocka7e24c12009-10-30 11:49:00 +0000809
Leon Clarked91b9f72010-01-27 17:25:45 +0000810 Result GenerateCall(MacroAssembler* masm,
811 VirtualFrame* frame,
812 Result* left,
813 Result* right);
814
Steve Blocka7e24c12009-10-30 11:49:00 +0000815 private:
816 Token::Value op_;
817 OverwriteMode mode_;
818 GenericBinaryFlags flags_;
Steve Block3ce2e202009-11-05 08:53:23 +0000819 bool args_in_registers_; // Arguments passed in registers not on the stack.
820 bool args_reversed_; // Left and right argument are swapped.
Steve Blocka7e24c12009-10-30 11:49:00 +0000821 bool use_sse3_;
Steve Block6ded16b2010-05-10 14:33:55 +0100822
823 // Number type information of operands, determined by code generator.
824 TypeInfo static_operands_type_;
825
826 // Operand type information determined at runtime.
827 BinaryOpIC::TypeInfo runtime_operands_type_;
828
Leon Clarkee46be812010-01-19 14:06:41 +0000829 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000830
831 const char* GetName();
832
833#ifdef DEBUG
834 void Print() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000835 PrintF("GenericBinaryOpStub %d (op %s), "
Steve Block6ded16b2010-05-10 14:33:55 +0100836 "(mode %d, flags %d, registers %d, reversed %d, type_info %s)\n",
Andrei Popescu402d9372010-02-26 13:31:12 +0000837 MinorKey(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 Token::String(op_),
839 static_cast<int>(mode_),
Steve Block3ce2e202009-11-05 08:53:23 +0000840 static_cast<int>(flags_),
841 static_cast<int>(args_in_registers_),
Andrei Popescu402d9372010-02-26 13:31:12 +0000842 static_cast<int>(args_reversed_),
Steve Block6ded16b2010-05-10 14:33:55 +0100843 static_operands_type_.ToString());
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 }
845#endif
846
Steve Block6ded16b2010-05-10 14:33:55 +0100847 // Minor key encoding in 18 bits RRNNNFRASOOOOOOOMM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000848 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000849 class OpBits: public BitField<Token::Value, 2, 7> {};
850 class SSE3Bits: public BitField<bool, 9, 1> {};
851 class ArgsInRegistersBits: public BitField<bool, 10, 1> {};
852 class ArgsReversedBits: public BitField<bool, 11, 1> {};
853 class FlagBits: public BitField<GenericBinaryFlags, 12, 1> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100854 class StaticTypeInfoBits: public BitField<int, 13, 3> {};
855 class RuntimeTypeInfoBits: public BitField<BinaryOpIC::TypeInfo, 16, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000856
857 Major MajorKey() { return GenericBinaryOp; }
858 int MinorKey() {
Steve Block6ded16b2010-05-10 14:33:55 +0100859 // Encode the parameters in a unique 18 bit value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000860 return OpBits::encode(op_)
861 | ModeBits::encode(mode_)
862 | FlagBits::encode(flags_)
Steve Block3ce2e202009-11-05 08:53:23 +0000863 | SSE3Bits::encode(use_sse3_)
864 | ArgsInRegistersBits::encode(args_in_registers_)
Andrei Popescu402d9372010-02-26 13:31:12 +0000865 | ArgsReversedBits::encode(args_reversed_)
Steve Block6ded16b2010-05-10 14:33:55 +0100866 | StaticTypeInfoBits::encode(
867 static_operands_type_.ThreeBitRepresentation())
868 | RuntimeTypeInfoBits::encode(runtime_operands_type_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 }
Steve Block3ce2e202009-11-05 08:53:23 +0000870
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 void Generate(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000872 void GenerateSmiCode(MacroAssembler* masm, Label* slow);
873 void GenerateLoadArguments(MacroAssembler* masm);
874 void GenerateReturn(MacroAssembler* masm);
Leon Clarked91b9f72010-01-27 17:25:45 +0000875 void GenerateHeapResultAllocation(MacroAssembler* masm, Label* alloc_failure);
Steve Block6ded16b2010-05-10 14:33:55 +0100876 void GenerateRegisterArgsPush(MacroAssembler* masm);
877 void GenerateTypeTransition(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000878
879 bool ArgsInRegistersSupported() {
Leon Clarked91b9f72010-01-27 17:25:45 +0000880 return op_ == Token::ADD || op_ == Token::SUB
881 || op_ == Token::MUL || op_ == Token::DIV;
Steve Block3ce2e202009-11-05 08:53:23 +0000882 }
883 bool IsOperationCommutative() {
884 return (op_ == Token::ADD) || (op_ == Token::MUL);
885 }
886
887 void SetArgsInRegisters() { args_in_registers_ = true; }
888 void SetArgsReversed() { args_reversed_ = true; }
889 bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; }
Leon Clarked91b9f72010-01-27 17:25:45 +0000890 bool HasArgsInRegisters() { return args_in_registers_; }
891 bool HasArgsReversed() { return args_reversed_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100892
893 bool ShouldGenerateSmiCode() {
894 return HasSmiCodeInStub() &&
895 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
896 runtime_operands_type_ != BinaryOpIC::STRINGS;
897 }
898
899 bool ShouldGenerateFPCode() {
900 return runtime_operands_type_ != BinaryOpIC::STRINGS;
901 }
902
903 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
904
905 virtual InlineCacheState GetICState() {
906 return BinaryOpIC::ToState(runtime_operands_type_);
907 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000908};
909
910
Steve Block6ded16b2010-05-10 14:33:55 +0100911class StringHelper : public AllStatic {
Leon Clarkee46be812010-01-19 14:06:41 +0000912 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100913 // Generates fast code for getting a char code out of a string
914 // object at the given index. May bail out for four reasons (in the
915 // listed order):
916 // * Receiver is not a string (receiver_not_string label).
917 // * Index is not a smi (index_not_smi label).
918 // * Index is out of range (index_out_of_range).
919 // * Some other reason (slow_case label). In this case it's
920 // guaranteed that the above conditions are not violated,
921 // e.g. it's safe to assume the receiver is a string and the
922 // index is a non-negative smi < length.
923 // When successful, object, index, and scratch are clobbered.
924 // Otherwise, scratch and result are clobbered.
925 static void GenerateFastCharCodeAt(MacroAssembler* masm,
926 Register object,
927 Register index,
928 Register scratch,
929 Register result,
930 Label* receiver_not_string,
931 Label* index_not_smi,
932 Label* index_out_of_range,
933 Label* slow_case);
934
935 // Generates code for creating a one-char string from the given char
936 // code. May do a runtime call, so any register can be clobbered
937 // and, if the given invoke flag specifies a call, an internal frame
938 // is required. In tail call mode the result must be eax register.
939 static void GenerateCharFromCode(MacroAssembler* masm,
940 Register code,
941 Register result,
942 InvokeFlag flag);
943
Leon Clarkee46be812010-01-19 14:06:41 +0000944 // Generate code for copying characters using a simple loop. This should only
945 // be used in places where the number of characters is small and the
946 // additional setup and checking in GenerateCopyCharactersREP adds too much
947 // overhead. Copying of overlapping regions is not supported.
Steve Block6ded16b2010-05-10 14:33:55 +0100948 static void GenerateCopyCharacters(MacroAssembler* masm,
949 Register dest,
950 Register src,
951 Register count,
952 Register scratch,
953 bool ascii);
Leon Clarkee46be812010-01-19 14:06:41 +0000954
955 // Generate code for copying characters using the rep movs instruction.
956 // Copies ecx characters from esi to edi. Copying of overlapping regions is
957 // not supported.
Steve Block6ded16b2010-05-10 14:33:55 +0100958 static void GenerateCopyCharactersREP(MacroAssembler* masm,
959 Register dest, // Must be edi.
960 Register src, // Must be esi.
961 Register count, // Must be ecx.
962 Register scratch, // Neither of above.
963 bool ascii);
Andrei Popescu402d9372010-02-26 13:31:12 +0000964
965 // Probe the symbol table for a two character string. If the string is
966 // not found by probing a jump to the label not_found is performed. This jump
967 // does not guarantee that the string is not in the symbol table. If the
968 // string is found the code falls through with the string in register eax.
Steve Block6ded16b2010-05-10 14:33:55 +0100969 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
970 Register c1,
971 Register c2,
972 Register scratch1,
973 Register scratch2,
974 Register scratch3,
975 Label* not_found);
Andrei Popescu402d9372010-02-26 13:31:12 +0000976
977 // Generate string hash.
Steve Block6ded16b2010-05-10 14:33:55 +0100978 static void GenerateHashInit(MacroAssembler* masm,
979 Register hash,
980 Register character,
981 Register scratch);
982 static void GenerateHashAddCharacter(MacroAssembler* masm,
983 Register hash,
984 Register character,
985 Register scratch);
986 static void GenerateHashGetHash(MacroAssembler* masm,
987 Register hash,
988 Register scratch);
989
990 private:
991 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
Leon Clarkee46be812010-01-19 14:06:41 +0000992};
993
994
Andrei Popescu31002712010-02-23 13:46:05 +0000995// Flag that indicates how to generate code for the stub StringAddStub.
996enum StringAddFlags {
997 NO_STRING_ADD_FLAGS = 0,
998 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
999};
1000
1001
Steve Block6ded16b2010-05-10 14:33:55 +01001002class StringAddStub: public CodeStub {
Steve Blockd0582a62009-12-15 09:54:21 +00001003 public:
1004 explicit StringAddStub(StringAddFlags flags) {
1005 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
1006 }
1007
1008 private:
1009 Major MajorKey() { return StringAdd; }
1010 int MinorKey() { return string_check_ ? 0 : 1; }
1011
1012 void Generate(MacroAssembler* masm);
1013
Steve Blockd0582a62009-12-15 09:54:21 +00001014 // Should the stub check whether arguments are strings?
1015 bool string_check_;
1016};
1017
1018
Steve Block6ded16b2010-05-10 14:33:55 +01001019class SubStringStub: public CodeStub {
Leon Clarkee46be812010-01-19 14:06:41 +00001020 public:
1021 SubStringStub() {}
1022
1023 private:
1024 Major MajorKey() { return SubString; }
1025 int MinorKey() { return 0; }
1026
1027 void Generate(MacroAssembler* masm);
1028};
1029
1030
Steve Block6ded16b2010-05-10 14:33:55 +01001031class StringCompareStub: public CodeStub {
Leon Clarkee46be812010-01-19 14:06:41 +00001032 public:
1033 explicit StringCompareStub() {
1034 }
1035
1036 // Compare two flat ascii strings and returns result in eax after popping two
1037 // arguments from the stack.
1038 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
1039 Register left,
1040 Register right,
1041 Register scratch1,
1042 Register scratch2,
1043 Register scratch3);
1044
1045 private:
1046 Major MajorKey() { return StringCompare; }
1047 int MinorKey() { return 0; }
1048
1049 void Generate(MacroAssembler* masm);
1050};
1051
1052
Andrei Popescu402d9372010-02-26 13:31:12 +00001053class NumberToStringStub: public CodeStub {
1054 public:
1055 NumberToStringStub() { }
1056
1057 // Generate code to do a lookup in the number string cache. If the number in
1058 // the register object is found in the cache the generated code falls through
1059 // with the result in the result register. The object and the result register
1060 // can be the same. If the number is not found in the cache the code jumps to
1061 // the label not_found with only the content of register object unchanged.
1062 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
1063 Register object,
1064 Register result,
1065 Register scratch1,
1066 Register scratch2,
1067 bool object_is_smi,
1068 Label* not_found);
1069
1070 private:
1071 Major MajorKey() { return NumberToString; }
1072 int MinorKey() { return 0; }
1073
1074 void Generate(MacroAssembler* masm);
1075
1076 const char* GetName() { return "NumberToStringStub"; }
1077
1078#ifdef DEBUG
1079 void Print() {
1080 PrintF("NumberToStringStub\n");
1081 }
1082#endif
1083};
1084
1085
Steve Block6ded16b2010-05-10 14:33:55 +01001086class RecordWriteStub : public CodeStub {
1087 public:
1088 RecordWriteStub(Register object, Register addr, Register scratch)
1089 : object_(object), addr_(addr), scratch_(scratch) { }
1090
1091 void Generate(MacroAssembler* masm);
1092
1093 private:
1094 Register object_;
1095 Register addr_;
1096 Register scratch_;
1097
1098#ifdef DEBUG
1099 void Print() {
1100 PrintF("RecordWriteStub (object reg %d), (addr reg %d), (scratch reg %d)\n",
1101 object_.code(), addr_.code(), scratch_.code());
1102 }
1103#endif
1104
1105 // Minor key encoding in 12 bits. 4 bits for each of the three
1106 // registers (object, address and scratch) OOOOAAAASSSS.
1107 class ScratchBits: public BitField<uint32_t, 0, 4> {};
1108 class AddressBits: public BitField<uint32_t, 4, 4> {};
1109 class ObjectBits: public BitField<uint32_t, 8, 4> {};
1110
1111 Major MajorKey() { return RecordWrite; }
1112
1113 int MinorKey() {
1114 // Encode the registers.
1115 return ObjectBits::encode(object_.code()) |
1116 AddressBits::encode(addr_.code()) |
1117 ScratchBits::encode(scratch_.code());
1118 }
1119};
1120
1121
Steve Blocka7e24c12009-10-30 11:49:00 +00001122} } // namespace v8::internal
1123
1124#endif // V8_IA32_CODEGEN_IA32_H_