blob: 376a5978c4c8eb6abe4a5a824e2f53ed89d3f1b3 [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.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000051static const Register kSmiConstantRegister = { 12 }; // r12 (callee save).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +000052static 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
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000281 // Simple comparison of smis. Both sides must be known smis to use these,
282 // otherwise use Cmp.
283 void SmiCompare(Register smi1, Register smi2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000284 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000285 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000286 void SmiCompare(const Operand& dst, Register src);
287 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000288 // Compare the int32 in src register to the value of the smi stored at dst.
289 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000290 // Sets sign and zero flags depending on value of smi in register.
291 void SmiTest(Register src);
292
ager@chromium.org4af710e2009-09-15 12:20:11 +0000293 // Functions performing a check on a known or potential smi. Returns
294 // a condition that is satisfied if the check is successful.
295
296 // Is the value a tagged smi.
297 Condition CheckSmi(Register src);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000298 Condition CheckSmi(const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000299
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000300 // Is the value a non-negative tagged smi.
301 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000302
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000303 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000304 Condition CheckBothSmi(Register first, Register second);
305
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000306 // Are both values non-negative tagged smis.
307 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000308
309 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000310 Condition CheckEitherSmi(Register first,
311 Register second,
312 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000313
ager@chromium.org4af710e2009-09-15 12:20:11 +0000314 // Is the value the minimum smi value (since we are using
315 // two's complement numbers, negating the value is known to yield
316 // a non-smi value).
317 Condition CheckIsMinSmi(Register src);
318
ager@chromium.org4af710e2009-09-15 12:20:11 +0000319 // Checks whether an 32-bit integer value is a valid for conversion
320 // to a smi.
321 Condition CheckInteger32ValidSmiValue(Register src);
322
ager@chromium.org3811b432009-10-28 14:53:37 +0000323 // Checks whether an 32-bit unsigned integer value is a valid for
324 // conversion to a smi.
325 Condition CheckUInteger32ValidSmiValue(Register src);
326
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000327 // Check whether src is a Smi, and set dst to zero if it is a smi,
328 // and to one if it isn't.
329 void CheckSmiToIndicator(Register dst, Register src);
330 void CheckSmiToIndicator(Register dst, const Operand& src);
331
ager@chromium.org4af710e2009-09-15 12:20:11 +0000332 // Test-and-jump functions. Typically combines a check function
333 // above with a conditional jump.
334
335 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000336 template <typename LabelType>
337 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000338
ager@chromium.org3811b432009-10-28 14:53:37 +0000339 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000340 template <typename LabelType>
341 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000342
ager@chromium.org4af710e2009-09-15 12:20:11 +0000343 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000344 template <typename LabelType>
345 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000346
347 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000348 template <typename LabelType>
349 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000350
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000351 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000352 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000353 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000354
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000355 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000356 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000357 template <typename LabelType>
358 void JumpIfSmiEqualsConstant(Register src,
359 Smi* constant,
360 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000361
ager@chromium.org4af710e2009-09-15 12:20:11 +0000362 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000363 template <typename LabelType>
364 void JumpIfNotBothSmi(Register src1,
365 Register src2,
366 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000367
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000368 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000369 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000370 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
371 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000372
ager@chromium.org4af710e2009-09-15 12:20:11 +0000373 // Operations on tagged smi values.
374
375 // Smis represent a subset of integers. The subset is always equivalent to
376 // a two's complement interpretation of a fixed number of bits.
377
378 // Optimistically adds an integer constant to a supposed smi.
379 // If the src is not a smi, or the result is not a smi, jump to
380 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000381 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000382 void SmiTryAddConstant(Register dst,
383 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000384 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000385 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000386
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000387 // Add an integer constant to a tagged smi, giving a tagged smi as result.
388 // No overflow testing on the result is done.
389 void SmiAddConstant(Register dst, Register src, Smi* constant);
390
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000391 // Add an integer constant to a tagged smi, giving a tagged smi as result.
392 // No overflow testing on the result is done.
393 void SmiAddConstant(const Operand& dst, Smi* constant);
394
ager@chromium.org4af710e2009-09-15 12:20:11 +0000395 // Add an integer constant to a tagged smi, giving a tagged smi as result,
396 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000397 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000398 void SmiAddConstant(Register dst,
399 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000400 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000401 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000402
403 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000404 // result. No testing on the result is done. Sets the N and Z flags
405 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000406 void SmiSubConstant(Register dst, Register src, Smi* constant);
407
408 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000409 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000410 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000411 void SmiSubConstant(Register dst,
412 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000413 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000414 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000415
416 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000417 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000418 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000419 void SmiNeg(Register dst,
420 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000421 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000422
423 // Adds smi values and return the result as a smi.
424 // If dst is src1, then src1 will be destroyed, even if
425 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000426 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000427 void SmiAdd(Register dst,
428 Register src1,
429 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000430 LabelType* on_not_smi_result);
431
432 void SmiAdd(Register dst,
433 Register src1,
434 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000435
436 // Subtracts smi values and return the result as a smi.
437 // If dst is src1, then src1 will be destroyed, even if
438 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000439 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000440 void SmiSub(Register dst,
441 Register src1,
442 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000443 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000444
ager@chromium.orgac091b72010-05-05 07:34:42 +0000445 void SmiSub(Register dst,
446 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000447 Register src2);
448
449 template <typename LabelType>
450 void SmiSub(Register dst,
451 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000452 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000453 LabelType* on_not_smi_result);
454
455 void SmiSub(Register dst,
456 Register src1,
457 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000458
ager@chromium.org4af710e2009-09-15 12:20:11 +0000459 // Multiplies smi values and return the result as a smi,
460 // if possible.
461 // If dst is src1, then src1 will be destroyed, even if
462 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000463 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000464 void SmiMul(Register dst,
465 Register src1,
466 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000467 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000468
469 // Divides one smi by another and returns the quotient.
470 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000471 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000472 void SmiDiv(Register dst,
473 Register src1,
474 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000475 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000476
477 // Divides one smi by another and returns the remainder.
478 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000479 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000480 void SmiMod(Register dst,
481 Register src1,
482 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000483 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000484
485 // Bitwise operations.
486 void SmiNot(Register dst, Register src);
487 void SmiAnd(Register dst, Register src1, Register src2);
488 void SmiOr(Register dst, Register src1, Register src2);
489 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000490 void SmiAndConstant(Register dst, Register src1, Smi* constant);
491 void SmiOrConstant(Register dst, Register src1, Smi* constant);
492 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000493
494 void SmiShiftLeftConstant(Register dst,
495 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000496 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000497 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000498 void SmiShiftLogicalRightConstant(Register dst,
499 Register src,
500 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000501 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000502 void SmiShiftArithmeticRightConstant(Register dst,
503 Register src,
504 int shift_value);
505
506 // Shifts a smi value to the left, and returns the result if that is a smi.
507 // Uses and clobbers rcx, so dst may not be rcx.
508 void SmiShiftLeft(Register dst,
509 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000510 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000511 // Shifts a smi value to the right, shifting in zero bits at the top, and
512 // returns the unsigned intepretation of the result if that is a smi.
513 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000514 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000515 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000516 Register src1,
517 Register src2,
518 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000519 // Shifts a smi value to the right, sign extending the top, and
520 // returns the signed intepretation of the result. That will always
521 // be a valid smi value, since it's numerically smaller than the
522 // original.
523 // Uses and clobbers rcx, so dst may not be rcx.
524 void SmiShiftArithmeticRight(Register dst,
525 Register src1,
526 Register src2);
527
528 // Specialized operations
529
530 // Select the non-smi register of two registers where exactly one is a
531 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000532 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000533 void SelectNonSmi(Register dst,
534 Register src1,
535 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000536 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000537
538 // Converts, if necessary, a smi to a combination of number and
539 // multiplier to be used as a scaled index.
540 // The src register contains a *positive* smi value. The shift is the
541 // power of two to multiply the index value by (e.g.
542 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
543 // The returned index register may be either src or dst, depending
544 // on what is most efficient. If src and dst are different registers,
545 // src is always unchanged.
546 SmiIndex SmiToIndex(Register dst, Register src, int shift);
547
548 // Converts a positive smi to a negative index.
549 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
550
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000551 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000552 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000553 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000554 }
555
556 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000557 Register constant = GetSmiConstant(source);
558 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000559 }
560
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000561 void Push(Smi* smi);
562 void Test(const Operand& dst, Smi* source);
563
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000564 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000565 // String macros.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000566
567 // If object is a string, its map is loaded into object_map.
568 template <typename LabelType>
569 void JumpIfNotString(Register object,
570 Register object_map,
571 LabelType* not_string);
572
573
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000574 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
576 Register second_object,
577 Register scratch1,
578 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000579 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000580
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000581 // Check whether the instance type represents a flat ascii string. Jump to the
582 // label if not. If the instance type can be scratched specify same register
583 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000584 template <typename LabelType>
585 void JumpIfInstanceTypeIsNotSequentialAscii(
586 Register instance_type,
587 Register scratch,
588 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000589
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000590 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000591 void JumpIfBothInstanceTypesAreNotSequentialAscii(
592 Register first_object_instance_type,
593 Register second_object_instance_type,
594 Register scratch1,
595 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000596 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000597
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000598 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000599 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000600
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000601 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000602 void Set(Register dst, int64_t x);
603 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000604
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000605 // Move if the registers are not identical.
606 void Move(Register target, Register source);
607
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000608 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000609 void Move(Register dst, Handle<Object> source);
610 void Move(const Operand& dst, Handle<Object> source);
611 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000612 void Cmp(const Operand& dst, Handle<Object> source);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000613 void Cmp(Register dst, Smi* src);
614 void Cmp(const Operand& dst, Smi* src);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000615 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000616
617 // Emit code to discard a non-negative number of pointer-sized elements
618 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000619 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000620
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000621 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000622
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000623 // Control Flow
624 void Jump(Address destination, RelocInfo::Mode rmode);
625 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000626 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
627
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000628 void Call(Address destination, RelocInfo::Mode rmode);
629 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000630 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000631
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000632 // The size of the code generated for different call instructions.
633 int CallSize(Address destination, RelocInfo::Mode rmode) {
634 return kCallInstructionLength;
635 }
636 int CallSize(ExternalReference ext) {
637 return kCallInstructionLength;
638 }
639 int CallSize(Handle<Code> code_object) {
640 // Code calls use 32-bit relative addressing.
641 return kShortCallInstructionLength;
642 }
643 int CallSize(Register target) {
644 // Opcode: REX_opt FF /2 m64
645 return (target.high_bit() != 0) ? 3 : 2;
646 }
647 int CallSize(const Operand& target) {
648 // Opcode: REX_opt FF /2 m64
649 return (target.requires_rex() ? 2 : 1) + target.operand_size();
650 }
651
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000652 // Emit call to the code we are currently generating.
653 void CallSelf() {
654 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
655 Call(self, RelocInfo::CODE_TARGET);
656 }
657
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000658 // Non-x64 instructions.
659 // Push/pop all general purpose registers.
660 // Does not push rsp/rbp nor any of the assembler's special purpose registers
661 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
662 void Pushad();
663 void Popad();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000664 // Sets the stack as after performing Popad, without actually loading the
665 // registers.
666 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000667
ager@chromium.org9085a012009-05-11 19:22:57 +0000668 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000669 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000670 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000671 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000672 void CmpObjectType(Register heap_object, InstanceType type, Register map);
673
674 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000675 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000676 void CmpInstanceType(Register map, InstanceType type);
677
ager@chromium.org5c838252010-02-19 08:53:10 +0000678 // Check if the map of an object is equal to a specified map and
679 // branch to label if not. Skip the smi check if not required
680 // (object is known to be a heap object)
681 void CheckMap(Register obj,
682 Handle<Map> map,
683 Label* fail,
684 bool is_heap_object);
685
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000686 // Check if the object in register heap_object is a string. Afterwards the
687 // register map contains the object map and the register instance_type
688 // contains the instance_type. The registers map and instance_type can be the
689 // same in which case it contains the instance type afterwards. Either of the
690 // registers map and instance_type can be the same as heap_object.
691 Condition IsObjectStringType(Register heap_object,
692 Register map,
693 Register instance_type);
694
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000695 // FCmp compares and pops the two values on top of the FPU stack.
696 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000697 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
698 void FCmp();
699
ager@chromium.org5c838252010-02-19 08:53:10 +0000700 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000701 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000702
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000703 // Abort execution if argument is a smi. Used in debug code.
704 void AbortIfSmi(Register object);
705
lrn@chromium.org25156de2010-04-06 13:10:27 +0000706 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000707 void AbortIfNotSmi(Register object);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000708 void AbortIfNotSmi(const Operand& object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000709
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000710 // Abort execution if argument is a string. Used in debug code.
711 void AbortIfNotString(Register object);
712
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000713 // Abort execution if argument is not the root value with the given index.
714 void AbortIfNotRootValue(Register src,
715 Heap::RootListIndex root_value_index,
716 const char* message);
717
ager@chromium.org9085a012009-05-11 19:22:57 +0000718 // ---------------------------------------------------------------------------
719 // Exception handling
720
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000721 // Push a new try handler and link into try handler chain. The return
722 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000723 void PushTryHandler(CodeLocation try_location, HandlerType type);
724
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000725 // Unlink the stack handler on top of the stack from the try handler chain.
726 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000727
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000728 // Activate the top handler in the try hander chain and pass the
729 // thrown value.
730 void Throw(Register value);
731
732 // Propagate an uncatchable exception out of the current JS stack.
733 void ThrowUncatchable(UncatchableExceptionType type, Register value);
734
ager@chromium.org9085a012009-05-11 19:22:57 +0000735 // ---------------------------------------------------------------------------
736 // Inline caching support
737
ager@chromium.org9085a012009-05-11 19:22:57 +0000738 // Generate code for checking access rights - used for security checks
739 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000740 // is left untouched, but the scratch register and kScratchRegister,
741 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000742 void CheckAccessGlobalProxy(Register holder_reg,
743 Register scratch,
744 Label* miss);
745
746
747 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000748 // Allocation support
749
750 // Allocate an object in new space. If the new space is exhausted control
751 // continues at the gc_required label. The allocated object is returned in
752 // result and end of the new object is returned in result_end. The register
753 // scratch can be passed as no_reg in which case an additional object
754 // reference will be added to the reloc info. The returned pointers in result
755 // and result_end have not yet been tagged as heap objects. If
756 // result_contains_top_on_entry is true the content of result is known to be
757 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000758 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000759 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000760 void AllocateInNewSpace(int object_size,
761 Register result,
762 Register result_end,
763 Register scratch,
764 Label* gc_required,
765 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000766
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000767 void AllocateInNewSpace(int header_size,
768 ScaleFactor element_size,
769 Register element_count,
770 Register result,
771 Register result_end,
772 Register scratch,
773 Label* gc_required,
774 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000775
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000776 void AllocateInNewSpace(Register object_size,
777 Register result,
778 Register result_end,
779 Register scratch,
780 Label* gc_required,
781 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000782
783 // Undo allocation in new space. The object passed and objects allocated after
784 // it will no longer be allocated. Make sure that no pointers are left to the
785 // object(s) no longer allocated as they would be invalid when allocation is
786 // un-done.
787 void UndoAllocationInNewSpace(Register object);
788
ager@chromium.org3811b432009-10-28 14:53:37 +0000789 // Allocate a heap number in new space with undefined value. Returns
790 // tagged pointer in result register, or jumps to gc_required if new
791 // space is full.
792 void AllocateHeapNumber(Register result,
793 Register scratch,
794 Label* gc_required);
795
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000796 // Allocate a sequential string. All the header fields of the string object
797 // are initialized.
798 void AllocateTwoByteString(Register result,
799 Register length,
800 Register scratch1,
801 Register scratch2,
802 Register scratch3,
803 Label* gc_required);
804 void AllocateAsciiString(Register result,
805 Register length,
806 Register scratch1,
807 Register scratch2,
808 Register scratch3,
809 Label* gc_required);
810
811 // Allocate a raw cons string object. Only the map field of the result is
812 // initialized.
813 void AllocateConsString(Register result,
814 Register scratch1,
815 Register scratch2,
816 Label* gc_required);
817 void AllocateAsciiConsString(Register result,
818 Register scratch1,
819 Register scratch2,
820 Label* gc_required);
821
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000822 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000823 // Support functions.
824
825 // Check if result is zero and op is negative.
826 void NegativeZeroTest(Register result, Register op, Label* then_label);
827
828 // Check if result is zero and op is negative in code using jump targets.
829 void NegativeZeroTest(CodeGenerator* cgen,
830 Register result,
831 Register op,
832 JumpTarget* then_target);
833
834 // Check if result is zero and any of op1 and op2 are negative.
835 // Register scratch is destroyed, and it must be different from op2.
836 void NegativeZeroTest(Register result, Register op1, Register op2,
837 Register scratch, Label* then_label);
838
839 // Try to get function prototype of a function and puts the value in
840 // the result register. Checks that the function really is a
841 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000842 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000843 // clobbered.
844 void TryGetFunctionPrototype(Register function,
845 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000846 Label* miss);
847
848 // Generates code for reporting that an illegal operation has
849 // occurred.
850 void IllegalOperation(int num_arguments);
851
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000852 // Picks out an array index from the hash field.
853 // Register use:
854 // hash - holds the index's hash. Clobbered.
855 // index - holds the overwritten index on exit.
856 void IndexFromHash(Register hash, Register index);
857
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000858 // Find the function context up the context chain.
859 void LoadContext(Register dst, int context_chain_length);
860
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000861 // Load the global function with the given index.
862 void LoadGlobalFunction(int index, Register function);
863
864 // Load the initial map from the global function. The registers
865 // function and map can be the same.
866 void LoadGlobalFunctionInitialMap(Register function, Register map);
867
ager@chromium.org9085a012009-05-11 19:22:57 +0000868 // ---------------------------------------------------------------------------
869 // Runtime calls
870
871 // Call a code stub.
872 void CallStub(CodeStub* stub);
873
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000874 // Call a code stub and return the code object called. Try to generate
875 // the code if necessary. Do not perform a GC but instead return a retry
876 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000877 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000878
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000879 // Tail call a code stub (jump).
880 void TailCallStub(CodeStub* stub);
881
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000882 // Tail call a code stub (jump) and return the code object called. Try to
883 // generate the code if necessary. Do not perform a GC but instead return
884 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000885 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000886
ager@chromium.org9085a012009-05-11 19:22:57 +0000887 // Return from a code stub after popping its arguments.
888 void StubReturn(int argc);
889
890 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000891 void CallRuntime(Runtime::Function* f, int num_arguments);
892
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000893 // Call a runtime function and save the value of XMM registers.
894 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
895
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000896 // Call a runtime function, returning the CodeStub object called.
897 // Try to generate the stub code if necessary. Do not perform a GC
898 // but instead return a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000899 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
900 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000901
ager@chromium.org9085a012009-05-11 19:22:57 +0000902 // Convenience function: Same as above, but takes the fid instead.
903 void CallRuntime(Runtime::FunctionId id, int num_arguments);
904
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000905 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000906 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
907 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000908
ager@chromium.org5c838252010-02-19 08:53:10 +0000909 // Convenience function: call an external reference.
910 void CallExternalReference(const ExternalReference& ext,
911 int num_arguments);
912
ager@chromium.org9085a012009-05-11 19:22:57 +0000913 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000914 // Like JumpToExternalReference, but also takes care of passing the number
915 // of parameters.
916 void TailCallExternalReference(const ExternalReference& ext,
917 int num_arguments,
918 int result_size);
919
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000920 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
921 const ExternalReference& ext, int num_arguments, int result_size);
922
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000923 // Convenience function: tail call a runtime routine (jump).
924 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000925 int num_arguments,
926 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000927
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000928 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
929 int num_arguments,
930 int result_size);
931
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000932 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000933 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000934
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000935 // Jump to a runtime routine.
936 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
937 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000938
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000939 // Prepares stack to put arguments (aligns and so on).
940 // WIN64 calling convention requires to put the pointer to the return value
941 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
942 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
943 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
944 void PrepareCallApiFunction(int arg_stack_space);
945
946 // Calls an API function. Allocates HandleScope, extracts
947 // returned value from handle and propagates exceptions.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000948 // Clobbers r14, r15, rbx and caller-save registers. Restores context.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000949 // On return removes stack_space * kPointerSize (GCed).
950 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
951 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000952
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953 // Before calling a C-function from generated code, align arguments on stack.
954 // After aligning the frame, arguments must be stored in esp[0], esp[4],
955 // etc., not pushed. The argument count assumes all arguments are word sized.
956 // The number of slots reserved for arguments depends on platform. On Windows
957 // stack slots are reserved for the arguments passed in registers. On other
958 // platforms stack slots are only reserved for the arguments actually passed
959 // on the stack.
960 void PrepareCallCFunction(int num_arguments);
961
962 // Calls a C function and cleans up the space for arguments allocated
963 // by PrepareCallCFunction. The called function is not allowed to trigger a
964 // garbage collection, since that might move the code and invalidate the
965 // return address (unless this is somehow accounted for by the called
966 // function).
967 void CallCFunction(ExternalReference function, int num_arguments);
968 void CallCFunction(Register function, int num_arguments);
969
970 // Calculate the number of stack slots to reserve for arguments when calling a
971 // C function.
972 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000973
974 // ---------------------------------------------------------------------------
975 // Utilities
976
977 void Ret();
978
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000979 // Return and drop arguments from stack, where the number of arguments
980 // may be bigger than 2^16 - 1. Requires a scratch register.
981 void Ret(int bytes_dropped, Register scratch);
982
ager@chromium.org9085a012009-05-11 19:22:57 +0000983 Handle<Object> CodeObject() { return code_object_; }
984
985
986 // ---------------------------------------------------------------------------
987 // StatsCounter support
988
989 void SetCounter(StatsCounter* counter, int value);
990 void IncrementCounter(StatsCounter* counter, int value);
991 void DecrementCounter(StatsCounter* counter, int value);
992
993
994 // ---------------------------------------------------------------------------
995 // Debugging
996
997 // Calls Abort(msg) if the condition cc is not satisfied.
998 // Use --debug_code to enable.
999 void Assert(Condition cc, const char* msg);
1000
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001001 void AssertFastElements(Register elements);
1002
ager@chromium.org9085a012009-05-11 19:22:57 +00001003 // Like Assert(), but always enabled.
1004 void Check(Condition cc, const char* msg);
1005
1006 // Print a message to stdout and abort execution.
1007 void Abort(const char* msg);
1008
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001009 // Check that the stack is aligned.
1010 void CheckStackAlignment();
1011
ager@chromium.org9085a012009-05-11 19:22:57 +00001012 // Verify restrictions about code generated in stubs.
1013 void set_generating_stub(bool value) { generating_stub_ = value; }
1014 bool generating_stub() { return generating_stub_; }
1015 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
1016 bool allow_stub_calls() { return allow_stub_calls_; }
1017
1018 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001019 // Order general registers are pushed by Pushad.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001020 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r14, r15.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001021 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001022 static const int kNumSafepointSavedRegisters = 11;
1023
ager@chromium.org9085a012009-05-11 19:22:57 +00001024 bool generating_stub_;
1025 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001026
1027 // Returns a register holding the smi value. The register MUST NOT be
1028 // modified. It may be the "smi 1 constant" register.
1029 Register GetSmiConstant(Smi* value);
1030
1031 // Moves the smi value to the destination register.
1032 void LoadSmiConstant(Register dst, Smi* value);
1033
ager@chromium.org5c838252010-02-19 08:53:10 +00001034 // This handle will be patched with the code object on installation.
1035 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +00001036
1037 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001038 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +00001039 void InvokePrologue(const ParameterCount& expected,
1040 const ParameterCount& actual,
1041 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001042 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001043 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001044 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001045 CallWrapper* call_wrapper);
ager@chromium.org9085a012009-05-11 19:22:57 +00001046
ager@chromium.org9085a012009-05-11 19:22:57 +00001047 // Activation support.
1048 void EnterFrame(StackFrame::Type type);
1049 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001050
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001051 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001052
1053 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
1054 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001055 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001056
1057 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001058
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001059 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001060 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001061 // Otherwise the address of the new-space top is loaded into scratch (if
1062 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001063 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001064 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001065 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001066 // Update allocation top with value in result_end register.
1067 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001068 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001069
1070 // Helper for PopHandleScope. Allowed to perform a GC and returns
1071 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1072 // possibly returns a failure object indicating an allocation failure.
1073 Object* PopHandleScopeHelper(Register saved,
1074 Register scratch,
1075 bool gc_allowed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001076
1077
1078 // Compute memory operands for safepoint stack slots.
1079 Operand SafepointRegisterSlot(Register reg);
1080 static int SafepointRegisterStackIndex(int reg_code) {
1081 return kNumSafepointRegisters - kSafepointPushRegisterIndices[reg_code] - 1;
1082 }
1083
1084 // Needs access to SafepointRegisterStackIndex for optimized frame
1085 // traversal.
1086 friend class OptimizedFrame;
ager@chromium.org9085a012009-05-11 19:22:57 +00001087};
1088
1089
ager@chromium.org4af710e2009-09-15 12:20:11 +00001090// The code patcher is used to patch (typically) small parts of code e.g. for
1091// debugging and other types of instrumentation. When using the code patcher
1092// the exact number of bytes specified must be emitted. Is not legal to emit
1093// relocation information. If any of these constraints are violated it causes
1094// an assertion.
1095class CodePatcher {
1096 public:
1097 CodePatcher(byte* address, int size);
1098 virtual ~CodePatcher();
1099
1100 // Macro assembler to emit code.
1101 MacroAssembler* masm() { return &masm_; }
1102
1103 private:
1104 byte* address_; // The address of the code being patched.
1105 int size_; // Number of bytes of the expected patch size.
1106 MacroAssembler masm_; // Macro assembler used to generate the code.
1107};
1108
1109
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001110// Helper class for generating code or data associated with the code
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001111// right before or after a call instruction. As an example this can be used to
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001112// generate safepoint data after calls for crankshaft.
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001113class CallWrapper {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001114 public:
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001115 CallWrapper() { }
1116 virtual ~CallWrapper() { }
1117 // Called just before emitting a call. Argument is the size of the generated
1118 // call code.
1119 virtual void BeforeCall(int call_size) = 0;
1120 // Called just after emitting a call, i.e., at the return site for the call.
1121 virtual void AfterCall() = 0;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001122};
1123
1124
ager@chromium.org9085a012009-05-11 19:22:57 +00001125// -----------------------------------------------------------------------------
1126// Static helper functions.
1127
1128// Generate an Operand for loading a field from an object.
1129static inline Operand FieldOperand(Register object, int offset) {
1130 return Operand(object, offset - kHeapObjectTag);
1131}
1132
1133
1134// Generate an Operand for loading an indexed field from an object.
1135static inline Operand FieldOperand(Register object,
1136 Register index,
1137 ScaleFactor scale,
1138 int offset) {
1139 return Operand(object, index, scale, offset - kHeapObjectTag);
1140}
1141
1142
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001143static inline Operand ContextOperand(Register context, int index) {
1144 return Operand(context, Context::SlotOffset(index));
1145}
1146
1147
1148static inline Operand GlobalObjectOperand() {
1149 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1150}
1151
1152
1153// Provides access to exit frame stack space (not GCed).
1154static inline Operand StackSpaceOperand(int index) {
1155#ifdef _WIN64
1156 const int kShaddowSpace = 4;
1157 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1158#else
1159 return Operand(rsp, index * kPointerSize);
1160#endif
1161}
1162
1163
1164
ager@chromium.org9085a012009-05-11 19:22:57 +00001165#ifdef GENERATED_CODE_COVERAGE
1166extern void LogGeneratedCodeCoverage(const char* file_line);
1167#define CODE_COVERAGE_STRINGIFY(x) #x
1168#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1169#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1170#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001171 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001172 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1173 masm->pushfd(); \
1174 masm->pushad(); \
1175 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001176 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001177 masm->pop(rax); \
1178 masm->popad(); \
1179 masm->popfd(); \
1180 } \
1181 masm->
1182#else
1183#define ACCESS_MASM(masm) masm->
1184#endif
1185
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001186// -----------------------------------------------------------------------------
1187// Template implementations.
1188
1189static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1190
1191
1192template <typename LabelType>
1193void MacroAssembler::SmiNeg(Register dst,
1194 Register src,
1195 LabelType* on_smi_result) {
1196 if (dst.is(src)) {
1197 ASSERT(!dst.is(kScratchRegister));
1198 movq(kScratchRegister, src);
1199 neg(dst); // Low 32 bits are retained as zero by negation.
1200 // Test if result is zero or Smi::kMinValue.
1201 cmpq(dst, kScratchRegister);
1202 j(not_equal, on_smi_result);
1203 movq(src, kScratchRegister);
1204 } else {
1205 movq(dst, src);
1206 neg(dst);
1207 cmpq(dst, src);
1208 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1209 j(not_equal, on_smi_result);
1210 }
1211}
1212
1213
1214template <typename LabelType>
1215void MacroAssembler::SmiAdd(Register dst,
1216 Register src1,
1217 Register src2,
1218 LabelType* on_not_smi_result) {
1219 ASSERT_NOT_NULL(on_not_smi_result);
1220 ASSERT(!dst.is(src2));
1221 if (dst.is(src1)) {
1222 movq(kScratchRegister, src1);
1223 addq(kScratchRegister, src2);
1224 j(overflow, on_not_smi_result);
1225 movq(dst, kScratchRegister);
1226 } else {
1227 movq(dst, src1);
1228 addq(dst, src2);
1229 j(overflow, on_not_smi_result);
1230 }
1231}
1232
1233
1234template <typename LabelType>
1235void MacroAssembler::SmiSub(Register dst,
1236 Register src1,
1237 Register src2,
1238 LabelType* on_not_smi_result) {
1239 ASSERT_NOT_NULL(on_not_smi_result);
1240 ASSERT(!dst.is(src2));
1241 if (dst.is(src1)) {
1242 cmpq(dst, src2);
1243 j(overflow, on_not_smi_result);
1244 subq(dst, src2);
1245 } else {
1246 movq(dst, src1);
1247 subq(dst, src2);
1248 j(overflow, on_not_smi_result);
1249 }
1250}
1251
1252
1253template <typename LabelType>
1254void MacroAssembler::SmiSub(Register dst,
1255 Register src1,
1256 const Operand& src2,
1257 LabelType* on_not_smi_result) {
1258 ASSERT_NOT_NULL(on_not_smi_result);
1259 if (dst.is(src1)) {
1260 movq(kScratchRegister, src2);
1261 cmpq(src1, kScratchRegister);
1262 j(overflow, on_not_smi_result);
1263 subq(src1, kScratchRegister);
1264 } else {
1265 movq(dst, src1);
1266 subq(dst, src2);
1267 j(overflow, on_not_smi_result);
1268 }
1269}
1270
1271
1272template <typename LabelType>
1273void MacroAssembler::SmiMul(Register dst,
1274 Register src1,
1275 Register src2,
1276 LabelType* on_not_smi_result) {
1277 ASSERT(!dst.is(src2));
1278 ASSERT(!dst.is(kScratchRegister));
1279 ASSERT(!src1.is(kScratchRegister));
1280 ASSERT(!src2.is(kScratchRegister));
1281
1282 if (dst.is(src1)) {
1283 NearLabel failure, zero_correct_result;
1284 movq(kScratchRegister, src1); // Create backup for later testing.
1285 SmiToInteger64(dst, src1);
1286 imul(dst, src2);
1287 j(overflow, &failure);
1288
1289 // Check for negative zero result. If product is zero, and one
1290 // argument is negative, go to slow case.
1291 NearLabel correct_result;
1292 testq(dst, dst);
1293 j(not_zero, &correct_result);
1294
1295 movq(dst, kScratchRegister);
1296 xor_(dst, src2);
1297 j(positive, &zero_correct_result); // Result was positive zero.
1298
1299 bind(&failure); // Reused failure exit, restores src1.
1300 movq(src1, kScratchRegister);
1301 jmp(on_not_smi_result);
1302
1303 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001304 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001305
1306 bind(&correct_result);
1307 } else {
1308 SmiToInteger64(dst, src1);
1309 imul(dst, src2);
1310 j(overflow, on_not_smi_result);
1311 // Check for negative zero result. If product is zero, and one
1312 // argument is negative, go to slow case.
1313 NearLabel correct_result;
1314 testq(dst, dst);
1315 j(not_zero, &correct_result);
1316 // One of src1 and src2 is zero, the check whether the other is
1317 // negative.
1318 movq(kScratchRegister, src1);
1319 xor_(kScratchRegister, src2);
1320 j(negative, on_not_smi_result);
1321 bind(&correct_result);
1322 }
1323}
1324
1325
1326template <typename LabelType>
1327void MacroAssembler::SmiTryAddConstant(Register dst,
1328 Register src,
1329 Smi* constant,
1330 LabelType* on_not_smi_result) {
1331 // Does not assume that src is a smi.
1332 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1333 ASSERT_EQ(0, kSmiTag);
1334 ASSERT(!dst.is(kScratchRegister));
1335 ASSERT(!src.is(kScratchRegister));
1336
1337 JumpIfNotSmi(src, on_not_smi_result);
1338 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1339 LoadSmiConstant(tmp, constant);
1340 addq(tmp, src);
1341 j(overflow, on_not_smi_result);
1342 if (dst.is(src)) {
1343 movq(dst, tmp);
1344 }
1345}
1346
1347
1348template <typename LabelType>
1349void MacroAssembler::SmiAddConstant(Register dst,
1350 Register src,
1351 Smi* constant,
1352 LabelType* on_not_smi_result) {
1353 if (constant->value() == 0) {
1354 if (!dst.is(src)) {
1355 movq(dst, src);
1356 }
1357 } else if (dst.is(src)) {
1358 ASSERT(!dst.is(kScratchRegister));
1359
1360 LoadSmiConstant(kScratchRegister, constant);
1361 addq(kScratchRegister, src);
1362 j(overflow, on_not_smi_result);
1363 movq(dst, kScratchRegister);
1364 } else {
1365 LoadSmiConstant(dst, constant);
1366 addq(dst, src);
1367 j(overflow, on_not_smi_result);
1368 }
1369}
1370
1371
1372template <typename LabelType>
1373void MacroAssembler::SmiSubConstant(Register dst,
1374 Register src,
1375 Smi* constant,
1376 LabelType* on_not_smi_result) {
1377 if (constant->value() == 0) {
1378 if (!dst.is(src)) {
1379 movq(dst, src);
1380 }
1381 } else if (dst.is(src)) {
1382 ASSERT(!dst.is(kScratchRegister));
1383 if (constant->value() == Smi::kMinValue) {
1384 // Subtracting min-value from any non-negative value will overflow.
1385 // We test the non-negativeness before doing the subtraction.
1386 testq(src, src);
1387 j(not_sign, on_not_smi_result);
1388 LoadSmiConstant(kScratchRegister, constant);
1389 subq(dst, kScratchRegister);
1390 } else {
1391 // Subtract by adding the negation.
1392 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1393 addq(kScratchRegister, dst);
1394 j(overflow, on_not_smi_result);
1395 movq(dst, kScratchRegister);
1396 }
1397 } else {
1398 if (constant->value() == Smi::kMinValue) {
1399 // Subtracting min-value from any non-negative value will overflow.
1400 // We test the non-negativeness before doing the subtraction.
1401 testq(src, src);
1402 j(not_sign, on_not_smi_result);
1403 LoadSmiConstant(dst, constant);
1404 // Adding and subtracting the min-value gives the same result, it only
1405 // differs on the overflow bit, which we don't check here.
1406 addq(dst, src);
1407 } else {
1408 // Subtract by adding the negation.
1409 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1410 addq(dst, src);
1411 j(overflow, on_not_smi_result);
1412 }
1413 }
1414}
1415
1416
1417template <typename LabelType>
1418void MacroAssembler::SmiDiv(Register dst,
1419 Register src1,
1420 Register src2,
1421 LabelType* on_not_smi_result) {
1422 ASSERT(!src1.is(kScratchRegister));
1423 ASSERT(!src2.is(kScratchRegister));
1424 ASSERT(!dst.is(kScratchRegister));
1425 ASSERT(!src2.is(rax));
1426 ASSERT(!src2.is(rdx));
1427 ASSERT(!src1.is(rdx));
1428
1429 // Check for 0 divisor (result is +/-Infinity).
1430 NearLabel positive_divisor;
1431 testq(src2, src2);
1432 j(zero, on_not_smi_result);
1433
1434 if (src1.is(rax)) {
1435 movq(kScratchRegister, src1);
1436 }
1437 SmiToInteger32(rax, src1);
1438 // We need to rule out dividing Smi::kMinValue by -1, since that would
1439 // overflow in idiv and raise an exception.
1440 // We combine this with negative zero test (negative zero only happens
1441 // when dividing zero by a negative number).
1442
1443 // We overshoot a little and go to slow case if we divide min-value
1444 // by any negative value, not just -1.
1445 NearLabel safe_div;
1446 testl(rax, Immediate(0x7fffffff));
1447 j(not_zero, &safe_div);
1448 testq(src2, src2);
1449 if (src1.is(rax)) {
1450 j(positive, &safe_div);
1451 movq(src1, kScratchRegister);
1452 jmp(on_not_smi_result);
1453 } else {
1454 j(negative, on_not_smi_result);
1455 }
1456 bind(&safe_div);
1457
1458 SmiToInteger32(src2, src2);
1459 // Sign extend src1 into edx:eax.
1460 cdq();
1461 idivl(src2);
1462 Integer32ToSmi(src2, src2);
1463 // Check that the remainder is zero.
1464 testl(rdx, rdx);
1465 if (src1.is(rax)) {
1466 NearLabel smi_result;
1467 j(zero, &smi_result);
1468 movq(src1, kScratchRegister);
1469 jmp(on_not_smi_result);
1470 bind(&smi_result);
1471 } else {
1472 j(not_zero, on_not_smi_result);
1473 }
1474 if (!dst.is(src1) && src1.is(rax)) {
1475 movq(src1, kScratchRegister);
1476 }
1477 Integer32ToSmi(dst, rax);
1478}
1479
1480
1481template <typename LabelType>
1482void MacroAssembler::SmiMod(Register dst,
1483 Register src1,
1484 Register src2,
1485 LabelType* on_not_smi_result) {
1486 ASSERT(!dst.is(kScratchRegister));
1487 ASSERT(!src1.is(kScratchRegister));
1488 ASSERT(!src2.is(kScratchRegister));
1489 ASSERT(!src2.is(rax));
1490 ASSERT(!src2.is(rdx));
1491 ASSERT(!src1.is(rdx));
1492 ASSERT(!src1.is(src2));
1493
1494 testq(src2, src2);
1495 j(zero, on_not_smi_result);
1496
1497 if (src1.is(rax)) {
1498 movq(kScratchRegister, src1);
1499 }
1500 SmiToInteger32(rax, src1);
1501 SmiToInteger32(src2, src2);
1502
1503 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1504 NearLabel safe_div;
1505 cmpl(rax, Immediate(Smi::kMinValue));
1506 j(not_equal, &safe_div);
1507 cmpl(src2, Immediate(-1));
1508 j(not_equal, &safe_div);
1509 // Retag inputs and go slow case.
1510 Integer32ToSmi(src2, src2);
1511 if (src1.is(rax)) {
1512 movq(src1, kScratchRegister);
1513 }
1514 jmp(on_not_smi_result);
1515 bind(&safe_div);
1516
1517 // Sign extend eax into edx:eax.
1518 cdq();
1519 idivl(src2);
1520 // Restore smi tags on inputs.
1521 Integer32ToSmi(src2, src2);
1522 if (src1.is(rax)) {
1523 movq(src1, kScratchRegister);
1524 }
1525 // Check for a negative zero result. If the result is zero, and the
1526 // dividend is negative, go slow to return a floating point negative zero.
1527 NearLabel smi_result;
1528 testl(rdx, rdx);
1529 j(not_zero, &smi_result);
1530 testq(src1, src1);
1531 j(negative, on_not_smi_result);
1532 bind(&smi_result);
1533 Integer32ToSmi(dst, rdx);
1534}
1535
1536
1537template <typename LabelType>
1538void MacroAssembler::SmiShiftLogicalRightConstant(
1539 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1540 // Logic right shift interprets its result as an *unsigned* number.
1541 if (dst.is(src)) {
1542 UNIMPLEMENTED(); // Not used.
1543 } else {
1544 movq(dst, src);
1545 if (shift_value == 0) {
1546 testq(dst, dst);
1547 j(negative, on_not_smi_result);
1548 }
1549 shr(dst, Immediate(shift_value + kSmiShift));
1550 shl(dst, Immediate(kSmiShift));
1551 }
1552}
1553
1554
1555template <typename LabelType>
1556void MacroAssembler::SmiShiftLogicalRight(Register dst,
1557 Register src1,
1558 Register src2,
1559 LabelType* on_not_smi_result) {
1560 ASSERT(!dst.is(kScratchRegister));
1561 ASSERT(!src1.is(kScratchRegister));
1562 ASSERT(!src2.is(kScratchRegister));
1563 ASSERT(!dst.is(rcx));
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001564 // dst and src1 can be the same, because the one case that bails out
1565 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001566 NearLabel result_ok;
1567 if (src1.is(rcx) || src2.is(rcx)) {
1568 movq(kScratchRegister, rcx);
1569 }
1570 if (!dst.is(src1)) {
1571 movq(dst, src1);
1572 }
1573 SmiToInteger32(rcx, src2);
1574 orl(rcx, Immediate(kSmiShift));
1575 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1576 shl(dst, Immediate(kSmiShift));
1577 testq(dst, dst);
1578 if (src1.is(rcx) || src2.is(rcx)) {
1579 NearLabel positive_result;
1580 j(positive, &positive_result);
1581 if (src1.is(rcx)) {
1582 movq(src1, kScratchRegister);
1583 } else {
1584 movq(src2, kScratchRegister);
1585 }
1586 jmp(on_not_smi_result);
1587 bind(&positive_result);
1588 } else {
1589 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1590 }
1591}
1592
1593
1594template <typename LabelType>
1595void MacroAssembler::SelectNonSmi(Register dst,
1596 Register src1,
1597 Register src2,
1598 LabelType* on_not_smis) {
1599 ASSERT(!dst.is(kScratchRegister));
1600 ASSERT(!src1.is(kScratchRegister));
1601 ASSERT(!src2.is(kScratchRegister));
1602 ASSERT(!dst.is(src1));
1603 ASSERT(!dst.is(src2));
1604 // Both operands must not be smis.
1605#ifdef DEBUG
1606 if (allow_stub_calls()) { // Check contains a stub call.
1607 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1608 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1609 }
1610#endif
1611 ASSERT_EQ(0, kSmiTag);
1612 ASSERT_EQ(0, Smi::FromInt(0));
1613 movl(kScratchRegister, Immediate(kSmiTagMask));
1614 and_(kScratchRegister, src1);
1615 testl(kScratchRegister, src2);
1616 // If non-zero then both are smis.
1617 j(not_zero, on_not_smis);
1618
1619 // Exactly one operand is a smi.
1620 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1621 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1622 subq(kScratchRegister, Immediate(1));
1623 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1624 movq(dst, src1);
1625 xor_(dst, src2);
1626 and_(dst, kScratchRegister);
1627 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1628 xor_(dst, src1);
1629 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1630}
1631
1632
1633template <typename LabelType>
1634void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1635 ASSERT_EQ(0, kSmiTag);
1636 Condition smi = CheckSmi(src);
1637 j(smi, on_smi);
1638}
1639
1640
1641template <typename LabelType>
1642void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1643 Condition smi = CheckSmi(src);
1644 j(NegateCondition(smi), on_not_smi);
1645}
1646
1647
1648template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001649void MacroAssembler::JumpUnlessNonNegativeSmi(
1650 Register src, LabelType* on_not_smi_or_negative) {
1651 Condition non_negative_smi = CheckNonNegativeSmi(src);
1652 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001653}
1654
1655
1656template <typename LabelType>
1657void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1658 Smi* constant,
1659 LabelType* on_equals) {
1660 SmiCompare(src, constant);
1661 j(equal, on_equals);
1662}
1663
1664
1665template <typename LabelType>
1666void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1667 LabelType* on_invalid) {
1668 Condition is_valid = CheckInteger32ValidSmiValue(src);
1669 j(NegateCondition(is_valid), on_invalid);
1670}
1671
1672
1673template <typename LabelType>
1674void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1675 LabelType* on_invalid) {
1676 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1677 j(NegateCondition(is_valid), on_invalid);
1678}
1679
1680
1681template <typename LabelType>
1682void MacroAssembler::JumpIfNotBothSmi(Register src1,
1683 Register src2,
1684 LabelType* on_not_both_smi) {
1685 Condition both_smi = CheckBothSmi(src1, src2);
1686 j(NegateCondition(both_smi), on_not_both_smi);
1687}
1688
1689
1690template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001691void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1692 Register src2,
1693 LabelType* on_not_both_smi) {
1694 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001695 j(NegateCondition(both_smi), on_not_both_smi);
1696}
1697
1698
1699template <typename LabelType>
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001700void MacroAssembler::JumpIfNotString(Register object,
1701 Register object_map,
1702 LabelType* not_string) {
1703 Condition is_smi = CheckSmi(object);
1704 j(is_smi, not_string);
1705 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1706 j(above_equal, not_string);
1707}
1708
1709
1710template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001711void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1712 Register second_object,
1713 Register scratch1,
1714 Register scratch2,
1715 LabelType* on_fail) {
1716 // Check that both objects are not smis.
1717 Condition either_smi = CheckEitherSmi(first_object, second_object);
1718 j(either_smi, on_fail);
1719
1720 // Load instance type for both strings.
1721 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1722 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1723 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1724 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1725
1726 // Check that both are flat ascii strings.
1727 ASSERT(kNotStringTag != 0);
1728 const int kFlatAsciiStringMask =
1729 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1730 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1731
1732 andl(scratch1, Immediate(kFlatAsciiStringMask));
1733 andl(scratch2, Immediate(kFlatAsciiStringMask));
1734 // Interleave the bits to check both scratch1 and scratch2 in one test.
1735 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1736 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1737 cmpl(scratch1,
1738 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1739 j(not_equal, on_fail);
1740}
1741
1742
1743template <typename LabelType>
1744void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1745 Register instance_type,
1746 Register scratch,
1747 LabelType *failure) {
1748 if (!scratch.is(instance_type)) {
1749 movl(scratch, instance_type);
1750 }
1751
1752 const int kFlatAsciiStringMask =
1753 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1754
1755 andl(scratch, Immediate(kFlatAsciiStringMask));
1756 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1757 j(not_equal, failure);
1758}
1759
1760
1761template <typename LabelType>
1762void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1763 Register first_object_instance_type,
1764 Register second_object_instance_type,
1765 Register scratch1,
1766 Register scratch2,
1767 LabelType* on_fail) {
1768 // Load instance type for both strings.
1769 movq(scratch1, first_object_instance_type);
1770 movq(scratch2, second_object_instance_type);
1771
1772 // Check that both are flat ascii strings.
1773 ASSERT(kNotStringTag != 0);
1774 const int kFlatAsciiStringMask =
1775 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1776 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1777
1778 andl(scratch1, Immediate(kFlatAsciiStringMask));
1779 andl(scratch2, Immediate(kFlatAsciiStringMask));
1780 // Interleave the bits to check both scratch1 and scratch2 in one test.
1781 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1782 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1783 cmpl(scratch1,
1784 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1785 j(not_equal, on_fail);
1786}
1787
1788
1789template <typename LabelType>
1790void MacroAssembler::InNewSpace(Register object,
1791 Register scratch,
1792 Condition cc,
1793 LabelType* branch) {
1794 if (Serializer::enabled()) {
1795 // Can't do arithmetic on external references if it might get serialized.
1796 // The mask isn't really an address. We load it as an external reference in
1797 // case the size of the new space is different between the snapshot maker
1798 // and the running system.
1799 if (scratch.is(object)) {
1800 movq(kScratchRegister, ExternalReference::new_space_mask());
1801 and_(scratch, kScratchRegister);
1802 } else {
1803 movq(scratch, ExternalReference::new_space_mask());
1804 and_(scratch, object);
1805 }
1806 movq(kScratchRegister, ExternalReference::new_space_start());
1807 cmpq(scratch, kScratchRegister);
1808 j(cc, branch);
1809 } else {
1810 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1811 intptr_t new_space_start =
1812 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1813 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1814 if (scratch.is(object)) {
1815 addq(scratch, kScratchRegister);
1816 } else {
1817 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1818 }
1819 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1820 j(cc, branch);
1821 }
1822}
1823
1824
1825template <typename LabelType>
1826void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1827 const ParameterCount& actual,
1828 Handle<Code> code_constant,
1829 Register code_register,
1830 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001831 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001832 CallWrapper* call_wrapper) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001833 bool definitely_matches = false;
1834 NearLabel invoke;
1835 if (expected.is_immediate()) {
1836 ASSERT(actual.is_immediate());
1837 if (expected.immediate() == actual.immediate()) {
1838 definitely_matches = true;
1839 } else {
1840 Set(rax, actual.immediate());
1841 if (expected.immediate() ==
1842 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1843 // Don't worry about adapting arguments for built-ins that
1844 // don't want that done. Skip adaption code by making it look
1845 // like we have a match between expected and actual number of
1846 // arguments.
1847 definitely_matches = true;
1848 } else {
1849 Set(rbx, expected.immediate());
1850 }
1851 }
1852 } else {
1853 if (actual.is_immediate()) {
1854 // Expected is in register, actual is immediate. This is the
1855 // case when we invoke function values without going through the
1856 // IC mechanism.
1857 cmpq(expected.reg(), Immediate(actual.immediate()));
1858 j(equal, &invoke);
1859 ASSERT(expected.reg().is(rbx));
1860 Set(rax, actual.immediate());
1861 } else if (!expected.reg().is(actual.reg())) {
1862 // Both expected and actual are in (different) registers. This
1863 // is the case when we invoke functions using call and apply.
1864 cmpq(expected.reg(), actual.reg());
1865 j(equal, &invoke);
1866 ASSERT(actual.reg().is(rax));
1867 ASSERT(expected.reg().is(rbx));
1868 }
1869 }
1870
1871 if (!definitely_matches) {
1872 Handle<Code> adaptor =
1873 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1874 if (!code_constant.is_null()) {
1875 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1876 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1877 } else if (!code_register.is(rdx)) {
1878 movq(rdx, code_register);
1879 }
1880
1881 if (flag == CALL_FUNCTION) {
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001882 if (call_wrapper != NULL) call_wrapper->BeforeCall(CallSize(adaptor));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001883 Call(adaptor, RelocInfo::CODE_TARGET);
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001884 if (call_wrapper != NULL) call_wrapper->AfterCall();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001885 jmp(done);
1886 } else {
1887 Jump(adaptor, RelocInfo::CODE_TARGET);
1888 }
1889 bind(&invoke);
1890 }
1891}
1892
ager@chromium.org9085a012009-05-11 19:22:57 +00001893
1894} } // namespace v8::internal
1895
1896#endif // V8_X64_MACRO_ASSEMBLER_X64_H_