blob: 348191ee254ae40e80528b4e885d6647ea1d7342 [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);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000077 void CompareRoot(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.
155 void EnterExitFrame(int arg_stack_space = 0);
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.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000164 void LeaveExitFrame();
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.
171 void PushSafepointRegisters() { UNIMPLEMENTED(); }
172 void PopSafepointRegisters() { UNIMPLEMENTED(); }
173 static int SafepointRegisterStackIndex(int reg_code) {
174 UNIMPLEMENTED();
175 return 0;
176 }
177
ager@chromium.org9085a012009-05-11 19:22:57 +0000178 // ---------------------------------------------------------------------------
179 // JavaScript invokes
180
181 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000182 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000183 const ParameterCount& expected,
184 const ParameterCount& actual,
185 InvokeFlag flag);
186
187 void InvokeCode(Handle<Code> code,
188 const ParameterCount& expected,
189 const ParameterCount& actual,
190 RelocInfo::Mode rmode,
191 InvokeFlag flag);
192
193 // Invoke the JavaScript function in the given register. Changes the
194 // current context to the context in the function before invoking.
195 void InvokeFunction(Register function,
196 const ParameterCount& actual,
197 InvokeFlag flag);
198
ager@chromium.org5c838252010-02-19 08:53:10 +0000199 void InvokeFunction(JSFunction* function,
200 const ParameterCount& actual,
201 InvokeFlag flag);
202
ager@chromium.org9085a012009-05-11 19:22:57 +0000203 // Invoke specified builtin JavaScript function. Adds an entry to
204 // the unresolved list if the name does not resolve.
205 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
206
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000207 // Store the function for the given builtin in the target register.
208 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
209
ager@chromium.org9085a012009-05-11 19:22:57 +0000210 // Store the code object for the given builtin in the target register.
211 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
212
ager@chromium.org4af710e2009-09-15 12:20:11 +0000213
214 // ---------------------------------------------------------------------------
215 // Smi tagging, untagging and operations on tagged smis.
216
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000217 void InitializeSmiConstantRegister() {
218 movq(kSmiConstantRegister,
219 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
220 RelocInfo::NONE);
221 }
222
ager@chromium.org4af710e2009-09-15 12:20:11 +0000223 // Conversions between tagged smi values and non-tagged integer values.
224
225 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000226 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000227 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000228 void Integer32ToSmi(Register dst, Register src);
229
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000230 // Stores an integer32 value into a memory field that already holds a smi.
231 void Integer32ToSmiField(const Operand& dst, Register src);
232
ager@chromium.org4af710e2009-09-15 12:20:11 +0000233 // Adds constant to src and tags the result as a smi.
234 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000235 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000236
237 // Convert smi to 32-bit integer. I.e., not sign extended into
238 // high 32 bits of destination.
239 void SmiToInteger32(Register dst, Register src);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000240 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000241
242 // Convert smi to 64-bit integer (sign extended if necessary).
243 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000244 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000245
246 // Multiply a positive smi's integer value by a power of two.
247 // Provides result as 64-bit integer value.
248 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
249 Register src,
250 int power);
251
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000252 // Divide a positive smi's integer value by a power of two.
253 // Provides result as 32-bit integer value.
254 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
255 Register src,
256 int power);
257
258
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000259 // Simple comparison of smis.
260 void SmiCompare(Register dst, Register src);
261 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000262 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000263 void SmiCompare(const Operand& dst, Register src);
264 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000265 // Compare the int32 in src register to the value of the smi stored at dst.
266 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000267 // Sets sign and zero flags depending on value of smi in register.
268 void SmiTest(Register src);
269
ager@chromium.org4af710e2009-09-15 12:20:11 +0000270 // Functions performing a check on a known or potential smi. Returns
271 // a condition that is satisfied if the check is successful.
272
273 // Is the value a tagged smi.
274 Condition CheckSmi(Register src);
275
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000276 // Is the value a non-negative tagged smi.
277 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000278
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000279 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000280 Condition CheckBothSmi(Register first, Register second);
281
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000282 // Are both values non-negative tagged smis.
283 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000284
285 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000286 Condition CheckEitherSmi(Register first,
287 Register second,
288 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000289
ager@chromium.org4af710e2009-09-15 12:20:11 +0000290 // Is the value the minimum smi value (since we are using
291 // two's complement numbers, negating the value is known to yield
292 // a non-smi value).
293 Condition CheckIsMinSmi(Register src);
294
ager@chromium.org4af710e2009-09-15 12:20:11 +0000295 // Checks whether an 32-bit integer value is a valid for conversion
296 // to a smi.
297 Condition CheckInteger32ValidSmiValue(Register src);
298
ager@chromium.org3811b432009-10-28 14:53:37 +0000299 // Checks whether an 32-bit unsigned integer value is a valid for
300 // conversion to a smi.
301 Condition CheckUInteger32ValidSmiValue(Register src);
302
ager@chromium.org4af710e2009-09-15 12:20:11 +0000303 // Test-and-jump functions. Typically combines a check function
304 // above with a conditional jump.
305
306 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000307 template <typename LabelType>
308 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000309
ager@chromium.org3811b432009-10-28 14:53:37 +0000310 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000311 template <typename LabelType>
312 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000313
ager@chromium.org4af710e2009-09-15 12:20:11 +0000314 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000315 template <typename LabelType>
316 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000317
318 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000319 template <typename LabelType>
320 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000321
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000322 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000323 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000324 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000325
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000326 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000327 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000328 template <typename LabelType>
329 void JumpIfSmiEqualsConstant(Register src,
330 Smi* constant,
331 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000332
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000334 template <typename LabelType>
335 void JumpIfNotBothSmi(Register src1,
336 Register src2,
337 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000338
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000339 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000340 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000341 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
342 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000343
ager@chromium.org4af710e2009-09-15 12:20:11 +0000344 // Operations on tagged smi values.
345
346 // Smis represent a subset of integers. The subset is always equivalent to
347 // a two's complement interpretation of a fixed number of bits.
348
349 // Optimistically adds an integer constant to a supposed smi.
350 // If the src is not a smi, or the result is not a smi, jump to
351 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000352 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000353 void SmiTryAddConstant(Register dst,
354 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000355 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000356 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000357
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000358 // Add an integer constant to a tagged smi, giving a tagged smi as result.
359 // No overflow testing on the result is done.
360 void SmiAddConstant(Register dst, Register src, Smi* constant);
361
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000362 // Add an integer constant to a tagged smi, giving a tagged smi as result.
363 // No overflow testing on the result is done.
364 void SmiAddConstant(const Operand& dst, Smi* constant);
365
ager@chromium.org4af710e2009-09-15 12:20:11 +0000366 // Add an integer constant to a tagged smi, giving a tagged smi as result,
367 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000368 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000369 void SmiAddConstant(Register dst,
370 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000371 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000372 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000373
374 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000375 // result. No testing on the result is done. Sets the N and Z flags
376 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000377 void SmiSubConstant(Register dst, Register src, Smi* constant);
378
379 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000380 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000381 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000382 void SmiSubConstant(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
387 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000388 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000389 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000390 void SmiNeg(Register dst,
391 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000392 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000393
394 // Adds smi values and return the result as a smi.
395 // If dst is src1, then src1 will be destroyed, even if
396 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000397 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000398 void SmiAdd(Register dst,
399 Register src1,
400 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000401 LabelType* on_not_smi_result);
402
403 void SmiAdd(Register dst,
404 Register src1,
405 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000406
407 // Subtracts smi values and return the result as a smi.
408 // If dst is src1, then src1 will be destroyed, even if
409 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000410 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000411 void SmiSub(Register dst,
412 Register src1,
413 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000414 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000415
ager@chromium.orgac091b72010-05-05 07:34:42 +0000416 void SmiSub(Register dst,
417 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000418 Register src2);
419
420 template <typename LabelType>
421 void SmiSub(Register dst,
422 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000423 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000424 LabelType* on_not_smi_result);
425
426 void SmiSub(Register dst,
427 Register src1,
428 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000429
ager@chromium.org4af710e2009-09-15 12:20:11 +0000430 // Multiplies smi values and return the result as a smi,
431 // if possible.
432 // If dst is src1, then src1 will be destroyed, even if
433 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000434 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000435 void SmiMul(Register dst,
436 Register src1,
437 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000438 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000439
440 // Divides one smi by another and returns the quotient.
441 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000442 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000443 void SmiDiv(Register dst,
444 Register src1,
445 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000446 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000447
448 // Divides one smi by another and returns the remainder.
449 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000450 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000451 void SmiMod(Register dst,
452 Register src1,
453 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000454 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000455
456 // Bitwise operations.
457 void SmiNot(Register dst, Register src);
458 void SmiAnd(Register dst, Register src1, Register src2);
459 void SmiOr(Register dst, Register src1, Register src2);
460 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000461 void SmiAndConstant(Register dst, Register src1, Smi* constant);
462 void SmiOrConstant(Register dst, Register src1, Smi* constant);
463 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000464
465 void SmiShiftLeftConstant(Register dst,
466 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000467 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000468 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000469 void SmiShiftLogicalRightConstant(Register dst,
470 Register src,
471 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000472 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000473 void SmiShiftArithmeticRightConstant(Register dst,
474 Register src,
475 int shift_value);
476
477 // Shifts a smi value to the left, and returns the result if that is a smi.
478 // Uses and clobbers rcx, so dst may not be rcx.
479 void SmiShiftLeft(Register dst,
480 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000481 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000482 // Shifts a smi value to the right, shifting in zero bits at the top, and
483 // returns the unsigned intepretation of the result if that is a smi.
484 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000485 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000486 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000487 Register src1,
488 Register src2,
489 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000490 // Shifts a smi value to the right, sign extending the top, and
491 // returns the signed intepretation of the result. That will always
492 // be a valid smi value, since it's numerically smaller than the
493 // original.
494 // Uses and clobbers rcx, so dst may not be rcx.
495 void SmiShiftArithmeticRight(Register dst,
496 Register src1,
497 Register src2);
498
499 // Specialized operations
500
501 // Select the non-smi register of two registers where exactly one is a
502 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000503 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000504 void SelectNonSmi(Register dst,
505 Register src1,
506 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000507 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000508
509 // Converts, if necessary, a smi to a combination of number and
510 // multiplier to be used as a scaled index.
511 // The src register contains a *positive* smi value. The shift is the
512 // power of two to multiply the index value by (e.g.
513 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
514 // The returned index register may be either src or dst, depending
515 // on what is most efficient. If src and dst are different registers,
516 // src is always unchanged.
517 SmiIndex SmiToIndex(Register dst, Register src, int shift);
518
519 // Converts a positive smi to a negative index.
520 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
521
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000522 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000523 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000524 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000525 }
526
527 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000528 Register constant = GetSmiConstant(source);
529 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000530 }
531
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000532 void Push(Smi* smi);
533 void Test(const Operand& dst, Smi* source);
534
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000535 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000536 // String macros.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000537 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000538 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
539 Register second_object,
540 Register scratch1,
541 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000542 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000543
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000544 // Check whether the instance type represents a flat ascii string. Jump to the
545 // label if not. If the instance type can be scratched specify same register
546 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000547 template <typename LabelType>
548 void JumpIfInstanceTypeIsNotSequentialAscii(
549 Register instance_type,
550 Register scratch,
551 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000552
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000553 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000554 void JumpIfBothInstanceTypesAreNotSequentialAscii(
555 Register first_object_instance_type,
556 Register second_object_instance_type,
557 Register scratch1,
558 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000559 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000560
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000561 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000562 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000563
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000564 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000565 void Set(Register dst, int64_t x);
566 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000567
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000568 // Move if the registers are not identical.
569 void Move(Register target, Register source);
570
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000571 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000572 void Move(Register dst, Handle<Object> source);
573 void Move(const Operand& dst, Handle<Object> source);
574 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000575 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000576 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000577
578 // Emit code to discard a non-negative number of pointer-sized elements
579 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000580 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000581
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000582 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000583
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000584 // Control Flow
585 void Jump(Address destination, RelocInfo::Mode rmode);
586 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000587 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
588
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000589 void Call(Address destination, RelocInfo::Mode rmode);
590 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000591 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000592
ager@chromium.org9085a012009-05-11 19:22:57 +0000593 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000594 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000595 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000596 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000597 void CmpObjectType(Register heap_object, InstanceType type, Register map);
598
599 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000600 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000601 void CmpInstanceType(Register map, InstanceType type);
602
ager@chromium.org5c838252010-02-19 08:53:10 +0000603 // Check if the map of an object is equal to a specified map and
604 // branch to label if not. Skip the smi check if not required
605 // (object is known to be a heap object)
606 void CheckMap(Register obj,
607 Handle<Map> map,
608 Label* fail,
609 bool is_heap_object);
610
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000611 // Check if the object in register heap_object is a string. Afterwards the
612 // register map contains the object map and the register instance_type
613 // contains the instance_type. The registers map and instance_type can be the
614 // same in which case it contains the instance type afterwards. Either of the
615 // registers map and instance_type can be the same as heap_object.
616 Condition IsObjectStringType(Register heap_object,
617 Register map,
618 Register instance_type);
619
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000620 // FCmp compares and pops the two values on top of the FPU stack.
621 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000622 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
623 void FCmp();
624
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000626 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000627
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000628 // Abort execution if argument is a smi. Used in debug code.
629 void AbortIfSmi(Register object);
630
lrn@chromium.org25156de2010-04-06 13:10:27 +0000631 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000632 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000633
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000634 // Abort execution if argument is not the root value with the given index.
635 void AbortIfNotRootValue(Register src,
636 Heap::RootListIndex root_value_index,
637 const char* message);
638
ager@chromium.org9085a012009-05-11 19:22:57 +0000639 // ---------------------------------------------------------------------------
640 // Exception handling
641
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000642 // Push a new try handler and link into try handler chain. The return
643 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000644 void PushTryHandler(CodeLocation try_location, HandlerType type);
645
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000646 // Unlink the stack handler on top of the stack from the try handler chain.
647 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000648
649 // ---------------------------------------------------------------------------
650 // Inline caching support
651
ager@chromium.org9085a012009-05-11 19:22:57 +0000652 // Generate code for checking access rights - used for security checks
653 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000654 // is left untouched, but the scratch register and kScratchRegister,
655 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000656 void CheckAccessGlobalProxy(Register holder_reg,
657 Register scratch,
658 Label* miss);
659
660
661 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000662 // Allocation support
663
664 // Allocate an object in new space. If the new space is exhausted control
665 // continues at the gc_required label. The allocated object is returned in
666 // result and end of the new object is returned in result_end. The register
667 // scratch can be passed as no_reg in which case an additional object
668 // reference will be added to the reloc info. The returned pointers in result
669 // and result_end have not yet been tagged as heap objects. If
670 // result_contains_top_on_entry is true the content of result is known to be
671 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000672 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000673 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000674 void AllocateInNewSpace(int object_size,
675 Register result,
676 Register result_end,
677 Register scratch,
678 Label* gc_required,
679 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000680
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000681 void AllocateInNewSpace(int header_size,
682 ScaleFactor element_size,
683 Register element_count,
684 Register result,
685 Register result_end,
686 Register scratch,
687 Label* gc_required,
688 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000689
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000690 void AllocateInNewSpace(Register object_size,
691 Register result,
692 Register result_end,
693 Register scratch,
694 Label* gc_required,
695 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000696
697 // Undo allocation in new space. The object passed and objects allocated after
698 // it will no longer be allocated. Make sure that no pointers are left to the
699 // object(s) no longer allocated as they would be invalid when allocation is
700 // un-done.
701 void UndoAllocationInNewSpace(Register object);
702
ager@chromium.org3811b432009-10-28 14:53:37 +0000703 // Allocate a heap number in new space with undefined value. Returns
704 // tagged pointer in result register, or jumps to gc_required if new
705 // space is full.
706 void AllocateHeapNumber(Register result,
707 Register scratch,
708 Label* gc_required);
709
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000710 // Allocate a sequential string. All the header fields of the string object
711 // are initialized.
712 void AllocateTwoByteString(Register result,
713 Register length,
714 Register scratch1,
715 Register scratch2,
716 Register scratch3,
717 Label* gc_required);
718 void AllocateAsciiString(Register result,
719 Register length,
720 Register scratch1,
721 Register scratch2,
722 Register scratch3,
723 Label* gc_required);
724
725 // Allocate a raw cons string object. Only the map field of the result is
726 // initialized.
727 void AllocateConsString(Register result,
728 Register scratch1,
729 Register scratch2,
730 Label* gc_required);
731 void AllocateAsciiConsString(Register result,
732 Register scratch1,
733 Register scratch2,
734 Label* gc_required);
735
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000736 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000737 // Support functions.
738
739 // Check if result is zero and op is negative.
740 void NegativeZeroTest(Register result, Register op, Label* then_label);
741
742 // Check if result is zero and op is negative in code using jump targets.
743 void NegativeZeroTest(CodeGenerator* cgen,
744 Register result,
745 Register op,
746 JumpTarget* then_target);
747
748 // Check if result is zero and any of op1 and op2 are negative.
749 // Register scratch is destroyed, and it must be different from op2.
750 void NegativeZeroTest(Register result, Register op1, Register op2,
751 Register scratch, Label* then_label);
752
753 // Try to get function prototype of a function and puts the value in
754 // the result register. Checks that the function really is a
755 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000756 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000757 // clobbered.
758 void TryGetFunctionPrototype(Register function,
759 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000760 Label* miss);
761
762 // Generates code for reporting that an illegal operation has
763 // occurred.
764 void IllegalOperation(int num_arguments);
765
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000766 // Picks out an array index from the hash field.
767 // Register use:
768 // hash - holds the index's hash. Clobbered.
769 // index - holds the overwritten index on exit.
770 void IndexFromHash(Register hash, Register index);
771
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000772 // Find the function context up the context chain.
773 void LoadContext(Register dst, int context_chain_length);
774
ager@chromium.org9085a012009-05-11 19:22:57 +0000775 // ---------------------------------------------------------------------------
776 // Runtime calls
777
778 // Call a code stub.
779 void CallStub(CodeStub* stub);
780
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000781 // Call a code stub and return the code object called. Try to generate
782 // the code if necessary. Do not perform a GC but instead return a retry
783 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000784 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000785
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000786 // Tail call a code stub (jump).
787 void TailCallStub(CodeStub* stub);
788
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000789 // Tail call a code stub (jump) and return the code object called. Try to
790 // generate the code if necessary. Do not perform a GC but instead return
791 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000792 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000793
ager@chromium.org9085a012009-05-11 19:22:57 +0000794 // Return from a code stub after popping its arguments.
795 void StubReturn(int argc);
796
797 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000798 void CallRuntime(Runtime::Function* f, int num_arguments);
799
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000800 // Call a runtime function, returning the CodeStub object called.
801 // Try to generate the stub code if necessary. Do not perform a GC
802 // but instead return a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000803 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
804 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000805
ager@chromium.org9085a012009-05-11 19:22:57 +0000806 // Convenience function: Same as above, but takes the fid instead.
807 void CallRuntime(Runtime::FunctionId id, int num_arguments);
808
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000809 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000810 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
811 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000812
ager@chromium.org5c838252010-02-19 08:53:10 +0000813 // Convenience function: call an external reference.
814 void CallExternalReference(const ExternalReference& ext,
815 int num_arguments);
816
ager@chromium.org9085a012009-05-11 19:22:57 +0000817 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000818 // Like JumpToExternalReference, but also takes care of passing the number
819 // of parameters.
820 void TailCallExternalReference(const ExternalReference& ext,
821 int num_arguments,
822 int result_size);
823
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000824 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
825 const ExternalReference& ext, int num_arguments, int result_size);
826
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000827 // Convenience function: tail call a runtime routine (jump).
828 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000829 int num_arguments,
830 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000831
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000832 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
833 int num_arguments,
834 int result_size);
835
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000836 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000837 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000838
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000839 // Jump to a runtime routine.
840 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
841 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000842
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000843 // Prepares stack to put arguments (aligns and so on).
844 // WIN64 calling convention requires to put the pointer to the return value
845 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
846 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
847 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
848 void PrepareCallApiFunction(int arg_stack_space);
849
850 // Calls an API function. Allocates HandleScope, extracts
851 // returned value from handle and propagates exceptions.
852 // Clobbers r12, r14, rbx and caller-save registers. Restores context.
853 // On return removes stack_space * kPointerSize (GCed).
854 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
855 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000856
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000857 // Before calling a C-function from generated code, align arguments on stack.
858 // After aligning the frame, arguments must be stored in esp[0], esp[4],
859 // etc., not pushed. The argument count assumes all arguments are word sized.
860 // The number of slots reserved for arguments depends on platform. On Windows
861 // stack slots are reserved for the arguments passed in registers. On other
862 // platforms stack slots are only reserved for the arguments actually passed
863 // on the stack.
864 void PrepareCallCFunction(int num_arguments);
865
866 // Calls a C function and cleans up the space for arguments allocated
867 // by PrepareCallCFunction. The called function is not allowed to trigger a
868 // garbage collection, since that might move the code and invalidate the
869 // return address (unless this is somehow accounted for by the called
870 // function).
871 void CallCFunction(ExternalReference function, int num_arguments);
872 void CallCFunction(Register function, int num_arguments);
873
874 // Calculate the number of stack slots to reserve for arguments when calling a
875 // C function.
876 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000877
878 // ---------------------------------------------------------------------------
879 // Utilities
880
881 void Ret();
882
ager@chromium.org9085a012009-05-11 19:22:57 +0000883 Handle<Object> CodeObject() { return code_object_; }
884
885
886 // ---------------------------------------------------------------------------
887 // StatsCounter support
888
889 void SetCounter(StatsCounter* counter, int value);
890 void IncrementCounter(StatsCounter* counter, int value);
891 void DecrementCounter(StatsCounter* counter, int value);
892
893
894 // ---------------------------------------------------------------------------
895 // Debugging
896
897 // Calls Abort(msg) if the condition cc is not satisfied.
898 // Use --debug_code to enable.
899 void Assert(Condition cc, const char* msg);
900
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000901 void AssertFastElements(Register elements);
902
ager@chromium.org9085a012009-05-11 19:22:57 +0000903 // Like Assert(), but always enabled.
904 void Check(Condition cc, const char* msg);
905
906 // Print a message to stdout and abort execution.
907 void Abort(const char* msg);
908
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000909 // Check that the stack is aligned.
910 void CheckStackAlignment();
911
ager@chromium.org9085a012009-05-11 19:22:57 +0000912 // Verify restrictions about code generated in stubs.
913 void set_generating_stub(bool value) { generating_stub_ = value; }
914 bool generating_stub() { return generating_stub_; }
915 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
916 bool allow_stub_calls() { return allow_stub_calls_; }
917
918 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000919 bool generating_stub_;
920 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000921
922 // Returns a register holding the smi value. The register MUST NOT be
923 // modified. It may be the "smi 1 constant" register.
924 Register GetSmiConstant(Smi* value);
925
926 // Moves the smi value to the destination register.
927 void LoadSmiConstant(Register dst, Smi* value);
928
ager@chromium.org5c838252010-02-19 08:53:10 +0000929 // This handle will be patched with the code object on installation.
930 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000931
932 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000933 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +0000934 void InvokePrologue(const ParameterCount& expected,
935 const ParameterCount& actual,
936 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000937 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000938 LabelType* done,
ager@chromium.org9085a012009-05-11 19:22:57 +0000939 InvokeFlag flag);
940
ager@chromium.org9085a012009-05-11 19:22:57 +0000941 // Activation support.
942 void EnterFrame(StackFrame::Type type);
943 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000944
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000945 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000946
947 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
948 // accessible via StackSpaceOperand.
949 void EnterExitFrameEpilogue(int arg_stack_space);
950
951 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000952
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000953 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000954 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000955 // Otherwise the address of the new-space top is loaded into scratch (if
956 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000957 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000958 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000959 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000960 // Update allocation top with value in result_end register.
961 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000962 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000963
964 // Helper for PopHandleScope. Allowed to perform a GC and returns
965 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
966 // possibly returns a failure object indicating an allocation failure.
967 Object* PopHandleScopeHelper(Register saved,
968 Register scratch,
969 bool gc_allowed);
ager@chromium.org9085a012009-05-11 19:22:57 +0000970};
971
972
ager@chromium.org4af710e2009-09-15 12:20:11 +0000973// The code patcher is used to patch (typically) small parts of code e.g. for
974// debugging and other types of instrumentation. When using the code patcher
975// the exact number of bytes specified must be emitted. Is not legal to emit
976// relocation information. If any of these constraints are violated it causes
977// an assertion.
978class CodePatcher {
979 public:
980 CodePatcher(byte* address, int size);
981 virtual ~CodePatcher();
982
983 // Macro assembler to emit code.
984 MacroAssembler* masm() { return &masm_; }
985
986 private:
987 byte* address_; // The address of the code being patched.
988 int size_; // Number of bytes of the expected patch size.
989 MacroAssembler masm_; // Macro assembler used to generate the code.
990};
991
992
ager@chromium.org9085a012009-05-11 19:22:57 +0000993// -----------------------------------------------------------------------------
994// Static helper functions.
995
996// Generate an Operand for loading a field from an object.
997static inline Operand FieldOperand(Register object, int offset) {
998 return Operand(object, offset - kHeapObjectTag);
999}
1000
1001
1002// Generate an Operand for loading an indexed field from an object.
1003static inline Operand FieldOperand(Register object,
1004 Register index,
1005 ScaleFactor scale,
1006 int offset) {
1007 return Operand(object, index, scale, offset - kHeapObjectTag);
1008}
1009
1010
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001011static inline Operand ContextOperand(Register context, int index) {
1012 return Operand(context, Context::SlotOffset(index));
1013}
1014
1015
1016static inline Operand GlobalObjectOperand() {
1017 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1018}
1019
1020
1021// Provides access to exit frame stack space (not GCed).
1022static inline Operand StackSpaceOperand(int index) {
1023#ifdef _WIN64
1024 const int kShaddowSpace = 4;
1025 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1026#else
1027 return Operand(rsp, index * kPointerSize);
1028#endif
1029}
1030
1031
1032
ager@chromium.org9085a012009-05-11 19:22:57 +00001033#ifdef GENERATED_CODE_COVERAGE
1034extern void LogGeneratedCodeCoverage(const char* file_line);
1035#define CODE_COVERAGE_STRINGIFY(x) #x
1036#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1037#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1038#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001039 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001040 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1041 masm->pushfd(); \
1042 masm->pushad(); \
1043 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001044 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001045 masm->pop(rax); \
1046 masm->popad(); \
1047 masm->popfd(); \
1048 } \
1049 masm->
1050#else
1051#define ACCESS_MASM(masm) masm->
1052#endif
1053
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001054// -----------------------------------------------------------------------------
1055// Template implementations.
1056
1057static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1058
1059
1060template <typename LabelType>
1061void MacroAssembler::SmiNeg(Register dst,
1062 Register src,
1063 LabelType* on_smi_result) {
1064 if (dst.is(src)) {
1065 ASSERT(!dst.is(kScratchRegister));
1066 movq(kScratchRegister, src);
1067 neg(dst); // Low 32 bits are retained as zero by negation.
1068 // Test if result is zero or Smi::kMinValue.
1069 cmpq(dst, kScratchRegister);
1070 j(not_equal, on_smi_result);
1071 movq(src, kScratchRegister);
1072 } else {
1073 movq(dst, src);
1074 neg(dst);
1075 cmpq(dst, src);
1076 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1077 j(not_equal, on_smi_result);
1078 }
1079}
1080
1081
1082template <typename LabelType>
1083void MacroAssembler::SmiAdd(Register dst,
1084 Register src1,
1085 Register src2,
1086 LabelType* on_not_smi_result) {
1087 ASSERT_NOT_NULL(on_not_smi_result);
1088 ASSERT(!dst.is(src2));
1089 if (dst.is(src1)) {
1090 movq(kScratchRegister, src1);
1091 addq(kScratchRegister, src2);
1092 j(overflow, on_not_smi_result);
1093 movq(dst, kScratchRegister);
1094 } else {
1095 movq(dst, src1);
1096 addq(dst, src2);
1097 j(overflow, on_not_smi_result);
1098 }
1099}
1100
1101
1102template <typename LabelType>
1103void MacroAssembler::SmiSub(Register dst,
1104 Register src1,
1105 Register src2,
1106 LabelType* on_not_smi_result) {
1107 ASSERT_NOT_NULL(on_not_smi_result);
1108 ASSERT(!dst.is(src2));
1109 if (dst.is(src1)) {
1110 cmpq(dst, src2);
1111 j(overflow, on_not_smi_result);
1112 subq(dst, src2);
1113 } else {
1114 movq(dst, src1);
1115 subq(dst, src2);
1116 j(overflow, on_not_smi_result);
1117 }
1118}
1119
1120
1121template <typename LabelType>
1122void MacroAssembler::SmiSub(Register dst,
1123 Register src1,
1124 const Operand& src2,
1125 LabelType* on_not_smi_result) {
1126 ASSERT_NOT_NULL(on_not_smi_result);
1127 if (dst.is(src1)) {
1128 movq(kScratchRegister, src2);
1129 cmpq(src1, kScratchRegister);
1130 j(overflow, on_not_smi_result);
1131 subq(src1, kScratchRegister);
1132 } else {
1133 movq(dst, src1);
1134 subq(dst, src2);
1135 j(overflow, on_not_smi_result);
1136 }
1137}
1138
1139
1140template <typename LabelType>
1141void MacroAssembler::SmiMul(Register dst,
1142 Register src1,
1143 Register src2,
1144 LabelType* on_not_smi_result) {
1145 ASSERT(!dst.is(src2));
1146 ASSERT(!dst.is(kScratchRegister));
1147 ASSERT(!src1.is(kScratchRegister));
1148 ASSERT(!src2.is(kScratchRegister));
1149
1150 if (dst.is(src1)) {
1151 NearLabel failure, zero_correct_result;
1152 movq(kScratchRegister, src1); // Create backup for later testing.
1153 SmiToInteger64(dst, src1);
1154 imul(dst, src2);
1155 j(overflow, &failure);
1156
1157 // Check for negative zero result. If product is zero, and one
1158 // argument is negative, go to slow case.
1159 NearLabel correct_result;
1160 testq(dst, dst);
1161 j(not_zero, &correct_result);
1162
1163 movq(dst, kScratchRegister);
1164 xor_(dst, src2);
1165 j(positive, &zero_correct_result); // Result was positive zero.
1166
1167 bind(&failure); // Reused failure exit, restores src1.
1168 movq(src1, kScratchRegister);
1169 jmp(on_not_smi_result);
1170
1171 bind(&zero_correct_result);
1172 xor_(dst, dst);
1173
1174 bind(&correct_result);
1175 } else {
1176 SmiToInteger64(dst, src1);
1177 imul(dst, src2);
1178 j(overflow, on_not_smi_result);
1179 // Check for negative zero result. If product is zero, and one
1180 // argument is negative, go to slow case.
1181 NearLabel correct_result;
1182 testq(dst, dst);
1183 j(not_zero, &correct_result);
1184 // One of src1 and src2 is zero, the check whether the other is
1185 // negative.
1186 movq(kScratchRegister, src1);
1187 xor_(kScratchRegister, src2);
1188 j(negative, on_not_smi_result);
1189 bind(&correct_result);
1190 }
1191}
1192
1193
1194template <typename LabelType>
1195void MacroAssembler::SmiTryAddConstant(Register dst,
1196 Register src,
1197 Smi* constant,
1198 LabelType* on_not_smi_result) {
1199 // Does not assume that src is a smi.
1200 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1201 ASSERT_EQ(0, kSmiTag);
1202 ASSERT(!dst.is(kScratchRegister));
1203 ASSERT(!src.is(kScratchRegister));
1204
1205 JumpIfNotSmi(src, on_not_smi_result);
1206 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1207 LoadSmiConstant(tmp, constant);
1208 addq(tmp, src);
1209 j(overflow, on_not_smi_result);
1210 if (dst.is(src)) {
1211 movq(dst, tmp);
1212 }
1213}
1214
1215
1216template <typename LabelType>
1217void MacroAssembler::SmiAddConstant(Register dst,
1218 Register src,
1219 Smi* constant,
1220 LabelType* on_not_smi_result) {
1221 if (constant->value() == 0) {
1222 if (!dst.is(src)) {
1223 movq(dst, src);
1224 }
1225 } else if (dst.is(src)) {
1226 ASSERT(!dst.is(kScratchRegister));
1227
1228 LoadSmiConstant(kScratchRegister, constant);
1229 addq(kScratchRegister, src);
1230 j(overflow, on_not_smi_result);
1231 movq(dst, kScratchRegister);
1232 } else {
1233 LoadSmiConstant(dst, constant);
1234 addq(dst, src);
1235 j(overflow, on_not_smi_result);
1236 }
1237}
1238
1239
1240template <typename LabelType>
1241void MacroAssembler::SmiSubConstant(Register dst,
1242 Register src,
1243 Smi* constant,
1244 LabelType* on_not_smi_result) {
1245 if (constant->value() == 0) {
1246 if (!dst.is(src)) {
1247 movq(dst, src);
1248 }
1249 } else if (dst.is(src)) {
1250 ASSERT(!dst.is(kScratchRegister));
1251 if (constant->value() == Smi::kMinValue) {
1252 // Subtracting min-value from any non-negative value will overflow.
1253 // We test the non-negativeness before doing the subtraction.
1254 testq(src, src);
1255 j(not_sign, on_not_smi_result);
1256 LoadSmiConstant(kScratchRegister, constant);
1257 subq(dst, kScratchRegister);
1258 } else {
1259 // Subtract by adding the negation.
1260 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1261 addq(kScratchRegister, dst);
1262 j(overflow, on_not_smi_result);
1263 movq(dst, kScratchRegister);
1264 }
1265 } else {
1266 if (constant->value() == Smi::kMinValue) {
1267 // Subtracting min-value from any non-negative value will overflow.
1268 // We test the non-negativeness before doing the subtraction.
1269 testq(src, src);
1270 j(not_sign, on_not_smi_result);
1271 LoadSmiConstant(dst, constant);
1272 // Adding and subtracting the min-value gives the same result, it only
1273 // differs on the overflow bit, which we don't check here.
1274 addq(dst, src);
1275 } else {
1276 // Subtract by adding the negation.
1277 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1278 addq(dst, src);
1279 j(overflow, on_not_smi_result);
1280 }
1281 }
1282}
1283
1284
1285template <typename LabelType>
1286void MacroAssembler::SmiDiv(Register dst,
1287 Register src1,
1288 Register src2,
1289 LabelType* on_not_smi_result) {
1290 ASSERT(!src1.is(kScratchRegister));
1291 ASSERT(!src2.is(kScratchRegister));
1292 ASSERT(!dst.is(kScratchRegister));
1293 ASSERT(!src2.is(rax));
1294 ASSERT(!src2.is(rdx));
1295 ASSERT(!src1.is(rdx));
1296
1297 // Check for 0 divisor (result is +/-Infinity).
1298 NearLabel positive_divisor;
1299 testq(src2, src2);
1300 j(zero, on_not_smi_result);
1301
1302 if (src1.is(rax)) {
1303 movq(kScratchRegister, src1);
1304 }
1305 SmiToInteger32(rax, src1);
1306 // We need to rule out dividing Smi::kMinValue by -1, since that would
1307 // overflow in idiv and raise an exception.
1308 // We combine this with negative zero test (negative zero only happens
1309 // when dividing zero by a negative number).
1310
1311 // We overshoot a little and go to slow case if we divide min-value
1312 // by any negative value, not just -1.
1313 NearLabel safe_div;
1314 testl(rax, Immediate(0x7fffffff));
1315 j(not_zero, &safe_div);
1316 testq(src2, src2);
1317 if (src1.is(rax)) {
1318 j(positive, &safe_div);
1319 movq(src1, kScratchRegister);
1320 jmp(on_not_smi_result);
1321 } else {
1322 j(negative, on_not_smi_result);
1323 }
1324 bind(&safe_div);
1325
1326 SmiToInteger32(src2, src2);
1327 // Sign extend src1 into edx:eax.
1328 cdq();
1329 idivl(src2);
1330 Integer32ToSmi(src2, src2);
1331 // Check that the remainder is zero.
1332 testl(rdx, rdx);
1333 if (src1.is(rax)) {
1334 NearLabel smi_result;
1335 j(zero, &smi_result);
1336 movq(src1, kScratchRegister);
1337 jmp(on_not_smi_result);
1338 bind(&smi_result);
1339 } else {
1340 j(not_zero, on_not_smi_result);
1341 }
1342 if (!dst.is(src1) && src1.is(rax)) {
1343 movq(src1, kScratchRegister);
1344 }
1345 Integer32ToSmi(dst, rax);
1346}
1347
1348
1349template <typename LabelType>
1350void MacroAssembler::SmiMod(Register dst,
1351 Register src1,
1352 Register src2,
1353 LabelType* on_not_smi_result) {
1354 ASSERT(!dst.is(kScratchRegister));
1355 ASSERT(!src1.is(kScratchRegister));
1356 ASSERT(!src2.is(kScratchRegister));
1357 ASSERT(!src2.is(rax));
1358 ASSERT(!src2.is(rdx));
1359 ASSERT(!src1.is(rdx));
1360 ASSERT(!src1.is(src2));
1361
1362 testq(src2, src2);
1363 j(zero, on_not_smi_result);
1364
1365 if (src1.is(rax)) {
1366 movq(kScratchRegister, src1);
1367 }
1368 SmiToInteger32(rax, src1);
1369 SmiToInteger32(src2, src2);
1370
1371 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1372 NearLabel safe_div;
1373 cmpl(rax, Immediate(Smi::kMinValue));
1374 j(not_equal, &safe_div);
1375 cmpl(src2, Immediate(-1));
1376 j(not_equal, &safe_div);
1377 // Retag inputs and go slow case.
1378 Integer32ToSmi(src2, src2);
1379 if (src1.is(rax)) {
1380 movq(src1, kScratchRegister);
1381 }
1382 jmp(on_not_smi_result);
1383 bind(&safe_div);
1384
1385 // Sign extend eax into edx:eax.
1386 cdq();
1387 idivl(src2);
1388 // Restore smi tags on inputs.
1389 Integer32ToSmi(src2, src2);
1390 if (src1.is(rax)) {
1391 movq(src1, kScratchRegister);
1392 }
1393 // Check for a negative zero result. If the result is zero, and the
1394 // dividend is negative, go slow to return a floating point negative zero.
1395 NearLabel smi_result;
1396 testl(rdx, rdx);
1397 j(not_zero, &smi_result);
1398 testq(src1, src1);
1399 j(negative, on_not_smi_result);
1400 bind(&smi_result);
1401 Integer32ToSmi(dst, rdx);
1402}
1403
1404
1405template <typename LabelType>
1406void MacroAssembler::SmiShiftLogicalRightConstant(
1407 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1408 // Logic right shift interprets its result as an *unsigned* number.
1409 if (dst.is(src)) {
1410 UNIMPLEMENTED(); // Not used.
1411 } else {
1412 movq(dst, src);
1413 if (shift_value == 0) {
1414 testq(dst, dst);
1415 j(negative, on_not_smi_result);
1416 }
1417 shr(dst, Immediate(shift_value + kSmiShift));
1418 shl(dst, Immediate(kSmiShift));
1419 }
1420}
1421
1422
1423template <typename LabelType>
1424void MacroAssembler::SmiShiftLogicalRight(Register dst,
1425 Register src1,
1426 Register src2,
1427 LabelType* on_not_smi_result) {
1428 ASSERT(!dst.is(kScratchRegister));
1429 ASSERT(!src1.is(kScratchRegister));
1430 ASSERT(!src2.is(kScratchRegister));
1431 ASSERT(!dst.is(rcx));
1432 NearLabel result_ok;
1433 if (src1.is(rcx) || src2.is(rcx)) {
1434 movq(kScratchRegister, rcx);
1435 }
1436 if (!dst.is(src1)) {
1437 movq(dst, src1);
1438 }
1439 SmiToInteger32(rcx, src2);
1440 orl(rcx, Immediate(kSmiShift));
1441 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1442 shl(dst, Immediate(kSmiShift));
1443 testq(dst, dst);
1444 if (src1.is(rcx) || src2.is(rcx)) {
1445 NearLabel positive_result;
1446 j(positive, &positive_result);
1447 if (src1.is(rcx)) {
1448 movq(src1, kScratchRegister);
1449 } else {
1450 movq(src2, kScratchRegister);
1451 }
1452 jmp(on_not_smi_result);
1453 bind(&positive_result);
1454 } else {
1455 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1456 }
1457}
1458
1459
1460template <typename LabelType>
1461void MacroAssembler::SelectNonSmi(Register dst,
1462 Register src1,
1463 Register src2,
1464 LabelType* on_not_smis) {
1465 ASSERT(!dst.is(kScratchRegister));
1466 ASSERT(!src1.is(kScratchRegister));
1467 ASSERT(!src2.is(kScratchRegister));
1468 ASSERT(!dst.is(src1));
1469 ASSERT(!dst.is(src2));
1470 // Both operands must not be smis.
1471#ifdef DEBUG
1472 if (allow_stub_calls()) { // Check contains a stub call.
1473 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1474 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1475 }
1476#endif
1477 ASSERT_EQ(0, kSmiTag);
1478 ASSERT_EQ(0, Smi::FromInt(0));
1479 movl(kScratchRegister, Immediate(kSmiTagMask));
1480 and_(kScratchRegister, src1);
1481 testl(kScratchRegister, src2);
1482 // If non-zero then both are smis.
1483 j(not_zero, on_not_smis);
1484
1485 // Exactly one operand is a smi.
1486 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1487 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1488 subq(kScratchRegister, Immediate(1));
1489 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1490 movq(dst, src1);
1491 xor_(dst, src2);
1492 and_(dst, kScratchRegister);
1493 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1494 xor_(dst, src1);
1495 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1496}
1497
1498
1499template <typename LabelType>
1500void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1501 ASSERT_EQ(0, kSmiTag);
1502 Condition smi = CheckSmi(src);
1503 j(smi, on_smi);
1504}
1505
1506
1507template <typename LabelType>
1508void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1509 Condition smi = CheckSmi(src);
1510 j(NegateCondition(smi), on_not_smi);
1511}
1512
1513
1514template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001515void MacroAssembler::JumpUnlessNonNegativeSmi(
1516 Register src, LabelType* on_not_smi_or_negative) {
1517 Condition non_negative_smi = CheckNonNegativeSmi(src);
1518 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001519}
1520
1521
1522template <typename LabelType>
1523void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1524 Smi* constant,
1525 LabelType* on_equals) {
1526 SmiCompare(src, constant);
1527 j(equal, on_equals);
1528}
1529
1530
1531template <typename LabelType>
1532void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1533 LabelType* on_invalid) {
1534 Condition is_valid = CheckInteger32ValidSmiValue(src);
1535 j(NegateCondition(is_valid), on_invalid);
1536}
1537
1538
1539template <typename LabelType>
1540void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1541 LabelType* on_invalid) {
1542 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1543 j(NegateCondition(is_valid), on_invalid);
1544}
1545
1546
1547template <typename LabelType>
1548void MacroAssembler::JumpIfNotBothSmi(Register src1,
1549 Register src2,
1550 LabelType* on_not_both_smi) {
1551 Condition both_smi = CheckBothSmi(src1, src2);
1552 j(NegateCondition(both_smi), on_not_both_smi);
1553}
1554
1555
1556template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001557void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1558 Register src2,
1559 LabelType* on_not_both_smi) {
1560 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001561 j(NegateCondition(both_smi), on_not_both_smi);
1562}
1563
1564
1565template <typename LabelType>
1566void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1567 Register second_object,
1568 Register scratch1,
1569 Register scratch2,
1570 LabelType* on_fail) {
1571 // Check that both objects are not smis.
1572 Condition either_smi = CheckEitherSmi(first_object, second_object);
1573 j(either_smi, on_fail);
1574
1575 // Load instance type for both strings.
1576 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1577 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1578 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1579 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1580
1581 // Check that both are flat ascii strings.
1582 ASSERT(kNotStringTag != 0);
1583 const int kFlatAsciiStringMask =
1584 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1585 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1586
1587 andl(scratch1, Immediate(kFlatAsciiStringMask));
1588 andl(scratch2, Immediate(kFlatAsciiStringMask));
1589 // Interleave the bits to check both scratch1 and scratch2 in one test.
1590 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1591 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1592 cmpl(scratch1,
1593 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1594 j(not_equal, on_fail);
1595}
1596
1597
1598template <typename LabelType>
1599void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1600 Register instance_type,
1601 Register scratch,
1602 LabelType *failure) {
1603 if (!scratch.is(instance_type)) {
1604 movl(scratch, instance_type);
1605 }
1606
1607 const int kFlatAsciiStringMask =
1608 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1609
1610 andl(scratch, Immediate(kFlatAsciiStringMask));
1611 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1612 j(not_equal, failure);
1613}
1614
1615
1616template <typename LabelType>
1617void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1618 Register first_object_instance_type,
1619 Register second_object_instance_type,
1620 Register scratch1,
1621 Register scratch2,
1622 LabelType* on_fail) {
1623 // Load instance type for both strings.
1624 movq(scratch1, first_object_instance_type);
1625 movq(scratch2, second_object_instance_type);
1626
1627 // Check that both are flat ascii strings.
1628 ASSERT(kNotStringTag != 0);
1629 const int kFlatAsciiStringMask =
1630 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1631 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1632
1633 andl(scratch1, Immediate(kFlatAsciiStringMask));
1634 andl(scratch2, Immediate(kFlatAsciiStringMask));
1635 // Interleave the bits to check both scratch1 and scratch2 in one test.
1636 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1637 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1638 cmpl(scratch1,
1639 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1640 j(not_equal, on_fail);
1641}
1642
1643
1644template <typename LabelType>
1645void MacroAssembler::InNewSpace(Register object,
1646 Register scratch,
1647 Condition cc,
1648 LabelType* branch) {
1649 if (Serializer::enabled()) {
1650 // Can't do arithmetic on external references if it might get serialized.
1651 // The mask isn't really an address. We load it as an external reference in
1652 // case the size of the new space is different between the snapshot maker
1653 // and the running system.
1654 if (scratch.is(object)) {
1655 movq(kScratchRegister, ExternalReference::new_space_mask());
1656 and_(scratch, kScratchRegister);
1657 } else {
1658 movq(scratch, ExternalReference::new_space_mask());
1659 and_(scratch, object);
1660 }
1661 movq(kScratchRegister, ExternalReference::new_space_start());
1662 cmpq(scratch, kScratchRegister);
1663 j(cc, branch);
1664 } else {
1665 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1666 intptr_t new_space_start =
1667 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1668 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1669 if (scratch.is(object)) {
1670 addq(scratch, kScratchRegister);
1671 } else {
1672 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1673 }
1674 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1675 j(cc, branch);
1676 }
1677}
1678
1679
1680template <typename LabelType>
1681void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1682 const ParameterCount& actual,
1683 Handle<Code> code_constant,
1684 Register code_register,
1685 LabelType* done,
1686 InvokeFlag flag) {
1687 bool definitely_matches = false;
1688 NearLabel invoke;
1689 if (expected.is_immediate()) {
1690 ASSERT(actual.is_immediate());
1691 if (expected.immediate() == actual.immediate()) {
1692 definitely_matches = true;
1693 } else {
1694 Set(rax, actual.immediate());
1695 if (expected.immediate() ==
1696 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1697 // Don't worry about adapting arguments for built-ins that
1698 // don't want that done. Skip adaption code by making it look
1699 // like we have a match between expected and actual number of
1700 // arguments.
1701 definitely_matches = true;
1702 } else {
1703 Set(rbx, expected.immediate());
1704 }
1705 }
1706 } else {
1707 if (actual.is_immediate()) {
1708 // Expected is in register, actual is immediate. This is the
1709 // case when we invoke function values without going through the
1710 // IC mechanism.
1711 cmpq(expected.reg(), Immediate(actual.immediate()));
1712 j(equal, &invoke);
1713 ASSERT(expected.reg().is(rbx));
1714 Set(rax, actual.immediate());
1715 } else if (!expected.reg().is(actual.reg())) {
1716 // Both expected and actual are in (different) registers. This
1717 // is the case when we invoke functions using call and apply.
1718 cmpq(expected.reg(), actual.reg());
1719 j(equal, &invoke);
1720 ASSERT(actual.reg().is(rax));
1721 ASSERT(expected.reg().is(rbx));
1722 }
1723 }
1724
1725 if (!definitely_matches) {
1726 Handle<Code> adaptor =
1727 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1728 if (!code_constant.is_null()) {
1729 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1730 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1731 } else if (!code_register.is(rdx)) {
1732 movq(rdx, code_register);
1733 }
1734
1735 if (flag == CALL_FUNCTION) {
1736 Call(adaptor, RelocInfo::CODE_TARGET);
1737 jmp(done);
1738 } else {
1739 Jump(adaptor, RelocInfo::CODE_TARGET);
1740 }
1741 bind(&invoke);
1742 }
1743}
1744
ager@chromium.org9085a012009-05-11 19:22:57 +00001745
1746} } // namespace v8::internal
1747
1748#endif // V8_X64_MACRO_ASSEMBLER_X64_H_