blob: 8499edfb5abc5e63dbb071c99b85acc1e31a0475 [file] [log] [blame]
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.org9085a012009-05-11 19:22:57 +000028#ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000035
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000036// Flags used for the AllocateInNewSpace functions.
37enum AllocationFlags {
38 // No special flags.
39 NO_ALLOCATION_FLAGS = 0,
40 // Return the pointer to the allocated already tagged as a heap object.
41 TAG_OBJECT = 1 << 0,
42 // The content of the result register already contains the allocation top in
43 // new space.
44 RESULT_CONTAINS_TOP = 1 << 1
45};
46
ager@chromium.orge2902be2009-06-08 12:21:35 +000047// Default scratch register used by MacroAssembler (and other code that needs
48// a spare register). The register isn't callee save, and not used by the
49// function calling convention.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +000050static const Register kScratchRegister = { 10 }; // r10.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000051static const Register kSmiConstantRegister = { 12 }; // r12 (callee save).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +000052static const Register kRootRegister = { 13 }; // r13 (callee save).
53// Value of smi in kSmiConstantRegister.
54static const int kSmiConstantRegisterValue = 1;
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000055// Actual value of root register is offset from the root array's start
56// to take advantage of negitive 8-bit displacement values.
57static const int kRootRegisterBias = 128;
ager@chromium.orge2902be2009-06-08 12:21:35 +000058
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000059// Convenience for platform-independent signatures.
60typedef Operand MemOperand;
61
ager@chromium.org9085a012009-05-11 19:22:57 +000062// Forward declaration.
63class JumpTarget;
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +000064class CallWrapper;
ager@chromium.org9085a012009-05-11 19:22:57 +000065
ager@chromium.org4af710e2009-09-15 12:20:11 +000066struct SmiIndex {
67 SmiIndex(Register index_register, ScaleFactor scale)
68 : reg(index_register),
69 scale(scale) {}
70 Register reg;
71 ScaleFactor scale;
72};
ager@chromium.org9085a012009-05-11 19:22:57 +000073
ager@chromium.org9085a012009-05-11 19:22:57 +000074// MacroAssembler implements a collection of frequently used macros.
75class MacroAssembler: public Assembler {
76 public:
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +000077 // The isolate parameter can be NULL if the macro assembler should
78 // not use isolate-dependent functionality. In this case, it's the
79 // responsibility of the caller to never invoke such function on the
80 // macro assembler.
81 MacroAssembler(Isolate* isolate, void* buffer, int size);
ager@chromium.org9085a012009-05-11 19:22:57 +000082
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000083 // Prevent the use of the RootArray during the lifetime of this
84 // scope object.
85 class NoRootArrayScope BASE_EMBEDDED {
86 public:
87 explicit NoRootArrayScope(MacroAssembler* assembler)
88 : variable_(&assembler->root_array_available_),
89 old_value_(assembler->root_array_available_) {
90 assembler->root_array_available_ = false;
91 }
92 ~NoRootArrayScope() {
93 *variable_ = old_value_;
94 }
95 private:
96 bool* variable_;
97 bool old_value_;
98 };
99
100 // Operand pointing to an external reference.
101 // May emit code to set up the scratch register. The operand is
102 // only guaranteed to be correct as long as the scratch register
103 // isn't changed.
104 // If the operand is used more than once, use a scratch register
105 // that is guaranteed not to be clobbered.
106 Operand ExternalOperand(ExternalReference reference,
107 Register scratch = kScratchRegister);
108 // Loads and stores the value of an external reference.
109 // Special case code for load and store to take advantage of
110 // load_rax/store_rax if possible/necessary.
111 // For other operations, just use:
112 // Operand operand = ExternalOperand(extref);
113 // operation(operand, ..);
114 void Load(Register destination, ExternalReference source);
115 void Store(ExternalReference destination, Register source);
116 // Loads the address of the external reference into the destination
117 // register.
118 void LoadAddress(Register destination, ExternalReference source);
119 // Returns the size of the code generated by LoadAddress.
120 // Used by CallSize(ExternalReference) to find the size of a call.
121 int LoadAddressSize(ExternalReference source);
122
123 // Operations on roots in the root-array.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000124 void LoadRoot(Register destination, Heap::RootListIndex index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 void StoreRoot(Register source, Heap::RootListIndex index);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000126 // Load a root value where the index (or part of it) is variable.
127 // The variable_offset register is added to the fixed_offset value
128 // to get the index into the root-array.
129 void LoadRootIndexed(Register destination,
130 Register variable_offset,
131 int fixed_offset);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000132 void CompareRoot(Register with, Heap::RootListIndex index);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000133 void CompareRoot(const Operand& with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000134 void PushRoot(Heap::RootListIndex index);
135
ager@chromium.org9085a012009-05-11 19:22:57 +0000136 // ---------------------------------------------------------------------------
137 // GC Support
138
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000139 // For page containing |object| mark region covering |addr| dirty.
140 // RecordWriteHelper only works if the object is not in new
ager@chromium.orgac091b72010-05-05 07:34:42 +0000141 // space.
142 void RecordWriteHelper(Register object,
143 Register addr,
144 Register scratch);
145
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000146 // Check if object is in new space. The condition cc can be equal or
147 // not_equal. If it is equal a jump will be done if the object is on new
148 // space. The register scratch can be object itself, but it will be clobbered.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000149 template <typename LabelType>
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000150 void InNewSpace(Register object,
151 Register scratch,
152 Condition cc,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000153 LabelType* branch);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000154
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000155 // For page containing |object| mark region covering [object+offset]
156 // dirty. |object| is the object being stored into, |value| is the
157 // object being stored. If |offset| is zero, then the |scratch|
158 // register contains the array index into the elements array
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000159 // represented as an untagged 32-bit integer. All registers are
160 // clobbered by the operation. RecordWrite filters out smis so it
161 // does not update the write barrier if the value is a smi.
ager@chromium.org9085a012009-05-11 19:22:57 +0000162 void RecordWrite(Register object,
163 int offset,
164 Register value,
165 Register scratch);
166
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000167 // For page containing |object| mark region covering [address]
168 // dirty. |object| is the object being stored into, |value| is the
169 // object being stored. All registers are clobbered by the
170 // operation. RecordWrite filters out smis so it does not update
171 // the write barrier if the value is a smi.
172 void RecordWrite(Register object,
173 Register address,
174 Register value);
175
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000176 // For page containing |object| mark region covering [object+offset] dirty.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000177 // The value is known to not be a smi.
178 // object is the object being stored into, value is the object being stored.
179 // If offset is zero, then the scratch register contains the array index into
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000180 // the elements array represented as an untagged 32-bit integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000181 // All registers are clobbered by the operation.
182 void RecordWriteNonSmi(Register object,
183 int offset,
184 Register value,
185 Register scratch);
186
ager@chromium.org9085a012009-05-11 19:22:57 +0000187#ifdef ENABLE_DEBUGGER_SUPPORT
188 // ---------------------------------------------------------------------------
189 // Debugger Support
190
ager@chromium.org5c838252010-02-19 08:53:10 +0000191 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000192#endif
193
194 // ---------------------------------------------------------------------------
195 // Activation frames
196
197 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
198 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
199
200 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
201 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
202
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000203 // Enter specific kind of exit frame; either in normal or
204 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000205 // sets up the number of arguments in register rdi and the pointer
206 // to the first argument in register rsi.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000207 //
208 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
209 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000210 void EnterExitFrame(int arg_stack_space = 0, bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000211
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000212 // Enter specific kind of exit frame. Allocates arg_stack_space * kPointerSize
213 // memory (not GCed) on the stack accessible via StackSpaceOperand.
214 void EnterApiExitFrame(int arg_stack_space);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000215
ager@chromium.orga1645e22009-09-09 19:27:10 +0000216 // Leave the current exit frame. Expects/provides the return value in
217 // register rax:rdx (untouched) and the pointer to the first
218 // argument in register rsi.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000219 void LeaveExitFrame(bool save_doubles = false);
ager@chromium.org9085a012009-05-11 19:22:57 +0000220
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000221 // Leave the current exit frame. Expects/provides the return value in
222 // register rax (untouched).
223 void LeaveApiExitFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +0000224
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000225 // Push and pop the registers that can hold pointers.
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000226 void PushSafepointRegisters() { Pushad(); }
227 void PopSafepointRegisters() { Popad(); }
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000228 // Store the value in register src in the safepoint register stack
229 // slot for register dst.
230 void StoreToSafepointRegisterSlot(Register dst, Register src);
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000231 void LoadFromSafepointRegisterSlot(Register dst, Register src);
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000232
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000233 void InitializeRootRegister() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000234 ExternalReference roots_address =
235 ExternalReference::roots_address(isolate());
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000236 movq(kRootRegister, roots_address);
237 addq(kRootRegister, Immediate(kRootRegisterBias));
238 }
239
ager@chromium.org9085a012009-05-11 19:22:57 +0000240 // ---------------------------------------------------------------------------
241 // JavaScript invokes
242
243 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000244 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000245 const ParameterCount& expected,
246 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000247 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000248 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000249
250 void InvokeCode(Handle<Code> code,
251 const ParameterCount& expected,
252 const ParameterCount& actual,
253 RelocInfo::Mode rmode,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000254 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000255 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000256
257 // Invoke the JavaScript function in the given register. Changes the
258 // current context to the context in the function before invoking.
259 void InvokeFunction(Register function,
260 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000261 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000262 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000263
ager@chromium.org5c838252010-02-19 08:53:10 +0000264 void InvokeFunction(JSFunction* function,
265 const ParameterCount& actual,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000266 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000267 CallWrapper* call_wrapper = NULL);
ager@chromium.org5c838252010-02-19 08:53:10 +0000268
ager@chromium.org9085a012009-05-11 19:22:57 +0000269 // Invoke specified builtin JavaScript function. Adds an entry to
270 // the unresolved list if the name does not resolve.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000271 void InvokeBuiltin(Builtins::JavaScript id,
272 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000273 CallWrapper* call_wrapper = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +0000274
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000275 // Store the function for the given builtin in the target register.
276 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
277
ager@chromium.org9085a012009-05-11 19:22:57 +0000278 // Store the code object for the given builtin in the target register.
279 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
280
ager@chromium.org4af710e2009-09-15 12:20:11 +0000281
282 // ---------------------------------------------------------------------------
283 // Smi tagging, untagging and operations on tagged smis.
284
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000285 void InitializeSmiConstantRegister() {
286 movq(kSmiConstantRegister,
287 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
288 RelocInfo::NONE);
289 }
290
ager@chromium.org4af710e2009-09-15 12:20:11 +0000291 // Conversions between tagged smi values and non-tagged integer values.
292
293 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000294 // Only uses the low 32 bits of the src register. Sets the N and Z flags
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000295 // based on the value of the resulting smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000296 void Integer32ToSmi(Register dst, Register src);
297
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000298 // Stores an integer32 value into a memory field that already holds a smi.
299 void Integer32ToSmiField(const Operand& dst, Register src);
300
ager@chromium.org4af710e2009-09-15 12:20:11 +0000301 // Adds constant to src and tags the result as a smi.
302 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000303 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000304
305 // Convert smi to 32-bit integer. I.e., not sign extended into
306 // high 32 bits of destination.
307 void SmiToInteger32(Register dst, Register src);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000308 void SmiToInteger32(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000309
310 // Convert smi to 64-bit integer (sign extended if necessary).
311 void SmiToInteger64(Register dst, Register src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000312 void SmiToInteger64(Register dst, const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000313
314 // Multiply a positive smi's integer value by a power of two.
315 // Provides result as 64-bit integer value.
316 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
317 Register src,
318 int power);
319
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000320 // Divide a positive smi's integer value by a power of two.
321 // Provides result as 32-bit integer value.
322 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
323 Register src,
324 int power);
325
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000326 // Perform the logical or of two smi values and return a smi value.
327 // If either argument is not a smi, jump to on_not_smis and retain
328 // the original values of source registers. The destination register
329 // may be changed if it's not one of the source registers.
330 template <typename LabelType>
331 void SmiOrIfSmis(Register dst,
332 Register src1,
333 Register src2,
334 LabelType* on_not_smis);
335
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000336
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000337 // Simple comparison of smis. Both sides must be known smis to use these,
338 // otherwise use Cmp.
339 void SmiCompare(Register smi1, Register smi2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000340 void SmiCompare(Register dst, Smi* src);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000341 void SmiCompare(Register dst, const Operand& src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000342 void SmiCompare(const Operand& dst, Register src);
343 void SmiCompare(const Operand& dst, Smi* src);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000344 // Compare the int32 in src register to the value of the smi stored at dst.
345 void SmiCompareInteger32(const Operand& dst, Register src);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000346 // Sets sign and zero flags depending on value of smi in register.
347 void SmiTest(Register src);
348
ager@chromium.org4af710e2009-09-15 12:20:11 +0000349 // Functions performing a check on a known or potential smi. Returns
350 // a condition that is satisfied if the check is successful.
351
352 // Is the value a tagged smi.
353 Condition CheckSmi(Register src);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000354 Condition CheckSmi(const Operand& src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000355
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000356 // Is the value a non-negative tagged smi.
357 Condition CheckNonNegativeSmi(Register src);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000358
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000359 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000360 Condition CheckBothSmi(Register first, Register second);
361
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000362 // Are both values non-negative tagged smis.
363 Condition CheckBothNonNegativeSmi(Register first, Register second);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364
365 // Are either value a tagged smi.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +0000366 Condition CheckEitherSmi(Register first,
367 Register second,
368 Register scratch = kScratchRegister);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000369
ager@chromium.org4af710e2009-09-15 12:20:11 +0000370 // Is the value the minimum smi value (since we are using
371 // two's complement numbers, negating the value is known to yield
372 // a non-smi value).
373 Condition CheckIsMinSmi(Register src);
374
ager@chromium.org4af710e2009-09-15 12:20:11 +0000375 // Checks whether an 32-bit integer value is a valid for conversion
376 // to a smi.
377 Condition CheckInteger32ValidSmiValue(Register src);
378
ager@chromium.org3811b432009-10-28 14:53:37 +0000379 // Checks whether an 32-bit unsigned integer value is a valid for
380 // conversion to a smi.
381 Condition CheckUInteger32ValidSmiValue(Register src);
382
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000383 // Check whether src is a Smi, and set dst to zero if it is a smi,
384 // and to one if it isn't.
385 void CheckSmiToIndicator(Register dst, Register src);
386 void CheckSmiToIndicator(Register dst, const Operand& src);
387
ager@chromium.org4af710e2009-09-15 12:20:11 +0000388 // Test-and-jump functions. Typically combines a check function
389 // above with a conditional jump.
390
391 // Jump if the value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000392 template <typename LabelType>
393 void JumpIfNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000394
ager@chromium.org3811b432009-10-28 14:53:37 +0000395 // Jump if the unsigned integer value cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000396 template <typename LabelType>
397 void JumpIfUIntNotValidSmiValue(Register src, LabelType* on_invalid);
ager@chromium.org3811b432009-10-28 14:53:37 +0000398
ager@chromium.org4af710e2009-09-15 12:20:11 +0000399 // Jump to label if the value is a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000400 template <typename LabelType>
401 void JumpIfSmi(Register src, LabelType* on_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000402
403 // Jump to label if the value is not a tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000404 template <typename LabelType>
405 void JumpIfNotSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000406
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000407 // Jump to label if the value is not a non-negative tagged smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000408 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000409 void JumpUnlessNonNegativeSmi(Register src, LabelType* on_not_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000410
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000411 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000412 // to the constant.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000413 template <typename LabelType>
414 void JumpIfSmiEqualsConstant(Register src,
415 Smi* constant,
416 LabelType* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000417
ager@chromium.org4af710e2009-09-15 12:20:11 +0000418 // Jump if either or both register are not smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000419 template <typename LabelType>
420 void JumpIfNotBothSmi(Register src1,
421 Register src2,
422 LabelType* on_not_both_smi);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000423
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000424 // Jump if either or both register are not non-negative smi values.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000425 template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000426 void JumpUnlessBothNonNegativeSmi(Register src1, Register src2,
427 LabelType* on_not_both_smi);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000428
ager@chromium.org4af710e2009-09-15 12:20:11 +0000429 // Operations on tagged smi values.
430
431 // Smis represent a subset of integers. The subset is always equivalent to
432 // a two's complement interpretation of a fixed number of bits.
433
434 // Optimistically adds an integer constant to a supposed smi.
435 // If the src is not a smi, or the result is not a smi, jump to
436 // the label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000437 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000438 void SmiTryAddConstant(Register dst,
439 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000440 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000441 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000442
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000443 // Add an integer constant to a tagged smi, giving a tagged smi as result.
444 // No overflow testing on the result is done.
445 void SmiAddConstant(Register dst, Register src, Smi* constant);
446
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000447 // Add an integer constant to a tagged smi, giving a tagged smi as result.
448 // No overflow testing on the result is done.
449 void SmiAddConstant(const Operand& dst, Smi* constant);
450
ager@chromium.org4af710e2009-09-15 12:20:11 +0000451 // Add an integer constant to a tagged smi, giving a tagged smi as result,
452 // or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000453 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000454 void SmiAddConstant(Register dst,
455 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000456 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000457 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000458
459 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.orgac091b72010-05-05 07:34:42 +0000460 // result. No testing on the result is done. Sets the N and Z flags
461 // based on the value of the resulting integer.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000462 void SmiSubConstant(Register dst, Register src, Smi* constant);
463
464 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000465 // result, or jumping to a label if the result cannot be represented by a smi.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000466 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000467 void SmiSubConstant(Register dst,
468 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000469 Smi* constant,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000470 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000471
472 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000473 // NOTICE: This operation jumps on success, not failure!
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000474 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000475 void SmiNeg(Register dst,
476 Register src,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000477 LabelType* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000478
479 // Adds smi values and return the result as a smi.
480 // If dst is src1, then src1 will be destroyed, even if
481 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000482 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000483 void SmiAdd(Register dst,
484 Register src1,
485 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000486 LabelType* on_not_smi_result);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000487 template <typename LabelType>
488 void SmiAdd(Register dst,
489 Register src1,
490 const Operand& src2,
491 LabelType* on_not_smi_result);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000492
493 void SmiAdd(Register dst,
494 Register src1,
495 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000496
497 // Subtracts smi values and return the result as a smi.
498 // If dst is src1, then src1 will be destroyed, even if
499 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000500 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000501 void SmiSub(Register dst,
502 Register src1,
503 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000504 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000505
ager@chromium.orgac091b72010-05-05 07:34:42 +0000506 void SmiSub(Register dst,
507 Register src1,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000508 Register src2);
509
510 template <typename LabelType>
511 void SmiSub(Register dst,
512 Register src1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000513 const Operand& src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000514 LabelType* on_not_smi_result);
515
516 void SmiSub(Register dst,
517 Register src1,
518 const Operand& src2);
ager@chromium.orgac091b72010-05-05 07:34:42 +0000519
ager@chromium.org4af710e2009-09-15 12:20:11 +0000520 // Multiplies smi values and return the result as a smi,
521 // if possible.
522 // If dst is src1, then src1 will be destroyed, even if
523 // the operation is unsuccessful.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000524 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000525 void SmiMul(Register dst,
526 Register src1,
527 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000528 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000529
530 // Divides one smi by another and returns the quotient.
531 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000532 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000533 void SmiDiv(Register dst,
534 Register src1,
535 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000536 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000537
538 // Divides one smi by another and returns the remainder.
539 // Clobbers rax and rdx registers.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000540 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000541 void SmiMod(Register dst,
542 Register src1,
543 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000544 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000545
546 // Bitwise operations.
547 void SmiNot(Register dst, Register src);
548 void SmiAnd(Register dst, Register src1, Register src2);
549 void SmiOr(Register dst, Register src1, Register src2);
550 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000551 void SmiAndConstant(Register dst, Register src1, Smi* constant);
552 void SmiOrConstant(Register dst, Register src1, Smi* constant);
553 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000554
555 void SmiShiftLeftConstant(Register dst,
556 Register src,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000557 int shift_value);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000558 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000559 void SmiShiftLogicalRightConstant(Register dst,
560 Register src,
561 int shift_value,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000562 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000563 void SmiShiftArithmeticRightConstant(Register dst,
564 Register src,
565 int shift_value);
566
567 // Shifts a smi value to the left, and returns the result if that is a smi.
568 // Uses and clobbers rcx, so dst may not be rcx.
569 void SmiShiftLeft(Register dst,
570 Register src1,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000571 Register src2);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000572 // Shifts a smi value to the right, shifting in zero bits at the top, and
573 // returns the unsigned intepretation of the result if that is a smi.
574 // Uses and clobbers rcx, so dst may not be rcx.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000575 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000576 void SmiShiftLogicalRight(Register dst,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000577 Register src1,
578 Register src2,
579 LabelType* on_not_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000580 // Shifts a smi value to the right, sign extending the top, and
581 // returns the signed intepretation of the result. That will always
582 // be a valid smi value, since it's numerically smaller than the
583 // original.
584 // Uses and clobbers rcx, so dst may not be rcx.
585 void SmiShiftArithmeticRight(Register dst,
586 Register src1,
587 Register src2);
588
589 // Specialized operations
590
591 // Select the non-smi register of two registers where exactly one is a
592 // smi. If neither are smis, jump to the failure label.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000593 template <typename LabelType>
ager@chromium.org4af710e2009-09-15 12:20:11 +0000594 void SelectNonSmi(Register dst,
595 Register src1,
596 Register src2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000597 LabelType* on_not_smis);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000598
599 // Converts, if necessary, a smi to a combination of number and
600 // multiplier to be used as a scaled index.
601 // The src register contains a *positive* smi value. The shift is the
602 // power of two to multiply the index value by (e.g.
603 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
604 // The returned index register may be either src or dst, depending
605 // on what is most efficient. If src and dst are different registers,
606 // src is always unchanged.
607 SmiIndex SmiToIndex(Register dst, Register src, int shift);
608
609 // Converts a positive smi to a negative index.
610 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
611
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000612 // Add the value of a smi in memory to an int32 register.
613 // Sets flags as a normal add.
614 void AddSmiField(Register dst, const Operand& src);
615
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000616 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000617 void Move(Register dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000618 LoadSmiConstant(dst, source);
ager@chromium.org3811b432009-10-28 14:53:37 +0000619 }
620
621 void Move(const Operand& dst, Smi* source) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000622 Register constant = GetSmiConstant(source);
623 movq(dst, constant);
ager@chromium.org3811b432009-10-28 14:53:37 +0000624 }
625
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000626 void Push(Smi* smi);
627 void Test(const Operand& dst, Smi* source);
628
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000629 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000630 // String macros.
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000631
632 // If object is a string, its map is loaded into object_map.
633 template <typename LabelType>
634 void JumpIfNotString(Register object,
635 Register object_map,
636 LabelType* not_string);
637
638
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000639 template <typename LabelType>
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000640 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
641 Register second_object,
642 Register scratch1,
643 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000644 LabelType* on_not_both_flat_ascii);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000645
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000646 // Check whether the instance type represents a flat ascii string. Jump to the
647 // label if not. If the instance type can be scratched specify same register
648 // for both instance type and scratch.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000649 template <typename LabelType>
650 void JumpIfInstanceTypeIsNotSequentialAscii(
651 Register instance_type,
652 Register scratch,
653 LabelType *on_not_flat_ascii_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000654
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000655 template <typename LabelType>
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000656 void JumpIfBothInstanceTypesAreNotSequentialAscii(
657 Register first_object_instance_type,
658 Register second_object_instance_type,
659 Register scratch1,
660 Register scratch2,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000661 LabelType* on_fail);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000662
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000664 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000665
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000666 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000667 void Set(Register dst, int64_t x);
668 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000669
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000670 // Move if the registers are not identical.
671 void Move(Register target, Register source);
672
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000673 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000674 void Move(Register dst, Handle<Object> source);
675 void Move(const Operand& dst, Handle<Object> source);
676 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000677 void Cmp(const Operand& dst, Handle<Object> source);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000678 void Cmp(Register dst, Smi* src);
679 void Cmp(const Operand& dst, Smi* src);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000680 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000681
682 // Emit code to discard a non-negative number of pointer-sized elements
683 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000684 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000685
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000686 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000687
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000688 // Control Flow
689 void Jump(Address destination, RelocInfo::Mode rmode);
690 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000691 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
692
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000693 void Call(Address destination, RelocInfo::Mode rmode);
694 void Call(ExternalReference ext);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000695 void Call(Handle<Code> code_object,
696 RelocInfo::Mode rmode,
697 unsigned ast_id = kNoASTId);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000698
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000699 // The size of the code generated for different call instructions.
700 int CallSize(Address destination, RelocInfo::Mode rmode) {
701 return kCallInstructionLength;
702 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000703 int CallSize(ExternalReference ext);
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +0000704 int CallSize(Handle<Code> code_object) {
705 // Code calls use 32-bit relative addressing.
706 return kShortCallInstructionLength;
707 }
708 int CallSize(Register target) {
709 // Opcode: REX_opt FF /2 m64
710 return (target.high_bit() != 0) ? 3 : 2;
711 }
712 int CallSize(const Operand& target) {
713 // Opcode: REX_opt FF /2 m64
714 return (target.requires_rex() ? 2 : 1) + target.operand_size();
715 }
716
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000717 // Emit call to the code we are currently generating.
718 void CallSelf() {
719 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
720 Call(self, RelocInfo::CODE_TARGET);
721 }
722
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000723 // Non-x64 instructions.
724 // Push/pop all general purpose registers.
725 // Does not push rsp/rbp nor any of the assembler's special purpose registers
726 // (kScratchRegister, kSmiConstantRegister, kRootRegister).
727 void Pushad();
728 void Popad();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000729 // Sets the stack as after performing Popad, without actually loading the
730 // registers.
731 void Dropad();
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000732
ager@chromium.org9085a012009-05-11 19:22:57 +0000733 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000734 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000735 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000736 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000737 void CmpObjectType(Register heap_object, InstanceType type, Register map);
738
739 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000740 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000741 void CmpInstanceType(Register map, InstanceType type);
742
ager@chromium.org5c838252010-02-19 08:53:10 +0000743 // Check if the map of an object is equal to a specified map and
744 // branch to label if not. Skip the smi check if not required
745 // (object is known to be a heap object)
746 void CheckMap(Register obj,
747 Handle<Map> map,
748 Label* fail,
749 bool is_heap_object);
750
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000751 // Check if the object in register heap_object is a string. Afterwards the
752 // register map contains the object map and the register instance_type
753 // contains the instance_type. The registers map and instance_type can be the
754 // same in which case it contains the instance type afterwards. Either of the
755 // registers map and instance_type can be the same as heap_object.
756 Condition IsObjectStringType(Register heap_object,
757 Register map,
758 Register instance_type);
759
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000760 // FCmp compares and pops the two values on top of the FPU stack.
761 // The flag results are similar to integer cmp, but requires unsigned
ager@chromium.org9085a012009-05-11 19:22:57 +0000762 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
763 void FCmp();
764
ager@chromium.org5c838252010-02-19 08:53:10 +0000765 // Abort execution if argument is not a number. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000766 void AbortIfNotNumber(Register object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000767
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000768 // Abort execution if argument is a smi. Used in debug code.
769 void AbortIfSmi(Register object);
770
lrn@chromium.org25156de2010-04-06 13:10:27 +0000771 // Abort execution if argument is not a smi. Used in debug code.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000772 void AbortIfNotSmi(Register object);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000773 void AbortIfNotSmi(const Operand& object);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000774
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000775 // Abort execution if argument is a string. Used in debug code.
776 void AbortIfNotString(Register object);
777
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000778 // Abort execution if argument is not the root value with the given index.
779 void AbortIfNotRootValue(Register src,
780 Heap::RootListIndex root_value_index,
781 const char* message);
782
ager@chromium.org9085a012009-05-11 19:22:57 +0000783 // ---------------------------------------------------------------------------
784 // Exception handling
785
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000786 // Push a new try handler and link into try handler chain. The return
787 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000788 void PushTryHandler(CodeLocation try_location, HandlerType type);
789
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000790 // Unlink the stack handler on top of the stack from the try handler chain.
791 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000792
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000793 // Activate the top handler in the try hander chain and pass the
794 // thrown value.
795 void Throw(Register value);
796
797 // Propagate an uncatchable exception out of the current JS stack.
798 void ThrowUncatchable(UncatchableExceptionType type, Register value);
799
ager@chromium.org9085a012009-05-11 19:22:57 +0000800 // ---------------------------------------------------------------------------
801 // Inline caching support
802
ager@chromium.org9085a012009-05-11 19:22:57 +0000803 // Generate code for checking access rights - used for security checks
804 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000805 // is left untouched, but the scratch register and kScratchRegister,
806 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000807 void CheckAccessGlobalProxy(Register holder_reg,
808 Register scratch,
809 Label* miss);
810
811
812 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000813 // Allocation support
814
815 // Allocate an object in new space. If the new space is exhausted control
816 // continues at the gc_required label. The allocated object is returned in
817 // result and end of the new object is returned in result_end. The register
818 // scratch can be passed as no_reg in which case an additional object
819 // reference will be added to the reloc info. The returned pointers in result
820 // and result_end have not yet been tagged as heap objects. If
821 // result_contains_top_on_entry is true the content of result is known to be
822 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000823 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000824 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000825 void AllocateInNewSpace(int object_size,
826 Register result,
827 Register result_end,
828 Register scratch,
829 Label* gc_required,
830 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000831
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000832 void AllocateInNewSpace(int header_size,
833 ScaleFactor element_size,
834 Register element_count,
835 Register result,
836 Register result_end,
837 Register scratch,
838 Label* gc_required,
839 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000840
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000841 void AllocateInNewSpace(Register object_size,
842 Register result,
843 Register result_end,
844 Register scratch,
845 Label* gc_required,
846 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000847
848 // Undo allocation in new space. The object passed and objects allocated after
849 // it will no longer be allocated. Make sure that no pointers are left to the
850 // object(s) no longer allocated as they would be invalid when allocation is
851 // un-done.
852 void UndoAllocationInNewSpace(Register object);
853
ager@chromium.org3811b432009-10-28 14:53:37 +0000854 // Allocate a heap number in new space with undefined value. Returns
855 // tagged pointer in result register, or jumps to gc_required if new
856 // space is full.
857 void AllocateHeapNumber(Register result,
858 Register scratch,
859 Label* gc_required);
860
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000861 // Allocate a sequential string. All the header fields of the string object
862 // are initialized.
863 void AllocateTwoByteString(Register result,
864 Register length,
865 Register scratch1,
866 Register scratch2,
867 Register scratch3,
868 Label* gc_required);
869 void AllocateAsciiString(Register result,
870 Register length,
871 Register scratch1,
872 Register scratch2,
873 Register scratch3,
874 Label* gc_required);
875
876 // Allocate a raw cons string object. Only the map field of the result is
877 // initialized.
878 void AllocateConsString(Register result,
879 Register scratch1,
880 Register scratch2,
881 Label* gc_required);
882 void AllocateAsciiConsString(Register result,
883 Register scratch1,
884 Register scratch2,
885 Label* gc_required);
886
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000887 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000888 // Support functions.
889
890 // Check if result is zero and op is negative.
891 void NegativeZeroTest(Register result, Register op, Label* then_label);
892
893 // Check if result is zero and op is negative in code using jump targets.
894 void NegativeZeroTest(CodeGenerator* cgen,
895 Register result,
896 Register op,
897 JumpTarget* then_target);
898
899 // Check if result is zero and any of op1 and op2 are negative.
900 // Register scratch is destroyed, and it must be different from op2.
901 void NegativeZeroTest(Register result, Register op1, Register op2,
902 Register scratch, Label* then_label);
903
904 // Try to get function prototype of a function and puts the value in
905 // the result register. Checks that the function really is a
906 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000907 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000908 // clobbered.
909 void TryGetFunctionPrototype(Register function,
910 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000911 Label* miss);
912
913 // Generates code for reporting that an illegal operation has
914 // occurred.
915 void IllegalOperation(int num_arguments);
916
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000917 // Picks out an array index from the hash field.
918 // Register use:
919 // hash - holds the index's hash. Clobbered.
920 // index - holds the overwritten index on exit.
921 void IndexFromHash(Register hash, Register index);
922
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000923 // Find the function context up the context chain.
924 void LoadContext(Register dst, int context_chain_length);
925
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000926 // Load the global function with the given index.
927 void LoadGlobalFunction(int index, Register function);
928
929 // Load the initial map from the global function. The registers
930 // function and map can be the same.
931 void LoadGlobalFunctionInitialMap(Register function, Register map);
932
ager@chromium.org9085a012009-05-11 19:22:57 +0000933 // ---------------------------------------------------------------------------
934 // Runtime calls
935
936 // Call a code stub.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000937 void CallStub(CodeStub* stub, unsigned ast_id = kNoASTId);
ager@chromium.org9085a012009-05-11 19:22:57 +0000938
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000939 // Call a code stub and return the code object called. Try to generate
940 // the code if necessary. Do not perform a GC but instead return a retry
941 // after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000942 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000943
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000944 // Tail call a code stub (jump).
945 void TailCallStub(CodeStub* stub);
946
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000947 // Tail call a code stub (jump) and return the code object called. Try to
948 // generate the code if necessary. Do not perform a GC but instead return
949 // a retry after GC failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000950 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000951
ager@chromium.org9085a012009-05-11 19:22:57 +0000952 // Return from a code stub after popping its arguments.
953 void StubReturn(int argc);
954
955 // Call a runtime routine.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000956 void CallRuntime(const Runtime::Function* f, int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000957
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000958 // Call a runtime function and save the value of XMM registers.
959 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
960
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000961 // Call a runtime function, returning the CodeStub object called.
962 // Try to generate the stub code if necessary. Do not perform a GC
963 // but instead return a retry after GC failure.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000964 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
lrn@chromium.org303ada72010-10-27 09:33:13 +0000965 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000966
ager@chromium.org9085a012009-05-11 19:22:57 +0000967 // Convenience function: Same as above, but takes the fid instead.
968 void CallRuntime(Runtime::FunctionId id, int num_arguments);
969
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000970 // Convenience function: Same as above, but takes the fid instead.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000971 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
972 int num_arguments);
whesse@chromium.orge90029b2010-08-02 11:52:17 +0000973
ager@chromium.org5c838252010-02-19 08:53:10 +0000974 // Convenience function: call an external reference.
975 void CallExternalReference(const ExternalReference& ext,
976 int num_arguments);
977
ager@chromium.org9085a012009-05-11 19:22:57 +0000978 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000979 // Like JumpToExternalReference, but also takes care of passing the number
980 // of parameters.
981 void TailCallExternalReference(const ExternalReference& ext,
982 int num_arguments,
983 int result_size);
984
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000985 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
986 const ExternalReference& ext, int num_arguments, int result_size);
987
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000988 // Convenience function: tail call a runtime routine (jump).
989 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000990 int num_arguments,
991 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000992
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000993 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
994 int num_arguments,
995 int result_size);
996
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000997 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000998 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000999
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001000 // Jump to a runtime routine.
1001 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext,
1002 int result_size);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001003
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001004 // Prepares stack to put arguments (aligns and so on).
1005 // WIN64 calling convention requires to put the pointer to the return value
1006 // slot into rcx (rcx must be preserverd until TryCallApiFunctionAndReturn).
1007 // Saves context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
1008 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
1009 void PrepareCallApiFunction(int arg_stack_space);
1010
1011 // Calls an API function. Allocates HandleScope, extracts
1012 // returned value from handle and propagates exceptions.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001013 // Clobbers r14, r15, rbx and caller-save registers. Restores context.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001014 // On return removes stack_space * kPointerSize (GCed).
1015 MUST_USE_RESULT MaybeObject* TryCallApiFunctionAndReturn(
1016 ApiFunction* function, int stack_space);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001017
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001018 // Before calling a C-function from generated code, align arguments on stack.
1019 // After aligning the frame, arguments must be stored in esp[0], esp[4],
1020 // etc., not pushed. The argument count assumes all arguments are word sized.
1021 // The number of slots reserved for arguments depends on platform. On Windows
1022 // stack slots are reserved for the arguments passed in registers. On other
1023 // platforms stack slots are only reserved for the arguments actually passed
1024 // on the stack.
1025 void PrepareCallCFunction(int num_arguments);
1026
1027 // Calls a C function and cleans up the space for arguments allocated
1028 // by PrepareCallCFunction. The called function is not allowed to trigger a
1029 // garbage collection, since that might move the code and invalidate the
1030 // return address (unless this is somehow accounted for by the called
1031 // function).
1032 void CallCFunction(ExternalReference function, int num_arguments);
1033 void CallCFunction(Register function, int num_arguments);
1034
1035 // Calculate the number of stack slots to reserve for arguments when calling a
1036 // C function.
1037 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +00001038
1039 // ---------------------------------------------------------------------------
1040 // Utilities
1041
1042 void Ret();
1043
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001044 // Return and drop arguments from stack, where the number of arguments
1045 // may be bigger than 2^16 - 1. Requires a scratch register.
1046 void Ret(int bytes_dropped, Register scratch);
1047
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001048 Handle<Object> CodeObject() {
1049 ASSERT(!code_object_.is_null());
1050 return code_object_;
1051 }
ager@chromium.org9085a012009-05-11 19:22:57 +00001052
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001053 // Copy length bytes from source to destination.
1054 // Uses scratch register internally (if you have a low-eight register
1055 // free, do use it, otherwise kScratchRegister will be used).
1056 // The min_length is a minimum limit on the value that length will have.
1057 // The algorithm has some special cases that might be omitted if the string
1058 // is known to always be long.
1059 void CopyBytes(Register destination,
1060 Register source,
1061 Register length,
1062 int min_length = 0,
1063 Register scratch = kScratchRegister);
1064
ager@chromium.org9085a012009-05-11 19:22:57 +00001065
1066 // ---------------------------------------------------------------------------
1067 // StatsCounter support
1068
1069 void SetCounter(StatsCounter* counter, int value);
1070 void IncrementCounter(StatsCounter* counter, int value);
1071 void DecrementCounter(StatsCounter* counter, int value);
1072
1073
1074 // ---------------------------------------------------------------------------
1075 // Debugging
1076
1077 // Calls Abort(msg) if the condition cc is not satisfied.
1078 // Use --debug_code to enable.
1079 void Assert(Condition cc, const char* msg);
1080
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001081 void AssertFastElements(Register elements);
1082
ager@chromium.org9085a012009-05-11 19:22:57 +00001083 // Like Assert(), but always enabled.
1084 void Check(Condition cc, const char* msg);
1085
1086 // Print a message to stdout and abort execution.
1087 void Abort(const char* msg);
1088
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001089 // Check that the stack is aligned.
1090 void CheckStackAlignment();
1091
ager@chromium.org9085a012009-05-11 19:22:57 +00001092 // Verify restrictions about code generated in stubs.
1093 void set_generating_stub(bool value) { generating_stub_ = value; }
1094 bool generating_stub() { return generating_stub_; }
1095 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
1096 bool allow_stub_calls() { return allow_stub_calls_; }
1097
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001098 static int SafepointRegisterStackIndex(Register reg) {
1099 return SafepointRegisterStackIndex(reg.code());
1100 }
1101
ager@chromium.org9085a012009-05-11 19:22:57 +00001102 private:
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001103 // Order general registers are pushed by Pushad.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001104 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r14, r15.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001105 static int kSafepointPushRegisterIndices[Register::kNumRegisters];
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001106 static const int kNumSafepointSavedRegisters = 11;
1107
ager@chromium.org9085a012009-05-11 19:22:57 +00001108 bool generating_stub_;
1109 bool allow_stub_calls_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001110 bool root_array_available_;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001111
1112 // Returns a register holding the smi value. The register MUST NOT be
1113 // modified. It may be the "smi 1 constant" register.
1114 Register GetSmiConstant(Smi* value);
1115
1116 // Moves the smi value to the destination register.
1117 void LoadSmiConstant(Register dst, Smi* value);
1118
ager@chromium.org5c838252010-02-19 08:53:10 +00001119 // This handle will be patched with the code object on installation.
1120 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +00001121
1122 // Helper functions for generating invokes.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001123 template <typename LabelType>
ager@chromium.org9085a012009-05-11 19:22:57 +00001124 void InvokePrologue(const ParameterCount& expected,
1125 const ParameterCount& actual,
1126 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001127 Register code_register,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001128 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001129 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001130 CallWrapper* call_wrapper);
ager@chromium.org9085a012009-05-11 19:22:57 +00001131
ager@chromium.org9085a012009-05-11 19:22:57 +00001132 // Activation support.
1133 void EnterFrame(StackFrame::Type type);
1134 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001135
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001136 void EnterExitFramePrologue(bool save_rax);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001137
1138 // Allocates arg_stack_space * kPointerSize memory (not GCed) on the stack
1139 // accessible via StackSpaceOperand.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001140 void EnterExitFrameEpilogue(int arg_stack_space, bool save_doubles);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001141
1142 void LeaveExitFrameEpilogue();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001143
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001144 // Allocation support helpers.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001145 // Loads the top of new-space into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +00001146 // Otherwise the address of the new-space top is loaded into scratch (if
1147 // scratch is valid), and the new-space top is loaded into result.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001148 void LoadAllocationTopHelper(Register result,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001149 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +00001150 AllocationFlags flags);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001151 // Update allocation top with value in result_end register.
1152 // If scratch is valid, it contains the address of the allocation top.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001153 void UpdateAllocationTopHelper(Register result_end, Register scratch);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001154
1155 // Helper for PopHandleScope. Allowed to perform a GC and returns
1156 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
1157 // possibly returns a failure object indicating an allocation failure.
1158 Object* PopHandleScopeHelper(Register saved,
1159 Register scratch,
1160 bool gc_allowed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001161
1162
1163 // Compute memory operands for safepoint stack slots.
1164 Operand SafepointRegisterSlot(Register reg);
1165 static int SafepointRegisterStackIndex(int reg_code) {
1166 return kNumSafepointRegisters - kSafepointPushRegisterIndices[reg_code] - 1;
1167 }
1168
1169 // Needs access to SafepointRegisterStackIndex for optimized frame
1170 // traversal.
1171 friend class OptimizedFrame;
ager@chromium.org9085a012009-05-11 19:22:57 +00001172};
1173
1174
ager@chromium.org4af710e2009-09-15 12:20:11 +00001175// The code patcher is used to patch (typically) small parts of code e.g. for
1176// debugging and other types of instrumentation. When using the code patcher
1177// the exact number of bytes specified must be emitted. Is not legal to emit
1178// relocation information. If any of these constraints are violated it causes
1179// an assertion.
1180class CodePatcher {
1181 public:
1182 CodePatcher(byte* address, int size);
1183 virtual ~CodePatcher();
1184
1185 // Macro assembler to emit code.
1186 MacroAssembler* masm() { return &masm_; }
1187
1188 private:
1189 byte* address_; // The address of the code being patched.
1190 int size_; // Number of bytes of the expected patch size.
1191 MacroAssembler masm_; // Macro assembler used to generate the code.
1192};
1193
1194
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001195// Helper class for generating code or data associated with the code
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001196// right before or after a call instruction. As an example this can be used to
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001197// generate safepoint data after calls for crankshaft.
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001198class CallWrapper {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001199 public:
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001200 CallWrapper() { }
1201 virtual ~CallWrapper() { }
1202 // Called just before emitting a call. Argument is the size of the generated
1203 // call code.
1204 virtual void BeforeCall(int call_size) = 0;
1205 // Called just after emitting a call, i.e., at the return site for the call.
1206 virtual void AfterCall() = 0;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001207};
1208
1209
ager@chromium.org9085a012009-05-11 19:22:57 +00001210// -----------------------------------------------------------------------------
1211// Static helper functions.
1212
1213// Generate an Operand for loading a field from an object.
1214static inline Operand FieldOperand(Register object, int offset) {
1215 return Operand(object, offset - kHeapObjectTag);
1216}
1217
1218
1219// Generate an Operand for loading an indexed field from an object.
1220static inline Operand FieldOperand(Register object,
1221 Register index,
1222 ScaleFactor scale,
1223 int offset) {
1224 return Operand(object, index, scale, offset - kHeapObjectTag);
1225}
1226
1227
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001228static inline Operand ContextOperand(Register context, int index) {
1229 return Operand(context, Context::SlotOffset(index));
1230}
1231
1232
1233static inline Operand GlobalObjectOperand() {
1234 return ContextOperand(rsi, Context::GLOBAL_INDEX);
1235}
1236
1237
1238// Provides access to exit frame stack space (not GCed).
1239static inline Operand StackSpaceOperand(int index) {
1240#ifdef _WIN64
1241 const int kShaddowSpace = 4;
1242 return Operand(rsp, (index + kShaddowSpace) * kPointerSize);
1243#else
1244 return Operand(rsp, index * kPointerSize);
1245#endif
1246}
1247
1248
1249
ager@chromium.org9085a012009-05-11 19:22:57 +00001250#ifdef GENERATED_CODE_COVERAGE
1251extern void LogGeneratedCodeCoverage(const char* file_line);
1252#define CODE_COVERAGE_STRINGIFY(x) #x
1253#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1254#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1255#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001256 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +00001257 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
1258 masm->pushfd(); \
1259 masm->pushad(); \
1260 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001261 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +00001262 masm->pop(rax); \
1263 masm->popad(); \
1264 masm->popfd(); \
1265 } \
1266 masm->
1267#else
1268#define ACCESS_MASM(masm) masm->
1269#endif
1270
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001271// -----------------------------------------------------------------------------
1272// Template implementations.
1273
1274static int kSmiShift = kSmiTagSize + kSmiShiftSize;
1275
1276
1277template <typename LabelType>
1278void MacroAssembler::SmiNeg(Register dst,
1279 Register src,
1280 LabelType* on_smi_result) {
1281 if (dst.is(src)) {
1282 ASSERT(!dst.is(kScratchRegister));
1283 movq(kScratchRegister, src);
1284 neg(dst); // Low 32 bits are retained as zero by negation.
1285 // Test if result is zero or Smi::kMinValue.
1286 cmpq(dst, kScratchRegister);
1287 j(not_equal, on_smi_result);
1288 movq(src, kScratchRegister);
1289 } else {
1290 movq(dst, src);
1291 neg(dst);
1292 cmpq(dst, src);
1293 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1294 j(not_equal, on_smi_result);
1295 }
1296}
1297
1298
1299template <typename LabelType>
1300void MacroAssembler::SmiAdd(Register dst,
1301 Register src1,
1302 Register src2,
1303 LabelType* on_not_smi_result) {
1304 ASSERT_NOT_NULL(on_not_smi_result);
1305 ASSERT(!dst.is(src2));
1306 if (dst.is(src1)) {
1307 movq(kScratchRegister, src1);
1308 addq(kScratchRegister, src2);
1309 j(overflow, on_not_smi_result);
1310 movq(dst, kScratchRegister);
1311 } else {
1312 movq(dst, src1);
1313 addq(dst, src2);
1314 j(overflow, on_not_smi_result);
1315 }
1316}
1317
1318
1319template <typename LabelType>
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001320void MacroAssembler::SmiAdd(Register dst,
1321 Register src1,
1322 const Operand& src2,
1323 LabelType* on_not_smi_result) {
1324 ASSERT_NOT_NULL(on_not_smi_result);
1325 if (dst.is(src1)) {
1326 movq(kScratchRegister, src1);
1327 addq(kScratchRegister, src2);
1328 j(overflow, on_not_smi_result);
1329 movq(dst, kScratchRegister);
1330 } else {
1331 ASSERT(!src2.AddressUsesRegister(dst));
1332 movq(dst, src1);
1333 addq(dst, src2);
1334 j(overflow, on_not_smi_result);
1335 }
1336}
1337
1338
1339template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001340void MacroAssembler::SmiSub(Register dst,
1341 Register src1,
1342 Register src2,
1343 LabelType* on_not_smi_result) {
1344 ASSERT_NOT_NULL(on_not_smi_result);
1345 ASSERT(!dst.is(src2));
1346 if (dst.is(src1)) {
1347 cmpq(dst, src2);
1348 j(overflow, on_not_smi_result);
1349 subq(dst, src2);
1350 } else {
1351 movq(dst, src1);
1352 subq(dst, src2);
1353 j(overflow, on_not_smi_result);
1354 }
1355}
1356
1357
1358template <typename LabelType>
1359void MacroAssembler::SmiSub(Register dst,
1360 Register src1,
1361 const Operand& src2,
1362 LabelType* on_not_smi_result) {
1363 ASSERT_NOT_NULL(on_not_smi_result);
1364 if (dst.is(src1)) {
1365 movq(kScratchRegister, src2);
1366 cmpq(src1, kScratchRegister);
1367 j(overflow, on_not_smi_result);
1368 subq(src1, kScratchRegister);
1369 } else {
1370 movq(dst, src1);
1371 subq(dst, src2);
1372 j(overflow, on_not_smi_result);
1373 }
1374}
1375
1376
1377template <typename LabelType>
1378void MacroAssembler::SmiMul(Register dst,
1379 Register src1,
1380 Register src2,
1381 LabelType* on_not_smi_result) {
1382 ASSERT(!dst.is(src2));
1383 ASSERT(!dst.is(kScratchRegister));
1384 ASSERT(!src1.is(kScratchRegister));
1385 ASSERT(!src2.is(kScratchRegister));
1386
1387 if (dst.is(src1)) {
1388 NearLabel failure, zero_correct_result;
1389 movq(kScratchRegister, src1); // Create backup for later testing.
1390 SmiToInteger64(dst, src1);
1391 imul(dst, src2);
1392 j(overflow, &failure);
1393
1394 // Check for negative zero result. If product is zero, and one
1395 // argument is negative, go to slow case.
1396 NearLabel correct_result;
1397 testq(dst, dst);
1398 j(not_zero, &correct_result);
1399
1400 movq(dst, kScratchRegister);
1401 xor_(dst, src2);
1402 j(positive, &zero_correct_result); // Result was positive zero.
1403
1404 bind(&failure); // Reused failure exit, restores src1.
1405 movq(src1, kScratchRegister);
1406 jmp(on_not_smi_result);
1407
1408 bind(&zero_correct_result);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001409 Set(dst, 0);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001410
1411 bind(&correct_result);
1412 } else {
1413 SmiToInteger64(dst, src1);
1414 imul(dst, src2);
1415 j(overflow, on_not_smi_result);
1416 // Check for negative zero result. If product is zero, and one
1417 // argument is negative, go to slow case.
1418 NearLabel correct_result;
1419 testq(dst, dst);
1420 j(not_zero, &correct_result);
1421 // One of src1 and src2 is zero, the check whether the other is
1422 // negative.
1423 movq(kScratchRegister, src1);
1424 xor_(kScratchRegister, src2);
1425 j(negative, on_not_smi_result);
1426 bind(&correct_result);
1427 }
1428}
1429
1430
1431template <typename LabelType>
1432void MacroAssembler::SmiTryAddConstant(Register dst,
1433 Register src,
1434 Smi* constant,
1435 LabelType* on_not_smi_result) {
1436 // Does not assume that src is a smi.
1437 ASSERT_EQ(static_cast<int>(1), static_cast<int>(kSmiTagMask));
1438 ASSERT_EQ(0, kSmiTag);
1439 ASSERT(!dst.is(kScratchRegister));
1440 ASSERT(!src.is(kScratchRegister));
1441
1442 JumpIfNotSmi(src, on_not_smi_result);
1443 Register tmp = (dst.is(src) ? kScratchRegister : dst);
1444 LoadSmiConstant(tmp, constant);
1445 addq(tmp, src);
1446 j(overflow, on_not_smi_result);
1447 if (dst.is(src)) {
1448 movq(dst, tmp);
1449 }
1450}
1451
1452
1453template <typename LabelType>
1454void MacroAssembler::SmiAddConstant(Register dst,
1455 Register src,
1456 Smi* constant,
1457 LabelType* on_not_smi_result) {
1458 if (constant->value() == 0) {
1459 if (!dst.is(src)) {
1460 movq(dst, src);
1461 }
1462 } else if (dst.is(src)) {
1463 ASSERT(!dst.is(kScratchRegister));
1464
1465 LoadSmiConstant(kScratchRegister, constant);
1466 addq(kScratchRegister, src);
1467 j(overflow, on_not_smi_result);
1468 movq(dst, kScratchRegister);
1469 } else {
1470 LoadSmiConstant(dst, constant);
1471 addq(dst, src);
1472 j(overflow, on_not_smi_result);
1473 }
1474}
1475
1476
1477template <typename LabelType>
1478void MacroAssembler::SmiSubConstant(Register dst,
1479 Register src,
1480 Smi* constant,
1481 LabelType* on_not_smi_result) {
1482 if (constant->value() == 0) {
1483 if (!dst.is(src)) {
1484 movq(dst, src);
1485 }
1486 } else if (dst.is(src)) {
1487 ASSERT(!dst.is(kScratchRegister));
1488 if (constant->value() == Smi::kMinValue) {
1489 // Subtracting min-value from any non-negative value will overflow.
1490 // We test the non-negativeness before doing the subtraction.
1491 testq(src, src);
1492 j(not_sign, on_not_smi_result);
1493 LoadSmiConstant(kScratchRegister, constant);
1494 subq(dst, kScratchRegister);
1495 } else {
1496 // Subtract by adding the negation.
1497 LoadSmiConstant(kScratchRegister, Smi::FromInt(-constant->value()));
1498 addq(kScratchRegister, dst);
1499 j(overflow, on_not_smi_result);
1500 movq(dst, kScratchRegister);
1501 }
1502 } else {
1503 if (constant->value() == Smi::kMinValue) {
1504 // Subtracting min-value from any non-negative value will overflow.
1505 // We test the non-negativeness before doing the subtraction.
1506 testq(src, src);
1507 j(not_sign, on_not_smi_result);
1508 LoadSmiConstant(dst, constant);
1509 // Adding and subtracting the min-value gives the same result, it only
1510 // differs on the overflow bit, which we don't check here.
1511 addq(dst, src);
1512 } else {
1513 // Subtract by adding the negation.
1514 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1515 addq(dst, src);
1516 j(overflow, on_not_smi_result);
1517 }
1518 }
1519}
1520
1521
1522template <typename LabelType>
1523void MacroAssembler::SmiDiv(Register dst,
1524 Register src1,
1525 Register src2,
1526 LabelType* on_not_smi_result) {
1527 ASSERT(!src1.is(kScratchRegister));
1528 ASSERT(!src2.is(kScratchRegister));
1529 ASSERT(!dst.is(kScratchRegister));
1530 ASSERT(!src2.is(rax));
1531 ASSERT(!src2.is(rdx));
1532 ASSERT(!src1.is(rdx));
1533
1534 // Check for 0 divisor (result is +/-Infinity).
1535 NearLabel positive_divisor;
1536 testq(src2, src2);
1537 j(zero, on_not_smi_result);
1538
1539 if (src1.is(rax)) {
1540 movq(kScratchRegister, src1);
1541 }
1542 SmiToInteger32(rax, src1);
1543 // We need to rule out dividing Smi::kMinValue by -1, since that would
1544 // overflow in idiv and raise an exception.
1545 // We combine this with negative zero test (negative zero only happens
1546 // when dividing zero by a negative number).
1547
1548 // We overshoot a little and go to slow case if we divide min-value
1549 // by any negative value, not just -1.
1550 NearLabel safe_div;
1551 testl(rax, Immediate(0x7fffffff));
1552 j(not_zero, &safe_div);
1553 testq(src2, src2);
1554 if (src1.is(rax)) {
1555 j(positive, &safe_div);
1556 movq(src1, kScratchRegister);
1557 jmp(on_not_smi_result);
1558 } else {
1559 j(negative, on_not_smi_result);
1560 }
1561 bind(&safe_div);
1562
1563 SmiToInteger32(src2, src2);
1564 // Sign extend src1 into edx:eax.
1565 cdq();
1566 idivl(src2);
1567 Integer32ToSmi(src2, src2);
1568 // Check that the remainder is zero.
1569 testl(rdx, rdx);
1570 if (src1.is(rax)) {
1571 NearLabel smi_result;
1572 j(zero, &smi_result);
1573 movq(src1, kScratchRegister);
1574 jmp(on_not_smi_result);
1575 bind(&smi_result);
1576 } else {
1577 j(not_zero, on_not_smi_result);
1578 }
1579 if (!dst.is(src1) && src1.is(rax)) {
1580 movq(src1, kScratchRegister);
1581 }
1582 Integer32ToSmi(dst, rax);
1583}
1584
1585
1586template <typename LabelType>
1587void MacroAssembler::SmiMod(Register dst,
1588 Register src1,
1589 Register src2,
1590 LabelType* on_not_smi_result) {
1591 ASSERT(!dst.is(kScratchRegister));
1592 ASSERT(!src1.is(kScratchRegister));
1593 ASSERT(!src2.is(kScratchRegister));
1594 ASSERT(!src2.is(rax));
1595 ASSERT(!src2.is(rdx));
1596 ASSERT(!src1.is(rdx));
1597 ASSERT(!src1.is(src2));
1598
1599 testq(src2, src2);
1600 j(zero, on_not_smi_result);
1601
1602 if (src1.is(rax)) {
1603 movq(kScratchRegister, src1);
1604 }
1605 SmiToInteger32(rax, src1);
1606 SmiToInteger32(src2, src2);
1607
1608 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
1609 NearLabel safe_div;
1610 cmpl(rax, Immediate(Smi::kMinValue));
1611 j(not_equal, &safe_div);
1612 cmpl(src2, Immediate(-1));
1613 j(not_equal, &safe_div);
1614 // Retag inputs and go slow case.
1615 Integer32ToSmi(src2, src2);
1616 if (src1.is(rax)) {
1617 movq(src1, kScratchRegister);
1618 }
1619 jmp(on_not_smi_result);
1620 bind(&safe_div);
1621
1622 // Sign extend eax into edx:eax.
1623 cdq();
1624 idivl(src2);
1625 // Restore smi tags on inputs.
1626 Integer32ToSmi(src2, src2);
1627 if (src1.is(rax)) {
1628 movq(src1, kScratchRegister);
1629 }
1630 // Check for a negative zero result. If the result is zero, and the
1631 // dividend is negative, go slow to return a floating point negative zero.
1632 NearLabel smi_result;
1633 testl(rdx, rdx);
1634 j(not_zero, &smi_result);
1635 testq(src1, src1);
1636 j(negative, on_not_smi_result);
1637 bind(&smi_result);
1638 Integer32ToSmi(dst, rdx);
1639}
1640
1641
1642template <typename LabelType>
1643void MacroAssembler::SmiShiftLogicalRightConstant(
1644 Register dst, Register src, int shift_value, LabelType* on_not_smi_result) {
1645 // Logic right shift interprets its result as an *unsigned* number.
1646 if (dst.is(src)) {
1647 UNIMPLEMENTED(); // Not used.
1648 } else {
1649 movq(dst, src);
1650 if (shift_value == 0) {
1651 testq(dst, dst);
1652 j(negative, on_not_smi_result);
1653 }
1654 shr(dst, Immediate(shift_value + kSmiShift));
1655 shl(dst, Immediate(kSmiShift));
1656 }
1657}
1658
1659
1660template <typename LabelType>
1661void MacroAssembler::SmiShiftLogicalRight(Register dst,
1662 Register src1,
1663 Register src2,
1664 LabelType* on_not_smi_result) {
1665 ASSERT(!dst.is(kScratchRegister));
1666 ASSERT(!src1.is(kScratchRegister));
1667 ASSERT(!src2.is(kScratchRegister));
1668 ASSERT(!dst.is(rcx));
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001669 // dst and src1 can be the same, because the one case that bails out
1670 // is a shift by 0, which leaves dst, and therefore src1, unchanged.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001671 NearLabel result_ok;
1672 if (src1.is(rcx) || src2.is(rcx)) {
1673 movq(kScratchRegister, rcx);
1674 }
1675 if (!dst.is(src1)) {
1676 movq(dst, src1);
1677 }
1678 SmiToInteger32(rcx, src2);
1679 orl(rcx, Immediate(kSmiShift));
1680 shr_cl(dst); // Shift is rcx modulo 0x1f + 32.
1681 shl(dst, Immediate(kSmiShift));
1682 testq(dst, dst);
1683 if (src1.is(rcx) || src2.is(rcx)) {
1684 NearLabel positive_result;
1685 j(positive, &positive_result);
1686 if (src1.is(rcx)) {
1687 movq(src1, kScratchRegister);
1688 } else {
1689 movq(src2, kScratchRegister);
1690 }
1691 jmp(on_not_smi_result);
1692 bind(&positive_result);
1693 } else {
1694 j(negative, on_not_smi_result); // src2 was zero and src1 negative.
1695 }
1696}
1697
1698
1699template <typename LabelType>
1700void MacroAssembler::SelectNonSmi(Register dst,
1701 Register src1,
1702 Register src2,
1703 LabelType* on_not_smis) {
1704 ASSERT(!dst.is(kScratchRegister));
1705 ASSERT(!src1.is(kScratchRegister));
1706 ASSERT(!src2.is(kScratchRegister));
1707 ASSERT(!dst.is(src1));
1708 ASSERT(!dst.is(src2));
1709 // Both operands must not be smis.
1710#ifdef DEBUG
1711 if (allow_stub_calls()) { // Check contains a stub call.
1712 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
1713 Check(not_both_smis, "Both registers were smis in SelectNonSmi.");
1714 }
1715#endif
1716 ASSERT_EQ(0, kSmiTag);
1717 ASSERT_EQ(0, Smi::FromInt(0));
1718 movl(kScratchRegister, Immediate(kSmiTagMask));
1719 and_(kScratchRegister, src1);
1720 testl(kScratchRegister, src2);
1721 // If non-zero then both are smis.
1722 j(not_zero, on_not_smis);
1723
1724 // Exactly one operand is a smi.
1725 ASSERT_EQ(1, static_cast<int>(kSmiTagMask));
1726 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
1727 subq(kScratchRegister, Immediate(1));
1728 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
1729 movq(dst, src1);
1730 xor_(dst, src2);
1731 and_(dst, kScratchRegister);
1732 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
1733 xor_(dst, src1);
1734 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
1735}
1736
1737
1738template <typename LabelType>
1739void MacroAssembler::JumpIfSmi(Register src, LabelType* on_smi) {
1740 ASSERT_EQ(0, kSmiTag);
1741 Condition smi = CheckSmi(src);
1742 j(smi, on_smi);
1743}
1744
1745
1746template <typename LabelType>
1747void MacroAssembler::JumpIfNotSmi(Register src, LabelType* on_not_smi) {
1748 Condition smi = CheckSmi(src);
1749 j(NegateCondition(smi), on_not_smi);
1750}
1751
1752
1753template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001754void MacroAssembler::JumpUnlessNonNegativeSmi(
1755 Register src, LabelType* on_not_smi_or_negative) {
1756 Condition non_negative_smi = CheckNonNegativeSmi(src);
1757 j(NegateCondition(non_negative_smi), on_not_smi_or_negative);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001758}
1759
1760
1761template <typename LabelType>
1762void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1763 Smi* constant,
1764 LabelType* on_equals) {
1765 SmiCompare(src, constant);
1766 j(equal, on_equals);
1767}
1768
1769
1770template <typename LabelType>
1771void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1772 LabelType* on_invalid) {
1773 Condition is_valid = CheckInteger32ValidSmiValue(src);
1774 j(NegateCondition(is_valid), on_invalid);
1775}
1776
1777
1778template <typename LabelType>
1779void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1780 LabelType* on_invalid) {
1781 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1782 j(NegateCondition(is_valid), on_invalid);
1783}
1784
1785
1786template <typename LabelType>
1787void MacroAssembler::JumpIfNotBothSmi(Register src1,
1788 Register src2,
1789 LabelType* on_not_both_smi) {
1790 Condition both_smi = CheckBothSmi(src1, src2);
1791 j(NegateCondition(both_smi), on_not_both_smi);
1792}
1793
1794
1795template <typename LabelType>
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001796void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1797 Register src2,
1798 LabelType* on_not_both_smi) {
1799 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001800 j(NegateCondition(both_smi), on_not_both_smi);
1801}
1802
1803
1804template <typename LabelType>
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001805void MacroAssembler::SmiOrIfSmis(Register dst, Register src1, Register src2,
1806 LabelType* on_not_smis) {
1807 if (dst.is(src1) || dst.is(src2)) {
1808 ASSERT(!src1.is(kScratchRegister));
1809 ASSERT(!src2.is(kScratchRegister));
1810 movq(kScratchRegister, src1);
1811 or_(kScratchRegister, src2);
1812 JumpIfNotSmi(kScratchRegister, on_not_smis);
1813 movq(dst, kScratchRegister);
1814 } else {
1815 movq(dst, src1);
1816 or_(dst, src2);
1817 JumpIfNotSmi(dst, on_not_smis);
1818 }
1819}
1820
1821
1822template <typename LabelType>
ricow@chromium.org83aa5492011-02-07 12:42:56 +00001823void MacroAssembler::JumpIfNotString(Register object,
1824 Register object_map,
1825 LabelType* not_string) {
1826 Condition is_smi = CheckSmi(object);
1827 j(is_smi, not_string);
1828 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
1829 j(above_equal, not_string);
1830}
1831
1832
1833template <typename LabelType>
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001834void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register first_object,
1835 Register second_object,
1836 Register scratch1,
1837 Register scratch2,
1838 LabelType* on_fail) {
1839 // Check that both objects are not smis.
1840 Condition either_smi = CheckEitherSmi(first_object, second_object);
1841 j(either_smi, on_fail);
1842
1843 // Load instance type for both strings.
1844 movq(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
1845 movq(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
1846 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
1847 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
1848
1849 // Check that both are flat ascii strings.
1850 ASSERT(kNotStringTag != 0);
1851 const int kFlatAsciiStringMask =
1852 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1853 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1854
1855 andl(scratch1, Immediate(kFlatAsciiStringMask));
1856 andl(scratch2, Immediate(kFlatAsciiStringMask));
1857 // Interleave the bits to check both scratch1 and scratch2 in one test.
1858 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1859 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1860 cmpl(scratch1,
1861 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1862 j(not_equal, on_fail);
1863}
1864
1865
1866template <typename LabelType>
1867void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
1868 Register instance_type,
1869 Register scratch,
1870 LabelType *failure) {
1871 if (!scratch.is(instance_type)) {
1872 movl(scratch, instance_type);
1873 }
1874
1875 const int kFlatAsciiStringMask =
1876 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1877
1878 andl(scratch, Immediate(kFlatAsciiStringMask));
1879 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kAsciiStringTag));
1880 j(not_equal, failure);
1881}
1882
1883
1884template <typename LabelType>
1885void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
1886 Register first_object_instance_type,
1887 Register second_object_instance_type,
1888 Register scratch1,
1889 Register scratch2,
1890 LabelType* on_fail) {
1891 // Load instance type for both strings.
1892 movq(scratch1, first_object_instance_type);
1893 movq(scratch2, second_object_instance_type);
1894
1895 // Check that both are flat ascii strings.
1896 ASSERT(kNotStringTag != 0);
1897 const int kFlatAsciiStringMask =
1898 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
1899 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
1900
1901 andl(scratch1, Immediate(kFlatAsciiStringMask));
1902 andl(scratch2, Immediate(kFlatAsciiStringMask));
1903 // Interleave the bits to check both scratch1 and scratch2 in one test.
1904 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
1905 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
1906 cmpl(scratch1,
1907 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
1908 j(not_equal, on_fail);
1909}
1910
1911
1912template <typename LabelType>
1913void MacroAssembler::InNewSpace(Register object,
1914 Register scratch,
1915 Condition cc,
1916 LabelType* branch) {
1917 if (Serializer::enabled()) {
1918 // Can't do arithmetic on external references if it might get serialized.
1919 // The mask isn't really an address. We load it as an external reference in
1920 // case the size of the new space is different between the snapshot maker
1921 // and the running system.
1922 if (scratch.is(object)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001923 movq(kScratchRegister, ExternalReference::new_space_mask(isolate()));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001924 and_(scratch, kScratchRegister);
1925 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001926 movq(scratch, ExternalReference::new_space_mask(isolate()));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001927 and_(scratch, object);
1928 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001929 movq(kScratchRegister, ExternalReference::new_space_start(isolate()));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001930 cmpq(scratch, kScratchRegister);
1931 j(cc, branch);
1932 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001933 ASSERT(is_int32(static_cast<int64_t>(HEAP->NewSpaceMask())));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001934 intptr_t new_space_start =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001935 reinterpret_cast<intptr_t>(HEAP->NewSpaceStart());
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001936 movq(kScratchRegister, -new_space_start, RelocInfo::NONE);
1937 if (scratch.is(object)) {
1938 addq(scratch, kScratchRegister);
1939 } else {
1940 lea(scratch, Operand(object, kScratchRegister, times_1, 0));
1941 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001942 and_(scratch, Immediate(static_cast<int32_t>(HEAP->NewSpaceMask())));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001943 j(cc, branch);
1944 }
1945}
1946
1947
1948template <typename LabelType>
1949void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1950 const ParameterCount& actual,
1951 Handle<Code> code_constant,
1952 Register code_register,
1953 LabelType* done,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001954 InvokeFlag flag,
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00001955 CallWrapper* call_wrapper) {
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001956 bool definitely_matches = false;
1957 NearLabel invoke;
1958 if (expected.is_immediate()) {
1959 ASSERT(actual.is_immediate());
1960 if (expected.immediate() == actual.immediate()) {
1961 definitely_matches = true;
1962 } else {
1963 Set(rax, actual.immediate());
1964 if (expected.immediate() ==
1965 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
1966 // Don't worry about adapting arguments for built-ins that
1967 // don't want that done. Skip adaption code by making it look
1968 // like we have a match between expected and actual number of
1969 // arguments.
1970 definitely_matches = true;
1971 } else {
1972 Set(rbx, expected.immediate());
1973 }
1974 }
1975 } else {
1976 if (actual.is_immediate()) {
1977 // Expected is in register, actual is immediate. This is the
1978 // case when we invoke function values without going through the
1979 // IC mechanism.
1980 cmpq(expected.reg(), Immediate(actual.immediate()));
1981 j(equal, &invoke);
1982 ASSERT(expected.reg().is(rbx));
1983 Set(rax, actual.immediate());
1984 } else if (!expected.reg().is(actual.reg())) {
1985 // Both expected and actual are in (different) registers. This
1986 // is the case when we invoke functions using call and apply.
1987 cmpq(expected.reg(), actual.reg());
1988 j(equal, &invoke);
1989 ASSERT(actual.reg().is(rax));
1990 ASSERT(expected.reg().is(rbx));
1991 }
1992 }
1993
1994 if (!definitely_matches) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001995 Handle<Code> adaptor = isolate()->builtins()->ArgumentsAdaptorTrampoline();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001996 if (!code_constant.is_null()) {
1997 movq(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
1998 addq(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1999 } else if (!code_register.is(rdx)) {
2000 movq(rdx, code_register);
2001 }
2002
2003 if (flag == CALL_FUNCTION) {
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00002004 if (call_wrapper != NULL) call_wrapper->BeforeCall(CallSize(adaptor));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002005 Call(adaptor, RelocInfo::CODE_TARGET);
ricow@chromium.orgeb96f4f2011-03-09 13:41:48 +00002006 if (call_wrapper != NULL) call_wrapper->AfterCall();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002007 jmp(done);
2008 } else {
2009 Jump(adaptor, RelocInfo::CODE_TARGET);
2010 }
2011 bind(&invoke);
2012 }
2013}
2014
ager@chromium.org9085a012009-05-11 19:22:57 +00002015
2016} } // namespace v8::internal
2017
2018#endif // V8_X64_MACRO_ASSEMBLER_X64_H_