blob: 2846ab33fbb39f564ad45bdcf0348886e471df17 [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,
10 const size_t return_pc_offset_in_bytes,
11 const uint32_t core_spill_mask,
12 const uint32_t fp_spill_mask,
13 std::vector<uint32_t>& mapping_table,
14 std::vector<uint16_t>& vmap_table) {
15 CHECK_NE(short_code.size(), 0U);
16
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());
34
35 instruction_set_ = instruction_set;
36 code_ = byte_code;
37 frame_size_in_bytes_ = frame_size_in_bytes;
38 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
39 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;
43}
44
45CompiledMethod::CompiledMethod(InstructionSet instruction_set,
46 std::vector<uint8_t>& code,
47 const size_t frame_size_in_bytes,
48 const size_t return_pc_offset_in_bytes,
49 const uint32_t core_spill_mask,
50 const uint32_t fp_spill_mask) {
51 CHECK_NE(code.size(), 0U);
52
53 instruction_set_ = instruction_set;
54 code_ = code;
55 frame_size_in_bytes_ = frame_size_in_bytes;
56 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
57 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
75size_t CompiledMethod::GetReturnPcOffsetInBytes() const {
76 return return_pc_offset_in_bytes_;
77}
78
79uint32_t CompiledMethod::GetCoreSpillMask() const {
80 return core_spill_mask_;
81}
82
83uint32_t CompiledMethod::GetFpSpillMask() const {
84 return fp_spill_mask_;
85}
86
87const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
88 return mapping_table_;
89}
90
91const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
92 return vmap_table_;
93}
94
95uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
96 return AlignCode(offset, instruction_set_);
97}
98
99uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
100 switch (instruction_set) {
101 case kArm:
102 case kThumb2:
103 return RoundUp(offset, kArmAlignment);
104 case kX86:
105 return offset;
106 default:
107 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
108 return 0;
109 }
110}
111
112size_t CompiledMethod::CodeDelta() const {
113 switch (instruction_set_) {
114 case kArm:
115 case kX86:
116 return 0;
117 case kThumb2: {
118 // +1 to set the low-order bit so a BLX will switch to Thumb mode
119 return 1;
120 }
121 default:
122 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set_;
123 return NULL;
124 }
125}
126
127const void* CompiledMethod::CodePointer(const void* code_pointer,
128 InstructionSet instruction_set) {
129 switch (instruction_set) {
130 case kArm:
131 case kX86:
132 return code_pointer;
133 case kThumb2: {
134 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
135 // Set the low-order bit so a BLX will switch to Thumb mode
136 address |= 0x1;
137 return reinterpret_cast<const void*>(address);
138 }
139 default:
140 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
141 return NULL;
142 }
143}
144
145
146CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
147 CHECK_NE(code.size(), 0U);
148 code_ = code;
149}
150
151CompiledInvokeStub::~CompiledInvokeStub() {}
152
153const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
154 return code_;
155}
156
157} // namespace art