blob: 0f0365b7c6c6cc77b356f8ddd0e43b4451a72da9 [file] [log] [blame]
Andrei Popescu31002712010-02-23 13:46:05 +00001// Copyright 2010 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_MIPS_MACRO_ASSEMBLER_MIPS_H_
29#define V8_MIPS_MACRO_ASSEMBLER_MIPS_H_
30
31#include "assembler.h"
32#include "mips/assembler-mips.h"
33
34namespace v8 {
35namespace internal {
36
37// Forward declaration.
38class JumpTarget;
39
40// Register at is used for instruction generation. So it is not safe to use it
41// unless we know exactly what we do.
42
43// Registers aliases
Steve Block6ded16b2010-05-10 14:33:55 +010044// cp is assumed to be a callee saved register.
Andrei Popescu31002712010-02-23 13:46:05 +000045const Register cp = s7; // JavaScript context pointer
46const Register fp = s8_fp; // Alias fp
47
48enum InvokeJSFlags {
49 CALL_JS,
50 JUMP_JS
51};
52
53// MacroAssembler implements a collection of frequently used macros.
54class MacroAssembler: public Assembler {
55 public:
56 MacroAssembler(void* buffer, int size);
57
58 // Jump, Call, and Ret pseudo instructions implementing inter-working.
59 void Jump(const Operand& target,
60 Condition cond = cc_always,
61 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
62 void Call(const Operand& target,
63 Condition cond = cc_always,
64 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
65 void Jump(Register target,
66 Condition cond = cc_always,
67 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
68 void Jump(byte* target, RelocInfo::Mode rmode,
69 Condition cond = cc_always,
70 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
71 void Jump(Handle<Code> code, RelocInfo::Mode rmode,
72 Condition cond = cc_always,
73 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
74 void Call(Register target,
75 Condition cond = cc_always,
76 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
77 void Call(byte* target, RelocInfo::Mode rmode,
78 Condition cond = cc_always,
79 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
80 void Call(Handle<Code> code, RelocInfo::Mode rmode,
81 Condition cond = cc_always,
82 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
83 void Ret(Condition cond = cc_always,
84 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
85 void Branch(Condition cond, int16_t offset, Register rs = zero_reg,
86 const Operand& rt = Operand(zero_reg), Register scratch = at);
87 void Branch(Condition cond, Label* L, Register rs = zero_reg,
88 const Operand& rt = Operand(zero_reg), Register scratch = at);
89 // conditionnal branch and link
90 void BranchAndLink(Condition cond, int16_t offset, Register rs = zero_reg,
91 const Operand& rt = Operand(zero_reg),
92 Register scratch = at);
93 void BranchAndLink(Condition cond, Label* L, Register rs = zero_reg,
94 const Operand& rt = Operand(zero_reg),
95 Register scratch = at);
96
97 // Emit code to discard a non-negative number of pointer-sized elements
98 // from the stack, clobbering only the sp register.
99 void Drop(int count, Condition cond = cc_always);
100
101 void Call(Label* target);
102
103 // Jump unconditionally to given label.
104 // We NEED a nop in the branch delay slot, as it used by v8, for example in
105 // CodeGenerator::ProcessDeferred().
Steve Block6ded16b2010-05-10 14:33:55 +0100106 // Currently the branch delay slot is filled by the MacroAssembler.
Andrei Popescu31002712010-02-23 13:46:05 +0000107 // Use rather b(Label) for code generation.
108 void jmp(Label* L) {
109 Branch(cc_always, L);
Andrei Popescu31002712010-02-23 13:46:05 +0000110 }
111
112 // Load an object from the root table.
113 void LoadRoot(Register destination,
114 Heap::RootListIndex index);
115 void LoadRoot(Register destination,
116 Heap::RootListIndex index,
117 Condition cond, Register src1, const Operand& src2);
118
Steve Block6ded16b2010-05-10 14:33:55 +0100119 // Load an external reference.
120 void LoadExternalReference(Register reg, ExternalReference ext) {
121 li(reg, Operand(ext));
122 }
123
124 // Sets the remembered set bit for [address+offset].
Andrei Popescu31002712010-02-23 13:46:05 +0000125 void RecordWrite(Register object, Register offset, Register scratch);
126
127
128 // ---------------------------------------------------------------------------
129 // Instruction macros
130
131#define DEFINE_INSTRUCTION(instr) \
132 void instr(Register rd, Register rs, const Operand& rt); \
133 void instr(Register rd, Register rs, Register rt) { \
134 instr(rd, rs, Operand(rt)); \
135 } \
136 void instr(Register rs, Register rt, int32_t j) { \
137 instr(rs, rt, Operand(j)); \
138 }
139
140#define DEFINE_INSTRUCTION2(instr) \
141 void instr(Register rs, const Operand& rt); \
142 void instr(Register rs, Register rt) { \
143 instr(rs, Operand(rt)); \
144 } \
145 void instr(Register rs, int32_t j) { \
146 instr(rs, Operand(j)); \
147 }
148
149 DEFINE_INSTRUCTION(Add);
150 DEFINE_INSTRUCTION(Addu);
151 DEFINE_INSTRUCTION(Mul);
152 DEFINE_INSTRUCTION2(Mult);
153 DEFINE_INSTRUCTION2(Multu);
154 DEFINE_INSTRUCTION2(Div);
155 DEFINE_INSTRUCTION2(Divu);
156
157 DEFINE_INSTRUCTION(And);
158 DEFINE_INSTRUCTION(Or);
159 DEFINE_INSTRUCTION(Xor);
160 DEFINE_INSTRUCTION(Nor);
161
162 DEFINE_INSTRUCTION(Slt);
163 DEFINE_INSTRUCTION(Sltu);
164
165#undef DEFINE_INSTRUCTION
166#undef DEFINE_INSTRUCTION2
167
168
169 //------------Pseudo-instructions-------------
170
171 void mov(Register rd, Register rt) { or_(rd, rt, zero_reg); }
172 // Move the logical ones complement of source to dest.
173 void movn(Register rd, Register rt);
174
175
176 // load int32 in the rd register
177 void li(Register rd, Operand j, bool gen2instr = false);
178 inline void li(Register rd, int32_t j, bool gen2instr = false) {
179 li(rd, Operand(j), gen2instr);
180 }
181
182 // Exception-generating instructions and debugging support
183 void stop(const char* msg);
184
185
186 // Push multiple registers on the stack.
Steve Block6ded16b2010-05-10 14:33:55 +0100187 // Registers are saved in numerical order, with higher numbered registers
188 // saved in higher memory addresses
Andrei Popescu31002712010-02-23 13:46:05 +0000189 void MultiPush(RegList regs);
190 void MultiPushReversed(RegList regs);
191 void Push(Register src) {
192 Addu(sp, sp, Operand(-kPointerSize));
193 sw(src, MemOperand(sp, 0));
194 }
195 inline void push(Register src) { Push(src); }
196
197 void Push(Register src, Condition cond, Register tst1, Register tst2) {
198 // Since we don't have conditionnal execution we use a Branch.
199 Branch(cond, 3, tst1, Operand(tst2));
Andrei Popescu31002712010-02-23 13:46:05 +0000200 Addu(sp, sp, Operand(-kPointerSize));
201 sw(src, MemOperand(sp, 0));
202 }
203
204 // Pops multiple values from the stack and load them in the
205 // registers specified in regs. Pop order is the opposite as in MultiPush.
206 void MultiPop(RegList regs);
207 void MultiPopReversed(RegList regs);
208 void Pop(Register dst) {
209 lw(dst, MemOperand(sp, 0));
210 Addu(sp, sp, Operand(kPointerSize));
211 }
212 void Pop() {
213 Add(sp, sp, Operand(kPointerSize));
214 }
215
216
217 // ---------------------------------------------------------------------------
Steve Block6ded16b2010-05-10 14:33:55 +0100218 // Activation frames
219
220 void EnterInternalFrame() { EnterFrame(StackFrame::INTERNAL); }
221 void LeaveInternalFrame() { LeaveFrame(StackFrame::INTERNAL); }
222
223 // Enter specific kind of exit frame; either EXIT or
224 // EXIT_DEBUG. Expects the number of arguments in register a0 and
225 // the builtin function to call in register a1.
226 // On output hold_argc, hold_function, and hold_argv are setup.
227 void EnterExitFrame(ExitFrame::Mode mode,
228 Register hold_argc,
229 Register hold_argv,
230 Register hold_function);
231
232 // Leave the current exit frame. Expects the return value in v0.
233 void LeaveExitFrame(ExitFrame::Mode mode);
234
235 // Align the stack by optionally pushing a Smi zero.
236 void AlignStack(int offset);
237
238 void SetupAlignedCall(Register scratch, int arg_count = 0);
239 void ReturnFromAlignedCall();
240
241
242 // ---------------------------------------------------------------------------
243 // JavaScript invokes
244
245 // Invoke the JavaScript function code by either calling or jumping.
246 void InvokeCode(Register code,
247 const ParameterCount& expected,
248 const ParameterCount& actual,
249 InvokeFlag flag);
250
251 void InvokeCode(Handle<Code> code,
252 const ParameterCount& expected,
253 const ParameterCount& actual,
254 RelocInfo::Mode rmode,
255 InvokeFlag flag);
256
257 // Invoke the JavaScript function in the given register. Changes the
258 // current context to the context in the function before invoking.
259 void InvokeFunction(Register function,
260 const ParameterCount& actual,
261 InvokeFlag flag);
262
263
264#ifdef ENABLE_DEBUGGER_SUPPORT
265 // ---------------------------------------------------------------------------
266 // Debugger Support
267
268 void SaveRegistersToMemory(RegList regs);
269 void RestoreRegistersFromMemory(RegList regs);
270 void CopyRegistersFromMemoryToStack(Register base, RegList regs);
271 void CopyRegistersFromStackToMemory(Register base,
272 Register scratch,
273 RegList regs);
274 void DebugBreak();
275#endif
276
277
278 // ---------------------------------------------------------------------------
Andrei Popescu31002712010-02-23 13:46:05 +0000279 // Exception handling
280
281 // Push a new try handler and link into try handler chain.
Steve Block6ded16b2010-05-10 14:33:55 +0100282 // The return address must be passed in register ra.
Andrei Popescu31002712010-02-23 13:46:05 +0000283 void PushTryHandler(CodeLocation try_location, HandlerType type);
284
285 // Unlink the stack handler on top of the stack from the try handler chain.
286 // Must preserve the result register.
287 void PopTryHandler();
288
289
290 // ---------------------------------------------------------------------------
291 // Support functions.
292
Steve Block6ded16b2010-05-10 14:33:55 +0100293 void GetObjectType(Register function,
294 Register map,
295 Register type_reg);
296
Andrei Popescu31002712010-02-23 13:46:05 +0000297 inline void BranchOnSmi(Register value, Label* smi_label,
298 Register scratch = at) {
299 ASSERT_EQ(0, kSmiTag);
300 andi(scratch, value, kSmiTagMask);
301 Branch(eq, smi_label, scratch, Operand(zero_reg));
302 }
303
304
305 inline void BranchOnNotSmi(Register value, Label* not_smi_label,
306 Register scratch = at) {
307 ASSERT_EQ(0, kSmiTag);
308 andi(scratch, value, kSmiTagMask);
309 Branch(ne, not_smi_label, scratch, Operand(zero_reg));
310 }
311
Steve Block6ded16b2010-05-10 14:33:55 +0100312 void CallBuiltin(ExternalReference builtin_entry);
313 void CallBuiltin(Register target);
314 void JumpToBuiltin(ExternalReference builtin_entry);
315 void JumpToBuiltin(Register target);
316
317 // Generates code for reporting that an illegal operation has
318 // occurred.
319 void IllegalOperation(int num_arguments);
320
Andrei Popescu31002712010-02-23 13:46:05 +0000321
322 // ---------------------------------------------------------------------------
323 // Runtime calls
324
325 // Call a code stub.
326 void CallStub(CodeStub* stub, Condition cond = cc_always,
327 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
328 void CallJSExitStub(CodeStub* stub);
329
330 // Return from a code stub after popping its arguments.
331 void StubReturn(int argc);
332
333 // Call a runtime routine.
Andrei Popescu31002712010-02-23 13:46:05 +0000334 void CallRuntime(Runtime::Function* f, int num_arguments);
335
336 // Convenience function: Same as above, but takes the fid instead.
337 void CallRuntime(Runtime::FunctionId fid, int num_arguments);
338
339 // Tail call of a runtime routine (jump).
Steve Block6ded16b2010-05-10 14:33:55 +0100340 // Like JumpToExternalReference, but also takes care of passing the number
Andrei Popescu31002712010-02-23 13:46:05 +0000341 // of parameters.
Steve Block6ded16b2010-05-10 14:33:55 +0100342 void TailCallExternalReference(const ExternalReference& ext,
343 int num_arguments,
344 int result_size);
345
346 // Convenience function: tail call a runtime routine (jump).
347 void TailCallRuntime(Runtime::FunctionId fid,
Andrei Popescu31002712010-02-23 13:46:05 +0000348 int num_arguments,
349 int result_size);
350
351 // Jump to the builtin routine.
Steve Block6ded16b2010-05-10 14:33:55 +0100352 void JumpToExternalReference(const ExternalReference& builtin);
Andrei Popescu31002712010-02-23 13:46:05 +0000353
354 // Invoke specified builtin JavaScript function. Adds an entry to
355 // the unresolved list if the name does not resolve.
356 void InvokeBuiltin(Builtins::JavaScript id, InvokeJSFlags flags);
357
358 // Store the code object for the given builtin in the target register and
359 // setup the function in r1.
360 void GetBuiltinEntry(Register target, Builtins::JavaScript id);
361
362 struct Unresolved {
363 int pc;
364 uint32_t flags; // see Bootstrapper::FixupFlags decoders/encoders.
365 const char* name;
366 };
367 List<Unresolved>* unresolved() { return &unresolved_; }
368
369 Handle<Object> CodeObject() { return code_object_; }
370
371
372 // ---------------------------------------------------------------------------
373 // Stack limit support
374
375 void StackLimitCheck(Label* on_stack_limit_hit);
376
377
378 // ---------------------------------------------------------------------------
379 // StatsCounter support
380
381 void SetCounter(StatsCounter* counter, int value,
382 Register scratch1, Register scratch2);
383 void IncrementCounter(StatsCounter* counter, int value,
384 Register scratch1, Register scratch2);
385 void DecrementCounter(StatsCounter* counter, int value,
386 Register scratch1, Register scratch2);
387
388
389 // ---------------------------------------------------------------------------
390 // Debugging
391
392 // Calls Abort(msg) if the condition cc is not satisfied.
393 // Use --debug_code to enable.
394 void Assert(Condition cc, const char* msg, Register rs, Operand rt);
395
396 // Like Assert(), but always enabled.
397 void Check(Condition cc, const char* msg, Register rs, Operand rt);
398
399 // Print a message to stdout and abort execution.
400 void Abort(const char* msg);
401
402 // Verify restrictions about code generated in stubs.
403 void set_generating_stub(bool value) { generating_stub_ = value; }
404 bool generating_stub() { return generating_stub_; }
405 void set_allow_stub_calls(bool value) { allow_stub_calls_ = value; }
406 bool allow_stub_calls() { return allow_stub_calls_; }
407
408 private:
Andrei Popescu31002712010-02-23 13:46:05 +0000409 List<Unresolved> unresolved_;
410 bool generating_stub_;
411 bool allow_stub_calls_;
412 // This handle will be patched with the code object on installation.
413 Handle<Object> code_object_;
Steve Block6ded16b2010-05-10 14:33:55 +0100414
415 void Jump(intptr_t target, RelocInfo::Mode rmode, Condition cond = cc_always,
416 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
417 void Call(intptr_t target, RelocInfo::Mode rmode, Condition cond = cc_always,
418 Register r1 = zero_reg, const Operand& r2 = Operand(zero_reg));
419
420 // Helper functions for generating invokes.
421 void InvokePrologue(const ParameterCount& expected,
422 const ParameterCount& actual,
423 Handle<Code> code_constant,
424 Register code_reg,
425 Label* done,
426 InvokeFlag flag);
427
428 // Get the code for the given builtin. Returns if able to resolve
429 // the function in the 'resolved' flag.
430 Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
431
432 // Activation support.
433 // EnterFrame clobbers t0 and t1.
434 void EnterFrame(StackFrame::Type type);
435 void LeaveFrame(StackFrame::Type type);
Andrei Popescu31002712010-02-23 13:46:05 +0000436};
437
438
439// -----------------------------------------------------------------------------
440// Static helper functions.
441
442// Generate a MemOperand for loading a field from an object.
443static inline MemOperand FieldMemOperand(Register object, int offset) {
444 return MemOperand(object, offset - kHeapObjectTag);
445}
446
447
448
449#ifdef GENERATED_CODE_COVERAGE
450#define CODE_COVERAGE_STRINGIFY(x) #x
451#define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
452#define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
453#define ACCESS_MASM(masm) masm->stop(__FILE_LINE__); masm->
454#else
455#define ACCESS_MASM(masm) masm->
456#endif
457
458} } // namespace v8::internal
459
460#endif // V8_MIPS_MACRO_ASSEMBLER_MIPS_H_
461