blob: 503c7f2c799870ca11c3346c215713952ce1b5f5 [file] [log] [blame]
ager@chromium.orga1645e22009-09-09 19:27:10 +00001// Copyright 2009 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
104 // represented as a Smi. All registers are clobbered by the
105 // operation. RecordWrite filters out smis so it does not update the
106 // 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
125 // the elements array represented as a Smi.
126 // 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 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000140 // Stack limit support
141
142 // Do simple test for stack overflow. This doesn't handle an overflow.
143 void StackLimitCheck(Label* on_stack_limit_hit);
144
145 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000146 // Activation frames
147
148 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
149 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
150
151 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
152 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
153
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000154 // Enter specific kind of exit frame; either in normal or
155 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000156 // sets up the number of arguments in register rdi and the pointer
157 // to the first argument in register rsi.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000158 void EnterExitFrame(int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000159
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000160 void EnterApiExitFrame(int stack_space,
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000161 int argc,
162 int result_size = 1);
163
ager@chromium.orga1645e22009-09-09 19:27:10 +0000164 // Leave the current exit frame. Expects/provides the return value in
165 // register rax:rdx (untouched) and the pointer to the first
166 // argument in register rsi.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000167 void LeaveExitFrame(int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000168
169
170 // ---------------------------------------------------------------------------
171 // JavaScript invokes
172
173 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000174 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000175 const ParameterCount& expected,
176 const ParameterCount& actual,
177 InvokeFlag flag);
178
179 void InvokeCode(Handle<Code> code,
180 const ParameterCount& expected,
181 const ParameterCount& actual,
182 RelocInfo::Mode rmode,
183 InvokeFlag flag);
184
185 // Invoke the JavaScript function in the given register. Changes the
186 // current context to the context in the function before invoking.
187 void InvokeFunction(Register function,
188 const ParameterCount& actual,
189 InvokeFlag flag);
190
ager@chromium.org5c838252010-02-19 08:53:10 +0000191 void InvokeFunction(JSFunction* function,
192 const ParameterCount& actual,
193 InvokeFlag flag);
194
ager@chromium.org9085a012009-05-11 19:22:57 +0000195 // Invoke specified builtin JavaScript function. Adds an entry to
196 // the unresolved list if the name does not resolve.
197 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
198
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000199 // Store the function for the given builtin in the target register.
200 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
201
ager@chromium.org9085a012009-05-11 19:22:57 +0000202 // Store the code object for the given builtin in the target register.
203 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
204
ager@chromium.org4af710e2009-09-15 12:20:11 +0000205
206 // ---------------------------------------------------------------------------
207 // Smi tagging, untagging and operations on tagged smis.
208
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000209 void InitializeSmiConstantRegister() {
210 movq(kSmiConstantRegister,
211 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
212 RelocInfo::NONE);
213 }
214
ager@chromium.org4af710e2009-09-15 12:20:11 +0000215 // Conversions between tagged smi values and non-tagged integer values.
216
217 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000218 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000219 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000220 void Integer32ToSmi(Register dst, Register src);
221
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000222 // Stores an integer32 value into a memory field that already holds a smi.
223 void Integer32ToSmiField(const Operand& dst, Register src);
224
ager@chromium.org4af710e2009-09-15 12:20:11 +0000225 // Adds constant to src and tags the result as a smi.
226 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000227 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000228
229 // Convert smi to 32-bit integer. I.e., not sign extended into
230 // high 32 bits of destination.
231 void SmiToInteger32(Register dst, Register src);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000232 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000233
234 // Convert smi to 64-bit integer (sign extended if necessary).
235 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000236 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000237
238 // Multiply a positive smi's integer value by a power of two.
239 // Provides result as 64-bit integer value.
240 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
241 Register src,
242 int power);
243
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000244 // Divide a positive smi's integer value by a power of two.
245 // Provides result as 32-bit integer value.
246 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
247 Register src,
248 int power);
249
250
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000251 // Simple comparison of smis.
252 void SmiCompare(Register dst, Register src);
253 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000254 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000255 void SmiCompare(const Operand& dst, Register src);
256 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000257 // Compare the int32 in src register to the value of the smi stored at dst.
258 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000259 // Sets sign and zero flags depending on value of smi in register.
260 void SmiTest(Register src);
261
ager@chromium.org4af710e2009-09-15 12:20:11 +0000262 // Functions performing a check on a known or potential smi. Returns
263 // a condition that is satisfied if the check is successful.
264
265 // Is the value a tagged smi.
266 Condition CheckSmi(Register src);
267
ager@chromium.org4af710e2009-09-15 12:20:11 +0000268 // Is the value a positive tagged smi.
269 Condition CheckPositiveSmi(Register src);
270
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000271 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000272 Condition CheckBothSmi(Register first, Register second);
273
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000274 // Are both values tagged smis.
275 Condition CheckBothPositiveSmi(Register first, Register second);
276
277 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000278 Condition CheckEitherSmi(Register first,
279 Register second,
280 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000281
ager@chromium.org4af710e2009-09-15 12:20:11 +0000282 // Is the value the minimum smi value (since we are using
283 // two's complement numbers, negating the value is known to yield
284 // a non-smi value).
285 Condition CheckIsMinSmi(Register src);
286
ager@chromium.org4af710e2009-09-15 12:20:11 +0000287 // Checks whether an 32-bit integer value is a valid for conversion
288 // to a smi.
289 Condition CheckInteger32ValidSmiValue(Register src);
290
ager@chromium.org3811b432009-10-28 14:53:37 +0000291 // Checks whether an 32-bit unsigned integer value is a valid for
292 // conversion to a smi.
293 Condition CheckUInteger32ValidSmiValue(Register src);
294
ager@chromium.org4af710e2009-09-15 12:20:11 +0000295 // Test-and-jump functions. Typically combines a check function
296 // above with a conditional jump.
297
298 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000299 template <typename LabelType>
300 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000301
ager@chromium.org3811b432009-10-28 14:53:37 +0000302 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000303 template <typename LabelType>
304 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000305
ager@chromium.org4af710e2009-09-15 12:20:11 +0000306 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000307 template <typename LabelType>
308 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000309
310 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000311 template <typename LabelType>
312 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000313
314 // Jump to label if the value is not a positive tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000315 template <typename LabelType>
316 void JumpIfNotPositiveSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000317
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000318 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000319 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000320 template <typename LabelType>
321 void JumpIfSmiEqualsConstant(Register src,
322 Smi* constant,
323 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000324
ager@chromium.org4af710e2009-09-15 12:20:11 +0000325 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000326 template <typename LabelType>
327 void JumpIfNotBothSmi(Register src1,
328 Register src2,
329 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000330
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000331 // Jump if either or both register are not positive smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000332 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000333 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000334 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000335
ager@chromium.org4af710e2009-09-15 12:20:11 +0000336 // Operations on tagged smi values.
337
338 // Smis represent a subset of integers. The subset is always equivalent to
339 // a two's complement interpretation of a fixed number of bits.
340
341 // Optimistically adds an integer constant to a supposed smi.
342 // If the src is not a smi, or the result is not a smi, jump to
343 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000344 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000345 void SmiTryAddConstant(Register dst,
346 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000347 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000348 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000349
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000350 // Add an integer constant to a tagged smi, giving a tagged smi as result.
351 // No overflow testing on the result is done.
352 void SmiAddConstant(Register dst, Register src, Smi* constant);
353
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000354 // Add an integer constant to a tagged smi, giving a tagged smi as result.
355 // No overflow testing on the result is done.
356 void SmiAddConstant(const Operand& dst, Smi* constant);
357
ager@chromium.org4af710e2009-09-15 12:20:11 +0000358 // Add an integer constant to a tagged smi, giving a tagged smi as result,
359 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000360 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000361 void SmiAddConstant(Register dst,
362 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000363 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000364 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000365
366 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000367 // result. No testing on the result is done. Sets the N and Z flags
368 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000369 void SmiSubConstant(Register dst, Register src, Smi* constant);
370
371 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000372 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000373 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000374 void SmiSubConstant(Register dst,
375 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000376 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000377 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000378
379 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000380 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000381 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000382 void SmiNeg(Register dst,
383 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000384 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000385
386 // Adds smi values and return the result as a smi.
387 // If dst is src1, then src1 will be destroyed, even if
388 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000389 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000390 void SmiAdd(Register dst,
391 Register src1,
392 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000393 LabelType* on_not_smi_result);
394
395 void SmiAdd(Register dst,
396 Register src1,
397 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000398
399 // Subtracts smi values and return the result as a smi.
400 // If dst is src1, then src1 will be destroyed, even if
401 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000402 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000403 void SmiSub(Register dst,
404 Register src1,
405 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000406 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000407
ager@chromium.orgac091b72010-05-05 07:34:42 +0000408 void SmiSub(Register dst,
409 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000410 Register src2);
411
412 template <typename LabelType>
413 void SmiSub(Register dst,
414 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000415 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000416 LabelType* on_not_smi_result);
417
418 void SmiSub(Register dst,
419 Register src1,
420 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000421
ager@chromium.org4af710e2009-09-15 12:20:11 +0000422 // Multiplies smi values and return the result as a smi,
423 // if possible.
424 // If dst is src1, then src1 will be destroyed, even if
425 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000426 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000427 void SmiMul(Register dst,
428 Register src1,
429 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000430 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000431
432 // Divides one smi by another and returns the quotient.
433 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000434 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000435 void SmiDiv(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 remainder.
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 SmiMod(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 // Bitwise operations.
449 void SmiNot(Register dst, Register src);
450 void SmiAnd(Register dst, Register src1, Register src2);
451 void SmiOr(Register dst, Register src1, Register src2);
452 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000453 void SmiAndConstant(Register dst, Register src1, Smi* constant);
454 void SmiOrConstant(Register dst, Register src1, Smi* constant);
455 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000456
457 void SmiShiftLeftConstant(Register dst,
458 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000459 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000460 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000461 void SmiShiftLogicalRightConstant(Register dst,
462 Register src,
463 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000464 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000465 void SmiShiftArithmeticRightConstant(Register dst,
466 Register src,
467 int shift_value);
468
469 // Shifts a smi value to the left, and returns the result if that is a smi.
470 // Uses and clobbers rcx, so dst may not be rcx.
471 void SmiShiftLeft(Register dst,
472 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000473 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000474 // Shifts a smi value to the right, shifting in zero bits at the top, and
475 // returns the unsigned intepretation of the result if that is a smi.
476 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000477 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000478 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000479 Register src1,
480 Register src2,
481 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000482 // Shifts a smi value to the right, sign extending the top, and
483 // returns the signed intepretation of the result. That will always
484 // be a valid smi value, since it's numerically smaller than the
485 // original.
486 // Uses and clobbers rcx, so dst may not be rcx.
487 void SmiShiftArithmeticRight(Register dst,
488 Register src1,
489 Register src2);
490
491 // Specialized operations
492
493 // Select the non-smi register of two registers where exactly one is a
494 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000495 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000496 void SelectNonSmi(Register dst,
497 Register src1,
498 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000499 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000500
501 // Converts, if necessary, a smi to a combination of number and
502 // multiplier to be used as a scaled index.
503 // The src register contains a *positive* smi value. The shift is the
504 // power of two to multiply the index value by (e.g.
505 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
506 // The returned index register may be either src or dst, depending
507 // on what is most efficient. If src and dst are different registers,
508 // src is always unchanged.
509 SmiIndex SmiToIndex(Register dst, Register src, int shift);
510
511 // Converts a positive smi to a negative index.
512 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
513
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000514 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000515 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000516 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000517 }
518
519 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000520 Register constant = GetSmiConstant(source);
521 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000522 }
523
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000524 void Push(Smi* smi);
525 void Test(const Operand& dst, Smi* source);
526
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000527 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000528 // String macros.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000529 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000530 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
531 Register second_object,
532 Register scratch1,
533 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000534 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000535
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000536 // Check whether the instance type represents a flat ascii string. Jump to the
537 // label if not. If the instance type can be scratched specify same register
538 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000539 template <typename LabelType>
540 void JumpIfInstanceTypeIsNotSequentialAscii(
541 Register instance_type,
542 Register scratch,
543 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000544
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000545 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000546 void JumpIfBothInstanceTypesAreNotSequentialAscii(
547 Register first_object_instance_type,
548 Register second_object_instance_type,
549 Register scratch1,
550 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000551 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000552
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000553 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000554 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000555
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000556 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000557 void Set(Register dst, int64_t x);
558 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000559
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000560 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000561 void Move(Register dst, Handle<Object> source);
562 void Move(const Operand& dst, Handle<Object> source);
563 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000564 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000565 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000566
567 // Emit code to discard a non-negative number of pointer-sized elements
568 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000569 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000570
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000571 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000572
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000573 // Control Flow
574 void Jump(Address destination, RelocInfo::Mode rmode);
575 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000576 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
577
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000578 void Call(Address destination, RelocInfo::Mode rmode);
579 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000580 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000581
ager@chromium.org9085a012009-05-11 19:22:57 +0000582 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000583 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000584 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000585 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000586 void CmpObjectType(Register heap_object, InstanceType type, Register map);
587
588 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000589 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000590 void CmpInstanceType(Register map, InstanceType type);
591
ager@chromium.org5c838252010-02-19 08:53:10 +0000592 // Check if the map of an object is equal to a specified map and
593 // branch to label if not. Skip the smi check if not required
594 // (object is known to be a heap object)
595 void CheckMap(Register obj,
596 Handle<Map> map,
597 Label* fail,
598 bool is_heap_object);
599
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000600 // Check if the object in register heap_object is a string. Afterwards the
601 // register map contains the object map and the register instance_type
602 // contains the instance_type. The registers map and instance_type can be the
603 // same in which case it contains the instance type afterwards. Either of the
604 // registers map and instance_type can be the same as heap_object.
605 Condition IsObjectStringType(Register heap_object,
606 Register map,
607 Register instance_type);
608
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000609 // FCmp compares and pops the two values on top of the FPU stack.
610 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000611 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
612 void FCmp();
613
ager@chromium.org5c838252010-02-19 08:53:10 +0000614 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000615 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000616
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000617 // Abort execution if argument is a smi. Used in debug code.
618 void AbortIfSmi(Register object);
619
lrn@chromium.org25156de2010-04-06 13:10:27 +0000620 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000621 void AbortIfNotSmi(Register object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000622
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000623 // Abort execution if argument is not the root value with the given index.
624 void AbortIfNotRootValue(Register src,
625 Heap::RootListIndex root_value_index,
626 const char* message);
627
ager@chromium.org9085a012009-05-11 19:22:57 +0000628 // ---------------------------------------------------------------------------
629 // Exception handling
630
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000631 // Push a new try handler and link into try handler chain. The return
632 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000633 void PushTryHandler(CodeLocation try_location, HandlerType type);
634
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000635 // Unlink the stack handler on top of the stack from the try handler chain.
636 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000637
638 // ---------------------------------------------------------------------------
639 // Inline caching support
640
ager@chromium.org9085a012009-05-11 19:22:57 +0000641 // Generate code for checking access rights - used for security checks
642 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000643 // is left untouched, but the scratch register and kScratchRegister,
644 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000645 void CheckAccessGlobalProxy(Register holder_reg,
646 Register scratch,
647 Label* miss);
648
649
650 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000651 // Allocation support
652
653 // Allocate an object in new space. If the new space is exhausted control
654 // continues at the gc_required label. The allocated object is returned in
655 // result and end of the new object is returned in result_end. The register
656 // scratch can be passed as no_reg in which case an additional object
657 // reference will be added to the reloc info. The returned pointers in result
658 // and result_end have not yet been tagged as heap objects. If
659 // result_contains_top_on_entry is true the content of result is known to be
660 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000661 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000662 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000663 void AllocateInNewSpace(int object_size,
664 Register result,
665 Register result_end,
666 Register scratch,
667 Label* gc_required,
668 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000669
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000670 void AllocateInNewSpace(int header_size,
671 ScaleFactor element_size,
672 Register element_count,
673 Register result,
674 Register result_end,
675 Register scratch,
676 Label* gc_required,
677 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000678
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000679 void AllocateInNewSpace(Register object_size,
680 Register result,
681 Register result_end,
682 Register scratch,
683 Label* gc_required,
684 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000685
686 // Undo allocation in new space. The object passed and objects allocated after
687 // it will no longer be allocated. Make sure that no pointers are left to the
688 // object(s) no longer allocated as they would be invalid when allocation is
689 // un-done.
690 void UndoAllocationInNewSpace(Register object);
691
ager@chromium.org3811b432009-10-28 14:53:37 +0000692 // Allocate a heap number in new space with undefined value. Returns
693 // tagged pointer in result register, or jumps to gc_required if new
694 // space is full.
695 void AllocateHeapNumber(Register result,
696 Register scratch,
697 Label* gc_required);
698
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000699 // Allocate a sequential string. All the header fields of the string object
700 // are initialized.
701 void AllocateTwoByteString(Register result,
702 Register length,
703 Register scratch1,
704 Register scratch2,
705 Register scratch3,
706 Label* gc_required);
707 void AllocateAsciiString(Register result,
708 Register length,
709 Register scratch1,
710 Register scratch2,
711 Register scratch3,
712 Label* gc_required);
713
714 // Allocate a raw cons string object. Only the map field of the result is
715 // initialized.
716 void AllocateConsString(Register result,
717 Register scratch1,
718 Register scratch2,
719 Label* gc_required);
720 void AllocateAsciiConsString(Register result,
721 Register scratch1,
722 Register scratch2,
723 Label* gc_required);
724
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000725 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000726 // Support functions.
727
728 // Check if result is zero and op is negative.
729 void NegativeZeroTest(Register result, Register op, Label* then_label);
730
731 // Check if result is zero and op is negative in code using jump targets.
732 void NegativeZeroTest(CodeGenerator* cgen,
733 Register result,
734 Register op,
735 JumpTarget* then_target);
736
737 // Check if result is zero and any of op1 and op2 are negative.
738 // Register scratch is destroyed, and it must be different from op2.
739 void NegativeZeroTest(Register result, Register op1, Register op2,
740 Register scratch, Label* then_label);
741
742 // Try to get function prototype of a function and puts the value in
743 // the result register. Checks that the function really is a
744 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000745 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000746 // clobbered.
747 void TryGetFunctionPrototype(Register function,
748 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000749 Label* miss);
750
751 // Generates code for reporting that an illegal operation has
752 // occurred.
753 void IllegalOperation(int num_arguments);
754
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000755 // Picks out an array index from the hash field.
756 // Register use:
757 // hash - holds the index's hash. Clobbered.
758 // index - holds the overwritten index on exit.
759 void IndexFromHash(Register hash, Register index);
760
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000761 // Find the function context up the context chain.
762 void LoadContext(Register dst, int context_chain_length);
763
ager@chromium.org9085a012009-05-11 19:22:57 +0000764 // ---------------------------------------------------------------------------
765 // Runtime calls
766
767 // Call a code stub.
768 void CallStub(CodeStub* stub);
769
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000770 // Call a code stub and return the code object called. Try to generate
771 // the code if necessary. Do not perform a GC but instead return a retry
772 // after GC failure.
773 Object* TryCallStub(CodeStub* stub);
774
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000775 // Tail call a code stub (jump).
776 void TailCallStub(CodeStub* stub);
777
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000778 // Tail call a code stub (jump) and return the code object called. Try to
779 // generate the code if necessary. Do not perform a GC but instead return
780 // a retry after GC failure.
781 Object* TryTailCallStub(CodeStub* stub);
782
ager@chromium.org9085a012009-05-11 19:22:57 +0000783 // Return from a code stub after popping its arguments.
784 void StubReturn(int argc);
785
786 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000787 void CallRuntime(Runtime::Function* f, int num_arguments);
788
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000789 // Call a runtime function, returning the CodeStub object called.
790 // Try to generate the stub code if necessary. Do not perform a GC
791 // but instead return a retry after GC failure.
792 Object* TryCallRuntime(Runtime::Function* f, int num_arguments);
793
ager@chromium.org9085a012009-05-11 19:22:57 +0000794 // Convenience function: Same as above, but takes the fid instead.
795 void CallRuntime(Runtime::FunctionId id, int num_arguments);
796
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000797 // Convenience function: Same as above, but takes the fid instead.
798 Object* TryCallRuntime(Runtime::FunctionId id, int num_arguments);
799
ager@chromium.org5c838252010-02-19 08:53:10 +0000800 // Convenience function: call an external reference.
801 void CallExternalReference(const ExternalReference& ext,
802 int num_arguments);
803
ager@chromium.org9085a012009-05-11 19:22:57 +0000804 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000805 // Like JumpToExternalReference, but also takes care of passing the number
806 // of parameters.
807 void TailCallExternalReference(const ExternalReference& ext,
808 int num_arguments,
809 int result_size);
810
811 // Convenience function: tail call a runtime routine (jump).
812 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000813 int num_arguments,
814 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000815
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000816 void PushHandleScope(Register scratch);
817
818 // Pops a handle scope using the specified scratch register and
819 // ensuring that saved register is left unchanged.
820 void PopHandleScope(Register saved, Register scratch);
821
822 // As PopHandleScope, but does not perform a GC. Instead, returns a
823 // retry after GC failure object if GC is necessary.
824 Object* TryPopHandleScope(Register saved, Register scratch);
825
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000826 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000827 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000828
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000829 // Before calling a C-function from generated code, align arguments on stack.
830 // After aligning the frame, arguments must be stored in esp[0], esp[4],
831 // etc., not pushed. The argument count assumes all arguments are word sized.
832 // The number of slots reserved for arguments depends on platform. On Windows
833 // stack slots are reserved for the arguments passed in registers. On other
834 // platforms stack slots are only reserved for the arguments actually passed
835 // on the stack.
836 void PrepareCallCFunction(int num_arguments);
837
838 // Calls a C function and cleans up the space for arguments allocated
839 // by PrepareCallCFunction. The called function is not allowed to trigger a
840 // garbage collection, since that might move the code and invalidate the
841 // return address (unless this is somehow accounted for by the called
842 // function).
843 void CallCFunction(ExternalReference function, int num_arguments);
844 void CallCFunction(Register function, int num_arguments);
845
846 // Calculate the number of stack slots to reserve for arguments when calling a
847 // C function.
848 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000849
850 // ---------------------------------------------------------------------------
851 // Utilities
852
853 void Ret();
854
ager@chromium.org9085a012009-05-11 19:22:57 +0000855 Handle<Object> CodeObject() { return code_object_; }
856
857
858 // ---------------------------------------------------------------------------
859 // StatsCounter support
860
861 void SetCounter(StatsCounter* counter, int value);
862 void IncrementCounter(StatsCounter* counter, int value);
863 void DecrementCounter(StatsCounter* counter, int value);
864
865
866 // ---------------------------------------------------------------------------
867 // Debugging
868
869 // Calls Abort(msg) if the condition cc is not satisfied.
870 // Use --debug_code to enable.
871 void Assert(Condition cc, const char* msg);
872
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000873 void AssertFastElements(Register elements);
874
ager@chromium.org9085a012009-05-11 19:22:57 +0000875 // Like Assert(), but always enabled.
876 void Check(Condition cc, const char* msg);
877
878 // Print a message to stdout and abort execution.
879 void Abort(const char* msg);
880
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000881 // Check that the stack is aligned.
882 void CheckStackAlignment();
883
ager@chromium.org9085a012009-05-11 19:22:57 +0000884 // Verify restrictions about code generated in stubs.
885 void set_generating_stub(bool value) { generating_stub_ = value; }
886 bool generating_stub() { return generating_stub_; }
887 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
888 bool allow_stub_calls() { return allow_stub_calls_; }
889
890 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000891 bool generating_stub_;
892 bool allow_stub_calls_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000893
894 // Returns a register holding the smi value. The register MUST NOT be
895 // modified. It may be the "smi 1 constant" register.
896 Register GetSmiConstant(Smi* value);
897
898 // Moves the smi value to the destination register.
899 void LoadSmiConstant(Register dst, Smi* value);
900
ager@chromium.org5c838252010-02-19 08:53:10 +0000901 // This handle will be patched with the code object on installation.
902 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000903
904 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000905 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +0000906 void InvokePrologue(const ParameterCount& expected,
907 const ParameterCount& actual,
908 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000909 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000910 LabelType* done,
ager@chromium.org9085a012009-05-11 19:22:57 +0000911 InvokeFlag flag);
912
ager@chromium.org9085a012009-05-11 19:22:57 +0000913 // Activation support.
914 void EnterFrame(StackFrame::Type type);
915 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000916
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000917 void EnterExitFramePrologue(bool save_rax);
918 void EnterExitFrameEpilogue(int result_size, int argc);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000919
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000920 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +0000921 // Loads the top of new-space into the result register.
922 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
923 // already contains the top of new-space, and scratch is invalid.
924 // Otherwise the address of the new-space top is loaded into scratch (if
925 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000926 void LoadAllocationTopHelper(Register result,
927 Register result_end,
928 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000929 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000930 // Update allocation top with value in result_end register.
931 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000932 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000933
934 // Helper for PopHandleScope. Allowed to perform a GC and returns
935 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
936 // possibly returns a failure object indicating an allocation failure.
937 Object* PopHandleScopeHelper(Register saved,
938 Register scratch,
939 bool gc_allowed);
ager@chromium.org9085a012009-05-11 19:22:57 +0000940};
941
942
ager@chromium.org4af710e2009-09-15 12:20:11 +0000943// The code patcher is used to patch (typically) small parts of code e.g. for
944// debugging and other types of instrumentation. When using the code patcher
945// the exact number of bytes specified must be emitted. Is not legal to emit
946// relocation information. If any of these constraints are violated it causes
947// an assertion.
948class CodePatcher {
949 public:
950 CodePatcher(byte* address, int size);
951 virtual ~CodePatcher();
952
953 // Macro assembler to emit code.
954 MacroAssembler* masm() { return &masm_; }
955
956 private:
957 byte* address_; // The address of the code being patched.
958 int size_; // Number of bytes of the expected patch size.
959 MacroAssembler masm_; // Macro assembler used to generate the code.
960};
961
962
ager@chromium.org9085a012009-05-11 19:22:57 +0000963// -----------------------------------------------------------------------------
964// Static helper functions.
965
966// Generate an Operand for loading a field from an object.
967static inline Operand FieldOperand(Register object, int offset) {
968 return Operand(object, offset - kHeapObjectTag);
969}
970
971
972// Generate an Operand for loading an indexed field from an object.
973static inline Operand FieldOperand(Register object,
974 Register index,
975 ScaleFactor scale,
976 int offset) {
977 return Operand(object, index, scale, offset - kHeapObjectTag);
978}
979
980
981#ifdef GENERATED_CODE_COVERAGE
982extern void LogGeneratedCodeCoverage(const char* file_line);
983#define CODE_COVERAGE_STRINGIFY(x) #x
984#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
985#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
986#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000987 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000988 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
989 masm->pushfd(); \
990 masm->pushad(); \
991 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000992 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000993 masm->pop(rax); \
994 masm->popad(); \
995 masm->popfd(); \
996 } \
997 masm->
998#else
999#define ACCESS_MASM(masm) masm->
1000#endif
1001
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001002// -----------------------------------------------------------------------------
1003// Template implementations.
1004
1005static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1006
1007
1008template <typename LabelType>
1009void MacroAssembler::SmiNeg(Register dst,
1010 Register src,
1011 LabelType* on_smi_result) {
1012 if (dst.is(src)) {
1013 ASSERT(!dst.is(kScratchRegister));
1014 movq(kScratchRegister, src);
1015 neg(dst); // Low 32 bits are retained as zero by negation.
1016 // Test if result is zero or Smi::kMinValue.
1017 cmpq(dst, kScratchRegister);
1018 j(not_equal, on_smi_result);
1019 movq(src, kScratchRegister);
1020 } else {
1021 movq(dst, src);
1022 neg(dst);
1023 cmpq(dst, src);
1024 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1025 j(not_equal, on_smi_result);
1026 }
1027}
1028
1029
1030template <typename LabelType>
1031void MacroAssembler::SmiAdd(Register dst,
1032 Register src1,
1033 Register src2,
1034 LabelType* on_not_smi_result) {
1035 ASSERT_NOT_NULL(on_not_smi_result);
1036 ASSERT(!dst.is(src2));
1037 if (dst.is(src1)) {
1038 movq(kScratchRegister, src1);
1039 addq(kScratchRegister, src2);
1040 j(overflow, on_not_smi_result);
1041 movq(dst, kScratchRegister);
1042 } else {
1043 movq(dst, src1);
1044 addq(dst, src2);
1045 j(overflow, on_not_smi_result);
1046 }
1047}
1048
1049
1050template <typename LabelType>
1051void MacroAssembler::SmiSub(Register dst,
1052 Register src1,
1053 Register src2,
1054 LabelType* on_not_smi_result) {
1055 ASSERT_NOT_NULL(on_not_smi_result);
1056 ASSERT(!dst.is(src2));
1057 if (dst.is(src1)) {
1058 cmpq(dst, src2);
1059 j(overflow, on_not_smi_result);
1060 subq(dst, src2);
1061 } else {
1062 movq(dst, src1);
1063 subq(dst, src2);
1064 j(overflow, on_not_smi_result);
1065 }
1066}
1067
1068
1069template <typename LabelType>
1070void MacroAssembler::SmiSub(Register dst,
1071 Register src1,
1072 const Operand& src2,
1073 LabelType* on_not_smi_result) {
1074 ASSERT_NOT_NULL(on_not_smi_result);
1075 if (dst.is(src1)) {
1076 movq(kScratchRegister, src2);
1077 cmpq(src1, kScratchRegister);
1078 j(overflow, on_not_smi_result);
1079 subq(src1, kScratchRegister);
1080 } else {
1081 movq(dst, src1);
1082 subq(dst, src2);
1083 j(overflow, on_not_smi_result);
1084 }
1085}
1086
1087
1088template <typename LabelType>
1089void MacroAssembler::SmiMul(Register dst,
1090 Register src1,
1091 Register src2,
1092 LabelType* on_not_smi_result) {
1093 ASSERT(!dst.is(src2));
1094 ASSERT(!dst.is(kScratchRegister));
1095 ASSERT(!src1.is(kScratchRegister));
1096 ASSERT(!src2.is(kScratchRegister));
1097
1098 if (dst.is(src1)) {
1099 NearLabel failure, zero_correct_result;
1100 movq(kScratchRegister, src1); // Create backup for later testing.
1101 SmiToInteger64(dst, src1);
1102 imul(dst, src2);
1103 j(overflow, &failure);
1104
1105 // Check for negative zero result. If product is zero, and one
1106 // argument is negative, go to slow case.
1107 NearLabel correct_result;
1108 testq(dst, dst);
1109 j(not_zero, &correct_result);
1110
1111 movq(dst, kScratchRegister);
1112 xor_(dst, src2);
1113 j(positive, &zero_correct_result); // Result was positive zero.
1114
1115 bind(&failure); // Reused failure exit, restores src1.
1116 movq(src1, kScratchRegister);
1117 jmp(on_not_smi_result);
1118
1119 bind(&zero_correct_result);
1120 xor_(dst, dst);
1121
1122 bind(&correct_result);
1123 } else {
1124 SmiToInteger64(dst, src1);
1125 imul(dst, src2);
1126 j(overflow, on_not_smi_result);
1127 // Check for negative zero result. If product is zero, and one
1128 // argument is negative, go to slow case.
1129 NearLabel correct_result;
1130 testq(dst, dst);
1131 j(not_zero, &correct_result);
1132 // One of src1 and src2 is zero, the check whether the other is
1133 // negative.
1134 movq(kScratchRegister, src1);
1135 xor_(kScratchRegister, src2);
1136 j(negative, on_not_smi_result);
1137 bind(&correct_result);
1138 }
1139}
1140
1141
1142template <typename LabelType>
1143void MacroAssembler::SmiTryAddConstant(Register dst,
1144 Register src,
1145 Smi* constant,
1146 LabelType* on_not_smi_result) {
1147 // Does not assume that src is a smi.
1148 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1149 ASSERT_EQ(0, kSmiTag);
1150 ASSERT(!dst.is(kScratchRegister));
1151 ASSERT(!src.is(kScratchRegister));
1152
1153 JumpIfNotSmi(src, on_not_smi_result);
1154 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1155 LoadSmiConstant(tmp, constant);
1156 addq(tmp, src);
1157 j(overflow, on_not_smi_result);
1158 if (dst.is(src)) {
1159 movq(dst, tmp);
1160 }
1161}
1162
1163
1164template <typename LabelType>
1165void MacroAssembler::SmiAddConstant(Register dst,
1166 Register src,
1167 Smi* constant,
1168 LabelType* on_not_smi_result) {
1169 if (constant->value() == 0) {
1170 if (!dst.is(src)) {
1171 movq(dst, src);
1172 }
1173 } else if (dst.is(src)) {
1174 ASSERT(!dst.is(kScratchRegister));
1175
1176 LoadSmiConstant(kScratchRegister, constant);
1177 addq(kScratchRegister, src);
1178 j(overflow, on_not_smi_result);
1179 movq(dst, kScratchRegister);
1180 } else {
1181 LoadSmiConstant(dst, constant);
1182 addq(dst, src);
1183 j(overflow, on_not_smi_result);
1184 }
1185}
1186
1187
1188template <typename LabelType>
1189void MacroAssembler::SmiSubConstant(Register dst,
1190 Register src,
1191 Smi* constant,
1192 LabelType* on_not_smi_result) {
1193 if (constant->value() == 0) {
1194 if (!dst.is(src)) {
1195 movq(dst, src);
1196 }
1197 } else if (dst.is(src)) {
1198 ASSERT(!dst.is(kScratchRegister));
1199 if (constant->value() == Smi::kMinValue) {
1200 // Subtracting min-value from any non-negative value will overflow.
1201 // We test the non-negativeness before doing the subtraction.
1202 testq(src, src);
1203 j(not_sign, on_not_smi_result);
1204 LoadSmiConstant(kScratchRegister, constant);
1205 subq(dst, kScratchRegister);
1206 } else {
1207 // Subtract by adding the negation.
1208 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1209 addq(kScratchRegister, dst);
1210 j(overflow, on_not_smi_result);
1211 movq(dst, kScratchRegister);
1212 }
1213 } else {
1214 if (constant->value() == Smi::kMinValue) {
1215 // Subtracting min-value from any non-negative value will overflow.
1216 // We test the non-negativeness before doing the subtraction.
1217 testq(src, src);
1218 j(not_sign, on_not_smi_result);
1219 LoadSmiConstant(dst, constant);
1220 // Adding and subtracting the min-value gives the same result, it only
1221 // differs on the overflow bit, which we don't check here.
1222 addq(dst, src);
1223 } else {
1224 // Subtract by adding the negation.
1225 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1226 addq(dst, src);
1227 j(overflow, on_not_smi_result);
1228 }
1229 }
1230}
1231
1232
1233template <typename LabelType>
1234void MacroAssembler::SmiDiv(Register dst,
1235 Register src1,
1236 Register src2,
1237 LabelType* on_not_smi_result) {
1238 ASSERT(!src1.is(kScratchRegister));
1239 ASSERT(!src2.is(kScratchRegister));
1240 ASSERT(!dst.is(kScratchRegister));
1241 ASSERT(!src2.is(rax));
1242 ASSERT(!src2.is(rdx));
1243 ASSERT(!src1.is(rdx));
1244
1245 // Check for 0 divisor (result is +/-Infinity).
1246 NearLabel positive_divisor;
1247 testq(src2, src2);
1248 j(zero, on_not_smi_result);
1249
1250 if (src1.is(rax)) {
1251 movq(kScratchRegister, src1);
1252 }
1253 SmiToInteger32(rax, src1);
1254 // We need to rule out dividing Smi::kMinValue by -1, since that would
1255 // overflow in idiv and raise an exception.
1256 // We combine this with negative zero test (negative zero only happens
1257 // when dividing zero by a negative number).
1258
1259 // We overshoot a little and go to slow case if we divide min-value
1260 // by any negative value, not just -1.
1261 NearLabel safe_div;
1262 testl(rax, Immediate(0x7fffffff));
1263 j(not_zero, &safe_div);
1264 testq(src2, src2);
1265 if (src1.is(rax)) {
1266 j(positive, &safe_div);
1267 movq(src1, kScratchRegister);
1268 jmp(on_not_smi_result);
1269 } else {
1270 j(negative, on_not_smi_result);
1271 }
1272 bind(&safe_div);
1273
1274 SmiToInteger32(src2, src2);
1275 // Sign extend src1 into edx:eax.
1276 cdq();
1277 idivl(src2);
1278 Integer32ToSmi(src2, src2);
1279 // Check that the remainder is zero.
1280 testl(rdx, rdx);
1281 if (src1.is(rax)) {
1282 NearLabel smi_result;
1283 j(zero, &smi_result);
1284 movq(src1, kScratchRegister);
1285 jmp(on_not_smi_result);
1286 bind(&smi_result);
1287 } else {
1288 j(not_zero, on_not_smi_result);
1289 }
1290 if (!dst.is(src1) && src1.is(rax)) {
1291 movq(src1, kScratchRegister);
1292 }
1293 Integer32ToSmi(dst, rax);
1294}
1295
1296
1297template <typename LabelType>
1298void MacroAssembler::SmiMod(Register dst,
1299 Register src1,
1300 Register src2,
1301 LabelType* on_not_smi_result) {
1302 ASSERT(!dst.is(kScratchRegister));
1303 ASSERT(!src1.is(kScratchRegister));
1304 ASSERT(!src2.is(kScratchRegister));
1305 ASSERT(!src2.is(rax));
1306 ASSERT(!src2.is(rdx));
1307 ASSERT(!src1.is(rdx));
1308 ASSERT(!src1.is(src2));
1309
1310 testq(src2, src2);
1311 j(zero, on_not_smi_result);
1312
1313 if (src1.is(rax)) {
1314 movq(kScratchRegister, src1);
1315 }
1316 SmiToInteger32(rax, src1);
1317 SmiToInteger32(src2, src2);
1318
1319 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1320 NearLabel safe_div;
1321 cmpl(rax, Immediate(Smi::kMinValue));
1322 j(not_equal, &safe_div);
1323 cmpl(src2, Immediate(-1));
1324 j(not_equal, &safe_div);
1325 // Retag inputs and go slow case.
1326 Integer32ToSmi(src2, src2);
1327 if (src1.is(rax)) {
1328 movq(src1, kScratchRegister);
1329 }
1330 jmp(on_not_smi_result);
1331 bind(&safe_div);
1332
1333 // Sign extend eax into edx:eax.
1334 cdq();
1335 idivl(src2);
1336 // Restore smi tags on inputs.
1337 Integer32ToSmi(src2, src2);
1338 if (src1.is(rax)) {
1339 movq(src1, kScratchRegister);
1340 }
1341 // Check for a negative zero result. If the result is zero, and the
1342 // dividend is negative, go slow to return a floating point negative zero.
1343 NearLabel smi_result;
1344 testl(rdx, rdx);
1345 j(not_zero, &smi_result);
1346 testq(src1, src1);
1347 j(negative, on_not_smi_result);
1348 bind(&smi_result);
1349 Integer32ToSmi(dst, rdx);
1350}
1351
1352
1353template <typename LabelType>
1354void MacroAssembler::SmiShiftLogicalRightConstant(
1355 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1356 // Logic right shift interprets its result as an *unsigned* number.
1357 if (dst.is(src)) {
1358 UNIMPLEMENTED(); // Not used.
1359 } else {
1360 movq(dst, src);
1361 if (shift_value == 0) {
1362 testq(dst, dst);
1363 j(negative, on_not_smi_result);
1364 }
1365 shr(dst, Immediate(shift_value + kSmiShift));
1366 shl(dst, Immediate(kSmiShift));
1367 }
1368}
1369
1370
1371template <typename LabelType>
1372void MacroAssembler::SmiShiftLogicalRight(Register dst,
1373 Register src1,
1374 Register src2,
1375 LabelType* on_not_smi_result) {
1376 ASSERT(!dst.is(kScratchRegister));
1377 ASSERT(!src1.is(kScratchRegister));
1378 ASSERT(!src2.is(kScratchRegister));
1379 ASSERT(!dst.is(rcx));
1380 NearLabel result_ok;
1381 if (src1.is(rcx) || src2.is(rcx)) {
1382 movq(kScratchRegister, rcx);
1383 }
1384 if (!dst.is(src1)) {
1385 movq(dst, src1);
1386 }
1387 SmiToInteger32(rcx, src2);
1388 orl(rcx, Immediate(kSmiShift));
1389 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1390 shl(dst, Immediate(kSmiShift));
1391 testq(dst, dst);
1392 if (src1.is(rcx) || src2.is(rcx)) {
1393 NearLabel positive_result;
1394 j(positive, &positive_result);
1395 if (src1.is(rcx)) {
1396 movq(src1, kScratchRegister);
1397 } else {
1398 movq(src2, kScratchRegister);
1399 }
1400 jmp(on_not_smi_result);
1401 bind(&positive_result);
1402 } else {
1403 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1404 }
1405}
1406
1407
1408template <typename LabelType>
1409void MacroAssembler::SelectNonSmi(Register dst,
1410 Register src1,
1411 Register src2,
1412 LabelType* on_not_smis) {
1413 ASSERT(!dst.is(kScratchRegister));
1414 ASSERT(!src1.is(kScratchRegister));
1415 ASSERT(!src2.is(kScratchRegister));
1416 ASSERT(!dst.is(src1));
1417 ASSERT(!dst.is(src2));
1418 // Both operands must not be smis.
1419#ifdef DEBUG
1420 if (allow_stub_calls()) { // Check contains a stub call.
1421 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1422 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1423 }
1424#endif
1425 ASSERT_EQ(0, kSmiTag);
1426 ASSERT_EQ(0, Smi::FromInt(0));
1427 movl(kScratchRegister, Immediate(kSmiTagMask));
1428 and_(kScratchRegister, src1);
1429 testl(kScratchRegister, src2);
1430 // If non-zero then both are smis.
1431 j(not_zero, on_not_smis);
1432
1433 // Exactly one operand is a smi.
1434 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1435 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1436 subq(kScratchRegister, Immediate(1));
1437 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1438 movq(dst, src1);
1439 xor_(dst, src2);
1440 and_(dst, kScratchRegister);
1441 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1442 xor_(dst, src1);
1443 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1444}
1445
1446
1447template <typename LabelType>
1448void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1449 ASSERT_EQ(0, kSmiTag);
1450 Condition smi = CheckSmi(src);
1451 j(smi, on_smi);
1452}
1453
1454
1455template <typename LabelType>
1456void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1457 Condition smi = CheckSmi(src);
1458 j(NegateCondition(smi), on_not_smi);
1459}
1460
1461
1462template <typename LabelType>
1463void MacroAssembler::JumpIfNotPositiveSmi(Register src,
1464 LabelType* on_not_positive_smi) {
1465 Condition positive_smi = CheckPositiveSmi(src);
1466 j(NegateCondition(positive_smi), on_not_positive_smi);
1467}
1468
1469
1470template <typename LabelType>
1471void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1472 Smi* constant,
1473 LabelType* on_equals) {
1474 SmiCompare(src, constant);
1475 j(equal, on_equals);
1476}
1477
1478
1479template <typename LabelType>
1480void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1481 LabelType* on_invalid) {
1482 Condition is_valid = CheckInteger32ValidSmiValue(src);
1483 j(NegateCondition(is_valid), on_invalid);
1484}
1485
1486
1487template <typename LabelType>
1488void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1489 LabelType* on_invalid) {
1490 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1491 j(NegateCondition(is_valid), on_invalid);
1492}
1493
1494
1495template <typename LabelType>
1496void MacroAssembler::JumpIfNotBothSmi(Register src1,
1497 Register src2,
1498 LabelType* on_not_both_smi) {
1499 Condition both_smi = CheckBothSmi(src1, src2);
1500 j(NegateCondition(both_smi), on_not_both_smi);
1501}
1502
1503
1504template <typename LabelType>
1505void MacroAssembler::JumpIfNotBothPositiveSmi(Register src1,
1506 Register src2,
1507 LabelType* on_not_both_smi) {
1508 Condition both_smi = CheckBothPositiveSmi(src1, src2);
1509 j(NegateCondition(both_smi), on_not_both_smi);
1510}
1511
1512
1513template <typename LabelType>
1514void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1515 Register second_object,
1516 Register scratch1,
1517 Register scratch2,
1518 LabelType* on_fail) {
1519 // Check that both objects are not smis.
1520 Condition either_smi = CheckEitherSmi(first_object, second_object);
1521 j(either_smi, on_fail);
1522
1523 // Load instance type for both strings.
1524 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1525 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1526 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1527 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1528
1529 // Check that both are flat ascii strings.
1530 ASSERT(kNotStringTag != 0);
1531 const int kFlatAsciiStringMask =
1532 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1533 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1534
1535 andl(scratch1, Immediate(kFlatAsciiStringMask));
1536 andl(scratch2, Immediate(kFlatAsciiStringMask));
1537 // Interleave the bits to check both scratch1 and scratch2 in one test.
1538 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1539 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1540 cmpl(scratch1,
1541 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1542 j(not_equal, on_fail);
1543}
1544
1545
1546template <typename LabelType>
1547void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1548 Register instance_type,
1549 Register scratch,
1550 LabelType *failure) {
1551 if (!scratch.is(instance_type)) {
1552 movl(scratch, instance_type);
1553 }
1554
1555 const int kFlatAsciiStringMask =
1556 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1557
1558 andl(scratch, Immediate(kFlatAsciiStringMask));
1559 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1560 j(not_equal, failure);
1561}
1562
1563
1564template <typename LabelType>
1565void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1566 Register first_object_instance_type,
1567 Register second_object_instance_type,
1568 Register scratch1,
1569 Register scratch2,
1570 LabelType* on_fail) {
1571 // Load instance type for both strings.
1572 movq(scratch1, first_object_instance_type);
1573 movq(scratch2, second_object_instance_type);
1574
1575 // Check that both are flat ascii strings.
1576 ASSERT(kNotStringTag != 0);
1577 const int kFlatAsciiStringMask =
1578 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1579 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1580
1581 andl(scratch1, Immediate(kFlatAsciiStringMask));
1582 andl(scratch2, Immediate(kFlatAsciiStringMask));
1583 // Interleave the bits to check both scratch1 and scratch2 in one test.
1584 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1585 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1586 cmpl(scratch1,
1587 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1588 j(not_equal, on_fail);
1589}
1590
1591
1592template <typename LabelType>
1593void MacroAssembler::InNewSpace(Register object,
1594 Register scratch,
1595 Condition cc,
1596 LabelType* branch) {
1597 if (Serializer::enabled()) {
1598 // Can't do arithmetic on external references if it might get serialized.
1599 // The mask isn't really an address. We load it as an external reference in
1600 // case the size of the new space is different between the snapshot maker
1601 // and the running system.
1602 if (scratch.is(object)) {
1603 movq(kScratchRegister, ExternalReference::new_space_mask());
1604 and_(scratch, kScratchRegister);
1605 } else {
1606 movq(scratch, ExternalReference::new_space_mask());
1607 and_(scratch, object);
1608 }
1609 movq(kScratchRegister, ExternalReference::new_space_start());
1610 cmpq(scratch, kScratchRegister);
1611 j(cc, branch);
1612 } else {
1613 ASSERT(is_int32(static_cast<int64_t>(Heap::NewSpaceMask())));
1614 intptr_t new_space_start =
1615 reinterpret_cast<intptr_t>(Heap::NewSpaceStart());
1616 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1617 if (scratch.is(object)) {
1618 addq(scratch, kScratchRegister);
1619 } else {
1620 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1621 }
1622 and_(scratch, Immediate(static_cast<int32_t>(Heap::NewSpaceMask())));
1623 j(cc, branch);
1624 }
1625}
1626
1627
1628template <typename LabelType>
1629void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1630 const ParameterCount& actual,
1631 Handle<Code> code_constant,
1632 Register code_register,
1633 LabelType* done,
1634 InvokeFlag flag) {
1635 bool definitely_matches = false;
1636 NearLabel invoke;
1637 if (expected.is_immediate()) {
1638 ASSERT(actual.is_immediate());
1639 if (expected.immediate() == actual.immediate()) {
1640 definitely_matches = true;
1641 } else {
1642 Set(rax, actual.immediate());
1643 if (expected.immediate() ==
1644 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1645 // Don't worry about adapting arguments for built-ins that
1646 // don't want that done. Skip adaption code by making it look
1647 // like we have a match between expected and actual number of
1648 // arguments.
1649 definitely_matches = true;
1650 } else {
1651 Set(rbx, expected.immediate());
1652 }
1653 }
1654 } else {
1655 if (actual.is_immediate()) {
1656 // Expected is in register, actual is immediate. This is the
1657 // case when we invoke function values without going through the
1658 // IC mechanism.
1659 cmpq(expected.reg(), Immediate(actual.immediate()));
1660 j(equal, &invoke);
1661 ASSERT(expected.reg().is(rbx));
1662 Set(rax, actual.immediate());
1663 } else if (!expected.reg().is(actual.reg())) {
1664 // Both expected and actual are in (different) registers. This
1665 // is the case when we invoke functions using call and apply.
1666 cmpq(expected.reg(), actual.reg());
1667 j(equal, &invoke);
1668 ASSERT(actual.reg().is(rax));
1669 ASSERT(expected.reg().is(rbx));
1670 }
1671 }
1672
1673 if (!definitely_matches) {
1674 Handle<Code> adaptor =
1675 Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
1676 if (!code_constant.is_null()) {
1677 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1678 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1679 } else if (!code_register.is(rdx)) {
1680 movq(rdx, code_register);
1681 }
1682
1683 if (flag == CALL_FUNCTION) {
1684 Call(adaptor, RelocInfo::CODE_TARGET);
1685 jmp(done);
1686 } else {
1687 Jump(adaptor, RelocInfo::CODE_TARGET);
1688 }
1689 bind(&invoke);
1690 }
1691}
1692
ager@chromium.org9085a012009-05-11 19:22:57 +00001693
1694} } // namespace v8::internal
1695
1696#endif // V8_X64_MACRO_ASSEMBLER_X64_H_