blob: 97200051a3f1963f06000dab0ffab1d39e9ef9aa [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
ager@chromium.org9085a012009-05-11 19:22:57 +000041// Forward declaration.
42class JumpTarget;
43
ager@chromium.org4af710e2009-09-15 12:20:11 +000044struct SmiIndex {
45 SmiIndex(Register index_register, ScaleFactor scale)
46 : reg(index_register),
47 scale(scale) {}
48 Register reg;
49 ScaleFactor scale;
50};
ager@chromium.org9085a012009-05-11 19:22:57 +000051
ager@chromium.org9085a012009-05-11 19:22:57 +000052// MacroAssembler implements a collection of frequently used macros.
53class MacroAssembler: public Assembler {
54 public:
55 MacroAssembler(void* buffer, int size);
56
ager@chromium.org18ad94b2009-09-02 08:22:29 +000057 void LoadRoot(Register destination, Heap::RootListIndex index);
58 void CompareRoot(Register with, Heap::RootListIndex index);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000059 void CompareRoot(Operand with, Heap::RootListIndex index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +000060 void PushRoot(Heap::RootListIndex index);
61
ager@chromium.org9085a012009-05-11 19:22:57 +000062 // ---------------------------------------------------------------------------
63 // GC Support
64
65 // Set the remembered set bit for [object+offset].
66 // object is the object being stored into, value is the object being stored.
67 // If offset is zero, then the scratch register contains the array index into
68 // the elements array represented as a Smi.
69 // All registers are clobbered by the operation.
70 void RecordWrite(Register object,
71 int offset,
72 Register value,
73 Register scratch);
74
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000075 // Set the remembered set bit for [object+offset].
76 // The value is known to not be a smi.
77 // object is the object being stored into, value is the object being stored.
78 // If offset is zero, then the scratch register contains the array index into
79 // the elements array represented as a Smi.
80 // All registers are clobbered by the operation.
81 void RecordWriteNonSmi(Register object,
82 int offset,
83 Register value,
84 Register scratch);
85
86
ager@chromium.org9085a012009-05-11 19:22:57 +000087#ifdef ENABLE_DEBUGGER_SUPPORT
88 // ---------------------------------------------------------------------------
89 // Debugger Support
90
91 void SaveRegistersToMemory(RegList regs);
92 void RestoreRegistersFromMemory(RegList regs);
93 void PushRegistersFromMemory(RegList regs);
94 void PopRegistersToMemory(RegList regs);
95 void CopyRegistersFromStackToMemory(Register base,
96 Register scratch,
97 RegList regs);
98#endif
99
100 // ---------------------------------------------------------------------------
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000101 // Stack limit support
102
103 // Do simple test for stack overflow. This doesn't handle an overflow.
104 void StackLimitCheck(Label* on_stack_limit_hit);
105
106 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000107 // Activation frames
108
109 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
110 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
111
112 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
113 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
114
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115 // Enter specific kind of exit frame; either in normal or
116 // debug mode. Expects the number of arguments in register rax and
ager@chromium.orga1645e22009-09-09 19:27:10 +0000117 // sets up the number of arguments in register rdi and the pointer
118 // to the first argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119 void EnterExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000120
ager@chromium.orga1645e22009-09-09 19:27:10 +0000121 // Leave the current exit frame. Expects/provides the return value in
122 // register rax:rdx (untouched) and the pointer to the first
123 // argument in register rsi.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000124 void LeaveExitFrame(ExitFrame::Mode mode, int result_size = 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000125
126
127 // ---------------------------------------------------------------------------
128 // JavaScript invokes
129
130 // Invoke the JavaScript function code by either calling or jumping.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000131 void InvokeCode(Register code,
ager@chromium.org9085a012009-05-11 19:22:57 +0000132 const ParameterCount& expected,
133 const ParameterCount& actual,
134 InvokeFlag flag);
135
136 void InvokeCode(Handle<Code> code,
137 const ParameterCount& expected,
138 const ParameterCount& actual,
139 RelocInfo::Mode rmode,
140 InvokeFlag flag);
141
142 // Invoke the JavaScript function in the given register. Changes the
143 // current context to the context in the function before invoking.
144 void InvokeFunction(Register function,
145 const ParameterCount& actual,
146 InvokeFlag flag);
147
148 // Invoke specified builtin JavaScript function. Adds an entry to
149 // the unresolved list if the name does not resolve.
150 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
151
152 // Store the code object for the given builtin in the target register.
153 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
154
ager@chromium.org4af710e2009-09-15 12:20:11 +0000155
156 // ---------------------------------------------------------------------------
157 // Smi tagging, untagging and operations on tagged smis.
158
159 // Conversions between tagged smi values and non-tagged integer values.
160
161 // Tag an integer value. The result must be known to be a valid smi value.
162 // Only uses the low 32 bits of the src register.
163 void Integer32ToSmi(Register dst, Register src);
164
165 // Tag an integer value if possible, or jump the integer value cannot be
166 // represented as a smi. Only uses the low 32 bit of the src registers.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000167 // NOTICE: Destroys the dst register even if unsuccessful!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000168 void Integer32ToSmi(Register dst, Register src, Label* on_overflow);
169
170 // Adds constant to src and tags the result as a smi.
171 // Result must be a valid smi.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000172 void Integer64PlusConstantToSmi(Register dst, Register src, int constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000173
174 // Convert smi to 32-bit integer. I.e., not sign extended into
175 // high 32 bits of destination.
176 void SmiToInteger32(Register dst, Register src);
177
178 // Convert smi to 64-bit integer (sign extended if necessary).
179 void SmiToInteger64(Register dst, Register src);
180
181 // Multiply a positive smi's integer value by a power of two.
182 // Provides result as 64-bit integer value.
183 void PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
184 Register src,
185 int power);
186
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000187 // Simple comparison of smis.
188 void SmiCompare(Register dst, Register src);
189 void SmiCompare(Register dst, Smi* src);
190 void SmiCompare(const Operand& dst, Register src);
191 void SmiCompare(const Operand& dst, Smi* src);
192 // Sets sign and zero flags depending on value of smi in register.
193 void SmiTest(Register src);
194
ager@chromium.org4af710e2009-09-15 12:20:11 +0000195 // Functions performing a check on a known or potential smi. Returns
196 // a condition that is satisfied if the check is successful.
197
198 // Is the value a tagged smi.
199 Condition CheckSmi(Register src);
200
ager@chromium.org4af710e2009-09-15 12:20:11 +0000201 // Is the value a positive tagged smi.
202 Condition CheckPositiveSmi(Register src);
203
ager@chromium.org4af710e2009-09-15 12:20:11 +0000204 // Are both values are tagged smis.
205 Condition CheckBothSmi(Register first, Register second);
206
ager@chromium.org4af710e2009-09-15 12:20:11 +0000207 // Is the value the minimum smi value (since we are using
208 // two's complement numbers, negating the value is known to yield
209 // a non-smi value).
210 Condition CheckIsMinSmi(Register src);
211
ager@chromium.org4af710e2009-09-15 12:20:11 +0000212 // Checks whether an 32-bit integer value is a valid for conversion
213 // to a smi.
214 Condition CheckInteger32ValidSmiValue(Register src);
215
ager@chromium.org3811b432009-10-28 14:53:37 +0000216 // Checks whether an 32-bit unsigned integer value is a valid for
217 // conversion to a smi.
218 Condition CheckUInteger32ValidSmiValue(Register src);
219
ager@chromium.org4af710e2009-09-15 12:20:11 +0000220 // Test-and-jump functions. Typically combines a check function
221 // above with a conditional jump.
222
223 // Jump if the value cannot be represented by a smi.
224 void JumpIfNotValidSmiValue(Register src, Label* on_invalid);
225
ager@chromium.org3811b432009-10-28 14:53:37 +0000226 // Jump if the unsigned integer value cannot be represented by a smi.
227 void JumpIfUIntNotValidSmiValue(Register src, Label* on_invalid);
228
ager@chromium.org4af710e2009-09-15 12:20:11 +0000229 // Jump to label if the value is a tagged smi.
230 void JumpIfSmi(Register src, Label* on_smi);
231
232 // Jump to label if the value is not a tagged smi.
233 void JumpIfNotSmi(Register src, Label* on_not_smi);
234
235 // Jump to label if the value is not a positive tagged smi.
236 void JumpIfNotPositiveSmi(Register src, Label* on_not_smi);
237
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000238 // Jump to label if the value, which must be a tagged smi, has value equal
ager@chromium.org4af710e2009-09-15 12:20:11 +0000239 // to the constant.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000240 void JumpIfSmiEqualsConstant(Register src, Smi* constant, Label* on_equals);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000241
ager@chromium.org4af710e2009-09-15 12:20:11 +0000242 // Jump if either or both register are not smi values.
243 void JumpIfNotBothSmi(Register src1, Register src2, Label* on_not_both_smi);
244
245 // Operations on tagged smi values.
246
247 // Smis represent a subset of integers. The subset is always equivalent to
248 // a two's complement interpretation of a fixed number of bits.
249
250 // Optimistically adds an integer constant to a supposed smi.
251 // If the src is not a smi, or the result is not a smi, jump to
252 // the label.
253 void SmiTryAddConstant(Register dst,
254 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000255 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000256 Label* on_not_smi_result);
257
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000258 // Add an integer constant to a tagged smi, giving a tagged smi as result.
259 // No overflow testing on the result is done.
260 void SmiAddConstant(Register dst, Register src, Smi* constant);
261
ager@chromium.org4af710e2009-09-15 12:20:11 +0000262 // Add an integer constant to a tagged smi, giving a tagged smi as result,
263 // or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000264 void SmiAddConstant(Register dst,
265 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000266 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000267 Label* on_not_smi_result);
268
269 // Subtract an integer constant from a tagged smi, giving a tagged smi as
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000270 // result. No testing on the result is done.
271 void SmiSubConstant(Register dst, Register src, Smi* constant);
272
273 // Subtract an integer constant from a tagged smi, giving a tagged smi as
ager@chromium.org4af710e2009-09-15 12:20:11 +0000274 // result, or jumping to a label if the result cannot be represented by a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000275 void SmiSubConstant(Register dst,
276 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000277 Smi* constant,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000278 Label* on_not_smi_result);
279
280 // Negating a smi can give a negative zero or too large positive value.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000281 // NOTICE: This operation jumps on success, not failure!
ager@chromium.org4af710e2009-09-15 12:20:11 +0000282 void SmiNeg(Register dst,
283 Register src,
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000284 Label* on_smi_result);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000285
286 // Adds smi values and return the result as a smi.
287 // If dst is src1, then src1 will be destroyed, even if
288 // the operation is unsuccessful.
289 void SmiAdd(Register dst,
290 Register src1,
291 Register src2,
292 Label* on_not_smi_result);
293
294 // Subtracts smi values and return the result as a smi.
295 // If dst is src1, then src1 will be destroyed, even if
296 // the operation is unsuccessful.
297 void SmiSub(Register dst,
298 Register src1,
299 Register src2,
300 Label* on_not_smi_result);
301
302 // Multiplies smi values and return the result as a smi,
303 // if possible.
304 // If dst is src1, then src1 will be destroyed, even if
305 // the operation is unsuccessful.
306 void SmiMul(Register dst,
307 Register src1,
308 Register src2,
309 Label* on_not_smi_result);
310
311 // Divides one smi by another and returns the quotient.
312 // Clobbers rax and rdx registers.
313 void SmiDiv(Register dst,
314 Register src1,
315 Register src2,
316 Label* on_not_smi_result);
317
318 // Divides one smi by another and returns the remainder.
319 // Clobbers rax and rdx registers.
320 void SmiMod(Register dst,
321 Register src1,
322 Register src2,
323 Label* on_not_smi_result);
324
325 // Bitwise operations.
326 void SmiNot(Register dst, Register src);
327 void SmiAnd(Register dst, Register src1, Register src2);
328 void SmiOr(Register dst, Register src1, Register src2);
329 void SmiXor(Register dst, Register src1, Register src2);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000330 void SmiAndConstant(Register dst, Register src1, Smi* constant);
331 void SmiOrConstant(Register dst, Register src1, Smi* constant);
332 void SmiXorConstant(Register dst, Register src1, Smi* constant);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333
334 void SmiShiftLeftConstant(Register dst,
335 Register src,
336 int shift_value,
337 Label* on_not_smi_result);
338 void SmiShiftLogicalRightConstant(Register dst,
339 Register src,
340 int shift_value,
341 Label* on_not_smi_result);
342 void SmiShiftArithmeticRightConstant(Register dst,
343 Register src,
344 int shift_value);
345
346 // Shifts a smi value to the left, and returns the result if that is a smi.
347 // Uses and clobbers rcx, so dst may not be rcx.
348 void SmiShiftLeft(Register dst,
349 Register src1,
350 Register src2,
351 Label* on_not_smi_result);
352 // Shifts a smi value to the right, shifting in zero bits at the top, and
353 // returns the unsigned intepretation of the result if that is a smi.
354 // Uses and clobbers rcx, so dst may not be rcx.
355 void SmiShiftLogicalRight(Register dst,
356 Register src1,
357 Register src2,
358 Label* on_not_smi_result);
359 // Shifts a smi value to the right, sign extending the top, and
360 // returns the signed intepretation of the result. That will always
361 // be a valid smi value, since it's numerically smaller than the
362 // original.
363 // Uses and clobbers rcx, so dst may not be rcx.
364 void SmiShiftArithmeticRight(Register dst,
365 Register src1,
366 Register src2);
367
368 // Specialized operations
369
370 // Select the non-smi register of two registers where exactly one is a
371 // smi. If neither are smis, jump to the failure label.
372 void SelectNonSmi(Register dst,
373 Register src1,
374 Register src2,
375 Label* on_not_smis);
376
377 // Converts, if necessary, a smi to a combination of number and
378 // multiplier to be used as a scaled index.
379 // The src register contains a *positive* smi value. The shift is the
380 // power of two to multiply the index value by (e.g.
381 // to index by smi-value * kPointerSize, pass the smi and kPointerSizeLog2).
382 // The returned index register may be either src or dst, depending
383 // on what is most efficient. If src and dst are different registers,
384 // src is always unchanged.
385 SmiIndex SmiToIndex(Register dst, Register src, int shift);
386
387 // Converts a positive smi to a negative index.
388 SmiIndex SmiToNegativeIndex(Register dst, Register src, int shift);
389
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000390 // Basic Smi operations.
ager@chromium.org3811b432009-10-28 14:53:37 +0000391 void Move(Register dst, Smi* source) {
392 Set(dst, reinterpret_cast<int64_t>(source));
393 }
394
395 void Move(const Operand& dst, Smi* source) {
396 Set(dst, reinterpret_cast<int64_t>(source));
397 }
398
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000399 void Push(Smi* smi);
400 void Test(const Operand& dst, Smi* source);
401
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000402 // ---------------------------------------------------------------------------
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000403 // Macro instructions.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000404
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000405 // Load a register with a long value as efficiently as possible.
ager@chromium.orge2902be2009-06-08 12:21:35 +0000406 void Set(Register dst, int64_t x);
407 void Set(const Operand& dst, int64_t x);
ager@chromium.org9085a012009-05-11 19:22:57 +0000408
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000409 // Handle support
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000410 void Move(Register dst, Handle<Object> source);
411 void Move(const Operand& dst, Handle<Object> source);
412 void Cmp(Register dst, Handle<Object> source);
ager@chromium.org3e875802009-06-29 08:26:34 +0000413 void Cmp(const Operand& dst, Handle<Object> source);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000414 void Push(Handle<Object> source);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000415 void Drop(int stack_elements);
416 void Call(Label* target) { call(target); }
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000417
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000418 // Control Flow
419 void Jump(Address destination, RelocInfo::Mode rmode);
420 void Jump(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000421 void Jump(Handle<Code> code_object, RelocInfo::Mode rmode);
422
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000423 void Call(Address destination, RelocInfo::Mode rmode);
424 void Call(ExternalReference ext);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000425 void Call(Handle<Code> code_object, RelocInfo::Mode rmode);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000426
ager@chromium.org9085a012009-05-11 19:22:57 +0000427 // Compare object type for heap object.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000428 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000429 // Incoming register is heap_object and outgoing register is map.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000430 // They may be the same register, and may be kScratchRegister.
ager@chromium.org9085a012009-05-11 19:22:57 +0000431 void CmpObjectType(Register heap_object, InstanceType type, Register map);
432
433 // Compare instance type for map.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000434 // Always use unsigned comparisons: above and below, not less and greater.
ager@chromium.org9085a012009-05-11 19:22:57 +0000435 void CmpInstanceType(Register map, InstanceType type);
436
437 // FCmp is similar to integer cmp, but requires unsigned
438 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
439 void FCmp();
440
441 // ---------------------------------------------------------------------------
442 // Exception handling
443
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000444 // Push a new try handler and link into try handler chain. The return
445 // address must be pushed before calling this helper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000446 void PushTryHandler(CodeLocation try_location, HandlerType type);
447
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000448 // Unlink the stack handler on top of the stack from the try handler chain.
449 void PopTryHandler();
ager@chromium.org9085a012009-05-11 19:22:57 +0000450
451 // ---------------------------------------------------------------------------
452 // Inline caching support
453
454 // Generates code that verifies that the maps of objects in the
455 // prototype chain of object hasn't changed since the code was
456 // generated and branches to the miss label if any map has. If
457 // necessary the function also generates code for security check
458 // in case of global object holders. The scratch and holder
459 // registers are always clobbered, but the object register is only
460 // clobbered if it the same as the holder register. The function
461 // returns a register containing the holder - either object_reg or
462 // holder_reg.
463 Register CheckMaps(JSObject* object, Register object_reg,
464 JSObject* holder, Register holder_reg,
465 Register scratch, Label* miss);
466
467 // Generate code for checking access rights - used for security checks
468 // on access to global objects across environments. The holder register
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000469 // is left untouched, but the scratch register and kScratchRegister,
470 // which must be different, are clobbered.
ager@chromium.org9085a012009-05-11 19:22:57 +0000471 void CheckAccessGlobalProxy(Register holder_reg,
472 Register scratch,
473 Label* miss);
474
475
476 // ---------------------------------------------------------------------------
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000477 // Allocation support
478
479 // Allocate an object in new space. If the new space is exhausted control
480 // continues at the gc_required label. The allocated object is returned in
481 // result and end of the new object is returned in result_end. The register
482 // scratch can be passed as no_reg in which case an additional object
483 // reference will be added to the reloc info. The returned pointers in result
484 // and result_end have not yet been tagged as heap objects. If
485 // result_contains_top_on_entry is true the content of result is known to be
486 // the allocation top on entry (could be result_end from a previous call to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000487 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000488 // should be no_reg as it is never used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000489 void AllocateInNewSpace(int object_size,
490 Register result,
491 Register result_end,
492 Register scratch,
493 Label* gc_required,
494 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000495
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000496 void AllocateInNewSpace(int header_size,
497 ScaleFactor element_size,
498 Register element_count,
499 Register result,
500 Register result_end,
501 Register scratch,
502 Label* gc_required,
503 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000504
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000505 void AllocateInNewSpace(Register object_size,
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
512 // Undo allocation in new space. The object passed and objects allocated after
513 // it will no longer be allocated. Make sure that no pointers are left to the
514 // object(s) no longer allocated as they would be invalid when allocation is
515 // un-done.
516 void UndoAllocationInNewSpace(Register object);
517
ager@chromium.org3811b432009-10-28 14:53:37 +0000518 // Allocate a heap number in new space with undefined value. Returns
519 // tagged pointer in result register, or jumps to gc_required if new
520 // space is full.
521 void AllocateHeapNumber(Register result,
522 Register scratch,
523 Label* gc_required);
524
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000525 // Allocate a sequential string. All the header fields of the string object
526 // are initialized.
527 void AllocateTwoByteString(Register result,
528 Register length,
529 Register scratch1,
530 Register scratch2,
531 Register scratch3,
532 Label* gc_required);
533 void AllocateAsciiString(Register result,
534 Register length,
535 Register scratch1,
536 Register scratch2,
537 Register scratch3,
538 Label* gc_required);
539
540 // Allocate a raw cons string object. Only the map field of the result is
541 // initialized.
542 void AllocateConsString(Register result,
543 Register scratch1,
544 Register scratch2,
545 Label* gc_required);
546 void AllocateAsciiConsString(Register result,
547 Register scratch1,
548 Register scratch2,
549 Label* gc_required);
550
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000551 // ---------------------------------------------------------------------------
ager@chromium.org9085a012009-05-11 19:22:57 +0000552 // Support functions.
553
554 // Check if result is zero and op is negative.
555 void NegativeZeroTest(Register result, Register op, Label* then_label);
556
557 // Check if result is zero and op is negative in code using jump targets.
558 void NegativeZeroTest(CodeGenerator* cgen,
559 Register result,
560 Register op,
561 JumpTarget* then_target);
562
563 // Check if result is zero and any of op1 and op2 are negative.
564 // Register scratch is destroyed, and it must be different from op2.
565 void NegativeZeroTest(Register result, Register op1, Register op2,
566 Register scratch, Label* then_label);
567
568 // Try to get function prototype of a function and puts the value in
569 // the result register. Checks that the function really is a
570 // function and jumps to the miss label if the fast checks fail. The
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000571 // function register will be untouched; the other register may be
ager@chromium.org9085a012009-05-11 19:22:57 +0000572 // clobbered.
573 void TryGetFunctionPrototype(Register function,
574 Register result,
ager@chromium.org9085a012009-05-11 19:22:57 +0000575 Label* miss);
576
577 // Generates code for reporting that an illegal operation has
578 // occurred.
579 void IllegalOperation(int num_arguments);
580
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000581 // Find the function context up the context chain.
582 void LoadContext(Register dst, int context_chain_length);
583
ager@chromium.org9085a012009-05-11 19:22:57 +0000584 // ---------------------------------------------------------------------------
585 // Runtime calls
586
587 // Call a code stub.
588 void CallStub(CodeStub* stub);
589
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000590 // Tail call a code stub (jump).
591 void TailCallStub(CodeStub* stub);
592
ager@chromium.org9085a012009-05-11 19:22:57 +0000593 // Return from a code stub after popping its arguments.
594 void StubReturn(int argc);
595
596 // Call a runtime routine.
597 // Eventually this should be used for all C calls.
598 void CallRuntime(Runtime::Function* f, int num_arguments);
599
600 // Convenience function: Same as above, but takes the fid instead.
601 void CallRuntime(Runtime::FunctionId id, int num_arguments);
602
603 // Tail call of a runtime routine (jump).
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000604 // Like JumpToRuntime, but also takes care of passing the number
ager@chromium.org9085a012009-05-11 19:22:57 +0000605 // of arguments.
ager@chromium.orga1645e22009-09-09 19:27:10 +0000606 void TailCallRuntime(const ExternalReference& ext,
607 int num_arguments,
608 int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000609
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000610 // Jump to a runtime routine.
611 void JumpToRuntime(const ExternalReference& ext, int result_size);
ager@chromium.org9085a012009-05-11 19:22:57 +0000612
613
614 // ---------------------------------------------------------------------------
615 // Utilities
616
617 void Ret();
618
619 struct Unresolved {
620 int pc;
621 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
622 const char* name;
623 };
624 List<Unresolved>* unresolved() { return &unresolved_; }
625
626 Handle<Object> CodeObject() { return code_object_; }
627
628
629 // ---------------------------------------------------------------------------
630 // StatsCounter support
631
632 void SetCounter(StatsCounter* counter, int value);
633 void IncrementCounter(StatsCounter* counter, int value);
634 void DecrementCounter(StatsCounter* counter, int value);
635
636
637 // ---------------------------------------------------------------------------
638 // Debugging
639
640 // Calls Abort(msg) if the condition cc is not satisfied.
641 // Use --debug_code to enable.
642 void Assert(Condition cc, const char* msg);
643
644 // Like Assert(), but always enabled.
645 void Check(Condition cc, const char* msg);
646
647 // Print a message to stdout and abort execution.
648 void Abort(const char* msg);
649
650 // Verify restrictions about code generated in stubs.
651 void set_generating_stub(bool value) { generating_stub_ = value; }
652 bool generating_stub() { return generating_stub_; }
653 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
654 bool allow_stub_calls() { return allow_stub_calls_; }
655
656 private:
657 List<Unresolved> unresolved_;
658 bool generating_stub_;
659 bool allow_stub_calls_;
660 Handle<Object> code_object_; // This handle will be patched with the code
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000661 // object on installation.
ager@chromium.org9085a012009-05-11 19:22:57 +0000662
663 // Helper functions for generating invokes.
664 void InvokePrologue(const ParameterCount& expected,
665 const ParameterCount& actual,
666 Handle<Code> code_constant,
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000667 Register code_register,
ager@chromium.org9085a012009-05-11 19:22:57 +0000668 Label* done,
669 InvokeFlag flag);
670
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000671 // Prepares for a call or jump to a builtin by doing two things:
672 // 1. Emits code that fetches the builtin's function object from the context
673 // at runtime, and puts it in the register rdi.
674 // 2. Fetches the builtin's code object, and returns it in a handle, at
675 // compile time, so that later code can emit instructions to jump or call
676 // the builtin directly. If the code object has not yet been created, it
677 // returns the builtin code object for IllegalFunction, and sets the
678 // output parameter "resolved" to false. Code that uses the return value
679 // should then add the address and the builtin name to the list of fixups
680 // called unresolved_, which is fixed up by the bootstrapper.
ager@chromium.org9085a012009-05-11 19:22:57 +0000681 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
682
683 // Activation support.
684 void EnterFrame(StackFrame::Type type);
685 void LeaveFrame(StackFrame::Type type);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000686
687 // Allocation support helpers.
688 void LoadAllocationTopHelper(Register result,
689 Register result_end,
690 Register scratch,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000691 AllocationFlags flags);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000692 void UpdateAllocationTopHelper(Register result_end, Register scratch);
ager@chromium.org9085a012009-05-11 19:22:57 +0000693};
694
695
ager@chromium.org4af710e2009-09-15 12:20:11 +0000696// The code patcher is used to patch (typically) small parts of code e.g. for
697// debugging and other types of instrumentation. When using the code patcher
698// the exact number of bytes specified must be emitted. Is not legal to emit
699// relocation information. If any of these constraints are violated it causes
700// an assertion.
701class CodePatcher {
702 public:
703 CodePatcher(byte* address, int size);
704 virtual ~CodePatcher();
705
706 // Macro assembler to emit code.
707 MacroAssembler* masm() { return &masm_; }
708
709 private:
710 byte* address_; // The address of the code being patched.
711 int size_; // Number of bytes of the expected patch size.
712 MacroAssembler masm_; // Macro assembler used to generate the code.
713};
714
715
ager@chromium.org9085a012009-05-11 19:22:57 +0000716// -----------------------------------------------------------------------------
717// Static helper functions.
718
719// Generate an Operand for loading a field from an object.
720static inline Operand FieldOperand(Register object, int offset) {
721 return Operand(object, offset - kHeapObjectTag);
722}
723
724
725// Generate an Operand for loading an indexed field from an object.
726static inline Operand FieldOperand(Register object,
727 Register index,
728 ScaleFactor scale,
729 int offset) {
730 return Operand(object, index, scale, offset - kHeapObjectTag);
731}
732
733
734#ifdef GENERATED_CODE_COVERAGE
735extern void LogGeneratedCodeCoverage(const char* file_line);
736#define CODE_COVERAGE_STRINGIFY(x) #x
737#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
738#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
739#define ACCESS_MASM(masm) { \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000740 byte* x64_coverage_function = \
ager@chromium.org9085a012009-05-11 19:22:57 +0000741 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
742 masm->pushfd(); \
743 masm->pushad(); \
744 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000745 masm->call(x64_coverage_function, RelocInfo::RUNTIME_ENTRY); \
ager@chromium.org9085a012009-05-11 19:22:57 +0000746 masm->pop(rax); \
747 masm->popad(); \
748 masm->popfd(); \
749 } \
750 masm->
751#else
752#define ACCESS_MASM(masm) masm->
753#endif
754
755
756} } // namespace v8::internal
757
758#endif // V8_X64_MACRO_ASSEMBLER_X64_H_