blob: 525b22b908fee1fee08457445ecb1dc7fb90a88f [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ager@chromium.org7c537e22008-10-16 08:43:32 +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.org5ec48922009-05-05 07:25:34 +000028#ifndef V8_ARM_CODEGEN_ARM_H_
29#define V8_ARM_CODEGEN_ARM_H_
ager@chromium.org7c537e22008-10-16 08:43:32 +000030
ager@chromium.org357bf652010-04-12 11:30:10 +000031#include "ic-inl.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
ager@chromium.org7c537e22008-10-16 08:43:32 +000035
36// Forward declarations
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000037class CompilationInfo;
ager@chromium.org7c537e22008-10-16 08:43:32 +000038class DeferredCode;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000039class RegisterAllocator;
40class RegisterFile;
ager@chromium.org7c537e22008-10-16 08:43:32 +000041
ager@chromium.org3bf7b912008-11-17 09:09:45 +000042enum InitState { CONST_INIT, NOT_CONST_INIT };
43enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
ager@chromium.org7c537e22008-10-16 08:43:32 +000044
ager@chromium.org3bf7b912008-11-17 09:09:45 +000045
46// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +000047// Reference support
48
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000049// A reference is a C++ stack-allocated object that puts a
50// reference on the virtual frame. The reference may be consumed
51// by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
52// When the lifetime (scope) of a valid reference ends, it must have
53// been consumed, and be in state UNLOADED.
ager@chromium.org7c537e22008-10-16 08:43:32 +000054class Reference BASE_EMBEDDED {
55 public:
56 // The values of the types is important, see size().
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000057 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
58 Reference(CodeGenerator* cgen,
59 Expression* expression,
60 bool persist_after_get = false);
ager@chromium.org7c537e22008-10-16 08:43:32 +000061 ~Reference();
62
63 Expression* expression() const { return expression_; }
64 Type type() const { return type_; }
65 void set_type(Type value) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000066 ASSERT_EQ(ILLEGAL, type_);
ager@chromium.org7c537e22008-10-16 08:43:32 +000067 type_ = value;
68 }
69
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000070 void set_unloaded() {
71 ASSERT_NE(ILLEGAL, type_);
72 ASSERT_NE(UNLOADED, type_);
73 type_ = UNLOADED;
74 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +000075 // The size the reference takes up on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000076 int size() const {
77 return (type_ < SLOT) ? 0 : type_;
78 }
ager@chromium.org7c537e22008-10-16 08:43:32 +000079
80 bool is_illegal() const { return type_ == ILLEGAL; }
81 bool is_slot() const { return type_ == SLOT; }
82 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000083 bool is_unloaded() const { return type_ == UNLOADED; }
ager@chromium.org7c537e22008-10-16 08:43:32 +000084
85 // Return the name. Only valid for named property references.
86 Handle<String> GetName();
87
88 // Generate code to push the value of the reference on top of the
89 // expression stack. The reference is expected to be already on top of
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000090 // the expression stack, and it is consumed by the call unless the
91 // reference is for a compound assignment.
92 // If the reference is not consumed, it is left in place under its value.
ager@chromium.orgc4c92722009-11-18 14:12:51 +000093 void GetValue();
ager@chromium.org7c537e22008-10-16 08:43:32 +000094
95 // Generate code to store the value on top of the expression stack in the
96 // reference. The reference is expected to be immediately below the value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000097 // on the expression stack. The value is stored in the location specified
98 // by the reference, and is left on top of the stack, after the reference
99 // is popped from beneath it (unloaded).
ager@chromium.org7c537e22008-10-16 08:43:32 +0000100 void SetValue(InitState init_state);
101
102 private:
103 CodeGenerator* cgen_;
104 Expression* expression_;
105 Type type_;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000106 // Keep the reference on the stack after get, so it can be used by set later.
107 bool persist_after_get_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000108};
109
110
111// -------------------------------------------------------------------------
112// Code generation state
113
114// The state is passed down the AST by the code generator (and back up, in
115// the form of the state of the label pair). It is threaded through the
116// call stack. Constructing a state implicitly pushes it on the owning code
117// generator's stack of states, and destroying one implicitly pops it.
118
119class CodeGenState BASE_EMBEDDED {
120 public:
121 // Create an initial code generator state. Destroying the initial state
122 // leaves the code generator with a NULL state.
123 explicit CodeGenState(CodeGenerator* owner);
124
125 // Create a code generator state based on a code generator's current
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000126 // state. The new state has its own pair of branch labels.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000127 CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000128 JumpTarget* true_target,
129 JumpTarget* false_target);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000130
131 // Destroy a code generator state and restore the owning code generator's
132 // previous state.
133 ~CodeGenState();
134
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000135 JumpTarget* true_target() const { return true_target_; }
136 JumpTarget* false_target() const { return false_target_; }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000137
138 private:
139 CodeGenerator* owner_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140 JumpTarget* true_target_;
141 JumpTarget* false_target_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000142 CodeGenState* previous_;
143};
144
145
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000146// -------------------------------------------------------------------------
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000147// Arguments allocation mode
148
149enum ArgumentsAllocationMode {
150 NO_ARGUMENTS_ALLOCATION,
151 EAGER_ARGUMENTS_ALLOCATION,
152 LAZY_ARGUMENTS_ALLOCATION
153};
154
155
156// Different nop operations are used by the code generator to detect certain
157// states of the generated code.
158enum NopMarkerTypes {
159 NON_MARKING_NOP = 0,
160 NAMED_PROPERTY_LOAD_INLINED
161};
162
163
164// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000165// CodeGenerator
166
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000167class CodeGenerator: public AstVisitor {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000168 public:
169 // Takes a function literal, generates code for it. This function should only
170 // be called by compiler.cc.
ager@chromium.org5c838252010-02-19 08:53:10 +0000171 static Handle<Code> MakeCode(CompilationInfo* info);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000172
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000173 // Printing of AST, etc. as requested by flags.
ager@chromium.org5c838252010-02-19 08:53:10 +0000174 static void MakeCodePrologue(CompilationInfo* info);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000175
176 // Allocate and install the code.
ager@chromium.org5c838252010-02-19 08:53:10 +0000177 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000178 Code::Flags flags,
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 CompilationInfo* info);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000180
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +0000181#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000182 static bool ShouldGenerateLog(Expression* type);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +0000183#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000184
ager@chromium.org7c537e22008-10-16 08:43:32 +0000185 static void SetFunctionInfo(Handle<JSFunction> fun,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000186 FunctionLiteral* lit,
ager@chromium.org7c537e22008-10-16 08:43:32 +0000187 bool is_toplevel,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000188 Handle<Script> script);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000189
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000190 static void RecordPositions(MacroAssembler* masm, int pos);
191
ager@chromium.org7c537e22008-10-16 08:43:32 +0000192 // Accessors
193 MacroAssembler* masm() { return masm_; }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000194 VirtualFrame* frame() const { return frame_; }
ager@chromium.org5c838252010-02-19 08:53:10 +0000195 inline Handle<Script> script();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000196
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000197 bool has_valid_frame() const { return frame_ != NULL; }
198
199 // Set the virtual frame to be new_frame, with non-frame register
200 // reference counts given by non_frame_registers. The non-frame
201 // register reference counts of the old frame are returned in
202 // non_frame_registers.
203 void SetFrame(VirtualFrame* new_frame, RegisterFile* non_frame_registers);
204
205 void DeleteFrame();
206
207 RegisterAllocator* allocator() const { return allocator_; }
208
ager@chromium.org7c537e22008-10-16 08:43:32 +0000209 CodeGenState* state() { return state_; }
210 void set_state(CodeGenState* state) { state_ = state; }
211
212 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
213
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000214 static const int kUnknownIntValue = -1;
215
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000216 // If the name is an inline runtime function call return the number of
217 // expected arguments. Otherwise return -1.
218 static int InlineRuntimeCallArgumentsCount(Handle<String> name);
219
ager@chromium.org7c537e22008-10-16 08:43:32 +0000220 private:
221 // Construction/Destruction
ager@chromium.org5c838252010-02-19 08:53:10 +0000222 explicit CodeGenerator(MacroAssembler* masm);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000223
224 // Accessors
ager@chromium.org5c838252010-02-19 08:53:10 +0000225 inline bool is_eval();
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000226 inline Scope* scope();
ager@chromium.org7c537e22008-10-16 08:43:32 +0000227
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000228 // Generating deferred code.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000229 void ProcessDeferred();
230
ager@chromium.org7c537e22008-10-16 08:43:32 +0000231 // State
232 bool has_cc() const { return cc_reg_ != al; }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000233 JumpTarget* true_target() const { return state_->true_target(); }
234 JumpTarget* false_target() const { return state_->false_target(); }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000235
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000236 // Track loop nesting level.
237 int loop_nesting() const { return loop_nesting_; }
238 void IncrementLoopNesting() { loop_nesting_++; }
239 void DecrementLoopNesting() { loop_nesting_--; }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000240
241 // Node visitors.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000242 void VisitStatements(ZoneList<Statement*>* statements);
243
ager@chromium.org7c537e22008-10-16 08:43:32 +0000244#define DEF_VISIT(type) \
245 void Visit##type(type* node);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000246 AST_NODE_LIST(DEF_VISIT)
ager@chromium.org7c537e22008-10-16 08:43:32 +0000247#undef DEF_VISIT
248
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000249 // Visit a statement and then spill the virtual frame if control flow can
250 // reach the end of the statement (ie, it does not exit via break,
251 // continue, return, or throw). This function is used temporarily while
252 // the code generator is being transformed.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000253 inline void VisitAndSpill(Statement* statement);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000254
255 // Visit a list of statements and then spill the virtual frame if control
256 // flow can reach the end of the list.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000257 inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000258
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 // Main code generation function
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000260 void Generate(CompilationInfo* info);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000261
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000262 // Returns the arguments allocation mode.
263 ArgumentsAllocationMode ArgumentsMode();
264
265 // Store the arguments object and allocate it if necessary.
266 void StoreArgumentsObject(bool initial);
267
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268 // The following are used by class Reference.
269 void LoadReference(Reference* ref);
270 void UnloadReference(Reference* ref);
271
ager@chromium.org3811b432009-10-28 14:53:37 +0000272 static MemOperand ContextOperand(Register context, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000273 return MemOperand(context, Context::SlotOffset(index));
274 }
275
276 MemOperand SlotOperand(Slot* slot, Register tmp);
277
ager@chromium.org381abbb2009-02-25 13:23:22 +0000278 MemOperand ContextSlotOperandCheckExtensions(Slot* slot,
279 Register tmp,
280 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000281 JumpTarget* slow);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000282
ager@chromium.org7c537e22008-10-16 08:43:32 +0000283 // Expressions
ager@chromium.org3811b432009-10-28 14:53:37 +0000284 static MemOperand GlobalObject() {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000285 return ContextOperand(cp, Context::GLOBAL_INDEX);
286 }
287
288 void LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000289 JumpTarget* true_target,
290 JumpTarget* false_target,
ager@chromium.org7c537e22008-10-16 08:43:32 +0000291 bool force_cc);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000292 void Load(Expression* expr);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000293 void LoadGlobal();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000294 void LoadGlobalReceiver(Register scratch);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000295
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000296 // Generate code to push the value of an expression on top of the frame
297 // and then spill the frame fully to memory. This function is used
298 // temporarily while the code generator is being transformed.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000299 inline void LoadAndSpill(Expression* expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000300
301 // Call LoadCondition and then spill the virtual frame unless control flow
302 // cannot reach the end of the expression (ie, by emitting only
303 // unconditional jumps to the control targets).
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000304 inline void LoadConditionAndSpill(Expression* expression,
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000305 JumpTarget* true_target,
306 JumpTarget* false_target,
307 bool force_control);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000308
ager@chromium.org7c537e22008-10-16 08:43:32 +0000309 // Read a value from a slot and leave it on top of the expression stack.
310 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000311 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000312 // Store the value on top of the stack to a slot.
313 void StoreToSlot(Slot* slot, InitState init_state);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000314
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000315 // Load a named property, leaving it in r0. The receiver is passed on the
316 // stack, and remains there.
317 void EmitNamedLoad(Handle<String> name, bool is_contextual);
318
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000319 // Load a keyed property, leaving it in r0. The receiver and key are
320 // passed on the stack, and remain there.
321 void EmitKeyedLoad(bool is_global);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000322
ager@chromium.org381abbb2009-02-25 13:23:22 +0000323 void LoadFromGlobalSlotCheckExtensions(Slot* slot,
324 TypeofState typeof_state,
325 Register tmp,
326 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000327 JumpTarget* slow);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000328
329 // Special code for typeof expressions: Unfortunately, we must
330 // be careful when loading the expression in 'typeof'
331 // expressions. We are not allowed to throw reference errors for
332 // non-existing properties of the global object, so we must make it
333 // look like an explicit property access, instead of an access
334 // through the context chain.
335 void LoadTypeofExpression(Expression* x);
336
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000337 void ToBoolean(JumpTarget* true_target, JumpTarget* false_target);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000338
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000339 // Generate code that computes a shortcutting logical operation.
340 void GenerateLogicalBooleanOperation(BinaryOperation* node);
341
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000342 void GenericBinaryOperation(Token::Value op,
343 OverwriteMode overwrite_mode,
344 int known_rhs = kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +0000345 void VirtualFrameBinaryOperation(Token::Value op,
346 OverwriteMode overwrite_mode,
347 int known_rhs = kUnknownIntValue);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000348 void Comparison(Condition cc,
349 Expression* left,
350 Expression* right,
351 bool strict = false);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000352
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000353 void SmiOperation(Token::Value op,
354 Handle<Object> value,
355 bool reversed,
356 OverwriteMode mode);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357
ager@chromium.org357bf652010-04-12 11:30:10 +0000358 void VirtualFrameSmiOperation(Token::Value op,
359 Handle<Object> value,
360 bool reversed,
361 OverwriteMode mode);
362
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000363 void CallWithArguments(ZoneList<Expression*>* arguments,
364 CallFunctionFlags flags,
365 int position);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000366
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000367 // An optimized implementation of expressions of the form
368 // x.apply(y, arguments). We call x the applicand and y the receiver.
369 // The optimization avoids allocating an arguments object if possible.
370 void CallApplyLazy(Expression* applicand,
371 Expression* receiver,
372 VariableProxy* arguments,
373 int position);
374
ager@chromium.org7c537e22008-10-16 08:43:32 +0000375 // Control flow
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000376 void Branch(bool if_true, JumpTarget* target);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000377 void CheckStack();
ager@chromium.org7c537e22008-10-16 08:43:32 +0000378
ager@chromium.org9085a012009-05-11 19:22:57 +0000379 struct InlineRuntimeLUT {
380 void (CodeGenerator::*method)(ZoneList<Expression*>*);
381 const char* name;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000382 int nargs;
ager@chromium.org9085a012009-05-11 19:22:57 +0000383 };
384
385 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000386 bool CheckForInlineRuntimeCall(CallRuntime* node);
ager@chromium.org9085a012009-05-11 19:22:57 +0000387 static bool PatchInlineRuntimeEntry(Handle<String> name,
388 const InlineRuntimeLUT& new_entry,
389 InlineRuntimeLUT* old_entry);
390
ager@chromium.org3811b432009-10-28 14:53:37 +0000391 static Handle<Code> ComputeLazyCompile(int argc);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000392 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
393
ager@chromium.org3811b432009-10-28 14:53:37 +0000394 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000395
396 // Declare global variables and functions in the given array of
397 // name/value pairs.
398 void DeclareGlobals(Handle<FixedArray> pairs);
399
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000400 // Instantiate the function based on the shared function info.
401 void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000402
403 // Support for type checks.
404 void GenerateIsSmi(ZoneList<Expression*>* args);
405 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
406 void GenerateIsArray(ZoneList<Expression*>* args);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000407 void GenerateIsRegExp(ZoneList<Expression*>* args);
ager@chromium.org6141cbe2009-11-20 12:14:52 +0000408 void GenerateIsObject(ZoneList<Expression*>* args);
409 void GenerateIsFunction(ZoneList<Expression*>* args);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000410 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000411
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000412 // Support for construct call checks.
413 void GenerateIsConstructCall(ZoneList<Expression*>* args);
414
ager@chromium.org7c537e22008-10-16 08:43:32 +0000415 // Support for arguments.length and arguments[?].
416 void GenerateArgumentsLength(ZoneList<Expression*>* args);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000417 void GenerateArguments(ZoneList<Expression*>* args);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000418
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000419 // Support for accessing the class and value fields of an object.
420 void GenerateClassOf(ZoneList<Expression*>* args);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000421 void GenerateValueOf(ZoneList<Expression*>* args);
422 void GenerateSetValueOf(ZoneList<Expression*>* args);
423
424 // Fast support for charCodeAt(n).
425 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
426
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000427 // Fast support for string.charAt(n) and string[n].
428 void GenerateCharFromCode(ZoneList<Expression*>* args);
429
ager@chromium.org7c537e22008-10-16 08:43:32 +0000430 // Fast support for object equality testing.
431 void GenerateObjectEquals(ZoneList<Expression*>* args);
432
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000433 void GenerateLog(ZoneList<Expression*>* args);
434
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000435 // Fast support for Math.random().
ager@chromium.org357bf652010-04-12 11:30:10 +0000436 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000437
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000438 // Fast support for StringAdd.
439 void GenerateStringAdd(ZoneList<Expression*>* args);
440
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000441 // Fast support for SubString.
442 void GenerateSubString(ZoneList<Expression*>* args);
443
444 // Fast support for StringCompare.
445 void GenerateStringCompare(ZoneList<Expression*>* args);
446
447 // Support for direct calls from JavaScript to native RegExp code.
448 void GenerateRegExpExec(ZoneList<Expression*>* args);
449
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000450 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
451
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000452 // Support for fast native caches.
453 void GenerateGetFromCache(ZoneList<Expression*>* args);
454
ager@chromium.org5c838252010-02-19 08:53:10 +0000455 // Fast support for number to string.
456 void GenerateNumberToString(ZoneList<Expression*>* args);
457
ager@chromium.org357bf652010-04-12 11:30:10 +0000458 // Fast call for custom callbacks.
459 void GenerateCallFunction(ZoneList<Expression*>* args);
460
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000461 // Fast call to math functions.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000462 void GenerateMathPow(ZoneList<Expression*>* args);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000463 void GenerateMathSin(ZoneList<Expression*>* args);
464 void GenerateMathCos(ZoneList<Expression*>* args);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000465 void GenerateMathSqrt(ZoneList<Expression*>* args);
466
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000467 // Simple condition analysis.
468 enum ConditionAnalysis {
469 ALWAYS_TRUE,
470 ALWAYS_FALSE,
471 DONT_KNOW
472 };
473 ConditionAnalysis AnalyzeCondition(Expression* cond);
474
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000475 // Methods used to indicate which source code is generated for. Source
476 // positions are collected by the assembler and emitted with the relocation
477 // information.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000478 void CodeForFunctionPosition(FunctionLiteral* fun);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000479 void CodeForReturnPosition(FunctionLiteral* fun);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000480 void CodeForStatementPosition(Statement* node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000481 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000482 void CodeForSourcePosition(int pos);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000483
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000484#ifdef DEBUG
485 // True if the registers are valid for entry to a block.
486 bool HasValidEntryRegisters();
487#endif
488
ager@chromium.org7c537e22008-10-16 08:43:32 +0000489 List<DeferredCode*> deferred_;
490
491 // Assembler
492 MacroAssembler* masm_; // to generate code
493
ager@chromium.org5c838252010-02-19 08:53:10 +0000494 CompilationInfo* info_;
495
ager@chromium.org7c537e22008-10-16 08:43:32 +0000496 // Code generation state
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000497 VirtualFrame* frame_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000498 RegisterAllocator* allocator_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000499 Condition cc_reg_;
500 CodeGenState* state_;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000501 int loop_nesting_;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000502
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000503 // Jump targets
504 BreakTarget function_return_;
505
506 // True if the function return is shadowed (ie, jumping to the target
507 // function_return_ does not jump to the true function return, but rather
508 // to some unlinking code).
509 bool function_return_is_shadowed_;
510
ager@chromium.org9085a012009-05-11 19:22:57 +0000511 static InlineRuntimeLUT kInlineRuntimeLUT[];
512
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000513 friend class VirtualFrame;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 friend class JumpTarget;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000515 friend class Reference;
ager@chromium.org3811b432009-10-28 14:53:37 +0000516 friend class FastCodeGenerator;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000517 friend class FullCodeGenerator;
518 friend class FullCodeGenSyntaxChecker;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000519
520 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
521};
522
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000524class GenericBinaryOpStub : public CodeStub {
525 public:
526 GenericBinaryOpStub(Token::Value op,
527 OverwriteMode mode,
ager@chromium.org357bf652010-04-12 11:30:10 +0000528 Register lhs,
529 Register rhs,
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000530 int constant_rhs = CodeGenerator::kUnknownIntValue)
531 : op_(op),
532 mode_(mode),
ager@chromium.org357bf652010-04-12 11:30:10 +0000533 lhs_(lhs),
534 rhs_(rhs),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000535 constant_rhs_(constant_rhs),
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000536 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
ager@chromium.org357bf652010-04-12 11:30:10 +0000537 runtime_operands_type_(BinaryOpIC::DEFAULT),
538 name_(NULL) { }
539
540 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info)
541 : op_(OpBits::decode(key)),
542 mode_(ModeBits::decode(key)),
543 lhs_(LhsRegister(RegisterBits::decode(key))),
544 rhs_(RhsRegister(RegisterBits::decode(key))),
545 constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))),
546 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)),
547 runtime_operands_type_(type_info),
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000548 name_(NULL) { }
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000549
550 private:
551 Token::Value op_;
552 OverwriteMode mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000553 Register lhs_;
554 Register rhs_;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000555 int constant_rhs_;
556 bool specialized_on_rhs_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000557 BinaryOpIC::TypeInfo runtime_operands_type_;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000558 char* name_;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000559
560 static const int kMaxKnownRhs = 0x40000000;
ager@chromium.org357bf652010-04-12 11:30:10 +0000561 static const int kKnownRhsKeyBits = 6;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000562
ager@chromium.org357bf652010-04-12 11:30:10 +0000563 // Minor key encoding in 17 bits.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000564 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
565 class OpBits: public BitField<Token::Value, 2, 6> {};
ager@chromium.org357bf652010-04-12 11:30:10 +0000566 class TypeInfoBits: public BitField<int, 8, 2> {};
567 class RegisterBits: public BitField<bool, 10, 1> {};
568 class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {};
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000569
570 Major MajorKey() { return GenericBinaryOp; }
571 int MinorKey() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000572 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
573 (lhs_.is(r1) && rhs_.is(r0)));
574 // Encode the parameters in a unique 18 bit value.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000575 return OpBits::encode(op_)
576 | ModeBits::encode(mode_)
ager@chromium.org357bf652010-04-12 11:30:10 +0000577 | KnownIntBits::encode(MinorKeyForKnownInt())
578 | TypeInfoBits::encode(runtime_operands_type_)
579 | RegisterBits::encode(lhs_.is(r0));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000580 }
581
582 void Generate(MacroAssembler* masm);
ager@chromium.org357bf652010-04-12 11:30:10 +0000583 void HandleNonSmiBitwiseOp(MacroAssembler* masm, Register lhs, Register rhs);
584 void HandleBinaryOpSlowCases(MacroAssembler* masm,
585 Label* not_smi,
586 Register lhs,
587 Register rhs,
588 const Builtins::JavaScript& builtin);
589 void GenerateTypeTransition(MacroAssembler* masm);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000590
591 static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
592 if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
593 if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
594 if (op == Token::MOD) {
595 if (constant_rhs <= 1) return false;
596 if (constant_rhs <= 10) return true;
597 if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
598 return false;
599 }
600 return false;
601 }
602
603 int MinorKeyForKnownInt() {
604 if (!specialized_on_rhs_) return 0;
605 if (constant_rhs_ <= 10) return constant_rhs_ + 1;
606 ASSERT(IsPowerOf2(constant_rhs_));
607 int key = 12;
608 int d = constant_rhs_;
609 while ((d & 1) == 0) {
610 key++;
611 d >>= 1;
612 }
ager@chromium.org357bf652010-04-12 11:30:10 +0000613 ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000614 return key;
615 }
616
ager@chromium.org357bf652010-04-12 11:30:10 +0000617 int KnownBitsForMinorKey(int key) {
618 if (!key) return 0;
619 if (key <= 11) return key - 1;
620 int d = 1;
621 while (key != 12) {
622 key--;
623 d <<= 1;
624 }
625 return d;
626 }
627
628 Register LhsRegister(bool lhs_is_r0) {
629 return lhs_is_r0 ? r0 : r1;
630 }
631
632 Register RhsRegister(bool lhs_is_r0) {
633 return lhs_is_r0 ? r1 : r0;
634 }
635
636 bool ShouldGenerateSmiCode() {
637 return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) &&
638 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
639 runtime_operands_type_ != BinaryOpIC::STRINGS;
640 }
641
642 bool ShouldGenerateFPCode() {
643 return runtime_operands_type_ != BinaryOpIC::STRINGS;
644 }
645
646 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
647
648 virtual InlineCacheState GetICState() {
649 return BinaryOpIC::ToState(runtime_operands_type_);
650 }
651
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000652 const char* GetName();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000653
654#ifdef DEBUG
655 void Print() {
656 if (!specialized_on_rhs_) {
657 PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
658 } else {
659 PrintF("GenericBinaryOpStub (%s by %d)\n",
660 Token::String(op_),
661 constant_rhs_);
662 }
663 }
664#endif
665};
666
667
ager@chromium.org5c838252010-02-19 08:53:10 +0000668class StringStubBase: public CodeStub {
669 public:
670 // Generate code for copying characters using a simple loop. This should only
671 // be used in places where the number of characters is small and the
672 // additional setup and checking in GenerateCopyCharactersLong adds too much
673 // overhead. Copying of overlapping regions is not supported.
674 // Dest register ends at the position after the last character written.
675 void GenerateCopyCharacters(MacroAssembler* masm,
676 Register dest,
677 Register src,
678 Register count,
679 Register scratch,
680 bool ascii);
681
682 // Generate code for copying a large number of characters. This function
683 // is allowed to spend extra time setting up conditions to make copying
684 // faster. Copying of overlapping regions is not supported.
685 // Dest register ends at the position after the last character written.
686 void GenerateCopyCharactersLong(MacroAssembler* masm,
687 Register dest,
688 Register src,
689 Register count,
690 Register scratch1,
691 Register scratch2,
692 Register scratch3,
693 Register scratch4,
694 Register scratch5,
695 int flags);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000696
697
698 // Probe the symbol table for a two character string. If the string is
699 // not found by probing a jump to the label not_found is performed. This jump
700 // does not guarantee that the string is not in the symbol table. If the
701 // string is found the code falls through with the string in register r0.
702 // Contents of both c1 and c2 registers are modified. At the exit c1 is
703 // guaranteed to contain halfword with low and high bytes equal to
704 // initial contents of c1 and c2 respectively.
705 void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
706 Register c1,
707 Register c2,
708 Register scratch1,
709 Register scratch2,
710 Register scratch3,
711 Register scratch4,
712 Register scratch5,
713 Label* not_found);
714
715 // Generate string hash.
716 void GenerateHashInit(MacroAssembler* masm,
717 Register hash,
718 Register character);
719
720 void GenerateHashAddCharacter(MacroAssembler* masm,
721 Register hash,
722 Register character);
723
724 void GenerateHashGetHash(MacroAssembler* masm,
725 Register hash);
ager@chromium.org5c838252010-02-19 08:53:10 +0000726};
727
728
729// Flag that indicates how to generate code for the stub StringAddStub.
730enum StringAddFlags {
731 NO_STRING_ADD_FLAGS = 0,
732 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
733};
734
735
736class StringAddStub: public StringStubBase {
737 public:
738 explicit StringAddStub(StringAddFlags flags) {
739 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
740 }
741
742 private:
743 Major MajorKey() { return StringAdd; }
744 int MinorKey() { return string_check_ ? 0 : 1; }
745
746 void Generate(MacroAssembler* masm);
747
748 // Should the stub check whether arguments are strings?
749 bool string_check_;
750};
751
752
753class SubStringStub: public StringStubBase {
754 public:
755 SubStringStub() {}
756
757 private:
758 Major MajorKey() { return SubString; }
759 int MinorKey() { return 0; }
760
761 void Generate(MacroAssembler* masm);
762};
763
764
765
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000766class StringCompareStub: public CodeStub {
767 public:
768 StringCompareStub() { }
769
770 // Compare two flat ASCII strings and returns result in r0.
771 // Does not use the stack.
772 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
773 Register left,
774 Register right,
775 Register scratch1,
776 Register scratch2,
777 Register scratch3,
778 Register scratch4);
779
780 private:
781 Major MajorKey() { return StringCompare; }
782 int MinorKey() { return 0; }
783
784 void Generate(MacroAssembler* masm);
785};
786
787
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000788// This stub can convert a signed int32 to a heap number (double). It does
789// not work for int32s that are in Smi range! No GC occurs during this stub
790// so you don't have to set up the frame.
791class WriteInt32ToHeapNumberStub : public CodeStub {
792 public:
793 WriteInt32ToHeapNumberStub(Register the_int,
794 Register the_heap_number,
795 Register scratch)
796 : the_int_(the_int),
797 the_heap_number_(the_heap_number),
798 scratch_(scratch) { }
799
800 private:
801 Register the_int_;
802 Register the_heap_number_;
803 Register scratch_;
804
805 // Minor key encoding in 16 bits.
806 class IntRegisterBits: public BitField<int, 0, 4> {};
807 class HeapNumberRegisterBits: public BitField<int, 4, 4> {};
808 class ScratchRegisterBits: public BitField<int, 8, 4> {};
809
810 Major MajorKey() { return WriteInt32ToHeapNumber; }
811 int MinorKey() {
812 // Encode the parameters in a unique 16 bit value.
813 return IntRegisterBits::encode(the_int_.code())
814 | HeapNumberRegisterBits::encode(the_heap_number_.code())
815 | ScratchRegisterBits::encode(scratch_.code());
816 }
817
818 void Generate(MacroAssembler* masm);
819
820 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
821
822#ifdef DEBUG
823 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
824#endif
825};
826
827
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000828class NumberToStringStub: public CodeStub {
829 public:
830 NumberToStringStub() { }
831
832 // Generate code to do a lookup in the number string cache. If the number in
833 // the register object is found in the cache the generated code falls through
834 // with the result in the result register. The object and the result register
835 // can be the same. If the number is not found in the cache the code jumps to
836 // the label not_found with only the content of register object unchanged.
837 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
838 Register object,
839 Register result,
840 Register scratch1,
841 Register scratch2,
842 bool object_is_smi,
843 Label* not_found);
844
845 private:
846 Major MajorKey() { return NumberToString; }
847 int MinorKey() { return 0; }
848
849 void Generate(MacroAssembler* masm);
850
851 const char* GetName() { return "NumberToStringStub"; }
852
853#ifdef DEBUG
854 void Print() {
855 PrintF("NumberToStringStub\n");
856 }
857#endif
858};
859
860
ager@chromium.org7c537e22008-10-16 08:43:32 +0000861} } // namespace v8::internal
862
ager@chromium.org5ec48922009-05-05 07:25:34 +0000863#endif // V8_ARM_CODEGEN_ARM_H_