blob: 92ce0b80010ea83d14c7a5fd97950c44f7a347c1 [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"
jeffhao7fbee072012-08-24 17:56:54 -070023#include "mips/assembler_mips.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024#include "x86/assembler_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "globals.h"
26#include "memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070027
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070028namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070029
30static byte* NewContents(size_t capacity) {
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070031 return new byte[capacity];
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032}
33
34
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035AssemblerBuffer::AssemblerBuffer() {
36 static const size_t kInitialBufferCapacity = 4 * KB;
37 contents_ = NewContents(kInitialBufferCapacity);
38 cursor_ = contents_;
39 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
40 fixup_ = NULL;
Ian Rogers45a76cb2011-07-21 22:00:15 -070041 slow_path_ = NULL;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070042#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070043 has_ensured_capacity_ = false;
44 fixups_processed_ = false;
45#endif
46
47 // Verify internal state.
48 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070049 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070050}
51
52
53AssemblerBuffer::~AssemblerBuffer() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070054 delete[] contents_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070055}
56
57
58void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
59 AssemblerFixup* fixup = fixup_;
60 while (fixup != NULL) {
61 fixup->Process(region, fixup->position());
62 fixup = fixup->previous();
63 }
64}
65
66
67void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
68 // Copy the instructions from the buffer.
69 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
70 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070071 // Process fixups in the instructions.
72 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070073#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070074 fixups_processed_ = true;
75#endif
76}
77
78
79void AssemblerBuffer::ExtendCapacity() {
80 size_t old_size = Size();
81 size_t old_capacity = Capacity();
82 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
83
84 // Allocate the new data area and copy contents of the old one to it.
85 byte* new_contents = NewContents(new_capacity);
86 memmove(reinterpret_cast<void*>(new_contents),
87 reinterpret_cast<void*>(contents_),
88 old_size);
89
90 // Compute the relocation delta and switch to the new contents area.
91 ptrdiff_t delta = new_contents - contents_;
92 contents_ = new_contents;
93
94 // Update the cursor and recompute the limit.
95 cursor_ += delta;
96 limit_ = ComputeLimit(new_contents, new_capacity);
97
98 // Verify internal state.
99 CHECK_EQ(Capacity(), new_capacity);
100 CHECK_EQ(Size(), old_size);
101}
102
Ian Rogers2c8f6532011-09-02 17:16:34 -0700103
104Assembler* Assembler::Create(InstructionSet instruction_set) {
jeffhao7fbee072012-08-24 17:56:54 -0700105 switch (instruction_set) {
106 case kArm:
107 case kThumb2:
108 return new arm::ArmAssembler();
109 case kMips:
110 return new mips::MipsAssembler();
111 case kX86:
112 return new x86::X86Assembler();
113 default:
114 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
115 return NULL;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700116 }
117}
118
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700119} // namespace art