blob: e3045e1a7b9b67991c19e096ce1e9aef67dfdaf0 [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"
Ian Rogers57b86d42012-03-27 16:05:41 -070026#include "x86/assembler_x86.h"
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070027#include "x86_64/assembler_x86_64.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "globals.h"
29#include "memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070030
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070031namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032
33static byte* NewContents(size_t capacity) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070034 return new byte[capacity];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035}
36
37
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070038AssemblerBuffer::AssemblerBuffer() {
39 static const size_t kInitialBufferCapacity = 4 * KB;
40 contents_ = NewContents(kInitialBufferCapacity);
41 cursor_ = contents_;
42 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
43 fixup_ = NULL;
Ian Rogers45a76cb2011-07-21 22:00:15 -070044 slow_path_ = NULL;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070045#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070046 has_ensured_capacity_ = false;
47 fixups_processed_ = false;
48#endif
49
50 // Verify internal state.
51 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070052 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070053}
54
55
56AssemblerBuffer::~AssemblerBuffer() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070057 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070058}
59
60
61void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
62 AssemblerFixup* fixup = fixup_;
63 while (fixup != NULL) {
64 fixup->Process(region, fixup->position());
65 fixup = fixup->previous();
66 }
67}
68
69
70void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
71 // Copy the instructions from the buffer.
72 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
73 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070074 // Process fixups in the instructions.
75 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070076#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070077 fixups_processed_ = true;
78#endif
79}
80
81
82void AssemblerBuffer::ExtendCapacity() {
83 size_t old_size = Size();
84 size_t old_capacity = Capacity();
85 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
86
87 // Allocate the new data area and copy contents of the old one to it.
88 byte* new_contents = NewContents(new_capacity);
89 memmove(reinterpret_cast<void*>(new_contents),
90 reinterpret_cast<void*>(contents_),
91 old_size);
92
93 // Compute the relocation delta and switch to the new contents area.
94 ptrdiff_t delta = new_contents - contents_;
Andreas Gampe928f72b2014-09-09 19:53:48 -070095 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070096 contents_ = new_contents;
97
98 // Update the cursor and recompute the limit.
99 cursor_ += delta;
100 limit_ = ComputeLimit(new_contents, new_capacity);
101
102 // Verify internal state.
103 CHECK_EQ(Capacity(), new_capacity);
104 CHECK_EQ(Size(), old_size);
105}
106
Ian Rogers2c8f6532011-09-02 17:16:34 -0700107
108Assembler* Assembler::Create(InstructionSet instruction_set) {
jeffhao7fbee072012-08-24 17:56:54 -0700109 switch (instruction_set) {
110 case kArm:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700111 return new arm::Arm32Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700112 case kThumb2:
Dave Allison65fcc2c2014-04-28 13:45:27 -0700113 return new arm::Thumb2Assembler();
Serban Constantinescued8dd492014-02-11 14:15:10 +0000114 case kArm64:
115 return new arm64::Arm64Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700116 case kMips:
117 return new mips::MipsAssembler();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700118 case kX86:
jeffhao7fbee072012-08-24 17:56:54 -0700119 return new x86::X86Assembler();
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700120 case kX86_64:
121 return new x86_64::X86_64Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700122 default:
123 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
124 return NULL;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700125 }
126}
127
Ian Rogersdd7624d2014-03-14 17:43:00 -0700128void Assembler::StoreImmediateToThread32(ThreadOffset<4> dest, uint32_t imm,
129 ManagedRegister scratch) {
130 UNIMPLEMENTED(FATAL);
131}
132
133void Assembler::StoreImmediateToThread64(ThreadOffset<8> dest, uint32_t imm,
134 ManagedRegister scratch) {
135 UNIMPLEMENTED(FATAL);
136}
137
138void Assembler::StoreStackOffsetToThread32(ThreadOffset<4> thr_offs,
139 FrameOffset fr_offs,
140 ManagedRegister scratch) {
141 UNIMPLEMENTED(FATAL);
142}
143
144void Assembler::StoreStackOffsetToThread64(ThreadOffset<8> thr_offs,
145 FrameOffset fr_offs,
146 ManagedRegister scratch) {
147 UNIMPLEMENTED(FATAL);
148}
149
150void Assembler::StoreStackPointerToThread32(ThreadOffset<4> thr_offs) {
151 UNIMPLEMENTED(FATAL);
152}
153
154void Assembler::StoreStackPointerToThread64(ThreadOffset<8> thr_offs) {
155 UNIMPLEMENTED(FATAL);
156}
157
158void Assembler::LoadFromThread32(ManagedRegister dest, ThreadOffset<4> src, size_t size) {
159 UNIMPLEMENTED(FATAL);
160}
161
162void Assembler::LoadFromThread64(ManagedRegister dest, ThreadOffset<8> src, size_t size) {
163 UNIMPLEMENTED(FATAL);
164}
165
166void Assembler::LoadRawPtrFromThread32(ManagedRegister dest, ThreadOffset<4> offs) {
167 UNIMPLEMENTED(FATAL);
168}
169
170void Assembler::LoadRawPtrFromThread64(ManagedRegister dest, ThreadOffset<8> offs) {
171 UNIMPLEMENTED(FATAL);
172}
173
174void Assembler::CopyRawPtrFromThread32(FrameOffset fr_offs, ThreadOffset<4> thr_offs,
175 ManagedRegister scratch) {
176 UNIMPLEMENTED(FATAL);
177}
178
179void Assembler::CopyRawPtrFromThread64(FrameOffset fr_offs, ThreadOffset<8> thr_offs,
180 ManagedRegister scratch) {
181 UNIMPLEMENTED(FATAL);
182}
183
184void Assembler::CopyRawPtrToThread32(ThreadOffset<4> thr_offs, FrameOffset fr_offs,
185 ManagedRegister scratch) {
186 UNIMPLEMENTED(FATAL);
187}
188
189void Assembler::CopyRawPtrToThread64(ThreadOffset<8> thr_offs, FrameOffset fr_offs,
190 ManagedRegister scratch) {
191 UNIMPLEMENTED(FATAL);
192}
193
194void Assembler::CallFromThread32(ThreadOffset<4> offset, ManagedRegister scratch) {
195 UNIMPLEMENTED(FATAL);
196}
197
198void Assembler::CallFromThread64(ThreadOffset<8> offset, ManagedRegister scratch) {
199 UNIMPLEMENTED(FATAL);
200}
201
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700202} // namespace art