blob: 843bbf76f750560e40697024679dce0ce64522e1 [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
31namespace v8 {
32namespace internal {
33
34// Forward declarations
Leon Clarke4515c472010-02-03 11:58:03 +000035class CompilationInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +000036class DeferredCode;
37class RegisterAllocator;
38class RegisterFile;
39
40enum InitState { CONST_INIT, NOT_CONST_INIT };
41enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
42
43
44// -------------------------------------------------------------------------
45// Reference support
46
Leon Clarked91b9f72010-01-27 17:25:45 +000047// A reference is a C++ stack-allocated object that puts a
48// reference on the virtual frame. The reference may be consumed
49// by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
50// When the lifetime (scope) of a valid reference ends, it must have
51// been consumed, and be in state UNLOADED.
Steve Blocka7e24c12009-10-30 11:49:00 +000052class Reference BASE_EMBEDDED {
53 public:
54 // The values of the types is important, see size().
Leon Clarked91b9f72010-01-27 17:25:45 +000055 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
56 Reference(CodeGenerator* cgen,
57 Expression* expression,
58 bool persist_after_get = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000059 ~Reference();
60
61 Expression* expression() const { return expression_; }
62 Type type() const { return type_; }
63 void set_type(Type value) {
Leon Clarked91b9f72010-01-27 17:25:45 +000064 ASSERT_EQ(ILLEGAL, type_);
Steve Blocka7e24c12009-10-30 11:49:00 +000065 type_ = value;
66 }
67
Leon Clarked91b9f72010-01-27 17:25:45 +000068 void set_unloaded() {
69 ASSERT_NE(ILLEGAL, type_);
70 ASSERT_NE(UNLOADED, type_);
71 type_ = UNLOADED;
72 }
Steve Blocka7e24c12009-10-30 11:49:00 +000073 // The size the reference takes up on the stack.
Leon Clarked91b9f72010-01-27 17:25:45 +000074 int size() const {
75 return (type_ < SLOT) ? 0 : type_;
76 }
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78 bool is_illegal() const { return type_ == ILLEGAL; }
79 bool is_slot() const { return type_ == SLOT; }
80 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
Leon Clarked91b9f72010-01-27 17:25:45 +000081 bool is_unloaded() const { return type_ == UNLOADED; }
Steve Blocka7e24c12009-10-30 11:49:00 +000082
83 // Return the name. Only valid for named property references.
84 Handle<String> GetName();
85
86 // Generate code to push the value of the reference on top of the
87 // expression stack. The reference is expected to be already on top of
Leon Clarked91b9f72010-01-27 17:25:45 +000088 // the expression stack, and it is consumed by the call unless the
89 // reference is for a compound assignment.
90 // If the reference is not consumed, it is left in place under its value.
Steve Blockd0582a62009-12-15 09:54:21 +000091 void GetValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000092
93 // Like GetValue except that the slot is expected to be written to before
Leon Clarked91b9f72010-01-27 17:25:45 +000094 // being read from again. The value of the reference may be invalidated,
Steve Blocka7e24c12009-10-30 11:49:00 +000095 // causing subsequent attempts to read it to fail.
Steve Blockd0582a62009-12-15 09:54:21 +000096 void TakeValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000097
98 // Generate code to store the value on top of the expression stack in the
99 // reference. The reference is expected to be immediately below the value
Leon Clarked91b9f72010-01-27 17:25:45 +0000100 // on the expression stack. The value is stored in the location specified
101 // by the reference, and is left on top of the stack, after the reference
102 // is popped from beneath it (unloaded).
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 void SetValue(InitState init_state);
104
105 private:
106 CodeGenerator* cgen_;
107 Expression* expression_;
108 Type type_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000109 // Keep the reference on the stack after get, so it can be used by set later.
110 bool persist_after_get_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000111};
112
113
114// -------------------------------------------------------------------------
115// Control destinations.
116
117// A control destination encapsulates a pair of jump targets and a
118// flag indicating which one is the preferred fall-through. The
119// preferred fall-through must be unbound, the other may be already
120// bound (ie, a backward target).
121//
122// The true and false targets may be jumped to unconditionally or
123// control may split conditionally. Unconditional jumping and
124// splitting should be emitted in tail position (as the last thing
125// when compiling an expression) because they can cause either label
126// to be bound or the non-fall through to be jumped to leaving an
127// invalid virtual frame.
128//
129// The labels in the control destination can be extracted and
130// manipulated normally without affecting the state of the
131// destination.
132
133class ControlDestination BASE_EMBEDDED {
134 public:
135 ControlDestination(JumpTarget* true_target,
136 JumpTarget* false_target,
137 bool true_is_fall_through)
138 : true_target_(true_target),
139 false_target_(false_target),
140 true_is_fall_through_(true_is_fall_through),
141 is_used_(false) {
142 ASSERT(true_is_fall_through ? !true_target->is_bound()
143 : !false_target->is_bound());
144 }
145
146 // Accessors for the jump targets. Directly jumping or branching to
147 // or binding the targets will not update the destination's state.
148 JumpTarget* true_target() const { return true_target_; }
149 JumpTarget* false_target() const { return false_target_; }
150
151 // True if the the destination has been jumped to unconditionally or
152 // control has been split to both targets. This predicate does not
153 // test whether the targets have been extracted and manipulated as
154 // raw jump targets.
155 bool is_used() const { return is_used_; }
156
157 // True if the destination is used and the true target (respectively
158 // false target) was the fall through. If the target is backward,
159 // "fall through" included jumping unconditionally to it.
160 bool true_was_fall_through() const {
161 return is_used_ && true_is_fall_through_;
162 }
163
164 bool false_was_fall_through() const {
165 return is_used_ && !true_is_fall_through_;
166 }
167
168 // Emit a branch to one of the true or false targets, and bind the
169 // other target. Because this binds the fall-through target, it
170 // should be emitted in tail position (as the last thing when
171 // compiling an expression).
172 void Split(Condition cc) {
173 ASSERT(!is_used_);
174 if (true_is_fall_through_) {
175 false_target_->Branch(NegateCondition(cc));
176 true_target_->Bind();
177 } else {
178 true_target_->Branch(cc);
179 false_target_->Bind();
180 }
181 is_used_ = true;
182 }
183
184 // Emit an unconditional jump in tail position, to the true target
185 // (if the argument is true) or the false target. The "jump" will
186 // actually bind the jump target if it is forward, jump to it if it
187 // is backward.
188 void Goto(bool where) {
189 ASSERT(!is_used_);
190 JumpTarget* target = where ? true_target_ : false_target_;
191 if (target->is_bound()) {
192 target->Jump();
193 } else {
194 target->Bind();
195 }
196 is_used_ = true;
197 true_is_fall_through_ = where;
198 }
199
200 // Mark this jump target as used as if Goto had been called, but
201 // without generating a jump or binding a label (the control effect
202 // should have already happened). This is used when the left
203 // subexpression of the short-circuit boolean operators are
204 // compiled.
205 void Use(bool where) {
206 ASSERT(!is_used_);
207 ASSERT((where ? true_target_ : false_target_)->is_bound());
208 is_used_ = true;
209 true_is_fall_through_ = where;
210 }
211
212 // Swap the true and false targets but keep the same actual label as
213 // the fall through. This is used when compiling negated
214 // expressions, where we want to swap the targets but preserve the
215 // state.
216 void Invert() {
217 JumpTarget* temp_target = true_target_;
218 true_target_ = false_target_;
219 false_target_ = temp_target;
220
221 true_is_fall_through_ = !true_is_fall_through_;
222 }
223
224 private:
225 // True and false jump targets.
226 JumpTarget* true_target_;
227 JumpTarget* false_target_;
228
229 // Before using the destination: true if the true target is the
230 // preferred fall through, false if the false target is. After
231 // using the destination: true if the true target was actually used
232 // as the fall through, false if the false target was.
233 bool true_is_fall_through_;
234
235 // True if the Split or Goto functions have been called.
236 bool is_used_;
237};
238
239
240// -------------------------------------------------------------------------
241// Code generation state
242
243// The state is passed down the AST by the code generator (and back up, in
244// the form of the state of the jump target pair). It is threaded through
245// the call stack. Constructing a state implicitly pushes it on the owning
246// code generator's stack of states, and destroying one implicitly pops it.
247//
248// The code generator state is only used for expressions, so statements have
249// the initial state.
250
251class CodeGenState BASE_EMBEDDED {
252 public:
253 // Create an initial code generator state. Destroying the initial state
254 // leaves the code generator with a NULL state.
255 explicit CodeGenState(CodeGenerator* owner);
256
257 // Create a code generator state based on a code generator's current
Steve Blockd0582a62009-12-15 09:54:21 +0000258 // state. The new state has its own control destination.
259 CodeGenState(CodeGenerator* owner, ControlDestination* destination);
Steve Blocka7e24c12009-10-30 11:49:00 +0000260
261 // Destroy a code generator state and restore the owning code generator's
262 // previous state.
263 ~CodeGenState();
264
265 // Accessors for the state.
Steve Blocka7e24c12009-10-30 11:49:00 +0000266 ControlDestination* destination() const { return destination_; }
267
268 private:
269 // The owning code generator.
270 CodeGenerator* owner_;
271
Steve Blocka7e24c12009-10-30 11:49:00 +0000272 // A control destination in case the expression has a control-flow
273 // effect.
274 ControlDestination* destination_;
275
276 // The previous state of the owning code generator, restored when
277 // this state is destroyed.
278 CodeGenState* previous_;
279};
280
281
282// -------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000283// Arguments allocation mode.
Steve Blocka7e24c12009-10-30 11:49:00 +0000284
285enum ArgumentsAllocationMode {
286 NO_ARGUMENTS_ALLOCATION,
287 EAGER_ARGUMENTS_ALLOCATION,
288 LAZY_ARGUMENTS_ALLOCATION
289};
290
291
292// -------------------------------------------------------------------------
293// CodeGenerator
294
295class CodeGenerator: public AstVisitor {
296 public:
Leon Clarke4515c472010-02-03 11:58:03 +0000297 // Compilation mode. Either the compiler is used as the primary
298 // compiler and needs to setup everything or the compiler is used as
299 // the secondary compiler for split compilation and has to handle
300 // bailouts.
301 enum Mode {
302 PRIMARY,
303 SECONDARY
304 };
305
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 // Takes a function literal, generates code for it. This function should only
307 // be called by compiler.cc.
Andrei Popescu31002712010-02-23 13:46:05 +0000308 static Handle<Code> MakeCode(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000309
Steve Block3ce2e202009-11-05 08:53:23 +0000310 // Printing of AST, etc. as requested by flags.
Andrei Popescu31002712010-02-23 13:46:05 +0000311 static void MakeCodePrologue(CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000312
313 // Allocate and install the code.
Andrei Popescu31002712010-02-23 13:46:05 +0000314 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +0000315 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +0000316 CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318#ifdef ENABLE_LOGGING_AND_PROFILING
319 static bool ShouldGenerateLog(Expression* type);
320#endif
321
Steve Block3ce2e202009-11-05 08:53:23 +0000322 static void RecordPositions(MacroAssembler* masm, int pos);
323
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 // Accessors
325 MacroAssembler* masm() { return masm_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 VirtualFrame* frame() const { return frame_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000327 inline Handle<Script> script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000328
329 bool has_valid_frame() const { return frame_ != NULL; }
330
331 // Set the virtual frame to be new_frame, with non-frame register
332 // reference counts given by non_frame_registers. The non-frame
333 // register reference counts of the old frame are returned in
334 // non_frame_registers.
335 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
336
337 void DeleteFrame();
338
339 RegisterAllocator* allocator() const { return allocator_; }
340
341 CodeGenState* state() { return state_; }
342 void set_state(CodeGenState* state) { state_ = state; }
343
344 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
345
346 bool in_spilled_code() const { return in_spilled_code_; }
347 void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
348
349 private:
350 // Construction/Destruction
Andrei Popescu31002712010-02-23 13:46:05 +0000351 explicit CodeGenerator(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000352
353 // Accessors
Andrei Popescu31002712010-02-23 13:46:05 +0000354 inline bool is_eval();
355 Scope* scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000356
357 // Generating deferred code.
358 void ProcessDeferred();
359
360 // State
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 ControlDestination* destination() const { return state_->destination(); }
362
363 // Track loop nesting level.
364 int loop_nesting() const { return loop_nesting_; }
365 void IncrementLoopNesting() { loop_nesting_++; }
366 void DecrementLoopNesting() { loop_nesting_--; }
367
368 // Node visitors.
369 void VisitStatements(ZoneList<Statement*>* statements);
370
371#define DEF_VISIT(type) \
372 void Visit##type(type* node);
373 AST_NODE_LIST(DEF_VISIT)
374#undef DEF_VISIT
375
376 // Visit a statement and then spill the virtual frame if control flow can
377 // reach the end of the statement (ie, it does not exit via break,
378 // continue, return, or throw). This function is used temporarily while
379 // the code generator is being transformed.
380 void VisitAndSpill(Statement* statement);
381
382 // Visit a list of statements and then spill the virtual frame if control
383 // flow can reach the end of the list.
384 void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
385
386 // Main code generation function
Andrei Popescu31002712010-02-23 13:46:05 +0000387 void Generate(CompilationInfo* info, Mode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000388
389 // Generate the return sequence code. Should be called no more than
390 // once per compiled function, immediately after binding the return
391 // target (which can not be done more than once).
392 void GenerateReturnSequence(Result* return_value);
393
394 // Returns the arguments allocation mode.
Andrei Popescu31002712010-02-23 13:46:05 +0000395 ArgumentsAllocationMode ArgumentsMode();
Steve Blocka7e24c12009-10-30 11:49:00 +0000396
397 // Store the arguments object and allocate it if necessary.
398 Result StoreArgumentsObject(bool initial);
399
400 // The following are used by class Reference.
401 void LoadReference(Reference* ref);
402 void UnloadReference(Reference* ref);
403
Steve Block3ce2e202009-11-05 08:53:23 +0000404 static Operand ContextOperand(Register context, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 return Operand(context, Context::SlotOffset(index));
406 }
407
408 Operand SlotOperand(Slot* slot, Register tmp);
409
410 Operand ContextSlotOperandCheckExtensions(Slot* slot,
411 Result tmp,
412 JumpTarget* slow);
413
414 // Expressions
Steve Block3ce2e202009-11-05 08:53:23 +0000415 static Operand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 return ContextOperand(esi, Context::GLOBAL_INDEX);
417 }
418
419 void LoadCondition(Expression* x,
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 ControlDestination* destination,
421 bool force_control);
Steve Blockd0582a62009-12-15 09:54:21 +0000422 void Load(Expression* expr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 void LoadGlobal();
424 void LoadGlobalReceiver();
425
426 // Generate code to push the value of an expression on top of the frame
427 // and then spill the frame fully to memory. This function is used
428 // temporarily while the code generator is being transformed.
Steve Blockd0582a62009-12-15 09:54:21 +0000429 void LoadAndSpill(Expression* expression);
Steve Blocka7e24c12009-10-30 11:49:00 +0000430
431 // Read a value from a slot and leave it on top of the expression stack.
432 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
433 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState typeof_state);
434 Result LoadFromGlobalSlotCheckExtensions(Slot* slot,
435 TypeofState typeof_state,
436 JumpTarget* slow);
437
438 // Store the value on top of the expression stack into a slot, leaving the
439 // value in place.
440 void StoreToSlot(Slot* slot, InitState init_state);
441
Leon Clarked91b9f72010-01-27 17:25:45 +0000442 // Load a property of an object, returning it in a Result.
443 // The object and the property name are passed on the stack, and
444 // not changed.
445 Result EmitKeyedLoad(bool is_global);
446
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 // Special code for typeof expressions: Unfortunately, we must
448 // be careful when loading the expression in 'typeof'
449 // expressions. We are not allowed to throw reference errors for
450 // non-existing properties of the global object, so we must make it
451 // look like an explicit property access, instead of an access
452 // through the context chain.
453 void LoadTypeofExpression(Expression* x);
454
455 // Translate the value on top of the frame into control flow to the
456 // control destination.
457 void ToBoolean(ControlDestination* destination);
458
459 void GenericBinaryOperation(
460 Token::Value op,
Leon Clarkee46be812010-01-19 14:06:41 +0000461 StaticType* type,
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 OverwriteMode overwrite_mode);
463
464 // If possible, combine two constant smi values using op to produce
465 // a smi result, and push it on the virtual frame, all at compile time.
466 // Returns true if it succeeds. Otherwise it has no effect.
467 bool FoldConstantSmis(Token::Value op, int left, int right);
468
469 // Emit code to perform a binary operation on a constant
470 // smi and a likely smi. Consumes the Result *operand.
Leon Clarked91b9f72010-01-27 17:25:45 +0000471 Result ConstantSmiBinaryOperation(Token::Value op,
472 Result* operand,
473 Handle<Object> constant_operand,
474 StaticType* type,
475 bool reversed,
476 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000477
478 // Emit code to perform a binary operation on two likely smis.
479 // The code to handle smi arguments is produced inline.
480 // Consumes the Results *left and *right.
Leon Clarked91b9f72010-01-27 17:25:45 +0000481 Result LikelySmiBinaryOperation(Token::Value op,
482 Result* left,
483 Result* right,
484 OverwriteMode overwrite_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000485
Leon Clarkee46be812010-01-19 14:06:41 +0000486 void Comparison(AstNode* node,
487 Condition cc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 bool strict,
489 ControlDestination* destination);
490
491 // To prevent long attacker-controlled byte sequences, integer constants
492 // from the JavaScript source are loaded in two parts if they are larger
493 // than 16 bits.
494 static const int kMaxSmiInlinedBits = 16;
495 bool IsUnsafeSmi(Handle<Object> value);
Steve Blockd0582a62009-12-15 09:54:21 +0000496 // Load an integer constant x into a register target or into the stack using
Steve Blocka7e24c12009-10-30 11:49:00 +0000497 // at most 16 bits of user-controlled data per assembly operation.
Steve Blockd0582a62009-12-15 09:54:21 +0000498 void MoveUnsafeSmi(Register target, Handle<Object> value);
499 void StoreUnsafeSmiToLocal(int offset, Handle<Object> value);
500 void PushUnsafeSmi(Handle<Object> value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000501
Leon Clarkee46be812010-01-19 14:06:41 +0000502 void CallWithArguments(ZoneList<Expression*>* arguments,
503 CallFunctionFlags flags,
504 int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000505
Leon Clarked91b9f72010-01-27 17:25:45 +0000506 // An optimized implementation of expressions of the form
507 // x.apply(y, arguments). We call x the applicand and y the receiver.
508 // The optimization avoids allocating an arguments object if possible.
509 void CallApplyLazy(Expression* applicand,
Steve Blocka7e24c12009-10-30 11:49:00 +0000510 Expression* receiver,
511 VariableProxy* arguments,
512 int position);
513
514 void CheckStack();
515
516 struct InlineRuntimeLUT {
517 void (CodeGenerator::*method)(ZoneList<Expression*>*);
518 const char* name;
519 };
520
521 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
522 bool CheckForInlineRuntimeCall(CallRuntime* node);
523 static bool PatchInlineRuntimeEntry(Handle<String> name,
524 const InlineRuntimeLUT& new_entry,
525 InlineRuntimeLUT* old_entry);
526
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
528
Steve Block3ce2e202009-11-05 08:53:23 +0000529 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000530
531 // Declare global variables and functions in the given array of
532 // name/value pairs.
533 void DeclareGlobals(Handle<FixedArray> pairs);
534
535 // Instantiate the function boilerplate.
536 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
537
538 // Support for type checks.
539 void GenerateIsSmi(ZoneList<Expression*>* args);
540 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
541 void GenerateIsArray(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000542 void GenerateIsObject(ZoneList<Expression*>* args);
543 void GenerateIsFunction(ZoneList<Expression*>* args);
Leon Clarked91b9f72010-01-27 17:25:45 +0000544 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000545
546 // Support for construct call checks.
547 void GenerateIsConstructCall(ZoneList<Expression*>* args);
548
549 // Support for arguments.length and arguments[?].
550 void GenerateArgumentsLength(ZoneList<Expression*>* args);
551 void GenerateArgumentsAccess(ZoneList<Expression*>* args);
552
553 // Support for accessing the class and value fields of an object.
554 void GenerateClassOf(ZoneList<Expression*>* args);
555 void GenerateValueOf(ZoneList<Expression*>* args);
556 void GenerateSetValueOf(ZoneList<Expression*>* args);
557
558 // Fast support for charCodeAt(n).
559 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
560
561 // Fast support for object equality testing.
562 void GenerateObjectEquals(ZoneList<Expression*>* args);
563
564 void GenerateLog(ZoneList<Expression*>* args);
565
566 void GenerateGetFramePointer(ZoneList<Expression*>* args);
567
568 // Fast support for Math.random().
569 void GenerateRandomPositiveSmi(ZoneList<Expression*>* args);
570
Steve Blockd0582a62009-12-15 09:54:21 +0000571 // Fast support for StringAdd.
572 void GenerateStringAdd(ZoneList<Expression*>* args);
573
Leon Clarkee46be812010-01-19 14:06:41 +0000574 // Fast support for SubString.
575 void GenerateSubString(ZoneList<Expression*>* args);
576
577 // Fast support for StringCompare.
578 void GenerateStringCompare(ZoneList<Expression*>* args);
579
580 // Support for direct calls from JavaScript to native RegExp code.
581 void GenerateRegExpExec(ZoneList<Expression*>* args);
582
Steve Block3ce2e202009-11-05 08:53:23 +0000583 // Simple condition analysis.
584 enum ConditionAnalysis {
585 ALWAYS_TRUE,
586 ALWAYS_FALSE,
587 DONT_KNOW
588 };
589 ConditionAnalysis AnalyzeCondition(Expression* cond);
590
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 // Methods used to indicate which source code is generated for. Source
592 // positions are collected by the assembler and emitted with the relocation
593 // information.
594 void CodeForFunctionPosition(FunctionLiteral* fun);
595 void CodeForReturnPosition(FunctionLiteral* fun);
596 void CodeForStatementPosition(Statement* stmt);
Steve Blockd0582a62009-12-15 09:54:21 +0000597 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 void CodeForSourcePosition(int pos);
599
600#ifdef DEBUG
601 // True if the registers are valid for entry to a block. There should
602 // be no frame-external references to (non-reserved) registers.
603 bool HasValidEntryRegisters();
604#endif
605
Steve Blocka7e24c12009-10-30 11:49:00 +0000606 ZoneList<DeferredCode*> deferred_;
607
608 // Assembler
609 MacroAssembler* masm_; // to generate code
610
Andrei Popescu31002712010-02-23 13:46:05 +0000611 CompilationInfo* info_;
612
Steve Blocka7e24c12009-10-30 11:49:00 +0000613 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000614 VirtualFrame* frame_;
615 RegisterAllocator* allocator_;
616 CodeGenState* state_;
617 int loop_nesting_;
618
619 // Jump targets.
620 // The target of the return from the function.
621 BreakTarget function_return_;
622
623 // True if the function return is shadowed (ie, jumping to the target
624 // function_return_ does not jump to the true function return, but rather
625 // to some unlinking code).
626 bool function_return_is_shadowed_;
627
628 // True when we are in code that expects the virtual frame to be fully
629 // spilled. Some virtual frame function are disabled in DEBUG builds when
630 // called from spilled code, because they do not leave the virtual frame
631 // in a spilled state.
632 bool in_spilled_code_;
633
634 static InlineRuntimeLUT kInlineRuntimeLUT[];
635
636 friend class VirtualFrame;
637 friend class JumpTarget;
638 friend class Reference;
639 friend class Result;
Leon Clarke4515c472010-02-03 11:58:03 +0000640 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000641 friend class FullCodeGenerator;
642 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000643
644 friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc
645
646 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
647};
648
649
Steve Blockd0582a62009-12-15 09:54:21 +0000650// Flag that indicates how to generate code for the stub GenericBinaryOpStub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000651enum GenericBinaryFlags {
Steve Block3ce2e202009-11-05 08:53:23 +0000652 NO_GENERIC_BINARY_FLAGS = 0,
653 NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub.
Steve Blocka7e24c12009-10-30 11:49:00 +0000654};
655
656
657class GenericBinaryOpStub: public CodeStub {
658 public:
Steve Blockd0582a62009-12-15 09:54:21 +0000659 GenericBinaryOpStub(Token::Value op,
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 OverwriteMode mode,
661 GenericBinaryFlags flags)
Steve Blockd0582a62009-12-15 09:54:21 +0000662 : op_(op),
Steve Block3ce2e202009-11-05 08:53:23 +0000663 mode_(mode),
664 flags_(flags),
665 args_in_registers_(false),
Leon Clarkee46be812010-01-19 14:06:41 +0000666 args_reversed_(false),
667 name_(NULL) {
Steve Blockd0582a62009-12-15 09:54:21 +0000668 use_sse3_ = CpuFeatures::IsSupported(SSE3);
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 ASSERT(OpBits::is_valid(Token::NUM_TOKENS));
670 }
671
Steve Block3ce2e202009-11-05 08:53:23 +0000672 // Generate code to call the stub with the supplied arguments. This will add
673 // code at the call site to prepare arguments either in registers or on the
674 // stack together with the actual call.
675 void GenerateCall(MacroAssembler* masm, Register left, Register right);
676 void GenerateCall(MacroAssembler* masm, Register left, Smi* right);
677 void GenerateCall(MacroAssembler* masm, Smi* left, Register right);
Steve Blocka7e24c12009-10-30 11:49:00 +0000678
Leon Clarked91b9f72010-01-27 17:25:45 +0000679 Result GenerateCall(MacroAssembler* masm,
680 VirtualFrame* frame,
681 Result* left,
682 Result* right);
683
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 private:
685 Token::Value op_;
686 OverwriteMode mode_;
687 GenericBinaryFlags flags_;
Steve Block3ce2e202009-11-05 08:53:23 +0000688 bool args_in_registers_; // Arguments passed in registers not on the stack.
689 bool args_reversed_; // Left and right argument are swapped.
Steve Blocka7e24c12009-10-30 11:49:00 +0000690 bool use_sse3_;
Leon Clarkee46be812010-01-19 14:06:41 +0000691 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000692
693 const char* GetName();
694
695#ifdef DEBUG
696 void Print() {
Steve Block3ce2e202009-11-05 08:53:23 +0000697 PrintF("GenericBinaryOpStub (op %s), "
698 "(mode %d, flags %d, registers %d, reversed %d)\n",
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 Token::String(op_),
700 static_cast<int>(mode_),
Steve Block3ce2e202009-11-05 08:53:23 +0000701 static_cast<int>(flags_),
702 static_cast<int>(args_in_registers_),
703 static_cast<int>(args_reversed_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000704 }
705#endif
706
Steve Block3ce2e202009-11-05 08:53:23 +0000707 // Minor key encoding in 16 bits FRASOOOOOOOOOOMM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
Steve Block3ce2e202009-11-05 08:53:23 +0000709 class OpBits: public BitField<Token::Value, 2, 10> {};
710 class SSE3Bits: public BitField<bool, 12, 1> {};
711 class ArgsInRegistersBits: public BitField<bool, 13, 1> {};
712 class ArgsReversedBits: public BitField<bool, 14, 1> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 class FlagBits: public BitField<GenericBinaryFlags, 15, 1> {};
714
715 Major MajorKey() { return GenericBinaryOp; }
716 int MinorKey() {
717 // Encode the parameters in a unique 16 bit value.
718 return OpBits::encode(op_)
719 | ModeBits::encode(mode_)
720 | FlagBits::encode(flags_)
Steve Block3ce2e202009-11-05 08:53:23 +0000721 | SSE3Bits::encode(use_sse3_)
722 | ArgsInRegistersBits::encode(args_in_registers_)
723 | ArgsReversedBits::encode(args_reversed_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000724 }
Steve Block3ce2e202009-11-05 08:53:23 +0000725
Steve Blocka7e24c12009-10-30 11:49:00 +0000726 void Generate(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000727 void GenerateSmiCode(MacroAssembler* masm, Label* slow);
728 void GenerateLoadArguments(MacroAssembler* masm);
729 void GenerateReturn(MacroAssembler* masm);
Leon Clarked91b9f72010-01-27 17:25:45 +0000730 void GenerateHeapResultAllocation(MacroAssembler* masm, Label* alloc_failure);
Steve Block3ce2e202009-11-05 08:53:23 +0000731
732 bool ArgsInRegistersSupported() {
Leon Clarked91b9f72010-01-27 17:25:45 +0000733 return op_ == Token::ADD || op_ == Token::SUB
734 || op_ == Token::MUL || op_ == Token::DIV;
Steve Block3ce2e202009-11-05 08:53:23 +0000735 }
736 bool IsOperationCommutative() {
737 return (op_ == Token::ADD) || (op_ == Token::MUL);
738 }
739
740 void SetArgsInRegisters() { args_in_registers_ = true; }
741 void SetArgsReversed() { args_reversed_ = true; }
742 bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; }
Leon Clarked91b9f72010-01-27 17:25:45 +0000743 bool HasArgsInRegisters() { return args_in_registers_; }
744 bool HasArgsReversed() { return args_reversed_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000745};
746
747
Leon Clarkee46be812010-01-19 14:06:41 +0000748class StringStubBase: public CodeStub {
749 public:
750 // Generate code for copying characters using a simple loop. This should only
751 // be used in places where the number of characters is small and the
752 // additional setup and checking in GenerateCopyCharactersREP adds too much
753 // overhead. Copying of overlapping regions is not supported.
754 void GenerateCopyCharacters(MacroAssembler* masm,
755 Register dest,
756 Register src,
757 Register count,
758 Register scratch,
759 bool ascii);
760
761 // Generate code for copying characters using the rep movs instruction.
762 // Copies ecx characters from esi to edi. Copying of overlapping regions is
763 // not supported.
764 void GenerateCopyCharactersREP(MacroAssembler* masm,
765 Register dest, // Must be edi.
766 Register src, // Must be esi.
767 Register count, // Must be ecx.
768 Register scratch, // Neither of the above.
769 bool ascii);
770};
771
772
Andrei Popescu31002712010-02-23 13:46:05 +0000773// Flag that indicates how to generate code for the stub StringAddStub.
774enum StringAddFlags {
775 NO_STRING_ADD_FLAGS = 0,
776 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
777};
778
779
Leon Clarkee46be812010-01-19 14:06:41 +0000780class StringAddStub: public StringStubBase {
Steve Blockd0582a62009-12-15 09:54:21 +0000781 public:
782 explicit StringAddStub(StringAddFlags flags) {
783 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
784 }
785
786 private:
787 Major MajorKey() { return StringAdd; }
788 int MinorKey() { return string_check_ ? 0 : 1; }
789
790 void Generate(MacroAssembler* masm);
791
Steve Blockd0582a62009-12-15 09:54:21 +0000792 // Should the stub check whether arguments are strings?
793 bool string_check_;
794};
795
796
Leon Clarkee46be812010-01-19 14:06:41 +0000797class SubStringStub: public StringStubBase {
798 public:
799 SubStringStub() {}
800
801 private:
802 Major MajorKey() { return SubString; }
803 int MinorKey() { return 0; }
804
805 void Generate(MacroAssembler* masm);
806};
807
808
809class StringCompareStub: public StringStubBase {
810 public:
811 explicit StringCompareStub() {
812 }
813
814 // Compare two flat ascii strings and returns result in eax after popping two
815 // arguments from the stack.
816 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
817 Register left,
818 Register right,
819 Register scratch1,
820 Register scratch2,
821 Register scratch3);
822
823 private:
824 Major MajorKey() { return StringCompare; }
825 int MinorKey() { return 0; }
826
827 void Generate(MacroAssembler* masm);
828};
829
830
Steve Blocka7e24c12009-10-30 11:49:00 +0000831} } // namespace v8::internal
832
833#endif // V8_IA32_CODEGEN_IA32_H_