blob: b986264513040582c730520e27ae4cb4386b162b [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"
Iain Merrick75681382010-08-19 15:07:18 +010032#include "type-info.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
Leon Clarkee46be812010-01-19 14:06:41 +000048// Convenience for platform-independent signatures. We do not normally
49// distinguish memory operands from other operands on ia32.
50typedef Operand MemOperand;
51
Steve Blocka7e24c12009-10-30 11:49:00 +000052// Forward declaration.
Ben Murdochb0fe1622011-05-05 13:52:32 +010053class PostCallGenerator;
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Steve Blocka7e24c12009-10-30 11:49:00 +000055// MacroAssembler implements a collection of frequently used macros.
56class MacroAssembler: public Assembler {
57 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +010058 // The isolate parameter can be NULL if the macro assembler should
59 // not use isolate-dependent functionality. In this case, it's the
60 // responsibility of the caller to never invoke such function on the
61 // macro assembler.
62 MacroAssembler(Isolate* isolate, void* buffer, int size);
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64 // ---------------------------------------------------------------------------
65 // GC Support
66
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010067 // For page containing |object| mark region covering |addr| dirty.
68 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010069 // space.
70 void RecordWriteHelper(Register object,
71 Register addr,
72 Register scratch);
73
74 // Check if object is in new space.
75 // scratch can be object itself, but it will be clobbered.
Ben Murdochb0fe1622011-05-05 13:52:32 +010076 template <typename LabelType>
Steve Block6ded16b2010-05-10 14:33:55 +010077 void InNewSpace(Register object,
78 Register scratch,
79 Condition cc, // equal for new space, not_equal otherwise.
Ben Murdochb0fe1622011-05-05 13:52:32 +010080 LabelType* branch);
Steve Block6ded16b2010-05-10 14:33:55 +010081
Steve Block8defd9f2010-07-08 12:39:36 +010082 // For page containing |object| mark region covering [object+offset]
83 // dirty. |object| is the object being stored into, |value| is the
84 // object being stored. If offset is zero, then the scratch register
85 // contains the array index into the elements array represented as a
86 // Smi. All registers are clobbered by the operation. RecordWrite
87 // filters out smis so it does not update the write barrier if the
88 // value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +000089 void RecordWrite(Register object,
90 int offset,
91 Register value,
92 Register scratch);
93
Steve Block8defd9f2010-07-08 12:39:36 +010094 // For page containing |object| mark region covering |address|
95 // dirty. |object| is the object being stored into, |value| is the
96 // object being stored. All registers are clobbered by the
97 // operation. RecordWrite filters out smis so it does not update the
98 // write barrier if the value is a smi.
99 void RecordWrite(Register object,
100 Register address,
101 Register value);
102
Steve Blocka7e24c12009-10-30 11:49:00 +0000103#ifdef ENABLE_DEBUGGER_SUPPORT
104 // ---------------------------------------------------------------------------
105 // Debugger Support
106
Andrei Popescu402d9372010-02-26 13:31:12 +0000107 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000108#endif
109
110 // ---------------------------------------------------------------------------
111 // Activation frames
112
113 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
114 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
115
116 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
117 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
118
Ben Murdochb0fe1622011-05-05 13:52:32 +0100119 // Enter specific kind of exit frame. Expects the number of
120 // arguments in register eax and sets up the number of arguments in
121 // register edi and the pointer to the first argument in register
122 // esi.
123 void EnterExitFrame(bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000124
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800125 void EnterApiExitFrame(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000126
127 // Leave the current exit frame. Expects the return value in
128 // register eax:edx (untouched) and the pointer to the first
129 // argument in register esi.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100130 void LeaveExitFrame(bool save_doubles);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800132 // Leave the current exit frame. Expects the return value in
133 // register eax (untouched).
134 void LeaveApiExitFrame();
135
Steve Blockd0582a62009-12-15 09:54:21 +0000136 // Find the function context up the context chain.
137 void LoadContext(Register dst, int context_chain_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000138
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100139 // Load the global function with the given index.
140 void LoadGlobalFunction(int index, Register function);
141
142 // Load the initial map from the global function. The registers
143 // function and map can be the same.
144 void LoadGlobalFunctionInitialMap(Register function, Register map);
145
Ben Murdochb0fe1622011-05-05 13:52:32 +0100146 // Push and pop the registers that can hold pointers.
147 void PushSafepointRegisters() { pushad(); }
148 void PopSafepointRegisters() { popad(); }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100149 // Store the value in register/immediate src in the safepoint
150 // register stack slot for register dst.
151 void StoreToSafepointRegisterSlot(Register dst, Register src);
152 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
153 void LoadFromSafepointRegisterSlot(Register dst, Register src);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 // ---------------------------------------------------------------------------
156 // JavaScript invokes
157
158 // Invoke the JavaScript function code by either calling or jumping.
159 void InvokeCode(const Operand& code,
160 const ParameterCount& expected,
161 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100162 InvokeFlag flag,
163 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
165 void InvokeCode(Handle<Code> code,
166 const ParameterCount& expected,
167 const ParameterCount& actual,
168 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100169 InvokeFlag flag,
170 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000171
172 // Invoke the JavaScript function in the given register. Changes the
173 // current context to the context in the function before invoking.
174 void InvokeFunction(Register function,
175 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100176 InvokeFlag flag,
177 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000178
Andrei Popescu402d9372010-02-26 13:31:12 +0000179 void InvokeFunction(JSFunction* function,
180 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181 InvokeFlag flag,
182 PostCallGenerator* post_call_generator = NULL);
Andrei Popescu402d9372010-02-26 13:31:12 +0000183
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 // Invoke specified builtin JavaScript function. Adds an entry to
185 // the unresolved list if the name does not resolve.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100186 void InvokeBuiltin(Builtins::JavaScript id,
187 InvokeFlag flag,
188 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000189
Steve Block791712a2010-08-27 10:21:07 +0100190 // Store the function for the given builtin in the target register.
191 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
192
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 // Store the code object for the given builtin in the target register.
194 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
195
196 // Expression support
197 void Set(Register dst, const Immediate& x);
198 void Set(const Operand& dst, const Immediate& x);
199
Steve Block053d10c2011-06-13 19:13:29 +0100200 // Support for constant splitting.
201 bool IsUnsafeImmediate(const Immediate& x);
202 void SafeSet(Register dst, const Immediate& x);
203 void SafePush(const Immediate& x);
204
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 // Compare object type for heap object.
206 // Incoming register is heap_object and outgoing register is map.
207 void CmpObjectType(Register heap_object, InstanceType type, Register map);
208
209 // Compare instance type for map.
210 void CmpInstanceType(Register map, InstanceType type);
211
Andrei Popescu31002712010-02-23 13:46:05 +0000212 // Check if the map of an object is equal to a specified map and
213 // branch to label if not. Skip the smi check if not required
214 // (object is known to be a heap object)
215 void CheckMap(Register obj,
216 Handle<Map> map,
217 Label* fail,
218 bool is_heap_object);
219
Leon Clarkee46be812010-01-19 14:06:41 +0000220 // Check if the object in register heap_object is a string. Afterwards the
221 // register map contains the object map and the register instance_type
222 // contains the instance_type. The registers map and instance_type can be the
223 // same in which case it contains the instance type afterwards. Either of the
224 // registers map and instance_type can be the same as heap_object.
225 Condition IsObjectStringType(Register heap_object,
226 Register map,
227 Register instance_type);
228
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100229 // Check if a heap object's type is in the JSObject range, not including
230 // JSFunction. The object's map will be loaded in the map register.
231 // Any or all of the three registers may be the same.
232 // The contents of the scratch register will always be overwritten.
233 void IsObjectJSObjectType(Register heap_object,
234 Register map,
235 Register scratch,
236 Label* fail);
237
238 // The contents of the scratch register will be overwritten.
239 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
240
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 // FCmp is similar to integer cmp, but requires unsigned
242 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
243 void FCmp();
244
Leon Clarkee46be812010-01-19 14:06:41 +0000245 // Smi tagging support.
246 void SmiTag(Register reg) {
247 ASSERT(kSmiTag == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100248 ASSERT(kSmiTagSize == 1);
249 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000250 }
251 void SmiUntag(Register reg) {
252 sar(reg, kSmiTagSize);
253 }
254
Iain Merrick75681382010-08-19 15:07:18 +0100255 // Modifies the register even if it does not contain a Smi!
256 void SmiUntag(Register reg, TypeInfo info, Label* non_smi) {
257 ASSERT(kSmiTagSize == 1);
258 sar(reg, kSmiTagSize);
259 if (info.IsSmi()) {
260 ASSERT(kSmiTag == 0);
261 j(carry, non_smi);
262 }
263 }
264
265 // Modifies the register even if it does not contain a Smi!
266 void SmiUntag(Register reg, Label* is_smi) {
267 ASSERT(kSmiTagSize == 1);
268 sar(reg, kSmiTagSize);
269 ASSERT(kSmiTag == 0);
270 j(not_carry, is_smi);
271 }
272
Steve Block1e0659c2011-05-24 12:43:12 +0100273 // Jump the register contains a smi.
274 inline void JumpIfSmi(Register value, Label* smi_label) {
275 test(value, Immediate(kSmiTagMask));
276 j(zero, smi_label, not_taken);
277 }
278 // Jump if register contain a non-smi.
279 inline void JumpIfNotSmi(Register value, Label* not_smi_label) {
280 test(value, Immediate(kSmiTagMask));
281 j(not_zero, not_smi_label, not_taken);
282 }
283
Iain Merrick75681382010-08-19 15:07:18 +0100284 // Assumes input is a heap object.
285 void JumpIfNotNumber(Register reg, TypeInfo info, Label* on_not_number);
286
287 // Assumes input is a heap number. Jumps on things out of range. Also jumps
288 // on the min negative int32. Ignores frational parts.
289 void ConvertToInt32(Register dst,
290 Register src, // Can be the same as dst.
291 Register scratch, // Can be no_reg or dst, but not src.
292 TypeInfo info,
293 Label* on_not_int32);
294
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100295 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
296
Andrei Popescu402d9372010-02-26 13:31:12 +0000297 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100298 void AbortIfNotNumber(Register object);
299
300 // Abort execution if argument is not a smi. Used in debug code.
301 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000302
Iain Merrick75681382010-08-19 15:07:18 +0100303 // Abort execution if argument is a smi. Used in debug code.
304 void AbortIfSmi(Register object);
305
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100306 // Abort execution if argument is a string. Used in debug code.
307 void AbortIfNotString(Register object);
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 // ---------------------------------------------------------------------------
310 // Exception handling
311
312 // Push a new try handler and link into try handler chain. The return
313 // address must be pushed before calling this helper.
314 void PushTryHandler(CodeLocation try_location, HandlerType type);
315
Leon Clarkee46be812010-01-19 14:06:41 +0000316 // Unlink the stack handler on top of the stack from the try handler chain.
317 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000318
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100319 // Activate the top handler in the try hander chain.
320 void Throw(Register value);
321
322 void ThrowUncatchable(UncatchableExceptionType type, Register value);
323
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 // ---------------------------------------------------------------------------
325 // Inline caching support
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 // Generate code for checking access rights - used for security checks
328 // on access to global objects across environments. The holder register
329 // is left untouched, but the scratch register is clobbered.
330 void CheckAccessGlobalProxy(Register holder_reg,
331 Register scratch,
332 Label* miss);
333
334
335 // ---------------------------------------------------------------------------
336 // Allocation support
337
338 // Allocate an object in new space. If the new space is exhausted control
339 // continues at the gc_required label. The allocated object is returned in
340 // result and end of the new object is returned in result_end. The register
341 // scratch can be passed as no_reg in which case an additional object
342 // reference will be added to the reloc info. The returned pointers in result
343 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000344 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 // the allocation top on entry (could be result_end from a previous call to
346 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
347 // should be no_reg as it is never used.
348 void AllocateInNewSpace(int object_size,
349 Register result,
350 Register result_end,
351 Register scratch,
352 Label* gc_required,
353 AllocationFlags flags);
354
355 void AllocateInNewSpace(int header_size,
356 ScaleFactor element_size,
357 Register element_count,
358 Register result,
359 Register result_end,
360 Register scratch,
361 Label* gc_required,
362 AllocationFlags flags);
363
364 void AllocateInNewSpace(Register object_size,
365 Register result,
366 Register result_end,
367 Register scratch,
368 Label* gc_required,
369 AllocationFlags flags);
370
371 // Undo allocation in new space. The object passed and objects allocated after
372 // it will no longer be allocated. Make sure that no pointers are left to the
373 // object(s) no longer allocated as they would be invalid when allocation is
374 // un-done.
375 void UndoAllocationInNewSpace(Register object);
376
Steve Block3ce2e202009-11-05 08:53:23 +0000377 // Allocate a heap number in new space with undefined value. The
378 // register scratch2 can be passed as no_reg; the others must be
379 // valid registers. Returns tagged pointer in result register, or
380 // jumps to gc_required if new space is full.
381 void AllocateHeapNumber(Register result,
382 Register scratch1,
383 Register scratch2,
384 Label* gc_required);
385
Steve Blockd0582a62009-12-15 09:54:21 +0000386 // Allocate a sequential string. All the header fields of the string object
387 // are initialized.
388 void AllocateTwoByteString(Register result,
389 Register length,
390 Register scratch1,
391 Register scratch2,
392 Register scratch3,
393 Label* gc_required);
394 void AllocateAsciiString(Register result,
395 Register length,
396 Register scratch1,
397 Register scratch2,
398 Register scratch3,
399 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100400 void AllocateAsciiString(Register result,
401 int length,
402 Register scratch1,
403 Register scratch2,
404 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000405
406 // Allocate a raw cons string object. Only the map field of the result is
407 // initialized.
408 void AllocateConsString(Register result,
409 Register scratch1,
410 Register scratch2,
411 Label* gc_required);
412 void AllocateAsciiConsString(Register result,
413 Register scratch1,
414 Register scratch2,
415 Label* gc_required);
416
Ben Murdochb8e0da22011-05-16 14:20:40 +0100417 // Copy memory, byte-by-byte, from source to destination. Not optimized for
418 // long or aligned copies.
419 // The contents of index and scratch are destroyed.
420 void CopyBytes(Register source,
421 Register destination,
422 Register length,
423 Register scratch);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800424
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 // ---------------------------------------------------------------------------
426 // Support functions.
427
428 // Check if result is zero and op is negative.
429 void NegativeZeroTest(Register result, Register op, Label* then_label);
430
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 // Check if result is zero and any of op1 and op2 are negative.
432 // Register scratch is destroyed, and it must be different from op2.
433 void NegativeZeroTest(Register result, Register op1, Register op2,
434 Register scratch, Label* then_label);
435
436 // Try to get function prototype of a function and puts the value in
437 // the result register. Checks that the function really is a
438 // function and jumps to the miss label if the fast checks fail. The
439 // function register will be untouched; the other registers may be
440 // clobbered.
441 void TryGetFunctionPrototype(Register function,
442 Register result,
443 Register scratch,
444 Label* miss);
445
446 // Generates code for reporting that an illegal operation has
447 // occurred.
448 void IllegalOperation(int num_arguments);
449
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100450 // Picks out an array index from the hash field.
451 // Register use:
452 // hash - holds the index's hash. Clobbered.
453 // index - holds the overwritten index on exit.
454 void IndexFromHash(Register hash, Register index);
455
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 // ---------------------------------------------------------------------------
457 // Runtime calls
458
Leon Clarkee46be812010-01-19 14:06:41 +0000459 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 void CallStub(CodeStub* stub);
461
Leon Clarkee46be812010-01-19 14:06:41 +0000462 // Call a code stub and return the code object called. Try to generate
463 // the code if necessary. Do not perform a GC but instead return a retry
464 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700465 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000466
467 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000468 void TailCallStub(CodeStub* stub);
469
Leon Clarkee46be812010-01-19 14:06:41 +0000470 // Tail call a code stub (jump) and return the code object called. Try to
471 // generate the code if necessary. Do not perform a GC but instead return
472 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700473 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 // Return from a code stub after popping its arguments.
476 void StubReturn(int argc);
477
478 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100479 void CallRuntime(const Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100480 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000481
Leon Clarke4515c472010-02-03 11:58:03 +0000482 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000483 // Try to generate the stub code if necessary. Do not perform a GC
484 // but instead return a retry after GC failure.
Steve Block44f0eee2011-05-26 01:26:41 +0100485 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -0700486 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000487
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 // Convenience function: Same as above, but takes the fid instead.
489 void CallRuntime(Runtime::FunctionId id, int num_arguments);
490
Leon Clarkee46be812010-01-19 14:06:41 +0000491 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700492 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
493 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000494
Ben Murdochbb769b22010-08-11 14:56:33 +0100495 // Convenience function: call an external reference.
496 void CallExternalReference(ExternalReference ref, int num_arguments);
497
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100499 // Like JumpToExternalReference, but also takes care of passing the number
500 // of parameters.
501 void TailCallExternalReference(const ExternalReference& ext,
502 int num_arguments,
503 int result_size);
504
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800505 // Tail call of a runtime routine (jump). Try to generate the code if
506 // necessary. Do not perform a GC but instead return a retry after GC failure.
507 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
508 const ExternalReference& ext, int num_arguments, int result_size);
509
Steve Block6ded16b2010-05-10 14:33:55 +0100510 // Convenience function: tail call a runtime routine (jump).
511 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 int num_arguments,
513 int result_size);
514
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800515 // Convenience function: tail call a runtime routine (jump). Try to generate
516 // the code if necessary. Do not perform a GC but instead return a retry after
517 // GC failure.
518 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
519 int num_arguments,
520 int result_size);
521
Steve Block6ded16b2010-05-10 14:33:55 +0100522 // Before calling a C-function from generated code, align arguments on stack.
523 // After aligning the frame, arguments must be stored in esp[0], esp[4],
524 // etc., not pushed. The argument count assumes all arguments are word sized.
525 // Some compilers/platforms require the stack to be aligned when calling
526 // C++ code.
527 // Needs a scratch register to do some arithmetic. This register will be
528 // trashed.
529 void PrepareCallCFunction(int num_arguments, Register scratch);
530
531 // Calls a C function and cleans up the space for arguments allocated
532 // by PrepareCallCFunction. The called function is not allowed to trigger a
533 // garbage collection, since that might move the code and invalidate the
534 // return address (unless this is somehow accounted for by the called
535 // function).
536 void CallCFunction(ExternalReference function, int num_arguments);
537 void CallCFunction(Register function, int num_arguments);
538
John Reck59135872010-11-02 12:39:01 -0700539 // Prepares stack to put arguments (aligns and so on). Reserves
540 // space for return value if needed (assumes the return value is a handle).
541 // Uses callee-saved esi to restore stack state after call. Arguments must be
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800542 // stored in ApiParameterOperand(0), ApiParameterOperand(1) etc. Saves
543 // context (esi).
544 void PrepareCallApiFunction(int argc, Register scratch);
Steve Blockd0582a62009-12-15 09:54:21 +0000545
Russell Brenner90bac252010-11-18 13:33:46 -0800546 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700547 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800548 // Clobbers ebx, edi and caller-save registers. Restores context.
549 // On return removes stack_space * kPointerSize (GCed).
550 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
551 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000552
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100554 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000555
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800556 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
557
Steve Blocka7e24c12009-10-30 11:49:00 +0000558
559 // ---------------------------------------------------------------------------
560 // Utilities
561
562 void Ret();
563
Steve Block1e0659c2011-05-24 12:43:12 +0100564 // Return and drop arguments from stack, where the number of arguments
565 // may be bigger than 2^16 - 1. Requires a scratch register.
566 void Ret(int bytes_dropped, Register scratch);
567
Leon Clarkee46be812010-01-19 14:06:41 +0000568 // Emit code to discard a non-negative number of pointer-sized elements
569 // from the stack, clobbering only the esp register.
570 void Drop(int element_count);
571
572 void Call(Label* target) { call(target); }
573
Ben Murdochb0fe1622011-05-05 13:52:32 +0100574 // Emit call to the code we are currently generating.
575 void CallSelf() {
576 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
577 call(self, RelocInfo::CODE_TARGET);
578 }
579
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100580 // Move if the registers are not identical.
581 void Move(Register target, Register source);
582
Leon Clarkee46be812010-01-19 14:06:41 +0000583 void Move(Register target, Handle<Object> value);
584
Ben Murdoch8b112d22011-06-08 16:22:53 +0100585 Handle<Object> CodeObject() {
586 ASSERT(!code_object_.is_null());
587 return code_object_;
588 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000589
590
591 // ---------------------------------------------------------------------------
592 // StatsCounter support
593
594 void SetCounter(StatsCounter* counter, int value);
595 void IncrementCounter(StatsCounter* counter, int value);
596 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000597 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
598 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000599
600
601 // ---------------------------------------------------------------------------
602 // Debugging
603
604 // Calls Abort(msg) if the condition cc is not satisfied.
605 // Use --debug_code to enable.
606 void Assert(Condition cc, const char* msg);
607
Iain Merrick75681382010-08-19 15:07:18 +0100608 void AssertFastElements(Register elements);
609
Steve Blocka7e24c12009-10-30 11:49:00 +0000610 // Like Assert(), but always enabled.
611 void Check(Condition cc, const char* msg);
612
613 // Print a message to stdout and abort execution.
614 void Abort(const char* msg);
615
Steve Block6ded16b2010-05-10 14:33:55 +0100616 // Check that the stack is aligned.
617 void CheckStackAlignment();
618
Steve Blocka7e24c12009-10-30 11:49:00 +0000619 // Verify restrictions about code generated in stubs.
620 void set_generating_stub(bool value) { generating_stub_ = value; }
621 bool generating_stub() { return generating_stub_; }
622 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
623 bool allow_stub_calls() { return allow_stub_calls_; }
624
Leon Clarked91b9f72010-01-27 17:25:45 +0000625 // ---------------------------------------------------------------------------
626 // String utilities.
627
Andrei Popescu402d9372010-02-26 13:31:12 +0000628 // Check whether the instance type represents a flat ascii string. Jump to the
629 // label if not. If the instance type can be scratched specify same register
630 // for both instance type and scratch.
631 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
632 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100633 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000634
Leon Clarked91b9f72010-01-27 17:25:45 +0000635 // Checks if both objects are sequential ASCII strings, and jumps to label
636 // if either is not.
637 void JumpIfNotBothSequentialAsciiStrings(Register object1,
638 Register object2,
639 Register scratch1,
640 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100641 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000642
Ben Murdoch8b112d22011-06-08 16:22:53 +0100643 static int SafepointRegisterStackIndex(Register reg) {
644 return SafepointRegisterStackIndex(reg.code());
645 }
646
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 bool generating_stub_;
649 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000650 // This handle will be patched with the code object on installation.
651 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000652
653 // Helper functions for generating invokes.
654 void InvokePrologue(const ParameterCount& expected,
655 const ParameterCount& actual,
656 Handle<Code> code_constant,
657 const Operand& code_operand,
Steve Block44f0eee2011-05-26 01:26:41 +0100658 NearLabel* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100659 InvokeFlag flag,
660 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000661
Steve Blocka7e24c12009-10-30 11:49:00 +0000662 // Activation support.
663 void EnterFrame(StackFrame::Type type);
664 void LeaveFrame(StackFrame::Type type);
665
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100666 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000668
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800669 void LeaveExitFrameEpilogue();
670
Steve Blocka7e24c12009-10-30 11:49:00 +0000671 // Allocation support helpers.
672 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000673 Register scratch,
674 AllocationFlags flags);
675 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000676
677 // Helper for PopHandleScope. Allowed to perform a GC and returns
678 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
679 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700680 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
681 Register scratch,
682 bool gc_allowed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100683
684
685 // Compute memory operands for safepoint stack slots.
686 Operand SafepointRegisterSlot(Register reg);
687 static int SafepointRegisterStackIndex(int reg_code);
688
689 // Needs access to SafepointRegisterStackIndex for optimized frame
690 // traversal.
691 friend class OptimizedFrame;
Steve Blocka7e24c12009-10-30 11:49:00 +0000692};
693
694
Ben Murdochb0fe1622011-05-05 13:52:32 +0100695template <typename LabelType>
696void MacroAssembler::InNewSpace(Register object,
697 Register scratch,
698 Condition cc,
699 LabelType* branch) {
700 ASSERT(cc == equal || cc == not_equal);
701 if (Serializer::enabled()) {
702 // Can't do arithmetic on external references if it might get serialized.
703 mov(scratch, Operand(object));
704 // The mask isn't really an address. We load it as an external reference in
705 // case the size of the new space is different between the snapshot maker
706 // and the running system.
Steve Block44f0eee2011-05-26 01:26:41 +0100707 and_(Operand(scratch),
708 Immediate(ExternalReference::new_space_mask(isolate())));
709 cmp(Operand(scratch),
710 Immediate(ExternalReference::new_space_start(isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100711 j(cc, branch);
712 } else {
713 int32_t new_space_start = reinterpret_cast<int32_t>(
Steve Block44f0eee2011-05-26 01:26:41 +0100714 ExternalReference::new_space_start(isolate()).address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100715 lea(scratch, Operand(object, -new_space_start));
Steve Block44f0eee2011-05-26 01:26:41 +0100716 and_(scratch, isolate()->heap()->NewSpaceMask());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717 j(cc, branch);
718 }
719}
720
721
Steve Blocka7e24c12009-10-30 11:49:00 +0000722// The code patcher is used to patch (typically) small parts of code e.g. for
723// debugging and other types of instrumentation. When using the code patcher
724// the exact number of bytes specified must be emitted. Is not legal to emit
725// relocation information. If any of these constraints are violated it causes
726// an assertion.
727class CodePatcher {
728 public:
729 CodePatcher(byte* address, int size);
730 virtual ~CodePatcher();
731
732 // Macro assembler to emit code.
733 MacroAssembler* masm() { return &masm_; }
734
735 private:
736 byte* address_; // The address of the code being patched.
737 int size_; // Number of bytes of the expected patch size.
738 MacroAssembler masm_; // Macro assembler used to generate the code.
739};
740
741
Ben Murdochb0fe1622011-05-05 13:52:32 +0100742// Helper class for generating code or data associated with the code
743// right after a call instruction. As an example this can be used to
744// generate safepoint data after calls for crankshaft.
745class PostCallGenerator {
746 public:
747 PostCallGenerator() { }
748 virtual ~PostCallGenerator() { }
749 virtual void Generate() = 0;
750};
751
752
Steve Blocka7e24c12009-10-30 11:49:00 +0000753// -----------------------------------------------------------------------------
754// Static helper functions.
755
756// Generate an Operand for loading a field from an object.
757static inline Operand FieldOperand(Register object, int offset) {
758 return Operand(object, offset - kHeapObjectTag);
759}
760
761
762// Generate an Operand for loading an indexed field from an object.
763static inline Operand FieldOperand(Register object,
764 Register index,
765 ScaleFactor scale,
766 int offset) {
767 return Operand(object, index, scale, offset - kHeapObjectTag);
768}
769
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800770
771static inline Operand ContextOperand(Register context, int index) {
772 return Operand(context, Context::SlotOffset(index));
773}
774
775
776static inline Operand GlobalObjectOperand() {
777 return ContextOperand(esi, Context::GLOBAL_INDEX);
778}
779
780
John Reck59135872010-11-02 12:39:01 -0700781// Generates an Operand for saving parameters after PrepareCallApiFunction.
782Operand ApiParameterOperand(int index);
783
Steve Blocka7e24c12009-10-30 11:49:00 +0000784
785#ifdef GENERATED_CODE_COVERAGE
786extern void LogGeneratedCodeCoverage(const char* file_line);
787#define CODE_COVERAGE_STRINGIFY(x) #x
788#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
789#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
790#define ACCESS_MASM(masm) { \
791 byte* ia32_coverage_function = \
792 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
793 masm->pushfd(); \
794 masm->pushad(); \
795 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
796 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
797 masm->pop(eax); \
798 masm->popad(); \
799 masm->popfd(); \
800 } \
801 masm->
802#else
803#define ACCESS_MASM(masm) masm->
804#endif
805
806
807} } // namespace v8::internal
808
809#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_