blob: a7cb2784a71db006833fa1992a1a41364a9219ac [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
Ian Rogers57b86d42012-03-27 16:05:41 -070022#include "arm/assembler_arm.h"
Serban Constantinescued8dd492014-02-11 14:15:10 +000023#include "arm64/assembler_arm64.h"
jeffhao7fbee072012-08-24 17:56:54 -070024#include "mips/assembler_mips.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "x86/assembler_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "globals.h"
27#include "memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070028
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070029namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070030
31static byte* NewContents(size_t capacity) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070032 return new byte[capacity];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070033}
34
35
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070036AssemblerBuffer::AssemblerBuffer() {
37 static const size_t kInitialBufferCapacity = 4 * KB;
38 contents_ = NewContents(kInitialBufferCapacity);
39 cursor_ = contents_;
40 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
41 fixup_ = NULL;
Ian Rogers45a76cb2011-07-21 22:00:15 -070042 slow_path_ = NULL;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070043#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070044 has_ensured_capacity_ = false;
45 fixups_processed_ = false;
46#endif
47
48 // Verify internal state.
49 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070050 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070051}
52
53
54AssemblerBuffer::~AssemblerBuffer() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070055 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070056}
57
58
59void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
60 AssemblerFixup* fixup = fixup_;
61 while (fixup != NULL) {
62 fixup->Process(region, fixup->position());
63 fixup = fixup->previous();
64 }
65}
66
67
68void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
69 // Copy the instructions from the buffer.
70 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
71 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070072 // Process fixups in the instructions.
73 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070074#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070075 fixups_processed_ = true;
76#endif
77}
78
79
80void AssemblerBuffer::ExtendCapacity() {
81 size_t old_size = Size();
82 size_t old_capacity = Capacity();
83 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
84
85 // Allocate the new data area and copy contents of the old one to it.
86 byte* new_contents = NewContents(new_capacity);
87 memmove(reinterpret_cast<void*>(new_contents),
88 reinterpret_cast<void*>(contents_),
89 old_size);
90
91 // Compute the relocation delta and switch to the new contents area.
92 ptrdiff_t delta = new_contents - contents_;
93 contents_ = new_contents;
94
95 // Update the cursor and recompute the limit.
96 cursor_ += delta;
97 limit_ = ComputeLimit(new_contents, new_capacity);
98
99 // Verify internal state.
100 CHECK_EQ(Capacity(), new_capacity);
101 CHECK_EQ(Size(), old_size);
102}
103
Ian Rogers2c8f6532011-09-02 17:16:34 -0700104
105Assembler* Assembler::Create(InstructionSet instruction_set) {
jeffhao7fbee072012-08-24 17:56:54 -0700106 switch (instruction_set) {
107 case kArm:
108 case kThumb2:
109 return new arm::ArmAssembler();
Serban Constantinescued8dd492014-02-11 14:15:10 +0000110 case kArm64:
111 return new arm64::Arm64Assembler();
jeffhao7fbee072012-08-24 17:56:54 -0700112 case kMips:
113 return new mips::MipsAssembler();
Ian Rogersbefbd572014-03-06 01:13:39 -0800114 case kX86: // Fall-through.
115 case kX86_64:
jeffhao7fbee072012-08-24 17:56:54 -0700116 return new x86::X86Assembler();
117 default:
118 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
119 return NULL;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700120 }
121}
122
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700123} // namespace art