blob: aedc3b9ced3416e8cfb83f828c88024a01cc6f53 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// 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
28#ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
33namespace v8 {
34namespace internal {
35
Kristian Monsen25f61362010-05-21 11:50:48 +010036// 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
Steve Blocka7e24c12009-10-30 11:49:00 +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.
Steve Block8defd9f2010-07-08 12:39:36 +010050static const Register kScratchRegister = { 10 }; // r10.
51static const Register kSmiConstantRegister = { 15 }; // r15 (callee save).
52static const Register kRootRegister = { 13 }; // r13 (callee save).
53// Value of smi in kSmiConstantRegister.
54static const int kSmiConstantRegisterValue = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Leon Clarkee46be812010-01-19 14:06:41 +000056// Convenience for platform-independent signatures.
57typedef Operand MemOperand;
58
Steve Blocka7e24c12009-10-30 11:49:00 +000059// Forward declaration.
60class JumpTarget;
61
62struct SmiIndex {
63 SmiIndex(Register index_register, ScaleFactor scale)
64 : reg(index_register),
65 scale(scale) {}
66 Register reg;
67 ScaleFactor scale;
68};
69
70// MacroAssembler implements a collection of frequently used macros.
71class MacroAssembler: public Assembler {
72 public:
73 MacroAssembler(void* buffer, int size);
74
75 void LoadRoot(Register destination, Heap::RootListIndex index);
76 void CompareRoot(Register with, Heap::RootListIndex index);
77 void CompareRoot(Operand with, Heap::RootListIndex index);
78 void PushRoot(Heap::RootListIndex index);
Kristian Monsen25f61362010-05-21 11:50:48 +010079 void StoreRoot(Register source, Heap::RootListIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // ---------------------------------------------------------------------------
82 // GC Support
83
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010084 // For page containing |object| mark region covering |addr| dirty.
85 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010086 // space.
87 void RecordWriteHelper(Register object,
88 Register addr,
89 Register scratch);
90
91 // Check if object is in new space. The condition cc can be equal or
92 // not_equal. If it is equal a jump will be done if the object is on new
93 // space. The register scratch can be object itself, but it will be clobbered.
94 void InNewSpace(Register object,
95 Register scratch,
96 Condition cc,
97 Label* branch);
98
Steve Block8defd9f2010-07-08 12:39:36 +010099 // For page containing |object| mark region covering [object+offset]
100 // dirty. |object| is the object being stored into, |value| is the
101 // object being stored. If |offset| is zero, then the |scratch|
102 // register contains the array index into the elements array
103 // represented as a Smi. All registers are clobbered by the
104 // operation. RecordWrite filters out smis so it does not update the
105 // write barrier if the value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 void RecordWrite(Register object,
107 int offset,
108 Register value,
109 Register scratch);
110
Steve Block8defd9f2010-07-08 12:39:36 +0100111 // For page containing |object| mark region covering [address]
112 // dirty. |object| is the object being stored into, |value| is the
113 // object being stored. All registers are clobbered by the
114 // operation. RecordWrite filters out smis so it does not update
115 // the write barrier if the value is a smi.
116 void RecordWrite(Register object,
117 Register address,
118 Register value);
119
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100120 // For page containing |object| mark region covering [object+offset] dirty.
Steve Block3ce2e202009-11-05 08:53:23 +0000121 // The value is known to not be a smi.
122 // object is the object being stored into, value is the object being stored.
123 // If offset is zero, then the scratch register contains the array index into
124 // the elements array represented as a Smi.
125 // All registers are clobbered by the operation.
126 void RecordWriteNonSmi(Register object,
127 int offset,
128 Register value,
129 Register scratch);
130
Steve Blocka7e24c12009-10-30 11:49:00 +0000131#ifdef ENABLE_DEBUGGER_SUPPORT
132 // ---------------------------------------------------------------------------
133 // Debugger Support
134
135 void SaveRegistersToMemory(RegList regs);
136 void RestoreRegistersFromMemory(RegList regs);
137 void PushRegistersFromMemory(RegList regs);
138 void PopRegistersToMemory(RegList regs);
139 void CopyRegistersFromStackToMemory(Register base,
140 Register scratch,
141 RegList regs);
Andrei Popescu402d9372010-02-26 13:31:12 +0000142 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000143#endif
144
145 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +0000146 // Stack limit support
147
148 // Do simple test for stack overflow. This doesn't handle an overflow.
149 void StackLimitCheck(Label* on_stack_limit_hit);
150
151 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 // Activation frames
153
154 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
155 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
156
157 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
158 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
159
Steve Blockd0582a62009-12-15 09:54:21 +0000160 // Enter specific kind of exit frame; either in normal or
161 // debug mode. Expects the number of arguments in register rax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 // sets up the number of arguments in register rdi and the pointer
163 // to the first argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000164 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Ben Murdochbb769b22010-08-11 14:56:33 +0100166 void EnterApiExitFrame(ExitFrame::Mode mode,
167 int stack_space,
168 int argc,
169 int result_size = 1);
170
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 // Leave the current exit frame. Expects/provides the return value in
172 // register rax:rdx (untouched) and the pointer to the first
173 // argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000174 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000175
176
177 // ---------------------------------------------------------------------------
178 // JavaScript invokes
179
180 // Invoke the JavaScript function code by either calling or jumping.
181 void InvokeCode(Register code,
182 const ParameterCount& expected,
183 const ParameterCount& actual,
184 InvokeFlag flag);
185
186 void InvokeCode(Handle<Code> code,
187 const ParameterCount& expected,
188 const ParameterCount& actual,
189 RelocInfo::Mode rmode,
190 InvokeFlag flag);
191
192 // Invoke the JavaScript function in the given register. Changes the
193 // current context to the context in the function before invoking.
194 void InvokeFunction(Register function,
195 const ParameterCount& actual,
196 InvokeFlag flag);
197
Andrei Popescu402d9372010-02-26 13:31:12 +0000198 void InvokeFunction(JSFunction* function,
199 const ParameterCount& actual,
200 InvokeFlag flag);
201
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 // Invoke specified builtin JavaScript function. Adds an entry to
203 // the unresolved list if the name does not resolve.
204 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
205
206 // Store the code object for the given builtin in the target register.
207 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
208
209
210 // ---------------------------------------------------------------------------
211 // Smi tagging, untagging and operations on tagged smis.
212
Steve Block8defd9f2010-07-08 12:39:36 +0100213 void InitializeSmiConstantRegister() {
214 movq(kSmiConstantRegister,
215 reinterpret_cast<uint64_t>(Smi::FromInt(kSmiConstantRegisterValue)),
216 RelocInfo::NONE);
217 }
218
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 // Conversions between tagged smi values and non-tagged integer values.
220
221 // Tag an integer value. The result must be known to be a valid smi value.
Leon Clarke4515c472010-02-03 11:58:03 +0000222 // Only uses the low 32 bits of the src register. Sets the N and Z flags
223 // based on the value of the resulting integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 void Integer32ToSmi(Register dst, Register src);
225
226 // Tag an integer value if possible, or jump the integer value cannot be
227 // represented as a smi. Only uses the low 32 bit of the src registers.
Steve Block3ce2e202009-11-05 08:53:23 +0000228 // NOTICE: Destroys the dst register even if unsuccessful!
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
230
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100231 // Stores an integer32 value into a memory field that already holds a smi.
232 void Integer32ToSmiField(const Operand& dst, Register src);
233
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 // Adds constant to src and tags the result as a smi.
235 // Result must be a valid smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000236 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
238 // Convert smi to 32-bit integer. I.e., not sign extended into
239 // high 32 bits of destination.
240 void SmiToInteger32(Register dst, Register src);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100241 void SmiToInteger32(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243 // Convert smi to 64-bit integer (sign extended if necessary).
244 void SmiToInteger64(Register dst, Register src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100245 void SmiToInteger64(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000246
247 // Multiply a positive smi's integer value by a power of two.
248 // Provides result as 64-bit integer value.
249 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
250 Register src,
251 int power);
252
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100253 // Divide a positive smi's integer value by a power of two.
254 // Provides result as 32-bit integer value.
255 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
256 Register src,
257 int power);
258
259
Steve Block3ce2e202009-11-05 08:53:23 +0000260 // Simple comparison of smis.
261 void SmiCompare(Register dst, Register src);
262 void SmiCompare(Register dst, Smi* src);
Steve Block6ded16b2010-05-10 14:33:55 +0100263 void SmiCompare(Register dst, const Operand& src);
Steve Block3ce2e202009-11-05 08:53:23 +0000264 void SmiCompare(const Operand& dst, Register src);
265 void SmiCompare(const Operand& dst, Smi* src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100266 // Compare the int32 in src register to the value of the smi stored at dst.
267 void SmiCompareInteger32(const Operand& dst, Register src);
Steve Block3ce2e202009-11-05 08:53:23 +0000268 // Sets sign and zero flags depending on value of smi in register.
269 void SmiTest(Register src);
270
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 // Functions performing a check on a known or potential smi. Returns
272 // a condition that is satisfied if the check is successful.
273
274 // Is the value a tagged smi.
275 Condition CheckSmi(Register src);
276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 // Is the value a positive tagged smi.
278 Condition CheckPositiveSmi(Register src);
279
Leon Clarkee46be812010-01-19 14:06:41 +0000280 // Are both values tagged smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 Condition CheckBothSmi(Register first, Register second);
282
Leon Clarked91b9f72010-01-27 17:25:45 +0000283 // Are both values tagged smis.
284 Condition CheckBothPositiveSmi(Register first, Register second);
285
Leon Clarkee46be812010-01-19 14:06:41 +0000286 // Are either value a tagged smi.
Ben Murdochbb769b22010-08-11 14:56:33 +0100287 Condition CheckEitherSmi(Register first,
288 Register second,
289 Register scratch = kScratchRegister);
Leon Clarkee46be812010-01-19 14:06:41 +0000290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 // Is the value the minimum smi value (since we are using
292 // two's complement numbers, negating the value is known to yield
293 // a non-smi value).
294 Condition CheckIsMinSmi(Register src);
295
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 // Checks whether an 32-bit integer value is a valid for conversion
297 // to a smi.
298 Condition CheckInteger32ValidSmiValue(Register src);
299
Steve Block3ce2e202009-11-05 08:53:23 +0000300 // Checks whether an 32-bit unsigned integer value is a valid for
301 // conversion to a smi.
302 Condition CheckUInteger32ValidSmiValue(Register src);
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 // Test-and-jump functions. Typically combines a check function
305 // above with a conditional jump.
306
307 // Jump if the value cannot be represented by a smi.
308 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
309
Steve Block3ce2e202009-11-05 08:53:23 +0000310 // Jump if the unsigned integer value cannot be represented by a smi.
311 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 // Jump to label if the value is a tagged smi.
314 void JumpIfSmi(Register src, Label* on_smi);
315
316 // Jump to label if the value is not a tagged smi.
317 void JumpIfNotSmi(Register src, Label* on_not_smi);
318
319 // Jump to label if the value is not a positive tagged smi.
320 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
321
Steve Block3ce2e202009-11-05 08:53:23 +0000322 // Jump to label if the value, which must be a tagged smi, has value equal
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 // to the constant.
Steve Block3ce2e202009-11-05 08:53:23 +0000324 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000325
326 // Jump if either or both register are not smi values.
327 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
328
Leon Clarked91b9f72010-01-27 17:25:45 +0000329 // Jump if either or both register are not positive smi values.
330 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
331 Label* on_not_both_smi);
332
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 // Operations on tagged smi values.
334
335 // Smis represent a subset of integers. The subset is always equivalent to
336 // a two's complement interpretation of a fixed number of bits.
337
338 // Optimistically adds an integer constant to a supposed smi.
339 // If the src is not a smi, or the result is not a smi, jump to
340 // the label.
341 void SmiTryAddConstant(Register dst,
342 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000343 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 Label* on_not_smi_result);
345
Steve Block3ce2e202009-11-05 08:53:23 +0000346 // Add an integer constant to a tagged smi, giving a tagged smi as result.
347 // No overflow testing on the result is done.
348 void SmiAddConstant(Register dst, Register src, Smi* constant);
349
Leon Clarkef7060e22010-06-03 12:02:55 +0100350 // Add an integer constant to a tagged smi, giving a tagged smi as result.
351 // No overflow testing on the result is done.
352 void SmiAddConstant(const Operand& dst, Smi* constant);
353
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 // Add an integer constant to a tagged smi, giving a tagged smi as result,
355 // or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 void SmiAddConstant(Register dst,
357 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000358 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 Label* on_not_smi_result);
360
361 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Block6ded16b2010-05-10 14:33:55 +0100362 // result. No testing on the result is done. Sets the N and Z flags
363 // based on the value of the resulting integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000364 void SmiSubConstant(Register dst, Register src, Smi* constant);
365
366 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // result, or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 void SmiSubConstant(Register dst,
369 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000370 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 Label* on_not_smi_result);
372
373 // Negating a smi can give a negative zero or too large positive value.
Steve Block3ce2e202009-11-05 08:53:23 +0000374 // NOTICE: This operation jumps on success, not failure!
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 void SmiNeg(Register dst,
376 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000377 Label* on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378
379 // Adds smi values and return the result as a smi.
380 // If dst is src1, then src1 will be destroyed, even if
381 // the operation is unsuccessful.
382 void SmiAdd(Register dst,
383 Register src1,
384 Register src2,
385 Label* on_not_smi_result);
386
387 // Subtracts smi values and return the result as a smi.
388 // If dst is src1, then src1 will be destroyed, even if
389 // the operation is unsuccessful.
390 void SmiSub(Register dst,
391 Register src1,
392 Register src2,
393 Label* on_not_smi_result);
394
Steve Block6ded16b2010-05-10 14:33:55 +0100395 void SmiSub(Register dst,
396 Register src1,
Leon Clarkef7060e22010-06-03 12:02:55 +0100397 const Operand& src2,
Steve Block6ded16b2010-05-10 14:33:55 +0100398 Label* on_not_smi_result);
399
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 // Multiplies smi values and return the result as a smi,
401 // if possible.
402 // If dst is src1, then src1 will be destroyed, even if
403 // the operation is unsuccessful.
404 void SmiMul(Register dst,
405 Register src1,
406 Register src2,
407 Label* on_not_smi_result);
408
409 // Divides one smi by another and returns the quotient.
410 // Clobbers rax and rdx registers.
411 void SmiDiv(Register dst,
412 Register src1,
413 Register src2,
414 Label* on_not_smi_result);
415
416 // Divides one smi by another and returns the remainder.
417 // Clobbers rax and rdx registers.
418 void SmiMod(Register dst,
419 Register src1,
420 Register src2,
421 Label* on_not_smi_result);
422
423 // Bitwise operations.
424 void SmiNot(Register dst, Register src);
425 void SmiAnd(Register dst, Register src1, Register src2);
426 void SmiOr(Register dst, Register src1, Register src2);
427 void SmiXor(Register dst, Register src1, Register src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000428 void SmiAndConstant(Register dst, Register src1, Smi* constant);
429 void SmiOrConstant(Register dst, Register src1, Smi* constant);
430 void SmiXorConstant(Register dst, Register src1, Smi* constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000431
432 void SmiShiftLeftConstant(Register dst,
433 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +0100434 int shift_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 void SmiShiftLogicalRightConstant(Register dst,
436 Register src,
437 int shift_value,
438 Label* on_not_smi_result);
439 void SmiShiftArithmeticRightConstant(Register dst,
440 Register src,
441 int shift_value);
442
443 // Shifts a smi value to the left, and returns the result if that is a smi.
444 // Uses and clobbers rcx, so dst may not be rcx.
445 void SmiShiftLeft(Register dst,
446 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +0100447 Register src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 // Shifts a smi value to the right, shifting in zero bits at the top, and
449 // returns the unsigned intepretation of the result if that is a smi.
450 // Uses and clobbers rcx, so dst may not be rcx.
451 void SmiShiftLogicalRight(Register dst,
452 Register src1,
453 Register src2,
454 Label* on_not_smi_result);
455 // Shifts a smi value to the right, sign extending the top, and
456 // returns the signed intepretation of the result. That will always
457 // be a valid smi value, since it's numerically smaller than the
458 // original.
459 // Uses and clobbers rcx, so dst may not be rcx.
460 void SmiShiftArithmeticRight(Register dst,
461 Register src1,
462 Register src2);
463
464 // Specialized operations
465
466 // Select the non-smi register of two registers where exactly one is a
467 // smi. If neither are smis, jump to the failure label.
468 void SelectNonSmi(Register dst,
469 Register src1,
470 Register src2,
471 Label* on_not_smis);
472
473 // Converts, if necessary, a smi to a combination of number and
474 // multiplier to be used as a scaled index.
475 // The src register contains a *positive* smi value. The shift is the
476 // power of two to multiply the index value by (e.g.
477 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
478 // The returned index register may be either src or dst, depending
479 // on what is most efficient. If src and dst are different registers,
480 // src is always unchanged.
481 SmiIndex SmiToIndex(Register dst, Register src, int shift);
482
483 // Converts a positive smi to a negative index.
484 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
485
Steve Block3ce2e202009-11-05 08:53:23 +0000486 // Basic Smi operations.
487 void Move(Register dst, Smi* source) {
Steve Block8defd9f2010-07-08 12:39:36 +0100488 LoadSmiConstant(dst, source);
Steve Block3ce2e202009-11-05 08:53:23 +0000489 }
490
491 void Move(const Operand& dst, Smi* source) {
Steve Block8defd9f2010-07-08 12:39:36 +0100492 Register constant = GetSmiConstant(source);
493 movq(dst, constant);
Steve Block3ce2e202009-11-05 08:53:23 +0000494 }
495
496 void Push(Smi* smi);
497 void Test(const Operand& dst, Smi* source);
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 // ---------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000500 // String macros.
501 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
502 Register second_object,
503 Register scratch1,
504 Register scratch2,
505 Label* on_not_both_flat_ascii);
506
Steve Block6ded16b2010-05-10 14:33:55 +0100507 // Check whether the instance type represents a flat ascii string. Jump to the
508 // label if not. If the instance type can be scratched specify same register
509 // for both instance type and scratch.
510 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
511 Register scratch,
512 Label *on_not_flat_ascii_string);
513
514 void JumpIfBothInstanceTypesAreNotSequentialAscii(
515 Register first_object_instance_type,
516 Register second_object_instance_type,
517 Register scratch1,
518 Register scratch2,
519 Label* on_fail);
520
Leon Clarkee46be812010-01-19 14:06:41 +0000521 // ---------------------------------------------------------------------------
522 // Macro instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000523
Steve Block3ce2e202009-11-05 08:53:23 +0000524 // Load a register with a long value as efficiently as possible.
Steve Blocka7e24c12009-10-30 11:49:00 +0000525 void Set(Register dst, int64_t x);
526 void Set(const Operand& dst, int64_t x);
527
528 // Handle support
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 void Move(Register dst, Handle<Object> source);
530 void Move(const Operand& dst, Handle<Object> source);
531 void Cmp(Register dst, Handle<Object> source);
532 void Cmp(const Operand& dst, Handle<Object> source);
533 void Push(Handle<Object> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534
Leon Clarkee46be812010-01-19 14:06:41 +0000535 // Emit code to discard a non-negative number of pointer-sized elements
536 // from the stack, clobbering only the rsp register.
537 void Drop(int stack_elements);
538
539 void Call(Label* target) { call(target); }
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 // Control Flow
542 void Jump(Address destination, RelocInfo::Mode rmode);
543 void Jump(ExternalReference ext);
544 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
545
546 void Call(Address destination, RelocInfo::Mode rmode);
547 void Call(ExternalReference ext);
548 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
549
550 // Compare object type for heap object.
551 // Always use unsigned comparisons: above and below, not less and greater.
552 // Incoming register is heap_object and outgoing register is map.
553 // They may be the same register, and may be kScratchRegister.
554 void CmpObjectType(Register heap_object, InstanceType type, Register map);
555
556 // Compare instance type for map.
557 // Always use unsigned comparisons: above and below, not less and greater.
558 void CmpInstanceType(Register map, InstanceType type);
559
Andrei Popescu31002712010-02-23 13:46:05 +0000560 // Check if the map of an object is equal to a specified map and
561 // branch to label if not. Skip the smi check if not required
562 // (object is known to be a heap object)
563 void CheckMap(Register obj,
564 Handle<Map> map,
565 Label* fail,
566 bool is_heap_object);
567
Leon Clarked91b9f72010-01-27 17:25:45 +0000568 // Check if the object in register heap_object is a string. Afterwards the
569 // register map contains the object map and the register instance_type
570 // contains the instance_type. The registers map and instance_type can be the
571 // same in which case it contains the instance type afterwards. Either of the
572 // registers map and instance_type can be the same as heap_object.
573 Condition IsObjectStringType(Register heap_object,
574 Register map,
575 Register instance_type);
576
Steve Block8defd9f2010-07-08 12:39:36 +0100577 // FCmp compares and pops the two values on top of the FPU stack.
578 // The flag results are similar to integer cmp, but requires unsigned
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
580 void FCmp();
581
Andrei Popescu402d9372010-02-26 13:31:12 +0000582 // Abort execution if argument is not a number. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100583 void AbortIfNotNumber(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000584
Iain Merrick75681382010-08-19 15:07:18 +0100585 // Abort execution if argument is a smi. Used in debug code.
586 void AbortIfSmi(Register object);
587
Steve Block6ded16b2010-05-10 14:33:55 +0100588 // Abort execution if argument is not a smi. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100589 void AbortIfNotSmi(Register object);
Steve Block6ded16b2010-05-10 14:33:55 +0100590
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100591 // Abort execution if argument is not the root value with the given index.
592 void AbortIfNotRootValue(Register src,
593 Heap::RootListIndex root_value_index,
594 const char* message);
595
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 // ---------------------------------------------------------------------------
597 // Exception handling
598
599 // Push a new try handler and link into try handler chain. The return
600 // address must be pushed before calling this helper.
601 void PushTryHandler(CodeLocation try_location, HandlerType type);
602
Leon Clarkee46be812010-01-19 14:06:41 +0000603 // Unlink the stack handler on top of the stack from the try handler chain.
604 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000605
606 // ---------------------------------------------------------------------------
607 // Inline caching support
608
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 // Generate code for checking access rights - used for security checks
610 // on access to global objects across environments. The holder register
611 // is left untouched, but the scratch register and kScratchRegister,
612 // which must be different, are clobbered.
613 void CheckAccessGlobalProxy(Register holder_reg,
614 Register scratch,
615 Label* miss);
616
617
618 // ---------------------------------------------------------------------------
619 // Allocation support
620
621 // Allocate an object in new space. If the new space is exhausted control
622 // continues at the gc_required label. The allocated object is returned in
623 // result and end of the new object is returned in result_end. The register
624 // scratch can be passed as no_reg in which case an additional object
625 // reference will be added to the reloc info. The returned pointers in result
626 // and result_end have not yet been tagged as heap objects. If
627 // result_contains_top_on_entry is true the content of result is known to be
628 // the allocation top on entry (could be result_end from a previous call to
629 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
630 // should be no_reg as it is never used.
631 void AllocateInNewSpace(int object_size,
632 Register result,
633 Register result_end,
634 Register scratch,
635 Label* gc_required,
636 AllocationFlags flags);
637
638 void AllocateInNewSpace(int header_size,
639 ScaleFactor element_size,
640 Register element_count,
641 Register result,
642 Register result_end,
643 Register scratch,
644 Label* gc_required,
645 AllocationFlags flags);
646
647 void AllocateInNewSpace(Register object_size,
648 Register result,
649 Register result_end,
650 Register scratch,
651 Label* gc_required,
652 AllocationFlags flags);
653
654 // Undo allocation in new space. The object passed and objects allocated after
655 // it will no longer be allocated. Make sure that no pointers are left to the
656 // object(s) no longer allocated as they would be invalid when allocation is
657 // un-done.
658 void UndoAllocationInNewSpace(Register object);
659
Steve Block3ce2e202009-11-05 08:53:23 +0000660 // Allocate a heap number in new space with undefined value. Returns
661 // tagged pointer in result register, or jumps to gc_required if new
662 // space is full.
663 void AllocateHeapNumber(Register result,
664 Register scratch,
665 Label* gc_required);
666
Leon Clarkee46be812010-01-19 14:06:41 +0000667 // Allocate a sequential string. All the header fields of the string object
668 // are initialized.
669 void AllocateTwoByteString(Register result,
670 Register length,
671 Register scratch1,
672 Register scratch2,
673 Register scratch3,
674 Label* gc_required);
675 void AllocateAsciiString(Register result,
676 Register length,
677 Register scratch1,
678 Register scratch2,
679 Register scratch3,
680 Label* gc_required);
681
682 // Allocate a raw cons string object. Only the map field of the result is
683 // initialized.
684 void AllocateConsString(Register result,
685 Register scratch1,
686 Register scratch2,
687 Label* gc_required);
688 void AllocateAsciiConsString(Register result,
689 Register scratch1,
690 Register scratch2,
691 Label* gc_required);
692
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 // ---------------------------------------------------------------------------
694 // Support functions.
695
696 // Check if result is zero and op is negative.
697 void NegativeZeroTest(Register result, Register op, Label* then_label);
698
699 // Check if result is zero and op is negative in code using jump targets.
700 void NegativeZeroTest(CodeGenerator* cgen,
701 Register result,
702 Register op,
703 JumpTarget* then_target);
704
705 // Check if result is zero and any of op1 and op2 are negative.
706 // Register scratch is destroyed, and it must be different from op2.
707 void NegativeZeroTest(Register result, Register op1, Register op2,
708 Register scratch, Label* then_label);
709
710 // Try to get function prototype of a function and puts the value in
711 // the result register. Checks that the function really is a
712 // function and jumps to the miss label if the fast checks fail. The
713 // function register will be untouched; the other register may be
714 // clobbered.
715 void TryGetFunctionPrototype(Register function,
716 Register result,
717 Label* miss);
718
719 // Generates code for reporting that an illegal operation has
720 // occurred.
721 void IllegalOperation(int num_arguments);
722
Steve Blockd0582a62009-12-15 09:54:21 +0000723 // Find the function context up the context chain.
724 void LoadContext(Register dst, int context_chain_length);
725
Steve Blocka7e24c12009-10-30 11:49:00 +0000726 // ---------------------------------------------------------------------------
727 // Runtime calls
728
729 // Call a code stub.
730 void CallStub(CodeStub* stub);
731
Ben Murdochbb769b22010-08-11 14:56:33 +0100732 // Call a code stub and return the code object called. Try to generate
733 // the code if necessary. Do not perform a GC but instead return a retry
734 // after GC failure.
735 Object* TryCallStub(CodeStub* stub);
736
Leon Clarkee46be812010-01-19 14:06:41 +0000737 // Tail call a code stub (jump).
738 void TailCallStub(CodeStub* stub);
739
Ben Murdochbb769b22010-08-11 14:56:33 +0100740 // Tail call a code stub (jump) and return the code object called. Try to
741 // generate the code if necessary. Do not perform a GC but instead return
742 // a retry after GC failure.
743 Object* TryTailCallStub(CodeStub* stub);
744
Steve Blocka7e24c12009-10-30 11:49:00 +0000745 // Return from a code stub after popping its arguments.
746 void StubReturn(int argc);
747
748 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 void CallRuntime(Runtime::Function* f, int num_arguments);
750
Ben Murdochbb769b22010-08-11 14:56:33 +0100751 // Call a runtime function, returning the CodeStub object called.
752 // Try to generate the stub code if necessary. Do not perform a GC
753 // but instead return a retry after GC failure.
754 Object* TryCallRuntime(Runtime::Function* f, int num_arguments);
755
Steve Blocka7e24c12009-10-30 11:49:00 +0000756 // Convenience function: Same as above, but takes the fid instead.
757 void CallRuntime(Runtime::FunctionId id, int num_arguments);
758
Ben Murdochbb769b22010-08-11 14:56:33 +0100759 // Convenience function: Same as above, but takes the fid instead.
760 Object* TryCallRuntime(Runtime::FunctionId id, int num_arguments);
761
Andrei Popescu402d9372010-02-26 13:31:12 +0000762 // Convenience function: call an external reference.
763 void CallExternalReference(const ExternalReference& ext,
764 int num_arguments);
765
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100767 // Like JumpToExternalReference, but also takes care of passing the number
768 // of parameters.
769 void TailCallExternalReference(const ExternalReference& ext,
770 int num_arguments,
771 int result_size);
772
773 // Convenience function: tail call a runtime routine (jump).
774 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000775 int num_arguments,
776 int result_size);
777
Ben Murdochbb769b22010-08-11 14:56:33 +0100778 void PushHandleScope(Register scratch);
779
780 // Pops a handle scope using the specified scratch register and
781 // ensuring that saved register is left unchanged.
782 void PopHandleScope(Register saved, Register scratch);
783
784 // As PopHandleScope, but does not perform a GC. Instead, returns a
785 // retry after GC failure object if GC is necessary.
786 Object* TryPopHandleScope(Register saved, Register scratch);
787
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100789 void JumpToExternalReference(const ExternalReference& ext, int result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000790
Leon Clarke4515c472010-02-03 11:58:03 +0000791 // Before calling a C-function from generated code, align arguments on stack.
792 // After aligning the frame, arguments must be stored in esp[0], esp[4],
793 // etc., not pushed. The argument count assumes all arguments are word sized.
794 // The number of slots reserved for arguments depends on platform. On Windows
795 // stack slots are reserved for the arguments passed in registers. On other
796 // platforms stack slots are only reserved for the arguments actually passed
797 // on the stack.
798 void PrepareCallCFunction(int num_arguments);
799
800 // Calls a C function and cleans up the space for arguments allocated
801 // by PrepareCallCFunction. The called function is not allowed to trigger a
802 // garbage collection, since that might move the code and invalidate the
803 // return address (unless this is somehow accounted for by the called
804 // function).
805 void CallCFunction(ExternalReference function, int num_arguments);
806 void CallCFunction(Register function, int num_arguments);
807
808 // Calculate the number of stack slots to reserve for arguments when calling a
809 // C function.
810 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000811
812 // ---------------------------------------------------------------------------
813 // Utilities
814
815 void Ret();
816
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 Handle<Object> CodeObject() { return code_object_; }
818
819
820 // ---------------------------------------------------------------------------
821 // StatsCounter support
822
823 void SetCounter(StatsCounter* counter, int value);
824 void IncrementCounter(StatsCounter* counter, int value);
825 void DecrementCounter(StatsCounter* counter, int value);
826
827
828 // ---------------------------------------------------------------------------
829 // Debugging
830
831 // Calls Abort(msg) if the condition cc is not satisfied.
832 // Use --debug_code to enable.
833 void Assert(Condition cc, const char* msg);
834
Iain Merrick75681382010-08-19 15:07:18 +0100835 void AssertFastElements(Register elements);
836
Steve Blocka7e24c12009-10-30 11:49:00 +0000837 // Like Assert(), but always enabled.
838 void Check(Condition cc, const char* msg);
839
840 // Print a message to stdout and abort execution.
841 void Abort(const char* msg);
842
Steve Block6ded16b2010-05-10 14:33:55 +0100843 // Check that the stack is aligned.
844 void CheckStackAlignment();
845
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 // Verify restrictions about code generated in stubs.
847 void set_generating_stub(bool value) { generating_stub_ = value; }
848 bool generating_stub() { return generating_stub_; }
849 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
850 bool allow_stub_calls() { return allow_stub_calls_; }
851
852 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000853 bool generating_stub_;
854 bool allow_stub_calls_;
Steve Block8defd9f2010-07-08 12:39:36 +0100855
856 // Returns a register holding the smi value. The register MUST NOT be
857 // modified. It may be the "smi 1 constant" register.
858 Register GetSmiConstant(Smi* value);
859
860 // Moves the smi value to the destination register.
861 void LoadSmiConstant(Register dst, Smi* value);
862
Andrei Popescu31002712010-02-23 13:46:05 +0000863 // This handle will be patched with the code object on installation.
864 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000865
866 // Helper functions for generating invokes.
867 void InvokePrologue(const ParameterCount& expected,
868 const ParameterCount& actual,
869 Handle<Code> code_constant,
870 Register code_register,
871 Label* done,
872 InvokeFlag flag);
873
Steve Blocka7e24c12009-10-30 11:49:00 +0000874 // Activation support.
875 void EnterFrame(StackFrame::Type type);
876 void LeaveFrame(StackFrame::Type type);
877
Ben Murdochbb769b22010-08-11 14:56:33 +0100878 void EnterExitFramePrologue(ExitFrame::Mode mode, bool save_rax);
879 void EnterExitFrameEpilogue(ExitFrame::Mode mode, int result_size, int argc);
880
Steve Blocka7e24c12009-10-30 11:49:00 +0000881 // Allocation support helpers.
Steve Block6ded16b2010-05-10 14:33:55 +0100882 // Loads the top of new-space into the result register.
883 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
884 // already contains the top of new-space, and scratch is invalid.
885 // Otherwise the address of the new-space top is loaded into scratch (if
886 // scratch is valid), and the new-space top is loaded into result.
Steve Blocka7e24c12009-10-30 11:49:00 +0000887 void LoadAllocationTopHelper(Register result,
888 Register result_end,
889 Register scratch,
890 AllocationFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +0100891 // Update allocation top with value in result_end register.
892 // If scratch is valid, it contains the address of the allocation top.
Steve Blocka7e24c12009-10-30 11:49:00 +0000893 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Ben Murdochbb769b22010-08-11 14:56:33 +0100894
895 // Helper for PopHandleScope. Allowed to perform a GC and returns
896 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
897 // possibly returns a failure object indicating an allocation failure.
898 Object* PopHandleScopeHelper(Register saved,
899 Register scratch,
900 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000901};
902
903
904// The code patcher is used to patch (typically) small parts of code e.g. for
905// debugging and other types of instrumentation. When using the code patcher
906// the exact number of bytes specified must be emitted. Is not legal to emit
907// relocation information. If any of these constraints are violated it causes
908// an assertion.
909class CodePatcher {
910 public:
911 CodePatcher(byte* address, int size);
912 virtual ~CodePatcher();
913
914 // Macro assembler to emit code.
915 MacroAssembler* masm() { return &masm_; }
916
917 private:
918 byte* address_; // The address of the code being patched.
919 int size_; // Number of bytes of the expected patch size.
920 MacroAssembler masm_; // Macro assembler used to generate the code.
921};
922
923
924// -----------------------------------------------------------------------------
925// Static helper functions.
926
927// Generate an Operand for loading a field from an object.
928static inline Operand FieldOperand(Register object, int offset) {
929 return Operand(object, offset - kHeapObjectTag);
930}
931
932
933// Generate an Operand for loading an indexed field from an object.
934static inline Operand FieldOperand(Register object,
935 Register index,
936 ScaleFactor scale,
937 int offset) {
938 return Operand(object, index, scale, offset - kHeapObjectTag);
939}
940
941
942#ifdef GENERATED_CODE_COVERAGE
943extern void LogGeneratedCodeCoverage(const char* file_line);
944#define CODE_COVERAGE_STRINGIFY(x) #x
945#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
946#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
947#define ACCESS_MASM(masm) { \
948 byte* x64_coverage_function = \
949 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
950 masm->pushfd(); \
951 masm->pushad(); \
952 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
953 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
954 masm->pop(rax); \
955 masm->popad(); \
956 masm->popfd(); \
957 } \
958 masm->
959#else
960#define ACCESS_MASM(masm) masm->
961#endif
962
963
964} } // namespace v8::internal
965
966#endif // V8_X64_MACRO_ASSEMBLER_X64_H_