blob: 5340dd3a251908b8f7b8eb2f16d2f6e2d2bb1731 [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
Dave Allison65fcc2c2014-04-28 13:45:27 -070022#include "arm/assembler_arm32.h"
23#include "arm/assembler_thumb2.h"
Serban Constantinescued8dd492014-02-11 14:15:10 +000024#include "arm64/assembler_arm64.h"
jeffhao7fbee072012-08-24 17:56:54 -070025#include "mips/assembler_mips.h"
Andreas Gampe57b34292015-01-14 15:45:59 -080026#include "mips64/assembler_mips64.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027#include "x86/assembler_x86.h"
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070028#include "x86_64/assembler_x86_64.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "globals.h"
30#include "memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070031
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070032namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070033
Ian Rogers13735952014-10-08 12:43:28 -070034static uint8_t* NewContents(size_t capacity) {
35 return new uint8_t[capacity];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070036}
37
38
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070039AssemblerBuffer::AssemblerBuffer() {
40 static const size_t kInitialBufferCapacity = 4 * KB;
41 contents_ = NewContents(kInitialBufferCapacity);
42 cursor_ = contents_;
43 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
44 fixup_ = NULL;
Ian Rogers45a76cb2011-07-21 22:00:15 -070045 slow_path_ = NULL;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070046#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070047 has_ensured_capacity_ = false;
48 fixups_processed_ = false;
49#endif
50
51 // Verify internal state.
52 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070053 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070054}
55
56
57AssemblerBuffer::~AssemblerBuffer() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070058 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070059}
60
61
62void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
63 AssemblerFixup* fixup = fixup_;
64 while (fixup != NULL) {
65 fixup->Process(region, fixup->position());
66 fixup = fixup->previous();
67 }
68}
69
70
71void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
72 // Copy the instructions from the buffer.
73 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
74 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070075 // Process fixups in the instructions.
76 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070077#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070078 fixups_processed_ = true;
79#endif
80}
81
82
83void AssemblerBuffer::ExtendCapacity() {
84 size_t old_size = Size();
85 size_t old_capacity = Capacity();
86 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
87
88 // Allocate the new data area and copy contents of the old one to it.
Ian Rogers13735952014-10-08 12:43:28 -070089 uint8_t* new_contents = NewContents(new_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070090 memmove(reinterpret_cast<void*>(new_contents),
91 reinterpret_cast<void*>(contents_),
92 old_size);
93
94 // Compute the relocation delta and switch to the new contents area.
95 ptrdiff_t delta = new_contents - contents_;
Andreas Gampe928f72b2014-09-09 19:53:48 -070096 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070097 contents_ = new_contents;
98
99 // Update the cursor and recompute the limit.
100 cursor_ += delta;
101 limit_ = ComputeLimit(new_contents, new_capacity);
102
103 // Verify internal state.
104 CHECK_EQ(Capacity(), new_capacity);
105 CHECK_EQ(Size(), old_size);
106}
107
Ian Rogers2c8f6532011-09-02 17:16:34 -0700108
109Assembler* Assembler::Create(InstructionSet instruction_set) {
jeffhao7fbee072012-08-24 17:56:54 -0700110 switch (instruction_set) {
111 case kArm:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700112 return new arm::Arm32Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700113 case kThumb2:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700114 return new arm::Thumb2Assembler();
Serban Constantinescued8dd492014-02-11 14:15:10 +0000115 case kArm64:
116 return new arm64::Arm64Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700117 case kMips:
118 return new mips::MipsAssembler();
Andreas Gampe57b34292015-01-14 15:45:59 -0800119 case kMips64:
120 return new mips64::Mips64Assembler();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700121 case kX86:
jeffhao7fbee072012-08-24 17:56:54 -0700122 return new x86::X86Assembler();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700123 case kX86_64:
124 return new x86_64::X86_64Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700125 default:
126 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
127 return NULL;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700128 }
129}
130
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700131void Assembler::StoreImmediateToThread32(ThreadOffset<4> dest ATTRIBUTE_UNUSED,
132 uint32_t imm ATTRIBUTE_UNUSED,
133 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700134 UNIMPLEMENTED(FATAL);
135}
136
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700137void Assembler::StoreImmediateToThread64(ThreadOffset<8> dest ATTRIBUTE_UNUSED,
138 uint32_t imm ATTRIBUTE_UNUSED,
139 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700140 UNIMPLEMENTED(FATAL);
141}
142
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700143void Assembler::StoreStackOffsetToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED,
144 FrameOffset fr_offs ATTRIBUTE_UNUSED,
145 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700146 UNIMPLEMENTED(FATAL);
147}
148
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700149void Assembler::StoreStackOffsetToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED,
150 FrameOffset fr_offs ATTRIBUTE_UNUSED,
151 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700152 UNIMPLEMENTED(FATAL);
153}
154
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700155void Assembler::StoreStackPointerToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700156 UNIMPLEMENTED(FATAL);
157}
158
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700159void Assembler::StoreStackPointerToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700160 UNIMPLEMENTED(FATAL);
161}
162
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700163void Assembler::LoadFromThread32(ManagedRegister dest ATTRIBUTE_UNUSED,
164 ThreadOffset<4> src ATTRIBUTE_UNUSED,
165 size_t size ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700166 UNIMPLEMENTED(FATAL);
167}
168
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700169void Assembler::LoadFromThread64(ManagedRegister dest ATTRIBUTE_UNUSED,
170 ThreadOffset<8> src ATTRIBUTE_UNUSED,
171 size_t size ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700172 UNIMPLEMENTED(FATAL);
173}
174
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700175void Assembler::LoadRawPtrFromThread32(ManagedRegister dest ATTRIBUTE_UNUSED,
176 ThreadOffset<4> offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700177 UNIMPLEMENTED(FATAL);
178}
179
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700180void Assembler::LoadRawPtrFromThread64(ManagedRegister dest ATTRIBUTE_UNUSED,
181 ThreadOffset<8> offs ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700182 UNIMPLEMENTED(FATAL);
183}
184
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700185void Assembler::CopyRawPtrFromThread32(FrameOffset fr_offs ATTRIBUTE_UNUSED,
186 ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED,
187 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700188 UNIMPLEMENTED(FATAL);
189}
190
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700191void Assembler::CopyRawPtrFromThread64(FrameOffset fr_offs ATTRIBUTE_UNUSED,
192 ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED,
193 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700194 UNIMPLEMENTED(FATAL);
195}
196
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700197void Assembler::CopyRawPtrToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED,
198 FrameOffset fr_offs ATTRIBUTE_UNUSED,
199 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700200 UNIMPLEMENTED(FATAL);
201}
202
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700203void Assembler::CopyRawPtrToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED,
204 FrameOffset fr_offs ATTRIBUTE_UNUSED,
205 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700206 UNIMPLEMENTED(FATAL);
207}
208
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700209void Assembler::CallFromThread32(ThreadOffset<4> offset ATTRIBUTE_UNUSED,
210 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700211 UNIMPLEMENTED(FATAL);
212}
213
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700214void Assembler::CallFromThread64(ThreadOffset<8> offset ATTRIBUTE_UNUSED,
215 ManagedRegister scratch ATTRIBUTE_UNUSED) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700216 UNIMPLEMENTED(FATAL);
217}
218
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700219} // namespace art