blob: d208dbe3fa88eb2d92fd4e6e9cde170d2b117798 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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;
54
Steve Blocka7e24c12009-10-30 11:49:00 +000055// MacroAssembler implements a collection of frequently used macros.
56class MacroAssembler: public Assembler {
57 public:
58 MacroAssembler(void* buffer, int size);
59
60 // ---------------------------------------------------------------------------
61 // GC Support
62
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010063 // For page containing |object| mark region covering |addr| dirty.
64 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010065 // space.
66 void RecordWriteHelper(Register object,
67 Register addr,
68 Register scratch);
69
70 // Check if object is in new space.
71 // scratch can be object itself, but it will be clobbered.
72 void InNewSpace(Register object,
73 Register scratch,
74 Condition cc, // equal for new space, not_equal otherwise.
75 Label* branch);
76
Steve Block8defd9f2010-07-08 12:39:36 +010077 // For page containing |object| mark region covering [object+offset]
78 // dirty. |object| is the object being stored into, |value| is the
79 // object being stored. If offset is zero, then the scratch register
80 // contains the array index into the elements array represented as a
81 // Smi. All registers are clobbered by the operation. RecordWrite
82 // filters out smis so it does not update the write barrier if the
83 // value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +000084 void RecordWrite(Register object,
85 int offset,
86 Register value,
87 Register scratch);
88
Steve Block8defd9f2010-07-08 12:39:36 +010089 // For page containing |object| mark region covering |address|
90 // dirty. |object| is the object being stored into, |value| is the
91 // object being stored. All registers are clobbered by the
92 // operation. RecordWrite filters out smis so it does not update the
93 // write barrier if the value is a smi.
94 void RecordWrite(Register object,
95 Register address,
96 Register value);
97
Steve Blocka7e24c12009-10-30 11:49:00 +000098#ifdef ENABLE_DEBUGGER_SUPPORT
99 // ---------------------------------------------------------------------------
100 // Debugger Support
101
Andrei Popescu402d9372010-02-26 13:31:12 +0000102 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103#endif
104
105 // ---------------------------------------------------------------------------
Steve Blockd0582a62009-12-15 09:54:21 +0000106 // Stack limit support
107
108 // Do simple test for stack overflow. This doesn't handle an overflow.
109 void StackLimitCheck(Label* on_stack_limit_hit);
110
111 // ---------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 // Activation frames
113
114 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
115 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
116
117 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
118 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
119
Steve Blockd0582a62009-12-15 09:54:21 +0000120 // Enter specific kind of exit frame; either in normal or debug mode.
121 // Expects the number of arguments in register eax and
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 // sets up the number of arguments in register edi and the pointer
123 // to the first argument in register esi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100124 void EnterExitFrame();
Steve Blockd0582a62009-12-15 09:54:21 +0000125
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800126 void EnterApiExitFrame(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000127
128 // Leave the current exit frame. Expects the return value in
129 // register eax:edx (untouched) and the pointer to the first
130 // argument in register esi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100131 void LeaveExitFrame();
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800133 // Leave the current exit frame. Expects the return value in
134 // register eax (untouched).
135 void LeaveApiExitFrame();
136
Steve Blockd0582a62009-12-15 09:54:21 +0000137 // Find the function context up the context chain.
138 void LoadContext(Register dst, int context_chain_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100140 // Load the global function with the given index.
141 void LoadGlobalFunction(int index, Register function);
142
143 // Load the initial map from the global function. The registers
144 // function and map can be the same.
145 void LoadGlobalFunctionInitialMap(Register function, Register map);
146
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 // ---------------------------------------------------------------------------
148 // JavaScript invokes
149
150 // Invoke the JavaScript function code by either calling or jumping.
151 void InvokeCode(const Operand& code,
152 const ParameterCount& expected,
153 const ParameterCount& actual,
154 InvokeFlag flag);
155
156 void InvokeCode(Handle<Code> code,
157 const ParameterCount& expected,
158 const ParameterCount& actual,
159 RelocInfo::Mode rmode,
160 InvokeFlag flag);
161
162 // Invoke the JavaScript function in the given register. Changes the
163 // current context to the context in the function before invoking.
164 void InvokeFunction(Register function,
165 const ParameterCount& actual,
166 InvokeFlag flag);
167
Andrei Popescu402d9372010-02-26 13:31:12 +0000168 void InvokeFunction(JSFunction* function,
169 const ParameterCount& actual,
170 InvokeFlag flag);
171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 // Invoke specified builtin JavaScript function. Adds an entry to
173 // the unresolved list if the name does not resolve.
174 void InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag);
175
Steve Block791712a2010-08-27 10:21:07 +0100176 // Store the function for the given builtin in the target register.
177 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
178
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 // Store the code object for the given builtin in the target register.
180 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
181
182 // Expression support
183 void Set(Register dst, const Immediate& x);
184 void Set(const Operand& dst, const Immediate& x);
185
186 // Compare object type for heap object.
187 // Incoming register is heap_object and outgoing register is map.
188 void CmpObjectType(Register heap_object, InstanceType type, Register map);
189
190 // Compare instance type for map.
191 void CmpInstanceType(Register map, InstanceType type);
192
Andrei Popescu31002712010-02-23 13:46:05 +0000193 // Check if the map of an object is equal to a specified map and
194 // branch to label if not. Skip the smi check if not required
195 // (object is known to be a heap object)
196 void CheckMap(Register obj,
197 Handle<Map> map,
198 Label* fail,
199 bool is_heap_object);
200
Leon Clarkee46be812010-01-19 14:06:41 +0000201 // Check if the object in register heap_object is a string. Afterwards the
202 // register map contains the object map and the register instance_type
203 // contains the instance_type. The registers map and instance_type can be the
204 // same in which case it contains the instance type afterwards. Either of the
205 // registers map and instance_type can be the same as heap_object.
206 Condition IsObjectStringType(Register heap_object,
207 Register map,
208 Register instance_type);
209
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100210 // Check if a heap object's type is in the JSObject range, not including
211 // JSFunction. The object's map will be loaded in the map register.
212 // Any or all of the three registers may be the same.
213 // The contents of the scratch register will always be overwritten.
214 void IsObjectJSObjectType(Register heap_object,
215 Register map,
216 Register scratch,
217 Label* fail);
218
219 // The contents of the scratch register will be overwritten.
220 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
221
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 // FCmp is similar to integer cmp, but requires unsigned
223 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
224 void FCmp();
225
Leon Clarkee46be812010-01-19 14:06:41 +0000226 // Smi tagging support.
227 void SmiTag(Register reg) {
228 ASSERT(kSmiTag == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100229 ASSERT(kSmiTagSize == 1);
230 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000231 }
232 void SmiUntag(Register reg) {
233 sar(reg, kSmiTagSize);
234 }
235
Iain Merrick75681382010-08-19 15:07:18 +0100236 // Modifies the register even if it does not contain a Smi!
237 void SmiUntag(Register reg, TypeInfo info, Label* non_smi) {
238 ASSERT(kSmiTagSize == 1);
239 sar(reg, kSmiTagSize);
240 if (info.IsSmi()) {
241 ASSERT(kSmiTag == 0);
242 j(carry, non_smi);
243 }
244 }
245
246 // Modifies the register even if it does not contain a Smi!
247 void SmiUntag(Register reg, Label* is_smi) {
248 ASSERT(kSmiTagSize == 1);
249 sar(reg, kSmiTagSize);
250 ASSERT(kSmiTag == 0);
251 j(not_carry, is_smi);
252 }
253
254 // Assumes input is a heap object.
255 void JumpIfNotNumber(Register reg, TypeInfo info, Label* on_not_number);
256
257 // Assumes input is a heap number. Jumps on things out of range. Also jumps
258 // on the min negative int32. Ignores frational parts.
259 void ConvertToInt32(Register dst,
260 Register src, // Can be the same as dst.
261 Register scratch, // Can be no_reg or dst, but not src.
262 TypeInfo info,
263 Label* on_not_int32);
264
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100265 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
266
Andrei Popescu402d9372010-02-26 13:31:12 +0000267 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100268 void AbortIfNotNumber(Register object);
269
270 // Abort execution if argument is not a smi. Used in debug code.
271 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000272
Iain Merrick75681382010-08-19 15:07:18 +0100273 // Abort execution if argument is a smi. Used in debug code.
274 void AbortIfSmi(Register object);
275
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100276 // Abort execution if argument is a string. Used in debug code.
277 void AbortIfNotString(Register object);
278
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 // ---------------------------------------------------------------------------
280 // Exception handling
281
282 // Push a new try handler and link into try handler chain. The return
283 // address must be pushed before calling this helper.
284 void PushTryHandler(CodeLocation try_location, HandlerType type);
285
Leon Clarkee46be812010-01-19 14:06:41 +0000286 // Unlink the stack handler on top of the stack from the try handler chain.
287 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000288
289 // ---------------------------------------------------------------------------
290 // Inline caching support
291
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 // Generate code for checking access rights - used for security checks
293 // on access to global objects across environments. The holder register
294 // is left untouched, but the scratch register is clobbered.
295 void CheckAccessGlobalProxy(Register holder_reg,
296 Register scratch,
297 Label* miss);
298
299
300 // ---------------------------------------------------------------------------
301 // Allocation support
302
303 // Allocate an object in new space. If the new space is exhausted control
304 // continues at the gc_required label. The allocated object is returned in
305 // result and end of the new object is returned in result_end. The register
306 // scratch can be passed as no_reg in which case an additional object
307 // reference will be added to the reloc info. The returned pointers in result
308 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000309 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 // the allocation top on entry (could be result_end from a previous call to
311 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
312 // should be no_reg as it is never used.
313 void AllocateInNewSpace(int object_size,
314 Register result,
315 Register result_end,
316 Register scratch,
317 Label* gc_required,
318 AllocationFlags flags);
319
320 void AllocateInNewSpace(int header_size,
321 ScaleFactor element_size,
322 Register element_count,
323 Register result,
324 Register result_end,
325 Register scratch,
326 Label* gc_required,
327 AllocationFlags flags);
328
329 void AllocateInNewSpace(Register object_size,
330 Register result,
331 Register result_end,
332 Register scratch,
333 Label* gc_required,
334 AllocationFlags flags);
335
336 // Undo allocation in new space. The object passed and objects allocated after
337 // it will no longer be allocated. Make sure that no pointers are left to the
338 // object(s) no longer allocated as they would be invalid when allocation is
339 // un-done.
340 void UndoAllocationInNewSpace(Register object);
341
Steve Block3ce2e202009-11-05 08:53:23 +0000342 // Allocate a heap number in new space with undefined value. The
343 // register scratch2 can be passed as no_reg; the others must be
344 // valid registers. Returns tagged pointer in result register, or
345 // jumps to gc_required if new space is full.
346 void AllocateHeapNumber(Register result,
347 Register scratch1,
348 Register scratch2,
349 Label* gc_required);
350
Steve Blockd0582a62009-12-15 09:54:21 +0000351 // Allocate a sequential string. All the header fields of the string object
352 // are initialized.
353 void AllocateTwoByteString(Register result,
354 Register length,
355 Register scratch1,
356 Register scratch2,
357 Register scratch3,
358 Label* gc_required);
359 void AllocateAsciiString(Register result,
360 Register length,
361 Register scratch1,
362 Register scratch2,
363 Register scratch3,
364 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100365 void AllocateAsciiString(Register result,
366 int length,
367 Register scratch1,
368 Register scratch2,
369 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000370
371 // Allocate a raw cons string object. Only the map field of the result is
372 // initialized.
373 void AllocateConsString(Register result,
374 Register scratch1,
375 Register scratch2,
376 Label* gc_required);
377 void AllocateAsciiConsString(Register result,
378 Register scratch1,
379 Register scratch2,
380 Label* gc_required);
381
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800382 // All registers must be distinct. Only current_string needs valid contents
383 // on entry. All registers may be invalid on exit. result_operand is
384 // unchanged, padding_chars is updated correctly.
385 // The top of new space must contain a sequential ascii string with
386 // padding_chars bytes free in its top word. The sequential ascii string
387 // current_string is concatenated to it, allocating the necessary amount
388 // of new memory.
389 void AppendStringToTopOfNewSpace(
390 Register current_string, // Tagged pointer to string to copy.
391 Register current_string_length,
392 Register result_pos,
393 Register scratch,
394 Register new_padding_chars,
395 Operand operand_result,
396 Operand operand_padding_chars,
397 Label* bailout);
398
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 // ---------------------------------------------------------------------------
400 // Support functions.
401
402 // Check if result is zero and op is negative.
403 void NegativeZeroTest(Register result, Register op, Label* then_label);
404
405 // Check if result is zero and op is negative in code using jump targets.
406 void NegativeZeroTest(CodeGenerator* cgen,
407 Register result,
408 Register op,
409 JumpTarget* then_target);
410
411 // Check if result is zero and any of op1 and op2 are negative.
412 // Register scratch is destroyed, and it must be different from op2.
413 void NegativeZeroTest(Register result, Register op1, Register op2,
414 Register scratch, Label* then_label);
415
416 // Try to get function prototype of a function and puts the value in
417 // the result register. Checks that the function really is a
418 // function and jumps to the miss label if the fast checks fail. The
419 // function register will be untouched; the other registers may be
420 // clobbered.
421 void TryGetFunctionPrototype(Register function,
422 Register result,
423 Register scratch,
424 Label* miss);
425
426 // Generates code for reporting that an illegal operation has
427 // occurred.
428 void IllegalOperation(int num_arguments);
429
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100430 // Picks out an array index from the hash field.
431 // Register use:
432 // hash - holds the index's hash. Clobbered.
433 // index - holds the overwritten index on exit.
434 void IndexFromHash(Register hash, Register index);
435
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 // ---------------------------------------------------------------------------
437 // Runtime calls
438
Leon Clarkee46be812010-01-19 14:06:41 +0000439 // Call a code stub. Generate the code if necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 void CallStub(CodeStub* stub);
441
Leon Clarkee46be812010-01-19 14:06:41 +0000442 // Call a code stub and return the code object called. Try to generate
443 // the code if necessary. Do not perform a GC but instead return a retry
444 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700445 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000446
447 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000448 void TailCallStub(CodeStub* stub);
449
Leon Clarkee46be812010-01-19 14:06:41 +0000450 // Tail call a code stub (jump) and return the code object called. Try to
451 // generate the code if necessary. Do not perform a GC but instead return
452 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700453 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000454
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 // Return from a code stub after popping its arguments.
456 void StubReturn(int argc);
457
458 // Call a runtime routine.
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 void CallRuntime(Runtime::Function* f, int num_arguments);
460
Leon Clarke4515c472010-02-03 11:58:03 +0000461 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000462 // Try to generate the stub code if necessary. Do not perform a GC
463 // but instead return a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700464 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::Function* f,
465 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000466
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 // Convenience function: Same as above, but takes the fid instead.
468 void CallRuntime(Runtime::FunctionId id, int num_arguments);
469
Leon Clarkee46be812010-01-19 14:06:41 +0000470 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700471 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
472 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000473
Ben Murdochbb769b22010-08-11 14:56:33 +0100474 // Convenience function: call an external reference.
475 void CallExternalReference(ExternalReference ref, int num_arguments);
476
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100478 // Like JumpToExternalReference, but also takes care of passing the number
479 // of parameters.
480 void TailCallExternalReference(const ExternalReference& ext,
481 int num_arguments,
482 int result_size);
483
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800484 // Tail call of a runtime routine (jump). Try to generate the code if
485 // necessary. Do not perform a GC but instead return a retry after GC failure.
486 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
487 const ExternalReference& ext, int num_arguments, int result_size);
488
Steve Block6ded16b2010-05-10 14:33:55 +0100489 // Convenience function: tail call a runtime routine (jump).
490 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 int num_arguments,
492 int result_size);
493
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800494 // Convenience function: tail call a runtime routine (jump). Try to generate
495 // the code if necessary. Do not perform a GC but instead return a retry after
496 // GC failure.
497 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
498 int num_arguments,
499 int result_size);
500
Steve Block6ded16b2010-05-10 14:33:55 +0100501 // Before calling a C-function from generated code, align arguments on stack.
502 // After aligning the frame, arguments must be stored in esp[0], esp[4],
503 // etc., not pushed. The argument count assumes all arguments are word sized.
504 // Some compilers/platforms require the stack to be aligned when calling
505 // C++ code.
506 // Needs a scratch register to do some arithmetic. This register will be
507 // trashed.
508 void PrepareCallCFunction(int num_arguments, Register scratch);
509
510 // Calls a C function and cleans up the space for arguments allocated
511 // by PrepareCallCFunction. The called function is not allowed to trigger a
512 // garbage collection, since that might move the code and invalidate the
513 // return address (unless this is somehow accounted for by the called
514 // function).
515 void CallCFunction(ExternalReference function, int num_arguments);
516 void CallCFunction(Register function, int num_arguments);
517
John Reck59135872010-11-02 12:39:01 -0700518 // Prepares stack to put arguments (aligns and so on). Reserves
519 // space for return value if needed (assumes the return value is a handle).
520 // Uses callee-saved esi to restore stack state after call. Arguments must be
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800521 // stored in ApiParameterOperand(0), ApiParameterOperand(1) etc. Saves
522 // context (esi).
523 void PrepareCallApiFunction(int argc, Register scratch);
Steve Blockd0582a62009-12-15 09:54:21 +0000524
Russell Brenner90bac252010-11-18 13:33:46 -0800525 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700526 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800527 // Clobbers ebx, edi and caller-save registers. Restores context.
528 // On return removes stack_space * kPointerSize (GCed).
529 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
530 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000531
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100533 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800535 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
536
Steve Blocka7e24c12009-10-30 11:49:00 +0000537
538 // ---------------------------------------------------------------------------
539 // Utilities
540
541 void Ret();
542
Leon Clarkee46be812010-01-19 14:06:41 +0000543 // Emit code to discard a non-negative number of pointer-sized elements
544 // from the stack, clobbering only the esp register.
545 void Drop(int element_count);
546
547 void Call(Label* target) { call(target); }
548
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100549 // Move if the registers are not identical.
550 void Move(Register target, Register source);
551
Leon Clarkee46be812010-01-19 14:06:41 +0000552 void Move(Register target, Handle<Object> value);
553
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 Handle<Object> CodeObject() { return code_object_; }
555
556
557 // ---------------------------------------------------------------------------
558 // StatsCounter support
559
560 void SetCounter(StatsCounter* counter, int value);
561 void IncrementCounter(StatsCounter* counter, int value);
562 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000563 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
564 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000565
566
567 // ---------------------------------------------------------------------------
568 // Debugging
569
570 // Calls Abort(msg) if the condition cc is not satisfied.
571 // Use --debug_code to enable.
572 void Assert(Condition cc, const char* msg);
573
Iain Merrick75681382010-08-19 15:07:18 +0100574 void AssertFastElements(Register elements);
575
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 // Like Assert(), but always enabled.
577 void Check(Condition cc, const char* msg);
578
579 // Print a message to stdout and abort execution.
580 void Abort(const char* msg);
581
Steve Block6ded16b2010-05-10 14:33:55 +0100582 // Check that the stack is aligned.
583 void CheckStackAlignment();
584
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 // Verify restrictions about code generated in stubs.
586 void set_generating_stub(bool value) { generating_stub_ = value; }
587 bool generating_stub() { return generating_stub_; }
588 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
589 bool allow_stub_calls() { return allow_stub_calls_; }
590
Leon Clarked91b9f72010-01-27 17:25:45 +0000591 // ---------------------------------------------------------------------------
592 // String utilities.
593
Andrei Popescu402d9372010-02-26 13:31:12 +0000594 // Check whether the instance type represents a flat ascii string. Jump to the
595 // label if not. If the instance type can be scratched specify same register
596 // for both instance type and scratch.
597 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
598 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100599 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000600
Leon Clarked91b9f72010-01-27 17:25:45 +0000601 // Checks if both objects are sequential ASCII strings, and jumps to label
602 // if either is not.
603 void JumpIfNotBothSequentialAsciiStrings(Register object1,
604 Register object2,
605 Register scratch1,
606 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100607 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000608
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000610 bool generating_stub_;
611 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000612 // This handle will be patched with the code object on installation.
613 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000614
615 // Helper functions for generating invokes.
616 void InvokePrologue(const ParameterCount& expected,
617 const ParameterCount& actual,
618 Handle<Code> code_constant,
619 const Operand& code_operand,
620 Label* done,
621 InvokeFlag flag);
622
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 // Activation support.
624 void EnterFrame(StackFrame::Type type);
625 void LeaveFrame(StackFrame::Type type);
626
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100627 void EnterExitFramePrologue();
628 void EnterExitFrameEpilogue(int argc);
Steve Blockd0582a62009-12-15 09:54:21 +0000629
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800630 void LeaveExitFrameEpilogue();
631
Steve Blocka7e24c12009-10-30 11:49:00 +0000632 // Allocation support helpers.
633 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 Register scratch,
635 AllocationFlags flags);
636 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000637
638 // Helper for PopHandleScope. Allowed to perform a GC and returns
639 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
640 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700641 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
642 Register scratch,
643 bool gc_allowed);
Steve Blocka7e24c12009-10-30 11:49:00 +0000644};
645
646
647// The code patcher is used to patch (typically) small parts of code e.g. for
648// debugging and other types of instrumentation. When using the code patcher
649// the exact number of bytes specified must be emitted. Is not legal to emit
650// relocation information. If any of these constraints are violated it causes
651// an assertion.
652class CodePatcher {
653 public:
654 CodePatcher(byte* address, int size);
655 virtual ~CodePatcher();
656
657 // Macro assembler to emit code.
658 MacroAssembler* masm() { return &masm_; }
659
660 private:
661 byte* address_; // The address of the code being patched.
662 int size_; // Number of bytes of the expected patch size.
663 MacroAssembler masm_; // Macro assembler used to generate the code.
664};
665
666
667// -----------------------------------------------------------------------------
668// Static helper functions.
669
670// Generate an Operand for loading a field from an object.
671static inline Operand FieldOperand(Register object, int offset) {
672 return Operand(object, offset - kHeapObjectTag);
673}
674
675
676// Generate an Operand for loading an indexed field from an object.
677static inline Operand FieldOperand(Register object,
678 Register index,
679 ScaleFactor scale,
680 int offset) {
681 return Operand(object, index, scale, offset - kHeapObjectTag);
682}
683
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800684
685static inline Operand ContextOperand(Register context, int index) {
686 return Operand(context, Context::SlotOffset(index));
687}
688
689
690static inline Operand GlobalObjectOperand() {
691 return ContextOperand(esi, Context::GLOBAL_INDEX);
692}
693
694
John Reck59135872010-11-02 12:39:01 -0700695// Generates an Operand for saving parameters after PrepareCallApiFunction.
696Operand ApiParameterOperand(int index);
697
Steve Blocka7e24c12009-10-30 11:49:00 +0000698
699#ifdef GENERATED_CODE_COVERAGE
700extern void LogGeneratedCodeCoverage(const char* file_line);
701#define CODE_COVERAGE_STRINGIFY(x) #x
702#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
703#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
704#define ACCESS_MASM(masm) { \
705 byte* ia32_coverage_function = \
706 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
707 masm->pushfd(); \
708 masm->pushad(); \
709 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
710 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
711 masm->pop(eax); \
712 masm->popad(); \
713 masm->popfd(); \
714 } \
715 masm->
716#else
717#define ACCESS_MASM(masm) masm->
718#endif
719
720
721} } // namespace v8::internal
722
723#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_