blob: ad36bd268e3f539e78a24e555ff0e34b98f489bc [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
Brian Carlstrom75412882012-01-18 01:26:54 -080053 gc_map_ = gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080054}
55
Brian Carlstrom3320cf42011-10-04 14:58:28 -070056CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080057 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070059 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -080060 const uint32_t fp_spill_mask)
61 : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes),
62 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070063 CHECK_NE(code.size(), 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070064}
65
66CompiledMethod::~CompiledMethod() {}
67
68InstructionSet CompiledMethod::GetInstructionSet() const {
69 return instruction_set_;
70}
71
72const std::vector<uint8_t>& CompiledMethod::GetCode() const {
73 return code_;
74}
75
76size_t CompiledMethod::GetFrameSizeInBytes() const {
77 return frame_size_in_bytes_;
78}
79
Brian Carlstrom3320cf42011-10-04 14:58:28 -070080uint32_t CompiledMethod::GetCoreSpillMask() const {
81 return core_spill_mask_;
82}
83
84uint32_t CompiledMethod::GetFpSpillMask() const {
85 return fp_spill_mask_;
86}
87
88const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
89 return mapping_table_;
90}
91
92const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
93 return vmap_table_;
94}
95
Brian Carlstrome7d856b2012-01-11 18:10:55 -080096const std::vector<uint8_t>& CompiledMethod::GetGcMap() const {
97 return gc_map_;
98}
99
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700100uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
101 return AlignCode(offset, instruction_set_);
102}
103
104uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
105 switch (instruction_set) {
106 case kArm:
107 case kThumb2:
108 return RoundUp(offset, kArmAlignment);
109 case kX86:
110 return offset;
111 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800112 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 return 0;
114 }
115}
116
117size_t CompiledMethod::CodeDelta() const {
118 switch (instruction_set_) {
119 case kArm:
120 case kX86:
121 return 0;
122 case kThumb2: {
123 // +1 to set the low-order bit so a BLX will switch to Thumb mode
124 return 1;
125 }
126 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800127 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set_);
Brian Carlstrom413f9e02012-01-09 22:24:30 -0800128 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700129 }
130}
131
132const void* CompiledMethod::CodePointer(const void* code_pointer,
133 InstructionSet instruction_set) {
134 switch (instruction_set) {
135 case kArm:
136 case kX86:
137 return code_pointer;
138 case kThumb2: {
139 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
140 // Set the low-order bit so a BLX will switch to Thumb mode
141 address |= 0x1;
142 return reinterpret_cast<const void*>(address);
143 }
144 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800145 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700146 return NULL;
147 }
148}
149
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700150CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
151 CHECK_NE(code.size(), 0U);
152 code_ = code;
153}
154
155CompiledInvokeStub::~CompiledInvokeStub() {}
156
157const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
158 return code_;
159}
160
161} // namespace art