blob: bb0b68105b67c715e3366027b9ffb4aa70272edd [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.
Andrei Popescu402d9372010-02-26 13:31:12 +000050static const Register kScratchRegister = { 10 }; // r10.
Steve Block6ded16b2010-05-10 14:33:55 +010051static const Register kRootRegister = { 13 }; // r13
Steve Blocka7e24c12009-10-30 11:49:00 +000052
Leon Clarkee46be812010-01-19 14:06:41 +000053// Convenience for platform-independent signatures.
54typedef Operand MemOperand;
55
Steve Blocka7e24c12009-10-30 11:49:00 +000056// Forward declaration.
57class JumpTarget;
58
59struct SmiIndex {
60 SmiIndex(Register index_register, ScaleFactor scale)
61 : reg(index_register),
62 scale(scale) {}
63 Register reg;
64 ScaleFactor scale;
65};
66
67// MacroAssembler implements a collection of frequently used macros.
68class MacroAssembler: public Assembler {
69 public:
70 MacroAssembler(void* buffer, int size);
71
72 void LoadRoot(Register destination, Heap::RootListIndex index);
73 void CompareRoot(Register with, Heap::RootListIndex index);
74 void CompareRoot(Operand with, Heap::RootListIndex index);
75 void PushRoot(Heap::RootListIndex index);
Kristian Monsen25f61362010-05-21 11:50:48 +010076 void StoreRoot(Register source, Heap::RootListIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78 // ---------------------------------------------------------------------------
79 // GC Support
80
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010081 // For page containing |object| mark region covering |addr| dirty.
82 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010083 // space.
84 void RecordWriteHelper(Register object,
85 Register addr,
86 Register scratch);
87
88 // Check if object is in new space. The condition cc can be equal or
89 // not_equal. If it is equal a jump will be done if the object is on new
90 // space. The register scratch can be object itself, but it will be clobbered.
91 void InNewSpace(Register object,
92 Register scratch,
93 Condition cc,
94 Label* branch);
95
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010096 // For page containing |object| mark region covering [object+offset] dirty.
Steve Blocka7e24c12009-10-30 11:49:00 +000097 // object is the object being stored into, value is the object being stored.
98 // If offset is zero, then the scratch register contains the array index into
99 // the elements array represented as a Smi.
100 // All registers are clobbered by the operation.
101 void RecordWrite(Register object,
102 int offset,
103 Register value,
104 Register scratch);
105
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100106 // For page containing |object| mark region covering [object+offset] dirty.
Steve Block3ce2e202009-11-05 08:53:23 +0000107 // The value is known to not be a smi.
108 // object is the object being stored into, value is the object being stored.
109 // If offset is zero, then the scratch register contains the array index into
110 // the elements array represented as a Smi.
111 // All registers are clobbered by the operation.
112 void RecordWriteNonSmi(Register object,
113 int offset,
114 Register value,
115 Register scratch);
116
Steve Blocka7e24c12009-10-30 11:49:00 +0000117#ifdef ENABLE_DEBUGGER_SUPPORT
118 // ---------------------------------------------------------------------------
119 // Debugger Support
120
121 void SaveRegistersToMemory(RegList regs);
122 void RestoreRegistersFromMemory(RegList regs);
123 void PushRegistersFromMemory(RegList regs);
124 void PopRegistersToMemory(RegList regs);
125 void CopyRegistersFromStackToMemory(Register base,
126 Register scratch,
127 RegList regs);
Andrei Popescu402d9372010-02-26 13:31:12 +0000128 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000129#endif
130
131 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +0000132 // Stack limit support
133
134 // Do simple test for stack overflow. This doesn't handle an overflow.
135 void StackLimitCheck(Label* on_stack_limit_hit);
136
137 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 // Activation frames
139
140 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
141 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
142
143 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
144 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
145
Steve Blockd0582a62009-12-15 09:54:21 +0000146 // Enter specific kind of exit frame; either in normal or
147 // debug mode. Expects the number of arguments in register rax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 // sets up the number of arguments in register rdi and the pointer
149 // to the first argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000150 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000151
152 // Leave the current exit frame. Expects/provides the return value in
153 // register rax:rdx (untouched) and the pointer to the first
154 // argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000155 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
157
158 // ---------------------------------------------------------------------------
159 // JavaScript invokes
160
161 // Invoke the JavaScript function code by either calling or jumping.
162 void InvokeCode(Register code,
163 const ParameterCount& expected,
164 const ParameterCount& actual,
165 InvokeFlag flag);
166
167 void InvokeCode(Handle<Code> code,
168 const ParameterCount& expected,
169 const ParameterCount& actual,
170 RelocInfo::Mode rmode,
171 InvokeFlag flag);
172
173 // Invoke the JavaScript function in the given register. Changes the
174 // current context to the context in the function before invoking.
175 void InvokeFunction(Register function,
176 const ParameterCount& actual,
177 InvokeFlag flag);
178
Andrei Popescu402d9372010-02-26 13:31:12 +0000179 void InvokeFunction(JSFunction* function,
180 const ParameterCount& actual,
181 InvokeFlag flag);
182
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 // Invoke specified builtin JavaScript function. Adds an entry to
184 // the unresolved list if the name does not resolve.
185 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
186
187 // Store the code object for the given builtin in the target register.
188 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
189
190
191 // ---------------------------------------------------------------------------
192 // Smi tagging, untagging and operations on tagged smis.
193
194 // Conversions between tagged smi values and non-tagged integer values.
195
196 // Tag an integer value. The result must be known to be a valid smi value.
Leon Clarke4515c472010-02-03 11:58:03 +0000197 // Only uses the low 32 bits of the src register. Sets the N and Z flags
198 // based on the value of the resulting integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 void Integer32ToSmi(Register dst, Register src);
200
201 // Tag an integer value if possible, or jump the integer value cannot be
202 // represented as a smi. Only uses the low 32 bit of the src registers.
Steve Block3ce2e202009-11-05 08:53:23 +0000203 // NOTICE: Destroys the dst register even if unsuccessful!
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
205
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100206 // Stores an integer32 value into a memory field that already holds a smi.
207 void Integer32ToSmiField(const Operand& dst, Register src);
208
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 // Adds constant to src and tags the result as a smi.
210 // Result must be a valid smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000211 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000212
213 // Convert smi to 32-bit integer. I.e., not sign extended into
214 // high 32 bits of destination.
215 void SmiToInteger32(Register dst, Register src);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100216 void SmiToInteger32(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000217
218 // Convert smi to 64-bit integer (sign extended if necessary).
219 void SmiToInteger64(Register dst, Register src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100220 void SmiToInteger64(Register dst, const Operand& src);
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
222 // Multiply a positive smi's integer value by a power of two.
223 // Provides result as 64-bit integer value.
224 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
225 Register src,
226 int power);
227
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100228 // Divide a positive smi's integer value by a power of two.
229 // Provides result as 32-bit integer value.
230 void PositiveSmiDivPowerOfTwoToInteger32(Register dst,
231 Register src,
232 int power);
233
234
Steve Block3ce2e202009-11-05 08:53:23 +0000235 // Simple comparison of smis.
236 void SmiCompare(Register dst, Register src);
237 void SmiCompare(Register dst, Smi* src);
Steve Block6ded16b2010-05-10 14:33:55 +0100238 void SmiCompare(Register dst, const Operand& src);
Steve Block3ce2e202009-11-05 08:53:23 +0000239 void SmiCompare(const Operand& dst, Register src);
240 void SmiCompare(const Operand& dst, Smi* src);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100241 // Compare the int32 in src register to the value of the smi stored at dst.
242 void SmiCompareInteger32(const Operand& dst, Register src);
Steve Block3ce2e202009-11-05 08:53:23 +0000243 // Sets sign and zero flags depending on value of smi in register.
244 void SmiTest(Register src);
245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 // Functions performing a check on a known or potential smi. Returns
247 // a condition that is satisfied if the check is successful.
248
249 // Is the value a tagged smi.
250 Condition CheckSmi(Register src);
251
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 // Is the value a positive tagged smi.
253 Condition CheckPositiveSmi(Register src);
254
Leon Clarkee46be812010-01-19 14:06:41 +0000255 // Are both values tagged smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 Condition CheckBothSmi(Register first, Register second);
257
Leon Clarked91b9f72010-01-27 17:25:45 +0000258 // Are both values tagged smis.
259 Condition CheckBothPositiveSmi(Register first, Register second);
260
Leon Clarkee46be812010-01-19 14:06:41 +0000261 // Are either value a tagged smi.
262 Condition CheckEitherSmi(Register first, Register second);
263
Steve Blocka7e24c12009-10-30 11:49:00 +0000264 // Is the value the minimum smi value (since we are using
265 // two's complement numbers, negating the value is known to yield
266 // a non-smi value).
267 Condition CheckIsMinSmi(Register src);
268
Steve Blocka7e24c12009-10-30 11:49:00 +0000269 // Checks whether an 32-bit integer value is a valid for conversion
270 // to a smi.
271 Condition CheckInteger32ValidSmiValue(Register src);
272
Steve Block3ce2e202009-11-05 08:53:23 +0000273 // Checks whether an 32-bit unsigned integer value is a valid for
274 // conversion to a smi.
275 Condition CheckUInteger32ValidSmiValue(Register src);
276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 // Test-and-jump functions. Typically combines a check function
278 // above with a conditional jump.
279
280 // Jump if the value cannot be represented by a smi.
281 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
282
Steve Block3ce2e202009-11-05 08:53:23 +0000283 // Jump if the unsigned integer value cannot be represented by a smi.
284 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
285
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 // Jump to label if the value is a tagged smi.
287 void JumpIfSmi(Register src, Label* on_smi);
288
289 // Jump to label if the value is not a tagged smi.
290 void JumpIfNotSmi(Register src, Label* on_not_smi);
291
292 // Jump to label if the value is not a positive tagged smi.
293 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
294
Steve Block3ce2e202009-11-05 08:53:23 +0000295 // Jump to label if the value, which must be a tagged smi, has value equal
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 // to the constant.
Steve Block3ce2e202009-11-05 08:53:23 +0000297 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000298
299 // Jump if either or both register are not smi values.
300 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
301
Leon Clarked91b9f72010-01-27 17:25:45 +0000302 // Jump if either or both register are not positive smi values.
303 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
304 Label* on_not_both_smi);
305
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 // Operations on tagged smi values.
307
308 // Smis represent a subset of integers. The subset is always equivalent to
309 // a two's complement interpretation of a fixed number of bits.
310
311 // Optimistically adds an integer constant to a supposed smi.
312 // If the src is not a smi, or the result is not a smi, jump to
313 // the label.
314 void SmiTryAddConstant(Register dst,
315 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000316 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 Label* on_not_smi_result);
318
Steve Block3ce2e202009-11-05 08:53:23 +0000319 // Add an integer constant to a tagged smi, giving a tagged smi as result.
320 // No overflow testing on the result is done.
321 void SmiAddConstant(Register dst, Register src, Smi* constant);
322
Leon Clarkef7060e22010-06-03 12:02:55 +0100323 // Add an integer constant to a tagged smi, giving a tagged smi as result.
324 // No overflow testing on the result is done.
325 void SmiAddConstant(const Operand& dst, Smi* constant);
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 // Add an integer constant to a tagged smi, giving a tagged smi as result,
328 // or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 void SmiAddConstant(Register dst,
330 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000331 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 Label* on_not_smi_result);
333
334 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Block6ded16b2010-05-10 14:33:55 +0100335 // result. No testing on the result is done. Sets the N and Z flags
336 // based on the value of the resulting integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000337 void SmiSubConstant(Register dst, Register src, Smi* constant);
338
339 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 // result, or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 void SmiSubConstant(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
346 // Negating a smi can give a negative zero or too large positive value.
Steve Block3ce2e202009-11-05 08:53:23 +0000347 // NOTICE: This operation jumps on success, not failure!
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 void SmiNeg(Register dst,
349 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000350 Label* on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000351
352 // Adds smi values and return the result as a smi.
353 // If dst is src1, then src1 will be destroyed, even if
354 // the operation is unsuccessful.
355 void SmiAdd(Register dst,
356 Register src1,
357 Register src2,
358 Label* on_not_smi_result);
359
360 // Subtracts smi values and return the result as a smi.
361 // If dst is src1, then src1 will be destroyed, even if
362 // the operation is unsuccessful.
363 void SmiSub(Register dst,
364 Register src1,
365 Register src2,
366 Label* on_not_smi_result);
367
Steve Block6ded16b2010-05-10 14:33:55 +0100368 void SmiSub(Register dst,
369 Register src1,
Leon Clarkef7060e22010-06-03 12:02:55 +0100370 const Operand& src2,
Steve Block6ded16b2010-05-10 14:33:55 +0100371 Label* on_not_smi_result);
372
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 // Multiplies smi values and return the result as a smi,
374 // if possible.
375 // If dst is src1, then src1 will be destroyed, even if
376 // the operation is unsuccessful.
377 void SmiMul(Register dst,
378 Register src1,
379 Register src2,
380 Label* on_not_smi_result);
381
382 // Divides one smi by another and returns the quotient.
383 // Clobbers rax and rdx registers.
384 void SmiDiv(Register dst,
385 Register src1,
386 Register src2,
387 Label* on_not_smi_result);
388
389 // Divides one smi by another and returns the remainder.
390 // Clobbers rax and rdx registers.
391 void SmiMod(Register dst,
392 Register src1,
393 Register src2,
394 Label* on_not_smi_result);
395
396 // Bitwise operations.
397 void SmiNot(Register dst, Register src);
398 void SmiAnd(Register dst, Register src1, Register src2);
399 void SmiOr(Register dst, Register src1, Register src2);
400 void SmiXor(Register dst, Register src1, Register src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000401 void SmiAndConstant(Register dst, Register src1, Smi* constant);
402 void SmiOrConstant(Register dst, Register src1, Smi* constant);
403 void SmiXorConstant(Register dst, Register src1, Smi* constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
405 void SmiShiftLeftConstant(Register dst,
406 Register src,
Kristian Monsen25f61362010-05-21 11:50:48 +0100407 int shift_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 void SmiShiftLogicalRightConstant(Register dst,
409 Register src,
410 int shift_value,
411 Label* on_not_smi_result);
412 void SmiShiftArithmeticRightConstant(Register dst,
413 Register src,
414 int shift_value);
415
416 // Shifts a smi value to the left, and returns the result if that is a smi.
417 // Uses and clobbers rcx, so dst may not be rcx.
418 void SmiShiftLeft(Register dst,
419 Register src1,
Kristian Monsen25f61362010-05-21 11:50:48 +0100420 Register src2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 // Shifts a smi value to the right, shifting in zero bits at the top, and
422 // returns the unsigned intepretation of the result if that is a smi.
423 // Uses and clobbers rcx, so dst may not be rcx.
424 void SmiShiftLogicalRight(Register dst,
425 Register src1,
426 Register src2,
427 Label* on_not_smi_result);
428 // Shifts a smi value to the right, sign extending the top, and
429 // returns the signed intepretation of the result. That will always
430 // be a valid smi value, since it's numerically smaller than the
431 // original.
432 // Uses and clobbers rcx, so dst may not be rcx.
433 void SmiShiftArithmeticRight(Register dst,
434 Register src1,
435 Register src2);
436
437 // Specialized operations
438
439 // Select the non-smi register of two registers where exactly one is a
440 // smi. If neither are smis, jump to the failure label.
441 void SelectNonSmi(Register dst,
442 Register src1,
443 Register src2,
444 Label* on_not_smis);
445
446 // Converts, if necessary, a smi to a combination of number and
447 // multiplier to be used as a scaled index.
448 // The src register contains a *positive* smi value. The shift is the
449 // power of two to multiply the index value by (e.g.
450 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
451 // The returned index register may be either src or dst, depending
452 // on what is most efficient. If src and dst are different registers,
453 // src is always unchanged.
454 SmiIndex SmiToIndex(Register dst, Register src, int shift);
455
456 // Converts a positive smi to a negative index.
457 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
458
Steve Block3ce2e202009-11-05 08:53:23 +0000459 // Basic Smi operations.
460 void Move(Register dst, Smi* source) {
461 Set(dst, reinterpret_cast<int64_t>(source));
462 }
463
464 void Move(const Operand& dst, Smi* source) {
465 Set(dst, reinterpret_cast<int64_t>(source));
466 }
467
468 void Push(Smi* smi);
469 void Test(const Operand& dst, Smi* source);
470
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 // ---------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000472 // String macros.
473 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
474 Register second_object,
475 Register scratch1,
476 Register scratch2,
477 Label* on_not_both_flat_ascii);
478
Steve Block6ded16b2010-05-10 14:33:55 +0100479 // Check whether the instance type represents a flat ascii string. Jump to the
480 // label if not. If the instance type can be scratched specify same register
481 // for both instance type and scratch.
482 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
483 Register scratch,
484 Label *on_not_flat_ascii_string);
485
486 void JumpIfBothInstanceTypesAreNotSequentialAscii(
487 Register first_object_instance_type,
488 Register second_object_instance_type,
489 Register scratch1,
490 Register scratch2,
491 Label* on_fail);
492
Leon Clarkee46be812010-01-19 14:06:41 +0000493 // ---------------------------------------------------------------------------
494 // Macro instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000495
Steve Block3ce2e202009-11-05 08:53:23 +0000496 // Load a register with a long value as efficiently as possible.
Steve Blocka7e24c12009-10-30 11:49:00 +0000497 void Set(Register dst, int64_t x);
498 void Set(const Operand& dst, int64_t x);
499
500 // Handle support
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 void Move(Register dst, Handle<Object> source);
502 void Move(const Operand& dst, Handle<Object> source);
503 void Cmp(Register dst, Handle<Object> source);
504 void Cmp(const Operand& dst, Handle<Object> source);
505 void Push(Handle<Object> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000506
Leon Clarkee46be812010-01-19 14:06:41 +0000507 // Emit code to discard a non-negative number of pointer-sized elements
508 // from the stack, clobbering only the rsp register.
509 void Drop(int stack_elements);
510
511 void Call(Label* target) { call(target); }
512
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 // Control Flow
514 void Jump(Address destination, RelocInfo::Mode rmode);
515 void Jump(ExternalReference ext);
516 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
517
518 void Call(Address destination, RelocInfo::Mode rmode);
519 void Call(ExternalReference ext);
520 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
521
522 // Compare object type for heap object.
523 // Always use unsigned comparisons: above and below, not less and greater.
524 // Incoming register is heap_object and outgoing register is map.
525 // They may be the same register, and may be kScratchRegister.
526 void CmpObjectType(Register heap_object, InstanceType type, Register map);
527
528 // Compare instance type for map.
529 // Always use unsigned comparisons: above and below, not less and greater.
530 void CmpInstanceType(Register map, InstanceType type);
531
Andrei Popescu31002712010-02-23 13:46:05 +0000532 // Check if the map of an object is equal to a specified map and
533 // branch to label if not. Skip the smi check if not required
534 // (object is known to be a heap object)
535 void CheckMap(Register obj,
536 Handle<Map> map,
537 Label* fail,
538 bool is_heap_object);
539
Leon Clarked91b9f72010-01-27 17:25:45 +0000540 // Check if the object in register heap_object is a string. Afterwards the
541 // register map contains the object map and the register instance_type
542 // contains the instance_type. The registers map and instance_type can be the
543 // same in which case it contains the instance type afterwards. Either of the
544 // registers map and instance_type can be the same as heap_object.
545 Condition IsObjectStringType(Register heap_object,
546 Register map,
547 Register instance_type);
548
Steve Blocka7e24c12009-10-30 11:49:00 +0000549 // FCmp is similar to integer cmp, but requires unsigned
550 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
551 void FCmp();
552
Andrei Popescu402d9372010-02-26 13:31:12 +0000553 // Abort execution if argument is not a number. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100554 void AbortIfNotNumber(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000555
Steve Block6ded16b2010-05-10 14:33:55 +0100556 // Abort execution if argument is not a smi. Used in debug code.
Leon Clarkef7060e22010-06-03 12:02:55 +0100557 void AbortIfNotSmi(Register object);
Steve Block6ded16b2010-05-10 14:33:55 +0100558
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100559 // Abort execution if argument is not the root value with the given index.
560 void AbortIfNotRootValue(Register src,
561 Heap::RootListIndex root_value_index,
562 const char* message);
563
Steve Blocka7e24c12009-10-30 11:49:00 +0000564 // ---------------------------------------------------------------------------
565 // Exception handling
566
567 // Push a new try handler and link into try handler chain. The return
568 // address must be pushed before calling this helper.
569 void PushTryHandler(CodeLocation try_location, HandlerType type);
570
Leon Clarkee46be812010-01-19 14:06:41 +0000571 // Unlink the stack handler on top of the stack from the try handler chain.
572 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000573
574 // ---------------------------------------------------------------------------
575 // Inline caching support
576
577 // Generates code that verifies that the maps of objects in the
578 // prototype chain of object hasn't changed since the code was
579 // generated and branches to the miss label if any map has. If
580 // necessary the function also generates code for security check
581 // in case of global object holders. The scratch and holder
582 // registers are always clobbered, but the object register is only
583 // clobbered if it the same as the holder register. The function
584 // returns a register containing the holder - either object_reg or
585 // holder_reg.
Steve Block6ded16b2010-05-10 14:33:55 +0100586 // The function can optionally (when save_at_depth !=
587 // kInvalidProtoDepth) save the object at the given depth by moving
588 // it to [rsp + kPointerSize].
Steve Blocka7e24c12009-10-30 11:49:00 +0000589 Register CheckMaps(JSObject* object, Register object_reg,
590 JSObject* holder, Register holder_reg,
Steve Block6ded16b2010-05-10 14:33:55 +0100591 Register scratch,
592 int save_at_depth,
593 Label* miss);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
595 // Generate code for checking access rights - used for security checks
596 // on access to global objects across environments. The holder register
597 // is left untouched, but the scratch register and kScratchRegister,
598 // which must be different, are clobbered.
599 void CheckAccessGlobalProxy(Register holder_reg,
600 Register scratch,
601 Label* miss);
602
603
604 // ---------------------------------------------------------------------------
605 // Allocation support
606
607 // Allocate an object in new space. If the new space is exhausted control
608 // continues at the gc_required label. The allocated object is returned in
609 // result and end of the new object is returned in result_end. The register
610 // scratch can be passed as no_reg in which case an additional object
611 // reference will be added to the reloc info. The returned pointers in result
612 // and result_end have not yet been tagged as heap objects. If
613 // result_contains_top_on_entry is true the content of result is known to be
614 // the allocation top on entry (could be result_end from a previous call to
615 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
616 // should be no_reg as it is never used.
617 void AllocateInNewSpace(int object_size,
618 Register result,
619 Register result_end,
620 Register scratch,
621 Label* gc_required,
622 AllocationFlags flags);
623
624 void AllocateInNewSpace(int header_size,
625 ScaleFactor element_size,
626 Register element_count,
627 Register result,
628 Register result_end,
629 Register scratch,
630 Label* gc_required,
631 AllocationFlags flags);
632
633 void AllocateInNewSpace(Register object_size,
634 Register result,
635 Register result_end,
636 Register scratch,
637 Label* gc_required,
638 AllocationFlags flags);
639
640 // Undo allocation in new space. The object passed and objects allocated after
641 // it will no longer be allocated. Make sure that no pointers are left to the
642 // object(s) no longer allocated as they would be invalid when allocation is
643 // un-done.
644 void UndoAllocationInNewSpace(Register object);
645
Steve Block3ce2e202009-11-05 08:53:23 +0000646 // Allocate a heap number in new space with undefined value. Returns
647 // tagged pointer in result register, or jumps to gc_required if new
648 // space is full.
649 void AllocateHeapNumber(Register result,
650 Register scratch,
651 Label* gc_required);
652
Leon Clarkee46be812010-01-19 14:06:41 +0000653 // Allocate a sequential string. All the header fields of the string object
654 // are initialized.
655 void AllocateTwoByteString(Register result,
656 Register length,
657 Register scratch1,
658 Register scratch2,
659 Register scratch3,
660 Label* gc_required);
661 void AllocateAsciiString(Register result,
662 Register length,
663 Register scratch1,
664 Register scratch2,
665 Register scratch3,
666 Label* gc_required);
667
668 // Allocate a raw cons string object. Only the map field of the result is
669 // initialized.
670 void AllocateConsString(Register result,
671 Register scratch1,
672 Register scratch2,
673 Label* gc_required);
674 void AllocateAsciiConsString(Register result,
675 Register scratch1,
676 Register scratch2,
677 Label* gc_required);
678
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 // ---------------------------------------------------------------------------
680 // Support functions.
681
682 // Check if result is zero and op is negative.
683 void NegativeZeroTest(Register result, Register op, Label* then_label);
684
685 // Check if result is zero and op is negative in code using jump targets.
686 void NegativeZeroTest(CodeGenerator* cgen,
687 Register result,
688 Register op,
689 JumpTarget* then_target);
690
691 // Check if result is zero and any of op1 and op2 are negative.
692 // Register scratch is destroyed, and it must be different from op2.
693 void NegativeZeroTest(Register result, Register op1, Register op2,
694 Register scratch, Label* then_label);
695
696 // Try to get function prototype of a function and puts the value in
697 // the result register. Checks that the function really is a
698 // function and jumps to the miss label if the fast checks fail. The
699 // function register will be untouched; the other register may be
700 // clobbered.
701 void TryGetFunctionPrototype(Register function,
702 Register result,
703 Label* miss);
704
705 // Generates code for reporting that an illegal operation has
706 // occurred.
707 void IllegalOperation(int num_arguments);
708
Steve Blockd0582a62009-12-15 09:54:21 +0000709 // Find the function context up the context chain.
710 void LoadContext(Register dst, int context_chain_length);
711
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 // ---------------------------------------------------------------------------
713 // Runtime calls
714
715 // Call a code stub.
716 void CallStub(CodeStub* stub);
717
Leon Clarkee46be812010-01-19 14:06:41 +0000718 // Tail call a code stub (jump).
719 void TailCallStub(CodeStub* stub);
720
Steve Blocka7e24c12009-10-30 11:49:00 +0000721 // Return from a code stub after popping its arguments.
722 void StubReturn(int argc);
723
724 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000725 void CallRuntime(Runtime::Function* f, int num_arguments);
726
727 // Convenience function: Same as above, but takes the fid instead.
728 void CallRuntime(Runtime::FunctionId id, int num_arguments);
729
Andrei Popescu402d9372010-02-26 13:31:12 +0000730 // Convenience function: call an external reference.
731 void CallExternalReference(const ExternalReference& ext,
732 int num_arguments);
733
Steve Blocka7e24c12009-10-30 11:49:00 +0000734 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100735 // Like JumpToExternalReference, but also takes care of passing the number
736 // of parameters.
737 void TailCallExternalReference(const ExternalReference& ext,
738 int num_arguments,
739 int result_size);
740
741 // Convenience function: tail call a runtime routine (jump).
742 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 int num_arguments,
744 int result_size);
745
746 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100747 void JumpToExternalReference(const ExternalReference& ext, int result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000748
Leon Clarke4515c472010-02-03 11:58:03 +0000749 // Before calling a C-function from generated code, align arguments on stack.
750 // After aligning the frame, arguments must be stored in esp[0], esp[4],
751 // etc., not pushed. The argument count assumes all arguments are word sized.
752 // The number of slots reserved for arguments depends on platform. On Windows
753 // stack slots are reserved for the arguments passed in registers. On other
754 // platforms stack slots are only reserved for the arguments actually passed
755 // on the stack.
756 void PrepareCallCFunction(int num_arguments);
757
758 // Calls a C function and cleans up the space for arguments allocated
759 // by PrepareCallCFunction. The called function is not allowed to trigger a
760 // garbage collection, since that might move the code and invalidate the
761 // return address (unless this is somehow accounted for by the called
762 // function).
763 void CallCFunction(ExternalReference function, int num_arguments);
764 void CallCFunction(Register function, int num_arguments);
765
766 // Calculate the number of stack slots to reserve for arguments when calling a
767 // C function.
768 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000769
770 // ---------------------------------------------------------------------------
771 // Utilities
772
773 void Ret();
774
Steve Blocka7e24c12009-10-30 11:49:00 +0000775 Handle<Object> CodeObject() { return code_object_; }
776
777
778 // ---------------------------------------------------------------------------
779 // StatsCounter support
780
781 void SetCounter(StatsCounter* counter, int value);
782 void IncrementCounter(StatsCounter* counter, int value);
783 void DecrementCounter(StatsCounter* counter, int value);
784
785
786 // ---------------------------------------------------------------------------
787 // Debugging
788
789 // Calls Abort(msg) if the condition cc is not satisfied.
790 // Use --debug_code to enable.
791 void Assert(Condition cc, const char* msg);
792
793 // Like Assert(), but always enabled.
794 void Check(Condition cc, const char* msg);
795
796 // Print a message to stdout and abort execution.
797 void Abort(const char* msg);
798
Steve Block6ded16b2010-05-10 14:33:55 +0100799 // Check that the stack is aligned.
800 void CheckStackAlignment();
801
Steve Blocka7e24c12009-10-30 11:49:00 +0000802 // Verify restrictions about code generated in stubs.
803 void set_generating_stub(bool value) { generating_stub_ = value; }
804 bool generating_stub() { return generating_stub_; }
805 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
806 bool allow_stub_calls() { return allow_stub_calls_; }
807
808 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000809 bool generating_stub_;
810 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000811 // This handle will be patched with the code object on installation.
812 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000813
814 // Helper functions for generating invokes.
815 void InvokePrologue(const ParameterCount& expected,
816 const ParameterCount& actual,
817 Handle<Code> code_constant,
818 Register code_register,
819 Label* done,
820 InvokeFlag flag);
821
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 // Activation support.
823 void EnterFrame(StackFrame::Type type);
824 void LeaveFrame(StackFrame::Type type);
825
826 // Allocation support helpers.
Steve Block6ded16b2010-05-10 14:33:55 +0100827 // Loads the top of new-space into the result register.
828 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
829 // already contains the top of new-space, and scratch is invalid.
830 // Otherwise the address of the new-space top is loaded into scratch (if
831 // scratch is valid), and the new-space top is loaded into result.
Steve Blocka7e24c12009-10-30 11:49:00 +0000832 void LoadAllocationTopHelper(Register result,
833 Register result_end,
834 Register scratch,
835 AllocationFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +0100836 // Update allocation top with value in result_end register.
837 // If scratch is valid, it contains the address of the allocation top.
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 void UpdateAllocationTopHelper(Register result_end, Register scratch);
839};
840
841
842// The code patcher is used to patch (typically) small parts of code e.g. for
843// debugging and other types of instrumentation. When using the code patcher
844// the exact number of bytes specified must be emitted. Is not legal to emit
845// relocation information. If any of these constraints are violated it causes
846// an assertion.
847class CodePatcher {
848 public:
849 CodePatcher(byte* address, int size);
850 virtual ~CodePatcher();
851
852 // Macro assembler to emit code.
853 MacroAssembler* masm() { return &masm_; }
854
855 private:
856 byte* address_; // The address of the code being patched.
857 int size_; // Number of bytes of the expected patch size.
858 MacroAssembler masm_; // Macro assembler used to generate the code.
859};
860
861
862// -----------------------------------------------------------------------------
863// Static helper functions.
864
865// Generate an Operand for loading a field from an object.
866static inline Operand FieldOperand(Register object, int offset) {
867 return Operand(object, offset - kHeapObjectTag);
868}
869
870
871// Generate an Operand for loading an indexed field from an object.
872static inline Operand FieldOperand(Register object,
873 Register index,
874 ScaleFactor scale,
875 int offset) {
876 return Operand(object, index, scale, offset - kHeapObjectTag);
877}
878
879
880#ifdef GENERATED_CODE_COVERAGE
881extern void LogGeneratedCodeCoverage(const char* file_line);
882#define CODE_COVERAGE_STRINGIFY(x) #x
883#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
884#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
885#define ACCESS_MASM(masm) { \
886 byte* x64_coverage_function = \
887 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
888 masm->pushfd(); \
889 masm->pushad(); \
890 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
891 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
892 masm->pop(rax); \
893 masm->popad(); \
894 masm->popfd(); \
895 } \
896 masm->
897#else
898#define ACCESS_MASM(masm) masm->
899#endif
900
901
902} } // namespace v8::internal
903
904#endif // V8_X64_MACRO_ASSEMBLER_X64_H_