blob: 4e21a9109bbc15141ac2d344fe6560d7b2216afa [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,
Ian Rogers169c9a72011-11-13 20:13:17 -080013 std::vector<uint16_t>& vmap_table)
14 : instruction_set_(instruction_set), frame_size_in_bytes_(frame_size_in_bytes),
15 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070016 CHECK_NE(short_code.size(), 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070017 CHECK_GE(vmap_table.size(), 1U); // should always contain an entry for LR
Brian Carlstrom3320cf42011-10-04 14:58:28 -070018
19 size_t code_byte_count = short_code.size() * sizeof(short_code[0]);
20 std::vector<uint8_t> byte_code(code_byte_count);
21 memcpy(&byte_code[0], &short_code[0], code_byte_count);
22
23 std::vector<uint32_t> length_prefixed_mapping_table;
24 length_prefixed_mapping_table.push_back(mapping_table.size());
25 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
26 mapping_table.begin(),
27 mapping_table.end());
28 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
29
30 std::vector<uint16_t> length_prefixed_vmap_table;
31 length_prefixed_vmap_table.push_back(vmap_table.size());
32 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
33 vmap_table.begin(),
34 vmap_table.end());
35 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070036 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070037
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038 code_ = byte_code;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 mapping_table_ = length_prefixed_mapping_table;
40 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070041
42 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 -070043}
44
45CompiledMethod::CompiledMethod(InstructionSet instruction_set,
46 std::vector<uint8_t>& code,
47 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070048 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -080049 const uint32_t fp_spill_mask)
50 : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes),
51 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070052 CHECK_NE(code.size(), 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070053}
54
55CompiledMethod::~CompiledMethod() {}
56
57InstructionSet CompiledMethod::GetInstructionSet() const {
58 return instruction_set_;
59}
60
61const std::vector<uint8_t>& CompiledMethod::GetCode() const {
62 return code_;
63}
64
65size_t CompiledMethod::GetFrameSizeInBytes() const {
66 return frame_size_in_bytes_;
67}
68
Brian Carlstrom3320cf42011-10-04 14:58:28 -070069uint32_t CompiledMethod::GetCoreSpillMask() const {
70 return core_spill_mask_;
71}
72
73uint32_t CompiledMethod::GetFpSpillMask() const {
74 return fp_spill_mask_;
75}
76
77const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
78 return mapping_table_;
79}
80
81const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
82 return vmap_table_;
83}
84
85uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
86 return AlignCode(offset, instruction_set_);
87}
88
89uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
90 switch (instruction_set) {
91 case kArm:
92 case kThumb2:
93 return RoundUp(offset, kArmAlignment);
94 case kX86:
95 return offset;
96 default:
97 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
98 return 0;
99 }
100}
101
102size_t CompiledMethod::CodeDelta() const {
103 switch (instruction_set_) {
104 case kArm:
105 case kX86:
106 return 0;
107 case kThumb2: {
108 // +1 to set the low-order bit so a BLX will switch to Thumb mode
109 return 1;
110 }
111 default:
112 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set_;
113 return NULL;
114 }
115}
116
117const void* CompiledMethod::CodePointer(const void* code_pointer,
118 InstructionSet instruction_set) {
119 switch (instruction_set) {
120 case kArm:
121 case kX86:
122 return code_pointer;
123 case kThumb2: {
124 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
125 // Set the low-order bit so a BLX will switch to Thumb mode
126 address |= 0x1;
127 return reinterpret_cast<const void*>(address);
128 }
129 default:
130 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
131 return NULL;
132 }
133}
134
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700135CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
136 CHECK_NE(code.size(), 0U);
137 code_ = code;
138}
139
140CompiledInvokeStub::~CompiledInvokeStub() {}
141
142const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
143 return code_;
144}
145
146} // namespace art