blob: 8352518323d44d9f872a02a5a4e55cd1d0dc2ce3 [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.org9085a012009-05-11 19:22:57 +000028#ifndef V8_X64_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;
ager@chromium.orge2902be2009-06-08 12:21:35 +000055
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000056// Convenience for platform-independent signatures.
57typedef Operand MemOperand;
58
ager@chromium.org9085a012009-05-11 19:22:57 +000059// Forward declaration.
60class JumpTarget;
61
ager@chromium.org4af710e2009-09-15 12:20:11 +000062struct SmiIndex {
63 SmiIndex(Register index_register, ScaleFactor scale)
64 : reg(index_register),
65 scale(scale) {}
66 Register reg;
67 ScaleFactor scale;
68};
ager@chromium.org9085a012009-05-11 19:22:57 +000069
ager@chromium.org9085a012009-05-11 19:22:57 +000070// MacroAssembler implements a collection of frequently used macros.
71class MacroAssembler: public Assembler {
72 public:
73 MacroAssembler(void* buffer, int size);
74
ager@chromium.org18ad94b2009-09-02 08:22:29 +000075 void LoadRoot(Register destination, Heap::RootListIndex index);
76 void CompareRoot(Register with, Heap::RootListIndex index);
ricow@chromium.org83aa5492011-02-07 12:42:56 +000077 void CompareRoot(const Operand& with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000078 void PushRoot(Heap::RootListIndex index);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000079 void StoreRoot(Register source, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000080
ager@chromium.org9085a012009-05-11 19:22:57 +000081 // ---------------------------------------------------------------------------
82 // GC Support
83
ricow@chromium.org30ce4112010-05-31 10:38:25 +000084 // For page containing |object| mark region covering |addr| dirty.
85 // RecordWriteHelper only works if the object is not in new
ager@chromium.orgac091b72010-05-05 07:34:42 +000086 // space.
87 void RecordWriteHelper(Register object,
88 Register addr,
89 Register scratch);
90
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000091 // Check if object is in new space. The condition cc can be equal or
92 // not_equal. If it is equal a jump will be done if the object is on new
93 // space. The register scratch can be object itself, but it will be clobbered.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000094 template <typename LabelType>
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000095 void InNewSpace(Register object,
96 Register scratch,
97 Condition cc,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000098 LabelType* branch);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000099
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000100 // For page containing |object| mark region covering [object+offset]
101 // dirty. |object| is the object being stored into, |value| is the
102 // object being stored. If |offset| is zero, then the |scratch|
103 // register contains the array index into the elements array
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000104 // represented as an untagged 32-bit integer. All registers are
105 // clobbered by the operation. RecordWrite filters out smis so it
106 // does not update the write barrier if the value is a smi.
ager@chromium.org9085a012009-05-11 19:22:57 +0000107 void RecordWrite(Register object,
108 int offset,
109 Register value,
110 Register scratch);
111
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000112 // For page containing |object| mark region covering [address]
113 // dirty. |object| is the object being stored into, |value| is the
114 // object being stored. All registers are clobbered by the
115 // operation. RecordWrite filters out smis so it does not update
116 // the write barrier if the value is a smi.
117 void RecordWrite(Register object,
118 Register address,
119 Register value);
120
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000121 // For page containing |object| mark region covering [object+offset] dirty.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000122 // The value is known to not be a smi.
123 // object is the object being stored into, value is the object being stored.
124 // If offset is zero, then the scratch register contains the array index into
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000125 // the elements array represented as an untagged 32-bit integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000126 // All registers are clobbered by the operation.
127 void RecordWriteNonSmi(Register object,
128 int offset,
129 Register value,
130 Register scratch);
131
ager@chromium.org9085a012009-05-11 19:22:57 +0000132#ifdef ENABLE_DEBUGGER_SUPPORT
133 // ---------------------------------------------------------------------------
134 // Debugger Support
135
ager@chromium.org5c838252010-02-19 08:53:10 +0000136 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000137#endif
138
139 // ---------------------------------------------------------------------------
140 // Activation frames
141
142 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
143 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
144
145 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
146 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
147
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000148 // Enter specific kind of exit frame; either in normal or
149 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000150 // sets up the number of arguments in register rdi and the pointer
151 // to the first argument in register rsi.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000152 //
153 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
154 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000155 void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000156
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000157 // Enter specific kind of exit frame. Allocates arg_stack_space * kPointerSize
158 // memory (not GCed) on the stack accessible via StackSpaceOperand.
159 void EnterApiExitFrame(int arg_stack_space);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000160
ager@chromium.orga1645e22009-09-09 19:27:10 +0000161 // Leave the current exit frame. Expects/provides the return value in
162 // register rax:rdx (untouched) and the pointer to the first
163 // argument in register rsi.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000164 void LeaveExitFrame(bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000165
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000166 // Leave the current exit frame. Expects/provides the return value in
167 // register rax (untouched).
168 void LeaveApiExitFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +0000169
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000170 // Push and pop the registers that can hold pointers.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000171 void PushSafepointRegisters() { Pushad(); }
172 void PopSafepointRegisters() { Popad(); }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000173 static int SafepointRegisterStackIndex(int reg_code) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000174 return kNumSafepointRegisters - 1 -
175 kSafepointPushRegisterIndices[reg_code];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000176 }
177
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000178
ager@chromium.org9085a012009-05-11 19:22:57 +0000179 // ---------------------------------------------------------------------------
180 // JavaScript invokes
181
182 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000183 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000184 const ParameterCount& expected,
185 const ParameterCount& actual,
186 InvokeFlag flag);
187
188 void InvokeCode(Handle<Code> code,
189 const ParameterCount& expected,
190 const ParameterCount& actual,
191 RelocInfo::Mode rmode,
192 InvokeFlag flag);
193
194 // Invoke the JavaScript function in the given register. Changes the
195 // current context to the context in the function before invoking.
196 void InvokeFunction(Register function,
197 const ParameterCount& actual,
198 InvokeFlag flag);
199
ager@chromium.org5c838252010-02-19 08:53:10 +0000200 void InvokeFunction(JSFunction* function,
201 const ParameterCount& actual,
202 InvokeFlag flag);
203
ager@chromium.org9085a012009-05-11 19:22:57 +0000204 // Invoke specified builtin JavaScript function. Adds an entry to
205 // the unresolved list if the name does not resolve.
206 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
207
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000208 // Store the function for the given builtin in the target register.
209 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
210
ager@chromium.org9085a012009-05-11 19:22:57 +0000211 // Store the code object for the given builtin in the target register.
212 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
213
ager@chromium.org4af710e2009-09-15 12:20:11 +0000214
215 // ---------------------------------------------------------------------------
216 // Smi tagging, untagging and operations on tagged smis.
217
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000218 void InitializeSmiConstantRegister() {
219 movq(kSmiConstantRegister,
220 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
221 RelocInfo::NONE);
222 }
223
ager@chromium.org4af710e2009-09-15 12:20:11 +0000224 // Conversions between tagged smi values and non-tagged integer values.
225
226 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000227 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000228 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000229 void Integer32ToSmi(Register dst, Register src);
230
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000231 // Stores an integer32 value into a memory field that already holds a smi.
232 void Integer32ToSmiField(const Operand& dst, Register src);
233
ager@chromium.org4af710e2009-09-15 12:20:11 +0000234 // Adds constant to src and tags the result as a smi.
235 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000236 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000237
238 // Convert smi to 32-bit integer. I.e., not sign extended into
239 // high 32 bits of destination.
240 void SmiToInteger32(Register dst, Register src);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000241 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000242
243 // Convert smi to 64-bit integer (sign extended if necessary).
244 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000245 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000246
247 // Multiply a positive smi's integer value by a power of two.
248 // Provides result as 64-bit integer value.
249 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
250 Register src,
251 int power);
252
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000253 // Divide a positive smi's integer value by a power of two.
254 // Provides result as 32-bit integer value.
255 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
256 Register src,
257 int power);
258
259
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000260 // Simple comparison of smis.
261 void SmiCompare(Register dst, Register src);
262 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000263 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000264 void SmiCompare(const Operand& dst, Register src);
265 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000266 // Compare the int32 in src register to the value of the smi stored at dst.
267 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000268 // Sets sign and zero flags depending on value of smi in register.
269 void SmiTest(Register src);
270
ager@chromium.org4af710e2009-09-15 12:20:11 +0000271 // Functions performing a check on a known or potential smi. Returns
272 // a condition that is satisfied if the check is successful.
273
274 // Is the value a tagged smi.
275 Condition CheckSmi(Register src);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000276 Condition CheckSmi(const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000277
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000278 // Is the value a non-negative tagged smi.
279 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000280
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000281 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000282 Condition CheckBothSmi(Register first, Register second);
283
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000284 // Are both values non-negative tagged smis.
285 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000286
287 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000288 Condition CheckEitherSmi(Register first,
289 Register second,
290 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000291
ager@chromium.org4af710e2009-09-15 12:20:11 +0000292 // Is the value the minimum smi value (since we are using
293 // two's complement numbers, negating the value is known to yield
294 // a non-smi value).
295 Condition CheckIsMinSmi(Register src);
296
ager@chromium.org4af710e2009-09-15 12:20:11 +0000297 // Checks whether an 32-bit integer value is a valid for conversion
298 // to a smi.
299 Condition CheckInteger32ValidSmiValue(Register src);
300
ager@chromium.org3811b432009-10-28 14:53:37 +0000301 // Checks whether an 32-bit unsigned integer value is a valid for
302 // conversion to a smi.
303 Condition CheckUInteger32ValidSmiValue(Register src);
304
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000305 // Check whether src is a Smi, and set dst to zero if it is a smi,
306 // and to one if it isn't.
307 void CheckSmiToIndicator(Register dst, Register src);
308 void CheckSmiToIndicator(Register dst, const Operand& src);
309
ager@chromium.org4af710e2009-09-15 12:20:11 +0000310 // Test-and-jump functions. Typically combines a check function
311 // above with a conditional jump.
312
313 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000314 template <typename LabelType>
315 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000316
ager@chromium.org3811b432009-10-28 14:53:37 +0000317 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000318 template <typename LabelType>
319 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000320
ager@chromium.org4af710e2009-09-15 12:20:11 +0000321 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000322 template <typename LabelType>
323 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000324
325 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000326 template <typename LabelType>
327 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000328
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000329 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000330 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000331 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000332
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000333 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000334 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000335 template <typename LabelType>
336 void JumpIfSmiEqualsConstant(Register src,
337 Smi* constant,
338 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000339
ager@chromium.org4af710e2009-09-15 12:20:11 +0000340 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000341 template <typename LabelType>
342 void JumpIfNotBothSmi(Register src1,
343 Register src2,
344 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000345
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000346 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000347 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000348 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
349 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000350
ager@chromium.org4af710e2009-09-15 12:20:11 +0000351 // Operations on tagged smi values.
352
353 // Smis represent a subset of integers. The subset is always equivalent to
354 // a two's complement interpretation of a fixed number of bits.
355
356 // Optimistically adds an integer constant to a supposed smi.
357 // If the src is not a smi, or the result is not a smi, jump to
358 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000359 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000360 void SmiTryAddConstant(Register dst,
361 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000362 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000363 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000364
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000365 // Add an integer constant to a tagged smi, giving a tagged smi as result.
366 // No overflow testing on the result is done.
367 void SmiAddConstant(Register dst, Register src, Smi* constant);
368
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000369 // Add an integer constant to a tagged smi, giving a tagged smi as result.
370 // No overflow testing on the result is done.
371 void SmiAddConstant(const Operand& dst, Smi* constant);
372
ager@chromium.org4af710e2009-09-15 12:20:11 +0000373 // Add an integer constant to a tagged smi, giving a tagged smi as result,
374 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000375 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000376 void SmiAddConstant(Register dst,
377 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000378 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000379 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000380
381 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000382 // result. No testing on the result is done. Sets the N and Z flags
383 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000384 void SmiSubConstant(Register dst, Register src, Smi* constant);
385
386 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000387 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000388 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000389 void SmiSubConstant(Register dst,
390 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000391 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000392 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000393
394 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000395 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000396 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000397 void SmiNeg(Register dst,
398 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000399 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000400
401 // Adds smi values and return the result as a smi.
402 // If dst is src1, then src1 will be destroyed, even if
403 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000404 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000405 void SmiAdd(Register dst,
406 Register src1,
407 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000408 LabelType* on_not_smi_result);
409
410 void SmiAdd(Register dst,
411 Register src1,
412 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000413
414 // Subtracts smi values and return the result as a smi.
415 // If dst is src1, then src1 will be destroyed, even if
416 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000417 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000418 void SmiSub(Register dst,
419 Register src1,
420 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000421 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000422
ager@chromium.orgac091b72010-05-05 07:34:42 +0000423 void SmiSub(Register dst,
424 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000425 Register src2);
426
427 template <typename LabelType>
428 void SmiSub(Register dst,
429 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000430 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000431 LabelType* on_not_smi_result);
432
433 void SmiSub(Register dst,
434 Register src1,
435 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000436
ager@chromium.org4af710e2009-09-15 12:20:11 +0000437 // Multiplies smi values and return the result as a smi,
438 // if possible.
439 // If dst is src1, then src1 will be destroyed, even if
440 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000441 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000442 void SmiMul(Register dst,
443 Register src1,
444 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000445 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000446
447 // Divides one smi by another and returns the quotient.
448 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000449 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000450 void SmiDiv(Register dst,
451 Register src1,
452 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000453 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000454
455 // Divides one smi by another and returns the remainder.
456 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000457 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000458 void SmiMod(Register dst,
459 Register src1,
460 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000461 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000462
463 // Bitwise operations.
464 void SmiNot(Register dst, Register src);
465 void SmiAnd(Register dst, Register src1, Register src2);
466 void SmiOr(Register dst, Register src1, Register src2);
467 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000468 void SmiAndConstant(Register dst, Register src1, Smi* constant);
469 void SmiOrConstant(Register dst, Register src1, Smi* constant);
470 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000471
472 void SmiShiftLeftConstant(Register dst,
473 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000474 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000475 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000476 void SmiShiftLogicalRightConstant(Register dst,
477 Register src,
478 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000479 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000480 void SmiShiftArithmeticRightConstant(Register dst,
481 Register src,
482 int shift_value);
483
484 // Shifts a smi value to the left, and returns the result if that is a smi.
485 // Uses and clobbers rcx, so dst may not be rcx.
486 void SmiShiftLeft(Register dst,
487 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000488 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000489 // Shifts a smi value to the right, shifting in zero bits at the top, and
490 // returns the unsigned intepretation of the result if that is a smi.
491 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000492 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000493 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000494 Register src1,
495 Register src2,
496 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000497 // Shifts a smi value to the right, sign extending the top, and
498 // returns the signed intepretation of the result. That will always
499 // be a valid smi value, since it's numerically smaller than the
500 // original.
501 // Uses and clobbers rcx, so dst may not be rcx.
502 void SmiShiftArithmeticRight(Register dst,
503 Register src1,
504 Register src2);
505
506 // Specialized operations
507
508 // Select the non-smi register of two registers where exactly one is a
509 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000510 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000511 void SelectNonSmi(Register dst,
512 Register src1,
513 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000514 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000515
516 // Converts, if necessary, a smi to a combination of number and
517 // multiplier to be used as a scaled index.
518 // The src register contains a *positive* smi value. The shift is the
519 // power of two to multiply the index value by (e.g.
520 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
521 // The returned index register may be either src or dst, depending
522 // on what is most efficient. If src and dst are different registers,
523 // src is always unchanged.
524 SmiIndex SmiToIndex(Register dst, Register src, int shift);
525
526 // Converts a positive smi to a negative index.
527 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
528
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000529 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000530 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000531 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000532 }
533
534 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000535 Register constant = GetSmiConstant(source);
536 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000537 }
538
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000539 void Push(Smi* smi);
540 void Test(const Operand& dst, Smi* source);
541
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000542 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000543 // String macros.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000544
545 // If object is a string, its map is loaded into object_map.
546 template <typename LabelType>
547 void JumpIfNotString(Register object,
548 Register object_map,
549 LabelType* not_string);
550
551
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000552 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000553 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
554 Register second_object,
555 Register scratch1,
556 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000557 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000558
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000559 // Check whether the instance type represents a flat ascii string. Jump to the
560 // label if not. If the instance type can be scratched specify same register
561 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000562 template <typename LabelType>
563 void JumpIfInstanceTypeIsNotSequentialAscii(
564 Register instance_type,
565 Register scratch,
566 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000567
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000568 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000569 void JumpIfBothInstanceTypesAreNotSequentialAscii(
570 Register first_object_instance_type,
571 Register second_object_instance_type,
572 Register scratch1,
573 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000574 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000575
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000576 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000577 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000578
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000579 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000580 void Set(Register dst, int64_t x);
581 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000582
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000583 // Move if the registers are not identical.
584 void Move(Register target, Register source);
585
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000586 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000587 void Move(Register dst, Handle<Object> source);
588 void Move(const Operand& dst, Handle<Object> source);
589 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000590 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000591 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000592
593 // Emit code to discard a non-negative number of pointer-sized elements
594 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000595 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000596
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000597 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000598
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000599 // Control Flow
600 void Jump(Address destination, RelocInfo::Mode rmode);
601 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000602 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
603
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000604 void Call(Address destination, RelocInfo::Mode rmode);
605 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000606 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000607
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000608 // Emit call to the code we are currently generating.
609 void CallSelf() {
610 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
611 Call(self, RelocInfo::CODE_TARGET);
612 }
613
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000614 // Non-x64 instructions.
615 // Push/pop all general purpose registers.
616 // Does not push rsp/rbp nor any of the assembler's special purpose registers
617 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
618 void Pushad();
619 void Popad();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000620 // Sets the stack as after performing Popad, without actually loading the
621 // registers.
622 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000623
ager@chromium.org9085a012009-05-11 19:22:57 +0000624 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000625 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000626 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000627 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000628 void CmpObjectType(Register heap_object, InstanceType type, Register map);
629
630 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000631 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000632 void CmpInstanceType(Register map, InstanceType type);
633
ager@chromium.org5c838252010-02-19 08:53:10 +0000634 // Check if the map of an object is equal to a specified map and
635 // branch to label if not. Skip the smi check if not required
636 // (object is known to be a heap object)
637 void CheckMap(Register obj,
638 Handle<Map> map,
639 Label* fail,
640 bool is_heap_object);
641
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000642 // Check if the object in register heap_object is a string. Afterwards the
643 // register map contains the object map and the register instance_type
644 // contains the instance_type. The registers map and instance_type can be the
645 // same in which case it contains the instance type afterwards. Either of the
646 // registers map and instance_type can be the same as heap_object.
647 Condition IsObjectStringType(Register heap_object,
648 Register map,
649 Register instance_type);
650
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000651 // FCmp compares and pops the two values on top of the FPU stack.
652 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000653 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
654 void FCmp();
655
ager@chromium.org5c838252010-02-19 08:53:10 +0000656 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000657 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000658
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000659 // Abort execution if argument is a smi. Used in debug code.
660 void AbortIfSmi(Register object);
661
lrn@chromium.org25156de2010-04-06 13:10:27 +0000662 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000663 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000664
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000665 // Abort execution if argument is a string. Used in debug code.
666 void AbortIfNotString(Register object);
667
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000668 // Abort execution if argument is not the root value with the given index.
669 void AbortIfNotRootValue(Register src,
670 Heap::RootListIndex root_value_index,
671 const char* message);
672
ager@chromium.org9085a012009-05-11 19:22:57 +0000673 // ---------------------------------------------------------------------------
674 // Exception handling
675
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000676 // Push a new try handler and link into try handler chain. The return
677 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000678 void PushTryHandler(CodeLocation try_location, HandlerType type);
679
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000680 // Unlink the stack handler on top of the stack from the try handler chain.
681 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000682
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000683 // Activate the top handler in the try hander chain and pass the
684 // thrown value.
685 void Throw(Register value);
686
687 // Propagate an uncatchable exception out of the current JS stack.
688 void ThrowUncatchable(UncatchableExceptionType type, Register value);
689
ager@chromium.org9085a012009-05-11 19:22:57 +0000690 // ---------------------------------------------------------------------------
691 // Inline caching support
692
ager@chromium.org9085a012009-05-11 19:22:57 +0000693 // Generate code for checking access rights - used for security checks
694 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000695 // is left untouched, but the scratch register and kScratchRegister,
696 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000697 void CheckAccessGlobalProxy(Register holder_reg,
698 Register scratch,
699 Label* miss);
700
701
702 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000703 // Allocation support
704
705 // Allocate an object in new space. If the new space is exhausted control
706 // continues at the gc_required label. The allocated object is returned in
707 // result and end of the new object is returned in result_end. The register
708 // scratch can be passed as no_reg in which case an additional object
709 // reference will be added to the reloc info. The returned pointers in result
710 // and result_end have not yet been tagged as heap objects. If
711 // result_contains_top_on_entry is true the content of result is known to be
712 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000713 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000714 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000715 void AllocateInNewSpace(int object_size,
716 Register result,
717 Register result_end,
718 Register scratch,
719 Label* gc_required,
720 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000721
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000722 void AllocateInNewSpace(int header_size,
723 ScaleFactor element_size,
724 Register element_count,
725 Register result,
726 Register result_end,
727 Register scratch,
728 Label* gc_required,
729 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000730
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000731 void AllocateInNewSpace(Register object_size,
732 Register result,
733 Register result_end,
734 Register scratch,
735 Label* gc_required,
736 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000737
738 // Undo allocation in new space. The object passed and objects allocated after
739 // it will no longer be allocated. Make sure that no pointers are left to the
740 // object(s) no longer allocated as they would be invalid when allocation is
741 // un-done.
742 void UndoAllocationInNewSpace(Register object);
743
ager@chromium.org3811b432009-10-28 14:53:37 +0000744 // Allocate a heap number in new space with undefined value. Returns
745 // tagged pointer in result register, or jumps to gc_required if new
746 // space is full.
747 void AllocateHeapNumber(Register result,
748 Register scratch,
749 Label* gc_required);
750
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000751 // Allocate a sequential string. All the header fields of the string object
752 // are initialized.
753 void AllocateTwoByteString(Register result,
754 Register length,
755 Register scratch1,
756 Register scratch2,
757 Register scratch3,
758 Label* gc_required);
759 void AllocateAsciiString(Register result,
760 Register length,
761 Register scratch1,
762 Register scratch2,
763 Register scratch3,
764 Label* gc_required);
765
766 // Allocate a raw cons string object. Only the map field of the result is
767 // initialized.
768 void AllocateConsString(Register result,
769 Register scratch1,
770 Register scratch2,
771 Label* gc_required);
772 void AllocateAsciiConsString(Register result,
773 Register scratch1,
774 Register scratch2,
775 Label* gc_required);
776
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000777 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000778 // Support functions.
779
780 // Check if result is zero and op is negative.
781 void NegativeZeroTest(Register result, Register op, Label* then_label);
782
783 // Check if result is zero and op is negative in code using jump targets.
784 void NegativeZeroTest(CodeGenerator* cgen,
785 Register result,
786 Register op,
787 JumpTarget* then_target);
788
789 // Check if result is zero and any of op1 and op2 are negative.
790 // Register scratch is destroyed, and it must be different from op2.
791 void NegativeZeroTest(Register result, Register op1, Register op2,
792 Register scratch, Label* then_label);
793
794 // Try to get function prototype of a function and puts the value in
795 // the result register. Checks that the function really is a
796 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000797 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000798 // clobbered.
799 void TryGetFunctionPrototype(Register function,
800 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000801 Label* miss);
802
803 // Generates code for reporting that an illegal operation has
804 // occurred.
805 void IllegalOperation(int num_arguments);
806
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000807 // Picks out an array index from the hash field.
808 // Register use:
809 // hash - holds the index's hash. Clobbered.
810 // index - holds the overwritten index on exit.
811 void IndexFromHash(Register hash, Register index);
812
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000813 // Find the function context up the context chain.
814 void LoadContext(Register dst, int context_chain_length);
815
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000816 // Load the global function with the given index.
817 void LoadGlobalFunction(int index, Register function);
818
819 // Load the initial map from the global function. The registers
820 // function and map can be the same.
821 void LoadGlobalFunctionInitialMap(Register function, Register map);
822
ager@chromium.org9085a012009-05-11 19:22:57 +0000823 // ---------------------------------------------------------------------------
824 // Runtime calls
825
826 // Call a code stub.
827 void CallStub(CodeStub* stub);
828
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000829 // Call a code stub and return the code object called. Try to generate
830 // the code if necessary. Do not perform a GC but instead return a retry
831 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000832 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000833
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000834 // Tail call a code stub (jump).
835 void TailCallStub(CodeStub* stub);
836
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000837 // Tail call a code stub (jump) and return the code object called. Try to
838 // generate the code if necessary. Do not perform a GC but instead return
839 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000840 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000841
ager@chromium.org9085a012009-05-11 19:22:57 +0000842 // Return from a code stub after popping its arguments.
843 void StubReturn(int argc);
844
845 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000846 void CallRuntime(Runtime::Function* f, int num_arguments);
847
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000848 // Call a runtime function and save the value of XMM registers.
849 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
850
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000851 // Call a runtime function, returning the CodeStub object called.
852 // Try to generate the stub code if necessary. Do not perform a GC
853 // but instead return a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000854 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
855 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000856
ager@chromium.org9085a012009-05-11 19:22:57 +0000857 // Convenience function: Same as above, but takes the fid instead.
858 void CallRuntime(Runtime::FunctionId id, int num_arguments);
859
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000860 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000861 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
862 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000863
ager@chromium.org5c838252010-02-19 08:53:10 +0000864 // Convenience function: call an external reference.
865 void CallExternalReference(const ExternalReference& ext,
866 int num_arguments);
867
ager@chromium.org9085a012009-05-11 19:22:57 +0000868 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000869 // Like JumpToExternalReference, but also takes care of passing the number
870 // of parameters.
871 void TailCallExternalReference(const ExternalReference& ext,
872 int num_arguments,
873 int result_size);
874
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000875 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
876 const ExternalReference& ext, int num_arguments, int result_size);
877
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000878 // Convenience function: tail call a runtime routine (jump).
879 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000880 int num_arguments,
881 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000882
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000883 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
884 int num_arguments,
885 int result_size);
886
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000887 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000888 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000889
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000890 // Jump to a runtime routine.
891 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
892 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000893
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000894 // Prepares stack to put arguments (aligns and so on).
895 // WIN64 calling convention requires to put the pointer to the return value
896 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
897 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
898 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
899 void PrepareCallApiFunction(int arg_stack_space);
900
901 // Calls an API function. Allocates HandleScope, extracts
902 // returned value from handle and propagates exceptions.
903 // Clobbers r12, r14, rbx and caller-save registers. Restores context.
904 // On return removes stack_space * kPointerSize (GCed).
905 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
906 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000907
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000908 // Before calling a C-function from generated code, align arguments on stack.
909 // After aligning the frame, arguments must be stored in esp[0], esp[4],
910 // etc., not pushed. The argument count assumes all arguments are word sized.
911 // The number of slots reserved for arguments depends on platform. On Windows
912 // stack slots are reserved for the arguments passed in registers. On other
913 // platforms stack slots are only reserved for the arguments actually passed
914 // on the stack.
915 void PrepareCallCFunction(int num_arguments);
916
917 // Calls a C function and cleans up the space for arguments allocated
918 // by PrepareCallCFunction. The called function is not allowed to trigger a
919 // garbage collection, since that might move the code and invalidate the
920 // return address (unless this is somehow accounted for by the called
921 // function).
922 void CallCFunction(ExternalReference function, int num_arguments);
923 void CallCFunction(Register function, int num_arguments);
924
925 // Calculate the number of stack slots to reserve for arguments when calling a
926 // C function.
927 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000928
929 // ---------------------------------------------------------------------------
930 // Utilities
931
932 void Ret();
933
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000934 // Return and drop arguments from stack, where the number of arguments
935 // may be bigger than 2^16 - 1. Requires a scratch register.
936 void Ret(int bytes_dropped, Register scratch);
937
ager@chromium.org9085a012009-05-11 19:22:57 +0000938 Handle<Object> CodeObject() { return code_object_; }
939
940
941 // ---------------------------------------------------------------------------
942 // StatsCounter support
943
944 void SetCounter(StatsCounter* counter, int value);
945 void IncrementCounter(StatsCounter* counter, int value);
946 void DecrementCounter(StatsCounter* counter, int value);
947
948
949 // ---------------------------------------------------------------------------
950 // Debugging
951
952 // Calls Abort(msg) if the condition cc is not satisfied.
953 // Use --debug_code to enable.
954 void Assert(Condition cc, const char* msg);
955
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000956 void AssertFastElements(Register elements);
957
ager@chromium.org9085a012009-05-11 19:22:57 +0000958 // Like Assert(), but always enabled.
959 void Check(Condition cc, const char* msg);
960
961 // Print a message to stdout and abort execution.
962 void Abort(const char* msg);
963
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000964 // Check that the stack is aligned.
965 void CheckStackAlignment();
966
ager@chromium.org9085a012009-05-11 19:22:57 +0000967 // Verify restrictions about code generated in stubs.
968 void set_generating_stub(bool value) { generating_stub_ = value; }
969 bool generating_stub() { return generating_stub_; }
970 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
971 bool allow_stub_calls() { return allow_stub_calls_; }
972
973 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000974 // Order general registers are pushed by Pushad.
975 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r12, r14.
976 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000977 static const int kNumSafepointSavedRegisters = 11;
978
ager@chromium.org9085a012009-05-11 19:22:57 +0000979 bool generating_stub_;
980 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000981
982 // Returns a register holding the smi value. The register MUST NOT be
983 // modified. It may be the "smi 1 constant" register.
984 Register GetSmiConstant(Smi* value);
985
986 // Moves the smi value to the destination register.
987 void LoadSmiConstant(Register dst, Smi* value);
988
ager@chromium.org5c838252010-02-19 08:53:10 +0000989 // This handle will be patched with the code object on installation.
990 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000991
992 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000993 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +0000994 void InvokePrologue(const ParameterCount& expected,
995 const ParameterCount& actual,
996 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000997 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000998 LabelType* done,
ager@chromium.org9085a012009-05-11 19:22:57 +0000999 InvokeFlag flag);
1000
ager@chromium.org9085a012009-05-11 19:22:57 +00001001 // Activation support.
1002 void EnterFrame(StackFrame::Type type);
1003 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001004
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001005 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001006
1007 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
1008 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001009 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001010
1011 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001012
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001013 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001014 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001015 // Otherwise the address of the new-space top is loaded into scratch (if
1016 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001017 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001018 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001019 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001020 // Update allocation top with value in result_end register.
1021 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001022 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001023
1024 // Helper for PopHandleScope. Allowed to perform a GC and returns
1025 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1026 // possibly returns a failure object indicating an allocation failure.
1027 Object* PopHandleScopeHelper(Register saved,
1028 Register scratch,
1029 bool gc_allowed);
ager@chromium.org9085a012009-05-11 19:22:57 +00001030};
1031
1032
ager@chromium.org4af710e2009-09-15 12:20:11 +00001033// The code patcher is used to patch (typically) small parts of code e.g. for
1034// debugging and other types of instrumentation. When using the code patcher
1035// the exact number of bytes specified must be emitted. Is not legal to emit
1036// relocation information. If any of these constraints are violated it causes
1037// an assertion.
1038class CodePatcher {
1039 public:
1040 CodePatcher(byte* address, int size);
1041 virtual ~CodePatcher();
1042
1043 // Macro assembler to emit code.
1044 MacroAssembler* masm() { return &masm_; }
1045
1046 private:
1047 byte* address_; // The address of the code being patched.
1048 int size_; // Number of bytes of the expected patch size.
1049 MacroAssembler masm_; // Macro assembler used to generate the code.
1050};
1051
1052
ager@chromium.org9085a012009-05-11 19:22:57 +00001053// -----------------------------------------------------------------------------
1054// Static helper functions.
1055
1056// Generate an Operand for loading a field from an object.
1057static inline Operand FieldOperand(Register object, int offset) {
1058 return Operand(object, offset - kHeapObjectTag);
1059}
1060
1061
1062// Generate an Operand for loading an indexed field from an object.
1063static inline Operand FieldOperand(Register object,
1064 Register index,
1065 ScaleFactor scale,
1066 int offset) {
1067 return Operand(object, index, scale, offset - kHeapObjectTag);
1068}
1069
1070
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001071static inline Operand ContextOperand(Register context, int index) {
1072 return Operand(context, Context::SlotOffset(index));
1073}
1074
1075
1076static inline Operand GlobalObjectOperand() {
1077 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1078}
1079
1080
1081// Provides access to exit frame stack space (not GCed).
1082static inline Operand StackSpaceOperand(int index) {
1083#ifdef _WIN64
1084 const int kShaddowSpace = 4;
1085 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1086#else
1087 return Operand(rsp, index * kPointerSize);
1088#endif
1089}
1090
1091
1092
ager@chromium.org9085a012009-05-11 19:22:57 +00001093#ifdef GENERATED_CODE_COVERAGE
1094extern void LogGeneratedCodeCoverage(const char* file_line);
1095#define CODE_COVERAGE_STRINGIFY(x) #x
1096#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1097#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1098#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001099 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001100 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1101 masm->pushfd(); \
1102 masm->pushad(); \
1103 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001104 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001105 masm->pop(rax); \
1106 masm->popad(); \
1107 masm->popfd(); \
1108 } \
1109 masm->
1110#else
1111#define ACCESS_MASM(masm) masm->
1112#endif
1113
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001114// -----------------------------------------------------------------------------
1115// Template implementations.
1116
1117static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1118
1119
1120template <typename LabelType>
1121void MacroAssembler::SmiNeg(Register dst,
1122 Register src,
1123 LabelType* on_smi_result) {
1124 if (dst.is(src)) {
1125 ASSERT(!dst.is(kScratchRegister));
1126 movq(kScratchRegister, src);
1127 neg(dst); // Low 32 bits are retained as zero by negation.
1128 // Test if result is zero or Smi::kMinValue.
1129 cmpq(dst, kScratchRegister);
1130 j(not_equal, on_smi_result);
1131 movq(src, kScratchRegister);
1132 } else {
1133 movq(dst, src);
1134 neg(dst);
1135 cmpq(dst, src);
1136 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1137 j(not_equal, on_smi_result);
1138 }
1139}
1140
1141
1142template <typename LabelType>
1143void MacroAssembler::SmiAdd(Register dst,
1144 Register src1,
1145 Register src2,
1146 LabelType* on_not_smi_result) {
1147 ASSERT_NOT_NULL(on_not_smi_result);
1148 ASSERT(!dst.is(src2));
1149 if (dst.is(src1)) {
1150 movq(kScratchRegister, src1);
1151 addq(kScratchRegister, src2);
1152 j(overflow, on_not_smi_result);
1153 movq(dst, kScratchRegister);
1154 } else {
1155 movq(dst, src1);
1156 addq(dst, src2);
1157 j(overflow, on_not_smi_result);
1158 }
1159}
1160
1161
1162template <typename LabelType>
1163void MacroAssembler::SmiSub(Register dst,
1164 Register src1,
1165 Register src2,
1166 LabelType* on_not_smi_result) {
1167 ASSERT_NOT_NULL(on_not_smi_result);
1168 ASSERT(!dst.is(src2));
1169 if (dst.is(src1)) {
1170 cmpq(dst, src2);
1171 j(overflow, on_not_smi_result);
1172 subq(dst, src2);
1173 } else {
1174 movq(dst, src1);
1175 subq(dst, src2);
1176 j(overflow, on_not_smi_result);
1177 }
1178}
1179
1180
1181template <typename LabelType>
1182void MacroAssembler::SmiSub(Register dst,
1183 Register src1,
1184 const Operand& src2,
1185 LabelType* on_not_smi_result) {
1186 ASSERT_NOT_NULL(on_not_smi_result);
1187 if (dst.is(src1)) {
1188 movq(kScratchRegister, src2);
1189 cmpq(src1, kScratchRegister);
1190 j(overflow, on_not_smi_result);
1191 subq(src1, kScratchRegister);
1192 } else {
1193 movq(dst, src1);
1194 subq(dst, src2);
1195 j(overflow, on_not_smi_result);
1196 }
1197}
1198
1199
1200template <typename LabelType>
1201void MacroAssembler::SmiMul(Register dst,
1202 Register src1,
1203 Register src2,
1204 LabelType* on_not_smi_result) {
1205 ASSERT(!dst.is(src2));
1206 ASSERT(!dst.is(kScratchRegister));
1207 ASSERT(!src1.is(kScratchRegister));
1208 ASSERT(!src2.is(kScratchRegister));
1209
1210 if (dst.is(src1)) {
1211 NearLabel failure, zero_correct_result;
1212 movq(kScratchRegister, src1); // Create backup for later testing.
1213 SmiToInteger64(dst, src1);
1214 imul(dst, src2);
1215 j(overflow, &failure);
1216
1217 // Check for negative zero result. If product is zero, and one
1218 // argument is negative, go to slow case.
1219 NearLabel correct_result;
1220 testq(dst, dst);
1221 j(not_zero, &correct_result);
1222
1223 movq(dst, kScratchRegister);
1224 xor_(dst, src2);
1225 j(positive, &zero_correct_result); // Result was positive zero.
1226
1227 bind(&failure); // Reused failure exit, restores src1.
1228 movq(src1, kScratchRegister);
1229 jmp(on_not_smi_result);
1230
1231 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001232 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001233
1234 bind(&correct_result);
1235 } else {
1236 SmiToInteger64(dst, src1);
1237 imul(dst, src2);
1238 j(overflow, on_not_smi_result);
1239 // Check for negative zero result. If product is zero, and one
1240 // argument is negative, go to slow case.
1241 NearLabel correct_result;
1242 testq(dst, dst);
1243 j(not_zero, &correct_result);
1244 // One of src1 and src2 is zero, the check whether the other is
1245 // negative.
1246 movq(kScratchRegister, src1);
1247 xor_(kScratchRegister, src2);
1248 j(negative, on_not_smi_result);
1249 bind(&correct_result);
1250 }
1251}
1252
1253
1254template <typename LabelType>
1255void MacroAssembler::SmiTryAddConstant(Register dst,
1256 Register src,
1257 Smi* constant,
1258 LabelType* on_not_smi_result) {
1259 // Does not assume that src is a smi.
1260 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1261 ASSERT_EQ(0, kSmiTag);
1262 ASSERT(!dst.is(kScratchRegister));
1263 ASSERT(!src.is(kScratchRegister));
1264
1265 JumpIfNotSmi(src, on_not_smi_result);
1266 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1267 LoadSmiConstant(tmp, constant);
1268 addq(tmp, src);
1269 j(overflow, on_not_smi_result);
1270 if (dst.is(src)) {
1271 movq(dst, tmp);
1272 }
1273}
1274
1275
1276template <typename LabelType>
1277void MacroAssembler::SmiAddConstant(Register dst,
1278 Register src,
1279 Smi* constant,
1280 LabelType* on_not_smi_result) {
1281 if (constant->value() == 0) {
1282 if (!dst.is(src)) {
1283 movq(dst, src);
1284 }
1285 } else if (dst.is(src)) {
1286 ASSERT(!dst.is(kScratchRegister));
1287
1288 LoadSmiConstant(kScratchRegister, constant);
1289 addq(kScratchRegister, src);
1290 j(overflow, on_not_smi_result);
1291 movq(dst, kScratchRegister);
1292 } else {
1293 LoadSmiConstant(dst, constant);
1294 addq(dst, src);
1295 j(overflow, on_not_smi_result);
1296 }
1297}
1298
1299
1300template <typename LabelType>
1301void MacroAssembler::SmiSubConstant(Register dst,
1302 Register src,
1303 Smi* constant,
1304 LabelType* on_not_smi_result) {
1305 if (constant->value() == 0) {
1306 if (!dst.is(src)) {
1307 movq(dst, src);
1308 }
1309 } else if (dst.is(src)) {
1310 ASSERT(!dst.is(kScratchRegister));
1311 if (constant->value() == Smi::kMinValue) {
1312 // Subtracting min-value from any non-negative value will overflow.
1313 // We test the non-negativeness before doing the subtraction.
1314 testq(src, src);
1315 j(not_sign, on_not_smi_result);
1316 LoadSmiConstant(kScratchRegister, constant);
1317 subq(dst, kScratchRegister);
1318 } else {
1319 // Subtract by adding the negation.
1320 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1321 addq(kScratchRegister, dst);
1322 j(overflow, on_not_smi_result);
1323 movq(dst, kScratchRegister);
1324 }
1325 } else {
1326 if (constant->value() == Smi::kMinValue) {
1327 // Subtracting min-value from any non-negative value will overflow.
1328 // We test the non-negativeness before doing the subtraction.
1329 testq(src, src);
1330 j(not_sign, on_not_smi_result);
1331 LoadSmiConstant(dst, constant);
1332 // Adding and subtracting the min-value gives the same result, it only
1333 // differs on the overflow bit, which we don't check here.
1334 addq(dst, src);
1335 } else {
1336 // Subtract by adding the negation.
1337 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1338 addq(dst, src);
1339 j(overflow, on_not_smi_result);
1340 }
1341 }
1342}
1343
1344
1345template <typename LabelType>
1346void MacroAssembler::SmiDiv(Register dst,
1347 Register src1,
1348 Register src2,
1349 LabelType* on_not_smi_result) {
1350 ASSERT(!src1.is(kScratchRegister));
1351 ASSERT(!src2.is(kScratchRegister));
1352 ASSERT(!dst.is(kScratchRegister));
1353 ASSERT(!src2.is(rax));
1354 ASSERT(!src2.is(rdx));
1355 ASSERT(!src1.is(rdx));
1356
1357 // Check for 0 divisor (result is +/-Infinity).
1358 NearLabel positive_divisor;
1359 testq(src2, src2);
1360 j(zero, on_not_smi_result);
1361
1362 if (src1.is(rax)) {
1363 movq(kScratchRegister, src1);
1364 }
1365 SmiToInteger32(rax, src1);
1366 // We need to rule out dividing Smi::kMinValue by -1, since that would
1367 // overflow in idiv and raise an exception.
1368 // We combine this with negative zero test (negative zero only happens
1369 // when dividing zero by a negative number).
1370
1371 // We overshoot a little and go to slow case if we divide min-value
1372 // by any negative value, not just -1.
1373 NearLabel safe_div;
1374 testl(rax, Immediate(0x7fffffff));
1375 j(not_zero, &safe_div);
1376 testq(src2, src2);
1377 if (src1.is(rax)) {
1378 j(positive, &safe_div);
1379 movq(src1, kScratchRegister);
1380 jmp(on_not_smi_result);
1381 } else {
1382 j(negative, on_not_smi_result);
1383 }
1384 bind(&safe_div);
1385
1386 SmiToInteger32(src2, src2);
1387 // Sign extend src1 into edx:eax.
1388 cdq();
1389 idivl(src2);
1390 Integer32ToSmi(src2, src2);
1391 // Check that the remainder is zero.
1392 testl(rdx, rdx);
1393 if (src1.is(rax)) {
1394 NearLabel smi_result;
1395 j(zero, &smi_result);
1396 movq(src1, kScratchRegister);
1397 jmp(on_not_smi_result);
1398 bind(&smi_result);
1399 } else {
1400 j(not_zero, on_not_smi_result);
1401 }
1402 if (!dst.is(src1) && src1.is(rax)) {
1403 movq(src1, kScratchRegister);
1404 }
1405 Integer32ToSmi(dst, rax);
1406}
1407
1408
1409template <typename LabelType>
1410void MacroAssembler::SmiMod(Register dst,
1411 Register src1,
1412 Register src2,
1413 LabelType* on_not_smi_result) {
1414 ASSERT(!dst.is(kScratchRegister));
1415 ASSERT(!src1.is(kScratchRegister));
1416 ASSERT(!src2.is(kScratchRegister));
1417 ASSERT(!src2.is(rax));
1418 ASSERT(!src2.is(rdx));
1419 ASSERT(!src1.is(rdx));
1420 ASSERT(!src1.is(src2));
1421
1422 testq(src2, src2);
1423 j(zero, on_not_smi_result);
1424
1425 if (src1.is(rax)) {
1426 movq(kScratchRegister, src1);
1427 }
1428 SmiToInteger32(rax, src1);
1429 SmiToInteger32(src2, src2);
1430
1431 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1432 NearLabel safe_div;
1433 cmpl(rax, Immediate(Smi::kMinValue));
1434 j(not_equal, &safe_div);
1435 cmpl(src2, Immediate(-1));
1436 j(not_equal, &safe_div);
1437 // Retag inputs and go slow case.
1438 Integer32ToSmi(src2, src2);
1439 if (src1.is(rax)) {
1440 movq(src1, kScratchRegister);
1441 }
1442 jmp(on_not_smi_result);
1443 bind(&safe_div);
1444
1445 // Sign extend eax into edx:eax.
1446 cdq();
1447 idivl(src2);
1448 // Restore smi tags on inputs.
1449 Integer32ToSmi(src2, src2);
1450 if (src1.is(rax)) {
1451 movq(src1, kScratchRegister);
1452 }
1453 // Check for a negative zero result. If the result is zero, and the
1454 // dividend is negative, go slow to return a floating point negative zero.
1455 NearLabel smi_result;
1456 testl(rdx, rdx);
1457 j(not_zero, &smi_result);
1458 testq(src1, src1);
1459 j(negative, on_not_smi_result);
1460 bind(&smi_result);
1461 Integer32ToSmi(dst, rdx);
1462}
1463
1464
1465template <typename LabelType>
1466void MacroAssembler::SmiShiftLogicalRightConstant(
1467 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1468 // Logic right shift interprets its result as an *unsigned* number.
1469 if (dst.is(src)) {
1470 UNIMPLEMENTED(); // Not used.
1471 } else {
1472 movq(dst, src);
1473 if (shift_value == 0) {
1474 testq(dst, dst);
1475 j(negative, on_not_smi_result);
1476 }
1477 shr(dst, Immediate(shift_value + kSmiShift));
1478 shl(dst, Immediate(kSmiShift));
1479 }
1480}
1481
1482
1483template <typename LabelType>
1484void MacroAssembler::SmiShiftLogicalRight(Register dst,
1485 Register src1,
1486 Register src2,
1487 LabelType* on_not_smi_result) {
1488 ASSERT(!dst.is(kScratchRegister));
1489 ASSERT(!src1.is(kScratchRegister));
1490 ASSERT(!src2.is(kScratchRegister));
1491 ASSERT(!dst.is(rcx));
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001492 // dst and src1 can be the same, because the one case that bails out
1493 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001494 NearLabel result_ok;
1495 if (src1.is(rcx) || src2.is(rcx)) {
1496 movq(kScratchRegister, rcx);
1497 }
1498 if (!dst.is(src1)) {
1499 movq(dst, src1);
1500 }
1501 SmiToInteger32(rcx, src2);
1502 orl(rcx, Immediate(kSmiShift));
1503 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1504 shl(dst, Immediate(kSmiShift));
1505 testq(dst, dst);
1506 if (src1.is(rcx) || src2.is(rcx)) {
1507 NearLabel positive_result;
1508 j(positive, &positive_result);
1509 if (src1.is(rcx)) {
1510 movq(src1, kScratchRegister);
1511 } else {
1512 movq(src2, kScratchRegister);
1513 }
1514 jmp(on_not_smi_result);
1515 bind(&positive_result);
1516 } else {
1517 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1518 }
1519}
1520
1521
1522template <typename LabelType>
1523void MacroAssembler::SelectNonSmi(Register dst,
1524 Register src1,
1525 Register src2,
1526 LabelType* on_not_smis) {
1527 ASSERT(!dst.is(kScratchRegister));
1528 ASSERT(!src1.is(kScratchRegister));
1529 ASSERT(!src2.is(kScratchRegister));
1530 ASSERT(!dst.is(src1));
1531 ASSERT(!dst.is(src2));
1532 // Both operands must not be smis.
1533#ifdef DEBUG
1534 if (allow_stub_calls()) { // Check contains a stub call.
1535 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1536 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1537 }
1538#endif
1539 ASSERT_EQ(0, kSmiTag);
1540 ASSERT_EQ(0, Smi::FromInt(0));
1541 movl(kScratchRegister, Immediate(kSmiTagMask));
1542 and_(kScratchRegister, src1);
1543 testl(kScratchRegister, src2);
1544 // If non-zero then both are smis.
1545 j(not_zero, on_not_smis);
1546
1547 // Exactly one operand is a smi.
1548 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1549 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1550 subq(kScratchRegister, Immediate(1));
1551 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1552 movq(dst, src1);
1553 xor_(dst, src2);
1554 and_(dst, kScratchRegister);
1555 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1556 xor_(dst, src1);
1557 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1558}
1559
1560
1561template <typename LabelType>
1562void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1563 ASSERT_EQ(0, kSmiTag);
1564 Condition smi = CheckSmi(src);
1565 j(smi, on_smi);
1566}
1567
1568
1569template <typename LabelType>
1570void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1571 Condition smi = CheckSmi(src);
1572 j(NegateCondition(smi), on_not_smi);
1573}
1574
1575
1576template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001577void MacroAssembler::JumpUnlessNonNegativeSmi(
1578 Register src, LabelType* on_not_smi_or_negative) {
1579 Condition non_negative_smi = CheckNonNegativeSmi(src);
1580 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001581}
1582
1583
1584template <typename LabelType>
1585void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1586 Smi* constant,
1587 LabelType* on_equals) {
1588 SmiCompare(src, constant);
1589 j(equal, on_equals);
1590}
1591
1592
1593template <typename LabelType>
1594void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1595 LabelType* on_invalid) {
1596 Condition is_valid = CheckInteger32ValidSmiValue(src);
1597 j(NegateCondition(is_valid), on_invalid);
1598}
1599
1600
1601template <typename LabelType>
1602void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1603 LabelType* on_invalid) {
1604 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1605 j(NegateCondition(is_valid), on_invalid);
1606}
1607
1608
1609template <typename LabelType>
1610void MacroAssembler::JumpIfNotBothSmi(Register src1,
1611 Register src2,
1612 LabelType* on_not_both_smi) {
1613 Condition both_smi = CheckBothSmi(src1, src2);
1614 j(NegateCondition(both_smi), on_not_both_smi);
1615}
1616
1617
1618template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001619void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1620 Register src2,
1621 LabelType* on_not_both_smi) {
1622 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001623 j(NegateCondition(both_smi), on_not_both_smi);
1624}
1625
1626
1627template <typename LabelType>
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001628void MacroAssembler::JumpIfNotString(Register object,
1629 Register object_map,
1630 LabelType* not_string) {
1631 Condition is_smi = CheckSmi(object);
1632 j(is_smi, not_string);
1633 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1634 j(above_equal, not_string);
1635}
1636
1637
1638template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001639void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1640 Register second_object,
1641 Register scratch1,
1642 Register scratch2,
1643 LabelType* on_fail) {
1644 // Check that both objects are not smis.
1645 Condition either_smi = CheckEitherSmi(first_object, second_object);
1646 j(either_smi, on_fail);
1647
1648 // Load instance type for both strings.
1649 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1650 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1651 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1652 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1653
1654 // Check that both are flat ascii strings.
1655 ASSERT(kNotStringTag != 0);
1656 const int kFlatAsciiStringMask =
1657 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1658 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1659
1660 andl(scratch1, Immediate(kFlatAsciiStringMask));
1661 andl(scratch2, Immediate(kFlatAsciiStringMask));
1662 // Interleave the bits to check both scratch1 and scratch2 in one test.
1663 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1664 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1665 cmpl(scratch1,
1666 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1667 j(not_equal, on_fail);
1668}
1669
1670
1671template <typename LabelType>
1672void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1673 Register instance_type,
1674 Register scratch,
1675 LabelType *failure) {
1676 if (!scratch.is(instance_type)) {
1677 movl(scratch, instance_type);
1678 }
1679
1680 const int kFlatAsciiStringMask =
1681 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1682
1683 andl(scratch, Immediate(kFlatAsciiStringMask));
1684 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1685 j(not_equal, failure);
1686}
1687
1688
1689template <typename LabelType>
1690void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1691 Register first_object_instance_type,
1692 Register second_object_instance_type,
1693 Register scratch1,
1694 Register scratch2,
1695 LabelType* on_fail) {
1696 // Load instance type for both strings.
1697 movq(scratch1, first_object_instance_type);
1698 movq(scratch2, second_object_instance_type);
1699
1700 // Check that both are flat ascii strings.
1701 ASSERT(kNotStringTag != 0);
1702 const int kFlatAsciiStringMask =
1703 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1704 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1705
1706 andl(scratch1, Immediate(kFlatAsciiStringMask));
1707 andl(scratch2, Immediate(kFlatAsciiStringMask));
1708 // Interleave the bits to check both scratch1 and scratch2 in one test.
1709 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1710 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1711 cmpl(scratch1,
1712 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1713 j(not_equal, on_fail);
1714}
1715
1716
1717template <typename LabelType>
1718void MacroAssembler::InNewSpace(Register object,
1719 Register scratch,
1720 Condition cc,
1721 LabelType* branch) {
1722 if (Serializer::enabled()) {
1723 // Can't do arithmetic on external references if it might get serialized.
1724 // The mask isn't really an address. We load it as an external reference in
1725 // case the size of the new space is different between the snapshot maker
1726 // and the running system.
1727 if (scratch.is(object)) {
1728 movq(kScratchRegister, ExternalReference::new_space_mask());
1729 and_(scratch, kScratchRegister);
1730 } else {
1731 movq(scratch, ExternalReference::new_space_mask());
1732 and_(scratch, object);
1733 }
1734 movq(kScratchRegister, ExternalReference::new_space_start());
1735 cmpq(scratch, kScratchRegister);
1736 j(cc, branch);
1737 } else {
1738 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1739 intptr_t new_space_start =
1740 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1741 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1742 if (scratch.is(object)) {
1743 addq(scratch, kScratchRegister);
1744 } else {
1745 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1746 }
1747 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1748 j(cc, branch);
1749 }
1750}
1751
1752
1753template <typename LabelType>
1754void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1755 const ParameterCount& actual,
1756 Handle<Code> code_constant,
1757 Register code_register,
1758 LabelType* done,
1759 InvokeFlag flag) {
1760 bool definitely_matches = false;
1761 NearLabel invoke;
1762 if (expected.is_immediate()) {
1763 ASSERT(actual.is_immediate());
1764 if (expected.immediate() == actual.immediate()) {
1765 definitely_matches = true;
1766 } else {
1767 Set(rax, actual.immediate());
1768 if (expected.immediate() ==
1769 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1770 // Don't worry about adapting arguments for built-ins that
1771 // don't want that done. Skip adaption code by making it look
1772 // like we have a match between expected and actual number of
1773 // arguments.
1774 definitely_matches = true;
1775 } else {
1776 Set(rbx, expected.immediate());
1777 }
1778 }
1779 } else {
1780 if (actual.is_immediate()) {
1781 // Expected is in register, actual is immediate. This is the
1782 // case when we invoke function values without going through the
1783 // IC mechanism.
1784 cmpq(expected.reg(), Immediate(actual.immediate()));
1785 j(equal, &invoke);
1786 ASSERT(expected.reg().is(rbx));
1787 Set(rax, actual.immediate());
1788 } else if (!expected.reg().is(actual.reg())) {
1789 // Both expected and actual are in (different) registers. This
1790 // is the case when we invoke functions using call and apply.
1791 cmpq(expected.reg(), actual.reg());
1792 j(equal, &invoke);
1793 ASSERT(actual.reg().is(rax));
1794 ASSERT(expected.reg().is(rbx));
1795 }
1796 }
1797
1798 if (!definitely_matches) {
1799 Handle<Code> adaptor =
1800 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1801 if (!code_constant.is_null()) {
1802 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1803 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1804 } else if (!code_register.is(rdx)) {
1805 movq(rdx, code_register);
1806 }
1807
1808 if (flag == CALL_FUNCTION) {
1809 Call(adaptor, RelocInfo::CODE_TARGET);
1810 jmp(done);
1811 } else {
1812 Jump(adaptor, RelocInfo::CODE_TARGET);
1813 }
1814 bind(&invoke);
1815 }
1816}
1817
ager@chromium.org9085a012009-05-11 19:22:57 +00001818
1819} } // namespace v8::internal
1820
1821#endif // V8_X64_MACRO_ASSEMBLER_X64_H_