blob: 09584f7a456830a0f3b534ffe769cbb58a85d894 [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
Steve Block1e0659c2011-05-24 12:43:12 +0100261 // Jump the register contains a smi.
262 inline void JumpIfSmi(Register value, Label* smi_label) {
263 test(value, Immediate(kSmiTagMask));
264 j(zero, smi_label, not_taken);
265 }
266 // Jump if register contain a non-smi.
267 inline void JumpIfNotSmi(Register value, Label* not_smi_label) {
268 test(value, Immediate(kSmiTagMask));
269 j(not_zero, not_smi_label, not_taken);
270 }
271
Iain Merrick75681382010-08-19 15:07:18 +0100272 // Assumes input is a heap object.
273 void JumpIfNotNumber(Register reg, TypeInfo info, Label* on_not_number);
274
275 // Assumes input is a heap number. Jumps on things out of range. Also jumps
276 // on the min negative int32. Ignores frational parts.
277 void ConvertToInt32(Register dst,
278 Register src, // Can be the same as dst.
279 Register scratch, // Can be no_reg or dst, but not src.
280 TypeInfo info,
281 Label* on_not_int32);
282
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100283 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
284
Andrei Popescu402d9372010-02-26 13:31:12 +0000285 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100286 void AbortIfNotNumber(Register object);
287
288 // Abort execution if argument is not a smi. Used in debug code.
289 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000290
Iain Merrick75681382010-08-19 15:07:18 +0100291 // Abort execution if argument is a smi. Used in debug code.
292 void AbortIfSmi(Register object);
293
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100294 // Abort execution if argument is a string. Used in debug code.
295 void AbortIfNotString(Register object);
296
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 // ---------------------------------------------------------------------------
298 // Exception handling
299
300 // Push a new try handler and link into try handler chain. The return
301 // address must be pushed before calling this helper.
302 void PushTryHandler(CodeLocation try_location, HandlerType type);
303
Leon Clarkee46be812010-01-19 14:06:41 +0000304 // Unlink the stack handler on top of the stack from the try handler chain.
305 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000306
307 // ---------------------------------------------------------------------------
308 // Inline caching support
309
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 // Generate code for checking access rights - used for security checks
311 // on access to global objects across environments. The holder register
312 // is left untouched, but the scratch register is clobbered.
313 void CheckAccessGlobalProxy(Register holder_reg,
314 Register scratch,
315 Label* miss);
316
317
318 // ---------------------------------------------------------------------------
319 // Allocation support
320
321 // Allocate an object in new space. If the new space is exhausted control
322 // continues at the gc_required label. The allocated object is returned in
323 // result and end of the new object is returned in result_end. The register
324 // scratch can be passed as no_reg in which case an additional object
325 // reference will be added to the reloc info. The returned pointers in result
326 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000327 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 // the allocation top on entry (could be result_end from a previous call to
329 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
330 // should be no_reg as it is never used.
331 void AllocateInNewSpace(int object_size,
332 Register result,
333 Register result_end,
334 Register scratch,
335 Label* gc_required,
336 AllocationFlags flags);
337
338 void AllocateInNewSpace(int header_size,
339 ScaleFactor element_size,
340 Register element_count,
341 Register result,
342 Register result_end,
343 Register scratch,
344 Label* gc_required,
345 AllocationFlags flags);
346
347 void AllocateInNewSpace(Register object_size,
348 Register result,
349 Register result_end,
350 Register scratch,
351 Label* gc_required,
352 AllocationFlags flags);
353
354 // Undo allocation in new space. The object passed and objects allocated after
355 // it will no longer be allocated. Make sure that no pointers are left to the
356 // object(s) no longer allocated as they would be invalid when allocation is
357 // un-done.
358 void UndoAllocationInNewSpace(Register object);
359
Steve Block3ce2e202009-11-05 08:53:23 +0000360 // Allocate a heap number in new space with undefined value. The
361 // register scratch2 can be passed as no_reg; the others must be
362 // valid registers. Returns tagged pointer in result register, or
363 // jumps to gc_required if new space is full.
364 void AllocateHeapNumber(Register result,
365 Register scratch1,
366 Register scratch2,
367 Label* gc_required);
368
Steve Blockd0582a62009-12-15 09:54:21 +0000369 // Allocate a sequential string. All the header fields of the string object
370 // are initialized.
371 void AllocateTwoByteString(Register result,
372 Register length,
373 Register scratch1,
374 Register scratch2,
375 Register scratch3,
376 Label* gc_required);
377 void AllocateAsciiString(Register result,
378 Register length,
379 Register scratch1,
380 Register scratch2,
381 Register scratch3,
382 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100383 void AllocateAsciiString(Register result,
384 int length,
385 Register scratch1,
386 Register scratch2,
387 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000388
389 // Allocate a raw cons string object. Only the map field of the result is
390 // initialized.
391 void AllocateConsString(Register result,
392 Register scratch1,
393 Register scratch2,
394 Label* gc_required);
395 void AllocateAsciiConsString(Register result,
396 Register scratch1,
397 Register scratch2,
398 Label* gc_required);
399
Ben Murdochb8e0da22011-05-16 14:20:40 +0100400 // Copy memory, byte-by-byte, from source to destination. Not optimized for
401 // long or aligned copies.
402 // The contents of index and scratch are destroyed.
403 void CopyBytes(Register source,
404 Register destination,
405 Register length,
406 Register scratch);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800407
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 // ---------------------------------------------------------------------------
409 // Support functions.
410
411 // Check if result is zero and op is negative.
412 void NegativeZeroTest(Register result, Register op, Label* then_label);
413
414 // Check if result is zero and op is negative in code using jump targets.
415 void NegativeZeroTest(CodeGenerator* cgen,
416 Register result,
417 Register op,
418 JumpTarget* then_target);
419
420 // Check if result is zero and any of op1 and op2 are negative.
421 // Register scratch is destroyed, and it must be different from op2.
422 void NegativeZeroTest(Register result, Register op1, Register op2,
423 Register scratch, Label* then_label);
424
425 // Try to get function prototype of a function and puts the value in
426 // the result register. Checks that the function really is a
427 // function and jumps to the miss label if the fast checks fail. The
428 // function register will be untouched; the other registers may be
429 // clobbered.
430 void TryGetFunctionPrototype(Register function,
431 Register result,
432 Register scratch,
433 Label* miss);
434
435 // Generates code for reporting that an illegal operation has
436 // occurred.
437 void IllegalOperation(int num_arguments);
438
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100439 // Picks out an array index from the hash field.
440 // Register use:
441 // hash - holds the index's hash. Clobbered.
442 // index - holds the overwritten index on exit.
443 void IndexFromHash(Register hash, Register index);
444
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 // ---------------------------------------------------------------------------
446 // Runtime calls
447
Leon Clarkee46be812010-01-19 14:06:41 +0000448 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 void CallStub(CodeStub* stub);
450
Leon Clarkee46be812010-01-19 14:06:41 +0000451 // Call a code stub and return the code object called. Try to generate
452 // the code if necessary. Do not perform a GC but instead return a retry
453 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700454 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000455
456 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000457 void TailCallStub(CodeStub* stub);
458
Leon Clarkee46be812010-01-19 14:06:41 +0000459 // Tail call a code stub (jump) and return the code object called. Try to
460 // generate the code if necessary. Do not perform a GC but instead return
461 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700462 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000463
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 // Return from a code stub after popping its arguments.
465 void StubReturn(int argc);
466
467 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 void CallRuntime(Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100469 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470
Leon Clarke4515c472010-02-03 11:58:03 +0000471 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000472 // Try to generate the stub code if necessary. Do not perform a GC
473 // but instead return a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700474 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
475 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000476
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 // Convenience function: Same as above, but takes the fid instead.
478 void CallRuntime(Runtime::FunctionId id, int num_arguments);
479
Leon Clarkee46be812010-01-19 14:06:41 +0000480 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700481 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
482 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000483
Ben Murdochbb769b22010-08-11 14:56:33 +0100484 // Convenience function: call an external reference.
485 void CallExternalReference(ExternalReference ref, int num_arguments);
486
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100488 // Like JumpToExternalReference, but also takes care of passing the number
489 // of parameters.
490 void TailCallExternalReference(const ExternalReference& ext,
491 int num_arguments,
492 int result_size);
493
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800494 // Tail call of a runtime routine (jump). Try to generate the code if
495 // necessary. Do not perform a GC but instead return a retry after GC failure.
496 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
497 const ExternalReference& ext, int num_arguments, int result_size);
498
Steve Block6ded16b2010-05-10 14:33:55 +0100499 // Convenience function: tail call a runtime routine (jump).
500 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 int num_arguments,
502 int result_size);
503
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800504 // Convenience function: tail call a runtime routine (jump). Try to generate
505 // the code if necessary. Do not perform a GC but instead return a retry after
506 // GC failure.
507 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
508 int num_arguments,
509 int result_size);
510
Steve Block6ded16b2010-05-10 14:33:55 +0100511 // Before calling a C-function from generated code, align arguments on stack.
512 // After aligning the frame, arguments must be stored in esp[0], esp[4],
513 // etc., not pushed. The argument count assumes all arguments are word sized.
514 // Some compilers/platforms require the stack to be aligned when calling
515 // C++ code.
516 // Needs a scratch register to do some arithmetic. This register will be
517 // trashed.
518 void PrepareCallCFunction(int num_arguments, Register scratch);
519
520 // Calls a C function and cleans up the space for arguments allocated
521 // by PrepareCallCFunction. The called function is not allowed to trigger a
522 // garbage collection, since that might move the code and invalidate the
523 // return address (unless this is somehow accounted for by the called
524 // function).
525 void CallCFunction(ExternalReference function, int num_arguments);
526 void CallCFunction(Register function, int num_arguments);
527
John Reck59135872010-11-02 12:39:01 -0700528 // Prepares stack to put arguments (aligns and so on). Reserves
529 // space for return value if needed (assumes the return value is a handle).
530 // Uses callee-saved esi to restore stack state after call. Arguments must be
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800531 // stored in ApiParameterOperand(0), ApiParameterOperand(1) etc. Saves
532 // context (esi).
533 void PrepareCallApiFunction(int argc, Register scratch);
Steve Blockd0582a62009-12-15 09:54:21 +0000534
Russell Brenner90bac252010-11-18 13:33:46 -0800535 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700536 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800537 // Clobbers ebx, edi and caller-save registers. Restores context.
538 // On return removes stack_space * kPointerSize (GCed).
539 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
540 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000541
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100543 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000544
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800545 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
546
Steve Blocka7e24c12009-10-30 11:49:00 +0000547
548 // ---------------------------------------------------------------------------
549 // Utilities
550
551 void Ret();
552
Steve Block1e0659c2011-05-24 12:43:12 +0100553 // Return and drop arguments from stack, where the number of arguments
554 // may be bigger than 2^16 - 1. Requires a scratch register.
555 void Ret(int bytes_dropped, Register scratch);
556
Leon Clarkee46be812010-01-19 14:06:41 +0000557 // Emit code to discard a non-negative number of pointer-sized elements
558 // from the stack, clobbering only the esp register.
559 void Drop(int element_count);
560
561 void Call(Label* target) { call(target); }
562
Ben Murdochb0fe1622011-05-05 13:52:32 +0100563 // Emit call to the code we are currently generating.
564 void CallSelf() {
565 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
566 call(self, RelocInfo::CODE_TARGET);
567 }
568
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100569 // Move if the registers are not identical.
570 void Move(Register target, Register source);
571
Leon Clarkee46be812010-01-19 14:06:41 +0000572 void Move(Register target, Handle<Object> value);
573
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 Handle<Object> CodeObject() { return code_object_; }
575
576
577 // ---------------------------------------------------------------------------
578 // StatsCounter support
579
580 void SetCounter(StatsCounter* counter, int value);
581 void IncrementCounter(StatsCounter* counter, int value);
582 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000583 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
584 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000585
586
587 // ---------------------------------------------------------------------------
588 // Debugging
589
590 // Calls Abort(msg) if the condition cc is not satisfied.
591 // Use --debug_code to enable.
592 void Assert(Condition cc, const char* msg);
593
Iain Merrick75681382010-08-19 15:07:18 +0100594 void AssertFastElements(Register elements);
595
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 // Like Assert(), but always enabled.
597 void Check(Condition cc, const char* msg);
598
599 // Print a message to stdout and abort execution.
600 void Abort(const char* msg);
601
Steve Block6ded16b2010-05-10 14:33:55 +0100602 // Check that the stack is aligned.
603 void CheckStackAlignment();
604
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 // Verify restrictions about code generated in stubs.
606 void set_generating_stub(bool value) { generating_stub_ = value; }
607 bool generating_stub() { return generating_stub_; }
608 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
609 bool allow_stub_calls() { return allow_stub_calls_; }
610
Leon Clarked91b9f72010-01-27 17:25:45 +0000611 // ---------------------------------------------------------------------------
612 // String utilities.
613
Andrei Popescu402d9372010-02-26 13:31:12 +0000614 // Check whether the instance type represents a flat ascii string. Jump to the
615 // label if not. If the instance type can be scratched specify same register
616 // for both instance type and scratch.
617 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
618 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100619 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000620
Leon Clarked91b9f72010-01-27 17:25:45 +0000621 // Checks if both objects are sequential ASCII strings, and jumps to label
622 // if either is not.
623 void JumpIfNotBothSequentialAsciiStrings(Register object1,
624 Register object2,
625 Register scratch1,
626 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100627 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000628
Steve Blocka7e24c12009-10-30 11:49:00 +0000629 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 bool generating_stub_;
631 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000632 // This handle will be patched with the code object on installation.
633 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000634
635 // Helper functions for generating invokes.
636 void InvokePrologue(const ParameterCount& expected,
637 const ParameterCount& actual,
638 Handle<Code> code_constant,
639 const Operand& code_operand,
640 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100641 InvokeFlag flag,
642 PostCallGenerator* post_call_generator = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000643
Steve Blocka7e24c12009-10-30 11:49:00 +0000644 // Activation support.
645 void EnterFrame(StackFrame::Type type);
646 void LeaveFrame(StackFrame::Type type);
647
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100648 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100649 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000650
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800651 void LeaveExitFrameEpilogue();
652
Steve Blocka7e24c12009-10-30 11:49:00 +0000653 // Allocation support helpers.
654 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000655 Register scratch,
656 AllocationFlags flags);
657 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000658
659 // Helper for PopHandleScope. Allowed to perform a GC and returns
660 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
661 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700662 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
663 Register scratch,
664 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000665};
666
667
Ben Murdochb0fe1622011-05-05 13:52:32 +0100668template <typename LabelType>
669void MacroAssembler::InNewSpace(Register object,
670 Register scratch,
671 Condition cc,
672 LabelType* branch) {
673 ASSERT(cc == equal || cc == not_equal);
674 if (Serializer::enabled()) {
675 // Can't do arithmetic on external references if it might get serialized.
676 mov(scratch, Operand(object));
677 // The mask isn't really an address. We load it as an external reference in
678 // case the size of the new space is different between the snapshot maker
679 // and the running system.
680 and_(Operand(scratch), Immediate(ExternalReference::new_space_mask()));
681 cmp(Operand(scratch), Immediate(ExternalReference::new_space_start()));
682 j(cc, branch);
683 } else {
684 int32_t new_space_start = reinterpret_cast<int32_t>(
685 ExternalReference::new_space_start().address());
686 lea(scratch, Operand(object, -new_space_start));
687 and_(scratch, Heap::NewSpaceMask());
688 j(cc, branch);
689 }
690}
691
692
Steve Blocka7e24c12009-10-30 11:49:00 +0000693// The code patcher is used to patch (typically) small parts of code e.g. for
694// debugging and other types of instrumentation. When using the code patcher
695// the exact number of bytes specified must be emitted. Is not legal to emit
696// relocation information. If any of these constraints are violated it causes
697// an assertion.
698class CodePatcher {
699 public:
700 CodePatcher(byte* address, int size);
701 virtual ~CodePatcher();
702
703 // Macro assembler to emit code.
704 MacroAssembler* masm() { return &masm_; }
705
706 private:
707 byte* address_; // The address of the code being patched.
708 int size_; // Number of bytes of the expected patch size.
709 MacroAssembler masm_; // Macro assembler used to generate the code.
710};
711
712
Ben Murdochb0fe1622011-05-05 13:52:32 +0100713// Helper class for generating code or data associated with the code
714// right after a call instruction. As an example this can be used to
715// generate safepoint data after calls for crankshaft.
716class PostCallGenerator {
717 public:
718 PostCallGenerator() { }
719 virtual ~PostCallGenerator() { }
720 virtual void Generate() = 0;
721};
722
723
Steve Blocka7e24c12009-10-30 11:49:00 +0000724// -----------------------------------------------------------------------------
725// Static helper functions.
726
727// Generate an Operand for loading a field from an object.
728static inline Operand FieldOperand(Register object, int offset) {
729 return Operand(object, offset - kHeapObjectTag);
730}
731
732
733// Generate an Operand for loading an indexed field from an object.
734static inline Operand FieldOperand(Register object,
735 Register index,
736 ScaleFactor scale,
737 int offset) {
738 return Operand(object, index, scale, offset - kHeapObjectTag);
739}
740
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800741
742static inline Operand ContextOperand(Register context, int index) {
743 return Operand(context, Context::SlotOffset(index));
744}
745
746
747static inline Operand GlobalObjectOperand() {
748 return ContextOperand(esi, Context::GLOBAL_INDEX);
749}
750
751
John Reck59135872010-11-02 12:39:01 -0700752// Generates an Operand for saving parameters after PrepareCallApiFunction.
753Operand ApiParameterOperand(int index);
754
Steve Blocka7e24c12009-10-30 11:49:00 +0000755
756#ifdef GENERATED_CODE_COVERAGE
757extern void LogGeneratedCodeCoverage(const char* file_line);
758#define CODE_COVERAGE_STRINGIFY(x) #x
759#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
760#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
761#define ACCESS_MASM(masm) { \
762 byte* ia32_coverage_function = \
763 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
764 masm->pushfd(); \
765 masm->pushad(); \
766 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
767 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
768 masm->pop(eax); \
769 masm->popad(); \
770 masm->popfd(); \
771 } \
772 masm->
773#else
774#define ACCESS_MASM(masm) masm->
775#endif
776
777
778} } // namespace v8::internal
779
780#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_