OLD | NEW |
1 // Copyright (c) 1994-2006 Sun Microsystems Inc. | 1 // Copyright (c) 1994-2006 Sun Microsystems Inc. |
2 // All Rights Reserved. | 2 // All Rights Reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // - Redistributions of source code must retain the above copyright notice, | 8 // - Redistributions of source code must retain the above copyright notice, |
9 // this list of conditions and the following disclaimer. | 9 // this list of conditions and the following disclaimer. |
10 // | 10 // |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 // The original source code covered by the above license above has been | 31 // The original source code covered by the above license above has been |
32 // modified significantly by Google Inc. | 32 // modified significantly by Google Inc. |
33 // Copyright 2012 the V8 project authors. All rights reserved. | 33 // Copyright 2012 the V8 project authors. All rights reserved. |
34 | 34 |
35 // A lightweight X64 Assembler. | 35 // A lightweight X64 Assembler. |
36 | 36 |
37 #ifndef V8_X64_ASSEMBLER_X64_H_ | 37 #ifndef V8_X64_ASSEMBLER_X64_H_ |
38 #define V8_X64_ASSEMBLER_X64_H_ | 38 #define V8_X64_ASSEMBLER_X64_H_ |
39 | 39 |
| 40 #include <deque> |
| 41 |
40 #include "src/serialize.h" | 42 #include "src/serialize.h" |
41 | 43 |
42 namespace v8 { | 44 namespace v8 { |
43 namespace internal { | 45 namespace internal { |
44 | 46 |
45 // Utility functions | 47 // Utility functions |
46 | 48 |
47 // CPU Registers. | 49 // CPU Registers. |
48 // | 50 // |
49 // 1) We would prefer to use an enum, but enum values are assignment- | 51 // 1) We would prefer to use an enum, but enum values are assignment- |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 // [index*scale + disp/r] | 403 // [index*scale + disp/r] |
402 Operand(Register index, | 404 Operand(Register index, |
403 ScaleFactor scale, | 405 ScaleFactor scale, |
404 int32_t disp); | 406 int32_t disp); |
405 | 407 |
406 // Offset from existing memory operand. | 408 // Offset from existing memory operand. |
407 // Offset is added to existing displacement as 32-bit signed values and | 409 // Offset is added to existing displacement as 32-bit signed values and |
408 // this must not overflow. | 410 // this must not overflow. |
409 Operand(const Operand& base, int32_t offset); | 411 Operand(const Operand& base, int32_t offset); |
410 | 412 |
| 413 // [rip + disp/r] |
| 414 explicit Operand(Label* label); |
| 415 |
411 // Checks whether either base or index register is the given register. | 416 // Checks whether either base or index register is the given register. |
412 // Does not check the "reg" part of the Operand. | 417 // Does not check the "reg" part of the Operand. |
413 bool AddressUsesRegister(Register reg) const; | 418 bool AddressUsesRegister(Register reg) const; |
414 | 419 |
415 // Queries related to the size of the generated instruction. | 420 // Queries related to the size of the generated instruction. |
416 // Whether the generated instruction will have a REX prefix. | 421 // Whether the generated instruction will have a REX prefix. |
417 bool requires_rex() const { return rex_ != 0; } | 422 bool requires_rex() const { return rex_ != 0; } |
418 // Size of the ModR/M, SIB and displacement parts of the generated | 423 // Size of the ModR/M, SIB and displacement parts of the generated |
419 // instruction. | 424 // instruction. |
420 int operand_size() const { return len_; } | 425 int operand_size() const { return len_; } |
421 | 426 |
422 private: | 427 private: |
423 byte rex_; | 428 byte rex_; |
424 byte buf_[6]; | 429 byte buf_[9]; |
425 // The number of bytes of buf_ in use. | 430 // The number of bytes of buf_ in use. |
426 byte len_; | 431 byte len_; |
427 | 432 |
428 // Set the ModR/M byte without an encoded 'reg' register. The | 433 // Set the ModR/M byte without an encoded 'reg' register. The |
429 // register is encoded later as part of the emit_operand operation. | 434 // register is encoded later as part of the emit_operand operation. |
430 // set_modrm can be called before or after set_sib and set_disp*. | 435 // set_modrm can be called before or after set_sib and set_disp*. |
431 inline void set_modrm(int mod, Register rm); | 436 inline void set_modrm(int mod, Register rm); |
432 | 437 |
433 // Set the SIB byte if one is needed. Sets the length to 2 rather than 1. | 438 // Set the SIB byte if one is needed. Sets the length to 2 rather than 1. |
434 inline void set_sib(ScaleFactor scale, Register index, Register base); | 439 inline void set_sib(ScaleFactor scale, Register index, Register base); |
435 | 440 |
436 // Adds operand displacement fields (offsets added to the memory address). | 441 // Adds operand displacement fields (offsets added to the memory address). |
437 // Needs to be called after set_sib, not before it. | 442 // Needs to be called after set_sib, not before it. |
438 inline void set_disp8(int disp); | 443 inline void set_disp8(int disp); |
439 inline void set_disp32(int disp); | 444 inline void set_disp32(int disp); |
| 445 inline void set_disp64(int64_t disp); // for labels. |
440 | 446 |
441 friend class Assembler; | 447 friend class Assembler; |
442 }; | 448 }; |
443 | 449 |
444 | 450 |
445 #define ASSEMBLER_INSTRUCTION_LIST(V) \ | 451 #define ASSEMBLER_INSTRUCTION_LIST(V) \ |
446 V(add) \ | 452 V(add) \ |
447 V(and) \ | 453 V(and) \ |
448 V(cmp) \ | 454 V(cmp) \ |
449 V(dec) \ | 455 V(dec) \ |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 void bsrl(Register dst, Register src); | 887 void bsrl(Register dst, Register src); |
882 | 888 |
883 // Miscellaneous | 889 // Miscellaneous |
884 void clc(); | 890 void clc(); |
885 void cld(); | 891 void cld(); |
886 void cpuid(); | 892 void cpuid(); |
887 void hlt(); | 893 void hlt(); |
888 void int3(); | 894 void int3(); |
889 void nop(); | 895 void nop(); |
890 void ret(int imm16); | 896 void ret(int imm16); |
| 897 void ud2(); |
891 void setcc(Condition cc, Register reg); | 898 void setcc(Condition cc, Register reg); |
892 | 899 |
893 // Label operations & relative jumps (PPUM Appendix D) | 900 // Label operations & relative jumps (PPUM Appendix D) |
894 // | 901 // |
895 // Takes a branch opcode (cc) and a label (L) and generates | 902 // Takes a branch opcode (cc) and a label (L) and generates |
896 // either a backward branch or a forward branch and links it | 903 // either a backward branch or a forward branch and links it |
897 // to the label fixup chain. Usage: | 904 // to the label fixup chain. Usage: |
898 // | 905 // |
899 // Label L; // unbound label | 906 // Label L; // unbound label |
900 // j(cc, &L); // forward branch to unbound label | 907 // j(cc, &L); // forward branch to unbound label |
(...skipping 26 matching lines...) Expand all Loading... |
927 // Jumps | 934 // Jumps |
928 // Jump short or near relative. | 935 // Jump short or near relative. |
929 // Use a 32-bit signed displacement. | 936 // Use a 32-bit signed displacement. |
930 // Unconditional jump to L | 937 // Unconditional jump to L |
931 void jmp(Label* L, Label::Distance distance = Label::kFar); | 938 void jmp(Label* L, Label::Distance distance = Label::kFar); |
932 void jmp(Address entry, RelocInfo::Mode rmode); | 939 void jmp(Address entry, RelocInfo::Mode rmode); |
933 void jmp(Handle<Code> target, RelocInfo::Mode rmode); | 940 void jmp(Handle<Code> target, RelocInfo::Mode rmode); |
934 | 941 |
935 // Jump near absolute indirect (r64) | 942 // Jump near absolute indirect (r64) |
936 void jmp(Register adr); | 943 void jmp(Register adr); |
| 944 void jmp(const Operand& src); |
937 | 945 |
938 // Conditional jumps | 946 // Conditional jumps |
939 void j(Condition cc, | 947 void j(Condition cc, |
940 Label* L, | 948 Label* L, |
941 Label::Distance distance = Label::kFar); | 949 Label::Distance distance = Label::kFar); |
942 void j(Condition cc, Address entry, RelocInfo::Mode rmode); | 950 void j(Condition cc, Address entry, RelocInfo::Mode rmode); |
943 void j(Condition cc, Handle<Code> target, RelocInfo::Mode rmode); | 951 void j(Condition cc, Handle<Code> target, RelocInfo::Mode rmode); |
944 | 952 |
945 // Floating-point operations | 953 // Floating-point operations |
946 void fld(int i); | 954 void fld(int i); |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1337 // Allocate a constant pool of the correct size for the generated code. | 1345 // Allocate a constant pool of the correct size for the generated code. |
1338 Handle<ConstantPoolArray> NewConstantPool(Isolate* isolate); | 1346 Handle<ConstantPoolArray> NewConstantPool(Isolate* isolate); |
1339 | 1347 |
1340 // Generate the constant pool for the generated code. | 1348 // Generate the constant pool for the generated code. |
1341 void PopulateConstantPool(ConstantPoolArray* constant_pool); | 1349 void PopulateConstantPool(ConstantPoolArray* constant_pool); |
1342 | 1350 |
1343 // Writes a single word of data in the code stream. | 1351 // Writes a single word of data in the code stream. |
1344 // Used for inline tables, e.g., jump-tables. | 1352 // Used for inline tables, e.g., jump-tables. |
1345 void db(uint8_t data); | 1353 void db(uint8_t data); |
1346 void dd(uint32_t data); | 1354 void dd(uint32_t data); |
| 1355 void dq(Label* label); |
1347 | 1356 |
1348 PositionsRecorder* positions_recorder() { return &positions_recorder_; } | 1357 PositionsRecorder* positions_recorder() { return &positions_recorder_; } |
1349 | 1358 |
1350 // Check if there is less than kGap bytes available in the buffer. | 1359 // Check if there is less than kGap bytes available in the buffer. |
1351 // If this is the case, we need to grow the buffer before emitting | 1360 // If this is the case, we need to grow the buffer before emitting |
1352 // an instruction or relocation information. | 1361 // an instruction or relocation information. |
1353 inline bool buffer_overflow() const { | 1362 inline bool buffer_overflow() const { |
1354 return pc_ >= reloc_info_writer.pos() - kGap; | 1363 return pc_ >= reloc_info_writer.pos() - kGap; |
1355 } | 1364 } |
1356 | 1365 |
1357 // Get the number of bytes available in the buffer. | 1366 // Get the number of bytes available in the buffer. |
1358 inline int available_space() const { | 1367 inline int available_space() const { |
1359 return static_cast<int>(reloc_info_writer.pos() - pc_); | 1368 return static_cast<int>(reloc_info_writer.pos() - pc_); |
1360 } | 1369 } |
1361 | 1370 |
1362 static bool IsNop(Address addr); | 1371 static bool IsNop(Address addr); |
1363 | 1372 |
1364 // Avoid overflows for displacements etc. | 1373 // Avoid overflows for displacements etc. |
1365 static const int kMaximalBufferSize = 512*MB; | 1374 static const int kMaximalBufferSize = 512*MB; |
1366 | 1375 |
1367 byte byte_at(int pos) { return buffer_[pos]; } | 1376 byte byte_at(int pos) { return buffer_[pos]; } |
1368 void set_byte_at(int pos, byte value) { buffer_[pos] = value; } | 1377 void set_byte_at(int pos, byte value) { buffer_[pos] = value; } |
1369 | 1378 |
1370 protected: | 1379 protected: |
1371 // Call near indirect | 1380 // Call near indirect |
1372 void call(const Operand& operand); | 1381 void call(const Operand& operand); |
1373 | 1382 |
1374 // Jump near absolute indirect (m64) | |
1375 void jmp(const Operand& src); | |
1376 | |
1377 private: | 1383 private: |
1378 byte* addr_at(int pos) { return buffer_ + pos; } | 1384 byte* addr_at(int pos) { return buffer_ + pos; } |
1379 uint32_t long_at(int pos) { | 1385 uint32_t long_at(int pos) { |
1380 return *reinterpret_cast<uint32_t*>(addr_at(pos)); | 1386 return *reinterpret_cast<uint32_t*>(addr_at(pos)); |
1381 } | 1387 } |
1382 void long_at_put(int pos, uint32_t x) { | 1388 void long_at_put(int pos, uint32_t x) { |
1383 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x; | 1389 *reinterpret_cast<uint32_t*>(addr_at(pos)) = x; |
1384 } | 1390 } |
1385 | 1391 |
1386 // code emission | 1392 // code emission |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1804 arithmetic_op(0x31, src, dst, size); | 1810 arithmetic_op(0x31, src, dst, size); |
1805 } | 1811 } |
1806 | 1812 |
1807 friend class CodePatcher; | 1813 friend class CodePatcher; |
1808 friend class EnsureSpace; | 1814 friend class EnsureSpace; |
1809 friend class RegExpMacroAssemblerX64; | 1815 friend class RegExpMacroAssemblerX64; |
1810 | 1816 |
1811 // code generation | 1817 // code generation |
1812 RelocInfoWriter reloc_info_writer; | 1818 RelocInfoWriter reloc_info_writer; |
1813 | 1819 |
| 1820 // Internal reference positions, required for (potential) patching in |
| 1821 // GrowBuffer(); contains only those internal references whose labels |
| 1822 // are already bound. |
| 1823 std::deque<int> internal_reference_positions_; |
| 1824 |
1814 List< Handle<Code> > code_targets_; | 1825 List< Handle<Code> > code_targets_; |
1815 | 1826 |
1816 PositionsRecorder positions_recorder_; | 1827 PositionsRecorder positions_recorder_; |
1817 friend class PositionsRecorder; | 1828 friend class PositionsRecorder; |
1818 }; | 1829 }; |
1819 | 1830 |
1820 | 1831 |
1821 // Helper class that ensures that there is enough space for generating | 1832 // Helper class that ensures that there is enough space for generating |
1822 // instructions and relocation information. The constructor makes | 1833 // instructions and relocation information. The constructor makes |
1823 // sure that there is enough space and (in debug mode) the destructor | 1834 // sure that there is enough space and (in debug mode) the destructor |
(...skipping 17 matching lines...) Expand all Loading... |
1841 private: | 1852 private: |
1842 Assembler* assembler_; | 1853 Assembler* assembler_; |
1843 #ifdef DEBUG | 1854 #ifdef DEBUG |
1844 int space_before_; | 1855 int space_before_; |
1845 #endif | 1856 #endif |
1846 }; | 1857 }; |
1847 | 1858 |
1848 } } // namespace v8::internal | 1859 } } // namespace v8::internal |
1849 | 1860 |
1850 #endif // V8_X64_ASSEMBLER_X64_H_ | 1861 #endif // V8_X64_ASSEMBLER_X64_H_ |
OLD | NEW |