blob: 500a62bd82db7254a466848918da34abc6e9b421 [file] [log] [blame]
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "compiled_method.h"
4
5namespace art {
6
7CompiledMethod::CompiledMethod(InstructionSet instruction_set,
8 std::vector<short>& short_code,
9 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070010 const uint32_t core_spill_mask,
11 const uint32_t fp_spill_mask,
12 std::vector<uint32_t>& mapping_table,
13 std::vector<uint16_t>& vmap_table) {
14 CHECK_NE(short_code.size(), 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070015 CHECK_GE(vmap_table.size(), 1U); // should always contain an entry for LR
Brian Carlstrom3320cf42011-10-04 14:58:28 -070016
17 size_t code_byte_count = short_code.size() * sizeof(short_code[0]);
18 std::vector<uint8_t> byte_code(code_byte_count);
19 memcpy(&byte_code[0], &short_code[0], code_byte_count);
20
21 std::vector<uint32_t> length_prefixed_mapping_table;
22 length_prefixed_mapping_table.push_back(mapping_table.size());
23 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
24 mapping_table.begin(),
25 mapping_table.end());
26 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
27
28 std::vector<uint16_t> length_prefixed_vmap_table;
29 length_prefixed_vmap_table.push_back(vmap_table.size());
30 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
31 vmap_table.begin(),
32 vmap_table.end());
33 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070034 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070035
36 instruction_set_ = instruction_set;
37 code_ = byte_code;
38 frame_size_in_bytes_ = frame_size_in_bytes;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 core_spill_mask_ = core_spill_mask;
40 fp_spill_mask_ = fp_spill_mask;
41 mapping_table_ = length_prefixed_mapping_table;
42 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070043
44 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask) + __builtin_popcount(fp_spill_mask)));
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045}
46
47CompiledMethod::CompiledMethod(InstructionSet instruction_set,
48 std::vector<uint8_t>& code,
49 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070050 const uint32_t core_spill_mask,
51 const uint32_t fp_spill_mask) {
52 CHECK_NE(code.size(), 0U);
53
54 instruction_set_ = instruction_set;
55 code_ = code;
56 frame_size_in_bytes_ = frame_size_in_bytes;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070057 core_spill_mask_ = core_spill_mask;
58 fp_spill_mask_ = fp_spill_mask;
59}
60
61CompiledMethod::~CompiledMethod() {}
62
63InstructionSet CompiledMethod::GetInstructionSet() const {
64 return instruction_set_;
65}
66
67const std::vector<uint8_t>& CompiledMethod::GetCode() const {
68 return code_;
69}
70
71size_t CompiledMethod::GetFrameSizeInBytes() const {
72 return frame_size_in_bytes_;
73}
74
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075uint32_t CompiledMethod::GetCoreSpillMask() const {
76 return core_spill_mask_;
77}
78
79uint32_t CompiledMethod::GetFpSpillMask() const {
80 return fp_spill_mask_;
81}
82
83const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
84 return mapping_table_;
85}
86
87const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
88 return vmap_table_;
89}
90
91uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
92 return AlignCode(offset, instruction_set_);
93}
94
95uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
96 switch (instruction_set) {
97 case kArm:
98 case kThumb2:
99 return RoundUp(offset, kArmAlignment);
100 case kX86:
101 return offset;
102 default:
103 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
104 return 0;
105 }
106}
107
108size_t CompiledMethod::CodeDelta() const {
109 switch (instruction_set_) {
110 case kArm:
111 case kX86:
112 return 0;
113 case kThumb2: {
114 // +1 to set the low-order bit so a BLX will switch to Thumb mode
115 return 1;
116 }
117 default:
118 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set_;
119 return NULL;
120 }
121}
122
123const void* CompiledMethod::CodePointer(const void* code_pointer,
124 InstructionSet instruction_set) {
125 switch (instruction_set) {
126 case kArm:
127 case kX86:
128 return code_pointer;
129 case kThumb2: {
130 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
131 // Set the low-order bit so a BLX will switch to Thumb mode
132 address |= 0x1;
133 return reinterpret_cast<const void*>(address);
134 }
135 default:
136 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
137 return NULL;
138 }
139}
140
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
142 CHECK_NE(code.size(), 0U);
143 code_ = code;
144}
145
146CompiledInvokeStub::~CompiledInvokeStub() {}
147
148const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
149 return code_;
150}
151
152} // namespace art