blob: ce5f6d1d3a5772363965b38a3ec1e62030098168 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ager@chromium.org5ec48922009-05-05 07:25:34 +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
ager@chromium.org9085a012009-05-11 19:22:57 +000028#ifndef V8_X64_CODEGEN_X64_H_
29#define V8_X64_CODEGEN_X64_H_
30
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031namespace v8 {
32namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000033
34// Forward declarations
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000035class CompilationInfo;
ager@chromium.org9085a012009-05-11 19:22:57 +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
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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.
ager@chromium.org9085a012009-05-11 19:22:57 +000052class Reference BASE_EMBEDDED {
53 public:
54 // The values of the types is important, see size().
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000055 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
56
57 Reference(CodeGenerator* cgen,
58 Expression* expression,
59 bool persist_after_get = false);
ager@chromium.org9085a012009-05-11 19:22:57 +000060 ~Reference();
61
62 Expression* expression() const { return expression_; }
63 Type type() const { return type_; }
64 void set_type(Type value) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000065 ASSERT_EQ(ILLEGAL, type_);
ager@chromium.org9085a012009-05-11 19:22:57 +000066 type_ = value;
67 }
68
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000069 void set_unloaded() {
70 ASSERT_NE(ILLEGAL, type_);
71 ASSERT_NE(UNLOADED, type_);
72 type_ = UNLOADED;
73 }
ager@chromium.org9085a012009-05-11 19:22:57 +000074 // The size the reference takes up on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000075 int size() const {
76 return (type_ < SLOT) ? 0 : type_;
77 }
ager@chromium.org9085a012009-05-11 19:22:57 +000078
79 bool is_illegal() const { return type_ == ILLEGAL; }
80 bool is_slot() const { return type_ == SLOT; }
81 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000082 bool is_unloaded() const { return type_ == UNLOADED; }
ager@chromium.org9085a012009-05-11 19:22:57 +000083
84 // Return the name. Only valid for named property references.
85 Handle<String> GetName();
86
87 // Generate code to push the value of the reference on top of the
88 // expression stack. The reference is expected to be already on top of
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000089 // the expression stack, and it is consumed by the call unless the
90 // reference is for a compound assignment.
91 // If the reference is not consumed, it is left in place under its value.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000092 void GetValue();
ager@chromium.org9085a012009-05-11 19:22:57 +000093
94 // Like GetValue except that the slot is expected to be written to before
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000095 // being read from again. The value of the reference may be invalidated,
ager@chromium.org9085a012009-05-11 19:22:57 +000096 // causing subsequent attempts to read it to fail.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000097 void TakeValue();
ager@chromium.org9085a012009-05-11 19:22:57 +000098
99 // Generate code to store the value on top of the expression stack in the
100 // reference. The reference is expected to be immediately below the value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000101 // on the expression stack. The value is stored in the location specified
102 // by the reference, and is left on top of the stack, after the reference
103 // is popped from beneath it (unloaded).
ager@chromium.org9085a012009-05-11 19:22:57 +0000104 void SetValue(InitState init_state);
105
106 private:
107 CodeGenerator* cgen_;
108 Expression* expression_;
109 Type type_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000110 bool persist_after_get_;
ager@chromium.org9085a012009-05-11 19:22:57 +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
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000258 // state. The new state has its own control destination.
259 CodeGenState(CodeGenerator* owner, ControlDestination* destination);
ager@chromium.org9085a012009-05-11 19:22:57 +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.
ager@chromium.org9085a012009-05-11 19:22:57 +0000266 ControlDestination* destination() const { return destination_; }
267
268 private:
269 // The owning code generator.
270 CodeGenerator* owner_;
271
ager@chromium.org9085a012009-05-11 19:22:57 +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
ager@chromium.org3e875802009-06-29 08:26:34 +0000282// -------------------------------------------------------------------------
283// Arguments allocation mode
284
285enum ArgumentsAllocationMode {
286 NO_ARGUMENTS_ALLOCATION,
287 EAGER_ARGUMENTS_ALLOCATION,
288 LAZY_ARGUMENTS_ALLOCATION
289};
ager@chromium.org9085a012009-05-11 19:22:57 +0000290
291
292// -------------------------------------------------------------------------
293// CodeGenerator
294
295class CodeGenerator: public AstVisitor {
296 public:
297 // Takes a function literal, generates code for it. This function should only
298 // be called by compiler.cc.
ager@chromium.org5c838252010-02-19 08:53:10 +0000299 static Handle<Code> MakeCode(CompilationInfo* info);
ager@chromium.org9085a012009-05-11 19:22:57 +0000300
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000301 // Printing of AST, etc. as requested by flags.
ager@chromium.org5c838252010-02-19 08:53:10 +0000302 static void MakeCodePrologue(CompilationInfo* info);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000303
304 // Allocate and install the code.
ager@chromium.org5c838252010-02-19 08:53:10 +0000305 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000306 Code::Flags flags,
ager@chromium.org5c838252010-02-19 08:53:10 +0000307 CompilationInfo* info);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000308
ager@chromium.org9085a012009-05-11 19:22:57 +0000309#ifdef ENABLE_LOGGING_AND_PROFILING
310 static bool ShouldGenerateLog(Expression* type);
311#endif
312
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000313 static void RecordPositions(MacroAssembler* masm, int pos);
314
ager@chromium.org9085a012009-05-11 19:22:57 +0000315 // Accessors
316 MacroAssembler* masm() { return masm_; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000317 VirtualFrame* frame() const { return frame_; }
ager@chromium.org5c838252010-02-19 08:53:10 +0000318 inline Handle<Script> script();
ager@chromium.org9085a012009-05-11 19:22:57 +0000319
320 bool has_valid_frame() const { return frame_ != NULL; }
321
322 // Set the virtual frame to be new_frame, with non-frame register
323 // reference counts given by non_frame_registers. The non-frame
324 // register reference counts of the old frame are returned in
325 // non_frame_registers.
326 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
327
328 void DeleteFrame();
329
330 RegisterAllocator* allocator() const { return allocator_; }
331
332 CodeGenState* state() { return state_; }
333 void set_state(CodeGenState* state) { state_ = state; }
334
335 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
336
337 bool in_spilled_code() const { return in_spilled_code_; }
338 void set_in_spilled_code(bool flag) { in_spilled_code_ = flag; }
339
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000340 // If the name is an inline runtime function call return the number of
341 // expected arguments. Otherwise return -1.
342 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
343
ager@chromium.org9085a012009-05-11 19:22:57 +0000344 private:
345 // Construction/Destruction
ager@chromium.org5c838252010-02-19 08:53:10 +0000346 explicit CodeGenerator(MacroAssembler* masm);
ager@chromium.org9085a012009-05-11 19:22:57 +0000347
348 // Accessors
ager@chromium.org5c838252010-02-19 08:53:10 +0000349 inline bool is_eval();
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000350 inline Scope* scope();
ager@chromium.org9085a012009-05-11 19:22:57 +0000351
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000352 // Generating deferred code.
ager@chromium.org9085a012009-05-11 19:22:57 +0000353 void ProcessDeferred();
354
ager@chromium.org9085a012009-05-11 19:22:57 +0000355 // State
ager@chromium.org9085a012009-05-11 19:22:57 +0000356 ControlDestination* destination() const { return state_->destination(); }
357
358 // Track loop nesting level.
359 int loop_nesting() const { return loop_nesting_; }
360 void IncrementLoopNesting() { loop_nesting_++; }
361 void DecrementLoopNesting() { loop_nesting_--; }
362
363
364 // Node visitors.
365 void VisitStatements(ZoneList<Statement*>* statements);
366
367#define DEF_VISIT(type) \
368 void Visit##type(type* node);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000369 AST_NODE_LIST(DEF_VISIT)
ager@chromium.org9085a012009-05-11 19:22:57 +0000370#undef DEF_VISIT
371
372 // Visit a statement and then spill the virtual frame if control flow can
373 // reach the end of the statement (ie, it does not exit via break,
374 // continue, return, or throw). This function is used temporarily while
375 // the code generator is being transformed.
376 void VisitAndSpill(Statement* statement);
377
378 // Visit a list of statements and then spill the virtual frame if control
379 // flow can reach the end of the list.
380 void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
381
382 // Main code generation function
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000383 void Generate(CompilationInfo* info);
ager@chromium.org9085a012009-05-11 19:22:57 +0000384
385 // Generate the return sequence code. Should be called no more than
386 // once per compiled function, immediately after binding the return
387 // target (which can not be done more than once).
388 void GenerateReturnSequence(Result* return_value);
389
ager@chromium.org3e875802009-06-29 08:26:34 +0000390 // Returns the arguments allocation mode.
ager@chromium.org5c838252010-02-19 08:53:10 +0000391 ArgumentsAllocationMode ArgumentsMode();
ager@chromium.org3e875802009-06-29 08:26:34 +0000392
393 // Store the arguments object and allocate it if necessary.
394 Result StoreArgumentsObject(bool initial);
395
ager@chromium.org9085a012009-05-11 19:22:57 +0000396 // The following are used by class Reference.
397 void LoadReference(Reference* ref);
398 void UnloadReference(Reference* ref);
399
ager@chromium.org3811b432009-10-28 14:53:37 +0000400 static Operand ContextOperand(Register context, int index) {
ager@chromium.org9085a012009-05-11 19:22:57 +0000401 return Operand(context, Context::SlotOffset(index));
402 }
403
404 Operand SlotOperand(Slot* slot, Register tmp);
405
406 Operand ContextSlotOperandCheckExtensions(Slot* slot,
407 Result tmp,
408 JumpTarget* slow);
409
410 // Expressions
ager@chromium.org3811b432009-10-28 14:53:37 +0000411 static Operand GlobalObject() {
ager@chromium.org9085a012009-05-11 19:22:57 +0000412 return ContextOperand(rsi, Context::GLOBAL_INDEX);
413 }
414
415 void LoadCondition(Expression* x,
ager@chromium.org9085a012009-05-11 19:22:57 +0000416 ControlDestination* destination,
417 bool force_control);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000418 void Load(Expression* expr);
ager@chromium.org9085a012009-05-11 19:22:57 +0000419 void LoadGlobal();
420 void LoadGlobalReceiver();
421
422 // Generate code to push the value of an expression on top of the frame
423 // and then spill the frame fully to memory. This function is used
424 // temporarily while the code generator is being transformed.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000425 void LoadAndSpill(Expression* expression);
ager@chromium.org9085a012009-05-11 19:22:57 +0000426
427 // Read a value from a slot and leave it on top of the expression stack.
428 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000429 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
ager@chromium.org9085a012009-05-11 19:22:57 +0000430 Result LoadFromGlobalSlotCheckExtensions(Slot* slot,
431 TypeofState typeof_state,
432 JumpTarget* slow);
433
434 // Store the value on top of the expression stack into a slot, leaving the
435 // value in place.
436 void StoreToSlot(Slot* slot, InitState init_state);
437
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000438 // Load a property of an object, returning it in a Result.
439 // The object and the property name are passed on the stack, and
440 // not changed.
441 Result EmitKeyedLoad(bool is_global);
442
ager@chromium.org9085a012009-05-11 19:22:57 +0000443 // Special code for typeof expressions: Unfortunately, we must
444 // be careful when loading the expression in 'typeof'
445 // expressions. We are not allowed to throw reference errors for
446 // non-existing properties of the global object, so we must make it
447 // look like an explicit property access, instead of an access
448 // through the context chain.
449 void LoadTypeofExpression(Expression* x);
450
451 // Translate the value on top of the frame into control flow to the
452 // control destination.
453 void ToBoolean(ControlDestination* destination);
454
455 void GenericBinaryOperation(
456 Token::Value op,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000457 StaticType* type,
ager@chromium.org9085a012009-05-11 19:22:57 +0000458 OverwriteMode overwrite_mode);
459
460 // If possible, combine two constant smi values using op to produce
461 // a smi result, and push it on the virtual frame, all at compile time.
462 // Returns true if it succeeds. Otherwise it has no effect.
463 bool FoldConstantSmis(Token::Value op, int left, int right);
464
465 // Emit code to perform a binary operation on a constant
466 // smi and a likely smi. Consumes the Result *operand.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000467 Result ConstantSmiBinaryOperation(Token::Value op,
468 Result* operand,
469 Handle<Object> constant_operand,
470 StaticType* type,
471 bool reversed,
472 OverwriteMode overwrite_mode);
ager@chromium.org9085a012009-05-11 19:22:57 +0000473
474 // Emit code to perform a binary operation on two likely smis.
475 // The code to handle smi arguments is produced inline.
476 // Consumes the Results *left and *right.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000477 Result LikelySmiBinaryOperation(Token::Value op,
478 Result* left,
479 Result* right,
480 OverwriteMode overwrite_mode);
ager@chromium.org9085a012009-05-11 19:22:57 +0000481
ager@chromium.org5c838252010-02-19 08:53:10 +0000482 void Comparison(AstNode* node,
483 Condition cc,
ager@chromium.org9085a012009-05-11 19:22:57 +0000484 bool strict,
485 ControlDestination* destination);
486
487 // To prevent long attacker-controlled byte sequences, integer constants
488 // from the JavaScript source are loaded in two parts if they are larger
489 // than 16 bits.
490 static const int kMaxSmiInlinedBits = 16;
491 bool IsUnsafeSmi(Handle<Object> value);
492 // Load an integer constant x into a register target using
493 // at most 16 bits of user-controlled data per assembly operation.
494 void LoadUnsafeSmi(Register target, Handle<Object> value);
495
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000496 void CallWithArguments(ZoneList<Expression*>* arguments,
497 CallFunctionFlags flags,
498 int position);
ager@chromium.org9085a012009-05-11 19:22:57 +0000499
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000500 // An optimized implementation of expressions of the form
501 // x.apply(y, arguments). We call x the applicand and y the receiver.
502 // The optimization avoids allocating an arguments object if possible.
503 void CallApplyLazy(Expression* applicand,
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000504 Expression* receiver,
505 VariableProxy* arguments,
506 int position);
507
ager@chromium.org9085a012009-05-11 19:22:57 +0000508 void CheckStack();
509
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000510 struct InlineRuntimeLUT {
511 void (CodeGenerator::*method)(ZoneList<Expression*>*);
512 const char* name;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000513 int nargs;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000514 };
515 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
ager@chromium.org9085a012009-05-11 19:22:57 +0000516 bool CheckForInlineRuntimeCall(CallRuntime* node);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000517 static bool PatchInlineRuntimeEntry(Handle<String> name,
518 const InlineRuntimeLUT& new_entry,
519 InlineRuntimeLUT* old_entry);
ager@chromium.org9085a012009-05-11 19:22:57 +0000520 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
521
ager@chromium.org3811b432009-10-28 14:53:37 +0000522 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
ager@chromium.org9085a012009-05-11 19:22:57 +0000523
524 // Declare global variables and functions in the given array of
525 // name/value pairs.
526 void DeclareGlobals(Handle<FixedArray> pairs);
527
528 // Instantiate the function boilerplate.
529 void InstantiateBoilerplate(Handle<JSFunction> boilerplate);
530
531 // Support for type checks.
532 void GenerateIsSmi(ZoneList<Expression*>* args);
533 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
534 void GenerateIsArray(ZoneList<Expression*>* args);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000535 void GenerateIsRegExp(ZoneList<Expression*>* args);
ager@chromium.org6141cbe2009-11-20 12:14:52 +0000536 void GenerateIsObject(ZoneList<Expression*>* args);
537 void GenerateIsFunction(ZoneList<Expression*>* args);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000538 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
ager@chromium.org9085a012009-05-11 19:22:57 +0000539
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000540 // Support for construct call checks.
541 void GenerateIsConstructCall(ZoneList<Expression*>* args);
542
ager@chromium.org9085a012009-05-11 19:22:57 +0000543 // Support for arguments.length and arguments[?].
544 void GenerateArgumentsLength(ZoneList<Expression*>* args);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000545 void GenerateArguments(ZoneList<Expression*>* args);
ager@chromium.org9085a012009-05-11 19:22:57 +0000546
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000547 // Support for accessing the class and value fields of an object.
548 void GenerateClassOf(ZoneList<Expression*>* args);
ager@chromium.org9085a012009-05-11 19:22:57 +0000549 void GenerateValueOf(ZoneList<Expression*>* args);
550 void GenerateSetValueOf(ZoneList<Expression*>* args);
551
552 // Fast support for charCodeAt(n).
553 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
554
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000555 // Fast support for string.charAt(n) and string[n].
556 void GenerateCharFromCode(ZoneList<Expression*>* args);
557
ager@chromium.org9085a012009-05-11 19:22:57 +0000558 // Fast support for object equality testing.
559 void GenerateObjectEquals(ZoneList<Expression*>* args);
560
561 void GenerateLog(ZoneList<Expression*>* args);
562
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000563 void GenerateGetFramePointer(ZoneList<Expression*>* args);
564
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000565 // Fast support for Math.random().
566 void GenerateRandomPositiveSmi(ZoneList<Expression*>* args);
567
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000568 // Fast support for StringAdd.
569 void GenerateStringAdd(ZoneList<Expression*>* args);
570
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000571 // Fast support for SubString.
572 void GenerateSubString(ZoneList<Expression*>* args);
573
574 // Fast support for StringCompare.
575 void GenerateStringCompare(ZoneList<Expression*>* args);
576
577 // Support for direct calls from JavaScript to native RegExp code.
578 void GenerateRegExpExec(ZoneList<Expression*>* args);
579
ager@chromium.org5c838252010-02-19 08:53:10 +0000580 // Fast support for number to string.
581 void GenerateNumberToString(ZoneList<Expression*>* args);
582
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000583 // Fast call to math functions.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000584 void GenerateMathPow(ZoneList<Expression*>* args);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000585 void GenerateMathSin(ZoneList<Expression*>* args);
586 void GenerateMathCos(ZoneList<Expression*>* args);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000587 void GenerateMathSqrt(ZoneList<Expression*>* args);
588
589// Simple condition analysis.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000590 enum ConditionAnalysis {
591 ALWAYS_TRUE,
592 ALWAYS_FALSE,
593 DONT_KNOW
594 };
595 ConditionAnalysis AnalyzeCondition(Expression* cond);
596
ager@chromium.org9085a012009-05-11 19:22:57 +0000597 // Methods used to indicate which source code is generated for. Source
598 // positions are collected by the assembler and emitted with the relocation
599 // information.
600 void CodeForFunctionPosition(FunctionLiteral* fun);
601 void CodeForReturnPosition(FunctionLiteral* fun);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000602 void CodeForStatementPosition(Statement* node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000603 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
ager@chromium.org9085a012009-05-11 19:22:57 +0000604 void CodeForSourcePosition(int pos);
605
606#ifdef DEBUG
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000607 // True if the registers are valid for entry to a block. There should
608 // be no frame-external references to (non-reserved) registers.
ager@chromium.org9085a012009-05-11 19:22:57 +0000609 bool HasValidEntryRegisters();
610#endif
611
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000612 ZoneList<DeferredCode*> deferred_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000613
614 // Assembler
615 MacroAssembler* masm_; // to generate code
616
ager@chromium.org5c838252010-02-19 08:53:10 +0000617 CompilationInfo* info_;
618
ager@chromium.org9085a012009-05-11 19:22:57 +0000619 // Code generation state
ager@chromium.org9085a012009-05-11 19:22:57 +0000620 VirtualFrame* frame_;
621 RegisterAllocator* allocator_;
622 CodeGenState* state_;
623 int loop_nesting_;
624
625 // Jump targets.
626 // The target of the return from the function.
627 BreakTarget function_return_;
628
629 // True if the function return is shadowed (ie, jumping to the target
630 // function_return_ does not jump to the true function return, but rather
631 // to some unlinking code).
632 bool function_return_is_shadowed_;
633
634 // True when we are in code that expects the virtual frame to be fully
635 // spilled. Some virtual frame function are disabled in DEBUG builds when
636 // called from spilled code, because they do not leave the virtual frame
637 // in a spilled state.
638 bool in_spilled_code_;
639
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000640 static InlineRuntimeLUT kInlineRuntimeLUT[];
641
ager@chromium.org9085a012009-05-11 19:22:57 +0000642 friend class VirtualFrame;
643 friend class JumpTarget;
644 friend class Reference;
645 friend class Result;
ager@chromium.org3811b432009-10-28 14:53:37 +0000646 friend class FastCodeGenerator;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000647 friend class FullCodeGenerator;
648 friend class FullCodeGenSyntaxChecker;
ager@chromium.org9085a012009-05-11 19:22:57 +0000649
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000650 friend class CodeGeneratorPatcher; // Used in test-log-stack-tracer.cc
651
ager@chromium.org9085a012009-05-11 19:22:57 +0000652 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
653};
654
655
ager@chromium.org3811b432009-10-28 14:53:37 +0000656// Flag that indicates how to generate code for the stub GenericBinaryOpStub.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000657enum GenericBinaryFlags {
ager@chromium.org3811b432009-10-28 14:53:37 +0000658 NO_GENERIC_BINARY_FLAGS = 0,
659 NO_SMI_CODE_IN_STUB = 1 << 0 // Omit smi code in stub.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000660};
661
662
663class GenericBinaryOpStub: public CodeStub {
664 public:
665 GenericBinaryOpStub(Token::Value op,
666 OverwriteMode mode,
ager@chromium.org5c838252010-02-19 08:53:10 +0000667 GenericBinaryFlags flags,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000668 NumberInfo operands_type = NumberInfo::Unknown())
ager@chromium.org3811b432009-10-28 14:53:37 +0000669 : op_(op),
670 mode_(mode),
671 flags_(flags),
672 args_in_registers_(false),
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000673 args_reversed_(false),
ager@chromium.org5c838252010-02-19 08:53:10 +0000674 name_(NULL),
675 operands_type_(operands_type) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000676 use_sse3_ = CpuFeatures::IsSupported(SSE3);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000677 ASSERT(OpBits::is_valid(Token::NUM_TOKENS));
678 }
679
ager@chromium.org3811b432009-10-28 14:53:37 +0000680 // Generate code to call the stub with the supplied arguments. This will add
681 // code at the call site to prepare arguments either in registers or on the
682 // stack together with the actual call.
683 void GenerateCall(MacroAssembler* masm, Register left, Register right);
684 void GenerateCall(MacroAssembler* masm, Register left, Smi* right);
685 void GenerateCall(MacroAssembler* masm, Smi* left, Register right);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000686
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000687 Result GenerateCall(MacroAssembler* masm,
688 VirtualFrame* frame,
689 Result* left,
690 Result* right);
691
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000692 private:
693 Token::Value op_;
694 OverwriteMode mode_;
695 GenericBinaryFlags flags_;
ager@chromium.org3811b432009-10-28 14:53:37 +0000696 bool args_in_registers_; // Arguments passed in registers not on the stack.
697 bool args_reversed_; // Left and right argument are swapped.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000698 bool use_sse3_;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000699 char* name_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000700 NumberInfo operands_type_;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000701
702 const char* GetName();
703
704#ifdef DEBUG
705 void Print() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000706 PrintF("GenericBinaryOpStub %d (op %s), "
707 "(mode %d, flags %d, registers %d, reversed %d, only_numbers %s)\n",
708 MinorKey(),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000709 Token::String(op_),
710 static_cast<int>(mode_),
ager@chromium.org3811b432009-10-28 14:53:37 +0000711 static_cast<int>(flags_),
712 static_cast<int>(args_in_registers_),
ager@chromium.org5c838252010-02-19 08:53:10 +0000713 static_cast<int>(args_reversed_),
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000714 operands_type_.ToString());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000715 }
716#endif
717
ager@chromium.org5c838252010-02-19 08:53:10 +0000718 // Minor key encoding in 16 bits NNNFRASOOOOOOOMM.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000719 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
ager@chromium.org5c838252010-02-19 08:53:10 +0000720 class OpBits: public BitField<Token::Value, 2, 7> {};
721 class SSE3Bits: public BitField<bool, 9, 1> {};
722 class ArgsInRegistersBits: public BitField<bool, 10, 1> {};
723 class ArgsReversedBits: public BitField<bool, 11, 1> {};
724 class FlagBits: public BitField<GenericBinaryFlags, 12, 1> {};
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000725 class NumberInfoBits: public BitField<int, 13, 3> {};
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000726
727 Major MajorKey() { return GenericBinaryOp; }
728 int MinorKey() {
729 // Encode the parameters in a unique 16 bit value.
730 return OpBits::encode(op_)
ager@chromium.org3811b432009-10-28 14:53:37 +0000731 | ModeBits::encode(mode_)
732 | FlagBits::encode(flags_)
733 | SSE3Bits::encode(use_sse3_)
734 | ArgsInRegistersBits::encode(args_in_registers_)
ager@chromium.org5c838252010-02-19 08:53:10 +0000735 | ArgsReversedBits::encode(args_reversed_)
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000736 | NumberInfoBits::encode(operands_type_.ThreeBitRepresentation());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000737 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000738
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000739 void Generate(MacroAssembler* masm);
ager@chromium.org3811b432009-10-28 14:53:37 +0000740 void GenerateSmiCode(MacroAssembler* masm, Label* slow);
741 void GenerateLoadArguments(MacroAssembler* masm);
742 void GenerateReturn(MacroAssembler* masm);
743
744 bool ArgsInRegistersSupported() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000745 return (op_ == Token::ADD) || (op_ == Token::SUB)
746 || (op_ == Token::MUL) || (op_ == Token::DIV);
ager@chromium.org3811b432009-10-28 14:53:37 +0000747 }
748 bool IsOperationCommutative() {
749 return (op_ == Token::ADD) || (op_ == Token::MUL);
750 }
751
752 void SetArgsInRegisters() { args_in_registers_ = true; }
753 void SetArgsReversed() { args_reversed_ = true; }
754 bool HasSmiCodeInStub() { return (flags_ & NO_SMI_CODE_IN_STUB) == 0; }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000755 bool HasArgsInRegisters() { return args_in_registers_; }
756 bool HasArgsReversed() { return args_reversed_; }
757};
758
759
760class StringStubBase: public CodeStub {
761 public:
762 // Generate code for copying characters using a simple loop. This should only
763 // be used in places where the number of characters is small and the
764 // additional setup and checking in GenerateCopyCharactersREP adds too much
765 // overhead. Copying of overlapping regions is not supported.
766 void GenerateCopyCharacters(MacroAssembler* masm,
767 Register dest,
768 Register src,
769 Register count,
770 bool ascii);
771
772 // Generate code for copying characters using the rep movs instruction.
773 // Copies rcx characters from rsi to rdi. Copying of overlapping regions is
774 // not supported.
775 void GenerateCopyCharactersREP(MacroAssembler* masm,
776 Register dest, // Must be rdi.
777 Register src, // Must be rsi.
778 Register count, // Must be rcx.
779 bool ascii);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000780
781
782 // Probe the symbol table for a two character string. If the string is
783 // not found by probing a jump to the label not_found is performed. This jump
784 // does not guarantee that the string is not in the symbol table. If the
785 // string is found the code falls through with the string in register rax.
786 void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
787 Register c1,
788 Register c2,
789 Register scratch1,
790 Register scratch2,
791 Register scratch3,
792 Register scratch4,
793 Label* not_found);
794
795 // Generate string hash.
796 void GenerateHashInit(MacroAssembler* masm,
797 Register hash,
798 Register character,
799 Register scratch);
800 void GenerateHashAddCharacter(MacroAssembler* masm,
801 Register hash,
802 Register character,
803 Register scratch);
804 void GenerateHashGetHash(MacroAssembler* masm,
805 Register hash,
806 Register scratch);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000807};
808
809
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000810// Flag that indicates how to generate code for the stub StringAddStub.
811enum StringAddFlags {
812 NO_STRING_ADD_FLAGS = 0,
813 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
814};
815
816
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000817class StringAddStub: public StringStubBase {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000818 public:
819 explicit StringAddStub(StringAddFlags flags) {
820 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
821 }
822
823 private:
824 Major MajorKey() { return StringAdd; }
825 int MinorKey() { return string_check_ ? 0 : 1; }
826
827 void Generate(MacroAssembler* masm);
828
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000829 // Should the stub check whether arguments are strings?
830 bool string_check_;
831};
832
833
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000834class SubStringStub: public StringStubBase {
835 public:
836 SubStringStub() {}
837
838 private:
839 Major MajorKey() { return SubString; }
840 int MinorKey() { return 0; }
841
842 void Generate(MacroAssembler* masm);
843};
844
845
846class StringCompareStub: public CodeStub {
847 public:
848 explicit StringCompareStub() {}
849
850 // Compare two flat ascii strings and returns result in rax after popping two
851 // arguments from the stack.
852 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
853 Register left,
854 Register right,
855 Register scratch1,
856 Register scratch2,
857 Register scratch3,
858 Register scratch4);
859
860 private:
861 Major MajorKey() { return StringCompare; }
862 int MinorKey() { return 0; }
863
864 void Generate(MacroAssembler* masm);
865};
866
867
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000868class NumberToStringStub: public CodeStub {
869 public:
870 NumberToStringStub() { }
871
872 // Generate code to do a lookup in the number string cache. If the number in
873 // the register object is found in the cache the generated code falls through
874 // with the result in the result register. The object and the result register
875 // can be the same. If the number is not found in the cache the code jumps to
876 // the label not_found with only the content of register object unchanged.
877 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
878 Register object,
879 Register result,
880 Register scratch1,
881 Register scratch2,
882 bool object_is_smi,
883 Label* not_found);
884
885 private:
886 Major MajorKey() { return NumberToString; }
887 int MinorKey() { return 0; }
888
889 void Generate(MacroAssembler* masm);
890
891 const char* GetName() { return "NumberToStringStub"; }
892
893#ifdef DEBUG
894 void Print() {
895 PrintF("NumberToStringStub\n");
896 }
897#endif
898};
899
900
ager@chromium.org9085a012009-05-11 19:22:57 +0000901} } // namespace v8::internal
902
903#endif // V8_X64_CODEGEN_X64_H_