blob: 690927262ca3c8c2e8f0f07dc5a184bd04143af7 [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
200 // Compare object type for heap object.
201 // Incoming register is heap_object and outgoing register is map.
202 void CmpObjectType(Register heap_object, InstanceType type, Register map);
203
204 // Compare instance type for map.
205 void CmpInstanceType(Register map, InstanceType type);
206
Andrei Popescu31002712010-02-23 13:46:05 +0000207 // Check if the map of an object is equal to a specified map and
208 // branch to label if not. Skip the smi check if not required
209 // (object is known to be a heap object)
210 void CheckMap(Register obj,
211 Handle<Map> map,
212 Label* fail,
213 bool is_heap_object);
214
Leon Clarkee46be812010-01-19 14:06:41 +0000215 // Check if the object in register heap_object is a string. Afterwards the
216 // register map contains the object map and the register instance_type
217 // contains the instance_type. The registers map and instance_type can be the
218 // same in which case it contains the instance type afterwards. Either of the
219 // registers map and instance_type can be the same as heap_object.
220 Condition IsObjectStringType(Register heap_object,
221 Register map,
222 Register instance_type);
223
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100224 // Check if a heap object's type is in the JSObject range, not including
225 // JSFunction. The object's map will be loaded in the map register.
226 // Any or all of the three registers may be the same.
227 // The contents of the scratch register will always be overwritten.
228 void IsObjectJSObjectType(Register heap_object,
229 Register map,
230 Register scratch,
231 Label* fail);
232
233 // The contents of the scratch register will be overwritten.
234 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
235
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 // FCmp is similar to integer cmp, but requires unsigned
237 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
238 void FCmp();
239
Leon Clarkee46be812010-01-19 14:06:41 +0000240 // Smi tagging support.
241 void SmiTag(Register reg) {
242 ASSERT(kSmiTag == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100243 ASSERT(kSmiTagSize == 1);
244 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000245 }
246 void SmiUntag(Register reg) {
247 sar(reg, kSmiTagSize);
248 }
249
Iain Merrick75681382010-08-19 15:07:18 +0100250 // Modifies the register even if it does not contain a Smi!
251 void SmiUntag(Register reg, TypeInfo info, Label* non_smi) {
252 ASSERT(kSmiTagSize == 1);
253 sar(reg, kSmiTagSize);
254 if (info.IsSmi()) {
255 ASSERT(kSmiTag == 0);
256 j(carry, non_smi);
257 }
258 }
259
260 // Modifies the register even if it does not contain a Smi!
261 void SmiUntag(Register reg, Label* is_smi) {
262 ASSERT(kSmiTagSize == 1);
263 sar(reg, kSmiTagSize);
264 ASSERT(kSmiTag == 0);
265 j(not_carry, is_smi);
266 }
267
Steve Block1e0659c2011-05-24 12:43:12 +0100268 // Jump the register contains a smi.
269 inline void JumpIfSmi(Register value, Label* smi_label) {
270 test(value, Immediate(kSmiTagMask));
271 j(zero, smi_label, not_taken);
272 }
273 // Jump if register contain a non-smi.
274 inline void JumpIfNotSmi(Register value, Label* not_smi_label) {
275 test(value, Immediate(kSmiTagMask));
276 j(not_zero, not_smi_label, not_taken);
277 }
278
Iain Merrick75681382010-08-19 15:07:18 +0100279 // Assumes input is a heap object.
280 void JumpIfNotNumber(Register reg, TypeInfo info, Label* on_not_number);
281
282 // Assumes input is a heap number. Jumps on things out of range. Also jumps
283 // on the min negative int32. Ignores frational parts.
284 void ConvertToInt32(Register dst,
285 Register src, // Can be the same as dst.
286 Register scratch, // Can be no_reg or dst, but not src.
287 TypeInfo info,
288 Label* on_not_int32);
289
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100290 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
291
Andrei Popescu402d9372010-02-26 13:31:12 +0000292 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100293 void AbortIfNotNumber(Register object);
294
295 // Abort execution if argument is not a smi. Used in debug code.
296 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000297
Iain Merrick75681382010-08-19 15:07:18 +0100298 // Abort execution if argument is a smi. Used in debug code.
299 void AbortIfSmi(Register object);
300
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100301 // Abort execution if argument is a string. Used in debug code.
302 void AbortIfNotString(Register object);
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 // ---------------------------------------------------------------------------
305 // Exception handling
306
307 // Push a new try handler and link into try handler chain. The return
308 // address must be pushed before calling this helper.
309 void PushTryHandler(CodeLocation try_location, HandlerType type);
310
Leon Clarkee46be812010-01-19 14:06:41 +0000311 // Unlink the stack handler on top of the stack from the try handler chain.
312 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000313
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100314 // Activate the top handler in the try hander chain.
315 void Throw(Register value);
316
317 void ThrowUncatchable(UncatchableExceptionType type, Register value);
318
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 // ---------------------------------------------------------------------------
320 // Inline caching support
321
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 // Generate code for checking access rights - used for security checks
323 // on access to global objects across environments. The holder register
324 // is left untouched, but the scratch register is clobbered.
325 void CheckAccessGlobalProxy(Register holder_reg,
326 Register scratch,
327 Label* miss);
328
329
330 // ---------------------------------------------------------------------------
331 // Allocation support
332
333 // Allocate an object in new space. If the new space is exhausted control
334 // continues at the gc_required label. The allocated object is returned in
335 // result and end of the new object is returned in result_end. The register
336 // scratch can be passed as no_reg in which case an additional object
337 // reference will be added to the reloc info. The returned pointers in result
338 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000339 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 // the allocation top on entry (could be result_end from a previous call to
341 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
342 // should be no_reg as it is never used.
343 void AllocateInNewSpace(int object_size,
344 Register result,
345 Register result_end,
346 Register scratch,
347 Label* gc_required,
348 AllocationFlags flags);
349
350 void AllocateInNewSpace(int header_size,
351 ScaleFactor element_size,
352 Register element_count,
353 Register result,
354 Register result_end,
355 Register scratch,
356 Label* gc_required,
357 AllocationFlags flags);
358
359 void AllocateInNewSpace(Register object_size,
360 Register result,
361 Register result_end,
362 Register scratch,
363 Label* gc_required,
364 AllocationFlags flags);
365
366 // Undo allocation in new space. The object passed and objects allocated after
367 // it will no longer be allocated. Make sure that no pointers are left to the
368 // object(s) no longer allocated as they would be invalid when allocation is
369 // un-done.
370 void UndoAllocationInNewSpace(Register object);
371
Steve Block3ce2e202009-11-05 08:53:23 +0000372 // Allocate a heap number in new space with undefined value. The
373 // register scratch2 can be passed as no_reg; the others must be
374 // valid registers. Returns tagged pointer in result register, or
375 // jumps to gc_required if new space is full.
376 void AllocateHeapNumber(Register result,
377 Register scratch1,
378 Register scratch2,
379 Label* gc_required);
380
Steve Blockd0582a62009-12-15 09:54:21 +0000381 // Allocate a sequential string. All the header fields of the string object
382 // are initialized.
383 void AllocateTwoByteString(Register result,
384 Register length,
385 Register scratch1,
386 Register scratch2,
387 Register scratch3,
388 Label* gc_required);
389 void AllocateAsciiString(Register result,
390 Register length,
391 Register scratch1,
392 Register scratch2,
393 Register scratch3,
394 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100395 void AllocateAsciiString(Register result,
396 int length,
397 Register scratch1,
398 Register scratch2,
399 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000400
401 // Allocate a raw cons string object. Only the map field of the result is
402 // initialized.
403 void AllocateConsString(Register result,
404 Register scratch1,
405 Register scratch2,
406 Label* gc_required);
407 void AllocateAsciiConsString(Register result,
408 Register scratch1,
409 Register scratch2,
410 Label* gc_required);
411
Ben Murdochb8e0da22011-05-16 14:20:40 +0100412 // Copy memory, byte-by-byte, from source to destination. Not optimized for
413 // long or aligned copies.
414 // The contents of index and scratch are destroyed.
415 void CopyBytes(Register source,
416 Register destination,
417 Register length,
418 Register scratch);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800419
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 // ---------------------------------------------------------------------------
421 // Support functions.
422
423 // Check if result is zero and op is negative.
424 void NegativeZeroTest(Register result, Register op, Label* then_label);
425
Steve Blocka7e24c12009-10-30 11:49:00 +0000426 // Check if result is zero and any of op1 and op2 are negative.
427 // Register scratch is destroyed, and it must be different from op2.
428 void NegativeZeroTest(Register result, Register op1, Register op2,
429 Register scratch, Label* then_label);
430
431 // Try to get function prototype of a function and puts the value in
432 // the result register. Checks that the function really is a
433 // function and jumps to the miss label if the fast checks fail. The
434 // function register will be untouched; the other registers may be
435 // clobbered.
436 void TryGetFunctionPrototype(Register function,
437 Register result,
438 Register scratch,
439 Label* miss);
440
441 // Generates code for reporting that an illegal operation has
442 // occurred.
443 void IllegalOperation(int num_arguments);
444
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100445 // Picks out an array index from the hash field.
446 // Register use:
447 // hash - holds the index's hash. Clobbered.
448 // index - holds the overwritten index on exit.
449 void IndexFromHash(Register hash, Register index);
450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 // ---------------------------------------------------------------------------
452 // Runtime calls
453
Leon Clarkee46be812010-01-19 14:06:41 +0000454 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 void CallStub(CodeStub* stub);
456
Leon Clarkee46be812010-01-19 14:06:41 +0000457 // Call a code stub and return the code object called. Try to generate
458 // the code if necessary. Do not perform a GC but instead return a retry
459 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700460 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000461
462 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000463 void TailCallStub(CodeStub* stub);
464
Leon Clarkee46be812010-01-19 14:06:41 +0000465 // Tail call a code stub (jump) and return the code object called. Try to
466 // generate the code if necessary. Do not perform a GC but instead return
467 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700468 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000469
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 // Return from a code stub after popping its arguments.
471 void StubReturn(int argc);
472
473 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100474 void CallRuntime(const Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100475 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000476
Leon Clarke4515c472010-02-03 11:58:03 +0000477 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000478 // Try to generate the stub code if necessary. Do not perform a GC
479 // but instead return a retry after GC failure.
Steve Block44f0eee2011-05-26 01:26:41 +0100480 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -0700481 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000482
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 // Convenience function: Same as above, but takes the fid instead.
484 void CallRuntime(Runtime::FunctionId id, int num_arguments);
485
Leon Clarkee46be812010-01-19 14:06:41 +0000486 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700487 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
488 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000489
Ben Murdochbb769b22010-08-11 14:56:33 +0100490 // Convenience function: call an external reference.
491 void CallExternalReference(ExternalReference ref, int num_arguments);
492
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100494 // Like JumpToExternalReference, but also takes care of passing the number
495 // of parameters.
496 void TailCallExternalReference(const ExternalReference& ext,
497 int num_arguments,
498 int result_size);
499
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800500 // Tail call of a runtime routine (jump). Try to generate the code if
501 // necessary. Do not perform a GC but instead return a retry after GC failure.
502 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
503 const ExternalReference& ext, int num_arguments, int result_size);
504
Steve Block6ded16b2010-05-10 14:33:55 +0100505 // Convenience function: tail call a runtime routine (jump).
506 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 int num_arguments,
508 int result_size);
509
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800510 // Convenience function: tail call a runtime routine (jump). Try to generate
511 // the code if necessary. Do not perform a GC but instead return a retry after
512 // GC failure.
513 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
514 int num_arguments,
515 int result_size);
516
Steve Block6ded16b2010-05-10 14:33:55 +0100517 // Before calling a C-function from generated code, align arguments on stack.
518 // After aligning the frame, arguments must be stored in esp[0], esp[4],
519 // etc., not pushed. The argument count assumes all arguments are word sized.
520 // Some compilers/platforms require the stack to be aligned when calling
521 // C++ code.
522 // Needs a scratch register to do some arithmetic. This register will be
523 // trashed.
524 void PrepareCallCFunction(int num_arguments, Register scratch);
525
526 // Calls a C function and cleans up the space for arguments allocated
527 // by PrepareCallCFunction. The called function is not allowed to trigger a
528 // garbage collection, since that might move the code and invalidate the
529 // return address (unless this is somehow accounted for by the called
530 // function).
531 void CallCFunction(ExternalReference function, int num_arguments);
532 void CallCFunction(Register function, int num_arguments);
533
John Reck59135872010-11-02 12:39:01 -0700534 // Prepares stack to put arguments (aligns and so on). Reserves
535 // space for return value if needed (assumes the return value is a handle).
536 // Uses callee-saved esi to restore stack state after call. Arguments must be
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800537 // stored in ApiParameterOperand(0), ApiParameterOperand(1) etc. Saves
538 // context (esi).
539 void PrepareCallApiFunction(int argc, Register scratch);
Steve Blockd0582a62009-12-15 09:54:21 +0000540
Russell Brenner90bac252010-11-18 13:33:46 -0800541 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700542 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800543 // Clobbers ebx, edi and caller-save registers. Restores context.
544 // On return removes stack_space * kPointerSize (GCed).
545 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
546 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000547
Steve Blocka7e24c12009-10-30 11:49:00 +0000548 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100549 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000550
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800551 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
552
Steve Blocka7e24c12009-10-30 11:49:00 +0000553
554 // ---------------------------------------------------------------------------
555 // Utilities
556
557 void Ret();
558
Steve Block1e0659c2011-05-24 12:43:12 +0100559 // Return and drop arguments from stack, where the number of arguments
560 // may be bigger than 2^16 - 1. Requires a scratch register.
561 void Ret(int bytes_dropped, Register scratch);
562
Leon Clarkee46be812010-01-19 14:06:41 +0000563 // Emit code to discard a non-negative number of pointer-sized elements
564 // from the stack, clobbering only the esp register.
565 void Drop(int element_count);
566
567 void Call(Label* target) { call(target); }
568
Ben Murdochb0fe1622011-05-05 13:52:32 +0100569 // Emit call to the code we are currently generating.
570 void CallSelf() {
571 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
572 call(self, RelocInfo::CODE_TARGET);
573 }
574
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100575 // Move if the registers are not identical.
576 void Move(Register target, Register source);
577
Leon Clarkee46be812010-01-19 14:06:41 +0000578 void Move(Register target, Handle<Object> value);
579
Ben Murdoch8b112d22011-06-08 16:22:53 +0100580 Handle<Object> CodeObject() {
581 ASSERT(!code_object_.is_null());
582 return code_object_;
583 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000584
585
586 // ---------------------------------------------------------------------------
587 // StatsCounter support
588
589 void SetCounter(StatsCounter* counter, int value);
590 void IncrementCounter(StatsCounter* counter, int value);
591 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000592 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
593 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
595
596 // ---------------------------------------------------------------------------
597 // Debugging
598
599 // Calls Abort(msg) if the condition cc is not satisfied.
600 // Use --debug_code to enable.
601 void Assert(Condition cc, const char* msg);
602
Iain Merrick75681382010-08-19 15:07:18 +0100603 void AssertFastElements(Register elements);
604
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 // Like Assert(), but always enabled.
606 void Check(Condition cc, const char* msg);
607
608 // Print a message to stdout and abort execution.
609 void Abort(const char* msg);
610
Steve Block6ded16b2010-05-10 14:33:55 +0100611 // Check that the stack is aligned.
612 void CheckStackAlignment();
613
Steve Blocka7e24c12009-10-30 11:49:00 +0000614 // Verify restrictions about code generated in stubs.
615 void set_generating_stub(bool value) { generating_stub_ = value; }
616 bool generating_stub() { return generating_stub_; }
617 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
618 bool allow_stub_calls() { return allow_stub_calls_; }
619
Leon Clarked91b9f72010-01-27 17:25:45 +0000620 // ---------------------------------------------------------------------------
621 // String utilities.
622
Andrei Popescu402d9372010-02-26 13:31:12 +0000623 // Check whether the instance type represents a flat ascii string. Jump to the
624 // label if not. If the instance type can be scratched specify same register
625 // for both instance type and scratch.
626 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
627 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100628 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000629
Leon Clarked91b9f72010-01-27 17:25:45 +0000630 // Checks if both objects are sequential ASCII strings, and jumps to label
631 // if either is not.
632 void JumpIfNotBothSequentialAsciiStrings(Register object1,
633 Register object2,
634 Register scratch1,
635 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100636 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000637
Ben Murdoch8b112d22011-06-08 16:22:53 +0100638 static int SafepointRegisterStackIndex(Register reg) {
639 return SafepointRegisterStackIndex(reg.code());
640 }
641
Steve Blocka7e24c12009-10-30 11:49:00 +0000642 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000643 bool generating_stub_;
644 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000645 // This handle will be patched with the code object on installation.
646 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000647
648 // Helper functions for generating invokes.
649 void InvokePrologue(const ParameterCount& expected,
650 const ParameterCount& actual,
651 Handle<Code> code_constant,
652 const Operand& code_operand,
Steve Block44f0eee2011-05-26 01:26:41 +0100653 NearLabel* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100654 InvokeFlag flag,
655 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000656
Steve Blocka7e24c12009-10-30 11:49:00 +0000657 // Activation support.
658 void EnterFrame(StackFrame::Type type);
659 void LeaveFrame(StackFrame::Type type);
660
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100661 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100662 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000663
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800664 void LeaveExitFrameEpilogue();
665
Steve Blocka7e24c12009-10-30 11:49:00 +0000666 // Allocation support helpers.
667 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 Register scratch,
669 AllocationFlags flags);
670 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000671
672 // Helper for PopHandleScope. Allowed to perform a GC and returns
673 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
674 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700675 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
676 Register scratch,
677 bool gc_allowed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100678
679
680 // Compute memory operands for safepoint stack slots.
681 Operand SafepointRegisterSlot(Register reg);
682 static int SafepointRegisterStackIndex(int reg_code);
683
684 // Needs access to SafepointRegisterStackIndex for optimized frame
685 // traversal.
686 friend class OptimizedFrame;
Steve Blocka7e24c12009-10-30 11:49:00 +0000687};
688
689
Ben Murdochb0fe1622011-05-05 13:52:32 +0100690template <typename LabelType>
691void MacroAssembler::InNewSpace(Register object,
692 Register scratch,
693 Condition cc,
694 LabelType* branch) {
695 ASSERT(cc == equal || cc == not_equal);
696 if (Serializer::enabled()) {
697 // Can't do arithmetic on external references if it might get serialized.
698 mov(scratch, Operand(object));
699 // The mask isn't really an address. We load it as an external reference in
700 // case the size of the new space is different between the snapshot maker
701 // and the running system.
Steve Block44f0eee2011-05-26 01:26:41 +0100702 and_(Operand(scratch),
703 Immediate(ExternalReference::new_space_mask(isolate())));
704 cmp(Operand(scratch),
705 Immediate(ExternalReference::new_space_start(isolate())));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100706 j(cc, branch);
707 } else {
708 int32_t new_space_start = reinterpret_cast<int32_t>(
Steve Block44f0eee2011-05-26 01:26:41 +0100709 ExternalReference::new_space_start(isolate()).address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100710 lea(scratch, Operand(object, -new_space_start));
Steve Block44f0eee2011-05-26 01:26:41 +0100711 and_(scratch, isolate()->heap()->NewSpaceMask());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100712 j(cc, branch);
713 }
714}
715
716
Steve Blocka7e24c12009-10-30 11:49:00 +0000717// The code patcher is used to patch (typically) small parts of code e.g. for
718// debugging and other types of instrumentation. When using the code patcher
719// the exact number of bytes specified must be emitted. Is not legal to emit
720// relocation information. If any of these constraints are violated it causes
721// an assertion.
722class CodePatcher {
723 public:
724 CodePatcher(byte* address, int size);
725 virtual ~CodePatcher();
726
727 // Macro assembler to emit code.
728 MacroAssembler* masm() { return &masm_; }
729
730 private:
731 byte* address_; // The address of the code being patched.
732 int size_; // Number of bytes of the expected patch size.
733 MacroAssembler masm_; // Macro assembler used to generate the code.
734};
735
736
Ben Murdochb0fe1622011-05-05 13:52:32 +0100737// Helper class for generating code or data associated with the code
738// right after a call instruction. As an example this can be used to
739// generate safepoint data after calls for crankshaft.
740class PostCallGenerator {
741 public:
742 PostCallGenerator() { }
743 virtual ~PostCallGenerator() { }
744 virtual void Generate() = 0;
745};
746
747
Steve Blocka7e24c12009-10-30 11:49:00 +0000748// -----------------------------------------------------------------------------
749// Static helper functions.
750
751// Generate an Operand for loading a field from an object.
752static inline Operand FieldOperand(Register object, int offset) {
753 return Operand(object, offset - kHeapObjectTag);
754}
755
756
757// Generate an Operand for loading an indexed field from an object.
758static inline Operand FieldOperand(Register object,
759 Register index,
760 ScaleFactor scale,
761 int offset) {
762 return Operand(object, index, scale, offset - kHeapObjectTag);
763}
764
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800765
766static inline Operand ContextOperand(Register context, int index) {
767 return Operand(context, Context::SlotOffset(index));
768}
769
770
771static inline Operand GlobalObjectOperand() {
772 return ContextOperand(esi, Context::GLOBAL_INDEX);
773}
774
775
John Reck59135872010-11-02 12:39:01 -0700776// Generates an Operand for saving parameters after PrepareCallApiFunction.
777Operand ApiParameterOperand(int index);
778
Steve Blocka7e24c12009-10-30 11:49:00 +0000779
780#ifdef GENERATED_CODE_COVERAGE
781extern void LogGeneratedCodeCoverage(const char* file_line);
782#define CODE_COVERAGE_STRINGIFY(x) #x
783#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
784#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
785#define ACCESS_MASM(masm) { \
786 byte* ia32_coverage_function = \
787 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
788 masm->pushfd(); \
789 masm->pushad(); \
790 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
791 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
792 masm->pop(eax); \
793 masm->popad(); \
794 masm->popfd(); \
795 } \
796 masm->
797#else
798#define ACCESS_MASM(masm) masm->
799#endif
800
801
802} } // namespace v8::internal
803
804#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_