blob: 6f5fa87297191df91f483cb736d4baf598918308 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 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.
53class JumpTarget;
Ben Murdochb0fe1622011-05-05 13:52:32 +010054class PostCallGenerator;
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Steve Blocka7e24c12009-10-30 11:49:00 +000056// MacroAssembler implements a collection of frequently used macros.
57class MacroAssembler: public Assembler {
58 public:
59 MacroAssembler(void* buffer, int size);
60
61 // ---------------------------------------------------------------------------
62 // GC Support
63
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010064 // For page containing |object| mark region covering |addr| dirty.
65 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010066 // space.
67 void RecordWriteHelper(Register object,
68 Register addr,
69 Register scratch);
70
71 // Check if object is in new space.
72 // scratch can be object itself, but it will be clobbered.
Ben Murdochb0fe1622011-05-05 13:52:32 +010073 template <typename LabelType>
Steve Block6ded16b2010-05-10 14:33:55 +010074 void InNewSpace(Register object,
75 Register scratch,
76 Condition cc, // equal for new space, not_equal otherwise.
Ben Murdochb0fe1622011-05-05 13:52:32 +010077 LabelType* branch);
Steve Block6ded16b2010-05-10 14:33:55 +010078
Steve Block8defd9f2010-07-08 12:39:36 +010079 // For page containing |object| mark region covering [object+offset]
80 // dirty. |object| is the object being stored into, |value| is the
81 // object being stored. If offset is zero, then the scratch register
82 // contains the array index into the elements array represented as a
83 // Smi. All registers are clobbered by the operation. RecordWrite
84 // filters out smis so it does not update the write barrier if the
85 // value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +000086 void RecordWrite(Register object,
87 int offset,
88 Register value,
89 Register scratch);
90
Steve Block8defd9f2010-07-08 12:39:36 +010091 // For page containing |object| mark region covering |address|
92 // dirty. |object| is the object being stored into, |value| is the
93 // object being stored. All registers are clobbered by the
94 // operation. RecordWrite filters out smis so it does not update the
95 // write barrier if the value is a smi.
96 void RecordWrite(Register object,
97 Register address,
98 Register value);
99
Steve Blocka7e24c12009-10-30 11:49:00 +0000100#ifdef ENABLE_DEBUGGER_SUPPORT
101 // ---------------------------------------------------------------------------
102 // Debugger Support
103
Andrei Popescu402d9372010-02-26 13:31:12 +0000104 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000105#endif
106
107 // ---------------------------------------------------------------------------
108 // Activation frames
109
110 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
111 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
112
113 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
114 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
115
Ben Murdochb0fe1622011-05-05 13:52:32 +0100116 // Enter specific kind of exit frame. Expects the number of
117 // arguments in register eax and sets up the number of arguments in
118 // register edi and the pointer to the first argument in register
119 // esi.
120 void EnterExitFrame(bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000121
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800122 void EnterApiExitFrame(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
124 // Leave the current exit frame. Expects the return value in
125 // register eax:edx (untouched) and the pointer to the first
126 // argument in register esi.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100127 void LeaveExitFrame(bool save_doubles);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800129 // Leave the current exit frame. Expects the return value in
130 // register eax (untouched).
131 void LeaveApiExitFrame();
132
Steve Blockd0582a62009-12-15 09:54:21 +0000133 // Find the function context up the context chain.
134 void LoadContext(Register dst, int context_chain_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000135
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100136 // Load the global function with the given index.
137 void LoadGlobalFunction(int index, Register function);
138
139 // Load the initial map from the global function. The registers
140 // function and map can be the same.
141 void LoadGlobalFunctionInitialMap(Register function, Register map);
142
Ben Murdochb0fe1622011-05-05 13:52:32 +0100143 // Push and pop the registers that can hold pointers.
144 void PushSafepointRegisters() { pushad(); }
145 void PopSafepointRegisters() { popad(); }
146 static int SafepointRegisterStackIndex(int reg_code);
147
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 // ---------------------------------------------------------------------------
149 // JavaScript invokes
150
151 // Invoke the JavaScript function code by either calling or jumping.
152 void InvokeCode(const Operand& code,
153 const ParameterCount& expected,
154 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100155 InvokeFlag flag,
156 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
158 void InvokeCode(Handle<Code> code,
159 const ParameterCount& expected,
160 const ParameterCount& actual,
161 RelocInfo::Mode rmode,
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 // Invoke the JavaScript function in the given register. Changes the
166 // current context to the context in the function before invoking.
167 void InvokeFunction(Register function,
168 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100169 InvokeFlag flag,
170 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000171
Andrei Popescu402d9372010-02-26 13:31:12 +0000172 void InvokeFunction(JSFunction* function,
173 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100174 InvokeFlag flag,
175 PostCallGenerator* post_call_generator = NULL);
Andrei Popescu402d9372010-02-26 13:31:12 +0000176
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 // Invoke specified builtin JavaScript function. Adds an entry to
178 // the unresolved list if the name does not resolve.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100179 void InvokeBuiltin(Builtins::JavaScript id,
180 InvokeFlag flag,
181 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000182
Steve Block791712a2010-08-27 10:21:07 +0100183 // Store the function for the given builtin in the target register.
184 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
185
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 // Store the code object for the given builtin in the target register.
187 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
188
189 // Expression support
190 void Set(Register dst, const Immediate& x);
191 void Set(const Operand& dst, const Immediate& x);
192
193 // Compare object type for heap object.
194 // Incoming register is heap_object and outgoing register is map.
195 void CmpObjectType(Register heap_object, InstanceType type, Register map);
196
197 // Compare instance type for map.
198 void CmpInstanceType(Register map, InstanceType type);
199
Andrei Popescu31002712010-02-23 13:46:05 +0000200 // Check if the map of an object is equal to a specified map and
201 // branch to label if not. Skip the smi check if not required
202 // (object is known to be a heap object)
203 void CheckMap(Register obj,
204 Handle<Map> map,
205 Label* fail,
206 bool is_heap_object);
207
Leon Clarkee46be812010-01-19 14:06:41 +0000208 // Check if the object in register heap_object is a string. Afterwards the
209 // register map contains the object map and the register instance_type
210 // contains the instance_type. The registers map and instance_type can be the
211 // same in which case it contains the instance type afterwards. Either of the
212 // registers map and instance_type can be the same as heap_object.
213 Condition IsObjectStringType(Register heap_object,
214 Register map,
215 Register instance_type);
216
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100217 // Check if a heap object's type is in the JSObject range, not including
218 // JSFunction. The object's map will be loaded in the map register.
219 // Any or all of the three registers may be the same.
220 // The contents of the scratch register will always be overwritten.
221 void IsObjectJSObjectType(Register heap_object,
222 Register map,
223 Register scratch,
224 Label* fail);
225
226 // The contents of the scratch register will be overwritten.
227 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
228
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 // FCmp is similar to integer cmp, but requires unsigned
230 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
231 void FCmp();
232
Leon Clarkee46be812010-01-19 14:06:41 +0000233 // Smi tagging support.
234 void SmiTag(Register reg) {
235 ASSERT(kSmiTag == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100236 ASSERT(kSmiTagSize == 1);
237 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000238 }
239 void SmiUntag(Register reg) {
240 sar(reg, kSmiTagSize);
241 }
242
Iain Merrick75681382010-08-19 15:07:18 +0100243 // Modifies the register even if it does not contain a Smi!
244 void SmiUntag(Register reg, TypeInfo info, Label* non_smi) {
245 ASSERT(kSmiTagSize == 1);
246 sar(reg, kSmiTagSize);
247 if (info.IsSmi()) {
248 ASSERT(kSmiTag == 0);
249 j(carry, non_smi);
250 }
251 }
252
253 // Modifies the register even if it does not contain a Smi!
254 void SmiUntag(Register reg, Label* is_smi) {
255 ASSERT(kSmiTagSize == 1);
256 sar(reg, kSmiTagSize);
257 ASSERT(kSmiTag == 0);
258 j(not_carry, is_smi);
259 }
260
261 // Assumes input is a heap object.
262 void JumpIfNotNumber(Register reg, TypeInfo info, Label* on_not_number);
263
264 // Assumes input is a heap number. Jumps on things out of range. Also jumps
265 // on the min negative int32. Ignores frational parts.
266 void ConvertToInt32(Register dst,
267 Register src, // Can be the same as dst.
268 Register scratch, // Can be no_reg or dst, but not src.
269 TypeInfo info,
270 Label* on_not_int32);
271
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100272 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
273
Andrei Popescu402d9372010-02-26 13:31:12 +0000274 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100275 void AbortIfNotNumber(Register object);
276
277 // Abort execution if argument is not a smi. Used in debug code.
278 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000279
Iain Merrick75681382010-08-19 15:07:18 +0100280 // Abort execution if argument is a smi. Used in debug code.
281 void AbortIfSmi(Register object);
282
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100283 // Abort execution if argument is a string. Used in debug code.
284 void AbortIfNotString(Register object);
285
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 // ---------------------------------------------------------------------------
287 // Exception handling
288
289 // Push a new try handler and link into try handler chain. The return
290 // address must be pushed before calling this helper.
291 void PushTryHandler(CodeLocation try_location, HandlerType type);
292
Leon Clarkee46be812010-01-19 14:06:41 +0000293 // Unlink the stack handler on top of the stack from the try handler chain.
294 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000295
296 // ---------------------------------------------------------------------------
297 // Inline caching support
298
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 // Generate code for checking access rights - used for security checks
300 // on access to global objects across environments. The holder register
301 // is left untouched, but the scratch register is clobbered.
302 void CheckAccessGlobalProxy(Register holder_reg,
303 Register scratch,
304 Label* miss);
305
306
307 // ---------------------------------------------------------------------------
308 // Allocation support
309
310 // Allocate an object in new space. If the new space is exhausted control
311 // continues at the gc_required label. The allocated object is returned in
312 // result and end of the new object is returned in result_end. The register
313 // scratch can be passed as no_reg in which case an additional object
314 // reference will be added to the reloc info. The returned pointers in result
315 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000316 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 // the allocation top on entry (could be result_end from a previous call to
318 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
319 // should be no_reg as it is never used.
320 void AllocateInNewSpace(int object_size,
321 Register result,
322 Register result_end,
323 Register scratch,
324 Label* gc_required,
325 AllocationFlags flags);
326
327 void AllocateInNewSpace(int header_size,
328 ScaleFactor element_size,
329 Register element_count,
330 Register result,
331 Register result_end,
332 Register scratch,
333 Label* gc_required,
334 AllocationFlags flags);
335
336 void AllocateInNewSpace(Register object_size,
337 Register result,
338 Register result_end,
339 Register scratch,
340 Label* gc_required,
341 AllocationFlags flags);
342
343 // Undo allocation in new space. The object passed and objects allocated after
344 // it will no longer be allocated. Make sure that no pointers are left to the
345 // object(s) no longer allocated as they would be invalid when allocation is
346 // un-done.
347 void UndoAllocationInNewSpace(Register object);
348
Steve Block3ce2e202009-11-05 08:53:23 +0000349 // Allocate a heap number in new space with undefined value. The
350 // register scratch2 can be passed as no_reg; the others must be
351 // valid registers. Returns tagged pointer in result register, or
352 // jumps to gc_required if new space is full.
353 void AllocateHeapNumber(Register result,
354 Register scratch1,
355 Register scratch2,
356 Label* gc_required);
357
Steve Blockd0582a62009-12-15 09:54:21 +0000358 // Allocate a sequential string. All the header fields of the string object
359 // are initialized.
360 void AllocateTwoByteString(Register result,
361 Register length,
362 Register scratch1,
363 Register scratch2,
364 Register scratch3,
365 Label* gc_required);
366 void AllocateAsciiString(Register result,
367 Register length,
368 Register scratch1,
369 Register scratch2,
370 Register scratch3,
371 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100372 void AllocateAsciiString(Register result,
373 int length,
374 Register scratch1,
375 Register scratch2,
376 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000377
378 // Allocate a raw cons string object. Only the map field of the result is
379 // initialized.
380 void AllocateConsString(Register result,
381 Register scratch1,
382 Register scratch2,
383 Label* gc_required);
384 void AllocateAsciiConsString(Register result,
385 Register scratch1,
386 Register scratch2,
387 Label* gc_required);
388
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800389 // All registers must be distinct. Only current_string needs valid contents
390 // on entry. All registers may be invalid on exit. result_operand is
391 // unchanged, padding_chars is updated correctly.
392 // The top of new space must contain a sequential ascii string with
393 // padding_chars bytes free in its top word. The sequential ascii string
394 // current_string is concatenated to it, allocating the necessary amount
395 // of new memory.
396 void AppendStringToTopOfNewSpace(
397 Register current_string, // Tagged pointer to string to copy.
398 Register current_string_length,
399 Register result_pos,
400 Register scratch,
401 Register new_padding_chars,
402 Operand operand_result,
403 Operand operand_padding_chars,
404 Label* bailout);
405
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 // ---------------------------------------------------------------------------
407 // Support functions.
408
409 // Check if result is zero and op is negative.
410 void NegativeZeroTest(Register result, Register op, Label* then_label);
411
412 // Check if result is zero and op is negative in code using jump targets.
413 void NegativeZeroTest(CodeGenerator* cgen,
414 Register result,
415 Register op,
416 JumpTarget* then_target);
417
418 // Check if result is zero and any of op1 and op2 are negative.
419 // Register scratch is destroyed, and it must be different from op2.
420 void NegativeZeroTest(Register result, Register op1, Register op2,
421 Register scratch, Label* then_label);
422
423 // Try to get function prototype of a function and puts the value in
424 // the result register. Checks that the function really is a
425 // function and jumps to the miss label if the fast checks fail. The
426 // function register will be untouched; the other registers may be
427 // clobbered.
428 void TryGetFunctionPrototype(Register function,
429 Register result,
430 Register scratch,
431 Label* miss);
432
433 // Generates code for reporting that an illegal operation has
434 // occurred.
435 void IllegalOperation(int num_arguments);
436
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100437 // Picks out an array index from the hash field.
438 // Register use:
439 // hash - holds the index's hash. Clobbered.
440 // index - holds the overwritten index on exit.
441 void IndexFromHash(Register hash, Register index);
442
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 // ---------------------------------------------------------------------------
444 // Runtime calls
445
Leon Clarkee46be812010-01-19 14:06:41 +0000446 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 void CallStub(CodeStub* stub);
448
Leon Clarkee46be812010-01-19 14:06:41 +0000449 // Call a code stub and return the code object called. Try to generate
450 // the code if necessary. Do not perform a GC but instead return a retry
451 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700452 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000453
454 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000455 void TailCallStub(CodeStub* stub);
456
Leon Clarkee46be812010-01-19 14:06:41 +0000457 // Tail call a code stub (jump) and return the code object called. Try to
458 // generate the code if necessary. Do not perform a GC but instead return
459 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700460 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000461
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 // Return from a code stub after popping its arguments.
463 void StubReturn(int argc);
464
465 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000466 void CallRuntime(Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100467 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000468
Leon Clarke4515c472010-02-03 11:58:03 +0000469 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000470 // Try to generate the stub code if necessary. Do not perform a GC
471 // but instead return a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700472 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
473 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 // Convenience function: Same as above, but takes the fid instead.
476 void CallRuntime(Runtime::FunctionId id, int num_arguments);
477
Leon Clarkee46be812010-01-19 14:06:41 +0000478 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700479 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
480 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000481
Ben Murdochbb769b22010-08-11 14:56:33 +0100482 // Convenience function: call an external reference.
483 void CallExternalReference(ExternalReference ref, int num_arguments);
484
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100486 // Like JumpToExternalReference, but also takes care of passing the number
487 // of parameters.
488 void TailCallExternalReference(const ExternalReference& ext,
489 int num_arguments,
490 int result_size);
491
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800492 // Tail call of a runtime routine (jump). Try to generate the code if
493 // necessary. Do not perform a GC but instead return a retry after GC failure.
494 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
495 const ExternalReference& ext, int num_arguments, int result_size);
496
Steve Block6ded16b2010-05-10 14:33:55 +0100497 // Convenience function: tail call a runtime routine (jump).
498 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 int num_arguments,
500 int result_size);
501
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800502 // Convenience function: tail call a runtime routine (jump). Try to generate
503 // the code if necessary. Do not perform a GC but instead return a retry after
504 // GC failure.
505 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
506 int num_arguments,
507 int result_size);
508
Steve Block6ded16b2010-05-10 14:33:55 +0100509 // Before calling a C-function from generated code, align arguments on stack.
510 // After aligning the frame, arguments must be stored in esp[0], esp[4],
511 // etc., not pushed. The argument count assumes all arguments are word sized.
512 // Some compilers/platforms require the stack to be aligned when calling
513 // C++ code.
514 // Needs a scratch register to do some arithmetic. This register will be
515 // trashed.
516 void PrepareCallCFunction(int num_arguments, Register scratch);
517
518 // Calls a C function and cleans up the space for arguments allocated
519 // by PrepareCallCFunction. The called function is not allowed to trigger a
520 // garbage collection, since that might move the code and invalidate the
521 // return address (unless this is somehow accounted for by the called
522 // function).
523 void CallCFunction(ExternalReference function, int num_arguments);
524 void CallCFunction(Register function, int num_arguments);
525
John Reck59135872010-11-02 12:39:01 -0700526 // Prepares stack to put arguments (aligns and so on). Reserves
527 // space for return value if needed (assumes the return value is a handle).
528 // Uses callee-saved esi to restore stack state after call. Arguments must be
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800529 // stored in ApiParameterOperand(0), ApiParameterOperand(1) etc. Saves
530 // context (esi).
531 void PrepareCallApiFunction(int argc, Register scratch);
Steve Blockd0582a62009-12-15 09:54:21 +0000532
Russell Brenner90bac252010-11-18 13:33:46 -0800533 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700534 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800535 // Clobbers ebx, edi and caller-save registers. Restores context.
536 // On return removes stack_space * kPointerSize (GCed).
537 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
538 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000539
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100541 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000542
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800543 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
544
Steve Blocka7e24c12009-10-30 11:49:00 +0000545
546 // ---------------------------------------------------------------------------
547 // Utilities
548
549 void Ret();
550
Leon Clarkee46be812010-01-19 14:06:41 +0000551 // Emit code to discard a non-negative number of pointer-sized elements
552 // from the stack, clobbering only the esp register.
553 void Drop(int element_count);
554
555 void Call(Label* target) { call(target); }
556
Ben Murdochb0fe1622011-05-05 13:52:32 +0100557 // Emit call to the code we are currently generating.
558 void CallSelf() {
559 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
560 call(self, RelocInfo::CODE_TARGET);
561 }
562
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100563 // Move if the registers are not identical.
564 void Move(Register target, Register source);
565
Leon Clarkee46be812010-01-19 14:06:41 +0000566 void Move(Register target, Handle<Object> value);
567
Steve Blocka7e24c12009-10-30 11:49:00 +0000568 Handle<Object> CodeObject() { return code_object_; }
569
570
571 // ---------------------------------------------------------------------------
572 // StatsCounter support
573
574 void SetCounter(StatsCounter* counter, int value);
575 void IncrementCounter(StatsCounter* counter, int value);
576 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000577 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
578 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000579
580
581 // ---------------------------------------------------------------------------
582 // Debugging
583
584 // Calls Abort(msg) if the condition cc is not satisfied.
585 // Use --debug_code to enable.
586 void Assert(Condition cc, const char* msg);
587
Iain Merrick75681382010-08-19 15:07:18 +0100588 void AssertFastElements(Register elements);
589
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 // Like Assert(), but always enabled.
591 void Check(Condition cc, const char* msg);
592
593 // Print a message to stdout and abort execution.
594 void Abort(const char* msg);
595
Steve Block6ded16b2010-05-10 14:33:55 +0100596 // Check that the stack is aligned.
597 void CheckStackAlignment();
598
Steve Blocka7e24c12009-10-30 11:49:00 +0000599 // Verify restrictions about code generated in stubs.
600 void set_generating_stub(bool value) { generating_stub_ = value; }
601 bool generating_stub() { return generating_stub_; }
602 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
603 bool allow_stub_calls() { return allow_stub_calls_; }
604
Leon Clarked91b9f72010-01-27 17:25:45 +0000605 // ---------------------------------------------------------------------------
606 // String utilities.
607
Andrei Popescu402d9372010-02-26 13:31:12 +0000608 // Check whether the instance type represents a flat ascii string. Jump to the
609 // label if not. If the instance type can be scratched specify same register
610 // for both instance type and scratch.
611 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
612 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100613 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000614
Leon Clarked91b9f72010-01-27 17:25:45 +0000615 // Checks if both objects are sequential ASCII strings, and jumps to label
616 // if either is not.
617 void JumpIfNotBothSequentialAsciiStrings(Register object1,
618 Register object2,
619 Register scratch1,
620 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100621 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000622
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 bool generating_stub_;
625 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000626 // This handle will be patched with the code object on installation.
627 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000628
629 // Helper functions for generating invokes.
630 void InvokePrologue(const ParameterCount& expected,
631 const ParameterCount& actual,
632 Handle<Code> code_constant,
633 const Operand& code_operand,
634 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100635 InvokeFlag flag,
636 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000637
Steve Blocka7e24c12009-10-30 11:49:00 +0000638 // Activation support.
639 void EnterFrame(StackFrame::Type type);
640 void LeaveFrame(StackFrame::Type type);
641
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100642 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100643 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000644
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800645 void LeaveExitFrameEpilogue();
646
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 // Allocation support helpers.
648 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000649 Register scratch,
650 AllocationFlags flags);
651 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000652
653 // Helper for PopHandleScope. Allowed to perform a GC and returns
654 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
655 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700656 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
657 Register scratch,
658 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000659};
660
661
Ben Murdochb0fe1622011-05-05 13:52:32 +0100662template <typename LabelType>
663void MacroAssembler::InNewSpace(Register object,
664 Register scratch,
665 Condition cc,
666 LabelType* branch) {
667 ASSERT(cc == equal || cc == not_equal);
668 if (Serializer::enabled()) {
669 // Can't do arithmetic on external references if it might get serialized.
670 mov(scratch, Operand(object));
671 // The mask isn't really an address. We load it as an external reference in
672 // case the size of the new space is different between the snapshot maker
673 // and the running system.
674 and_(Operand(scratch), Immediate(ExternalReference::new_space_mask()));
675 cmp(Operand(scratch), Immediate(ExternalReference::new_space_start()));
676 j(cc, branch);
677 } else {
678 int32_t new_space_start = reinterpret_cast<int32_t>(
679 ExternalReference::new_space_start().address());
680 lea(scratch, Operand(object, -new_space_start));
681 and_(scratch, Heap::NewSpaceMask());
682 j(cc, branch);
683 }
684}
685
686
Steve Blocka7e24c12009-10-30 11:49:00 +0000687// The code patcher is used to patch (typically) small parts of code e.g. for
688// debugging and other types of instrumentation. When using the code patcher
689// the exact number of bytes specified must be emitted. Is not legal to emit
690// relocation information. If any of these constraints are violated it causes
691// an assertion.
692class CodePatcher {
693 public:
694 CodePatcher(byte* address, int size);
695 virtual ~CodePatcher();
696
697 // Macro assembler to emit code.
698 MacroAssembler* masm() { return &masm_; }
699
700 private:
701 byte* address_; // The address of the code being patched.
702 int size_; // Number of bytes of the expected patch size.
703 MacroAssembler masm_; // Macro assembler used to generate the code.
704};
705
706
Ben Murdochb0fe1622011-05-05 13:52:32 +0100707// Helper class for generating code or data associated with the code
708// right after a call instruction. As an example this can be used to
709// generate safepoint data after calls for crankshaft.
710class PostCallGenerator {
711 public:
712 PostCallGenerator() { }
713 virtual ~PostCallGenerator() { }
714 virtual void Generate() = 0;
715};
716
717
Steve Blocka7e24c12009-10-30 11:49:00 +0000718// -----------------------------------------------------------------------------
719// Static helper functions.
720
721// Generate an Operand for loading a field from an object.
722static inline Operand FieldOperand(Register object, int offset) {
723 return Operand(object, offset - kHeapObjectTag);
724}
725
726
727// Generate an Operand for loading an indexed field from an object.
728static inline Operand FieldOperand(Register object,
729 Register index,
730 ScaleFactor scale,
731 int offset) {
732 return Operand(object, index, scale, offset - kHeapObjectTag);
733}
734
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800735
736static inline Operand ContextOperand(Register context, int index) {
737 return Operand(context, Context::SlotOffset(index));
738}
739
740
741static inline Operand GlobalObjectOperand() {
742 return ContextOperand(esi, Context::GLOBAL_INDEX);
743}
744
745
John Reck59135872010-11-02 12:39:01 -0700746// Generates an Operand for saving parameters after PrepareCallApiFunction.
747Operand ApiParameterOperand(int index);
748
Steve Blocka7e24c12009-10-30 11:49:00 +0000749
750#ifdef GENERATED_CODE_COVERAGE
751extern void LogGeneratedCodeCoverage(const char* file_line);
752#define CODE_COVERAGE_STRINGIFY(x) #x
753#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
754#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
755#define ACCESS_MASM(masm) { \
756 byte* ia32_coverage_function = \
757 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
758 masm->pushfd(); \
759 masm->pushad(); \
760 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
761 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
762 masm->pop(eax); \
763 masm->popad(); \
764 masm->popfd(); \
765 } \
766 masm->
767#else
768#define ACCESS_MASM(masm) masm->
769#endif
770
771
772} } // namespace v8::internal
773
774#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_