blob: dac22731a95b0ccf9d61da88af568aed8745a479 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_IA32_MACRO_ASSEMBLER_IA32_H_
29#define V8_IA32_MACRO_ASSEMBLER_IA32_H_
30
31#include "assembler.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000032#include "v8globals.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
Ben Murdoch257744e2011-11-30 15:57:28 +000048
Leon Clarkee46be812010-01-19 14:06:41 +000049// Convenience for platform-independent signatures. We do not normally
50// distinguish memory operands from other operands on ia32.
51typedef Operand MemOperand;
52
Steve Blocka7e24c12009-10-30 11:49:00 +000053// MacroAssembler implements a collection of frequently used macros.
54class MacroAssembler: public Assembler {
55 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +010056 // The isolate parameter can be NULL if the macro assembler should
57 // not use isolate-dependent functionality. In this case, it's the
58 // responsibility of the caller to never invoke such function on the
59 // macro assembler.
60 MacroAssembler(Isolate* isolate, void* buffer, int size);
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62 // ---------------------------------------------------------------------------
63 // GC Support
64
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010065 // For page containing |object| mark region covering |addr| dirty.
66 // RecordWriteHelper only works if the object is not in new
Steve Block6ded16b2010-05-10 14:33:55 +010067 // space.
68 void RecordWriteHelper(Register object,
69 Register addr,
70 Register scratch);
71
72 // Check if object is in new space.
73 // scratch can be object itself, but it will be clobbered.
74 void InNewSpace(Register object,
75 Register scratch,
76 Condition cc, // equal for new space, not_equal otherwise.
Ben Murdoch257744e2011-11-30 15:57:28 +000077 Label* branch,
78 Label::Distance branch_near = Label::kFar);
Steve Block6ded16b2010-05-10 14:33:55 +010079
Steve Block8defd9f2010-07-08 12:39:36 +010080 // For page containing |object| mark region covering [object+offset]
81 // dirty. |object| is the object being stored into, |value| is the
82 // object being stored. If offset is zero, then the scratch register
83 // contains the array index into the elements array represented as a
84 // Smi. All registers are clobbered by the operation. RecordWrite
85 // filters out smis so it does not update the write barrier if the
86 // value is a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +000087 void RecordWrite(Register object,
88 int offset,
89 Register value,
90 Register scratch);
91
Steve Block8defd9f2010-07-08 12:39:36 +010092 // For page containing |object| mark region covering |address|
93 // dirty. |object| is the object being stored into, |value| is the
94 // object being stored. All registers are clobbered by the
95 // operation. RecordWrite filters out smis so it does not update the
96 // write barrier if the value is a smi.
97 void RecordWrite(Register object,
98 Register address,
99 Register value);
100
Steve Blocka7e24c12009-10-30 11:49:00 +0000101#ifdef ENABLE_DEBUGGER_SUPPORT
102 // ---------------------------------------------------------------------------
103 // Debugger Support
104
Andrei Popescu402d9372010-02-26 13:31:12 +0000105 void DebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000106#endif
107
108 // ---------------------------------------------------------------------------
109 // Activation frames
110
111 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
112 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
113
114 void EnterConstructFrame() { EnterFrame(StackFrame::CONSTRUCT); }
115 void LeaveConstructFrame() { LeaveFrame(StackFrame::CONSTRUCT); }
116
Ben Murdochb0fe1622011-05-05 13:52:32 +0100117 // Enter specific kind of exit frame. Expects the number of
118 // arguments in register eax and sets up the number of arguments in
119 // register edi and the pointer to the first argument in register
120 // esi.
121 void EnterExitFrame(bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000122
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800123 void EnterApiExitFrame(int argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000124
125 // Leave the current exit frame. Expects the return value in
126 // register eax:edx (untouched) and the pointer to the first
127 // argument in register esi.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100128 void LeaveExitFrame(bool save_doubles);
Steve Blocka7e24c12009-10-30 11:49:00 +0000129
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800130 // Leave the current exit frame. Expects the return value in
131 // register eax (untouched).
132 void LeaveApiExitFrame();
133
Steve Blockd0582a62009-12-15 09:54:21 +0000134 // Find the function context up the context chain.
135 void LoadContext(Register dst, int context_chain_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100137 // Load the global function with the given index.
138 void LoadGlobalFunction(int index, Register function);
139
140 // Load the initial map from the global function. The registers
141 // function and map can be the same.
142 void LoadGlobalFunctionInitialMap(Register function, Register map);
143
Ben Murdochb0fe1622011-05-05 13:52:32 +0100144 // Push and pop the registers that can hold pointers.
145 void PushSafepointRegisters() { pushad(); }
146 void PopSafepointRegisters() { popad(); }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100147 // Store the value in register/immediate src in the safepoint
148 // register stack slot for register dst.
149 void StoreToSafepointRegisterSlot(Register dst, Register src);
150 void StoreToSafepointRegisterSlot(Register dst, Immediate src);
151 void LoadFromSafepointRegisterSlot(Register dst, Register src);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 // ---------------------------------------------------------------------------
154 // JavaScript invokes
155
Ben Murdoch257744e2011-11-30 15:57:28 +0000156 // Setup call kind marking in ecx. The method takes ecx as an
157 // explicit first parameter to make the code more readable at the
158 // call sites.
159 void SetCallKind(Register dst, CallKind kind);
160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 // Invoke the JavaScript function code by either calling or jumping.
162 void InvokeCode(const Operand& code,
163 const ParameterCount& expected,
164 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000166 const CallWrapper& call_wrapper,
167 CallKind call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000168
169 void InvokeCode(Handle<Code> code,
170 const ParameterCount& expected,
171 const ParameterCount& actual,
172 RelocInfo::Mode rmode,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100173 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 const CallWrapper& call_wrapper,
175 CallKind call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
177 // Invoke the JavaScript function in the given register. Changes the
178 // current context to the context in the function before invoking.
179 void InvokeFunction(Register function,
180 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100181 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000182 const CallWrapper& call_wrapper,
183 CallKind call_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
Andrei Popescu402d9372010-02-26 13:31:12 +0000185 void InvokeFunction(JSFunction* function,
186 const ParameterCount& actual,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100187 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000188 const CallWrapper& call_wrapper,
189 CallKind call_kind);
Andrei Popescu402d9372010-02-26 13:31:12 +0000190
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 // Invoke specified builtin JavaScript function. Adds an entry to
192 // the unresolved list if the name does not resolve.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100193 void InvokeBuiltin(Builtins::JavaScript id,
194 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000195 const CallWrapper& call_wrapper = NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
Steve Block791712a2010-08-27 10:21:07 +0100197 // Store the function for the given builtin in the target register.
198 void GetBuiltinFunction(Register target, Builtins::JavaScript id);
199
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 // Store the code object for the given builtin in the target register.
201 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
202
203 // Expression support
204 void Set(Register dst, const Immediate& x);
205 void Set(const Operand& dst, const Immediate& x);
206
Steve Block053d10c2011-06-13 19:13:29 +0100207 // Support for constant splitting.
208 bool IsUnsafeImmediate(const Immediate& x);
209 void SafeSet(Register dst, const Immediate& x);
210 void SafePush(const Immediate& x);
211
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 // Compare object type for heap object.
213 // Incoming register is heap_object and outgoing register is map.
214 void CmpObjectType(Register heap_object, InstanceType type, Register map);
215
216 // Compare instance type for map.
217 void CmpInstanceType(Register map, InstanceType type);
218
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000219 // Check if a map for a JSObject indicates that the object has fast elements.
220 // Jump to the specified label if it does not.
221 void CheckFastElements(Register map,
222 Label* fail,
223 Label::Distance distance = Label::kFar);
224
Ben Murdoch257744e2011-11-30 15:57:28 +0000225 // Check if the map of an object is equal to a specified map and branch to
226 // label if not. Skip the smi check if not required (object is known to be a
227 // heap object)
Andrei Popescu31002712010-02-23 13:46:05 +0000228 void CheckMap(Register obj,
229 Handle<Map> map,
230 Label* fail,
Ben Murdoch257744e2011-11-30 15:57:28 +0000231 SmiCheckType smi_check_type);
232
233 // Check if the map of an object is equal to a specified map and branch to a
234 // specified target if equal. Skip the smi check if not required (object is
235 // known to be a heap object)
236 void DispatchMap(Register obj,
237 Handle<Map> map,
238 Handle<Code> success,
239 SmiCheckType smi_check_type);
Andrei Popescu31002712010-02-23 13:46:05 +0000240
Leon Clarkee46be812010-01-19 14:06:41 +0000241 // Check if the object in register heap_object is a string. Afterwards the
242 // register map contains the object map and the register instance_type
243 // contains the instance_type. The registers map and instance_type can be the
244 // same in which case it contains the instance type afterwards. Either of the
245 // registers map and instance_type can be the same as heap_object.
246 Condition IsObjectStringType(Register heap_object,
247 Register map,
248 Register instance_type);
249
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100250 // Check if a heap object's type is in the JSObject range, not including
251 // JSFunction. The object's map will be loaded in the map register.
252 // Any or all of the three registers may be the same.
253 // The contents of the scratch register will always be overwritten.
254 void IsObjectJSObjectType(Register heap_object,
255 Register map,
256 Register scratch,
257 Label* fail);
258
259 // The contents of the scratch register will be overwritten.
260 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
261
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 // FCmp is similar to integer cmp, but requires unsigned
263 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
264 void FCmp();
265
Ben Murdoch257744e2011-11-30 15:57:28 +0000266 void ClampUint8(Register reg);
267
268 void ClampDoubleToUint8(XMMRegister input_reg,
269 XMMRegister scratch_reg,
270 Register result_reg);
271
272
Leon Clarkee46be812010-01-19 14:06:41 +0000273 // Smi tagging support.
274 void SmiTag(Register reg) {
275 ASSERT(kSmiTag == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100276 ASSERT(kSmiTagSize == 1);
277 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000278 }
279 void SmiUntag(Register reg) {
280 sar(reg, kSmiTagSize);
281 }
282
Iain Merrick75681382010-08-19 15:07:18 +0100283 // Modifies the register even if it does not contain a Smi!
Iain Merrick75681382010-08-19 15:07:18 +0100284 void SmiUntag(Register reg, Label* is_smi) {
285 ASSERT(kSmiTagSize == 1);
286 sar(reg, kSmiTagSize);
287 ASSERT(kSmiTag == 0);
288 j(not_carry, is_smi);
289 }
290
Steve Block1e0659c2011-05-24 12:43:12 +0100291 // Jump the register contains a smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000292 inline void JumpIfSmi(Register value,
293 Label* smi_label,
294 Label::Distance distance = Label::kFar) {
Steve Block1e0659c2011-05-24 12:43:12 +0100295 test(value, Immediate(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000296 j(zero, smi_label, distance);
297 }
298 // Jump if the operand is a smi.
299 inline void JumpIfSmi(Operand value,
300 Label* smi_label,
301 Label::Distance distance = Label::kFar) {
302 test(value, Immediate(kSmiTagMask));
303 j(zero, smi_label, distance);
Steve Block1e0659c2011-05-24 12:43:12 +0100304 }
305 // Jump if register contain a non-smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000306 inline void JumpIfNotSmi(Register value,
307 Label* not_smi_label,
308 Label::Distance distance = Label::kFar) {
Steve Block1e0659c2011-05-24 12:43:12 +0100309 test(value, Immediate(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000310 j(not_zero, not_smi_label, distance);
Steve Block1e0659c2011-05-24 12:43:12 +0100311 }
312
Ben Murdoch257744e2011-11-30 15:57:28 +0000313 void LoadInstanceDescriptors(Register map, Register descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100314
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100315 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
316
Andrei Popescu402d9372010-02-26 13:31:12 +0000317 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100318 void AbortIfNotNumber(Register object);
319
320 // Abort execution if argument is not a smi. Used in debug code.
321 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000322
Iain Merrick75681382010-08-19 15:07:18 +0100323 // Abort execution if argument is a smi. Used in debug code.
324 void AbortIfSmi(Register object);
325
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100326 // Abort execution if argument is a string. Used in debug code.
327 void AbortIfNotString(Register object);
328
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 // ---------------------------------------------------------------------------
330 // Exception handling
331
332 // Push a new try handler and link into try handler chain. The return
333 // address must be pushed before calling this helper.
334 void PushTryHandler(CodeLocation try_location, HandlerType type);
335
Leon Clarkee46be812010-01-19 14:06:41 +0000336 // Unlink the stack handler on top of the stack from the try handler chain.
337 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000338
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100339 // Activate the top handler in the try hander chain.
340 void Throw(Register value);
341
342 void ThrowUncatchable(UncatchableExceptionType type, Register value);
343
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 // ---------------------------------------------------------------------------
345 // Inline caching support
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 // Generate code for checking access rights - used for security checks
348 // on access to global objects across environments. The holder register
349 // is left untouched, but the scratch register is clobbered.
350 void CheckAccessGlobalProxy(Register holder_reg,
351 Register scratch,
352 Label* miss);
353
354
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000355 void LoadFromNumberDictionary(Label* miss,
356 Register elements,
357 Register key,
358 Register r0,
359 Register r1,
360 Register r2,
361 Register result);
362
363
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 // ---------------------------------------------------------------------------
365 // Allocation support
366
367 // Allocate an object in new space. If the new space is exhausted control
368 // continues at the gc_required label. The allocated object is returned in
369 // result and end of the new object is returned in result_end. The register
370 // scratch can be passed as no_reg in which case an additional object
371 // reference will be added to the reloc info. The returned pointers in result
372 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000373 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 // the allocation top on entry (could be result_end from a previous call to
375 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
376 // should be no_reg as it is never used.
377 void AllocateInNewSpace(int object_size,
378 Register result,
379 Register result_end,
380 Register scratch,
381 Label* gc_required,
382 AllocationFlags flags);
383
384 void AllocateInNewSpace(int header_size,
385 ScaleFactor element_size,
386 Register element_count,
387 Register result,
388 Register result_end,
389 Register scratch,
390 Label* gc_required,
391 AllocationFlags flags);
392
393 void AllocateInNewSpace(Register object_size,
394 Register result,
395 Register result_end,
396 Register scratch,
397 Label* gc_required,
398 AllocationFlags flags);
399
400 // Undo allocation in new space. The object passed and objects allocated after
401 // it will no longer be allocated. Make sure that no pointers are left to the
402 // object(s) no longer allocated as they would be invalid when allocation is
403 // un-done.
404 void UndoAllocationInNewSpace(Register object);
405
Steve Block3ce2e202009-11-05 08:53:23 +0000406 // Allocate a heap number in new space with undefined value. The
407 // register scratch2 can be passed as no_reg; the others must be
408 // valid registers. Returns tagged pointer in result register, or
409 // jumps to gc_required if new space is full.
410 void AllocateHeapNumber(Register result,
411 Register scratch1,
412 Register scratch2,
413 Label* gc_required);
414
Steve Blockd0582a62009-12-15 09:54:21 +0000415 // Allocate a sequential string. All the header fields of the string object
416 // are initialized.
417 void AllocateTwoByteString(Register result,
418 Register length,
419 Register scratch1,
420 Register scratch2,
421 Register scratch3,
422 Label* gc_required);
423 void AllocateAsciiString(Register result,
424 Register length,
425 Register scratch1,
426 Register scratch2,
427 Register scratch3,
428 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100429 void AllocateAsciiString(Register result,
430 int length,
431 Register scratch1,
432 Register scratch2,
433 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000434
435 // Allocate a raw cons string object. Only the map field of the result is
436 // initialized.
437 void AllocateConsString(Register result,
438 Register scratch1,
439 Register scratch2,
440 Label* gc_required);
441 void AllocateAsciiConsString(Register result,
442 Register scratch1,
443 Register scratch2,
444 Label* gc_required);
445
Ben Murdochb8e0da22011-05-16 14:20:40 +0100446 // Copy memory, byte-by-byte, from source to destination. Not optimized for
447 // long or aligned copies.
448 // The contents of index and scratch are destroyed.
449 void CopyBytes(Register source,
450 Register destination,
451 Register length,
452 Register scratch);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800453
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 // ---------------------------------------------------------------------------
455 // Support functions.
456
457 // Check if result is zero and op is negative.
458 void NegativeZeroTest(Register result, Register op, Label* then_label);
459
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 // Check if result is zero and any of op1 and op2 are negative.
461 // Register scratch is destroyed, and it must be different from op2.
462 void NegativeZeroTest(Register result, Register op1, Register op2,
463 Register scratch, Label* then_label);
464
465 // Try to get function prototype of a function and puts the value in
466 // the result register. Checks that the function really is a
467 // function and jumps to the miss label if the fast checks fail. The
468 // function register will be untouched; the other registers may be
469 // clobbered.
470 void TryGetFunctionPrototype(Register function,
471 Register result,
472 Register scratch,
473 Label* miss);
474
475 // Generates code for reporting that an illegal operation has
476 // occurred.
477 void IllegalOperation(int num_arguments);
478
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100479 // Picks out an array index from the hash field.
480 // Register use:
481 // hash - holds the index's hash. Clobbered.
482 // index - holds the overwritten index on exit.
483 void IndexFromHash(Register hash, Register index);
484
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 // ---------------------------------------------------------------------------
486 // Runtime calls
487
Leon Clarkee46be812010-01-19 14:06:41 +0000488 // Call a code stub. Generate the code if necessary.
Ben Murdoch257744e2011-11-30 15:57:28 +0000489 void CallStub(CodeStub* stub, unsigned ast_id = kNoASTId);
Steve Blocka7e24c12009-10-30 11:49:00 +0000490
Leon Clarkee46be812010-01-19 14:06:41 +0000491 // Call a code stub and return the code object called. Try to generate
492 // the code if necessary. Do not perform a GC but instead return a retry
493 // after GC failure.
John Reck59135872010-11-02 12:39:01 -0700494 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000495
496 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000497 void TailCallStub(CodeStub* stub);
498
Leon Clarkee46be812010-01-19 14:06:41 +0000499 // Tail call a code stub (jump) and return the code object called. Try to
500 // generate the code if necessary. Do not perform a GC but instead return
501 // a retry after GC failure.
John Reck59135872010-11-02 12:39:01 -0700502 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
Leon Clarkee46be812010-01-19 14:06:41 +0000503
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 // Return from a code stub after popping its arguments.
505 void StubReturn(int argc);
506
507 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100508 void CallRuntime(const Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100509 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
Leon Clarke4515c472010-02-03 11:58:03 +0000511 // Call a runtime function, returning the CodeStub object called.
Leon Clarkee46be812010-01-19 14:06:41 +0000512 // Try to generate the stub code if necessary. Do not perform a GC
513 // but instead return a retry after GC failure.
Steve Block44f0eee2011-05-26 01:26:41 +0100514 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
John Reck59135872010-11-02 12:39:01 -0700515 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000516
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 // Convenience function: Same as above, but takes the fid instead.
518 void CallRuntime(Runtime::FunctionId id, int num_arguments);
519
Leon Clarkee46be812010-01-19 14:06:41 +0000520 // Convenience function: Same as above, but takes the fid instead.
John Reck59135872010-11-02 12:39:01 -0700521 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
522 int num_arguments);
Leon Clarkee46be812010-01-19 14:06:41 +0000523
Ben Murdochbb769b22010-08-11 14:56:33 +0100524 // Convenience function: call an external reference.
525 void CallExternalReference(ExternalReference ref, int num_arguments);
526
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100528 // Like JumpToExternalReference, but also takes care of passing the number
529 // of parameters.
530 void TailCallExternalReference(const ExternalReference& ext,
531 int num_arguments,
532 int result_size);
533
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800534 // Tail call of a runtime routine (jump). Try to generate the code if
535 // necessary. Do not perform a GC but instead return a retry after GC failure.
536 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
537 const ExternalReference& ext, int num_arguments, int result_size);
538
Steve Block6ded16b2010-05-10 14:33:55 +0100539 // Convenience function: tail call a runtime routine (jump).
540 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000541 int num_arguments,
542 int result_size);
543
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800544 // Convenience function: tail call a runtime routine (jump). Try to generate
545 // the code if necessary. Do not perform a GC but instead return a retry after
546 // GC failure.
547 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
548 int num_arguments,
549 int result_size);
550
Steve Block6ded16b2010-05-10 14:33:55 +0100551 // Before calling a C-function from generated code, align arguments on stack.
552 // After aligning the frame, arguments must be stored in esp[0], esp[4],
553 // etc., not pushed. The argument count assumes all arguments are word sized.
554 // Some compilers/platforms require the stack to be aligned when calling
555 // C++ code.
556 // Needs a scratch register to do some arithmetic. This register will be
557 // trashed.
558 void PrepareCallCFunction(int num_arguments, Register scratch);
559
560 // Calls a C function and cleans up the space for arguments allocated
561 // by PrepareCallCFunction. The called function is not allowed to trigger a
562 // garbage collection, since that might move the code and invalidate the
563 // return address (unless this is somehow accounted for by the called
564 // function).
565 void CallCFunction(ExternalReference function, int num_arguments);
566 void CallCFunction(Register function, int num_arguments);
567
John Reck59135872010-11-02 12:39:01 -0700568 // Prepares stack to put arguments (aligns and so on). Reserves
569 // space for return value if needed (assumes the return value is a handle).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000570 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
571 // etc. Saves context (esi). If space was reserved for return value then
572 // stores the pointer to the reserved slot into esi.
573 void PrepareCallApiFunction(int argc);
Steve Blockd0582a62009-12-15 09:54:21 +0000574
Russell Brenner90bac252010-11-18 13:33:46 -0800575 // Calls an API function. Allocates HandleScope, extracts
John Reck59135872010-11-02 12:39:01 -0700576 // returned value from handle and propagates exceptions.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800577 // Clobbers ebx, edi and caller-save registers. Restores context.
578 // On return removes stack_space * kPointerSize (GCed).
579 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
580 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000581
Steve Blocka7e24c12009-10-30 11:49:00 +0000582 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100583 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000584
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800585 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
586
Steve Blocka7e24c12009-10-30 11:49:00 +0000587
588 // ---------------------------------------------------------------------------
589 // Utilities
590
591 void Ret();
592
Steve Block1e0659c2011-05-24 12:43:12 +0100593 // Return and drop arguments from stack, where the number of arguments
594 // may be bigger than 2^16 - 1. Requires a scratch register.
595 void Ret(int bytes_dropped, Register scratch);
596
Leon Clarkee46be812010-01-19 14:06:41 +0000597 // Emit code to discard a non-negative number of pointer-sized elements
598 // from the stack, clobbering only the esp register.
599 void Drop(int element_count);
600
601 void Call(Label* target) { call(target); }
602
Ben Murdochb0fe1622011-05-05 13:52:32 +0100603 // Emit call to the code we are currently generating.
604 void CallSelf() {
605 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
606 call(self, RelocInfo::CODE_TARGET);
607 }
608
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100609 // Move if the registers are not identical.
610 void Move(Register target, Register source);
611
Leon Clarkee46be812010-01-19 14:06:41 +0000612 void Move(Register target, Handle<Object> value);
613
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000614 // Push a handle value.
615 void Push(Handle<Object> handle) { push(handle); }
616
Ben Murdoch8b112d22011-06-08 16:22:53 +0100617 Handle<Object> CodeObject() {
618 ASSERT(!code_object_.is_null());
619 return code_object_;
620 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000621
622
623 // ---------------------------------------------------------------------------
624 // StatsCounter support
625
626 void SetCounter(StatsCounter* counter, int value);
627 void IncrementCounter(StatsCounter* counter, int value);
628 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000629 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
630 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000631
632
633 // ---------------------------------------------------------------------------
634 // Debugging
635
636 // Calls Abort(msg) if the condition cc is not satisfied.
637 // Use --debug_code to enable.
638 void Assert(Condition cc, const char* msg);
639
Iain Merrick75681382010-08-19 15:07:18 +0100640 void AssertFastElements(Register elements);
641
Steve Blocka7e24c12009-10-30 11:49:00 +0000642 // Like Assert(), but always enabled.
643 void Check(Condition cc, const char* msg);
644
645 // Print a message to stdout and abort execution.
646 void Abort(const char* msg);
647
Steve Block6ded16b2010-05-10 14:33:55 +0100648 // Check that the stack is aligned.
649 void CheckStackAlignment();
650
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 // Verify restrictions about code generated in stubs.
652 void set_generating_stub(bool value) { generating_stub_ = value; }
653 bool generating_stub() { return generating_stub_; }
654 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
655 bool allow_stub_calls() { return allow_stub_calls_; }
656
Leon Clarked91b9f72010-01-27 17:25:45 +0000657 // ---------------------------------------------------------------------------
658 // String utilities.
659
Andrei Popescu402d9372010-02-26 13:31:12 +0000660 // Check whether the instance type represents a flat ascii string. Jump to the
661 // label if not. If the instance type can be scratched specify same register
662 // for both instance type and scratch.
663 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
664 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100665 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000666
Leon Clarked91b9f72010-01-27 17:25:45 +0000667 // Checks if both objects are sequential ASCII strings, and jumps to label
668 // if either is not.
669 void JumpIfNotBothSequentialAsciiStrings(Register object1,
670 Register object2,
671 Register scratch1,
672 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100673 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000674
Ben Murdoch8b112d22011-06-08 16:22:53 +0100675 static int SafepointRegisterStackIndex(Register reg) {
676 return SafepointRegisterStackIndex(reg.code());
677 }
678
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000680 bool generating_stub_;
681 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000682 // This handle will be patched with the code object on installation.
683 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000684
685 // Helper functions for generating invokes.
686 void InvokePrologue(const ParameterCount& expected,
687 const ParameterCount& actual,
688 Handle<Code> code_constant,
689 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +0000690 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100691 InvokeFlag flag,
Ben Murdoch257744e2011-11-30 15:57:28 +0000692 Label::Distance done_near = Label::kFar,
693 const CallWrapper& call_wrapper = NullCallWrapper(),
694 CallKind call_kind = CALL_AS_METHOD);
Steve Blocka7e24c12009-10-30 11:49:00 +0000695
Steve Blocka7e24c12009-10-30 11:49:00 +0000696 // Activation support.
697 void EnterFrame(StackFrame::Type type);
698 void LeaveFrame(StackFrame::Type type);
699
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100700 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100701 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000702
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800703 void LeaveExitFrameEpilogue();
704
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 // Allocation support helpers.
706 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 Register scratch,
708 AllocationFlags flags);
709 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000710
711 // Helper for PopHandleScope. Allowed to perform a GC and returns
712 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
713 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700714 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
715 Register scratch,
716 bool gc_allowed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100717
718
719 // Compute memory operands for safepoint stack slots.
720 Operand SafepointRegisterSlot(Register reg);
721 static int SafepointRegisterStackIndex(int reg_code);
722
723 // Needs access to SafepointRegisterStackIndex for optimized frame
724 // traversal.
725 friend class OptimizedFrame;
Steve Blocka7e24c12009-10-30 11:49:00 +0000726};
727
728
729// The code patcher is used to patch (typically) small parts of code e.g. for
730// debugging and other types of instrumentation. When using the code patcher
731// the exact number of bytes specified must be emitted. Is not legal to emit
732// relocation information. If any of these constraints are violated it causes
733// an assertion.
734class CodePatcher {
735 public:
736 CodePatcher(byte* address, int size);
737 virtual ~CodePatcher();
738
739 // Macro assembler to emit code.
740 MacroAssembler* masm() { return &masm_; }
741
742 private:
743 byte* address_; // The address of the code being patched.
744 int size_; // Number of bytes of the expected patch size.
745 MacroAssembler masm_; // Macro assembler used to generate the code.
746};
747
748
749// -----------------------------------------------------------------------------
750// Static helper functions.
751
752// Generate an Operand for loading a field from an object.
753static inline Operand FieldOperand(Register object, int offset) {
754 return Operand(object, offset - kHeapObjectTag);
755}
756
757
758// Generate an Operand for loading an indexed field from an object.
759static inline Operand FieldOperand(Register object,
760 Register index,
761 ScaleFactor scale,
762 int offset) {
763 return Operand(object, index, scale, offset - kHeapObjectTag);
764}
765
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800766
767static inline Operand ContextOperand(Register context, int index) {
768 return Operand(context, Context::SlotOffset(index));
769}
770
771
772static inline Operand GlobalObjectOperand() {
773 return ContextOperand(esi, Context::GLOBAL_INDEX);
774}
775
776
John Reck59135872010-11-02 12:39:01 -0700777// Generates an Operand for saving parameters after PrepareCallApiFunction.
778Operand ApiParameterOperand(int index);
779
Steve Blocka7e24c12009-10-30 11:49:00 +0000780
781#ifdef GENERATED_CODE_COVERAGE
782extern void LogGeneratedCodeCoverage(const char* file_line);
783#define CODE_COVERAGE_STRINGIFY(x) #x
784#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
785#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
786#define ACCESS_MASM(masm) { \
787 byte* ia32_coverage_function = \
788 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
789 masm->pushfd(); \
790 masm->pushad(); \
791 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
792 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
793 masm->pop(eax); \
794 masm->popad(); \
795 masm->popfd(); \
796 } \
797 masm->
798#else
799#define ACCESS_MASM(masm) masm->
800#endif
801
802
803} } // namespace v8::internal
804
805#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_