blob: bb76b633bd845e9a507993ea5b9dd47ec04f0513 [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_ARM_CODEGEN_ARM_H_
29#define V8_ARM_CODEGEN_ARM_H_
30
Steve Block6ded16b2010-05-10 14:33:55 +010031#include "ic-inl.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
36// Forward declarations
Leon Clarke4515c472010-02-03 11:58:03 +000037class CompilationInfo;
Steve Blocka7e24c12009-10-30 11:49:00 +000038class DeferredCode;
39class RegisterAllocator;
40class RegisterFile;
41
42enum InitState { CONST_INIT, NOT_CONST_INIT };
43enum TypeofState { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
44
45
46// -------------------------------------------------------------------------
47// Reference support
48
Leon Clarked91b9f72010-01-27 17:25:45 +000049// A reference is a C++ stack-allocated object that puts a
50// reference on the virtual frame. The reference may be consumed
51// by GetValue, TakeValue, SetValue, and Codegen::UnloadReference.
52// When the lifetime (scope) of a valid reference ends, it must have
53// been consumed, and be in state UNLOADED.
Steve Blocka7e24c12009-10-30 11:49:00 +000054class Reference BASE_EMBEDDED {
55 public:
56 // The values of the types is important, see size().
Leon Clarked91b9f72010-01-27 17:25:45 +000057 enum Type { UNLOADED = -2, ILLEGAL = -1, SLOT = 0, NAMED = 1, KEYED = 2 };
58 Reference(CodeGenerator* cgen,
59 Expression* expression,
60 bool persist_after_get = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000061 ~Reference();
62
63 Expression* expression() const { return expression_; }
64 Type type() const { return type_; }
65 void set_type(Type value) {
Leon Clarked91b9f72010-01-27 17:25:45 +000066 ASSERT_EQ(ILLEGAL, type_);
Steve Blocka7e24c12009-10-30 11:49:00 +000067 type_ = value;
68 }
69
Leon Clarked91b9f72010-01-27 17:25:45 +000070 void set_unloaded() {
71 ASSERT_NE(ILLEGAL, type_);
72 ASSERT_NE(UNLOADED, type_);
73 type_ = UNLOADED;
74 }
Steve Blocka7e24c12009-10-30 11:49:00 +000075 // The size the reference takes up on the stack.
Leon Clarked91b9f72010-01-27 17:25:45 +000076 int size() const {
77 return (type_ < SLOT) ? 0 : type_;
78 }
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80 bool is_illegal() const { return type_ == ILLEGAL; }
81 bool is_slot() const { return type_ == SLOT; }
82 bool is_property() const { return type_ == NAMED || type_ == KEYED; }
Leon Clarked91b9f72010-01-27 17:25:45 +000083 bool is_unloaded() const { return type_ == UNLOADED; }
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85 // Return the name. Only valid for named property references.
86 Handle<String> GetName();
87
88 // Generate code to push the value of the reference on top of the
89 // expression stack. The reference is expected to be already on top of
Leon Clarked91b9f72010-01-27 17:25:45 +000090 // the expression stack, and it is consumed by the call unless the
91 // reference is for a compound assignment.
92 // If the reference is not consumed, it is left in place under its value.
Steve Blockd0582a62009-12-15 09:54:21 +000093 void GetValue();
Steve Blocka7e24c12009-10-30 11:49:00 +000094
Steve Blocka7e24c12009-10-30 11:49:00 +000095 // 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
Leon Clarked91b9f72010-01-27 17:25:45 +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).
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 void SetValue(InitState init_state);
101
102 private:
103 CodeGenerator* cgen_;
104 Expression* expression_;
105 Type type_;
Leon Clarked91b9f72010-01-27 17:25:45 +0000106 // Keep the reference on the stack after get, so it can be used by set later.
107 bool persist_after_get_;
Steve Blocka7e24c12009-10-30 11:49:00 +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
Steve Blockd0582a62009-12-15 09:54:21 +0000126 // state. The new state has its own pair of branch labels.
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 CodeGenState(CodeGenerator* owner,
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 JumpTarget* true_target,
129 JumpTarget* false_target);
130
131 // Destroy a code generator state and restore the owning code generator's
132 // previous state.
133 ~CodeGenState();
134
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 JumpTarget* true_target() const { return true_target_; }
136 JumpTarget* false_target() const { return false_target_; }
137
138 private:
139 CodeGenerator* owner_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 JumpTarget* true_target_;
141 JumpTarget* false_target_;
142 CodeGenState* previous_;
143};
144
145
146// -------------------------------------------------------------------------
Steve Block6ded16b2010-05-10 14:33:55 +0100147// 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 PROPERTY_ACCESS_INLINED
161};
162
163
164// -------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000165// CodeGenerator
166
167class CodeGenerator: public AstVisitor {
168 public:
169 // Takes a function literal, generates code for it. This function should only
170 // be called by compiler.cc.
Andrei Popescu31002712010-02-23 13:46:05 +0000171 static Handle<Code> MakeCode(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000172
Steve Block3ce2e202009-11-05 08:53:23 +0000173 // Printing of AST, etc. as requested by flags.
Andrei Popescu31002712010-02-23 13:46:05 +0000174 static void MakeCodePrologue(CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000175
176 // Allocate and install the code.
Andrei Popescu31002712010-02-23 13:46:05 +0000177 static Handle<Code> MakeCodeEpilogue(MacroAssembler* masm,
Steve Block3ce2e202009-11-05 08:53:23 +0000178 Code::Flags flags,
Andrei Popescu31002712010-02-23 13:46:05 +0000179 CompilationInfo* info);
Steve Block3ce2e202009-11-05 08:53:23 +0000180
Steve Blocka7e24c12009-10-30 11:49:00 +0000181#ifdef ENABLE_LOGGING_AND_PROFILING
182 static bool ShouldGenerateLog(Expression* type);
183#endif
184
185 static void SetFunctionInfo(Handle<JSFunction> fun,
186 FunctionLiteral* lit,
187 bool is_toplevel,
188 Handle<Script> script);
189
Steve Block3ce2e202009-11-05 08:53:23 +0000190 static void RecordPositions(MacroAssembler* masm, int pos);
191
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 // Accessors
193 MacroAssembler* masm() { return masm_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 VirtualFrame* frame() const { return frame_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000195 inline Handle<Script> script();
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
197 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
209 CodeGenState* state() { return state_; }
210 void set_state(CodeGenState* state) { state_ = state; }
211
212 void AddDeferred(DeferredCode* code) { deferred_.Add(code); }
213
214 static const int kUnknownIntValue = -1;
215
Steve Block6ded16b2010-05-10 14:33:55 +0100216 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 private:
221 // Construction/Destruction
Andrei Popescu31002712010-02-23 13:46:05 +0000222 explicit CodeGenerator(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000223
224 // Accessors
Andrei Popescu31002712010-02-23 13:46:05 +0000225 inline bool is_eval();
Steve Block6ded16b2010-05-10 14:33:55 +0100226 inline Scope* scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
228 // Generating deferred code.
229 void ProcessDeferred();
230
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 // State
232 bool has_cc() const { return cc_reg_ != al; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 JumpTarget* true_target() const { return state_->true_target(); }
234 JumpTarget* false_target() const { return state_->false_target(); }
235
Steve Block6ded16b2010-05-10 14:33:55 +0100236 // Track loop nesting level.
237 int loop_nesting() const { return loop_nesting_; }
238 void IncrementLoopNesting() { loop_nesting_++; }
239 void DecrementLoopNesting() { loop_nesting_--; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000240
241 // Node visitors.
242 void VisitStatements(ZoneList<Statement*>* statements);
243
244#define DEF_VISIT(type) \
245 void Visit##type(type* node);
246 AST_NODE_LIST(DEF_VISIT)
247#undef DEF_VISIT
248
249 // 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.
253 inline void VisitAndSpill(Statement* statement);
254
255 // Visit a list of statements and then spill the virtual frame if control
256 // flow can reach the end of the list.
257 inline void VisitStatementsAndSpill(ZoneList<Statement*>* statements);
258
259 // Main code generation function
Andrei Popescu402d9372010-02-26 13:31:12 +0000260 void Generate(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000261
Steve Block6ded16b2010-05-10 14:33:55 +0100262 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000268 // The following are used by class Reference.
269 void LoadReference(Reference* ref);
270 void UnloadReference(Reference* ref);
271
Steve Block3ce2e202009-11-05 08:53:23 +0000272 static MemOperand ContextOperand(Register context, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000273 return MemOperand(context, Context::SlotOffset(index));
274 }
275
276 MemOperand SlotOperand(Slot* slot, Register tmp);
277
278 MemOperand ContextSlotOperandCheckExtensions(Slot* slot,
279 Register tmp,
280 Register tmp2,
281 JumpTarget* slow);
282
283 // Expressions
Steve Block3ce2e202009-11-05 08:53:23 +0000284 static MemOperand GlobalObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 return ContextOperand(cp, Context::GLOBAL_INDEX);
286 }
287
288 void LoadCondition(Expression* x,
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 JumpTarget* true_target,
290 JumpTarget* false_target,
291 bool force_cc);
Steve Blockd0582a62009-12-15 09:54:21 +0000292 void Load(Expression* expr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 void LoadGlobal();
294 void LoadGlobalReceiver(Register scratch);
295
296 // 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.
Steve Blockd0582a62009-12-15 09:54:21 +0000299 inline void LoadAndSpill(Expression* expression);
Steve Blocka7e24c12009-10-30 11:49:00 +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).
304 inline void LoadConditionAndSpill(Expression* expression,
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 JumpTarget* true_target,
306 JumpTarget* false_target,
307 bool force_control);
308
309 // Read a value from a slot and leave it on top of the expression stack.
310 void LoadFromSlot(Slot* slot, TypeofState typeof_state);
Steve Block6ded16b2010-05-10 14:33:55 +0100311 void LoadFromSlotCheckForArguments(Slot* slot, TypeofState state);
Leon Clarkee46be812010-01-19 14:06:41 +0000312 // Store the value on top of the stack to a slot.
313 void StoreToSlot(Slot* slot, InitState init_state);
Steve Block6ded16b2010-05-10 14:33:55 +0100314
315 // Support for compiling assignment expressions.
316 void EmitSlotAssignment(Assignment* node);
317 void EmitNamedPropertyAssignment(Assignment* node);
318 void EmitKeyedPropertyAssignment(Assignment* node);
319
320 // Load a named property, returning it in r0. The receiver is passed on the
321 // stack, and remains there.
322 void EmitNamedLoad(Handle<String> name, bool is_contextual);
323
324 // Store to a named property. If the store is contextual, value is passed on
325 // the frame and consumed. Otherwise, receiver and value are passed on the
326 // frame and consumed. The result is returned in r0.
327 void EmitNamedStore(Handle<String> name, bool is_contextual);
328
Leon Clarked91b9f72010-01-27 17:25:45 +0000329 // Load a keyed property, leaving it in r0. The receiver and key are
330 // passed on the stack, and remain there.
Steve Block6ded16b2010-05-10 14:33:55 +0100331 void EmitKeyedLoad();
332
333 // Store a keyed property. Key and receiver are on the stack and the value is
334 // in r0. Result is returned in r0.
335 void EmitKeyedStore(StaticType* key_type);
Leon Clarkee46be812010-01-19 14:06:41 +0000336
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 void LoadFromGlobalSlotCheckExtensions(Slot* slot,
338 TypeofState typeof_state,
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 JumpTarget* slow);
340
341 // Special code for typeof expressions: Unfortunately, we must
342 // be careful when loading the expression in 'typeof'
343 // expressions. We are not allowed to throw reference errors for
344 // non-existing properties of the global object, so we must make it
345 // look like an explicit property access, instead of an access
346 // through the context chain.
347 void LoadTypeofExpression(Expression* x);
348
349 void ToBoolean(JumpTarget* true_target, JumpTarget* false_target);
350
Steve Block6ded16b2010-05-10 14:33:55 +0100351 // Generate code that computes a shortcutting logical operation.
352 void GenerateLogicalBooleanOperation(BinaryOperation* node);
353
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 void GenericBinaryOperation(Token::Value op,
355 OverwriteMode overwrite_mode,
356 int known_rhs = kUnknownIntValue);
Steve Block6ded16b2010-05-10 14:33:55 +0100357 void VirtualFrameBinaryOperation(Token::Value op,
358 OverwriteMode overwrite_mode,
359 int known_rhs = kUnknownIntValue);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 void Comparison(Condition cc,
361 Expression* left,
362 Expression* right,
363 bool strict = false);
364
365 void SmiOperation(Token::Value op,
366 Handle<Object> value,
367 bool reversed,
368 OverwriteMode mode);
369
Leon Clarkee46be812010-01-19 14:06:41 +0000370 void CallWithArguments(ZoneList<Expression*>* arguments,
371 CallFunctionFlags flags,
372 int position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000373
Steve Block6ded16b2010-05-10 14:33:55 +0100374 // An optimized implementation of expressions of the form
375 // x.apply(y, arguments). We call x the applicand and y the receiver.
376 // The optimization avoids allocating an arguments object if possible.
377 void CallApplyLazy(Expression* applicand,
378 Expression* receiver,
379 VariableProxy* arguments,
380 int position);
381
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 // Control flow
383 void Branch(bool if_true, JumpTarget* target);
384 void CheckStack();
385
386 struct InlineRuntimeLUT {
387 void (CodeGenerator::*method)(ZoneList<Expression*>*);
388 const char* name;
Steve Block6ded16b2010-05-10 14:33:55 +0100389 int nargs;
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 };
391
392 static InlineRuntimeLUT* FindInlineRuntimeLUT(Handle<String> name);
393 bool CheckForInlineRuntimeCall(CallRuntime* node);
394 static bool PatchInlineRuntimeEntry(Handle<String> name,
395 const InlineRuntimeLUT& new_entry,
396 InlineRuntimeLUT* old_entry);
397
Steve Block3ce2e202009-11-05 08:53:23 +0000398 static Handle<Code> ComputeLazyCompile(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 void ProcessDeclarations(ZoneList<Declaration*>* declarations);
400
Steve Block3ce2e202009-11-05 08:53:23 +0000401 static Handle<Code> ComputeCallInitialize(int argc, InLoopFlag in_loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000402
403 // Declare global variables and functions in the given array of
404 // name/value pairs.
405 void DeclareGlobals(Handle<FixedArray> pairs);
406
Steve Block6ded16b2010-05-10 14:33:55 +0100407 // Instantiate the function based on the shared function info.
408 void InstantiateFunction(Handle<SharedFunctionInfo> function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000409
410 // Support for type checks.
411 void GenerateIsSmi(ZoneList<Expression*>* args);
412 void GenerateIsNonNegativeSmi(ZoneList<Expression*>* args);
413 void GenerateIsArray(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000414 void GenerateIsRegExp(ZoneList<Expression*>* args);
Steve Blockd0582a62009-12-15 09:54:21 +0000415 void GenerateIsObject(ZoneList<Expression*>* args);
416 void GenerateIsFunction(ZoneList<Expression*>* args);
Leon Clarked91b9f72010-01-27 17:25:45 +0000417 void GenerateIsUndetectableObject(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000418
419 // Support for construct call checks.
420 void GenerateIsConstructCall(ZoneList<Expression*>* args);
421
422 // Support for arguments.length and arguments[?].
423 void GenerateArgumentsLength(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100424 void GenerateArguments(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000425
426 // Support for accessing the class and value fields of an object.
427 void GenerateClassOf(ZoneList<Expression*>* args);
428 void GenerateValueOf(ZoneList<Expression*>* args);
429 void GenerateSetValueOf(ZoneList<Expression*>* args);
430
431 // Fast support for charCodeAt(n).
432 void GenerateFastCharCodeAt(ZoneList<Expression*>* args);
433
Steve Block6ded16b2010-05-10 14:33:55 +0100434 // Fast support for string.charAt(n) and string[n].
435 void GenerateCharFromCode(ZoneList<Expression*>* args);
436
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 // Fast support for object equality testing.
438 void GenerateObjectEquals(ZoneList<Expression*>* args);
439
440 void GenerateLog(ZoneList<Expression*>* args);
441
442 // Fast support for Math.random().
Steve Block6ded16b2010-05-10 14:33:55 +0100443 void GenerateRandomHeapNumber(ZoneList<Expression*>* args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000444
Steve Blockd0582a62009-12-15 09:54:21 +0000445 // Fast support for StringAdd.
446 void GenerateStringAdd(ZoneList<Expression*>* args);
447
Leon Clarkee46be812010-01-19 14:06:41 +0000448 // Fast support for SubString.
449 void GenerateSubString(ZoneList<Expression*>* args);
450
451 // Fast support for StringCompare.
452 void GenerateStringCompare(ZoneList<Expression*>* args);
453
454 // Support for direct calls from JavaScript to native RegExp code.
455 void GenerateRegExpExec(ZoneList<Expression*>* args);
456
Steve Block6ded16b2010-05-10 14:33:55 +0100457 void GenerateRegExpConstructResult(ZoneList<Expression*>* args);
458
459 // Support for fast native caches.
460 void GenerateGetFromCache(ZoneList<Expression*>* args);
461
Andrei Popescu402d9372010-02-26 13:31:12 +0000462 // Fast support for number to string.
463 void GenerateNumberToString(ZoneList<Expression*>* args);
464
Steve Block6ded16b2010-05-10 14:33:55 +0100465 // Fast swapping of elements.
466 void GenerateSwapElements(ZoneList<Expression*>* args);
467
468 // Fast call for custom callbacks.
469 void GenerateCallFunction(ZoneList<Expression*>* args);
470
471 // Fast call to math functions.
472 void GenerateMathPow(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000473 void GenerateMathSin(ZoneList<Expression*>* args);
474 void GenerateMathCos(ZoneList<Expression*>* args);
Steve Block6ded16b2010-05-10 14:33:55 +0100475 void GenerateMathSqrt(ZoneList<Expression*>* args);
Andrei Popescu402d9372010-02-26 13:31:12 +0000476
Steve Block3ce2e202009-11-05 08:53:23 +0000477 // Simple condition analysis.
478 enum ConditionAnalysis {
479 ALWAYS_TRUE,
480 ALWAYS_FALSE,
481 DONT_KNOW
482 };
483 ConditionAnalysis AnalyzeCondition(Expression* cond);
484
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 // Methods used to indicate which source code is generated for. Source
486 // positions are collected by the assembler and emitted with the relocation
487 // information.
488 void CodeForFunctionPosition(FunctionLiteral* fun);
489 void CodeForReturnPosition(FunctionLiteral* fun);
490 void CodeForStatementPosition(Statement* node);
Steve Blockd0582a62009-12-15 09:54:21 +0000491 void CodeForDoWhileConditionPosition(DoWhileStatement* stmt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000492 void CodeForSourcePosition(int pos);
493
494#ifdef DEBUG
495 // True if the registers are valid for entry to a block.
496 bool HasValidEntryRegisters();
497#endif
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 List<DeferredCode*> deferred_;
500
501 // Assembler
502 MacroAssembler* masm_; // to generate code
503
Andrei Popescu31002712010-02-23 13:46:05 +0000504 CompilationInfo* info_;
505
Steve Blocka7e24c12009-10-30 11:49:00 +0000506 // Code generation state
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 VirtualFrame* frame_;
508 RegisterAllocator* allocator_;
509 Condition cc_reg_;
510 CodeGenState* state_;
Steve Block6ded16b2010-05-10 14:33:55 +0100511 int loop_nesting_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000512
513 // Jump targets
514 BreakTarget function_return_;
515
516 // True if the function return is shadowed (ie, jumping to the target
517 // function_return_ does not jump to the true function return, but rather
518 // to some unlinking code).
519 bool function_return_is_shadowed_;
520
521 static InlineRuntimeLUT kInlineRuntimeLUT[];
522
523 friend class VirtualFrame;
524 friend class JumpTarget;
525 friend class Reference;
Leon Clarke4515c472010-02-03 11:58:03 +0000526 friend class FastCodeGenerator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000527 friend class FullCodeGenerator;
528 friend class FullCodeGenSyntaxChecker;
Steve Blocka7e24c12009-10-30 11:49:00 +0000529
530 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
531};
532
533
Steve Blocka7e24c12009-10-30 11:49:00 +0000534class GenericBinaryOpStub : public CodeStub {
535 public:
536 GenericBinaryOpStub(Token::Value op,
537 OverwriteMode mode,
Steve Block6ded16b2010-05-10 14:33:55 +0100538 Register lhs,
539 Register rhs,
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 int constant_rhs = CodeGenerator::kUnknownIntValue)
541 : op_(op),
542 mode_(mode),
Steve Block6ded16b2010-05-10 14:33:55 +0100543 lhs_(lhs),
544 rhs_(rhs),
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 constant_rhs_(constant_rhs),
Leon Clarkee46be812010-01-19 14:06:41 +0000546 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)),
Steve Block6ded16b2010-05-10 14:33:55 +0100547 runtime_operands_type_(BinaryOpIC::DEFAULT),
548 name_(NULL) { }
549
550 GenericBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info)
551 : op_(OpBits::decode(key)),
552 mode_(ModeBits::decode(key)),
553 lhs_(LhsRegister(RegisterBits::decode(key))),
554 rhs_(RhsRegister(RegisterBits::decode(key))),
555 constant_rhs_(KnownBitsForMinorKey(KnownIntBits::decode(key))),
556 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op_, constant_rhs_)),
557 runtime_operands_type_(type_info),
Leon Clarkee46be812010-01-19 14:06:41 +0000558 name_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000559
560 private:
561 Token::Value op_;
562 OverwriteMode mode_;
Steve Block6ded16b2010-05-10 14:33:55 +0100563 Register lhs_;
564 Register rhs_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 int constant_rhs_;
566 bool specialized_on_rhs_;
Steve Block6ded16b2010-05-10 14:33:55 +0100567 BinaryOpIC::TypeInfo runtime_operands_type_;
Leon Clarkee46be812010-01-19 14:06:41 +0000568 char* name_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000569
570 static const int kMaxKnownRhs = 0x40000000;
Steve Block6ded16b2010-05-10 14:33:55 +0100571 static const int kKnownRhsKeyBits = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +0000572
Steve Block6ded16b2010-05-10 14:33:55 +0100573 // Minor key encoding in 17 bits.
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
575 class OpBits: public BitField<Token::Value, 2, 6> {};
Steve Block6ded16b2010-05-10 14:33:55 +0100576 class TypeInfoBits: public BitField<int, 8, 2> {};
577 class RegisterBits: public BitField<bool, 10, 1> {};
578 class KnownIntBits: public BitField<int, 11, kKnownRhsKeyBits> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000579
580 Major MajorKey() { return GenericBinaryOp; }
581 int MinorKey() {
Steve Block6ded16b2010-05-10 14:33:55 +0100582 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
583 (lhs_.is(r1) && rhs_.is(r0)));
584 // Encode the parameters in a unique 18 bit value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 return OpBits::encode(op_)
586 | ModeBits::encode(mode_)
Steve Block6ded16b2010-05-10 14:33:55 +0100587 | KnownIntBits::encode(MinorKeyForKnownInt())
588 | TypeInfoBits::encode(runtime_operands_type_)
589 | RegisterBits::encode(lhs_.is(r0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 }
591
592 void Generate(MacroAssembler* masm);
Steve Block6ded16b2010-05-10 14:33:55 +0100593 void HandleNonSmiBitwiseOp(MacroAssembler* masm, Register lhs, Register rhs);
594 void HandleBinaryOpSlowCases(MacroAssembler* masm,
595 Label* not_smi,
596 Register lhs,
597 Register rhs,
598 const Builtins::JavaScript& builtin);
599 void GenerateTypeTransition(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000600
601 static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
602 if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
603 if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
604 if (op == Token::MOD) {
605 if (constant_rhs <= 1) return false;
606 if (constant_rhs <= 10) return true;
607 if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
608 return false;
609 }
610 return false;
611 }
612
613 int MinorKeyForKnownInt() {
614 if (!specialized_on_rhs_) return 0;
615 if (constant_rhs_ <= 10) return constant_rhs_ + 1;
616 ASSERT(IsPowerOf2(constant_rhs_));
617 int key = 12;
618 int d = constant_rhs_;
619 while ((d & 1) == 0) {
620 key++;
621 d >>= 1;
622 }
Steve Block6ded16b2010-05-10 14:33:55 +0100623 ASSERT(key >= 0 && key < (1 << kKnownRhsKeyBits));
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 return key;
625 }
626
Steve Block6ded16b2010-05-10 14:33:55 +0100627 int KnownBitsForMinorKey(int key) {
628 if (!key) return 0;
629 if (key <= 11) return key - 1;
630 int d = 1;
631 while (key != 12) {
632 key--;
633 d <<= 1;
634 }
635 return d;
636 }
637
638 Register LhsRegister(bool lhs_is_r0) {
639 return lhs_is_r0 ? r0 : r1;
640 }
641
642 Register RhsRegister(bool lhs_is_r0) {
643 return lhs_is_r0 ? r1 : r0;
644 }
645
646 bool ShouldGenerateSmiCode() {
647 return ((op_ != Token::DIV && op_ != Token::MOD) || specialized_on_rhs_) &&
648 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
649 runtime_operands_type_ != BinaryOpIC::STRINGS;
650 }
651
652 bool ShouldGenerateFPCode() {
653 return runtime_operands_type_ != BinaryOpIC::STRINGS;
654 }
655
656 virtual int GetCodeKind() { return Code::BINARY_OP_IC; }
657
658 virtual InlineCacheState GetICState() {
659 return BinaryOpIC::ToState(runtime_operands_type_);
660 }
661
Leon Clarkee46be812010-01-19 14:06:41 +0000662 const char* GetName();
Steve Blocka7e24c12009-10-30 11:49:00 +0000663
664#ifdef DEBUG
665 void Print() {
666 if (!specialized_on_rhs_) {
667 PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
668 } else {
669 PrintF("GenericBinaryOpStub (%s by %d)\n",
670 Token::String(op_),
671 constant_rhs_);
672 }
673 }
674#endif
675};
676
677
Steve Block6ded16b2010-05-10 14:33:55 +0100678class StringHelper : public AllStatic {
Andrei Popescu31002712010-02-23 13:46:05 +0000679 public:
Steve Block6ded16b2010-05-10 14:33:55 +0100680 // Generates fast code for getting a char code out of a string
681 // object at the given index. May bail out for four reasons (in the
682 // listed order):
683 // * Receiver is not a string (receiver_not_string label).
684 // * Index is not a smi (index_not_smi label).
685 // * Index is out of range (index_out_of_range).
686 // * Some other reason (slow_case label). In this case it's
687 // guaranteed that the above conditions are not violated,
688 // e.g. it's safe to assume the receiver is a string and the
689 // index is a non-negative smi < length.
690 // When successful, object, index, and scratch are clobbered.
691 // Otherwise, scratch and result are clobbered.
692 static void GenerateFastCharCodeAt(MacroAssembler* masm,
693 Register object,
694 Register index,
695 Register scratch,
696 Register result,
697 Label* receiver_not_string,
698 Label* index_not_smi,
699 Label* index_out_of_range,
700 Label* slow_case);
701
702 // Generates code for creating a one-char string from the given char
703 // code. May do a runtime call, so any register can be clobbered
704 // and, if the given invoke flag specifies a call, an internal frame
705 // is required. In tail call mode the result must be r0 register.
706 static void GenerateCharFromCode(MacroAssembler* masm,
707 Register code,
708 Register scratch,
709 Register result,
710 InvokeFlag flag);
711
Andrei Popescu31002712010-02-23 13:46:05 +0000712 // Generate code for copying characters using a simple loop. This should only
713 // be used in places where the number of characters is small and the
714 // additional setup and checking in GenerateCopyCharactersLong adds too much
715 // overhead. Copying of overlapping regions is not supported.
716 // Dest register ends at the position after the last character written.
Steve Block6ded16b2010-05-10 14:33:55 +0100717 static void GenerateCopyCharacters(MacroAssembler* masm,
718 Register dest,
719 Register src,
720 Register count,
721 Register scratch,
722 bool ascii);
Andrei Popescu31002712010-02-23 13:46:05 +0000723
724 // Generate code for copying a large number of characters. This function
725 // is allowed to spend extra time setting up conditions to make copying
726 // faster. Copying of overlapping regions is not supported.
727 // Dest register ends at the position after the last character written.
Steve Block6ded16b2010-05-10 14:33:55 +0100728 static void GenerateCopyCharactersLong(MacroAssembler* masm,
729 Register dest,
730 Register src,
731 Register count,
732 Register scratch1,
733 Register scratch2,
734 Register scratch3,
735 Register scratch4,
736 Register scratch5,
737 int flags);
738
739
740 // Probe the symbol table for a two character string. If the string is
741 // not found by probing a jump to the label not_found is performed. This jump
742 // does not guarantee that the string is not in the symbol table. If the
743 // string is found the code falls through with the string in register r0.
744 // Contents of both c1 and c2 registers are modified. At the exit c1 is
745 // guaranteed to contain halfword with low and high bytes equal to
746 // initial contents of c1 and c2 respectively.
747 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
748 Register c1,
749 Register c2,
750 Register scratch1,
751 Register scratch2,
752 Register scratch3,
753 Register scratch4,
754 Register scratch5,
755 Label* not_found);
756
757 // Generate string hash.
758 static void GenerateHashInit(MacroAssembler* masm,
759 Register hash,
760 Register character);
761
762 static void GenerateHashAddCharacter(MacroAssembler* masm,
763 Register hash,
764 Register character);
765
766 static void GenerateHashGetHash(MacroAssembler* masm,
767 Register hash);
768
769 private:
770 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
Andrei Popescu31002712010-02-23 13:46:05 +0000771};
772
773
774// Flag that indicates how to generate code for the stub StringAddStub.
775enum StringAddFlags {
776 NO_STRING_ADD_FLAGS = 0,
777 NO_STRING_CHECK_IN_STUB = 1 << 0 // Omit string check in stub.
778};
779
780
Steve Block6ded16b2010-05-10 14:33:55 +0100781class StringAddStub: public CodeStub {
Andrei Popescu31002712010-02-23 13:46:05 +0000782 public:
783 explicit StringAddStub(StringAddFlags flags) {
784 string_check_ = ((flags & NO_STRING_CHECK_IN_STUB) == 0);
785 }
786
787 private:
788 Major MajorKey() { return StringAdd; }
789 int MinorKey() { return string_check_ ? 0 : 1; }
790
791 void Generate(MacroAssembler* masm);
792
793 // Should the stub check whether arguments are strings?
794 bool string_check_;
795};
796
797
Steve Block6ded16b2010-05-10 14:33:55 +0100798class SubStringStub: public CodeStub {
Andrei Popescu31002712010-02-23 13:46:05 +0000799 public:
800 SubStringStub() {}
801
802 private:
803 Major MajorKey() { return SubString; }
804 int MinorKey() { return 0; }
805
806 void Generate(MacroAssembler* masm);
807};
808
809
810
Leon Clarked91b9f72010-01-27 17:25:45 +0000811class StringCompareStub: public CodeStub {
812 public:
813 StringCompareStub() { }
814
815 // Compare two flat ASCII strings and returns result in r0.
816 // Does not use the stack.
817 static void GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
818 Register left,
819 Register right,
820 Register scratch1,
821 Register scratch2,
822 Register scratch3,
823 Register scratch4);
824
825 private:
826 Major MajorKey() { return StringCompare; }
827 int MinorKey() { return 0; }
828
829 void Generate(MacroAssembler* masm);
830};
831
832
Steve Block6ded16b2010-05-10 14:33:55 +0100833// This stub can convert a signed int32 to a heap number (double). It does
834// not work for int32s that are in Smi range! No GC occurs during this stub
835// so you don't have to set up the frame.
836class WriteInt32ToHeapNumberStub : public CodeStub {
837 public:
838 WriteInt32ToHeapNumberStub(Register the_int,
839 Register the_heap_number,
840 Register scratch)
841 : the_int_(the_int),
842 the_heap_number_(the_heap_number),
843 scratch_(scratch) { }
844
845 private:
846 Register the_int_;
847 Register the_heap_number_;
848 Register scratch_;
849
850 // Minor key encoding in 16 bits.
851 class IntRegisterBits: public BitField<int, 0, 4> {};
852 class HeapNumberRegisterBits: public BitField<int, 4, 4> {};
853 class ScratchRegisterBits: public BitField<int, 8, 4> {};
854
855 Major MajorKey() { return WriteInt32ToHeapNumber; }
856 int MinorKey() {
857 // Encode the parameters in a unique 16 bit value.
858 return IntRegisterBits::encode(the_int_.code())
859 | HeapNumberRegisterBits::encode(the_heap_number_.code())
860 | ScratchRegisterBits::encode(scratch_.code());
861 }
862
863 void Generate(MacroAssembler* masm);
864
865 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
866
867#ifdef DEBUG
868 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
869#endif
870};
871
872
873class NumberToStringStub: public CodeStub {
874 public:
875 NumberToStringStub() { }
876
877 // Generate code to do a lookup in the number string cache. If the number in
878 // the register object is found in the cache the generated code falls through
879 // with the result in the result register. The object and the result register
880 // can be the same. If the number is not found in the cache the code jumps to
881 // the label not_found with only the content of register object unchanged.
882 static void GenerateLookupNumberStringCache(MacroAssembler* masm,
883 Register object,
884 Register result,
885 Register scratch1,
886 Register scratch2,
887 Register scratch3,
888 bool object_is_smi,
889 Label* not_found);
890
891 private:
892 Major MajorKey() { return NumberToString; }
893 int MinorKey() { return 0; }
894
895 void Generate(MacroAssembler* masm);
896
897 const char* GetName() { return "NumberToStringStub"; }
898
899#ifdef DEBUG
900 void Print() {
901 PrintF("NumberToStringStub\n");
902 }
903#endif
904};
905
906
907class RecordWriteStub : public CodeStub {
908 public:
909 RecordWriteStub(Register object, Register offset, Register scratch)
910 : object_(object), offset_(offset), scratch_(scratch) { }
911
912 void Generate(MacroAssembler* masm);
913
914 private:
915 Register object_;
916 Register offset_;
917 Register scratch_;
918
919#ifdef DEBUG
920 void Print() {
921 PrintF("RecordWriteStub (object reg %d), (offset reg %d),"
922 " (scratch reg %d)\n",
923 object_.code(), offset_.code(), scratch_.code());
924 }
925#endif
926
927 // Minor key encoding in 12 bits. 4 bits for each of the three
928 // registers (object, offset and scratch) OOOOAAAASSSS.
929 class ScratchBits: public BitField<uint32_t, 0, 4> {};
930 class OffsetBits: public BitField<uint32_t, 4, 4> {};
931 class ObjectBits: public BitField<uint32_t, 8, 4> {};
932
933 Major MajorKey() { return RecordWrite; }
934
935 int MinorKey() {
936 // Encode the registers.
937 return ObjectBits::encode(object_.code()) |
938 OffsetBits::encode(offset_.code()) |
939 ScratchBits::encode(scratch_.code());
940 }
941};
942
943
Steve Blocka7e24c12009-10-30 11:49:00 +0000944} } // namespace v8::internal
945
946#endif // V8_ARM_CODEGEN_ARM_H_