blob: bbb6e2183286cbbdbe09294e4041781e3308aee1 [file] [log] [blame]
ager@chromium.orga1645e22009-09-09 19:27:10 +00001// Copyright 2009 the V8 project authors. All rights reserved.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ager@chromium.org9085a012009-05-11 19:22:57 +000028#ifndef V8_X64_MACRO_ASSEMBLER_X64_H_
29#define V8_X64_MACRO_ASSEMBLER_X64_H_
30
31#include "assembler.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000035
ager@chromium.orge2902be2009-06-08 12:21:35 +000036// 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.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +000039static const Register kScratchRegister = { 10 }; // r10.
ager@chromium.orge2902be2009-06-08 12:21:35 +000040
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000041// Convenience for platform-independent signatures.
42typedef Operand MemOperand;
43
ager@chromium.org9085a012009-05-11 19:22:57 +000044// Forward declaration.
45class JumpTarget;
46
ager@chromium.org4af710e2009-09-15 12:20:11 +000047struct SmiIndex {
48 SmiIndex(Register index_register, ScaleFactor scale)
49 : reg(index_register),
50 scale(scale) {}
51 Register reg;
52 ScaleFactor scale;
53};
ager@chromium.org9085a012009-05-11 19:22:57 +000054
ager@chromium.org9085a012009-05-11 19:22:57 +000055// MacroAssembler implements a collection of frequently used macros.
56class MacroAssembler: public Assembler {
57 public:
58 MacroAssembler(void* buffer, int size);
59
ager@chromium.org18ad94b2009-09-02 08:22:29 +000060 void LoadRoot(Register destination, Heap::RootListIndex index);
61 void CompareRoot(Register with, Heap::RootListIndex index);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000062 void CompareRoot(Operand with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000063 void PushRoot(Heap::RootListIndex index);
64
ager@chromium.org9085a012009-05-11 19:22:57 +000065 // ---------------------------------------------------------------------------
66 // GC Support
67
68 // Set the remembered set bit for [object+offset].
69 // object is the object being stored into, value is the object being stored.
70 // If offset is zero, then the scratch register contains the array index into
71 // the elements array represented as a Smi.
72 // All registers are clobbered by the operation.
73 void RecordWrite(Register object,
74 int offset,
75 Register value,
76 Register scratch);
77
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000078 // Set the remembered set bit for [object+offset].
79 // The value is known to not be a smi.
80 // object is the object being stored into, value is the object being stored.
81 // If offset is zero, then the scratch register contains the array index into
82 // the elements array represented as a Smi.
83 // All registers are clobbered by the operation.
84 void RecordWriteNonSmi(Register object,
85 int offset,
86 Register value,
87 Register scratch);
88
89
ager@chromium.org9085a012009-05-11 19:22:57 +000090#ifdef ENABLE_DEBUGGER_SUPPORT
91 // ---------------------------------------------------------------------------
92 // Debugger Support
93
94 void SaveRegistersToMemory(RegList regs);
95 void RestoreRegistersFromMemory(RegList regs);
96 void PushRegistersFromMemory(RegList regs);
97 void PopRegistersToMemory(RegList regs);
98 void CopyRegistersFromStackToMemory(Register base,
99 Register scratch,
100 RegList regs);
ager@chromium.org5c838252010-02-19 08:53:10 +0000101 void DebugBreak();
ager@chromium.org9085a012009-05-11 19:22:57 +0000102#endif
103
104 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000105 // Stack limit support
106
107 // Do simple test for stack overflow. This doesn't handle an overflow.
108 void StackLimitCheck(Label* on_stack_limit_hit);
109
110 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000111 // Activation frames
112
113 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
114 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
115
116 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
117 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
118
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119 // Enter specific kind of exit frame; either in normal or
120 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000121 // sets up the number of arguments in register rdi and the pointer
122 // to the first argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000123 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000124
ager@chromium.orga1645e22009-09-09 19:27:10 +0000125 // Leave the current exit frame. Expects/provides the return value in
126 // register rax:rdx (untouched) and the pointer to the first
127 // argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000128 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000129
130
131 // ---------------------------------------------------------------------------
132 // JavaScript invokes
133
134 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000135 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000136 const ParameterCount& expected,
137 const ParameterCount& actual,
138 InvokeFlag flag);
139
140 void InvokeCode(Handle<Code> code,
141 const ParameterCount& expected,
142 const ParameterCount& actual,
143 RelocInfo::Mode rmode,
144 InvokeFlag flag);
145
146 // Invoke the JavaScript function in the given register. Changes the
147 // current context to the context in the function before invoking.
148 void InvokeFunction(Register function,
149 const ParameterCount& actual,
150 InvokeFlag flag);
151
ager@chromium.org5c838252010-02-19 08:53:10 +0000152 void InvokeFunction(JSFunction* function,
153 const ParameterCount& actual,
154 InvokeFlag flag);
155
ager@chromium.org9085a012009-05-11 19:22:57 +0000156 // Invoke specified builtin JavaScript function. Adds an entry to
157 // the unresolved list if the name does not resolve.
158 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
159
160 // Store the code object for the given builtin in the target register.
161 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
162
ager@chromium.org4af710e2009-09-15 12:20:11 +0000163
164 // ---------------------------------------------------------------------------
165 // Smi tagging, untagging and operations on tagged smis.
166
167 // Conversions between tagged smi values and non-tagged integer values.
168
169 // Tag an integer value. The result must be known to be a valid smi value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000170 // Only uses the low 32 bits of the src register. Sets the N and Z flags
171 // based on the value of the resulting integer.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000172 void Integer32ToSmi(Register dst, Register src);
173
174 // Tag an integer value if possible, or jump the integer value cannot be
175 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000176 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000177 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
178
179 // Adds constant to src and tags the result as a smi.
180 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000181 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000182
183 // Convert smi to 32-bit integer. I.e., not sign extended into
184 // high 32 bits of destination.
185 void SmiToInteger32(Register dst, Register src);
186
187 // Convert smi to 64-bit integer (sign extended if necessary).
188 void SmiToInteger64(Register dst, Register src);
189
190 // Multiply a positive smi's integer value by a power of two.
191 // Provides result as 64-bit integer value.
192 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
193 Register src,
194 int power);
195
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000196 // Simple comparison of smis.
197 void SmiCompare(Register dst, Register src);
198 void SmiCompare(Register dst, Smi* src);
199 void SmiCompare(const Operand& dst, Register src);
200 void SmiCompare(const Operand& dst, Smi* src);
201 // Sets sign and zero flags depending on value of smi in register.
202 void SmiTest(Register src);
203
ager@chromium.org4af710e2009-09-15 12:20:11 +0000204 // Functions performing a check on a known or potential smi. Returns
205 // a condition that is satisfied if the check is successful.
206
207 // Is the value a tagged smi.
208 Condition CheckSmi(Register src);
209
ager@chromium.org4af710e2009-09-15 12:20:11 +0000210 // Is the value a positive tagged smi.
211 Condition CheckPositiveSmi(Register src);
212
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000213 // Are both values tagged smis.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000214 Condition CheckBothSmi(Register first, Register second);
215
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000216 // Are both values tagged smis.
217 Condition CheckBothPositiveSmi(Register first, Register second);
218
219 // Are either value a tagged smi.
220 Condition CheckEitherSmi(Register first, Register second);
221
ager@chromium.org4af710e2009-09-15 12:20:11 +0000222 // Is the value the minimum smi value (since we are using
223 // two's complement numbers, negating the value is known to yield
224 // a non-smi value).
225 Condition CheckIsMinSmi(Register src);
226
ager@chromium.org4af710e2009-09-15 12:20:11 +0000227 // Checks whether an 32-bit integer value is a valid for conversion
228 // to a smi.
229 Condition CheckInteger32ValidSmiValue(Register src);
230
ager@chromium.org3811b432009-10-28 14:53:37 +0000231 // Checks whether an 32-bit unsigned integer value is a valid for
232 // conversion to a smi.
233 Condition CheckUInteger32ValidSmiValue(Register src);
234
ager@chromium.org4af710e2009-09-15 12:20:11 +0000235 // Test-and-jump functions. Typically combines a check function
236 // above with a conditional jump.
237
238 // Jump if the value cannot be represented by a smi.
239 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
240
ager@chromium.org3811b432009-10-28 14:53:37 +0000241 // Jump if the unsigned integer value cannot be represented by a smi.
242 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
243
ager@chromium.org4af710e2009-09-15 12:20:11 +0000244 // Jump to label if the value is a tagged smi.
245 void JumpIfSmi(Register src, Label* on_smi);
246
247 // Jump to label if the value is not a tagged smi.
248 void JumpIfNotSmi(Register src, Label* on_not_smi);
249
250 // Jump to label if the value is not a positive tagged smi.
251 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
252
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000253 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000254 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000255 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000256
ager@chromium.org4af710e2009-09-15 12:20:11 +0000257 // Jump if either or both register are not smi values.
258 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
259
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000260 // Jump if either or both register are not positive smi values.
261 void JumpIfNotBothPositiveSmi(Register src1, Register src2,
262 Label* on_not_both_smi);
263
ager@chromium.org4af710e2009-09-15 12:20:11 +0000264 // Operations on tagged smi values.
265
266 // Smis represent a subset of integers. The subset is always equivalent to
267 // a two's complement interpretation of a fixed number of bits.
268
269 // Optimistically adds an integer constant to a supposed smi.
270 // If the src is not a smi, or the result is not a smi, jump to
271 // the label.
272 void SmiTryAddConstant(Register dst,
273 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000274 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000275 Label* on_not_smi_result);
276
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000277 // Add an integer constant to a tagged smi, giving a tagged smi as result.
278 // No overflow testing on the result is done.
279 void SmiAddConstant(Register dst, Register src, Smi* constant);
280
ager@chromium.org4af710e2009-09-15 12:20:11 +0000281 // Add an integer constant to a tagged smi, giving a tagged smi as result,
282 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000283 void SmiAddConstant(Register dst,
284 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000285 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000286 Label* on_not_smi_result);
287
288 // Subtract an integer constant from a tagged smi, giving a tagged smi as
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000289 // result. No testing on the result is done.
290 void SmiSubConstant(Register dst, Register src, Smi* constant);
291
292 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000293 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000294 void SmiSubConstant(Register dst,
295 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000296 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000297 Label* on_not_smi_result);
298
299 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000300 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000301 void SmiNeg(Register dst,
302 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000303 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000304
305 // Adds smi values and return the result as a smi.
306 // If dst is src1, then src1 will be destroyed, even if
307 // the operation is unsuccessful.
308 void SmiAdd(Register dst,
309 Register src1,
310 Register src2,
311 Label* on_not_smi_result);
312
313 // Subtracts smi values and return the result as a smi.
314 // If dst is src1, then src1 will be destroyed, even if
315 // the operation is unsuccessful.
316 void SmiSub(Register dst,
317 Register src1,
318 Register src2,
319 Label* on_not_smi_result);
320
321 // Multiplies smi values and return the result as a smi,
322 // if possible.
323 // If dst is src1, then src1 will be destroyed, even if
324 // the operation is unsuccessful.
325 void SmiMul(Register dst,
326 Register src1,
327 Register src2,
328 Label* on_not_smi_result);
329
330 // Divides one smi by another and returns the quotient.
331 // Clobbers rax and rdx registers.
332 void SmiDiv(Register dst,
333 Register src1,
334 Register src2,
335 Label* on_not_smi_result);
336
337 // Divides one smi by another and returns the remainder.
338 // Clobbers rax and rdx registers.
339 void SmiMod(Register dst,
340 Register src1,
341 Register src2,
342 Label* on_not_smi_result);
343
344 // Bitwise operations.
345 void SmiNot(Register dst, Register src);
346 void SmiAnd(Register dst, Register src1, Register src2);
347 void SmiOr(Register dst, Register src1, Register src2);
348 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000349 void SmiAndConstant(Register dst, Register src1, Smi* constant);
350 void SmiOrConstant(Register dst, Register src1, Smi* constant);
351 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000352
353 void SmiShiftLeftConstant(Register dst,
354 Register src,
355 int shift_value,
356 Label* on_not_smi_result);
357 void SmiShiftLogicalRightConstant(Register dst,
358 Register src,
359 int shift_value,
360 Label* on_not_smi_result);
361 void SmiShiftArithmeticRightConstant(Register dst,
362 Register src,
363 int shift_value);
364
365 // Shifts a smi value to the left, and returns the result if that is a smi.
366 // Uses and clobbers rcx, so dst may not be rcx.
367 void SmiShiftLeft(Register dst,
368 Register src1,
369 Register src2,
370 Label* on_not_smi_result);
371 // Shifts a smi value to the right, shifting in zero bits at the top, and
372 // returns the unsigned intepretation of the result if that is a smi.
373 // Uses and clobbers rcx, so dst may not be rcx.
374 void SmiShiftLogicalRight(Register dst,
375 Register src1,
376 Register src2,
377 Label* on_not_smi_result);
378 // Shifts a smi value to the right, sign extending the top, and
379 // returns the signed intepretation of the result. That will always
380 // be a valid smi value, since it's numerically smaller than the
381 // original.
382 // Uses and clobbers rcx, so dst may not be rcx.
383 void SmiShiftArithmeticRight(Register dst,
384 Register src1,
385 Register src2);
386
387 // Specialized operations
388
389 // Select the non-smi register of two registers where exactly one is a
390 // smi. If neither are smis, jump to the failure label.
391 void SelectNonSmi(Register dst,
392 Register src1,
393 Register src2,
394 Label* on_not_smis);
395
396 // Converts, if necessary, a smi to a combination of number and
397 // multiplier to be used as a scaled index.
398 // The src register contains a *positive* smi value. The shift is the
399 // power of two to multiply the index value by (e.g.
400 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
401 // The returned index register may be either src or dst, depending
402 // on what is most efficient. If src and dst are different registers,
403 // src is always unchanged.
404 SmiIndex SmiToIndex(Register dst, Register src, int shift);
405
406 // Converts a positive smi to a negative index.
407 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
408
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000409 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000410 void Move(Register dst, Smi* source) {
411 Set(dst, reinterpret_cast<int64_t>(source));
412 }
413
414 void Move(const Operand& dst, Smi* source) {
415 Set(dst, reinterpret_cast<int64_t>(source));
416 }
417
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000418 void Push(Smi* smi);
419 void Test(const Operand& dst, Smi* source);
420
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000421 // ---------------------------------------------------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000422 // String macros.
423 void JumpIfNotBothSequentialAsciiStrings(Register first_object,
424 Register second_object,
425 Register scratch1,
426 Register scratch2,
427 Label* on_not_both_flat_ascii);
428
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000429 // Check whether the instance type represents a flat ascii string. Jump to the
430 // label if not. If the instance type can be scratched specify same register
431 // for both instance type and scratch.
432 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
433 Register scratch,
434 Label *on_not_flat_ascii_string);
435
436 void JumpIfBothInstanceTypesAreNotSequentialAscii(
437 Register first_object_instance_type,
438 Register second_object_instance_type,
439 Register scratch1,
440 Register scratch2,
441 Label* on_fail);
442
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000443 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000444 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000445
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000446 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000447 void Set(Register dst, int64_t x);
448 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000449
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000450 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000451 void Move(Register dst, Handle<Object> source);
452 void Move(const Operand& dst, Handle<Object> source);
453 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000454 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000455 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000456
457 // Emit code to discard a non-negative number of pointer-sized elements
458 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000459 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000460
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000461 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000462
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000463 // Control Flow
464 void Jump(Address destination, RelocInfo::Mode rmode);
465 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000466 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
467
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000468 void Call(Address destination, RelocInfo::Mode rmode);
469 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000470 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000471
ager@chromium.org9085a012009-05-11 19:22:57 +0000472 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000473 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000474 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000475 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000476 void CmpObjectType(Register heap_object, InstanceType type, Register map);
477
478 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000479 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000480 void CmpInstanceType(Register map, InstanceType type);
481
ager@chromium.org5c838252010-02-19 08:53:10 +0000482 // Check if the map of an object is equal to a specified map and
483 // branch to label if not. Skip the smi check if not required
484 // (object is known to be a heap object)
485 void CheckMap(Register obj,
486 Handle<Map> map,
487 Label* fail,
488 bool is_heap_object);
489
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000490 // Check if the object in register heap_object is a string. Afterwards the
491 // register map contains the object map and the register instance_type
492 // contains the instance_type. The registers map and instance_type can be the
493 // same in which case it contains the instance type afterwards. Either of the
494 // registers map and instance_type can be the same as heap_object.
495 Condition IsObjectStringType(Register heap_object,
496 Register map,
497 Register instance_type);
498
ager@chromium.org9085a012009-05-11 19:22:57 +0000499 // FCmp is similar to integer cmp, but requires unsigned
500 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
501 void FCmp();
502
ager@chromium.org5c838252010-02-19 08:53:10 +0000503 // Abort execution if argument is not a number. Used in debug code.
504 void AbortIfNotNumber(Register object, const char* msg);
505
ager@chromium.org9085a012009-05-11 19:22:57 +0000506 // ---------------------------------------------------------------------------
507 // Exception handling
508
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000509 // Push a new try handler and link into try handler chain. The return
510 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000511 void PushTryHandler(CodeLocation try_location, HandlerType type);
512
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000513 // Unlink the stack handler on top of the stack from the try handler chain.
514 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000515
516 // ---------------------------------------------------------------------------
517 // Inline caching support
518
519 // Generates code that verifies that the maps of objects in the
520 // prototype chain of object hasn't changed since the code was
521 // generated and branches to the miss label if any map has. If
522 // necessary the function also generates code for security check
523 // in case of global object holders. The scratch and holder
524 // registers are always clobbered, but the object register is only
525 // clobbered if it the same as the holder register. The function
526 // returns a register containing the holder - either object_reg or
527 // holder_reg.
528 Register CheckMaps(JSObject* object, Register object_reg,
529 JSObject* holder, Register holder_reg,
530 Register scratch, Label* miss);
531
532 // Generate code for checking access rights - used for security checks
533 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000534 // is left untouched, but the scratch register and kScratchRegister,
535 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000536 void CheckAccessGlobalProxy(Register holder_reg,
537 Register scratch,
538 Label* miss);
539
540
541 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000542 // Allocation support
543
544 // Allocate an object in new space. If the new space is exhausted control
545 // continues at the gc_required label. The allocated object is returned in
546 // result and end of the new object is returned in result_end. The register
547 // scratch can be passed as no_reg in which case an additional object
548 // reference will be added to the reloc info. The returned pointers in result
549 // and result_end have not yet been tagged as heap objects. If
550 // result_contains_top_on_entry is true the content of result is known to be
551 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000552 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000553 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000554 void AllocateInNewSpace(int object_size,
555 Register result,
556 Register result_end,
557 Register scratch,
558 Label* gc_required,
559 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000560
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000561 void AllocateInNewSpace(int header_size,
562 ScaleFactor element_size,
563 Register element_count,
564 Register result,
565 Register result_end,
566 Register scratch,
567 Label* gc_required,
568 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000569
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000570 void AllocateInNewSpace(Register object_size,
571 Register result,
572 Register result_end,
573 Register scratch,
574 Label* gc_required,
575 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000576
577 // Undo allocation in new space. The object passed and objects allocated after
578 // it will no longer be allocated. Make sure that no pointers are left to the
579 // object(s) no longer allocated as they would be invalid when allocation is
580 // un-done.
581 void UndoAllocationInNewSpace(Register object);
582
ager@chromium.org3811b432009-10-28 14:53:37 +0000583 // Allocate a heap number in new space with undefined value. Returns
584 // tagged pointer in result register, or jumps to gc_required if new
585 // space is full.
586 void AllocateHeapNumber(Register result,
587 Register scratch,
588 Label* gc_required);
589
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000590 // Allocate a sequential string. All the header fields of the string object
591 // are initialized.
592 void AllocateTwoByteString(Register result,
593 Register length,
594 Register scratch1,
595 Register scratch2,
596 Register scratch3,
597 Label* gc_required);
598 void AllocateAsciiString(Register result,
599 Register length,
600 Register scratch1,
601 Register scratch2,
602 Register scratch3,
603 Label* gc_required);
604
605 // Allocate a raw cons string object. Only the map field of the result is
606 // initialized.
607 void AllocateConsString(Register result,
608 Register scratch1,
609 Register scratch2,
610 Label* gc_required);
611 void AllocateAsciiConsString(Register result,
612 Register scratch1,
613 Register scratch2,
614 Label* gc_required);
615
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000616 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000617 // Support functions.
618
619 // Check if result is zero and op is negative.
620 void NegativeZeroTest(Register result, Register op, Label* then_label);
621
622 // Check if result is zero and op is negative in code using jump targets.
623 void NegativeZeroTest(CodeGenerator* cgen,
624 Register result,
625 Register op,
626 JumpTarget* then_target);
627
628 // Check if result is zero and any of op1 and op2 are negative.
629 // Register scratch is destroyed, and it must be different from op2.
630 void NegativeZeroTest(Register result, Register op1, Register op2,
631 Register scratch, Label* then_label);
632
633 // Try to get function prototype of a function and puts the value in
634 // the result register. Checks that the function really is a
635 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000636 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000637 // clobbered.
638 void TryGetFunctionPrototype(Register function,
639 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000640 Label* miss);
641
642 // Generates code for reporting that an illegal operation has
643 // occurred.
644 void IllegalOperation(int num_arguments);
645
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000646 // Find the function context up the context chain.
647 void LoadContext(Register dst, int context_chain_length);
648
ager@chromium.org9085a012009-05-11 19:22:57 +0000649 // ---------------------------------------------------------------------------
650 // Runtime calls
651
652 // Call a code stub.
653 void CallStub(CodeStub* stub);
654
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000655 // Tail call a code stub (jump).
656 void TailCallStub(CodeStub* stub);
657
ager@chromium.org9085a012009-05-11 19:22:57 +0000658 // Return from a code stub after popping its arguments.
659 void StubReturn(int argc);
660
661 // Call a runtime routine.
ager@chromium.org9085a012009-05-11 19:22:57 +0000662 void CallRuntime(Runtime::Function* f, int num_arguments);
663
664 // Convenience function: Same as above, but takes the fid instead.
665 void CallRuntime(Runtime::FunctionId id, int num_arguments);
666
ager@chromium.org5c838252010-02-19 08:53:10 +0000667 // Convenience function: call an external reference.
668 void CallExternalReference(const ExternalReference& ext,
669 int num_arguments);
670
ager@chromium.org9085a012009-05-11 19:22:57 +0000671 // Tail call of a runtime routine (jump).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000672 // Like JumpToExternalReference, but also takes care of passing the number
673 // of parameters.
674 void TailCallExternalReference(const ExternalReference& ext,
675 int num_arguments,
676 int result_size);
677
678 // Convenience function: tail call a runtime routine (jump).
679 void TailCallRuntime(Runtime::FunctionId fid,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000680 int num_arguments,
681 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000682
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000683 // Jump to a runtime routine.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000684 void JumpToExternalReference(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000685
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000686 // Before calling a C-function from generated code, align arguments on stack.
687 // After aligning the frame, arguments must be stored in esp[0], esp[4],
688 // etc., not pushed. The argument count assumes all arguments are word sized.
689 // The number of slots reserved for arguments depends on platform. On Windows
690 // stack slots are reserved for the arguments passed in registers. On other
691 // platforms stack slots are only reserved for the arguments actually passed
692 // on the stack.
693 void PrepareCallCFunction(int num_arguments);
694
695 // Calls a C function and cleans up the space for arguments allocated
696 // by PrepareCallCFunction. The called function is not allowed to trigger a
697 // garbage collection, since that might move the code and invalidate the
698 // return address (unless this is somehow accounted for by the called
699 // function).
700 void CallCFunction(ExternalReference function, int num_arguments);
701 void CallCFunction(Register function, int num_arguments);
702
703 // Calculate the number of stack slots to reserve for arguments when calling a
704 // C function.
705 int ArgumentStackSlotsForCFunctionCall(int num_arguments);
ager@chromium.org9085a012009-05-11 19:22:57 +0000706
707 // ---------------------------------------------------------------------------
708 // Utilities
709
710 void Ret();
711
ager@chromium.org9085a012009-05-11 19:22:57 +0000712 Handle<Object> CodeObject() { return code_object_; }
713
714
715 // ---------------------------------------------------------------------------
716 // StatsCounter support
717
718 void SetCounter(StatsCounter* counter, int value);
719 void IncrementCounter(StatsCounter* counter, int value);
720 void DecrementCounter(StatsCounter* counter, int value);
721
722
723 // ---------------------------------------------------------------------------
724 // Debugging
725
726 // Calls Abort(msg) if the condition cc is not satisfied.
727 // Use --debug_code to enable.
728 void Assert(Condition cc, const char* msg);
729
730 // Like Assert(), but always enabled.
731 void Check(Condition cc, const char* msg);
732
733 // Print a message to stdout and abort execution.
734 void Abort(const char* msg);
735
736 // Verify restrictions about code generated in stubs.
737 void set_generating_stub(bool value) { generating_stub_ = value; }
738 bool generating_stub() { return generating_stub_; }
739 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
740 bool allow_stub_calls() { return allow_stub_calls_; }
741
742 private:
ager@chromium.org9085a012009-05-11 19:22:57 +0000743 bool generating_stub_;
744 bool allow_stub_calls_;
ager@chromium.org5c838252010-02-19 08:53:10 +0000745 // This handle will be patched with the code object on installation.
746 Handle<Object> code_object_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000747
748 // Helper functions for generating invokes.
749 void InvokePrologue(const ParameterCount& expected,
750 const ParameterCount& actual,
751 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000752 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000753 Label* done,
754 InvokeFlag flag);
755
ager@chromium.org9085a012009-05-11 19:22:57 +0000756 // Activation support.
757 void EnterFrame(StackFrame::Type type);
758 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000759
760 // Allocation support helpers.
761 void LoadAllocationTopHelper(Register result,
762 Register result_end,
763 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000764 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000765 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000766};
767
768
ager@chromium.org4af710e2009-09-15 12:20:11 +0000769// The code patcher is used to patch (typically) small parts of code e.g. for
770// debugging and other types of instrumentation. When using the code patcher
771// the exact number of bytes specified must be emitted. Is not legal to emit
772// relocation information. If any of these constraints are violated it causes
773// an assertion.
774class CodePatcher {
775 public:
776 CodePatcher(byte* address, int size);
777 virtual ~CodePatcher();
778
779 // Macro assembler to emit code.
780 MacroAssembler* masm() { return &masm_; }
781
782 private:
783 byte* address_; // The address of the code being patched.
784 int size_; // Number of bytes of the expected patch size.
785 MacroAssembler masm_; // Macro assembler used to generate the code.
786};
787
788
ager@chromium.org9085a012009-05-11 19:22:57 +0000789// -----------------------------------------------------------------------------
790// Static helper functions.
791
792// Generate an Operand for loading a field from an object.
793static inline Operand FieldOperand(Register object, int offset) {
794 return Operand(object, offset - kHeapObjectTag);
795}
796
797
798// Generate an Operand for loading an indexed field from an object.
799static inline Operand FieldOperand(Register object,
800 Register index,
801 ScaleFactor scale,
802 int offset) {
803 return Operand(object, index, scale, offset - kHeapObjectTag);
804}
805
806
807#ifdef GENERATED_CODE_COVERAGE
808extern void LogGeneratedCodeCoverage(const char* file_line);
809#define CODE_COVERAGE_STRINGIFY(x) #x
810#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
811#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
812#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000813 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000814 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
815 masm->pushfd(); \
816 masm->pushad(); \
817 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000818 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000819 masm->pop(rax); \
820 masm->popad(); \
821 masm->popfd(); \
822 } \
823 masm->
824#else
825#define ACCESS_MASM(masm) masm->
826#endif
827
828
829} } // namespace v8::internal
830
831#endif // V8_X64_MACRO_ASSEMBLER_X64_H_