blob: b01b0fe4e02c845f74432efdcb67704f2942f1da [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Ian Rogers2c8f6532011-09-02 17:16:34 -070017#include "assembler.h"
18
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019#include <algorithm>
20#include <vector>
Ian Rogers2c8f6532011-09-02 17:16:34 -070021
Alex Light50fa9932015-08-10 15:30:07 -070022#ifdef ART_ENABLE_CODEGEN_arm
Dave Allison65fcc2c2014-04-28 13:45:27 -070023#include "arm/assembler_arm32.h"
24#include "arm/assembler_thumb2.h"
Alex Light50fa9932015-08-10 15:30:07 -070025#endif
26#ifdef ART_ENABLE_CODEGEN_arm64
Serban Constantinescued8dd492014-02-11 14:15:10 +000027#include "arm64/assembler_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070028#endif
29#ifdef ART_ENABLE_CODEGEN_mips
jeffhao7fbee072012-08-24 17:56:54 -070030#include "mips/assembler_mips.h"
Alex Light50fa9932015-08-10 15:30:07 -070031#endif
32#ifdef ART_ENABLE_CODEGEN_mips64
Andreas Gampe57b34292015-01-14 15:45:59 -080033#include "mips64/assembler_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070034#endif
35#ifdef ART_ENABLE_CODEGEN_x86
Ian Rogers57b86d42012-03-27 16:05:41 -070036#include "x86/assembler_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070037#endif
38#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070039#include "x86_64/assembler_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070040#endif
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041#include "globals.h"
42#include "memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070043
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070044namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070045
Ian Rogers13735952014-10-08 12:43:28 -070046static uint8_t* NewContents(size_t capacity) {
47 return new uint8_t[capacity];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070048}
49
50
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070051AssemblerBuffer::AssemblerBuffer() {
52 static const size_t kInitialBufferCapacity = 4 * KB;
53 contents_ = NewContents(kInitialBufferCapacity);
54 cursor_ = contents_;
55 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070056 fixup_ = nullptr;
57 slow_path_ = nullptr;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070058#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070059 has_ensured_capacity_ = false;
60 fixups_processed_ = false;
61#endif
62
63 // Verify internal state.
64 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070065 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070066}
67
68
69AssemblerBuffer::~AssemblerBuffer() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070070 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070071}
72
73
74void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
75 AssemblerFixup* fixup = fixup_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070076 while (fixup != nullptr) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070077 fixup->Process(region, fixup->position());
78 fixup = fixup->previous();
79 }
80}
81
82
83void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
84 // Copy the instructions from the buffer.
85 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
86 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070087 // Process fixups in the instructions.
88 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070089#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070090 fixups_processed_ = true;
91#endif
92}
93
94
Vladimir Markocf93a5c2015-06-16 11:33:24 +000095void AssemblerBuffer::ExtendCapacity(size_t min_capacity) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070096 size_t old_size = Size();
97 size_t old_capacity = Capacity();
98 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000099 new_capacity = std::max(new_capacity, min_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700100
101 // Allocate the new data area and copy contents of the old one to it.
Ian Rogers13735952014-10-08 12:43:28 -0700102 uint8_t* new_contents = NewContents(new_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700103 memmove(reinterpret_cast<void*>(new_contents),
104 reinterpret_cast<void*>(contents_),
105 old_size);
106
107 // Compute the relocation delta and switch to the new contents area.
108 ptrdiff_t delta = new_contents - contents_;
Andreas Gampe928f72b2014-09-09 19:53:48 -0700109 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700110 contents_ = new_contents;
111
112 // Update the cursor and recompute the limit.
113 cursor_ += delta;
114 limit_ = ComputeLimit(new_contents, new_capacity);
115
116 // Verify internal state.
117 CHECK_EQ(Capacity(), new_capacity);
118 CHECK_EQ(Size(), old_size);
119}
120
David Srbeckydd973932015-04-07 20:29:48 +0100121void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() {
122 this->AdvancePC(assembler_->CodeSize());
123}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700124
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200125Assembler* Assembler::Create(InstructionSet instruction_set,
126 const InstructionSetFeatures* instruction_set_features) {
jeffhao7fbee072012-08-24 17:56:54 -0700127 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700128#ifdef ART_ENABLE_CODEGEN_arm
jeffhao7fbee072012-08-24 17:56:54 -0700129 case kArm:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700130 return new arm::Arm32Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700131 case kThumb2:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700132 return new arm::Thumb2Assembler();
Alex Light50fa9932015-08-10 15:30:07 -0700133#endif
134#ifdef ART_ENABLE_CODEGEN_arm64
Serban Constantinescued8dd492014-02-11 14:15:10 +0000135 case kArm64:
136 return new arm64::Arm64Assembler();
Alex Light50fa9932015-08-10 15:30:07 -0700137#endif
138#ifdef ART_ENABLE_CODEGEN_mips
jeffhao7fbee072012-08-24 17:56:54 -0700139 case kMips:
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200140 return new mips::MipsAssembler(instruction_set_features != nullptr
141 ? instruction_set_features->AsMipsInstructionSetFeatures()
142 : nullptr);
Alex Light50fa9932015-08-10 15:30:07 -0700143#endif
144#ifdef ART_ENABLE_CODEGEN_mips64
Andreas Gampe57b34292015-01-14 15:45:59 -0800145 case kMips64:
146 return new mips64::Mips64Assembler();
Alex Light50fa9932015-08-10 15:30:07 -0700147#endif
148#ifdef ART_ENABLE_CODEGEN_x86
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700149 case kX86:
jeffhao7fbee072012-08-24 17:56:54 -0700150 return new x86::X86Assembler();
Alex Light50fa9932015-08-10 15:30:07 -0700151#endif
152#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700153 case kX86_64:
154 return new x86_64::X86_64Assembler();
Alex Light50fa9932015-08-10 15:30:07 -0700155#endif
jeffhao7fbee072012-08-24 17:56:54 -0700156 default:
157 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700158 return nullptr;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700159 }
160}
161
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700162void Assembler::StoreImmediateToThread32(ThreadOffset<4> dest ATTRIBUTE_UNUSED,
163 uint32_t imm ATTRIBUTE_UNUSED,
164 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700165 UNIMPLEMENTED(FATAL);
166}
167
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700168void Assembler::StoreImmediateToThread64(ThreadOffset<8> dest ATTRIBUTE_UNUSED,
169 uint32_t imm ATTRIBUTE_UNUSED,
170 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700171 UNIMPLEMENTED(FATAL);
172}
173
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700174void Assembler::StoreStackOffsetToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED,
175 FrameOffset fr_offs ATTRIBUTE_UNUSED,
176 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700177 UNIMPLEMENTED(FATAL);
178}
179
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700180void Assembler::StoreStackOffsetToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED,
181 FrameOffset fr_offs ATTRIBUTE_UNUSED,
182 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700183 UNIMPLEMENTED(FATAL);
184}
185
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700186void Assembler::StoreStackPointerToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700187 UNIMPLEMENTED(FATAL);
188}
189
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700190void Assembler::StoreStackPointerToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700191 UNIMPLEMENTED(FATAL);
192}
193
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700194void Assembler::LoadFromThread32(ManagedRegister dest ATTRIBUTE_UNUSED,
195 ThreadOffset<4> src ATTRIBUTE_UNUSED,
196 size_t size ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700197 UNIMPLEMENTED(FATAL);
198}
199
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700200void Assembler::LoadFromThread64(ManagedRegister dest ATTRIBUTE_UNUSED,
201 ThreadOffset<8> src ATTRIBUTE_UNUSED,
202 size_t size ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700203 UNIMPLEMENTED(FATAL);
204}
205
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700206void Assembler::LoadRawPtrFromThread32(ManagedRegister dest ATTRIBUTE_UNUSED,
207 ThreadOffset<4> offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700208 UNIMPLEMENTED(FATAL);
209}
210
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700211void Assembler::LoadRawPtrFromThread64(ManagedRegister dest ATTRIBUTE_UNUSED,
212 ThreadOffset<8> offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700213 UNIMPLEMENTED(FATAL);
214}
215
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700216void Assembler::CopyRawPtrFromThread32(FrameOffset fr_offs ATTRIBUTE_UNUSED,
217 ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED,
218 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700219 UNIMPLEMENTED(FATAL);
220}
221
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700222void Assembler::CopyRawPtrFromThread64(FrameOffset fr_offs ATTRIBUTE_UNUSED,
223 ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED,
224 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700225 UNIMPLEMENTED(FATAL);
226}
227
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700228void Assembler::CopyRawPtrToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED,
229 FrameOffset fr_offs ATTRIBUTE_UNUSED,
230 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700231 UNIMPLEMENTED(FATAL);
232}
233
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700234void Assembler::CopyRawPtrToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED,
235 FrameOffset fr_offs ATTRIBUTE_UNUSED,
236 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700237 UNIMPLEMENTED(FATAL);
238}
239
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700240void Assembler::CallFromThread32(ThreadOffset<4> offset ATTRIBUTE_UNUSED,
241 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700242 UNIMPLEMENTED(FATAL);
243}
244
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700245void Assembler::CallFromThread64(ThreadOffset<8> offset ATTRIBUTE_UNUSED,
246 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700247 UNIMPLEMENTED(FATAL);
248}
249
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700250} // namespace art