blob: 40230b42dcc063bc8fb08c191b59a7789992367d [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,
Brian Carlstrome7d856b2012-01-11 18:10:55 -08008 const std::vector<uint16_t>& short_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -07009 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,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080012 const std::vector<uint32_t>& mapping_table,
13 const std::vector<uint16_t>& vmap_table)
Ian Rogers169c9a72011-11-13 20:13:17 -080014 : 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 Carlstrome7d856b2012-01-11 18:10:55 -080018 CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1
Brian Carlstrom3320cf42011-10-04 14:58:28 -070019
20 size_t code_byte_count = short_code.size() * sizeof(short_code[0]);
21 std::vector<uint8_t> byte_code(code_byte_count);
22 memcpy(&byte_code[0], &short_code[0], code_byte_count);
23
24 std::vector<uint32_t> length_prefixed_mapping_table;
25 length_prefixed_mapping_table.push_back(mapping_table.size());
26 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
27 mapping_table.begin(),
28 mapping_table.end());
29 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
30
31 std::vector<uint16_t> length_prefixed_vmap_table;
32 length_prefixed_vmap_table.push_back(vmap_table.size());
33 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
34 vmap_table.begin(),
35 vmap_table.end());
36 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070037 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 code_ = byte_code;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040 mapping_table_ = length_prefixed_mapping_table;
41 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070042
43 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 -070044}
45
Brian Carlstrome7d856b2012-01-11 18:10:55 -080046void CompiledMethod::SetGcMap(const std::vector<uint8_t>& gc_map) {
47 CHECK_NE(gc_map.size(), 0U);
48
49 // Should only be used with CompiledMethods created with oatCompileMethod
50 CHECK_NE(mapping_table_.size(), 0U);
51 CHECK_NE(vmap_table_.size(), 0U);
52
53 std::vector<uint8_t> length_prefixed_gc_map;
54 length_prefixed_gc_map.push_back((gc_map.size() & 0xff000000) >> 24);
55 length_prefixed_gc_map.push_back((gc_map.size() & 0x00ff0000) >> 16);
56 length_prefixed_gc_map.push_back((gc_map.size() & 0x0000ff00) >> 8);
57 length_prefixed_gc_map.push_back((gc_map.size() & 0x000000ff) >> 0);
58 length_prefixed_gc_map.insert(length_prefixed_gc_map.end(),
59 gc_map.begin(),
60 gc_map.end());
61 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map.size());
62 DCHECK_EQ(gc_map.size(),
63 static_cast<size_t>((length_prefixed_gc_map[0] << 24) |
64 (length_prefixed_gc_map[1] << 16) |
65 (length_prefixed_gc_map[2] << 8) |
66 (length_prefixed_gc_map[3] << 0)));
67
68 gc_map_ = length_prefixed_gc_map;
69}
70
Brian Carlstrom3320cf42011-10-04 14:58:28 -070071CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080072 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070073 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070074 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -080075 const uint32_t fp_spill_mask)
76 : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes),
77 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070078 CHECK_NE(code.size(), 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070079}
80
81CompiledMethod::~CompiledMethod() {}
82
83InstructionSet CompiledMethod::GetInstructionSet() const {
84 return instruction_set_;
85}
86
87const std::vector<uint8_t>& CompiledMethod::GetCode() const {
88 return code_;
89}
90
91size_t CompiledMethod::GetFrameSizeInBytes() const {
92 return frame_size_in_bytes_;
93}
94
Brian Carlstrom3320cf42011-10-04 14:58:28 -070095uint32_t CompiledMethod::GetCoreSpillMask() const {
96 return core_spill_mask_;
97}
98
99uint32_t CompiledMethod::GetFpSpillMask() const {
100 return fp_spill_mask_;
101}
102
103const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
104 return mapping_table_;
105}
106
107const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
108 return vmap_table_;
109}
110
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800111const std::vector<uint8_t>& CompiledMethod::GetGcMap() const {
112 return gc_map_;
113}
114
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700115uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
116 return AlignCode(offset, instruction_set_);
117}
118
119uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
120 switch (instruction_set) {
121 case kArm:
122 case kThumb2:
123 return RoundUp(offset, kArmAlignment);
124 case kX86:
125 return offset;
126 default:
127 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
128 return 0;
129 }
130}
131
132size_t CompiledMethod::CodeDelta() const {
133 switch (instruction_set_) {
134 case kArm:
135 case kX86:
136 return 0;
137 case kThumb2: {
138 // +1 to set the low-order bit so a BLX will switch to Thumb mode
139 return 1;
140 }
141 default:
142 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set_;
Brian Carlstrom413f9e02012-01-09 22:24:30 -0800143 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700144 }
145}
146
147const void* CompiledMethod::CodePointer(const void* code_pointer,
148 InstructionSet instruction_set) {
149 switch (instruction_set) {
150 case kArm:
151 case kX86:
152 return code_pointer;
153 case kThumb2: {
154 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
155 // Set the low-order bit so a BLX will switch to Thumb mode
156 address |= 0x1;
157 return reinterpret_cast<const void*>(address);
158 }
159 default:
160 LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set;
161 return NULL;
162 }
163}
164
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700165CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
166 CHECK_NE(code.size(), 0U);
167 code_ = code;
168}
169
170CompiledInvokeStub::~CompiledInvokeStub() {}
171
172const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
173 return code_;
174}
175
176} // namespace art