blob: 37f96a66ff67318185fc9cfe018fe8c2077e5ed3 [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.
39static const Register kScratchRegister = r10;
40
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);
101#endif
102
103 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000104 // Stack limit support
105
106 // Do simple test for stack overflow. This doesn't handle an overflow.
107 void StackLimitCheck(Label* on_stack_limit_hit);
108
109 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000110 // Activation frames
111
112 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
113 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
114
115 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
116 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
117
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000118 // Enter specific kind of exit frame; either in normal or
119 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000120 // sets up the number of arguments in register rdi and the pointer
121 // to the first argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000122 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000123
ager@chromium.orga1645e22009-09-09 19:27:10 +0000124 // Leave the current exit frame. Expects/provides the return value in
125 // register rax:rdx (untouched) and the pointer to the first
126 // argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000127 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000128
129
130 // ---------------------------------------------------------------------------
131 // JavaScript invokes
132
133 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000134 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000135 const ParameterCount& expected,
136 const ParameterCount& actual,
137 InvokeFlag flag);
138
139 void InvokeCode(Handle<Code> code,
140 const ParameterCount& expected,
141 const ParameterCount& actual,
142 RelocInfo::Mode rmode,
143 InvokeFlag flag);
144
145 // Invoke the JavaScript function in the given register. Changes the
146 // current context to the context in the function before invoking.
147 void InvokeFunction(Register function,
148 const ParameterCount& actual,
149 InvokeFlag flag);
150
151 // Invoke specified builtin JavaScript function. Adds an entry to
152 // the unresolved list if the name does not resolve.
153 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
154
155 // Store the code object for the given builtin in the target register.
156 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
157
ager@chromium.org4af710e2009-09-15 12:20:11 +0000158
159 // ---------------------------------------------------------------------------
160 // Smi tagging, untagging and operations on tagged smis.
161
162 // Conversions between tagged smi values and non-tagged integer values.
163
164 // Tag an integer value. The result must be known to be a valid smi value.
165 // Only uses the low 32 bits of the src register.
166 void Integer32ToSmi(Register dst, Register src);
167
168 // Tag an integer value if possible, or jump the integer value cannot be
169 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000170 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000171 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
172
173 // Adds constant to src and tags the result as a smi.
174 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000175 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000176
177 // Convert smi to 32-bit integer. I.e., not sign extended into
178 // high 32 bits of destination.
179 void SmiToInteger32(Register dst, Register src);
180
181 // Convert smi to 64-bit integer (sign extended if necessary).
182 void SmiToInteger64(Register dst, Register src);
183
184 // Multiply a positive smi's integer value by a power of two.
185 // Provides result as 64-bit integer value.
186 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
187 Register src,
188 int power);
189
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000190 // Simple comparison of smis.
191 void SmiCompare(Register dst, Register src);
192 void SmiCompare(Register dst, Smi* src);
193 void SmiCompare(const Operand& dst, Register src);
194 void SmiCompare(const Operand& dst, Smi* src);
195 // Sets sign and zero flags depending on value of smi in register.
196 void SmiTest(Register src);
197
ager@chromium.org4af710e2009-09-15 12:20:11 +0000198 // Functions performing a check on a known or potential smi. Returns
199 // a condition that is satisfied if the check is successful.
200
201 // Is the value a tagged smi.
202 Condition CheckSmi(Register src);
203
ager@chromium.org4af710e2009-09-15 12:20:11 +0000204 // Is the value a positive tagged smi.
205 Condition CheckPositiveSmi(Register src);
206
ager@chromium.org4af710e2009-09-15 12:20:11 +0000207 // Are both values are tagged smis.
208 Condition CheckBothSmi(Register first, Register second);
209
ager@chromium.org4af710e2009-09-15 12:20:11 +0000210 // Is the value the minimum smi value (since we are using
211 // two's complement numbers, negating the value is known to yield
212 // a non-smi value).
213 Condition CheckIsMinSmi(Register src);
214
ager@chromium.org4af710e2009-09-15 12:20:11 +0000215 // Checks whether an 32-bit integer value is a valid for conversion
216 // to a smi.
217 Condition CheckInteger32ValidSmiValue(Register src);
218
ager@chromium.org3811b432009-10-28 14:53:37 +0000219 // Checks whether an 32-bit unsigned integer value is a valid for
220 // conversion to a smi.
221 Condition CheckUInteger32ValidSmiValue(Register src);
222
ager@chromium.org4af710e2009-09-15 12:20:11 +0000223 // Test-and-jump functions. Typically combines a check function
224 // above with a conditional jump.
225
226 // Jump if the value cannot be represented by a smi.
227 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
228
ager@chromium.org3811b432009-10-28 14:53:37 +0000229 // Jump if the unsigned integer value cannot be represented by a smi.
230 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
231
ager@chromium.org4af710e2009-09-15 12:20:11 +0000232 // Jump to label if the value is a tagged smi.
233 void JumpIfSmi(Register src, Label* on_smi);
234
235 // Jump to label if the value is not a tagged smi.
236 void JumpIfNotSmi(Register src, Label* on_not_smi);
237
238 // Jump to label if the value is not a positive tagged smi.
239 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
240
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000241 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000242 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000243 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000244
ager@chromium.org4af710e2009-09-15 12:20:11 +0000245 // Jump if either or both register are not smi values.
246 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
247
248 // Operations on tagged smi values.
249
250 // Smis represent a subset of integers. The subset is always equivalent to
251 // a two's complement interpretation of a fixed number of bits.
252
253 // Optimistically adds an integer constant to a supposed smi.
254 // If the src is not a smi, or the result is not a smi, jump to
255 // the label.
256 void SmiTryAddConstant(Register dst,
257 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000258 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000259 Label* on_not_smi_result);
260
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000261 // Add an integer constant to a tagged smi, giving a tagged smi as result.
262 // No overflow testing on the result is done.
263 void SmiAddConstant(Register dst, Register src, Smi* constant);
264
ager@chromium.org4af710e2009-09-15 12:20:11 +0000265 // Add an integer constant to a tagged smi, giving a tagged smi as result,
266 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000267 void SmiAddConstant(Register dst,
268 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000269 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000270 Label* on_not_smi_result);
271
272 // Subtract an integer constant from a tagged smi, giving a tagged smi as
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000273 // result. No testing on the result is done.
274 void SmiSubConstant(Register dst, Register src, Smi* constant);
275
276 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000277 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000278 void SmiSubConstant(Register dst,
279 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000280 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000281 Label* on_not_smi_result);
282
283 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000284 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000285 void SmiNeg(Register dst,
286 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000287 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000288
289 // Adds smi values and return the result as a smi.
290 // If dst is src1, then src1 will be destroyed, even if
291 // the operation is unsuccessful.
292 void SmiAdd(Register dst,
293 Register src1,
294 Register src2,
295 Label* on_not_smi_result);
296
297 // Subtracts smi values and return the result as a smi.
298 // If dst is src1, then src1 will be destroyed, even if
299 // the operation is unsuccessful.
300 void SmiSub(Register dst,
301 Register src1,
302 Register src2,
303 Label* on_not_smi_result);
304
305 // Multiplies smi values and return the result as a smi,
306 // if possible.
307 // If dst is src1, then src1 will be destroyed, even if
308 // the operation is unsuccessful.
309 void SmiMul(Register dst,
310 Register src1,
311 Register src2,
312 Label* on_not_smi_result);
313
314 // Divides one smi by another and returns the quotient.
315 // Clobbers rax and rdx registers.
316 void SmiDiv(Register dst,
317 Register src1,
318 Register src2,
319 Label* on_not_smi_result);
320
321 // Divides one smi by another and returns the remainder.
322 // Clobbers rax and rdx registers.
323 void SmiMod(Register dst,
324 Register src1,
325 Register src2,
326 Label* on_not_smi_result);
327
328 // Bitwise operations.
329 void SmiNot(Register dst, Register src);
330 void SmiAnd(Register dst, Register src1, Register src2);
331 void SmiOr(Register dst, Register src1, Register src2);
332 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000333 void SmiAndConstant(Register dst, Register src1, Smi* constant);
334 void SmiOrConstant(Register dst, Register src1, Smi* constant);
335 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000336
337 void SmiShiftLeftConstant(Register dst,
338 Register src,
339 int shift_value,
340 Label* on_not_smi_result);
341 void SmiShiftLogicalRightConstant(Register dst,
342 Register src,
343 int shift_value,
344 Label* on_not_smi_result);
345 void SmiShiftArithmeticRightConstant(Register dst,
346 Register src,
347 int shift_value);
348
349 // Shifts a smi value to the left, and returns the result if that is a smi.
350 // Uses and clobbers rcx, so dst may not be rcx.
351 void SmiShiftLeft(Register dst,
352 Register src1,
353 Register src2,
354 Label* on_not_smi_result);
355 // Shifts a smi value to the right, shifting in zero bits at the top, and
356 // returns the unsigned intepretation of the result if that is a smi.
357 // Uses and clobbers rcx, so dst may not be rcx.
358 void SmiShiftLogicalRight(Register dst,
359 Register src1,
360 Register src2,
361 Label* on_not_smi_result);
362 // Shifts a smi value to the right, sign extending the top, and
363 // returns the signed intepretation of the result. That will always
364 // be a valid smi value, since it's numerically smaller than the
365 // original.
366 // Uses and clobbers rcx, so dst may not be rcx.
367 void SmiShiftArithmeticRight(Register dst,
368 Register src1,
369 Register src2);
370
371 // Specialized operations
372
373 // Select the non-smi register of two registers where exactly one is a
374 // smi. If neither are smis, jump to the failure label.
375 void SelectNonSmi(Register dst,
376 Register src1,
377 Register src2,
378 Label* on_not_smis);
379
380 // Converts, if necessary, a smi to a combination of number and
381 // multiplier to be used as a scaled index.
382 // The src register contains a *positive* smi value. The shift is the
383 // power of two to multiply the index value by (e.g.
384 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
385 // The returned index register may be either src or dst, depending
386 // on what is most efficient. If src and dst are different registers,
387 // src is always unchanged.
388 SmiIndex SmiToIndex(Register dst, Register src, int shift);
389
390 // Converts a positive smi to a negative index.
391 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
392
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000393 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000394 void Move(Register dst, Smi* source) {
395 Set(dst, reinterpret_cast<int64_t>(source));
396 }
397
398 void Move(const Operand& dst, Smi* source) {
399 Set(dst, reinterpret_cast<int64_t>(source));
400 }
401
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000402 void Push(Smi* smi);
403 void Test(const Operand& dst, Smi* source);
404
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000405 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000406 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000407
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000408 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000409 void Set(Register dst, int64_t x);
410 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000411
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000412 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000413 void Move(Register dst, Handle<Object> source);
414 void Move(const Operand& dst, Handle<Object> source);
415 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000416 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000417 void Push(Handle<Object> source);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000418
419 // Emit code to discard a non-negative number of pointer-sized elements
420 // from the stack, clobbering only the rsp register.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000421 void Drop(int stack_elements);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000422
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000423 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000424
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000425 // Control Flow
426 void Jump(Address destination, RelocInfo::Mode rmode);
427 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000428 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
429
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000430 void Call(Address destination, RelocInfo::Mode rmode);
431 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000432 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000433
ager@chromium.org9085a012009-05-11 19:22:57 +0000434 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000435 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000436 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000437 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000438 void CmpObjectType(Register heap_object, InstanceType type, Register map);
439
440 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000441 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000442 void CmpInstanceType(Register map, InstanceType type);
443
444 // FCmp is similar to integer cmp, but requires unsigned
445 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
446 void FCmp();
447
448 // ---------------------------------------------------------------------------
449 // Exception handling
450
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000451 // Push a new try handler and link into try handler chain. The return
452 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000453 void PushTryHandler(CodeLocation try_location, HandlerType type);
454
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000455 // Unlink the stack handler on top of the stack from the try handler chain.
456 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000457
458 // ---------------------------------------------------------------------------
459 // Inline caching support
460
461 // Generates code that verifies that the maps of objects in the
462 // prototype chain of object hasn't changed since the code was
463 // generated and branches to the miss label if any map has. If
464 // necessary the function also generates code for security check
465 // in case of global object holders. The scratch and holder
466 // registers are always clobbered, but the object register is only
467 // clobbered if it the same as the holder register. The function
468 // returns a register containing the holder - either object_reg or
469 // holder_reg.
470 Register CheckMaps(JSObject* object, Register object_reg,
471 JSObject* holder, Register holder_reg,
472 Register scratch, Label* miss);
473
474 // Generate code for checking access rights - used for security checks
475 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000476 // is left untouched, but the scratch register and kScratchRegister,
477 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000478 void CheckAccessGlobalProxy(Register holder_reg,
479 Register scratch,
480 Label* miss);
481
482
483 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000484 // Allocation support
485
486 // Allocate an object in new space. If the new space is exhausted control
487 // continues at the gc_required label. The allocated object is returned in
488 // result and end of the new object is returned in result_end. The register
489 // scratch can be passed as no_reg in which case an additional object
490 // reference will be added to the reloc info. The returned pointers in result
491 // and result_end have not yet been tagged as heap objects. If
492 // result_contains_top_on_entry is true the content of result is known to be
493 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000494 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000495 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000496 void AllocateInNewSpace(int object_size,
497 Register result,
498 Register result_end,
499 Register scratch,
500 Label* gc_required,
501 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000502
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000503 void AllocateInNewSpace(int header_size,
504 ScaleFactor element_size,
505 Register element_count,
506 Register result,
507 Register result_end,
508 Register scratch,
509 Label* gc_required,
510 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000511
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000512 void AllocateInNewSpace(Register object_size,
513 Register result,
514 Register result_end,
515 Register scratch,
516 Label* gc_required,
517 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000518
519 // Undo allocation in new space. The object passed and objects allocated after
520 // it will no longer be allocated. Make sure that no pointers are left to the
521 // object(s) no longer allocated as they would be invalid when allocation is
522 // un-done.
523 void UndoAllocationInNewSpace(Register object);
524
ager@chromium.org3811b432009-10-28 14:53:37 +0000525 // Allocate a heap number in new space with undefined value. Returns
526 // tagged pointer in result register, or jumps to gc_required if new
527 // space is full.
528 void AllocateHeapNumber(Register result,
529 Register scratch,
530 Label* gc_required);
531
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000532 // Allocate a sequential string. All the header fields of the string object
533 // are initialized.
534 void AllocateTwoByteString(Register result,
535 Register length,
536 Register scratch1,
537 Register scratch2,
538 Register scratch3,
539 Label* gc_required);
540 void AllocateAsciiString(Register result,
541 Register length,
542 Register scratch1,
543 Register scratch2,
544 Register scratch3,
545 Label* gc_required);
546
547 // Allocate a raw cons string object. Only the map field of the result is
548 // initialized.
549 void AllocateConsString(Register result,
550 Register scratch1,
551 Register scratch2,
552 Label* gc_required);
553 void AllocateAsciiConsString(Register result,
554 Register scratch1,
555 Register scratch2,
556 Label* gc_required);
557
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000558 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000559 // Support functions.
560
561 // Check if result is zero and op is negative.
562 void NegativeZeroTest(Register result, Register op, Label* then_label);
563
564 // Check if result is zero and op is negative in code using jump targets.
565 void NegativeZeroTest(CodeGenerator* cgen,
566 Register result,
567 Register op,
568 JumpTarget* then_target);
569
570 // Check if result is zero and any of op1 and op2 are negative.
571 // Register scratch is destroyed, and it must be different from op2.
572 void NegativeZeroTest(Register result, Register op1, Register op2,
573 Register scratch, Label* then_label);
574
575 // Try to get function prototype of a function and puts the value in
576 // the result register. Checks that the function really is a
577 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000578 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000579 // clobbered.
580 void TryGetFunctionPrototype(Register function,
581 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000582 Label* miss);
583
584 // Generates code for reporting that an illegal operation has
585 // occurred.
586 void IllegalOperation(int num_arguments);
587
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000588 // Find the function context up the context chain.
589 void LoadContext(Register dst, int context_chain_length);
590
ager@chromium.org9085a012009-05-11 19:22:57 +0000591 // ---------------------------------------------------------------------------
592 // Runtime calls
593
594 // Call a code stub.
595 void CallStub(CodeStub* stub);
596
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000597 // Tail call a code stub (jump).
598 void TailCallStub(CodeStub* stub);
599
ager@chromium.org9085a012009-05-11 19:22:57 +0000600 // Return from a code stub after popping its arguments.
601 void StubReturn(int argc);
602
603 // Call a runtime routine.
604 // Eventually this should be used for all C calls.
605 void CallRuntime(Runtime::Function* f, int num_arguments);
606
607 // Convenience function: Same as above, but takes the fid instead.
608 void CallRuntime(Runtime::FunctionId id, int num_arguments);
609
610 // Tail call of a runtime routine (jump).
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000611 // Like JumpToRuntime, but also takes care of passing the number
ager@chromium.org9085a012009-05-11 19:22:57 +0000612 // of arguments.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000613 void TailCallRuntime(const ExternalReference& ext,
614 int num_arguments,
615 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000616
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000617 // Jump to a runtime routine.
618 void JumpToRuntime(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000619
620
621 // ---------------------------------------------------------------------------
622 // Utilities
623
624 void Ret();
625
626 struct Unresolved {
627 int pc;
628 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
629 const char* name;
630 };
631 List<Unresolved>* unresolved() { return &unresolved_; }
632
633 Handle<Object> CodeObject() { return code_object_; }
634
635
636 // ---------------------------------------------------------------------------
637 // StatsCounter support
638
639 void SetCounter(StatsCounter* counter, int value);
640 void IncrementCounter(StatsCounter* counter, int value);
641 void DecrementCounter(StatsCounter* counter, int value);
642
643
644 // ---------------------------------------------------------------------------
645 // Debugging
646
647 // Calls Abort(msg) if the condition cc is not satisfied.
648 // Use --debug_code to enable.
649 void Assert(Condition cc, const char* msg);
650
651 // Like Assert(), but always enabled.
652 void Check(Condition cc, const char* msg);
653
654 // Print a message to stdout and abort execution.
655 void Abort(const char* msg);
656
657 // Verify restrictions about code generated in stubs.
658 void set_generating_stub(bool value) { generating_stub_ = value; }
659 bool generating_stub() { return generating_stub_; }
660 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
661 bool allow_stub_calls() { return allow_stub_calls_; }
662
663 private:
664 List<Unresolved> unresolved_;
665 bool generating_stub_;
666 bool allow_stub_calls_;
667 Handle<Object> code_object_; // This handle will be patched with the code
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000668 // object on installation.
ager@chromium.org9085a012009-05-11 19:22:57 +0000669
670 // Helper functions for generating invokes.
671 void InvokePrologue(const ParameterCount& expected,
672 const ParameterCount& actual,
673 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000674 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000675 Label* done,
676 InvokeFlag flag);
677
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000678 // Prepares for a call or jump to a builtin by doing two things:
679 // 1. Emits code that fetches the builtin's function object from the context
680 // at runtime, and puts it in the register rdi.
681 // 2. Fetches the builtin's code object, and returns it in a handle, at
682 // compile time, so that later code can emit instructions to jump or call
683 // the builtin directly. If the code object has not yet been created, it
684 // returns the builtin code object for IllegalFunction, and sets the
685 // output parameter "resolved" to false. Code that uses the return value
686 // should then add the address and the builtin name to the list of fixups
687 // called unresolved_, which is fixed up by the bootstrapper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000688 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
689
690 // Activation support.
691 void EnterFrame(StackFrame::Type type);
692 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000693
694 // Allocation support helpers.
695 void LoadAllocationTopHelper(Register result,
696 Register result_end,
697 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000698 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000699 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000700};
701
702
ager@chromium.org4af710e2009-09-15 12:20:11 +0000703// The code patcher is used to patch (typically) small parts of code e.g. for
704// debugging and other types of instrumentation. When using the code patcher
705// the exact number of bytes specified must be emitted. Is not legal to emit
706// relocation information. If any of these constraints are violated it causes
707// an assertion.
708class CodePatcher {
709 public:
710 CodePatcher(byte* address, int size);
711 virtual ~CodePatcher();
712
713 // Macro assembler to emit code.
714 MacroAssembler* masm() { return &masm_; }
715
716 private:
717 byte* address_; // The address of the code being patched.
718 int size_; // Number of bytes of the expected patch size.
719 MacroAssembler masm_; // Macro assembler used to generate the code.
720};
721
722
ager@chromium.org9085a012009-05-11 19:22:57 +0000723// -----------------------------------------------------------------------------
724// Static helper functions.
725
726// Generate an Operand for loading a field from an object.
727static inline Operand FieldOperand(Register object, int offset) {
728 return Operand(object, offset - kHeapObjectTag);
729}
730
731
732// Generate an Operand for loading an indexed field from an object.
733static inline Operand FieldOperand(Register object,
734 Register index,
735 ScaleFactor scale,
736 int offset) {
737 return Operand(object, index, scale, offset - kHeapObjectTag);
738}
739
740
741#ifdef GENERATED_CODE_COVERAGE
742extern void LogGeneratedCodeCoverage(const char* file_line);
743#define CODE_COVERAGE_STRINGIFY(x) #x
744#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
745#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
746#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000747 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000748 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
749 masm->pushfd(); \
750 masm->pushad(); \
751 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000752 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000753 masm->pop(rax); \
754 masm->popad(); \
755 masm->popfd(); \
756 } \
757 masm->
758#else
759#define ACCESS_MASM(masm) masm->
760#endif
761
762
763} } // namespace v8::internal
764
765#endif // V8_X64_MACRO_ASSEMBLER_X64_H_