blob: 1906644c351c6c85f74e6b45ca20d2581b542571 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
28#ifndef V8_IA32_MACRO_ASSEMBLER_IA32_H_
29#define V8_IA32_MACRO_ASSEMBLER_IA32_H_
30
31#include "assembler.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000032#include "v8globals.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34namespace v8 {
35namespace internal {
36
Kristian Monsen25f61362010-05-21 11:50:48 +010037// Flags used for the AllocateInNewSpace functions.
38enum AllocationFlags {
39 // No special flags.
40 NO_ALLOCATION_FLAGS = 0,
41 // Return the pointer to the allocated already tagged as a heap object.
42 TAG_OBJECT = 1 << 0,
43 // The content of the result register already contains the allocation top in
44 // new space.
45 RESULT_CONTAINS_TOP = 1 << 1
46};
47
Ben Murdoch257744e2011-11-30 15:57:28 +000048
Leon Clarkee46be812010-01-19 14:06:41 +000049// Convenience for platform-independent signatures. We do not normally
50// distinguish memory operands from other operands on ia32.
51typedef Operand MemOperand;
52
Steve Blocka7e24c12009-10-30 11:49:00 +000053// MacroAssembler implements a collection of frequently used macros.
54class MacroAssembler: public Assembler {
55 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +010056 // The isolate parameter can be NULL if the macro assembler should
57 // not use isolate-dependent functionality. In this case, it's the
58 // responsibility of the caller to never invoke such function on the
59 // macro assembler.
60 MacroAssembler(Isolate* isolate, void* buffer, int size);
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62 // ---------------------------------------------------------------------------
63 // GC Support
64
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010065 // For page containing |object| mark region covering |addr| dirty.
66 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010067 // space.
68 void RecordWriteHelper(Register object,
69 Register addr,
70 Register scratch);
71
72 // Check if object is in new space.
73 // scratch can be object itself, but it will be clobbered.
74 void InNewSpace(Register object,
75 Register scratch,
76 Condition cc, // equal for new space, not_equal otherwise.
Ben Murdoch257744e2011-11-30 15:57:28 +000077 Label* branch,
78 Label::Distance branch_near = Label::kFar);
Steve Block6ded16b2010-05-10 14:33:55 +010079
Steve Block8defd9f2010-07-08 12:39:36 +010080 // For page containing |object| mark region covering [object+offset]
81 // dirty. |object| is the object being stored into, |value| is the
82 // object being stored. If offset is zero, then the scratch register
83 // contains the array index into the elements array represented as a
84 // Smi. All registers are clobbered by the operation. RecordWrite
85 // filters out smis so it does not update the write barrier if the
86 // value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +000087 void RecordWrite(Register object,
88 int offset,
89 Register value,
90 Register scratch);
91
Steve Block8defd9f2010-07-08 12:39:36 +010092 // For page containing |object| mark region covering |address|
93 // dirty. |object| is the object being stored into, |value| is the
94 // object being stored. All registers are clobbered by the
95 // operation. RecordWrite filters out smis so it does not update the
96 // write barrier if the value is a smi.
97 void RecordWrite(Register object,
98 Register address,
99 Register value);
100
Steve Blocka7e24c12009-10-30 11:49:00 +0000101#ifdef ENABLE_DEBUGGER_SUPPORT
102 // ---------------------------------------------------------------------------
103 // Debugger Support
104
Andrei Popescu402d9372010-02-26 13:31:12 +0000105 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000106#endif
107
108 // ---------------------------------------------------------------------------
109 // Activation frames
110
111 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
112 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
113
114 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
115 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
116
Ben Murdochb0fe1622011-05-05 13:52:32 +0100117 // Enter specific kind of exit frame. Expects the number of
118 // arguments in register eax and sets up the number of arguments in
119 // register edi and the pointer to the first argument in register
120 // esi.
121 void EnterExitFrame(bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000122
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800123 void EnterApiExitFrame(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
125 // Leave the current exit frame. Expects the return value in
126 // register eax:edx (untouched) and the pointer to the first
127 // argument in register esi.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100128 void LeaveExitFrame(bool save_doubles);
Steve Blocka7e24c12009-10-30 11:49:00 +0000129
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800130 // Leave the current exit frame. Expects the return value in
131 // register eax (untouched).
132 void LeaveApiExitFrame();
133
Steve Blockd0582a62009-12-15 09:54:21 +0000134 // Find the function context up the context chain.
135 void LoadContext(Register dst, int context_chain_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100137 // Load the global function with the given index.
138 void LoadGlobalFunction(int index, Register function);
139
140 // Load the initial map from the global function. The registers
141 // function and map can be the same.
142 void LoadGlobalFunctionInitialMap(Register function, Register map);
143
Ben Murdochb0fe1622011-05-05 13:52:32 +0100144 // Push and pop the registers that can hold pointers.
145 void PushSafepointRegisters() { pushad(); }
146 void PopSafepointRegisters() { popad(); }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100147 // Store the value in register/immediate src in the safepoint
148 // register stack slot for register dst.
149 void StoreToSafepointRegisterSlot(Register dst, Register src);
150 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
151 void LoadFromSafepointRegisterSlot(Register dst, Register src);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 // ---------------------------------------------------------------------------
154 // JavaScript invokes
155
Ben Murdoch257744e2011-11-30 15:57:28 +0000156 // Setup call kind marking in ecx. The method takes ecx as an
157 // explicit first parameter to make the code more readable at the
158 // call sites.
159 void SetCallKind(Register dst, CallKind kind);
160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 // Invoke the JavaScript function code by either calling or jumping.
162 void InvokeCode(const Operand& code,
163 const ParameterCount& expected,
164 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000166 const CallWrapper& call_wrapper,
167 CallKind call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000168
169 void InvokeCode(Handle<Code> code,
170 const ParameterCount& expected,
171 const ParameterCount& actual,
172 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 const CallWrapper& call_wrapper,
175 CallKind call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
177 // Invoke the JavaScript function in the given register. Changes the
178 // current context to the context in the function before invoking.
179 void InvokeFunction(Register function,
180 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000182 const CallWrapper& call_wrapper,
183 CallKind call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
Andrei Popescu402d9372010-02-26 13:31:12 +0000185 void InvokeFunction(JSFunction* function,
186 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100187 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000188 const CallWrapper& call_wrapper,
189 CallKind call_kind);
Andrei Popescu402d9372010-02-26 13:31:12 +0000190
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 // Invoke specified builtin JavaScript function. Adds an entry to
192 // the unresolved list if the name does not resolve.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100193 void InvokeBuiltin(Builtins::JavaScript id,
194 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000195 const CallWrapper& call_wrapper = NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
Steve Block791712a2010-08-27 10:21:07 +0100197 // Store the function for the given builtin in the target register.
198 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
199
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 // Store the code object for the given builtin in the target register.
201 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
202
203 // Expression support
204 void Set(Register dst, const Immediate& x);
205 void Set(const Operand& dst, const Immediate& x);
206
Steve Block053d10c2011-06-13 19:13:29 +0100207 // Support for constant splitting.
208 bool IsUnsafeImmediate(const Immediate& x);
209 void SafeSet(Register dst, const Immediate& x);
210 void SafePush(const Immediate& x);
211
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000212 // Compare a register against a known root, e.g. undefined, null, true, ...
213 void CompareRoot(Register with, Heap::RootListIndex index);
214
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 // Compare object type for heap object.
216 // Incoming register is heap_object and outgoing register is map.
217 void CmpObjectType(Register heap_object, InstanceType type, Register map);
218
219 // Compare instance type for map.
220 void CmpInstanceType(Register map, InstanceType type);
221
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222 // Check if a map for a JSObject indicates that the object has fast elements.
223 // Jump to the specified label if it does not.
224 void CheckFastElements(Register map,
225 Label* fail,
226 Label::Distance distance = Label::kFar);
227
Ben Murdoch257744e2011-11-30 15:57:28 +0000228 // Check if the map of an object is equal to a specified map and branch to
229 // label if not. Skip the smi check if not required (object is known to be a
230 // heap object)
Andrei Popescu31002712010-02-23 13:46:05 +0000231 void CheckMap(Register obj,
232 Handle<Map> map,
233 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +0000234 SmiCheckType smi_check_type);
235
236 // Check if the map of an object is equal to a specified map and branch to a
237 // specified target if equal. Skip the smi check if not required (object is
238 // known to be a heap object)
239 void DispatchMap(Register obj,
240 Handle<Map> map,
241 Handle<Code> success,
242 SmiCheckType smi_check_type);
Andrei Popescu31002712010-02-23 13:46:05 +0000243
Leon Clarkee46be812010-01-19 14:06:41 +0000244 // Check if the object in register heap_object is a string. Afterwards the
245 // register map contains the object map and the register instance_type
246 // contains the instance_type. The registers map and instance_type can be the
247 // same in which case it contains the instance type afterwards. Either of the
248 // registers map and instance_type can be the same as heap_object.
249 Condition IsObjectStringType(Register heap_object,
250 Register map,
251 Register instance_type);
252
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100253 // Check if a heap object's type is in the JSObject range, not including
254 // JSFunction. The object's map will be loaded in the map register.
255 // Any or all of the three registers may be the same.
256 // The contents of the scratch register will always be overwritten.
257 void IsObjectJSObjectType(Register heap_object,
258 Register map,
259 Register scratch,
260 Label* fail);
261
262 // The contents of the scratch register will be overwritten.
263 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
264
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 // FCmp is similar to integer cmp, but requires unsigned
266 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
267 void FCmp();
268
Ben Murdoch257744e2011-11-30 15:57:28 +0000269 void ClampUint8(Register reg);
270
271 void ClampDoubleToUint8(XMMRegister input_reg,
272 XMMRegister scratch_reg,
273 Register result_reg);
274
275
Leon Clarkee46be812010-01-19 14:06:41 +0000276 // Smi tagging support.
277 void SmiTag(Register reg) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000278 STATIC_ASSERT(kSmiTag == 0);
279 STATIC_ASSERT(kSmiTagSize == 1);
Steve Block6ded16b2010-05-10 14:33:55 +0100280 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000281 }
282 void SmiUntag(Register reg) {
283 sar(reg, kSmiTagSize);
284 }
285
Iain Merrick75681382010-08-19 15:07:18 +0100286 // Modifies the register even if it does not contain a Smi!
Iain Merrick75681382010-08-19 15:07:18 +0100287 void SmiUntag(Register reg, Label* is_smi) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000288 STATIC_ASSERT(kSmiTagSize == 1);
Iain Merrick75681382010-08-19 15:07:18 +0100289 sar(reg, kSmiTagSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000290 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +0100291 j(not_carry, is_smi);
292 }
293
Steve Block1e0659c2011-05-24 12:43:12 +0100294 // Jump the register contains a smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000295 inline void JumpIfSmi(Register value,
296 Label* smi_label,
297 Label::Distance distance = Label::kFar) {
Steve Block1e0659c2011-05-24 12:43:12 +0100298 test(value, Immediate(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000299 j(zero, smi_label, distance);
300 }
301 // Jump if the operand is a smi.
302 inline void JumpIfSmi(Operand value,
303 Label* smi_label,
304 Label::Distance distance = Label::kFar) {
305 test(value, Immediate(kSmiTagMask));
306 j(zero, smi_label, distance);
Steve Block1e0659c2011-05-24 12:43:12 +0100307 }
308 // Jump if register contain a non-smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000309 inline void JumpIfNotSmi(Register value,
310 Label* not_smi_label,
311 Label::Distance distance = Label::kFar) {
Steve Block1e0659c2011-05-24 12:43:12 +0100312 test(value, Immediate(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000313 j(not_zero, not_smi_label, distance);
Steve Block1e0659c2011-05-24 12:43:12 +0100314 }
315
Ben Murdoch257744e2011-11-30 15:57:28 +0000316 void LoadInstanceDescriptors(Register map, Register descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100317
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100318 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
319
Andrei Popescu402d9372010-02-26 13:31:12 +0000320 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100321 void AbortIfNotNumber(Register object);
322
323 // Abort execution if argument is not a smi. Used in debug code.
324 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000325
Iain Merrick75681382010-08-19 15:07:18 +0100326 // Abort execution if argument is a smi. Used in debug code.
327 void AbortIfSmi(Register object);
328
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100329 // Abort execution if argument is a string. Used in debug code.
330 void AbortIfNotString(Register object);
331
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 // ---------------------------------------------------------------------------
333 // Exception handling
334
335 // Push a new try handler and link into try handler chain. The return
336 // address must be pushed before calling this helper.
337 void PushTryHandler(CodeLocation try_location, HandlerType type);
338
Leon Clarkee46be812010-01-19 14:06:41 +0000339 // Unlink the stack handler on top of the stack from the try handler chain.
340 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000341
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100342 // Activate the top handler in the try hander chain.
343 void Throw(Register value);
344
345 void ThrowUncatchable(UncatchableExceptionType type, Register value);
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 // ---------------------------------------------------------------------------
348 // Inline caching support
349
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 // Generate code for checking access rights - used for security checks
351 // on access to global objects across environments. The holder register
352 // is left untouched, but the scratch register is clobbered.
353 void CheckAccessGlobalProxy(Register holder_reg,
354 Register scratch,
355 Label* miss);
356
357
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000358 void LoadFromNumberDictionary(Label* miss,
359 Register elements,
360 Register key,
361 Register r0,
362 Register r1,
363 Register r2,
364 Register result);
365
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // ---------------------------------------------------------------------------
368 // Allocation support
369
370 // Allocate an object in new space. If the new space is exhausted control
371 // continues at the gc_required label. The allocated object is returned in
372 // result and end of the new object is returned in result_end. The register
373 // scratch can be passed as no_reg in which case an additional object
374 // reference will be added to the reloc info. The returned pointers in result
375 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000376 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000377 // the allocation top on entry (could be result_end from a previous call to
378 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
379 // should be no_reg as it is never used.
380 void AllocateInNewSpace(int object_size,
381 Register result,
382 Register result_end,
383 Register scratch,
384 Label* gc_required,
385 AllocationFlags flags);
386
387 void AllocateInNewSpace(int header_size,
388 ScaleFactor element_size,
389 Register element_count,
390 Register result,
391 Register result_end,
392 Register scratch,
393 Label* gc_required,
394 AllocationFlags flags);
395
396 void AllocateInNewSpace(Register object_size,
397 Register result,
398 Register result_end,
399 Register scratch,
400 Label* gc_required,
401 AllocationFlags flags);
402
403 // Undo allocation in new space. The object passed and objects allocated after
404 // it will no longer be allocated. Make sure that no pointers are left to the
405 // object(s) no longer allocated as they would be invalid when allocation is
406 // un-done.
407 void UndoAllocationInNewSpace(Register object);
408
Steve Block3ce2e202009-11-05 08:53:23 +0000409 // Allocate a heap number in new space with undefined value. The
410 // register scratch2 can be passed as no_reg; the others must be
411 // valid registers. Returns tagged pointer in result register, or
412 // jumps to gc_required if new space is full.
413 void AllocateHeapNumber(Register result,
414 Register scratch1,
415 Register scratch2,
416 Label* gc_required);
417
Steve Blockd0582a62009-12-15 09:54:21 +0000418 // Allocate a sequential string. All the header fields of the string object
419 // are initialized.
420 void AllocateTwoByteString(Register result,
421 Register length,
422 Register scratch1,
423 Register scratch2,
424 Register scratch3,
425 Label* gc_required);
426 void AllocateAsciiString(Register result,
427 Register length,
428 Register scratch1,
429 Register scratch2,
430 Register scratch3,
431 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100432 void AllocateAsciiString(Register result,
433 int length,
434 Register scratch1,
435 Register scratch2,
436 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000437
438 // Allocate a raw cons string object. Only the map field of the result is
439 // initialized.
Ben Murdoch589d6972011-11-30 16:04:58 +0000440 void AllocateTwoByteConsString(Register result,
Steve Blockd0582a62009-12-15 09:54:21 +0000441 Register scratch1,
442 Register scratch2,
443 Label* gc_required);
444 void AllocateAsciiConsString(Register result,
445 Register scratch1,
446 Register scratch2,
447 Label* gc_required);
448
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000449 // Allocate a raw sliced string object. Only the map field of the result is
450 // initialized.
Ben Murdoch589d6972011-11-30 16:04:58 +0000451 void AllocateTwoByteSlicedString(Register result,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000452 Register scratch1,
453 Register scratch2,
454 Label* gc_required);
455 void AllocateAsciiSlicedString(Register result,
456 Register scratch1,
457 Register scratch2,
458 Label* gc_required);
459
Ben Murdochb8e0da22011-05-16 14:20:40 +0100460 // Copy memory, byte-by-byte, from source to destination. Not optimized for
461 // long or aligned copies.
462 // The contents of index and scratch are destroyed.
463 void CopyBytes(Register source,
464 Register destination,
465 Register length,
466 Register scratch);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800467
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 // ---------------------------------------------------------------------------
469 // Support functions.
470
471 // Check if result is zero and op is negative.
472 void NegativeZeroTest(Register result, Register op, Label* then_label);
473
Steve Blocka7e24c12009-10-30 11:49:00 +0000474 // Check if result is zero and any of op1 and op2 are negative.
475 // Register scratch is destroyed, and it must be different from op2.
476 void NegativeZeroTest(Register result, Register op1, Register op2,
477 Register scratch, Label* then_label);
478
479 // Try to get function prototype of a function and puts the value in
480 // the result register. Checks that the function really is a
481 // function and jumps to the miss label if the fast checks fail. The
482 // function register will be untouched; the other registers may be
483 // clobbered.
484 void TryGetFunctionPrototype(Register function,
485 Register result,
486 Register scratch,
487 Label* miss);
488
489 // Generates code for reporting that an illegal operation has
490 // occurred.
491 void IllegalOperation(int num_arguments);
492
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100493 // Picks out an array index from the hash field.
494 // Register use:
495 // hash - holds the index's hash. Clobbered.
496 // index - holds the overwritten index on exit.
497 void IndexFromHash(Register hash, Register index);
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 // ---------------------------------------------------------------------------
500 // Runtime calls
501
Leon Clarkee46be812010-01-19 14:06:41 +0000502 // Call a code stub. Generate the code if necessary.
Ben Murdoch257744e2011-11-30 15:57:28 +0000503 void CallStub(CodeStub* stub, unsigned ast_id = kNoASTId);
Steve Blocka7e24c12009-10-30 11:49:00 +0000504
Leon Clarkee46be812010-01-19 14:06:41 +0000505 // Call a code stub and return the code object called. Try to generate
506 // the code if necessary. Do not perform a GC but instead return a retry
507 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700508 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000509
510 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000511 void TailCallStub(CodeStub* stub);
512
Leon Clarkee46be812010-01-19 14:06:41 +0000513 // Tail call a code stub (jump) and return the code object called. Try to
514 // generate the code if necessary. Do not perform a GC but instead return
515 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700516 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000517
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 // Return from a code stub after popping its arguments.
519 void StubReturn(int argc);
520
521 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100522 void CallRuntime(const Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100523 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000524
Leon Clarke4515c472010-02-03 11:58:03 +0000525 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000526 // Try to generate the stub code if necessary. Do not perform a GC
527 // but instead return a retry after GC failure.
Steve Block44f0eee2011-05-26 01:26:41 +0100528 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -0700529 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000530
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 // Convenience function: Same as above, but takes the fid instead.
532 void CallRuntime(Runtime::FunctionId id, int num_arguments);
533
Leon Clarkee46be812010-01-19 14:06:41 +0000534 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700535 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
536 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000537
Ben Murdochbb769b22010-08-11 14:56:33 +0100538 // Convenience function: call an external reference.
539 void CallExternalReference(ExternalReference ref, int num_arguments);
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100542 // Like JumpToExternalReference, but also takes care of passing the number
543 // of parameters.
544 void TailCallExternalReference(const ExternalReference& ext,
545 int num_arguments,
546 int result_size);
547
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800548 // Tail call of a runtime routine (jump). Try to generate the code if
549 // necessary. Do not perform a GC but instead return a retry after GC failure.
550 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
551 const ExternalReference& ext, int num_arguments, int result_size);
552
Steve Block6ded16b2010-05-10 14:33:55 +0100553 // Convenience function: tail call a runtime routine (jump).
554 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000555 int num_arguments,
556 int result_size);
557
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800558 // Convenience function: tail call a runtime routine (jump). Try to generate
559 // the code if necessary. Do not perform a GC but instead return a retry after
560 // GC failure.
561 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
562 int num_arguments,
563 int result_size);
564
Steve Block6ded16b2010-05-10 14:33:55 +0100565 // Before calling a C-function from generated code, align arguments on stack.
566 // After aligning the frame, arguments must be stored in esp[0], esp[4],
567 // etc., not pushed. The argument count assumes all arguments are word sized.
568 // Some compilers/platforms require the stack to be aligned when calling
569 // C++ code.
570 // Needs a scratch register to do some arithmetic. This register will be
571 // trashed.
572 void PrepareCallCFunction(int num_arguments, Register scratch);
573
574 // Calls a C function and cleans up the space for arguments allocated
575 // by PrepareCallCFunction. The called function is not allowed to trigger a
576 // garbage collection, since that might move the code and invalidate the
577 // return address (unless this is somehow accounted for by the called
578 // function).
579 void CallCFunction(ExternalReference function, int num_arguments);
580 void CallCFunction(Register function, int num_arguments);
581
John Reck59135872010-11-02 12:39:01 -0700582 // Prepares stack to put arguments (aligns and so on). Reserves
583 // space for return value if needed (assumes the return value is a handle).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000584 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
585 // etc. Saves context (esi). If space was reserved for return value then
586 // stores the pointer to the reserved slot into esi.
587 void PrepareCallApiFunction(int argc);
Steve Blockd0582a62009-12-15 09:54:21 +0000588
Russell Brenner90bac252010-11-18 13:33:46 -0800589 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700590 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800591 // Clobbers ebx, edi and caller-save registers. Restores context.
592 // On return removes stack_space * kPointerSize (GCed).
593 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
594 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000595
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100597 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000598
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800599 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
600
Steve Blocka7e24c12009-10-30 11:49:00 +0000601
602 // ---------------------------------------------------------------------------
603 // Utilities
604
605 void Ret();
606
Steve Block1e0659c2011-05-24 12:43:12 +0100607 // Return and drop arguments from stack, where the number of arguments
608 // may be bigger than 2^16 - 1. Requires a scratch register.
609 void Ret(int bytes_dropped, Register scratch);
610
Leon Clarkee46be812010-01-19 14:06:41 +0000611 // Emit code to discard a non-negative number of pointer-sized elements
612 // from the stack, clobbering only the esp register.
613 void Drop(int element_count);
614
615 void Call(Label* target) { call(target); }
616
Ben Murdochb0fe1622011-05-05 13:52:32 +0100617 // Emit call to the code we are currently generating.
618 void CallSelf() {
619 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
620 call(self, RelocInfo::CODE_TARGET);
621 }
622
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100623 // Move if the registers are not identical.
624 void Move(Register target, Register source);
625
Leon Clarkee46be812010-01-19 14:06:41 +0000626 void Move(Register target, Handle<Object> value);
627
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000628 // Push a handle value.
629 void Push(Handle<Object> handle) { push(handle); }
630
Ben Murdoch8b112d22011-06-08 16:22:53 +0100631 Handle<Object> CodeObject() {
632 ASSERT(!code_object_.is_null());
633 return code_object_;
634 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000635
636
637 // ---------------------------------------------------------------------------
638 // StatsCounter support
639
640 void SetCounter(StatsCounter* counter, int value);
641 void IncrementCounter(StatsCounter* counter, int value);
642 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000643 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
644 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000645
646
647 // ---------------------------------------------------------------------------
648 // Debugging
649
650 // Calls Abort(msg) if the condition cc is not satisfied.
651 // Use --debug_code to enable.
652 void Assert(Condition cc, const char* msg);
653
Iain Merrick75681382010-08-19 15:07:18 +0100654 void AssertFastElements(Register elements);
655
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 // Like Assert(), but always enabled.
657 void Check(Condition cc, const char* msg);
658
659 // Print a message to stdout and abort execution.
660 void Abort(const char* msg);
661
Steve Block6ded16b2010-05-10 14:33:55 +0100662 // Check that the stack is aligned.
663 void CheckStackAlignment();
664
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 // Verify restrictions about code generated in stubs.
666 void set_generating_stub(bool value) { generating_stub_ = value; }
667 bool generating_stub() { return generating_stub_; }
668 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
669 bool allow_stub_calls() { return allow_stub_calls_; }
670
Leon Clarked91b9f72010-01-27 17:25:45 +0000671 // ---------------------------------------------------------------------------
672 // String utilities.
673
Andrei Popescu402d9372010-02-26 13:31:12 +0000674 // Check whether the instance type represents a flat ascii string. Jump to the
675 // label if not. If the instance type can be scratched specify same register
676 // for both instance type and scratch.
677 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
678 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100679 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000680
Leon Clarked91b9f72010-01-27 17:25:45 +0000681 // Checks if both objects are sequential ASCII strings, and jumps to label
682 // if either is not.
683 void JumpIfNotBothSequentialAsciiStrings(Register object1,
684 Register object2,
685 Register scratch1,
686 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100687 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000688
Ben Murdoch8b112d22011-06-08 16:22:53 +0100689 static int SafepointRegisterStackIndex(Register reg) {
690 return SafepointRegisterStackIndex(reg.code());
691 }
692
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 bool generating_stub_;
695 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000696 // This handle will be patched with the code object on installation.
697 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000698
699 // Helper functions for generating invokes.
700 void InvokePrologue(const ParameterCount& expected,
701 const ParameterCount& actual,
702 Handle<Code> code_constant,
703 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +0000704 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100705 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000706 Label::Distance done_near = Label::kFar,
707 const CallWrapper& call_wrapper = NullCallWrapper(),
708 CallKind call_kind = CALL_AS_METHOD);
Steve Blocka7e24c12009-10-30 11:49:00 +0000709
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 // Activation support.
711 void EnterFrame(StackFrame::Type type);
712 void LeaveFrame(StackFrame::Type type);
713
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100714 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100715 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000716
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800717 void LeaveExitFrameEpilogue();
718
Steve Blocka7e24c12009-10-30 11:49:00 +0000719 // Allocation support helpers.
720 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000721 Register scratch,
722 AllocationFlags flags);
723 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000724
725 // Helper for PopHandleScope. Allowed to perform a GC and returns
726 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
727 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700728 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
729 Register scratch,
730 bool gc_allowed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100731
732
733 // Compute memory operands for safepoint stack slots.
734 Operand SafepointRegisterSlot(Register reg);
735 static int SafepointRegisterStackIndex(int reg_code);
736
737 // Needs access to SafepointRegisterStackIndex for optimized frame
738 // traversal.
739 friend class OptimizedFrame;
Steve Blocka7e24c12009-10-30 11:49:00 +0000740};
741
742
743// The code patcher is used to patch (typically) small parts of code e.g. for
744// debugging and other types of instrumentation. When using the code patcher
745// the exact number of bytes specified must be emitted. Is not legal to emit
746// relocation information. If any of these constraints are violated it causes
747// an assertion.
748class CodePatcher {
749 public:
750 CodePatcher(byte* address, int size);
751 virtual ~CodePatcher();
752
753 // Macro assembler to emit code.
754 MacroAssembler* masm() { return &masm_; }
755
756 private:
757 byte* address_; // The address of the code being patched.
758 int size_; // Number of bytes of the expected patch size.
759 MacroAssembler masm_; // Macro assembler used to generate the code.
760};
761
762
763// -----------------------------------------------------------------------------
764// Static helper functions.
765
766// Generate an Operand for loading a field from an object.
767static inline Operand FieldOperand(Register object, int offset) {
768 return Operand(object, offset - kHeapObjectTag);
769}
770
771
772// Generate an Operand for loading an indexed field from an object.
773static inline Operand FieldOperand(Register object,
774 Register index,
775 ScaleFactor scale,
776 int offset) {
777 return Operand(object, index, scale, offset - kHeapObjectTag);
778}
779
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800780
781static inline Operand ContextOperand(Register context, int index) {
782 return Operand(context, Context::SlotOffset(index));
783}
784
785
786static inline Operand GlobalObjectOperand() {
787 return ContextOperand(esi, Context::GLOBAL_INDEX);
788}
789
790
John Reck59135872010-11-02 12:39:01 -0700791// Generates an Operand for saving parameters after PrepareCallApiFunction.
792Operand ApiParameterOperand(int index);
793
Steve Blocka7e24c12009-10-30 11:49:00 +0000794
795#ifdef GENERATED_CODE_COVERAGE
796extern void LogGeneratedCodeCoverage(const char* file_line);
797#define CODE_COVERAGE_STRINGIFY(x) #x
798#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
799#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
800#define ACCESS_MASM(masm) { \
801 byte* ia32_coverage_function = \
802 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
803 masm->pushfd(); \
804 masm->pushad(); \
805 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
806 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
807 masm->pop(eax); \
808 masm->popad(); \
809 masm->popfd(); \
810 } \
811 masm->
812#else
813#define ACCESS_MASM(masm) masm->
814#endif
815
816
817} } // namespace v8::internal
818
819#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_