Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: src/mips/assembler-mips.h

Issue 543161: Added support for MIPS in architecture independent files.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // - Neither the name of Sun Microsystems or the names of contributors may
16 // be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // The original source code covered by the above license above has been
32 // modified significantly by Google Inc.
33 // Copyright 2006-2010 the V8 project authors. All rights reserved.
34
35
36 #ifndef V8_MIPS_ASSEMBLER_MIPS_H_
37 #define V8_MIPS_ASSEMBLER_MIPS_H_
38
39 #include <stdio.h>
40 #include "assembler.h"
41 #include "constants-mips.h"
42 #include "serialize.h"
43
44 using namespace assembler::mips;
45
46 namespace v8 {
47 namespace internal {
48
49 // CPU Registers.
50 //
51 // 1) We would prefer to use an enum, but enum values are assignment-
52 // compatible with int, which has caused code-generation bugs.
53 //
54 // 2) We would prefer to use a class instead of a struct but we don't like
55 // the register initialization to depend on the particular initialization
56 // order (which appears to be different on OS X, Linux, and Windows for the
57 // installed versions of C++ we tried). Using a struct permits C-style
58 // "initialization". Also, the Register objects cannot be const as this
59 // forces initialization stubs in MSVC, making us dependent on initialization
60 // order.
61 //
62 // 3) By not using an enum, we are possibly preventing the compiler from
63 // doing certain constant folds, which may significantly reduce the
64 // code generated for some assembly instructions (because they boil down
65 // to a few constants). If this is a problem, we could change the code
66 // such that we use an enum in optimized mode, and the struct in debug
67 // mode. This way we get the compile-time error checking in debug mode
68 // and best performance in optimized code.
69
70
71 // -----------------------------------------------------------------------------
72 // Implementation of Register and FPURegister
73
74 // Core register
75 struct Register {
76 bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
77 bool is(Register reg) const { return code_ == reg.code_; }
78 // The byte-register distinction of ai32 has dissapeared.
79 bool is_byte_register() const { return false; }
80 int code() const {
81 ASSERT(is_valid());
82 return code_;
83 }
84 int bit() const {
85 ASSERT(is_valid());
86 return 1 << code_;
87 }
88
89 // (unfortunately we can't make this private in a struct)
90 int code_;
91 };
92
93 extern const Register no_reg;
94
95 extern const Register zero_reg;
96 extern const Register at;
97 extern const Register v0;
98 extern const Register v1;
99 extern const Register a0;
100 extern const Register a1;
101 extern const Register a2;
102 extern const Register a3;
103 extern const Register t0;
104 extern const Register t1;
105 extern const Register t2;
106 extern const Register t3;
107 extern const Register t4;
108 extern const Register t5;
109 extern const Register t6;
110 extern const Register t7;
111 extern const Register s0;
112 extern const Register s1;
113 extern const Register s2;
114 extern const Register s3;
115 extern const Register s4;
116 extern const Register s5;
117 extern const Register s6;
118 extern const Register s7;
119 extern const Register t8;
120 extern const Register t9;
121 extern const Register k0;
122 extern const Register k1;
123 extern const Register gp;
124 extern const Register sp;
125 extern const Register s8_fp;
126 extern const Register ra;
127
128 int ToNumber(Register reg);
129
130 Register ToRegister(int num);
131
132 // Coprocessor register
133 struct FPURegister {
134 bool is_valid() const { return 0 <= code_ && code_ < kNumFPURegister ; }
135 bool is(FPURegister creg) const { return code_ == creg.code_; }
136 int code() const {
137 ASSERT(is_valid());
138 return code_;
139 }
140 int bit() const {
141 ASSERT(is_valid());
142 return 1 << code_;
143 }
144
145 // (unfortunately we can't make this private in a struct)
146 int code_;
147 };
148
149 extern const FPURegister no_creg;
150
151 extern const FPURegister f0;
152 extern const FPURegister f1;
153 extern const FPURegister f2;
154 extern const FPURegister f3;
155 extern const FPURegister f4;
156 extern const FPURegister f5;
157 extern const FPURegister f6;
158 extern const FPURegister f7;
159 extern const FPURegister f8;
160 extern const FPURegister f9;
161 extern const FPURegister f10;
162 extern const FPURegister f11;
163 extern const FPURegister f12; // arg
164 extern const FPURegister f13;
165 extern const FPURegister f14; // arg
166 extern const FPURegister f15;
167 extern const FPURegister f16;
168 extern const FPURegister f17;
169 extern const FPURegister f18;
170 extern const FPURegister f19;
171 extern const FPURegister f20;
172 extern const FPURegister f21;
173 extern const FPURegister f22;
174 extern const FPURegister f23;
175 extern const FPURegister f24;
176 extern const FPURegister f25;
177 extern const FPURegister f26;
178 extern const FPURegister f27;
179 extern const FPURegister f28;
180 extern const FPURegister f29;
181 extern const FPURegister f30;
182 extern const FPURegister f31;
183
184
185 // Returns the equivalent of !cc.
186 // Negation of the default no_condition (-1) results in a non-default
187 // no_condition value (-2). As long as tests for no_condition check
188 // for condition < 0, this will work as expected.
189 inline Condition NegateCondition(Condition cc);
190
191 inline Condition ReverseCondition(Condition cc) {
192 switch (cc) {
193 case Uless:
194 return Ugreater;
195 case Ugreater:
196 return Uless;
197 case Ugreater_equal:
198 return Uless_equal;
199 case Uless_equal:
200 return Ugreater_equal;
201 case less:
202 return greater;
203 case greater:
204 return less;
205 case greater_equal:
206 return less_equal;
207 case less_equal:
208 return greater_equal;
209 default:
210 return cc;
211 };
212 }
213
214
215 enum Hint {
216 no_hint = 0
217 };
218
219 inline Hint NegateHint(Hint hint) {
220 return no_hint;
221 }
222
223
224 // -----------------------------------------------------------------------------
225 // Machine instruction Operands
226
227 // Class Operand represents a shifter operand in data processing instructions
228 class Operand BASE_EMBEDDED {
229 public:
230 // Immediate.
231 INLINE(explicit Operand(int32_t immediate,
232 RelocInfo::Mode rmode = RelocInfo::NONE));
233 INLINE(explicit Operand(const ExternalReference& f));
234 INLINE(explicit Operand(const char* s));
235 INLINE(explicit Operand(Object** opp));
236 INLINE(explicit Operand(Context** cpp));
237 explicit Operand(Handle<Object> handle);
238 INLINE(explicit Operand(Smi* value));
239
240 // Register.
241 INLINE(explicit Operand(Register rm));
242
243 // Return true if this is a register operand.
244 INLINE(bool is_reg() const);
245
246 Register rm() const { return rm_; }
247
248 private:
249 Register rm_;
250 int32_t imm32_; // Valid if rm_ == no_reg
251 RelocInfo::Mode rmode_;
252
253 friend class Assembler;
254 friend class MacroAssembler;
255 };
256
257
258 // On MIPS we have only one adressing mode with base_reg + offset.
259 // Class MemOperand represents a memory operand in load and store instructions.
260 class MemOperand : public Operand {
261 public:
262
263 explicit MemOperand(Register rn, int16_t offset = 0);
264
265 private:
266 int16_t offset_;
267
268 friend class Assembler;
269 };
270
271
272 class Assembler : public Malloced {
273 public:
274 // Create an assembler. Instructions and relocation information are emitted
275 // into a buffer, with the instructions starting from the beginning and the
276 // relocation information starting from the end of the buffer. See CodeDesc
277 // for a detailed comment on the layout (globals.h).
278 //
279 // If the provided buffer is NULL, the assembler allocates and grows its own
280 // buffer, and buffer_size determines the initial buffer size. The buffer is
281 // owned by the assembler and deallocated upon destruction of the assembler.
282 //
283 // If the provided buffer is not NULL, the assembler uses the provided buffer
284 // for code generation and assumes its size to be buffer_size. If the buffer
285 // is too small, a fatal error occurs. No deallocation of the buffer is done
286 // upon destruction of the assembler.
287 Assembler(void* buffer, int buffer_size);
288 ~Assembler();
289
290 // GetCode emits any pending (non-emitted) code and fills the descriptor
291 // desc. GetCode() is idempotent; it returns the same result if no other
292 // Assembler functions are invoked in between GetCode() calls.
293 void GetCode(CodeDesc* desc);
294
295 // Label operations & relative jumps (PPUM Appendix D)
296 //
297 // Takes a branch opcode (cc) and a label (L) and generates
298 // either a backward branch or a forward branch and links it
299 // to the label fixup chain. Usage:
300 //
301 // Label L; // unbound label
302 // j(cc, &L); // forward branch to unbound label
303 // bind(&L); // bind label to the current pc
304 // j(cc, &L); // backward branch to bound label
305 // bind(&L); // illegal: a label may be bound only once
306 //
307 // Note: The same Label can be used for forward and backward branches
308 // but it may be bound only once.
309 void bind(Label* L); // binds an unbound label L to the current code position
310
311 // Returns the branch offset to the given label from the current code position
312 // Links the label to the current position if it is still unbound
313 // Manages the jump elimination optimization if the second parameter is true.
314 int32_t branch_offset(Label* L, bool jump_elimination_allowed);
315 int32_t shifted_branch_offset(Label* L, bool jump_elimination_allowed) {
316 int32_t o = branch_offset(L, jump_elimination_allowed);
317 ASSERT((o & 3) == 0); // Assert the offset is aligned.
318 return o >> 2;
319 }
320
321 // Puts a labels target address at the given position.
322 // The high 8 bits are set to zero.
323 void label_at_put(Label* L, int at_offset);
324
325 // Size of an instruction.
326 static const int kInstrSize = sizeof(Instr);
327
328 // Difference between address of current opcode and target address offset.
329 static const int kBranchPCOffset = 4;
330
331 // Read/Modify the code target address in the branch/call instruction at pc.
332 static Address target_address_at(Address pc);
333 static void set_target_address_at(Address pc, Address target);
334
335 // This sets the branch destination (which gets loaded at the call address).
336 // This is for calls and branches within generated code.
337 inline static void set_target_at(Address instruction_payload,
338 Address target) {
339 set_target_address_at(instruction_payload, target);
340 }
341
342 // This sets the branch destination.
343 // This is for calls and branches to runtime code.
344 inline static void set_external_target_at(Address instruction_payload,
345 Address target) {
346 set_target_address_at(instruction_payload, target);
347 }
348
349 static const int kCallTargetSize = 3 * kPointerSize;
350 static const int kExternalTargetSize = 3 * kPointerSize;
351
352 // Distance between the instruction referring to the address of the call
353 // target and the return address
354 static const int kCallTargetAddressOffset = 4 * kInstrSize;
355
356 // Distance between start of patched return sequence and the emitted address
357 // to jump to.
358 static const int kPatchReturnSequenceAddressOffset = kInstrSize;
359
360
361 // ---------------------------------------------------------------------------
362 // Code generation
363
364 void nop() { sll(zero_reg, zero_reg, 0); }
365
366
367 //------- Branch and jump instructions --------
368 // We don't use likely variant of instructions
369 void b(int16_t offset);
370 void b(Label* L) { b(branch_offset(L, false)>>2); }
371 void bal(int16_t offset);
372 void bal(Label* L) { bal(branch_offset(L, false)>>2); }
373
374 void beq(Register rs, Register rt, int16_t offset);
375 void beq(Register rs, Register rt, Label* L) {
376 beq(rs, rt, branch_offset(L, false) >> 2);
377 }
378 void bgez(Register rs, int16_t offset);
379 void bgezal(Register rs, int16_t offset);
380 void bgtz(Register rs, int16_t offset);
381 void blez(Register rs, int16_t offset);
382 void bltz(Register rs, int16_t offset);
383 void bltzal(Register rs, int16_t offset);
384 void bne(Register rs, Register rt, int16_t offset);
385 void bne(Register rs, Register rt, Label* L) {
386 bne(rs, rt, branch_offset(L, false)>>2);
387 }
388
389 // Never use the int16_t b(l)cond version with a branch offset
390 // instead of using the Label* version. See Twiki for infos.
391
392 // Jump targets must be in the current 256 MB-aligned region. ie 28 bits.
393 void j(int32_t target);
394 void jal(int32_t target);
395 void jalr(Register rs, Register rd = ra);
396 void jr(Register target);
397
398
399 //-------Data-processing-instructions---------
400
401 // Arithmetic
402 void add(Register rd, Register rs, Register rt);
403 void addu(Register rd, Register rs, Register rt);
404 void sub(Register rd, Register rs, Register rt);
405 void subu(Register rd, Register rs, Register rt);
406 void mult(Register rs, Register rt);
407 void multu(Register rs, Register rt);
408 void div(Register rs, Register rt);
409 void divu(Register rs, Register rt);
410 void mul(Register rd, Register rs, Register rt);
411
412 void addi(Register rd, Register rs, int32_t j);
413 void addiu(Register rd, Register rs, int32_t j);
414
415 // Logical
416 void and_(Register rd, Register rs, Register rt);
417 void or_(Register rd, Register rs, Register rt);
418 void xor_(Register rd, Register rs, Register rt);
419 void nor(Register rd, Register rs, Register rt);
420
421 void andi(Register rd, Register rs, int32_t j);
422 void ori(Register rd, Register rs, int32_t j);
423 void xori(Register rd, Register rs, int32_t j);
424 void lui(Register rd, int32_t j);
425
426 // Shifts
427 void sll(Register rd, Register rt, uint16_t sa);
428 void sllv(Register rd, Register rt, Register rs);
429 void srl(Register rd, Register rt, uint16_t sa);
430 void srlv(Register rd, Register rt, Register rs);
431 void sra(Register rt, Register rd, uint16_t sa);
432 void srav(Register rt, Register rd, Register rs);
433
434
435 //------------Memory-instructions-------------
436
437 void lb(Register rd, const MemOperand& rs);
438 void lbu(Register rd, const MemOperand& rs);
439 void lw(Register rd, const MemOperand& rs);
440 void sb(Register rd, const MemOperand& rs);
441 void sw(Register rd, const MemOperand& rs);
442
443
444 //-------------Misc-instructions--------------
445
446 // Break / Trap instructions
447 void break_(uint32_t code);
448 void tge(Register rs, Register rt, uint16_t code);
449 void tgeu(Register rs, Register rt, uint16_t code);
450 void tlt(Register rs, Register rt, uint16_t code);
451 void tltu(Register rs, Register rt, uint16_t code);
452 void teq(Register rs, Register rt, uint16_t code);
453 void tne(Register rs, Register rt, uint16_t code);
454
455 // Move from HI/LO register
456 void mfhi(Register rd);
457 void mflo(Register rd);
458
459 // Set on less than
460 void slt(Register rd, Register rs, Register rt);
461 void sltu(Register rd, Register rs, Register rt);
462 void slti(Register rd, Register rs, int32_t j);
463 void sltiu(Register rd, Register rs, int32_t j);
464
465
466 //--------Coprocessor-instructions----------------
467
468 // Load, store, and move
469 void lwc1(FPURegister fd, const MemOperand& src);
470 void ldc1(FPURegister fd, const MemOperand& src);
471
472 void swc1(FPURegister fs, const MemOperand& dst);
473 void sdc1(FPURegister fs, const MemOperand& dst);
474
475 // When paired with MTC1 to write a value to a 64-bit FPR, the MTC1 must be
476 // executed first, followed by the MTHC1.
477 void mtc1(FPURegister fs, Register rt);
478 void mthc1(FPURegister fs, Register rt);
479 void mfc1(FPURegister fs, Register rt);
480 void mfhc1(FPURegister fs, Register rt);
481
482 // Conversion
483 void cvt_w_s(FPURegister fd, FPURegister fs);
484 void cvt_w_d(FPURegister fd, FPURegister fs);
485
486 void cvt_l_s(FPURegister fd, FPURegister fs);
487 void cvt_l_d(FPURegister fd, FPURegister fs);
488
489 void cvt_s_w(FPURegister fd, FPURegister fs);
490 void cvt_s_l(FPURegister fd, FPURegister fs);
491 void cvt_s_d(FPURegister fd, FPURegister fs);
492
493 void cvt_d_w(FPURegister fd, FPURegister fs);
494 void cvt_d_l(FPURegister fd, FPURegister fs);
495 void cvt_d_s(FPURegister fd, FPURegister fs);
496
497 // Conditions and branches
498 void c(FPUCondition cond, SecondaryField fmt,
499 FPURegister ft, FPURegister fs, uint16_t cc = 0);
500
501 void bc1f(int16_t offset, uint16_t cc = 0);
502 void bc1f(Label* L, uint16_t cc = 0) { bc1f(branch_offset(L, false)>>2, cc); }
503 void bc1t(int16_t offset, uint16_t cc = 0);
504 void bc1t(Label* L, uint16_t cc = 0) { bc1t(branch_offset(L, false)>>2, cc); }
505
506
507 // Check the code size generated from label to here.
508 int InstructionsGeneratedSince(Label* l) {
509 return (pc_offset() - l->pos()) / kInstrSize;
510 }
511
512 // Debugging
513
514 // Mark address of the ExitJSFrame code.
515 void RecordJSReturn();
516
517 // Record a comment relocation entry that can be used by a disassembler.
518 // Use --debug_code to enable.
519 void RecordComment(const char* msg);
520
521 void RecordPosition(int pos);
522 void RecordStatementPosition(int pos);
523 void WriteRecordedPositions();
524
525 int32_t pc_offset() const { return pc_ - buffer_; }
526 int32_t current_position() const { return current_position_; }
527 int32_t current_statement_position() const { return current_position_; }
528
529 // Check if there is less than kGap bytes available in the buffer.
530 // If this is the case, we need to grow the buffer before emitting
531 // an instruction or relocation information.
532 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
533
534 // Get the number of bytes available in the buffer.
535 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
536
537 protected:
538 int32_t buffer_space() const { return reloc_info_writer.pos() - pc_; }
539
540 // Read/patch instructions
541 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
542 void instr_at_put(byte* pc, Instr instr) {
543 *reinterpret_cast<Instr*>(pc) = instr;
544 }
545 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
546 void instr_at_put(int pos, Instr instr) {
547 *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
548 }
549
550 // Check if an instruction is a branch of some kind.
551 bool is_branch(Instr instr);
552
553 // Decode branch instruction at pos and return branch target pos
554 int target_at(int32_t pos);
555
556 // Patch branch instruction at pos to branch to given branch target pos
557 void target_at_put(int32_t pos, int32_t target_pos);
558
559 // Say if we need to relocate with this mode.
560 bool MustUseAt(RelocInfo::Mode rmode);
561
562 // Record reloc info for current pc_
563 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
564
565 private:
566 // Code buffer:
567 // The buffer into which code and relocation info are generated.
568 byte* buffer_;
569 int buffer_size_;
570 // True if the assembler owns the buffer, false if buffer is external.
571 bool own_buffer_;
572
573 // Buffer size and constant pool distance are checked together at regular
574 // intervals of kBufferCheckInterval emitted bytes
575 static const int kBufferCheckInterval = 1*KB/2;
576
577 // Code generation
578 // The relocation writer's position is at least kGap bytes Uless the end of
579 // the generated instructions. This is so that multi-instruction sequences do
580 // not have to check for overflow. The same is true for writes of large
581 // relocation info entries.
582 static const int kGap = 32;
583 byte* pc_; // the program counter; moves forward
584
585 // Relocation information generation
586 // Each relocation is encoded as a variable size value
587 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
588 RelocInfoWriter reloc_info_writer;
589
590 // The bound position, before this we cannot do instruction elimination.
591 int last_bound_pos_;
592
593 // source position information
594 int current_position_;
595 int current_statement_position_;
596 int written_position_;
597 int written_statement_position_;
598
599 // Code emission
600 inline void CheckBuffer();
601 void GrowBuffer();
602 inline void emit(Instr x);
603
604 // Instruction generation
605 // We have 3 different kind of encoding layout on MIPS.
606 // However due to many different types of objects encoded in the same fields
607 // we have quite a few aliases for each mode.
608 // Using the same structure to refer to Register and FPURegister would spare a
609 // few aliases, but mixing both does not look clean to me.
610 // Anyway we could surely implement this differently.
611
612 void GenInstrRegister(Opcode opcode,
613 Register rs,
614 Register rt,
615 Register rd,
616 uint16_t sa = 0,
617 SecondaryField func = NULLSF);
618
619 void GenInstrRegister(Opcode opcode,
620 SecondaryField fmt,
621 FPURegister ft,
622 FPURegister fs,
623 FPURegister fd,
624 SecondaryField func = NULLSF);
625
626 void GenInstrRegister(Opcode opcode,
627 SecondaryField fmt,
628 Register rt,
629 FPURegister fs,
630 FPURegister fd,
631 SecondaryField func = NULLSF);
632
633
634 void GenInstrImmediate(Opcode opcode,
635 Register rs,
636 Register rt,
637 int32_t j);
638 void GenInstrImmediate(Opcode opcode,
639 Register rs,
640 SecondaryField SF,
641 int32_t j);
642 void GenInstrImmediate(Opcode opcode,
643 Register r1,
644 FPURegister r2,
645 int32_t j);
646
647
648 void GenInstrJump(Opcode opcode,
649 uint32_t address);
650
651
652 // Labels
653 void print(Label* L);
654 void bind_to(Label* L, int pos);
655 void link_to(Label* L, Label* appendix);
656 void next(Label* L);
657
658 friend class RegExpMacroAssemblerMIPS;
659 friend class RelocInfo;
660 };
661
662 } } // namespace v8::internal
663
664 #endif // V8_ARM_ASSEMBLER_MIPS_H_
665
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698