blob: 32e1f4972f145063713230056f5598601d321d6f [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
36// Default scratch register used by MacroAssembler (and other code that needs
37// a spare register). The register isn't callee save, and not used by the
38// function calling convention.
Andrei Popescu402d9372010-02-26 13:31:12 +000039static const Register kScratchRegister = { 10 }; // r10.
Steve Block6ded16b2010-05-10 14:33:55 +010040static const Register kRootRegister = { 13 }; // r13
Steve Blocka7e24c12009-10-30 11:49:00 +000041
Leon Clarkee46be812010-01-19 14:06:41 +000042// Convenience for platform-independent signatures.
43typedef Operand MemOperand;
44
Steve Blocka7e24c12009-10-30 11:49:00 +000045// Forward declaration.
46class JumpTarget;
47
48struct SmiIndex {
49 SmiIndex(Register index_register, ScaleFactor scale)
50 : reg(index_register),
51 scale(scale) {}
52 Register reg;
53 ScaleFactor scale;
54};
55
56// MacroAssembler implements a collection of frequently used macros.
57class MacroAssembler: public Assembler {
58 public:
59 MacroAssembler(void* buffer, int size);
60
61 void LoadRoot(Register destination, Heap::RootListIndex index);
62 void CompareRoot(Register with, Heap::RootListIndex index);
63 void CompareRoot(Operand with, Heap::RootListIndex index);
64 void PushRoot(Heap::RootListIndex index);
65
66 // ---------------------------------------------------------------------------
67 // GC Support
68
Steve Block6ded16b2010-05-10 14:33:55 +010069 // Set the remebered set bit for an address which points into an
70 // object. RecordWriteHelper only works if the object is not in new
71 // space.
72 void RecordWriteHelper(Register object,
73 Register addr,
74 Register scratch);
75
76 // Check if object is in new space. The condition cc can be equal or
77 // not_equal. If it is equal a jump will be done if the object is on new
78 // space. The register scratch can be object itself, but it will be clobbered.
79 void InNewSpace(Register object,
80 Register scratch,
81 Condition cc,
82 Label* branch);
83
Steve Blocka7e24c12009-10-30 11:49:00 +000084 // Set the remembered set bit for [object+offset].
85 // object is the object being stored into, value is the object being stored.
86 // If offset is zero, then the scratch register contains the array index into
87 // the elements array represented as a Smi.
88 // All registers are clobbered by the operation.
89 void RecordWrite(Register object,
90 int offset,
91 Register value,
92 Register scratch);
93
Steve Block3ce2e202009-11-05 08:53:23 +000094 // Set the remembered set bit for [object+offset].
95 // The value is known to not be a smi.
96 // object is the object being stored into, value is the object being stored.
97 // If offset is zero, then the scratch register contains the array index into
98 // the elements array represented as a Smi.
99 // All registers are clobbered by the operation.
100 void RecordWriteNonSmi(Register object,
101 int offset,
102 Register value,
103 Register scratch);
104
Steve Blocka7e24c12009-10-30 11:49:00 +0000105#ifdef ENABLE_DEBUGGER_SUPPORT
106 // ---------------------------------------------------------------------------
107 // Debugger Support
108
109 void SaveRegistersToMemory(RegList regs);
110 void RestoreRegistersFromMemory(RegList regs);
111 void PushRegistersFromMemory(RegList regs);
112 void PopRegistersToMemory(RegList regs);
113 void CopyRegistersFromStackToMemory(Register base,
114 Register scratch,
115 RegList regs);
Andrei Popescu402d9372010-02-26 13:31:12 +0000116 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000117#endif
118
119 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +0000120 // Stack limit support
121
122 // Do simple test for stack overflow. This doesn't handle an overflow.
123 void StackLimitCheck(Label* on_stack_limit_hit);
124
125 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 // Activation frames
127
128 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
129 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
130
131 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
132 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
133
Steve Blockd0582a62009-12-15 09:54:21 +0000134 // Enter specific kind of exit frame; either in normal or
135 // debug mode. Expects the number of arguments in register rax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 // sets up the number of arguments in register rdi and the pointer
137 // to the first argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000138 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
140 // Leave the current exit frame. Expects/provides the return value in
141 // register rax:rdx (untouched) and the pointer to the first
142 // argument in register rsi.
Steve Blockd0582a62009-12-15 09:54:21 +0000143 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
145
146 // ---------------------------------------------------------------------------
147 // JavaScript invokes
148
149 // Invoke the JavaScript function code by either calling or jumping.
150 void InvokeCode(Register code,
151 const ParameterCount& expected,
152 const ParameterCount& actual,
153 InvokeFlag flag);
154
155 void InvokeCode(Handle<Code> code,
156 const ParameterCount& expected,
157 const ParameterCount& actual,
158 RelocInfo::Mode rmode,
159 InvokeFlag flag);
160
161 // Invoke the JavaScript function in the given register. Changes the
162 // current context to the context in the function before invoking.
163 void InvokeFunction(Register function,
164 const ParameterCount& actual,
165 InvokeFlag flag);
166
Andrei Popescu402d9372010-02-26 13:31:12 +0000167 void InvokeFunction(JSFunction* function,
168 const ParameterCount& actual,
169 InvokeFlag flag);
170
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 // Invoke specified builtin JavaScript function. Adds an entry to
172 // the unresolved list if the name does not resolve.
173 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
174
175 // Store the code object for the given builtin in the target register.
176 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
177
178
179 // ---------------------------------------------------------------------------
180 // Smi tagging, untagging and operations on tagged smis.
181
182 // Conversions between tagged smi values and non-tagged integer values.
183
184 // Tag an integer value. The result must be known to be a valid smi value.
Leon Clarke4515c472010-02-03 11:58:03 +0000185 // Only uses the low 32 bits of the src register. Sets the N and Z flags
186 // based on the value of the resulting integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 void Integer32ToSmi(Register dst, Register src);
188
189 // Tag an integer value if possible, or jump the integer value cannot be
190 // represented as a smi. Only uses the low 32 bit of the src registers.
Steve Block3ce2e202009-11-05 08:53:23 +0000191 // NOTICE: Destroys the dst register even if unsuccessful!
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
193
194 // Adds constant to src and tags the result as a smi.
195 // Result must be a valid smi.
Steve Block3ce2e202009-11-05 08:53:23 +0000196 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
198 // Convert smi to 32-bit integer. I.e., not sign extended into
199 // high 32 bits of destination.
200 void SmiToInteger32(Register dst, Register src);
201
202 // Convert smi to 64-bit integer (sign extended if necessary).
203 void SmiToInteger64(Register dst, Register src);
204
205 // Multiply a positive smi's integer value by a power of two.
206 // Provides result as 64-bit integer value.
207 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
208 Register src,
209 int power);
210
Steve Block3ce2e202009-11-05 08:53:23 +0000211 // Simple comparison of smis.
212 void SmiCompare(Register dst, Register src);
213 void SmiCompare(Register dst, Smi* src);
Steve Block6ded16b2010-05-10 14:33:55 +0100214 void SmiCompare(Register dst, const Operand& src);
Steve Block3ce2e202009-11-05 08:53:23 +0000215 void SmiCompare(const Operand& dst, Register src);
216 void SmiCompare(const Operand& dst, Smi* src);
217 // Sets sign and zero flags depending on value of smi in register.
218 void SmiTest(Register src);
219
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 // Functions performing a check on a known or potential smi. Returns
221 // a condition that is satisfied if the check is successful.
222
223 // Is the value a tagged smi.
224 Condition CheckSmi(Register src);
225
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 // Is the value a positive tagged smi.
227 Condition CheckPositiveSmi(Register src);
228
Leon Clarkee46be812010-01-19 14:06:41 +0000229 // Are both values tagged smis.
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 Condition CheckBothSmi(Register first, Register second);
231
Leon Clarked91b9f72010-01-27 17:25:45 +0000232 // Are both values tagged smis.
233 Condition CheckBothPositiveSmi(Register first, Register second);
234
Leon Clarkee46be812010-01-19 14:06:41 +0000235 // Are either value a tagged smi.
236 Condition CheckEitherSmi(Register first, Register second);
237
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 // Is the value the minimum smi value (since we are using
239 // two's complement numbers, negating the value is known to yield
240 // a non-smi value).
241 Condition CheckIsMinSmi(Register src);
242
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 // Checks whether an 32-bit integer value is a valid for conversion
244 // to a smi.
245 Condition CheckInteger32ValidSmiValue(Register src);
246
Steve Block3ce2e202009-11-05 08:53:23 +0000247 // Checks whether an 32-bit unsigned integer value is a valid for
248 // conversion to a smi.
249 Condition CheckUInteger32ValidSmiValue(Register src);
250
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 // Test-and-jump functions. Typically combines a check function
252 // above with a conditional jump.
253
254 // Jump if the value cannot be represented by a smi.
255 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
256
Steve Block3ce2e202009-11-05 08:53:23 +0000257 // Jump if the unsigned integer value cannot be represented by a smi.
258 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
259
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 // Jump to label if the value is a tagged smi.
261 void JumpIfSmi(Register src, Label* on_smi);
262
263 // Jump to label if the value is not a tagged smi.
264 void JumpIfNotSmi(Register src, Label* on_not_smi);
265
266 // Jump to label if the value is not a positive tagged smi.
267 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
268
Steve Block3ce2e202009-11-05 08:53:23 +0000269 // Jump to label if the value, which must be a tagged smi, has value equal
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 // to the constant.
Steve Block3ce2e202009-11-05 08:53:23 +0000271 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000272
273 // Jump if either or both register are not smi values.
274 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
275
Leon Clarked91b9f72010-01-27 17:25:45 +0000276 // Jump if either or both register are not positive smi values.
277 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
278 Label* on_not_both_smi);
279
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 // Operations on tagged smi values.
281
282 // Smis represent a subset of integers. The subset is always equivalent to
283 // a two's complement interpretation of a fixed number of bits.
284
285 // Optimistically adds an integer constant to a supposed smi.
286 // If the src is not a smi, or the result is not a smi, jump to
287 // the label.
288 void SmiTryAddConstant(Register dst,
289 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000290 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 Label* on_not_smi_result);
292
Steve Block3ce2e202009-11-05 08:53:23 +0000293 // Add an integer constant to a tagged smi, giving a tagged smi as result.
294 // No overflow testing on the result is done.
295 void SmiAddConstant(Register dst, Register src, Smi* constant);
296
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 // Add an integer constant to a tagged smi, giving a tagged smi as result,
298 // or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 void SmiAddConstant(Register dst,
300 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000301 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 Label* on_not_smi_result);
303
304 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Block6ded16b2010-05-10 14:33:55 +0100305 // result. No testing on the result is done. Sets the N and Z flags
306 // based on the value of the resulting integer.
Steve Block3ce2e202009-11-05 08:53:23 +0000307 void SmiSubConstant(Register dst, Register src, Smi* constant);
308
309 // Subtract an integer constant from a tagged smi, giving a tagged smi as
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 // result, or jumping to a label if the result cannot be represented by a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 void SmiSubConstant(Register dst,
312 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000313 Smi* constant,
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 Label* on_not_smi_result);
315
316 // Negating a smi can give a negative zero or too large positive value.
Steve Block3ce2e202009-11-05 08:53:23 +0000317 // NOTICE: This operation jumps on success, not failure!
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 void SmiNeg(Register dst,
319 Register src,
Steve Block3ce2e202009-11-05 08:53:23 +0000320 Label* on_smi_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321
322 // Adds smi values and return the result as a smi.
323 // If dst is src1, then src1 will be destroyed, even if
324 // the operation is unsuccessful.
325 void SmiAdd(Register dst,
326 Register src1,
327 Register src2,
328 Label* on_not_smi_result);
329
330 // Subtracts smi values and return the result as a smi.
331 // If dst is src1, then src1 will be destroyed, even if
332 // the operation is unsuccessful.
333 void SmiSub(Register dst,
334 Register src1,
335 Register src2,
336 Label* on_not_smi_result);
337
Steve Block6ded16b2010-05-10 14:33:55 +0100338 void SmiSub(Register dst,
339 Register src1,
340 Operand const& src2,
341 Label* on_not_smi_result);
342
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 // Multiplies smi values and return the result as a smi,
344 // if possible.
345 // If dst is src1, then src1 will be destroyed, even if
346 // the operation is unsuccessful.
347 void SmiMul(Register dst,
348 Register src1,
349 Register src2,
350 Label* on_not_smi_result);
351
352 // Divides one smi by another and returns the quotient.
353 // Clobbers rax and rdx registers.
354 void SmiDiv(Register dst,
355 Register src1,
356 Register src2,
357 Label* on_not_smi_result);
358
359 // Divides one smi by another and returns the remainder.
360 // Clobbers rax and rdx registers.
361 void SmiMod(Register dst,
362 Register src1,
363 Register src2,
364 Label* on_not_smi_result);
365
366 // Bitwise operations.
367 void SmiNot(Register dst, Register src);
368 void SmiAnd(Register dst, Register src1, Register src2);
369 void SmiOr(Register dst, Register src1, Register src2);
370 void SmiXor(Register dst, Register src1, Register src2);
Steve Block3ce2e202009-11-05 08:53:23 +0000371 void SmiAndConstant(Register dst, Register src1, Smi* constant);
372 void SmiOrConstant(Register dst, Register src1, Smi* constant);
373 void SmiXorConstant(Register dst, Register src1, Smi* constant);
Steve Blocka7e24c12009-10-30 11:49:00 +0000374
375 void SmiShiftLeftConstant(Register dst,
376 Register src,
377 int shift_value,
378 Label* on_not_smi_result);
379 void SmiShiftLogicalRightConstant(Register dst,
380 Register src,
381 int shift_value,
382 Label* on_not_smi_result);
383 void SmiShiftArithmeticRightConstant(Register dst,
384 Register src,
385 int shift_value);
386
387 // Shifts a smi value to the left, and returns the result if that is a smi.
388 // Uses and clobbers rcx, so dst may not be rcx.
389 void SmiShiftLeft(Register dst,
390 Register src1,
391 Register src2,
392 Label* on_not_smi_result);
393 // Shifts a smi value to the right, shifting in zero bits at the top, and
394 // returns the unsigned intepretation of the result if that is a smi.
395 // Uses and clobbers rcx, so dst may not be rcx.
396 void SmiShiftLogicalRight(Register dst,
397 Register src1,
398 Register src2,
399 Label* on_not_smi_result);
400 // Shifts a smi value to the right, sign extending the top, and
401 // returns the signed intepretation of the result. That will always
402 // be a valid smi value, since it's numerically smaller than the
403 // original.
404 // Uses and clobbers rcx, so dst may not be rcx.
405 void SmiShiftArithmeticRight(Register dst,
406 Register src1,
407 Register src2);
408
409 // Specialized operations
410
411 // Select the non-smi register of two registers where exactly one is a
412 // smi. If neither are smis, jump to the failure label.
413 void SelectNonSmi(Register dst,
414 Register src1,
415 Register src2,
416 Label* on_not_smis);
417
418 // Converts, if necessary, a smi to a combination of number and
419 // multiplier to be used as a scaled index.
420 // The src register contains a *positive* smi value. The shift is the
421 // power of two to multiply the index value by (e.g.
422 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
423 // The returned index register may be either src or dst, depending
424 // on what is most efficient. If src and dst are different registers,
425 // src is always unchanged.
426 SmiIndex SmiToIndex(Register dst, Register src, int shift);
427
428 // Converts a positive smi to a negative index.
429 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
430
Steve Block3ce2e202009-11-05 08:53:23 +0000431 // Basic Smi operations.
432 void Move(Register dst, Smi* source) {
433 Set(dst, reinterpret_cast<int64_t>(source));
434 }
435
436 void Move(const Operand& dst, Smi* source) {
437 Set(dst, reinterpret_cast<int64_t>(source));
438 }
439
440 void Push(Smi* smi);
441 void Test(const Operand& dst, Smi* source);
442
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 // ---------------------------------------------------------------------------
Leon Clarkee46be812010-01-19 14:06:41 +0000444 // String macros.
445 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
446 Register second_object,
447 Register scratch1,
448 Register scratch2,
449 Label* on_not_both_flat_ascii);
450
Steve Block6ded16b2010-05-10 14:33:55 +0100451 // Check whether the instance type represents a flat ascii string. Jump to the
452 // label if not. If the instance type can be scratched specify same register
453 // for both instance type and scratch.
454 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
455 Register scratch,
456 Label *on_not_flat_ascii_string);
457
458 void JumpIfBothInstanceTypesAreNotSequentialAscii(
459 Register first_object_instance_type,
460 Register second_object_instance_type,
461 Register scratch1,
462 Register scratch2,
463 Label* on_fail);
464
Leon Clarkee46be812010-01-19 14:06:41 +0000465 // ---------------------------------------------------------------------------
466 // Macro instructions.
Steve Blocka7e24c12009-10-30 11:49:00 +0000467
Steve Block3ce2e202009-11-05 08:53:23 +0000468 // Load a register with a long value as efficiently as possible.
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 void Set(Register dst, int64_t x);
470 void Set(const Operand& dst, int64_t x);
471
472 // Handle support
Steve Blocka7e24c12009-10-30 11:49:00 +0000473 void Move(Register dst, Handle<Object> source);
474 void Move(const Operand& dst, Handle<Object> source);
475 void Cmp(Register dst, Handle<Object> source);
476 void Cmp(const Operand& dst, Handle<Object> source);
477 void Push(Handle<Object> source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478
Leon Clarkee46be812010-01-19 14:06:41 +0000479 // Emit code to discard a non-negative number of pointer-sized elements
480 // from the stack, clobbering only the rsp register.
481 void Drop(int stack_elements);
482
483 void Call(Label* target) { call(target); }
484
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 // Control Flow
486 void Jump(Address destination, RelocInfo::Mode rmode);
487 void Jump(ExternalReference ext);
488 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
489
490 void Call(Address destination, RelocInfo::Mode rmode);
491 void Call(ExternalReference ext);
492 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
493
494 // Compare object type for heap object.
495 // Always use unsigned comparisons: above and below, not less and greater.
496 // Incoming register is heap_object and outgoing register is map.
497 // They may be the same register, and may be kScratchRegister.
498 void CmpObjectType(Register heap_object, InstanceType type, Register map);
499
500 // Compare instance type for map.
501 // Always use unsigned comparisons: above and below, not less and greater.
502 void CmpInstanceType(Register map, InstanceType type);
503
Andrei Popescu31002712010-02-23 13:46:05 +0000504 // Check if the map of an object is equal to a specified map and
505 // branch to label if not. Skip the smi check if not required
506 // (object is known to be a heap object)
507 void CheckMap(Register obj,
508 Handle<Map> map,
509 Label* fail,
510 bool is_heap_object);
511
Leon Clarked91b9f72010-01-27 17:25:45 +0000512 // Check if the object in register heap_object is a string. Afterwards the
513 // register map contains the object map and the register instance_type
514 // contains the instance_type. The registers map and instance_type can be the
515 // same in which case it contains the instance type afterwards. Either of the
516 // registers map and instance_type can be the same as heap_object.
517 Condition IsObjectStringType(Register heap_object,
518 Register map,
519 Register instance_type);
520
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 // FCmp is similar to integer cmp, but requires unsigned
522 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
523 void FCmp();
524
Andrei Popescu402d9372010-02-26 13:31:12 +0000525 // Abort execution if argument is not a number. Used in debug code.
526 void AbortIfNotNumber(Register object, const char* msg);
527
Steve Block6ded16b2010-05-10 14:33:55 +0100528 // Abort execution if argument is not a smi. Used in debug code.
529 void AbortIfNotSmi(Register object, const char* msg);
530
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 // ---------------------------------------------------------------------------
532 // Exception handling
533
534 // Push a new try handler and link into try handler chain. The return
535 // address must be pushed before calling this helper.
536 void PushTryHandler(CodeLocation try_location, HandlerType type);
537
Leon Clarkee46be812010-01-19 14:06:41 +0000538 // Unlink the stack handler on top of the stack from the try handler chain.
539 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000540
541 // ---------------------------------------------------------------------------
542 // Inline caching support
543
544 // Generates code that verifies that the maps of objects in the
545 // prototype chain of object hasn't changed since the code was
546 // generated and branches to the miss label if any map has. If
547 // necessary the function also generates code for security check
548 // in case of global object holders. The scratch and holder
549 // registers are always clobbered, but the object register is only
550 // clobbered if it the same as the holder register. The function
551 // returns a register containing the holder - either object_reg or
552 // holder_reg.
Steve Block6ded16b2010-05-10 14:33:55 +0100553 // The function can optionally (when save_at_depth !=
554 // kInvalidProtoDepth) save the object at the given depth by moving
555 // it to [rsp + kPointerSize].
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 Register CheckMaps(JSObject* object, Register object_reg,
557 JSObject* holder, Register holder_reg,
Steve Block6ded16b2010-05-10 14:33:55 +0100558 Register scratch,
559 int save_at_depth,
560 Label* miss);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561
562 // Generate code for checking access rights - used for security checks
563 // on access to global objects across environments. The holder register
564 // is left untouched, but the scratch register and kScratchRegister,
565 // which must be different, are clobbered.
566 void CheckAccessGlobalProxy(Register holder_reg,
567 Register scratch,
568 Label* miss);
569
570
571 // ---------------------------------------------------------------------------
572 // Allocation support
573
574 // Allocate an object in new space. If the new space is exhausted control
575 // continues at the gc_required label. The allocated object is returned in
576 // result and end of the new object is returned in result_end. The register
577 // scratch can be passed as no_reg in which case an additional object
578 // reference will be added to the reloc info. The returned pointers in result
579 // and result_end have not yet been tagged as heap objects. If
580 // result_contains_top_on_entry is true the content of result is known to be
581 // the allocation top on entry (could be result_end from a previous call to
582 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
583 // should be no_reg as it is never used.
584 void AllocateInNewSpace(int object_size,
585 Register result,
586 Register result_end,
587 Register scratch,
588 Label* gc_required,
589 AllocationFlags flags);
590
591 void AllocateInNewSpace(int header_size,
592 ScaleFactor element_size,
593 Register element_count,
594 Register result,
595 Register result_end,
596 Register scratch,
597 Label* gc_required,
598 AllocationFlags flags);
599
600 void AllocateInNewSpace(Register object_size,
601 Register result,
602 Register result_end,
603 Register scratch,
604 Label* gc_required,
605 AllocationFlags flags);
606
607 // Undo allocation in new space. The object passed and objects allocated after
608 // it will no longer be allocated. Make sure that no pointers are left to the
609 // object(s) no longer allocated as they would be invalid when allocation is
610 // un-done.
611 void UndoAllocationInNewSpace(Register object);
612
Steve Block3ce2e202009-11-05 08:53:23 +0000613 // Allocate a heap number in new space with undefined value. Returns
614 // tagged pointer in result register, or jumps to gc_required if new
615 // space is full.
616 void AllocateHeapNumber(Register result,
617 Register scratch,
618 Label* gc_required);
619
Leon Clarkee46be812010-01-19 14:06:41 +0000620 // Allocate a sequential string. All the header fields of the string object
621 // are initialized.
622 void AllocateTwoByteString(Register result,
623 Register length,
624 Register scratch1,
625 Register scratch2,
626 Register scratch3,
627 Label* gc_required);
628 void AllocateAsciiString(Register result,
629 Register length,
630 Register scratch1,
631 Register scratch2,
632 Register scratch3,
633 Label* gc_required);
634
635 // Allocate a raw cons string object. Only the map field of the result is
636 // initialized.
637 void AllocateConsString(Register result,
638 Register scratch1,
639 Register scratch2,
640 Label* gc_required);
641 void AllocateAsciiConsString(Register result,
642 Register scratch1,
643 Register scratch2,
644 Label* gc_required);
645
Steve Blocka7e24c12009-10-30 11:49:00 +0000646 // ---------------------------------------------------------------------------
647 // Support functions.
648
649 // Check if result is zero and op is negative.
650 void NegativeZeroTest(Register result, Register op, Label* then_label);
651
652 // Check if result is zero and op is negative in code using jump targets.
653 void NegativeZeroTest(CodeGenerator* cgen,
654 Register result,
655 Register op,
656 JumpTarget* then_target);
657
658 // Check if result is zero and any of op1 and op2 are negative.
659 // Register scratch is destroyed, and it must be different from op2.
660 void NegativeZeroTest(Register result, Register op1, Register op2,
661 Register scratch, Label* then_label);
662
663 // Try to get function prototype of a function and puts the value in
664 // the result register. Checks that the function really is a
665 // function and jumps to the miss label if the fast checks fail. The
666 // function register will be untouched; the other register may be
667 // clobbered.
668 void TryGetFunctionPrototype(Register function,
669 Register result,
670 Label* miss);
671
672 // Generates code for reporting that an illegal operation has
673 // occurred.
674 void IllegalOperation(int num_arguments);
675
Steve Blockd0582a62009-12-15 09:54:21 +0000676 // Find the function context up the context chain.
677 void LoadContext(Register dst, int context_chain_length);
678
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 // ---------------------------------------------------------------------------
680 // Runtime calls
681
682 // Call a code stub.
683 void CallStub(CodeStub* stub);
684
Leon Clarkee46be812010-01-19 14:06:41 +0000685 // Tail call a code stub (jump).
686 void TailCallStub(CodeStub* stub);
687
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 // Return from a code stub after popping its arguments.
689 void StubReturn(int argc);
690
691 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000692 void CallRuntime(Runtime::Function* f, int num_arguments);
693
694 // Convenience function: Same as above, but takes the fid instead.
695 void CallRuntime(Runtime::FunctionId id, int num_arguments);
696
Andrei Popescu402d9372010-02-26 13:31:12 +0000697 // Convenience function: call an external reference.
698 void CallExternalReference(const ExternalReference& ext,
699 int num_arguments);
700
Steve Blocka7e24c12009-10-30 11:49:00 +0000701 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100702 // Like JumpToExternalReference, but also takes care of passing the number
703 // of parameters.
704 void TailCallExternalReference(const ExternalReference& ext,
705 int num_arguments,
706 int result_size);
707
708 // Convenience function: tail call a runtime routine (jump).
709 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 int num_arguments,
711 int result_size);
712
713 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100714 void JumpToExternalReference(const ExternalReference& ext, int result_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000715
Leon Clarke4515c472010-02-03 11:58:03 +0000716 // Before calling a C-function from generated code, align arguments on stack.
717 // After aligning the frame, arguments must be stored in esp[0], esp[4],
718 // etc., not pushed. The argument count assumes all arguments are word sized.
719 // The number of slots reserved for arguments depends on platform. On Windows
720 // stack slots are reserved for the arguments passed in registers. On other
721 // platforms stack slots are only reserved for the arguments actually passed
722 // on the stack.
723 void PrepareCallCFunction(int num_arguments);
724
725 // Calls a C function and cleans up the space for arguments allocated
726 // by PrepareCallCFunction. The called function is not allowed to trigger a
727 // garbage collection, since that might move the code and invalidate the
728 // return address (unless this is somehow accounted for by the called
729 // function).
730 void CallCFunction(ExternalReference function, int num_arguments);
731 void CallCFunction(Register function, int num_arguments);
732
733 // Calculate the number of stack slots to reserve for arguments when calling a
734 // C function.
735 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000736
737 // ---------------------------------------------------------------------------
738 // Utilities
739
740 void Ret();
741
Steve Blocka7e24c12009-10-30 11:49:00 +0000742 Handle<Object> CodeObject() { return code_object_; }
743
744
745 // ---------------------------------------------------------------------------
746 // StatsCounter support
747
748 void SetCounter(StatsCounter* counter, int value);
749 void IncrementCounter(StatsCounter* counter, int value);
750 void DecrementCounter(StatsCounter* counter, int value);
751
752
753 // ---------------------------------------------------------------------------
754 // Debugging
755
756 // Calls Abort(msg) if the condition cc is not satisfied.
757 // Use --debug_code to enable.
758 void Assert(Condition cc, const char* msg);
759
760 // Like Assert(), but always enabled.
761 void Check(Condition cc, const char* msg);
762
763 // Print a message to stdout and abort execution.
764 void Abort(const char* msg);
765
Steve Block6ded16b2010-05-10 14:33:55 +0100766 // Check that the stack is aligned.
767 void CheckStackAlignment();
768
Steve Blocka7e24c12009-10-30 11:49:00 +0000769 // Verify restrictions about code generated in stubs.
770 void set_generating_stub(bool value) { generating_stub_ = value; }
771 bool generating_stub() { return generating_stub_; }
772 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
773 bool allow_stub_calls() { return allow_stub_calls_; }
774
775 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 bool generating_stub_;
777 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000778 // This handle will be patched with the code object on installation.
779 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000780
781 // Helper functions for generating invokes.
782 void InvokePrologue(const ParameterCount& expected,
783 const ParameterCount& actual,
784 Handle<Code> code_constant,
785 Register code_register,
786 Label* done,
787 InvokeFlag flag);
788
Steve Blocka7e24c12009-10-30 11:49:00 +0000789 // Activation support.
790 void EnterFrame(StackFrame::Type type);
791 void LeaveFrame(StackFrame::Type type);
792
793 // Allocation support helpers.
Steve Block6ded16b2010-05-10 14:33:55 +0100794 // Loads the top of new-space into the result register.
795 // If flags contains RESULT_CONTAINS_TOP then result_end is valid and
796 // already contains the top of new-space, and scratch is invalid.
797 // Otherwise the address of the new-space top is loaded into scratch (if
798 // scratch is valid), and the new-space top is loaded into result.
Steve Blocka7e24c12009-10-30 11:49:00 +0000799 void LoadAllocationTopHelper(Register result,
800 Register result_end,
801 Register scratch,
802 AllocationFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +0100803 // Update allocation top with value in result_end register.
804 // If scratch is valid, it contains the address of the allocation top.
Steve Blocka7e24c12009-10-30 11:49:00 +0000805 void UpdateAllocationTopHelper(Register result_end, Register scratch);
806};
807
808
809// The code patcher is used to patch (typically) small parts of code e.g. for
810// debugging and other types of instrumentation. When using the code patcher
811// the exact number of bytes specified must be emitted. Is not legal to emit
812// relocation information. If any of these constraints are violated it causes
813// an assertion.
814class CodePatcher {
815 public:
816 CodePatcher(byte* address, int size);
817 virtual ~CodePatcher();
818
819 // Macro assembler to emit code.
820 MacroAssembler* masm() { return &masm_; }
821
822 private:
823 byte* address_; // The address of the code being patched.
824 int size_; // Number of bytes of the expected patch size.
825 MacroAssembler masm_; // Macro assembler used to generate the code.
826};
827
828
829// -----------------------------------------------------------------------------
830// Static helper functions.
831
832// Generate an Operand for loading a field from an object.
833static inline Operand FieldOperand(Register object, int offset) {
834 return Operand(object, offset - kHeapObjectTag);
835}
836
837
838// Generate an Operand for loading an indexed field from an object.
839static inline Operand FieldOperand(Register object,
840 Register index,
841 ScaleFactor scale,
842 int offset) {
843 return Operand(object, index, scale, offset - kHeapObjectTag);
844}
845
846
847#ifdef GENERATED_CODE_COVERAGE
848extern void LogGeneratedCodeCoverage(const char* file_line);
849#define CODE_COVERAGE_STRINGIFY(x) #x
850#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
851#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
852#define ACCESS_MASM(masm) { \
853 byte* x64_coverage_function = \
854 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
855 masm->pushfd(); \
856 masm->pushad(); \
857 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
858 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
859 masm->pop(rax); \
860 masm->popad(); \
861 masm->popfd(); \
862 } \
863 masm->
864#else
865#define ACCESS_MASM(masm) masm->
866#endif
867
868
869} } // namespace v8::internal
870
871#endif // V8_X64_MACRO_ASSEMBLER_X64_H_