blob: 5908cefb1be8a64f69311f8e1f5388cf1ac0f49f [file] [log] [blame]
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +00001// Copyright 2011 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_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000035
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000036// Flags used for the AllocateInNewSpace functions.
37enum AllocationFlags {
38 // No special flags.
39 NO_ALLOCATION_FLAGS = 0,
40 // Return the pointer to the allocated already tagged as a heap object.
41 TAG_OBJECT = 1 << 0,
42 // The content of the result register already contains the allocation top in
43 // new space.
44 RESULT_CONTAINS_TOP = 1 << 1
45};
46
ager@chromium.orge2902be2009-06-08 12:21:35 +000047// Default scratch register used by MacroAssembler (and other code that needs
48// a spare register). The register isn't callee save, and not used by the
49// function calling convention.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +000050static const Register kScratchRegister = { 10 }; // r10.
51static const Register kSmiConstantRegister = { 15 }; // r15 (callee save).
52static const Register kRootRegister = { 13 }; // r13 (callee save).
53// Value of smi in kSmiConstantRegister.
54static const int kSmiConstantRegisterValue = 1;
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000055// Actual value of root register is offset from the root array's start
56// to take advantage of negitive 8-bit displacement values.
57static const int kRootRegisterBias = 128;
ager@chromium.orge2902be2009-06-08 12:21:35 +000058
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000059// Convenience for platform-independent signatures.
60typedef Operand MemOperand;
61
ager@chromium.org9085a012009-05-11 19:22:57 +000062// Forward declaration.
63class JumpTarget;
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +000064class CallWrapper;
ager@chromium.org9085a012009-05-11 19:22:57 +000065
ager@chromium.org4af710e2009-09-15 12:20:11 +000066struct SmiIndex {
67 SmiIndex(Register index_register, ScaleFactor scale)
68 : reg(index_register),
69 scale(scale) {}
70 Register reg;
71 ScaleFactor scale;
72};
ager@chromium.org9085a012009-05-11 19:22:57 +000073
ager@chromium.org9085a012009-05-11 19:22:57 +000074// MacroAssembler implements a collection of frequently used macros.
75class MacroAssembler: public Assembler {
76 public:
77 MacroAssembler(void* buffer, int size);
78
ager@chromium.org18ad94b2009-09-02 08:22:29 +000079 void LoadRoot(Register destination, Heap::RootListIndex index);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000080 // Load a root value where the index (or part of it) is variable.
81 // The variable_offset register is added to the fixed_offset value
82 // to get the index into the root-array.
83 void LoadRootIndexed(Register destination,
84 Register variable_offset,
85 int fixed_offset);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000086 void CompareRoot(Register with, Heap::RootListIndex index);
ricow@chromium.org83aa5492011-02-07 12:42:56 +000087 void CompareRoot(const Operand& with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000088 void PushRoot(Heap::RootListIndex index);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000089 void StoreRoot(Register source, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000090
ager@chromium.org9085a012009-05-11 19:22:57 +000091 // ---------------------------------------------------------------------------
92 // GC Support
93
ricow@chromium.org30ce4112010-05-31 10:38:25 +000094 // For page containing |object| mark region covering |addr| dirty.
95 // RecordWriteHelper only works if the object is not in new
ager@chromium.orgac091b72010-05-05 07:34:42 +000096 // space.
97 void RecordWriteHelper(Register object,
98 Register addr,
99 Register scratch);
100
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000101 // Check if object is in new space. The condition cc can be equal or
102 // not_equal. If it is equal a jump will be done if the object is on new
103 // space. The register scratch can be object itself, but it will be clobbered.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000104 template <typename LabelType>
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000105 void InNewSpace(Register object,
106 Register scratch,
107 Condition cc,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000108 LabelType* branch);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000109
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000110 // For page containing |object| mark region covering [object+offset]
111 // dirty. |object| is the object being stored into, |value| is the
112 // object being stored. If |offset| is zero, then the |scratch|
113 // register contains the array index into the elements array
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000114 // represented as an untagged 32-bit integer. All registers are
115 // clobbered by the operation. RecordWrite filters out smis so it
116 // does not update the write barrier if the value is a smi.
ager@chromium.org9085a012009-05-11 19:22:57 +0000117 void RecordWrite(Register object,
118 int offset,
119 Register value,
120 Register scratch);
121
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000122 // For page containing |object| mark region covering [address]
123 // dirty. |object| is the object being stored into, |value| is the
124 // object being stored. All registers are clobbered by the
125 // operation. RecordWrite filters out smis so it does not update
126 // the write barrier if the value is a smi.
127 void RecordWrite(Register object,
128 Register address,
129 Register value);
130
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000131 // For page containing |object| mark region covering [object+offset] dirty.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000132 // The value is known to not be a smi.
133 // object is the object being stored into, value is the object being stored.
134 // If offset is zero, then the scratch register contains the array index into
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000135 // the elements array represented as an untagged 32-bit integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000136 // All registers are clobbered by the operation.
137 void RecordWriteNonSmi(Register object,
138 int offset,
139 Register value,
140 Register scratch);
141
ager@chromium.org9085a012009-05-11 19:22:57 +0000142#ifdef ENABLE_DEBUGGER_SUPPORT
143 // ---------------------------------------------------------------------------
144 // Debugger Support
145
ager@chromium.org5c838252010-02-19 08:53:10 +0000146 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000147#endif
148
149 // ---------------------------------------------------------------------------
150 // Activation frames
151
152 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
153 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
154
155 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
156 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
157
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000158 // Enter specific kind of exit frame; either in normal or
159 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000160 // sets up the number of arguments in register rdi and the pointer
161 // to the first argument in register rsi.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000162 //
163 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
164 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000165 void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000166
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000167 // Enter specific kind of exit frame. Allocates arg_stack_space * kPointerSize
168 // memory (not GCed) on the stack accessible via StackSpaceOperand.
169 void EnterApiExitFrame(int arg_stack_space);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000170
ager@chromium.orga1645e22009-09-09 19:27:10 +0000171 // Leave the current exit frame. Expects/provides the return value in
172 // register rax:rdx (untouched) and the pointer to the first
173 // argument in register rsi.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000174 void LeaveExitFrame(bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000175
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000176 // Leave the current exit frame. Expects/provides the return value in
177 // register rax (untouched).
178 void LeaveApiExitFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +0000179
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000180 // Push and pop the registers that can hold pointers.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000181 void PushSafepointRegisters() { Pushad(); }
182 void PopSafepointRegisters() { Popad(); }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000183 // Store the value in register src in the safepoint register stack
184 // slot for register dst.
185 void StoreToSafepointRegisterSlot(Register dst, Register src);
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000186 void LoadFromSafepointRegisterSlot(Register dst, Register src);
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000187
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000188 void InitializeRootRegister() {
189 ExternalReference roots_address = ExternalReference::roots_address();
190 movq(kRootRegister, roots_address);
191 addq(kRootRegister, Immediate(kRootRegisterBias));
192 }
193
ager@chromium.org9085a012009-05-11 19:22:57 +0000194 // ---------------------------------------------------------------------------
195 // JavaScript invokes
196
197 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000198 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000199 const ParameterCount& expected,
200 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000201 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000202 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000203
204 void InvokeCode(Handle<Code> code,
205 const ParameterCount& expected,
206 const ParameterCount& actual,
207 RelocInfo::Mode rmode,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000208 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000209 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000210
211 // Invoke the JavaScript function in the given register. Changes the
212 // current context to the context in the function before invoking.
213 void InvokeFunction(Register function,
214 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000215 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000216 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000217
ager@chromium.org5c838252010-02-19 08:53:10 +0000218 void InvokeFunction(JSFunction* function,
219 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000220 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000221 CallWrapper* call_wrapper = NULL);
ager@chromium.org5c838252010-02-19 08:53:10 +0000222
ager@chromium.org9085a012009-05-11 19:22:57 +0000223 // Invoke specified builtin JavaScript function. Adds an entry to
224 // the unresolved list if the name does not resolve.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000225 void InvokeBuiltin(Builtins::JavaScript id,
226 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000227 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000228
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000229 // Store the function for the given builtin in the target register.
230 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
231
ager@chromium.org9085a012009-05-11 19:22:57 +0000232 // Store the code object for the given builtin in the target register.
233 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
234
ager@chromium.org4af710e2009-09-15 12:20:11 +0000235
236 // ---------------------------------------------------------------------------
237 // Smi tagging, untagging and operations on tagged smis.
238
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000239 void InitializeSmiConstantRegister() {
240 movq(kSmiConstantRegister,
241 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
242 RelocInfo::NONE);
243 }
244
ager@chromium.org4af710e2009-09-15 12:20:11 +0000245 // Conversions between tagged smi values and non-tagged integer values.
246
247 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000248 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000249 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000250 void Integer32ToSmi(Register dst, Register src);
251
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000252 // Stores an integer32 value into a memory field that already holds a smi.
253 void Integer32ToSmiField(const Operand& dst, Register src);
254
ager@chromium.org4af710e2009-09-15 12:20:11 +0000255 // Adds constant to src and tags the result as a smi.
256 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000257 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000258
259 // Convert smi to 32-bit integer. I.e., not sign extended into
260 // high 32 bits of destination.
261 void SmiToInteger32(Register dst, Register src);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000262 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000263
264 // Convert smi to 64-bit integer (sign extended if necessary).
265 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000266 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000267
268 // Multiply a positive smi's integer value by a power of two.
269 // Provides result as 64-bit integer value.
270 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
271 Register src,
272 int power);
273
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000274 // Divide a positive smi's integer value by a power of two.
275 // Provides result as 32-bit integer value.
276 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
277 Register src,
278 int power);
279
280
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000281 // Simple comparison of smis.
282 void SmiCompare(Register dst, Register src);
283 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000284 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000285 void SmiCompare(const Operand& dst, Register src);
286 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000287 // Compare the int32 in src register to the value of the smi stored at dst.
288 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000289 // Sets sign and zero flags depending on value of smi in register.
290 void SmiTest(Register src);
291
ager@chromium.org4af710e2009-09-15 12:20:11 +0000292 // Functions performing a check on a known or potential smi. Returns
293 // a condition that is satisfied if the check is successful.
294
295 // Is the value a tagged smi.
296 Condition CheckSmi(Register src);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000297 Condition CheckSmi(const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000298
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000299 // Is the value a non-negative tagged smi.
300 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000301
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000302 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000303 Condition CheckBothSmi(Register first, Register second);
304
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000305 // Are both values non-negative tagged smis.
306 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000307
308 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000309 Condition CheckEitherSmi(Register first,
310 Register second,
311 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000312
ager@chromium.org4af710e2009-09-15 12:20:11 +0000313 // Is the value the minimum smi value (since we are using
314 // two's complement numbers, negating the value is known to yield
315 // a non-smi value).
316 Condition CheckIsMinSmi(Register src);
317
ager@chromium.org4af710e2009-09-15 12:20:11 +0000318 // Checks whether an 32-bit integer value is a valid for conversion
319 // to a smi.
320 Condition CheckInteger32ValidSmiValue(Register src);
321
ager@chromium.org3811b432009-10-28 14:53:37 +0000322 // Checks whether an 32-bit unsigned integer value is a valid for
323 // conversion to a smi.
324 Condition CheckUInteger32ValidSmiValue(Register src);
325
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000326 // Check whether src is a Smi, and set dst to zero if it is a smi,
327 // and to one if it isn't.
328 void CheckSmiToIndicator(Register dst, Register src);
329 void CheckSmiToIndicator(Register dst, const Operand& src);
330
ager@chromium.org4af710e2009-09-15 12:20:11 +0000331 // Test-and-jump functions. Typically combines a check function
332 // above with a conditional jump.
333
334 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000335 template <typename LabelType>
336 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000337
ager@chromium.org3811b432009-10-28 14:53:37 +0000338 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000339 template <typename LabelType>
340 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000341
ager@chromium.org4af710e2009-09-15 12:20:11 +0000342 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000343 template <typename LabelType>
344 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000345
346 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000347 template <typename LabelType>
348 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000349
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000350 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000351 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000352 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000353
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000354 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000355 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000356 template <typename LabelType>
357 void JumpIfSmiEqualsConstant(Register src,
358 Smi* constant,
359 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000360
ager@chromium.org4af710e2009-09-15 12:20:11 +0000361 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000362 template <typename LabelType>
363 void JumpIfNotBothSmi(Register src1,
364 Register src2,
365 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000366
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000367 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000368 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000369 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
370 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000371
ager@chromium.org4af710e2009-09-15 12:20:11 +0000372 // Operations on tagged smi values.
373
374 // Smis represent a subset of integers. The subset is always equivalent to
375 // a two's complement interpretation of a fixed number of bits.
376
377 // Optimistically adds an integer constant to a supposed smi.
378 // If the src is not a smi, or the result is not a smi, jump to
379 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000380 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000381 void SmiTryAddConstant(Register dst,
382 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000383 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000384 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000385
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000386 // Add an integer constant to a tagged smi, giving a tagged smi as result.
387 // No overflow testing on the result is done.
388 void SmiAddConstant(Register dst, Register src, Smi* constant);
389
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000390 // Add an integer constant to a tagged smi, giving a tagged smi as result.
391 // No overflow testing on the result is done.
392 void SmiAddConstant(const Operand& dst, Smi* constant);
393
ager@chromium.org4af710e2009-09-15 12:20:11 +0000394 // Add an integer constant to a tagged smi, giving a tagged smi as result,
395 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000396 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000397 void SmiAddConstant(Register dst,
398 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000399 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000400 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000401
402 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000403 // result. No testing on the result is done. Sets the N and Z flags
404 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000405 void SmiSubConstant(Register dst, Register src, Smi* constant);
406
407 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000408 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000409 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000410 void SmiSubConstant(Register dst,
411 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000412 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000413 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000414
415 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000416 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000417 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000418 void SmiNeg(Register dst,
419 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000420 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000421
422 // Adds smi values and return the result as a smi.
423 // If dst is src1, then src1 will be destroyed, even if
424 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000425 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000426 void SmiAdd(Register dst,
427 Register src1,
428 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000429 LabelType* on_not_smi_result);
430
431 void SmiAdd(Register dst,
432 Register src1,
433 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000434
435 // Subtracts smi values and return the result as a smi.
436 // If dst is src1, then src1 will be destroyed, even if
437 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000438 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000439 void SmiSub(Register dst,
440 Register src1,
441 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000442 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000443
ager@chromium.orgac091b72010-05-05 07:34:42 +0000444 void SmiSub(Register dst,
445 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000446 Register src2);
447
448 template <typename LabelType>
449 void SmiSub(Register dst,
450 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000451 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000452 LabelType* on_not_smi_result);
453
454 void SmiSub(Register dst,
455 Register src1,
456 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000457
ager@chromium.org4af710e2009-09-15 12:20:11 +0000458 // Multiplies smi values and return the result as a smi,
459 // if possible.
460 // If dst is src1, then src1 will be destroyed, even if
461 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000462 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000463 void SmiMul(Register dst,
464 Register src1,
465 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000466 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000467
468 // Divides one smi by another and returns the quotient.
469 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000470 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000471 void SmiDiv(Register dst,
472 Register src1,
473 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000474 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000475
476 // Divides one smi by another and returns the remainder.
477 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000478 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000479 void SmiMod(Register dst,
480 Register src1,
481 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000482 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000483
484 // Bitwise operations.
485 void SmiNot(Register dst, Register src);
486 void SmiAnd(Register dst, Register src1, Register src2);
487 void SmiOr(Register dst, Register src1, Register src2);
488 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000489 void SmiAndConstant(Register dst, Register src1, Smi* constant);
490 void SmiOrConstant(Register dst, Register src1, Smi* constant);
491 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000492
493 void SmiShiftLeftConstant(Register dst,
494 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000495 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000496 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000497 void SmiShiftLogicalRightConstant(Register dst,
498 Register src,
499 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000500 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000501 void SmiShiftArithmeticRightConstant(Register dst,
502 Register src,
503 int shift_value);
504
505 // Shifts a smi value to the left, and returns the result if that is a smi.
506 // Uses and clobbers rcx, so dst may not be rcx.
507 void SmiShiftLeft(Register dst,
508 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000509 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000510 // Shifts a smi value to the right, shifting in zero bits at the top, and
511 // returns the unsigned intepretation of the result if that is a smi.
512 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000513 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000514 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000515 Register src1,
516 Register src2,
517 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000518 // Shifts a smi value to the right, sign extending the top, and
519 // returns the signed intepretation of the result. That will always
520 // be a valid smi value, since it's numerically smaller than the
521 // original.
522 // Uses and clobbers rcx, so dst may not be rcx.
523 void SmiShiftArithmeticRight(Register dst,
524 Register src1,
525 Register src2);
526
527 // Specialized operations
528
529 // Select the non-smi register of two registers where exactly one is a
530 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000531 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000532 void SelectNonSmi(Register dst,
533 Register src1,
534 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000535 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000536
537 // Converts, if necessary, a smi to a combination of number and
538 // multiplier to be used as a scaled index.
539 // The src register contains a *positive* smi value. The shift is the
540 // power of two to multiply the index value by (e.g.
541 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
542 // The returned index register may be either src or dst, depending
543 // on what is most efficient. If src and dst are different registers,
544 // src is always unchanged.
545 SmiIndex SmiToIndex(Register dst, Register src, int shift);
546
547 // Converts a positive smi to a negative index.
548 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
549
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000550 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000551 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000552 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000553 }
554
555 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000556 Register constant = GetSmiConstant(source);
557 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000558 }
559
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000560 void Push(Smi* smi);
561 void Test(const Operand& dst, Smi* source);
562
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000563 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000564 // String macros.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000565
566 // If object is a string, its map is loaded into object_map.
567 template <typename LabelType>
568 void JumpIfNotString(Register object,
569 Register object_map,
570 LabelType* not_string);
571
572
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000573 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000574 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
575 Register second_object,
576 Register scratch1,
577 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000578 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000579
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000580 // Check whether the instance type represents a flat ascii string. Jump to the
581 // label if not. If the instance type can be scratched specify same register
582 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000583 template <typename LabelType>
584 void JumpIfInstanceTypeIsNotSequentialAscii(
585 Register instance_type,
586 Register scratch,
587 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000588
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000589 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000590 void JumpIfBothInstanceTypesAreNotSequentialAscii(
591 Register first_object_instance_type,
592 Register second_object_instance_type,
593 Register scratch1,
594 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000595 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000596
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000597 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000598 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000599
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000600 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000601 void Set(Register dst, int64_t x);
602 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000603
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000604 // Move if the registers are not identical.
605 void Move(Register target, Register source);
606
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000607 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000608 void Move(Register dst, Handle<Object> source);
609 void Move(const Operand& dst, Handle<Object> source);
610 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000611 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000612 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000613
614 // Emit code to discard a non-negative number of pointer-sized elements
615 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000616 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000617
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000618 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000619
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000620 // Control Flow
621 void Jump(Address destination, RelocInfo::Mode rmode);
622 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000623 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
624
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000625 void Call(Address destination, RelocInfo::Mode rmode);
626 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000627 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000628
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000629 // The size of the code generated for different call instructions.
630 int CallSize(Address destination, RelocInfo::Mode rmode) {
631 return kCallInstructionLength;
632 }
633 int CallSize(ExternalReference ext) {
634 return kCallInstructionLength;
635 }
636 int CallSize(Handle<Code> code_object) {
637 // Code calls use 32-bit relative addressing.
638 return kShortCallInstructionLength;
639 }
640 int CallSize(Register target) {
641 // Opcode: REX_opt FF /2 m64
642 return (target.high_bit() != 0) ? 3 : 2;
643 }
644 int CallSize(const Operand& target) {
645 // Opcode: REX_opt FF /2 m64
646 return (target.requires_rex() ? 2 : 1) + target.operand_size();
647 }
648
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000649 // Emit call to the code we are currently generating.
650 void CallSelf() {
651 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
652 Call(self, RelocInfo::CODE_TARGET);
653 }
654
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000655 // Non-x64 instructions.
656 // Push/pop all general purpose registers.
657 // Does not push rsp/rbp nor any of the assembler's special purpose registers
658 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
659 void Pushad();
660 void Popad();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000661 // Sets the stack as after performing Popad, without actually loading the
662 // registers.
663 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000664
ager@chromium.org9085a012009-05-11 19:22:57 +0000665 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000666 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000667 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000668 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000669 void CmpObjectType(Register heap_object, InstanceType type, Register map);
670
671 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000672 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000673 void CmpInstanceType(Register map, InstanceType type);
674
ager@chromium.org5c838252010-02-19 08:53:10 +0000675 // Check if the map of an object is equal to a specified map and
676 // branch to label if not. Skip the smi check if not required
677 // (object is known to be a heap object)
678 void CheckMap(Register obj,
679 Handle<Map> map,
680 Label* fail,
681 bool is_heap_object);
682
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000683 // Check if the object in register heap_object is a string. Afterwards the
684 // register map contains the object map and the register instance_type
685 // contains the instance_type. The registers map and instance_type can be the
686 // same in which case it contains the instance type afterwards. Either of the
687 // registers map and instance_type can be the same as heap_object.
688 Condition IsObjectStringType(Register heap_object,
689 Register map,
690 Register instance_type);
691
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000692 // FCmp compares and pops the two values on top of the FPU stack.
693 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000694 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
695 void FCmp();
696
ager@chromium.org5c838252010-02-19 08:53:10 +0000697 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000698 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000699
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000700 // Abort execution if argument is a smi. Used in debug code.
701 void AbortIfSmi(Register object);
702
lrn@chromium.org25156de2010-04-06 13:10:27 +0000703 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000704 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000705
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000706 // Abort execution if argument is a string. Used in debug code.
707 void AbortIfNotString(Register object);
708
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000709 // Abort execution if argument is not the root value with the given index.
710 void AbortIfNotRootValue(Register src,
711 Heap::RootListIndex root_value_index,
712 const char* message);
713
ager@chromium.org9085a012009-05-11 19:22:57 +0000714 // ---------------------------------------------------------------------------
715 // Exception handling
716
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000717 // Push a new try handler and link into try handler chain. The return
718 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000719 void PushTryHandler(CodeLocation try_location, HandlerType type);
720
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000721 // Unlink the stack handler on top of the stack from the try handler chain.
722 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000723
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000724 // Activate the top handler in the try hander chain and pass the
725 // thrown value.
726 void Throw(Register value);
727
728 // Propagate an uncatchable exception out of the current JS stack.
729 void ThrowUncatchable(UncatchableExceptionType type, Register value);
730
ager@chromium.org9085a012009-05-11 19:22:57 +0000731 // ---------------------------------------------------------------------------
732 // Inline caching support
733
ager@chromium.org9085a012009-05-11 19:22:57 +0000734 // Generate code for checking access rights - used for security checks
735 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000736 // is left untouched, but the scratch register and kScratchRegister,
737 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000738 void CheckAccessGlobalProxy(Register holder_reg,
739 Register scratch,
740 Label* miss);
741
742
743 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000744 // Allocation support
745
746 // Allocate an object in new space. If the new space is exhausted control
747 // continues at the gc_required label. The allocated object is returned in
748 // result and end of the new object is returned in result_end. The register
749 // scratch can be passed as no_reg in which case an additional object
750 // reference will be added to the reloc info. The returned pointers in result
751 // and result_end have not yet been tagged as heap objects. If
752 // result_contains_top_on_entry is true the content of result is known to be
753 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000754 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000755 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000756 void AllocateInNewSpace(int object_size,
757 Register result,
758 Register result_end,
759 Register scratch,
760 Label* gc_required,
761 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000762
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000763 void AllocateInNewSpace(int header_size,
764 ScaleFactor element_size,
765 Register element_count,
766 Register result,
767 Register result_end,
768 Register scratch,
769 Label* gc_required,
770 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000771
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000772 void AllocateInNewSpace(Register object_size,
773 Register result,
774 Register result_end,
775 Register scratch,
776 Label* gc_required,
777 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000778
779 // Undo allocation in new space. The object passed and objects allocated after
780 // it will no longer be allocated. Make sure that no pointers are left to the
781 // object(s) no longer allocated as they would be invalid when allocation is
782 // un-done.
783 void UndoAllocationInNewSpace(Register object);
784
ager@chromium.org3811b432009-10-28 14:53:37 +0000785 // Allocate a heap number in new space with undefined value. Returns
786 // tagged pointer in result register, or jumps to gc_required if new
787 // space is full.
788 void AllocateHeapNumber(Register result,
789 Register scratch,
790 Label* gc_required);
791
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000792 // Allocate a sequential string. All the header fields of the string object
793 // are initialized.
794 void AllocateTwoByteString(Register result,
795 Register length,
796 Register scratch1,
797 Register scratch2,
798 Register scratch3,
799 Label* gc_required);
800 void AllocateAsciiString(Register result,
801 Register length,
802 Register scratch1,
803 Register scratch2,
804 Register scratch3,
805 Label* gc_required);
806
807 // Allocate a raw cons string object. Only the map field of the result is
808 // initialized.
809 void AllocateConsString(Register result,
810 Register scratch1,
811 Register scratch2,
812 Label* gc_required);
813 void AllocateAsciiConsString(Register result,
814 Register scratch1,
815 Register scratch2,
816 Label* gc_required);
817
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000818 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000819 // Support functions.
820
821 // Check if result is zero and op is negative.
822 void NegativeZeroTest(Register result, Register op, Label* then_label);
823
824 // Check if result is zero and op is negative in code using jump targets.
825 void NegativeZeroTest(CodeGenerator* cgen,
826 Register result,
827 Register op,
828 JumpTarget* then_target);
829
830 // Check if result is zero and any of op1 and op2 are negative.
831 // Register scratch is destroyed, and it must be different from op2.
832 void NegativeZeroTest(Register result, Register op1, Register op2,
833 Register scratch, Label* then_label);
834
835 // Try to get function prototype of a function and puts the value in
836 // the result register. Checks that the function really is a
837 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000838 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000839 // clobbered.
840 void TryGetFunctionPrototype(Register function,
841 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000842 Label* miss);
843
844 // Generates code for reporting that an illegal operation has
845 // occurred.
846 void IllegalOperation(int num_arguments);
847
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000848 // Picks out an array index from the hash field.
849 // Register use:
850 // hash - holds the index's hash. Clobbered.
851 // index - holds the overwritten index on exit.
852 void IndexFromHash(Register hash, Register index);
853
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000854 // Find the function context up the context chain.
855 void LoadContext(Register dst, int context_chain_length);
856
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000857 // Load the global function with the given index.
858 void LoadGlobalFunction(int index, Register function);
859
860 // Load the initial map from the global function. The registers
861 // function and map can be the same.
862 void LoadGlobalFunctionInitialMap(Register function, Register map);
863
ager@chromium.org9085a012009-05-11 19:22:57 +0000864 // ---------------------------------------------------------------------------
865 // Runtime calls
866
867 // Call a code stub.
868 void CallStub(CodeStub* stub);
869
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000870 // Call a code stub and return the code object called. Try to generate
871 // the code if necessary. Do not perform a GC but instead return a retry
872 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000873 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000874
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000875 // Tail call a code stub (jump).
876 void TailCallStub(CodeStub* stub);
877
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000878 // Tail call a code stub (jump) and return the code object called. Try to
879 // generate the code if necessary. Do not perform a GC but instead return
880 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000881 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000882
ager@chromium.org9085a012009-05-11 19:22:57 +0000883 // Return from a code stub after popping its arguments.
884 void StubReturn(int argc);
885
886 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000887 void CallRuntime(Runtime::Function* f, int num_arguments);
888
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000889 // Call a runtime function and save the value of XMM registers.
890 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
891
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000892 // Call a runtime function, returning the CodeStub object called.
893 // Try to generate the stub code if necessary. Do not perform a GC
894 // but instead return a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000895 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
896 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000897
ager@chromium.org9085a012009-05-11 19:22:57 +0000898 // Convenience function: Same as above, but takes the fid instead.
899 void CallRuntime(Runtime::FunctionId id, int num_arguments);
900
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000901 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000902 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
903 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000904
ager@chromium.org5c838252010-02-19 08:53:10 +0000905 // Convenience function: call an external reference.
906 void CallExternalReference(const ExternalReference& ext,
907 int num_arguments);
908
ager@chromium.org9085a012009-05-11 19:22:57 +0000909 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000910 // Like JumpToExternalReference, but also takes care of passing the number
911 // of parameters.
912 void TailCallExternalReference(const ExternalReference& ext,
913 int num_arguments,
914 int result_size);
915
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000916 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
917 const ExternalReference& ext, int num_arguments, int result_size);
918
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000919 // Convenience function: tail call a runtime routine (jump).
920 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000921 int num_arguments,
922 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000923
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000924 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
925 int num_arguments,
926 int result_size);
927
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000928 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000929 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000930
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000931 // Jump to a runtime routine.
932 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
933 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000934
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000935 // Prepares stack to put arguments (aligns and so on).
936 // WIN64 calling convention requires to put the pointer to the return value
937 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
938 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
939 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
940 void PrepareCallApiFunction(int arg_stack_space);
941
942 // Calls an API function. Allocates HandleScope, extracts
943 // returned value from handle and propagates exceptions.
944 // Clobbers r12, r14, rbx and caller-save registers. Restores context.
945 // On return removes stack_space * kPointerSize (GCed).
946 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
947 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000948
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000949 // Before calling a C-function from generated code, align arguments on stack.
950 // After aligning the frame, arguments must be stored in esp[0], esp[4],
951 // etc., not pushed. The argument count assumes all arguments are word sized.
952 // The number of slots reserved for arguments depends on platform. On Windows
953 // stack slots are reserved for the arguments passed in registers. On other
954 // platforms stack slots are only reserved for the arguments actually passed
955 // on the stack.
956 void PrepareCallCFunction(int num_arguments);
957
958 // Calls a C function and cleans up the space for arguments allocated
959 // by PrepareCallCFunction. The called function is not allowed to trigger a
960 // garbage collection, since that might move the code and invalidate the
961 // return address (unless this is somehow accounted for by the called
962 // function).
963 void CallCFunction(ExternalReference function, int num_arguments);
964 void CallCFunction(Register function, int num_arguments);
965
966 // Calculate the number of stack slots to reserve for arguments when calling a
967 // C function.
968 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000969
970 // ---------------------------------------------------------------------------
971 // Utilities
972
973 void Ret();
974
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000975 // Return and drop arguments from stack, where the number of arguments
976 // may be bigger than 2^16 - 1. Requires a scratch register.
977 void Ret(int bytes_dropped, Register scratch);
978
ager@chromium.org9085a012009-05-11 19:22:57 +0000979 Handle<Object> CodeObject() { return code_object_; }
980
981
982 // ---------------------------------------------------------------------------
983 // StatsCounter support
984
985 void SetCounter(StatsCounter* counter, int value);
986 void IncrementCounter(StatsCounter* counter, int value);
987 void DecrementCounter(StatsCounter* counter, int value);
988
989
990 // ---------------------------------------------------------------------------
991 // Debugging
992
993 // Calls Abort(msg) if the condition cc is not satisfied.
994 // Use --debug_code to enable.
995 void Assert(Condition cc, const char* msg);
996
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000997 void AssertFastElements(Register elements);
998
ager@chromium.org9085a012009-05-11 19:22:57 +0000999 // Like Assert(), but always enabled.
1000 void Check(Condition cc, const char* msg);
1001
1002 // Print a message to stdout and abort execution.
1003 void Abort(const char* msg);
1004
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001005 // Check that the stack is aligned.
1006 void CheckStackAlignment();
1007
ager@chromium.org9085a012009-05-11 19:22:57 +00001008 // Verify restrictions about code generated in stubs.
1009 void set_generating_stub(bool value) { generating_stub_ = value; }
1010 bool generating_stub() { return generating_stub_; }
1011 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
1012 bool allow_stub_calls() { return allow_stub_calls_; }
1013
1014 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001015 // Order general registers are pushed by Pushad.
1016 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14.
1017 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001018 static const int kNumSafepointSavedRegisters = 11;
1019
ager@chromium.org9085a012009-05-11 19:22:57 +00001020 bool generating_stub_;
1021 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001022
1023 // Returns a register holding the smi value. The register MUST NOT be
1024 // modified. It may be the "smi 1 constant" register.
1025 Register GetSmiConstant(Smi* value);
1026
1027 // Moves the smi value to the destination register.
1028 void LoadSmiConstant(Register dst, Smi* value);
1029
ager@chromium.org5c838252010-02-19 08:53:10 +00001030 // This handle will be patched with the code object on installation.
1031 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +00001032
1033 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001034 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +00001035 void InvokePrologue(const ParameterCount& expected,
1036 const ParameterCount& actual,
1037 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001038 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001039 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001040 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001041 CallWrapper* call_wrapper);
ager@chromium.org9085a012009-05-11 19:22:57 +00001042
ager@chromium.org9085a012009-05-11 19:22:57 +00001043 // Activation support.
1044 void EnterFrame(StackFrame::Type type);
1045 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001046
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001047 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001048
1049 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
1050 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001051 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001052
1053 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001054
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001055 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001056 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001057 // Otherwise the address of the new-space top is loaded into scratch (if
1058 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001059 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001060 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001061 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001062 // Update allocation top with value in result_end register.
1063 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001064 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001065
1066 // Helper for PopHandleScope. Allowed to perform a GC and returns
1067 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1068 // possibly returns a failure object indicating an allocation failure.
1069 Object* PopHandleScopeHelper(Register saved,
1070 Register scratch,
1071 bool gc_allowed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001072
1073
1074 // Compute memory operands for safepoint stack slots.
1075 Operand SafepointRegisterSlot(Register reg);
1076 static int SafepointRegisterStackIndex(int reg_code) {
1077 return kNumSafepointRegisters - kSafepointPushRegisterIndices[reg_code] - 1;
1078 }
1079
1080 // Needs access to SafepointRegisterStackIndex for optimized frame
1081 // traversal.
1082 friend class OptimizedFrame;
ager@chromium.org9085a012009-05-11 19:22:57 +00001083};
1084
1085
ager@chromium.org4af710e2009-09-15 12:20:11 +00001086// The code patcher is used to patch (typically) small parts of code e.g. for
1087// debugging and other types of instrumentation. When using the code patcher
1088// the exact number of bytes specified must be emitted. Is not legal to emit
1089// relocation information. If any of these constraints are violated it causes
1090// an assertion.
1091class CodePatcher {
1092 public:
1093 CodePatcher(byte* address, int size);
1094 virtual ~CodePatcher();
1095
1096 // Macro assembler to emit code.
1097 MacroAssembler* masm() { return &masm_; }
1098
1099 private:
1100 byte* address_; // The address of the code being patched.
1101 int size_; // Number of bytes of the expected patch size.
1102 MacroAssembler masm_; // Macro assembler used to generate the code.
1103};
1104
1105
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001106// Helper class for generating code or data associated with the code
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001107// right before or after a call instruction. As an example this can be used to
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001108// generate safepoint data after calls for crankshaft.
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001109class CallWrapper {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001110 public:
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001111 CallWrapper() { }
1112 virtual ~CallWrapper() { }
1113 // Called just before emitting a call. Argument is the size of the generated
1114 // call code.
1115 virtual void BeforeCall(int call_size) = 0;
1116 // Called just after emitting a call, i.e., at the return site for the call.
1117 virtual void AfterCall() = 0;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001118};
1119
1120
ager@chromium.org9085a012009-05-11 19:22:57 +00001121// -----------------------------------------------------------------------------
1122// Static helper functions.
1123
1124// Generate an Operand for loading a field from an object.
1125static inline Operand FieldOperand(Register object, int offset) {
1126 return Operand(object, offset - kHeapObjectTag);
1127}
1128
1129
1130// Generate an Operand for loading an indexed field from an object.
1131static inline Operand FieldOperand(Register object,
1132 Register index,
1133 ScaleFactor scale,
1134 int offset) {
1135 return Operand(object, index, scale, offset - kHeapObjectTag);
1136}
1137
1138
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001139static inline Operand ContextOperand(Register context, int index) {
1140 return Operand(context, Context::SlotOffset(index));
1141}
1142
1143
1144static inline Operand GlobalObjectOperand() {
1145 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1146}
1147
1148
1149// Provides access to exit frame stack space (not GCed).
1150static inline Operand StackSpaceOperand(int index) {
1151#ifdef _WIN64
1152 const int kShaddowSpace = 4;
1153 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1154#else
1155 return Operand(rsp, index * kPointerSize);
1156#endif
1157}
1158
1159
1160
ager@chromium.org9085a012009-05-11 19:22:57 +00001161#ifdef GENERATED_CODE_COVERAGE
1162extern void LogGeneratedCodeCoverage(const char* file_line);
1163#define CODE_COVERAGE_STRINGIFY(x) #x
1164#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1165#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1166#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001167 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001168 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1169 masm->pushfd(); \
1170 masm->pushad(); \
1171 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001172 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001173 masm->pop(rax); \
1174 masm->popad(); \
1175 masm->popfd(); \
1176 } \
1177 masm->
1178#else
1179#define ACCESS_MASM(masm) masm->
1180#endif
1181
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001182// -----------------------------------------------------------------------------
1183// Template implementations.
1184
1185static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1186
1187
1188template <typename LabelType>
1189void MacroAssembler::SmiNeg(Register dst,
1190 Register src,
1191 LabelType* on_smi_result) {
1192 if (dst.is(src)) {
1193 ASSERT(!dst.is(kScratchRegister));
1194 movq(kScratchRegister, src);
1195 neg(dst); // Low 32 bits are retained as zero by negation.
1196 // Test if result is zero or Smi::kMinValue.
1197 cmpq(dst, kScratchRegister);
1198 j(not_equal, on_smi_result);
1199 movq(src, kScratchRegister);
1200 } else {
1201 movq(dst, src);
1202 neg(dst);
1203 cmpq(dst, src);
1204 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1205 j(not_equal, on_smi_result);
1206 }
1207}
1208
1209
1210template <typename LabelType>
1211void MacroAssembler::SmiAdd(Register dst,
1212 Register src1,
1213 Register src2,
1214 LabelType* on_not_smi_result) {
1215 ASSERT_NOT_NULL(on_not_smi_result);
1216 ASSERT(!dst.is(src2));
1217 if (dst.is(src1)) {
1218 movq(kScratchRegister, src1);
1219 addq(kScratchRegister, src2);
1220 j(overflow, on_not_smi_result);
1221 movq(dst, kScratchRegister);
1222 } else {
1223 movq(dst, src1);
1224 addq(dst, src2);
1225 j(overflow, on_not_smi_result);
1226 }
1227}
1228
1229
1230template <typename LabelType>
1231void MacroAssembler::SmiSub(Register dst,
1232 Register src1,
1233 Register src2,
1234 LabelType* on_not_smi_result) {
1235 ASSERT_NOT_NULL(on_not_smi_result);
1236 ASSERT(!dst.is(src2));
1237 if (dst.is(src1)) {
1238 cmpq(dst, src2);
1239 j(overflow, on_not_smi_result);
1240 subq(dst, src2);
1241 } else {
1242 movq(dst, src1);
1243 subq(dst, src2);
1244 j(overflow, on_not_smi_result);
1245 }
1246}
1247
1248
1249template <typename LabelType>
1250void MacroAssembler::SmiSub(Register dst,
1251 Register src1,
1252 const Operand& src2,
1253 LabelType* on_not_smi_result) {
1254 ASSERT_NOT_NULL(on_not_smi_result);
1255 if (dst.is(src1)) {
1256 movq(kScratchRegister, src2);
1257 cmpq(src1, kScratchRegister);
1258 j(overflow, on_not_smi_result);
1259 subq(src1, kScratchRegister);
1260 } else {
1261 movq(dst, src1);
1262 subq(dst, src2);
1263 j(overflow, on_not_smi_result);
1264 }
1265}
1266
1267
1268template <typename LabelType>
1269void MacroAssembler::SmiMul(Register dst,
1270 Register src1,
1271 Register src2,
1272 LabelType* on_not_smi_result) {
1273 ASSERT(!dst.is(src2));
1274 ASSERT(!dst.is(kScratchRegister));
1275 ASSERT(!src1.is(kScratchRegister));
1276 ASSERT(!src2.is(kScratchRegister));
1277
1278 if (dst.is(src1)) {
1279 NearLabel failure, zero_correct_result;
1280 movq(kScratchRegister, src1); // Create backup for later testing.
1281 SmiToInteger64(dst, src1);
1282 imul(dst, src2);
1283 j(overflow, &failure);
1284
1285 // Check for negative zero result. If product is zero, and one
1286 // argument is negative, go to slow case.
1287 NearLabel correct_result;
1288 testq(dst, dst);
1289 j(not_zero, &correct_result);
1290
1291 movq(dst, kScratchRegister);
1292 xor_(dst, src2);
1293 j(positive, &zero_correct_result); // Result was positive zero.
1294
1295 bind(&failure); // Reused failure exit, restores src1.
1296 movq(src1, kScratchRegister);
1297 jmp(on_not_smi_result);
1298
1299 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001300 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001301
1302 bind(&correct_result);
1303 } else {
1304 SmiToInteger64(dst, src1);
1305 imul(dst, src2);
1306 j(overflow, on_not_smi_result);
1307 // Check for negative zero result. If product is zero, and one
1308 // argument is negative, go to slow case.
1309 NearLabel correct_result;
1310 testq(dst, dst);
1311 j(not_zero, &correct_result);
1312 // One of src1 and src2 is zero, the check whether the other is
1313 // negative.
1314 movq(kScratchRegister, src1);
1315 xor_(kScratchRegister, src2);
1316 j(negative, on_not_smi_result);
1317 bind(&correct_result);
1318 }
1319}
1320
1321
1322template <typename LabelType>
1323void MacroAssembler::SmiTryAddConstant(Register dst,
1324 Register src,
1325 Smi* constant,
1326 LabelType* on_not_smi_result) {
1327 // Does not assume that src is a smi.
1328 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1329 ASSERT_EQ(0, kSmiTag);
1330 ASSERT(!dst.is(kScratchRegister));
1331 ASSERT(!src.is(kScratchRegister));
1332
1333 JumpIfNotSmi(src, on_not_smi_result);
1334 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1335 LoadSmiConstant(tmp, constant);
1336 addq(tmp, src);
1337 j(overflow, on_not_smi_result);
1338 if (dst.is(src)) {
1339 movq(dst, tmp);
1340 }
1341}
1342
1343
1344template <typename LabelType>
1345void MacroAssembler::SmiAddConstant(Register dst,
1346 Register src,
1347 Smi* constant,
1348 LabelType* on_not_smi_result) {
1349 if (constant->value() == 0) {
1350 if (!dst.is(src)) {
1351 movq(dst, src);
1352 }
1353 } else if (dst.is(src)) {
1354 ASSERT(!dst.is(kScratchRegister));
1355
1356 LoadSmiConstant(kScratchRegister, constant);
1357 addq(kScratchRegister, src);
1358 j(overflow, on_not_smi_result);
1359 movq(dst, kScratchRegister);
1360 } else {
1361 LoadSmiConstant(dst, constant);
1362 addq(dst, src);
1363 j(overflow, on_not_smi_result);
1364 }
1365}
1366
1367
1368template <typename LabelType>
1369void MacroAssembler::SmiSubConstant(Register dst,
1370 Register src,
1371 Smi* constant,
1372 LabelType* on_not_smi_result) {
1373 if (constant->value() == 0) {
1374 if (!dst.is(src)) {
1375 movq(dst, src);
1376 }
1377 } else if (dst.is(src)) {
1378 ASSERT(!dst.is(kScratchRegister));
1379 if (constant->value() == Smi::kMinValue) {
1380 // Subtracting min-value from any non-negative value will overflow.
1381 // We test the non-negativeness before doing the subtraction.
1382 testq(src, src);
1383 j(not_sign, on_not_smi_result);
1384 LoadSmiConstant(kScratchRegister, constant);
1385 subq(dst, kScratchRegister);
1386 } else {
1387 // Subtract by adding the negation.
1388 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1389 addq(kScratchRegister, dst);
1390 j(overflow, on_not_smi_result);
1391 movq(dst, kScratchRegister);
1392 }
1393 } else {
1394 if (constant->value() == Smi::kMinValue) {
1395 // Subtracting min-value from any non-negative value will overflow.
1396 // We test the non-negativeness before doing the subtraction.
1397 testq(src, src);
1398 j(not_sign, on_not_smi_result);
1399 LoadSmiConstant(dst, constant);
1400 // Adding and subtracting the min-value gives the same result, it only
1401 // differs on the overflow bit, which we don't check here.
1402 addq(dst, src);
1403 } else {
1404 // Subtract by adding the negation.
1405 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1406 addq(dst, src);
1407 j(overflow, on_not_smi_result);
1408 }
1409 }
1410}
1411
1412
1413template <typename LabelType>
1414void MacroAssembler::SmiDiv(Register dst,
1415 Register src1,
1416 Register src2,
1417 LabelType* on_not_smi_result) {
1418 ASSERT(!src1.is(kScratchRegister));
1419 ASSERT(!src2.is(kScratchRegister));
1420 ASSERT(!dst.is(kScratchRegister));
1421 ASSERT(!src2.is(rax));
1422 ASSERT(!src2.is(rdx));
1423 ASSERT(!src1.is(rdx));
1424
1425 // Check for 0 divisor (result is +/-Infinity).
1426 NearLabel positive_divisor;
1427 testq(src2, src2);
1428 j(zero, on_not_smi_result);
1429
1430 if (src1.is(rax)) {
1431 movq(kScratchRegister, src1);
1432 }
1433 SmiToInteger32(rax, src1);
1434 // We need to rule out dividing Smi::kMinValue by -1, since that would
1435 // overflow in idiv and raise an exception.
1436 // We combine this with negative zero test (negative zero only happens
1437 // when dividing zero by a negative number).
1438
1439 // We overshoot a little and go to slow case if we divide min-value
1440 // by any negative value, not just -1.
1441 NearLabel safe_div;
1442 testl(rax, Immediate(0x7fffffff));
1443 j(not_zero, &safe_div);
1444 testq(src2, src2);
1445 if (src1.is(rax)) {
1446 j(positive, &safe_div);
1447 movq(src1, kScratchRegister);
1448 jmp(on_not_smi_result);
1449 } else {
1450 j(negative, on_not_smi_result);
1451 }
1452 bind(&safe_div);
1453
1454 SmiToInteger32(src2, src2);
1455 // Sign extend src1 into edx:eax.
1456 cdq();
1457 idivl(src2);
1458 Integer32ToSmi(src2, src2);
1459 // Check that the remainder is zero.
1460 testl(rdx, rdx);
1461 if (src1.is(rax)) {
1462 NearLabel smi_result;
1463 j(zero, &smi_result);
1464 movq(src1, kScratchRegister);
1465 jmp(on_not_smi_result);
1466 bind(&smi_result);
1467 } else {
1468 j(not_zero, on_not_smi_result);
1469 }
1470 if (!dst.is(src1) && src1.is(rax)) {
1471 movq(src1, kScratchRegister);
1472 }
1473 Integer32ToSmi(dst, rax);
1474}
1475
1476
1477template <typename LabelType>
1478void MacroAssembler::SmiMod(Register dst,
1479 Register src1,
1480 Register src2,
1481 LabelType* on_not_smi_result) {
1482 ASSERT(!dst.is(kScratchRegister));
1483 ASSERT(!src1.is(kScratchRegister));
1484 ASSERT(!src2.is(kScratchRegister));
1485 ASSERT(!src2.is(rax));
1486 ASSERT(!src2.is(rdx));
1487 ASSERT(!src1.is(rdx));
1488 ASSERT(!src1.is(src2));
1489
1490 testq(src2, src2);
1491 j(zero, on_not_smi_result);
1492
1493 if (src1.is(rax)) {
1494 movq(kScratchRegister, src1);
1495 }
1496 SmiToInteger32(rax, src1);
1497 SmiToInteger32(src2, src2);
1498
1499 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1500 NearLabel safe_div;
1501 cmpl(rax, Immediate(Smi::kMinValue));
1502 j(not_equal, &safe_div);
1503 cmpl(src2, Immediate(-1));
1504 j(not_equal, &safe_div);
1505 // Retag inputs and go slow case.
1506 Integer32ToSmi(src2, src2);
1507 if (src1.is(rax)) {
1508 movq(src1, kScratchRegister);
1509 }
1510 jmp(on_not_smi_result);
1511 bind(&safe_div);
1512
1513 // Sign extend eax into edx:eax.
1514 cdq();
1515 idivl(src2);
1516 // Restore smi tags on inputs.
1517 Integer32ToSmi(src2, src2);
1518 if (src1.is(rax)) {
1519 movq(src1, kScratchRegister);
1520 }
1521 // Check for a negative zero result. If the result is zero, and the
1522 // dividend is negative, go slow to return a floating point negative zero.
1523 NearLabel smi_result;
1524 testl(rdx, rdx);
1525 j(not_zero, &smi_result);
1526 testq(src1, src1);
1527 j(negative, on_not_smi_result);
1528 bind(&smi_result);
1529 Integer32ToSmi(dst, rdx);
1530}
1531
1532
1533template <typename LabelType>
1534void MacroAssembler::SmiShiftLogicalRightConstant(
1535 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1536 // Logic right shift interprets its result as an *unsigned* number.
1537 if (dst.is(src)) {
1538 UNIMPLEMENTED(); // Not used.
1539 } else {
1540 movq(dst, src);
1541 if (shift_value == 0) {
1542 testq(dst, dst);
1543 j(negative, on_not_smi_result);
1544 }
1545 shr(dst, Immediate(shift_value + kSmiShift));
1546 shl(dst, Immediate(kSmiShift));
1547 }
1548}
1549
1550
1551template <typename LabelType>
1552void MacroAssembler::SmiShiftLogicalRight(Register dst,
1553 Register src1,
1554 Register src2,
1555 LabelType* on_not_smi_result) {
1556 ASSERT(!dst.is(kScratchRegister));
1557 ASSERT(!src1.is(kScratchRegister));
1558 ASSERT(!src2.is(kScratchRegister));
1559 ASSERT(!dst.is(rcx));
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001560 // dst and src1 can be the same, because the one case that bails out
1561 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001562 NearLabel result_ok;
1563 if (src1.is(rcx) || src2.is(rcx)) {
1564 movq(kScratchRegister, rcx);
1565 }
1566 if (!dst.is(src1)) {
1567 movq(dst, src1);
1568 }
1569 SmiToInteger32(rcx, src2);
1570 orl(rcx, Immediate(kSmiShift));
1571 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1572 shl(dst, Immediate(kSmiShift));
1573 testq(dst, dst);
1574 if (src1.is(rcx) || src2.is(rcx)) {
1575 NearLabel positive_result;
1576 j(positive, &positive_result);
1577 if (src1.is(rcx)) {
1578 movq(src1, kScratchRegister);
1579 } else {
1580 movq(src2, kScratchRegister);
1581 }
1582 jmp(on_not_smi_result);
1583 bind(&positive_result);
1584 } else {
1585 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1586 }
1587}
1588
1589
1590template <typename LabelType>
1591void MacroAssembler::SelectNonSmi(Register dst,
1592 Register src1,
1593 Register src2,
1594 LabelType* on_not_smis) {
1595 ASSERT(!dst.is(kScratchRegister));
1596 ASSERT(!src1.is(kScratchRegister));
1597 ASSERT(!src2.is(kScratchRegister));
1598 ASSERT(!dst.is(src1));
1599 ASSERT(!dst.is(src2));
1600 // Both operands must not be smis.
1601#ifdef DEBUG
1602 if (allow_stub_calls()) { // Check contains a stub call.
1603 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1604 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1605 }
1606#endif
1607 ASSERT_EQ(0, kSmiTag);
1608 ASSERT_EQ(0, Smi::FromInt(0));
1609 movl(kScratchRegister, Immediate(kSmiTagMask));
1610 and_(kScratchRegister, src1);
1611 testl(kScratchRegister, src2);
1612 // If non-zero then both are smis.
1613 j(not_zero, on_not_smis);
1614
1615 // Exactly one operand is a smi.
1616 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1617 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1618 subq(kScratchRegister, Immediate(1));
1619 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1620 movq(dst, src1);
1621 xor_(dst, src2);
1622 and_(dst, kScratchRegister);
1623 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1624 xor_(dst, src1);
1625 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1626}
1627
1628
1629template <typename LabelType>
1630void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1631 ASSERT_EQ(0, kSmiTag);
1632 Condition smi = CheckSmi(src);
1633 j(smi, on_smi);
1634}
1635
1636
1637template <typename LabelType>
1638void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1639 Condition smi = CheckSmi(src);
1640 j(NegateCondition(smi), on_not_smi);
1641}
1642
1643
1644template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001645void MacroAssembler::JumpUnlessNonNegativeSmi(
1646 Register src, LabelType* on_not_smi_or_negative) {
1647 Condition non_negative_smi = CheckNonNegativeSmi(src);
1648 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001649}
1650
1651
1652template <typename LabelType>
1653void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1654 Smi* constant,
1655 LabelType* on_equals) {
1656 SmiCompare(src, constant);
1657 j(equal, on_equals);
1658}
1659
1660
1661template <typename LabelType>
1662void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1663 LabelType* on_invalid) {
1664 Condition is_valid = CheckInteger32ValidSmiValue(src);
1665 j(NegateCondition(is_valid), on_invalid);
1666}
1667
1668
1669template <typename LabelType>
1670void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1671 LabelType* on_invalid) {
1672 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1673 j(NegateCondition(is_valid), on_invalid);
1674}
1675
1676
1677template <typename LabelType>
1678void MacroAssembler::JumpIfNotBothSmi(Register src1,
1679 Register src2,
1680 LabelType* on_not_both_smi) {
1681 Condition both_smi = CheckBothSmi(src1, src2);
1682 j(NegateCondition(both_smi), on_not_both_smi);
1683}
1684
1685
1686template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001687void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1688 Register src2,
1689 LabelType* on_not_both_smi) {
1690 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001691 j(NegateCondition(both_smi), on_not_both_smi);
1692}
1693
1694
1695template <typename LabelType>
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001696void MacroAssembler::JumpIfNotString(Register object,
1697 Register object_map,
1698 LabelType* not_string) {
1699 Condition is_smi = CheckSmi(object);
1700 j(is_smi, not_string);
1701 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1702 j(above_equal, not_string);
1703}
1704
1705
1706template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001707void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1708 Register second_object,
1709 Register scratch1,
1710 Register scratch2,
1711 LabelType* on_fail) {
1712 // Check that both objects are not smis.
1713 Condition either_smi = CheckEitherSmi(first_object, second_object);
1714 j(either_smi, on_fail);
1715
1716 // Load instance type for both strings.
1717 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1718 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1719 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1720 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1721
1722 // Check that both are flat ascii strings.
1723 ASSERT(kNotStringTag != 0);
1724 const int kFlatAsciiStringMask =
1725 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1726 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1727
1728 andl(scratch1, Immediate(kFlatAsciiStringMask));
1729 andl(scratch2, Immediate(kFlatAsciiStringMask));
1730 // Interleave the bits to check both scratch1 and scratch2 in one test.
1731 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1732 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1733 cmpl(scratch1,
1734 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1735 j(not_equal, on_fail);
1736}
1737
1738
1739template <typename LabelType>
1740void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1741 Register instance_type,
1742 Register scratch,
1743 LabelType *failure) {
1744 if (!scratch.is(instance_type)) {
1745 movl(scratch, instance_type);
1746 }
1747
1748 const int kFlatAsciiStringMask =
1749 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1750
1751 andl(scratch, Immediate(kFlatAsciiStringMask));
1752 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1753 j(not_equal, failure);
1754}
1755
1756
1757template <typename LabelType>
1758void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1759 Register first_object_instance_type,
1760 Register second_object_instance_type,
1761 Register scratch1,
1762 Register scratch2,
1763 LabelType* on_fail) {
1764 // Load instance type for both strings.
1765 movq(scratch1, first_object_instance_type);
1766 movq(scratch2, second_object_instance_type);
1767
1768 // Check that both are flat ascii strings.
1769 ASSERT(kNotStringTag != 0);
1770 const int kFlatAsciiStringMask =
1771 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1772 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1773
1774 andl(scratch1, Immediate(kFlatAsciiStringMask));
1775 andl(scratch2, Immediate(kFlatAsciiStringMask));
1776 // Interleave the bits to check both scratch1 and scratch2 in one test.
1777 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1778 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1779 cmpl(scratch1,
1780 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1781 j(not_equal, on_fail);
1782}
1783
1784
1785template <typename LabelType>
1786void MacroAssembler::InNewSpace(Register object,
1787 Register scratch,
1788 Condition cc,
1789 LabelType* branch) {
1790 if (Serializer::enabled()) {
1791 // Can't do arithmetic on external references if it might get serialized.
1792 // The mask isn't really an address. We load it as an external reference in
1793 // case the size of the new space is different between the snapshot maker
1794 // and the running system.
1795 if (scratch.is(object)) {
1796 movq(kScratchRegister, ExternalReference::new_space_mask());
1797 and_(scratch, kScratchRegister);
1798 } else {
1799 movq(scratch, ExternalReference::new_space_mask());
1800 and_(scratch, object);
1801 }
1802 movq(kScratchRegister, ExternalReference::new_space_start());
1803 cmpq(scratch, kScratchRegister);
1804 j(cc, branch);
1805 } else {
1806 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1807 intptr_t new_space_start =
1808 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1809 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1810 if (scratch.is(object)) {
1811 addq(scratch, kScratchRegister);
1812 } else {
1813 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1814 }
1815 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1816 j(cc, branch);
1817 }
1818}
1819
1820
1821template <typename LabelType>
1822void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1823 const ParameterCount& actual,
1824 Handle<Code> code_constant,
1825 Register code_register,
1826 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001827 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001828 CallWrapper* call_wrapper) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001829 bool definitely_matches = false;
1830 NearLabel invoke;
1831 if (expected.is_immediate()) {
1832 ASSERT(actual.is_immediate());
1833 if (expected.immediate() == actual.immediate()) {
1834 definitely_matches = true;
1835 } else {
1836 Set(rax, actual.immediate());
1837 if (expected.immediate() ==
1838 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1839 // Don't worry about adapting arguments for built-ins that
1840 // don't want that done. Skip adaption code by making it look
1841 // like we have a match between expected and actual number of
1842 // arguments.
1843 definitely_matches = true;
1844 } else {
1845 Set(rbx, expected.immediate());
1846 }
1847 }
1848 } else {
1849 if (actual.is_immediate()) {
1850 // Expected is in register, actual is immediate. This is the
1851 // case when we invoke function values without going through the
1852 // IC mechanism.
1853 cmpq(expected.reg(), Immediate(actual.immediate()));
1854 j(equal, &invoke);
1855 ASSERT(expected.reg().is(rbx));
1856 Set(rax, actual.immediate());
1857 } else if (!expected.reg().is(actual.reg())) {
1858 // Both expected and actual are in (different) registers. This
1859 // is the case when we invoke functions using call and apply.
1860 cmpq(expected.reg(), actual.reg());
1861 j(equal, &invoke);
1862 ASSERT(actual.reg().is(rax));
1863 ASSERT(expected.reg().is(rbx));
1864 }
1865 }
1866
1867 if (!definitely_matches) {
1868 Handle<Code> adaptor =
1869 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1870 if (!code_constant.is_null()) {
1871 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1872 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1873 } else if (!code_register.is(rdx)) {
1874 movq(rdx, code_register);
1875 }
1876
1877 if (flag == CALL_FUNCTION) {
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001878 if (call_wrapper != NULL) call_wrapper->BeforeCall(CallSize(adaptor));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001879 Call(adaptor, RelocInfo::CODE_TARGET);
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001880 if (call_wrapper != NULL) call_wrapper->AfterCall();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001881 jmp(done);
1882 } else {
1883 Jump(adaptor, RelocInfo::CODE_TARGET);
1884 }
1885 bind(&invoke);
1886 }
1887}
1888
ager@chromium.org9085a012009-05-11 19:22:57 +00001889
1890} } // namespace v8::internal
1891
1892#endif // V8_X64_MACRO_ASSEMBLER_X64_H_