blob: 8c5f5e9421fc57467fe46d979c11c1524c47f36a [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +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 Murdoch85b71792012-04-11 18:30:58 +010065 // For page containing |object| mark region covering |addr| dirty.
66 // RecordWriteHelper only works if the object is not in new
67 // space.
68 void RecordWriteHelper(Register object,
69 Register addr,
70 Register scratch);
Steve Block6ded16b2010-05-10 14:33:55 +010071
Ben Murdoch85b71792012-04-11 18:30:58 +010072 // 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.
77 Label* branch,
78 Label::Distance branch_near = Label::kFar);
Steve Block6ded16b2010-05-10 14:33:55 +010079
Ben Murdoch85b71792012-04-11 18:30:58 +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
Steve Block8defd9f2010-07-08 12:39:36 +010085 // filters out smis so it does not update the write barrier if the
86 // value is a smi.
Ben Murdoch85b71792012-04-11 18:30:58 +010087 void RecordWrite(Register object,
88 int offset,
89 Register value,
90 Register scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +000091
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
Ben Murdoch85b71792012-04-11 18:30:58 +010094 // object being stored. All registers are clobbered by the
Steve Block8defd9f2010-07-08 12:39:36 +010095 // operation. RecordWrite filters out smis so it does not update the
96 // write barrier if the value is a smi.
Ben Murdoch85b71792012-04-11 18:30:58 +010097 void RecordWrite(Register object,
98 Register address,
99 Register value);
Steve Block8defd9f2010-07-08 12:39:36 +0100100
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
Ben Murdoch85b71792012-04-11 18:30:58 +0100108 // ---------------------------------------------------------------------------
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 Murdoch85b71792012-04-11 18:30:58 +0100156 // Setup call kind marking in ecx. The method takes ecx as an
Ben Murdoch257744e2011-11-30 15:57:28 +0000157 // 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
Ben Murdoch85b71792012-04-11 18:30:58 +0100185 void InvokeFunction(JSFunction* function,
Andrei Popescu402d9372010-02-26 13:31:12 +0000186 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
Ben Murdoch85b71792012-04-11 18:30:58 +0100212 // Compare a register against a known root, e.g. undefined, null, true, ...
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000213 void CompareRoot(Register with, Heap::RootListIndex index);
214
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 // Compare object type for heap object.
216 // Incoming register is heap_object and outgoing register is map.
217 void CmpObjectType(Register heap_object, InstanceType type, Register map);
218
219 // Compare instance type for map.
220 void CmpInstanceType(Register map, InstanceType type);
221
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222 // Check if a map for a JSObject indicates that the object has fast elements.
223 // Jump to the specified label if it does not.
224 void CheckFastElements(Register map,
225 Label* fail,
226 Label::Distance distance = Label::kFar);
227
Ben Murdoch257744e2011-11-30 15:57:28 +0000228 // Check if the map of an object is equal to a specified map and branch to
229 // label if not. Skip the smi check if not required (object is known to be a
Ben Murdoch85b71792012-04-11 18:30:58 +0100230 // heap object)
Andrei Popescu31002712010-02-23 13:46:05 +0000231 void CheckMap(Register obj,
232 Handle<Map> map,
233 Label* fail,
Ben Murdoch85b71792012-04-11 18:30:58 +0100234 SmiCheckType smi_check_type);
Ben Murdoch257744e2011-11-30 15:57:28 +0000235
236 // Check if the map of an object is equal to a specified map and branch to a
237 // specified target if equal. Skip the smi check if not required (object is
238 // known to be a heap object)
239 void DispatchMap(Register obj,
240 Handle<Map> map,
241 Handle<Code> success,
242 SmiCheckType smi_check_type);
Andrei Popescu31002712010-02-23 13:46:05 +0000243
Leon Clarkee46be812010-01-19 14:06:41 +0000244 // Check if the object in register heap_object is a string. Afterwards the
245 // register map contains the object map and the register instance_type
246 // contains the instance_type. The registers map and instance_type can be the
247 // same in which case it contains the instance type afterwards. Either of the
248 // registers map and instance_type can be the same as heap_object.
249 Condition IsObjectStringType(Register heap_object,
250 Register map,
251 Register instance_type);
252
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100253 // Check if a heap object's type is in the JSObject range, not including
254 // JSFunction. The object's map will be loaded in the map register.
255 // Any or all of the three registers may be the same.
256 // The contents of the scratch register will always be overwritten.
257 void IsObjectJSObjectType(Register heap_object,
258 Register map,
259 Register scratch,
260 Label* fail);
261
262 // The contents of the scratch register will be overwritten.
263 void IsInstanceJSObjectType(Register map, Register scratch, Label* fail);
264
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 // FCmp is similar to integer cmp, but requires unsigned
266 // jcc instructions (je, ja, jae, jb, jbe, je, and jz).
267 void FCmp();
268
Ben Murdoch257744e2011-11-30 15:57:28 +0000269 void ClampUint8(Register reg);
270
271 void ClampDoubleToUint8(XMMRegister input_reg,
272 XMMRegister scratch_reg,
273 Register result_reg);
274
275
Leon Clarkee46be812010-01-19 14:06:41 +0000276 // Smi tagging support.
277 void SmiTag(Register reg) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000278 STATIC_ASSERT(kSmiTag == 0);
279 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch85b71792012-04-11 18:30:58 +0100280 add(reg, Operand(reg));
Leon Clarkee46be812010-01-19 14:06:41 +0000281 }
282 void SmiUntag(Register reg) {
283 sar(reg, kSmiTagSize);
284 }
285
Iain Merrick75681382010-08-19 15:07:18 +0100286 // Modifies the register even if it does not contain a Smi!
Iain Merrick75681382010-08-19 15:07:18 +0100287 void SmiUntag(Register reg, Label* is_smi) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000288 STATIC_ASSERT(kSmiTagSize == 1);
Iain Merrick75681382010-08-19 15:07:18 +0100289 sar(reg, kSmiTagSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000290 STATIC_ASSERT(kSmiTag == 0);
Iain Merrick75681382010-08-19 15:07:18 +0100291 j(not_carry, is_smi);
292 }
293
Steve Block1e0659c2011-05-24 12:43:12 +0100294 // Jump the register contains a smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000295 inline void JumpIfSmi(Register value,
296 Label* smi_label,
297 Label::Distance distance = Label::kFar) {
Steve Block1e0659c2011-05-24 12:43:12 +0100298 test(value, Immediate(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000299 j(zero, smi_label, distance);
300 }
301 // Jump if the operand is a smi.
302 inline void JumpIfSmi(Operand value,
303 Label* smi_label,
304 Label::Distance distance = Label::kFar) {
305 test(value, Immediate(kSmiTagMask));
306 j(zero, smi_label, distance);
Steve Block1e0659c2011-05-24 12:43:12 +0100307 }
308 // Jump if register contain a non-smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000309 inline void JumpIfNotSmi(Register value,
310 Label* not_smi_label,
311 Label::Distance distance = Label::kFar) {
Steve Block1e0659c2011-05-24 12:43:12 +0100312 test(value, Immediate(kSmiTagMask));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000313 j(not_zero, not_smi_label, distance);
Steve Block1e0659c2011-05-24 12:43:12 +0100314 }
315
Ben Murdoch257744e2011-11-30 15:57:28 +0000316 void LoadInstanceDescriptors(Register map, Register descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100317
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100318 void LoadPowerOf2(XMMRegister dst, Register scratch, int power);
319
Andrei Popescu402d9372010-02-26 13:31:12 +0000320 // Abort execution if argument is not a number. Used in debug code.
Steve Block6ded16b2010-05-10 14:33:55 +0100321 void AbortIfNotNumber(Register object);
322
323 // Abort execution if argument is not a smi. Used in debug code.
324 void AbortIfNotSmi(Register object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000325
Iain Merrick75681382010-08-19 15:07:18 +0100326 // Abort execution if argument is a smi. Used in debug code.
327 void AbortIfSmi(Register object);
328
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100329 // Abort execution if argument is a string. Used in debug code.
330 void AbortIfNotString(Register object);
331
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 // ---------------------------------------------------------------------------
333 // Exception handling
334
Ben Murdoch85b71792012-04-11 18:30:58 +0100335 // Push a new try handler and link into try handler chain. The return
336 // address must be pushed before calling this helper.
337 void PushTryHandler(CodeLocation try_location, HandlerType type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000338
Leon Clarkee46be812010-01-19 14:06:41 +0000339 // Unlink the stack handler on top of the stack from the try handler chain.
340 void PopTryHandler();
Steve Blocka7e24c12009-10-30 11:49:00 +0000341
Ben Murdoch85b71792012-04-11 18:30:58 +0100342 // Activate the top handler in the try hander chain.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100343 void Throw(Register value);
344
Ben Murdoch85b71792012-04-11 18:30:58 +0100345 void ThrowUncatchable(UncatchableExceptionType type, Register value);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 // ---------------------------------------------------------------------------
348 // Inline caching support
349
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 // Generate code for checking access rights - used for security checks
351 // on access to global objects across environments. The holder register
352 // is left untouched, but the scratch register is clobbered.
353 void CheckAccessGlobalProxy(Register holder_reg,
354 Register scratch,
355 Label* miss);
356
Ben Murdochc7cc0282012-03-05 14:35:55 +0000357 void GetNumberHash(Register r0, Register scratch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000359 void LoadFromNumberDictionary(Label* miss,
360 Register elements,
361 Register key,
362 Register r0,
363 Register r1,
364 Register r2,
365 Register result);
366
367
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 // ---------------------------------------------------------------------------
369 // Allocation support
370
371 // Allocate an object in new space. If the new space is exhausted control
372 // continues at the gc_required label. The allocated object is returned in
373 // result and end of the new object is returned in result_end. The register
374 // scratch can be passed as no_reg in which case an additional object
375 // reference will be added to the reloc info. The returned pointers in result
376 // and result_end have not yet been tagged as heap objects. If
Steve Blockd0582a62009-12-15 09:54:21 +0000377 // result_contains_top_on_entry is true the content of result is known to be
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 // the allocation top on entry (could be result_end from a previous call to
379 // AllocateInNewSpace). If result_contains_top_on_entry is true scratch
380 // should be no_reg as it is never used.
381 void AllocateInNewSpace(int object_size,
382 Register result,
383 Register result_end,
384 Register scratch,
385 Label* gc_required,
386 AllocationFlags flags);
387
388 void AllocateInNewSpace(int header_size,
389 ScaleFactor element_size,
390 Register element_count,
391 Register result,
392 Register result_end,
393 Register scratch,
394 Label* gc_required,
395 AllocationFlags flags);
396
397 void AllocateInNewSpace(Register object_size,
398 Register result,
399 Register result_end,
400 Register scratch,
401 Label* gc_required,
402 AllocationFlags flags);
403
404 // Undo allocation in new space. The object passed and objects allocated after
405 // it will no longer be allocated. Make sure that no pointers are left to the
406 // object(s) no longer allocated as they would be invalid when allocation is
407 // un-done.
408 void UndoAllocationInNewSpace(Register object);
409
Steve Block3ce2e202009-11-05 08:53:23 +0000410 // Allocate a heap number in new space with undefined value. The
411 // register scratch2 can be passed as no_reg; the others must be
412 // valid registers. Returns tagged pointer in result register, or
413 // jumps to gc_required if new space is full.
414 void AllocateHeapNumber(Register result,
415 Register scratch1,
416 Register scratch2,
417 Label* gc_required);
418
Steve Blockd0582a62009-12-15 09:54:21 +0000419 // Allocate a sequential string. All the header fields of the string object
420 // are initialized.
421 void AllocateTwoByteString(Register result,
422 Register length,
423 Register scratch1,
424 Register scratch2,
425 Register scratch3,
426 Label* gc_required);
427 void AllocateAsciiString(Register result,
428 Register length,
429 Register scratch1,
430 Register scratch2,
431 Register scratch3,
432 Label* gc_required);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100433 void AllocateAsciiString(Register result,
434 int length,
435 Register scratch1,
436 Register scratch2,
437 Label* gc_required);
Steve Blockd0582a62009-12-15 09:54:21 +0000438
439 // Allocate a raw cons string object. Only the map field of the result is
440 // initialized.
Ben Murdoch589d6972011-11-30 16:04:58 +0000441 void AllocateTwoByteConsString(Register result,
Steve Blockd0582a62009-12-15 09:54:21 +0000442 Register scratch1,
443 Register scratch2,
444 Label* gc_required);
445 void AllocateAsciiConsString(Register result,
446 Register scratch1,
447 Register scratch2,
448 Label* gc_required);
449
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000450 // Allocate a raw sliced string object. Only the map field of the result is
451 // initialized.
Ben Murdoch589d6972011-11-30 16:04:58 +0000452 void AllocateTwoByteSlicedString(Register result,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000453 Register scratch1,
454 Register scratch2,
455 Label* gc_required);
456 void AllocateAsciiSlicedString(Register result,
457 Register scratch1,
458 Register scratch2,
459 Label* gc_required);
460
Ben Murdochb8e0da22011-05-16 14:20:40 +0100461 // Copy memory, byte-by-byte, from source to destination. Not optimized for
462 // long or aligned copies.
463 // The contents of index and scratch are destroyed.
464 void CopyBytes(Register source,
465 Register destination,
466 Register length,
467 Register scratch);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800468
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 // ---------------------------------------------------------------------------
470 // Support functions.
471
472 // Check if result is zero and op is negative.
473 void NegativeZeroTest(Register result, Register op, Label* then_label);
474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 // Check if result is zero and any of op1 and op2 are negative.
476 // Register scratch is destroyed, and it must be different from op2.
477 void NegativeZeroTest(Register result, Register op1, Register op2,
478 Register scratch, Label* then_label);
479
480 // Try to get function prototype of a function and puts the value in
481 // the result register. Checks that the function really is a
482 // function and jumps to the miss label if the fast checks fail. The
483 // function register will be untouched; the other registers may be
484 // clobbered.
485 void TryGetFunctionPrototype(Register function,
486 Register result,
487 Register scratch,
Ben Murdoch85b71792012-04-11 18:30:58 +0100488 Label* miss);
Steve Blocka7e24c12009-10-30 11:49:00 +0000489
490 // Generates code for reporting that an illegal operation has
491 // occurred.
492 void IllegalOperation(int num_arguments);
493
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100494 // Picks out an array index from the hash field.
495 // Register use:
496 // hash - holds the index's hash. Clobbered.
497 // index - holds the overwritten index on exit.
498 void IndexFromHash(Register hash, Register index);
499
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 // ---------------------------------------------------------------------------
501 // Runtime calls
502
Leon Clarkee46be812010-01-19 14:06:41 +0000503 // Call a code stub. Generate the code if necessary.
Ben Murdoch257744e2011-11-30 15:57:28 +0000504 void CallStub(CodeStub* stub, unsigned ast_id = kNoASTId);
Steve Blocka7e24c12009-10-30 11:49:00 +0000505
Ben Murdoch85b71792012-04-11 18:30:58 +0100506 // Call a code stub and return the code object called. Try to generate
507 // the code if necessary. Do not perform a GC but instead return a retry
508 // after GC failure.
509 MUST_USE_RESULT MaybeObject* TryCallStub(CodeStub* stub);
510
Leon Clarkee46be812010-01-19 14:06:41 +0000511 // Tail call a code stub (jump). Generate the code if necessary.
Steve Blockd0582a62009-12-15 09:54:21 +0000512 void TailCallStub(CodeStub* stub);
513
Ben Murdoch85b71792012-04-11 18:30:58 +0100514 // Tail call a code stub (jump) and return the code object called. Try to
515 // generate the code if necessary. Do not perform a GC but instead return
516 // a retry after GC failure.
517 MUST_USE_RESULT MaybeObject* TryTailCallStub(CodeStub* stub);
518
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 // Return from a code stub after popping its arguments.
520 void StubReturn(int argc);
521
522 // Call a runtime routine.
Steve Block44f0eee2011-05-26 01:26:41 +0100523 void CallRuntime(const Runtime::Function* f, int num_arguments);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100524 void CallRuntimeSaveDoubles(Runtime::FunctionId id);
Steve Blocka7e24c12009-10-30 11:49:00 +0000525
Ben Murdoch85b71792012-04-11 18:30:58 +0100526 // Call a runtime function, returning the CodeStub object called.
527 // Try to generate the stub code if necessary. Do not perform a GC
528 // but instead return a retry after GC failure.
529 MUST_USE_RESULT MaybeObject* TryCallRuntime(const Runtime::Function* f,
530 int num_arguments);
531
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 // Convenience function: Same as above, but takes the fid instead.
533 void CallRuntime(Runtime::FunctionId id, int num_arguments);
534
Ben Murdoch85b71792012-04-11 18:30:58 +0100535 // Convenience function: Same as above, but takes the fid instead.
536 MUST_USE_RESULT MaybeObject* TryCallRuntime(Runtime::FunctionId id,
537 int num_arguments);
538
Ben Murdochbb769b22010-08-11 14:56:33 +0100539 // Convenience function: call an external reference.
540 void CallExternalReference(ExternalReference ref, int num_arguments);
541
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100543 // Like JumpToExternalReference, but also takes care of passing the number
544 // of parameters.
545 void TailCallExternalReference(const ExternalReference& ext,
546 int num_arguments,
547 int result_size);
548
Ben Murdoch85b71792012-04-11 18:30:58 +0100549 // Tail call of a runtime routine (jump). Try to generate the code if
550 // necessary. Do not perform a GC but instead return a retry after GC failure.
551 MUST_USE_RESULT MaybeObject* TryTailCallExternalReference(
552 const ExternalReference& ext, int num_arguments, int result_size);
553
Steve Block6ded16b2010-05-10 14:33:55 +0100554 // Convenience function: tail call a runtime routine (jump).
555 void TailCallRuntime(Runtime::FunctionId fid,
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 int num_arguments,
557 int result_size);
558
Ben Murdoch85b71792012-04-11 18:30:58 +0100559 // Convenience function: tail call a runtime routine (jump). Try to generate
560 // the code if necessary. Do not perform a GC but instead return a retry after
561 // GC failure.
562 MUST_USE_RESULT MaybeObject* TryTailCallRuntime(Runtime::FunctionId fid,
563 int num_arguments,
564 int result_size);
565
Steve Block6ded16b2010-05-10 14:33:55 +0100566 // Before calling a C-function from generated code, align arguments on stack.
567 // After aligning the frame, arguments must be stored in esp[0], esp[4],
568 // etc., not pushed. The argument count assumes all arguments are word sized.
569 // Some compilers/platforms require the stack to be aligned when calling
570 // C++ code.
571 // Needs a scratch register to do some arithmetic. This register will be
572 // trashed.
573 void PrepareCallCFunction(int num_arguments, Register scratch);
574
575 // Calls a C function and cleans up the space for arguments allocated
576 // by PrepareCallCFunction. The called function is not allowed to trigger a
577 // garbage collection, since that might move the code and invalidate the
578 // return address (unless this is somehow accounted for by the called
579 // function).
580 void CallCFunction(ExternalReference function, int num_arguments);
581 void CallCFunction(Register function, int num_arguments);
582
John Reck59135872010-11-02 12:39:01 -0700583 // Prepares stack to put arguments (aligns and so on). Reserves
584 // space for return value if needed (assumes the return value is a handle).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000585 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
586 // etc. Saves context (esi). If space was reserved for return value then
587 // stores the pointer to the reserved slot into esi.
588 void PrepareCallApiFunction(int argc);
Steve Blockd0582a62009-12-15 09:54:21 +0000589
Ben Murdoch85b71792012-04-11 18:30:58 +0100590 // Calls an API function. Allocates HandleScope, extracts
591 // returned value from handle and propagates exceptions.
592 // Clobbers ebx, edi and caller-save registers. Restores context.
593 // On return removes stack_space * kPointerSize (GCed).
594 MaybeObject* TryCallApiFunctionAndReturn(ApiFunction* function,
595 int stack_space);
Leon Clarkee46be812010-01-19 14:06:41 +0000596
Steve Blocka7e24c12009-10-30 11:49:00 +0000597 // Jump to a runtime routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100598 void JumpToExternalReference(const ExternalReference& ext);
Steve Blocka7e24c12009-10-30 11:49:00 +0000599
Ben Murdoch85b71792012-04-11 18:30:58 +0100600 MaybeObject* TryJumpToExternalReference(const ExternalReference& ext);
601
602
Steve Blocka7e24c12009-10-30 11:49:00 +0000603 // ---------------------------------------------------------------------------
604 // Utilities
605
606 void Ret();
607
Steve Block1e0659c2011-05-24 12:43:12 +0100608 // Return and drop arguments from stack, where the number of arguments
609 // may be bigger than 2^16 - 1. Requires a scratch register.
610 void Ret(int bytes_dropped, Register scratch);
611
Leon Clarkee46be812010-01-19 14:06:41 +0000612 // Emit code to discard a non-negative number of pointer-sized elements
613 // from the stack, clobbering only the esp register.
614 void Drop(int element_count);
615
616 void Call(Label* target) { call(target); }
617
Ben Murdochb0fe1622011-05-05 13:52:32 +0100618 // Emit call to the code we are currently generating.
619 void CallSelf() {
620 Handle<Code> self(reinterpret_cast<Code**>(CodeObject().location()));
621 call(self, RelocInfo::CODE_TARGET);
622 }
623
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100624 // Move if the registers are not identical.
625 void Move(Register target, Register source);
626
Ben Murdoch85b71792012-04-11 18:30:58 +0100627 void Move(Register target, Handle<Object> value);
628
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000629 // Push a handle value.
Ben Murdoch85b71792012-04-11 18:30:58 +0100630 void Push(Handle<Object> handle) { push(handle); }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000631
Ben Murdoch8b112d22011-06-08 16:22:53 +0100632 Handle<Object> CodeObject() {
633 ASSERT(!code_object_.is_null());
634 return code_object_;
635 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000636
637
638 // ---------------------------------------------------------------------------
639 // StatsCounter support
640
641 void SetCounter(StatsCounter* counter, int value);
642 void IncrementCounter(StatsCounter* counter, int value);
643 void DecrementCounter(StatsCounter* counter, int value);
Leon Clarked91b9f72010-01-27 17:25:45 +0000644 void IncrementCounter(Condition cc, StatsCounter* counter, int value);
645 void DecrementCounter(Condition cc, StatsCounter* counter, int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000646
647
648 // ---------------------------------------------------------------------------
649 // Debugging
650
651 // Calls Abort(msg) if the condition cc is not satisfied.
652 // Use --debug_code to enable.
653 void Assert(Condition cc, const char* msg);
654
Iain Merrick75681382010-08-19 15:07:18 +0100655 void AssertFastElements(Register elements);
656
Steve Blocka7e24c12009-10-30 11:49:00 +0000657 // Like Assert(), but always enabled.
658 void Check(Condition cc, const char* msg);
659
660 // Print a message to stdout and abort execution.
661 void Abort(const char* msg);
662
Steve Block6ded16b2010-05-10 14:33:55 +0100663 // Check that the stack is aligned.
664 void CheckStackAlignment();
665
Steve Blocka7e24c12009-10-30 11:49:00 +0000666 // Verify restrictions about code generated in stubs.
667 void set_generating_stub(bool value) { generating_stub_ = value; }
668 bool generating_stub() { return generating_stub_; }
669 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
670 bool allow_stub_calls() { return allow_stub_calls_; }
671
Leon Clarked91b9f72010-01-27 17:25:45 +0000672 // ---------------------------------------------------------------------------
673 // String utilities.
674
Ben Murdoch85b71792012-04-11 18:30:58 +0100675 // Check whether the instance type represents a flat ascii string. Jump to the
Andrei Popescu402d9372010-02-26 13:31:12 +0000676 // label if not. If the instance type can be scratched specify same register
677 // for both instance type and scratch.
678 void JumpIfInstanceTypeIsNotSequentialAscii(Register instance_type,
679 Register scratch,
Steve Block6ded16b2010-05-10 14:33:55 +0100680 Label* on_not_flat_ascii_string);
Andrei Popescu402d9372010-02-26 13:31:12 +0000681
Leon Clarked91b9f72010-01-27 17:25:45 +0000682 // Checks if both objects are sequential ASCII strings, and jumps to label
683 // if either is not.
684 void JumpIfNotBothSequentialAsciiStrings(Register object1,
685 Register object2,
686 Register scratch1,
687 Register scratch2,
Steve Block6ded16b2010-05-10 14:33:55 +0100688 Label* on_not_flat_ascii_strings);
Leon Clarked91b9f72010-01-27 17:25:45 +0000689
Ben Murdoch8b112d22011-06-08 16:22:53 +0100690 static int SafepointRegisterStackIndex(Register reg) {
691 return SafepointRegisterStackIndex(reg.code());
692 }
693
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000695 bool generating_stub_;
696 bool allow_stub_calls_;
Andrei Popescu31002712010-02-23 13:46:05 +0000697 // This handle will be patched with the code object on installation.
698 Handle<Object> code_object_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000699
700 // Helper functions for generating invokes.
701 void InvokePrologue(const ParameterCount& expected,
702 const ParameterCount& actual,
703 Handle<Code> code_constant,
704 const Operand& code_operand,
Ben Murdoch257744e2011-11-30 15:57:28 +0000705 Label* done,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100706 InvokeFlag flag,
Ben Murdoch85b71792012-04-11 18:30:58 +0100707 Label::Distance done_near = Label::kFar,
Ben Murdoch257744e2011-11-30 15:57:28 +0000708 const CallWrapper& call_wrapper = NullCallWrapper(),
709 CallKind call_kind = CALL_AS_METHOD);
Steve Blocka7e24c12009-10-30 11:49:00 +0000710
Ben Murdoch85b71792012-04-11 18:30:58 +0100711 // Activation support.
712 void EnterFrame(StackFrame::Type type);
713 void LeaveFrame(StackFrame::Type type);
714
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100715 void EnterExitFramePrologue();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100716 void EnterExitFrameEpilogue(int argc, bool save_doubles);
Steve Blockd0582a62009-12-15 09:54:21 +0000717
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800718 void LeaveExitFrameEpilogue();
719
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 // Allocation support helpers.
721 void LoadAllocationTopHelper(Register result,
Steve Blocka7e24c12009-10-30 11:49:00 +0000722 Register scratch,
723 AllocationFlags flags);
724 void UpdateAllocationTopHelper(Register result_end, Register scratch);
Leon Clarkee46be812010-01-19 14:06:41 +0000725
726 // Helper for PopHandleScope. Allowed to perform a GC and returns
727 // NULL if gc_allowed. Does not perform a GC if !gc_allowed, and
728 // possibly returns a failure object indicating an allocation failure.
John Reck59135872010-11-02 12:39:01 -0700729 MUST_USE_RESULT MaybeObject* PopHandleScopeHelper(Register saved,
730 Register scratch,
731 bool gc_allowed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100732
733
734 // Compute memory operands for safepoint stack slots.
735 Operand SafepointRegisterSlot(Register reg);
736 static int SafepointRegisterStackIndex(int reg_code);
737
738 // Needs access to SafepointRegisterStackIndex for optimized frame
739 // traversal.
740 friend class OptimizedFrame;
Steve Blocka7e24c12009-10-30 11:49:00 +0000741};
742
743
744// The code patcher is used to patch (typically) small parts of code e.g. for
745// debugging and other types of instrumentation. When using the code patcher
746// the exact number of bytes specified must be emitted. Is not legal to emit
747// relocation information. If any of these constraints are violated it causes
748// an assertion.
749class CodePatcher {
750 public:
751 CodePatcher(byte* address, int size);
752 virtual ~CodePatcher();
753
754 // Macro assembler to emit code.
755 MacroAssembler* masm() { return &masm_; }
756
757 private:
758 byte* address_; // The address of the code being patched.
759 int size_; // Number of bytes of the expected patch size.
760 MacroAssembler masm_; // Macro assembler used to generate the code.
761};
762
763
764// -----------------------------------------------------------------------------
765// Static helper functions.
766
767// Generate an Operand for loading a field from an object.
Ben Murdoch85b71792012-04-11 18:30:58 +0100768static inline Operand FieldOperand(Register object, int offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000769 return Operand(object, offset - kHeapObjectTag);
770}
771
772
773// Generate an Operand for loading an indexed field from an object.
Ben Murdoch85b71792012-04-11 18:30:58 +0100774static inline Operand FieldOperand(Register object,
775 Register index,
776 ScaleFactor scale,
777 int offset) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000778 return Operand(object, index, scale, offset - kHeapObjectTag);
779}
780
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800781
Ben Murdoch85b71792012-04-11 18:30:58 +0100782static inline Operand ContextOperand(Register context, int index) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800783 return Operand(context, Context::SlotOffset(index));
784}
785
786
Ben Murdoch85b71792012-04-11 18:30:58 +0100787static inline Operand GlobalObjectOperand() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800788 return ContextOperand(esi, Context::GLOBAL_INDEX);
789}
790
791
John Reck59135872010-11-02 12:39:01 -0700792// Generates an Operand for saving parameters after PrepareCallApiFunction.
793Operand ApiParameterOperand(int index);
794
Steve Blocka7e24c12009-10-30 11:49:00 +0000795
796#ifdef GENERATED_CODE_COVERAGE
797extern void LogGeneratedCodeCoverage(const char* file_line);
798#define CODE_COVERAGE_STRINGIFY(x) #x
799#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
800#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
801#define ACCESS_MASM(masm) { \
802 byte* ia32_coverage_function = \
803 reinterpret_cast<byte*>(FUNCTION_ADDR(LogGeneratedCodeCoverage)); \
804 masm->pushfd(); \
805 masm->pushad(); \
806 masm->push(Immediate(reinterpret_cast<int>(&__FILE_LINE__))); \
807 masm->call(ia32_coverage_function, RelocInfo::RUNTIME_ENTRY); \
808 masm->pop(eax); \
809 masm->popad(); \
810 masm->popfd(); \
811 } \
812 masm->
813#else
814#define ACCESS_MASM(masm) masm->
815#endif
816
817
818} } // namespace v8::internal
819
820#endif // V8_IA32_MACRO_ASSEMBLER_IA32_H_