blob: 57f3b1570a1e438a5e9cabe79ee7984568dccc18 [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_thumb2.h"
Alex Light50fa9932015-08-10 15:30:07 -070024#endif
25#ifdef ART_ENABLE_CODEGEN_arm64
Serban Constantinescued8dd492014-02-11 14:15:10 +000026#include "arm64/assembler_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070027#endif
28#ifdef ART_ENABLE_CODEGEN_mips
jeffhao7fbee072012-08-24 17:56:54 -070029#include "mips/assembler_mips.h"
Alex Light50fa9932015-08-10 15:30:07 -070030#endif
31#ifdef ART_ENABLE_CODEGEN_mips64
Andreas Gampe57b34292015-01-14 15:45:59 -080032#include "mips64/assembler_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070033#endif
34#ifdef ART_ENABLE_CODEGEN_x86
Ian Rogers57b86d42012-03-27 16:05:41 -070035#include "x86/assembler_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070036#endif
37#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070038#include "x86_64/assembler_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070039#endif
Vladimir Marko10ef6942015-10-22 15:25:54 +010040#include "base/casts.h"
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
Vladimir Marko93205e32016-04-13 11:59:46 +010046AssemblerBuffer::AssemblerBuffer(ArenaAllocator* arena)
47 : arena_(arena) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070048 static const size_t kInitialBufferCapacity = 4 * KB;
Vladimir Marko9152fed2016-04-20 14:39:47 +010049 contents_ = arena_->AllocArray<uint8_t>(kInitialBufferCapacity, kArenaAllocAssembler);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070050 cursor_ = contents_;
51 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070052 fixup_ = nullptr;
53 slow_path_ = nullptr;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070054#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070055 has_ensured_capacity_ = false;
56 fixups_processed_ = false;
57#endif
58
59 // Verify internal state.
60 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070061 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070062}
63
64
65AssemblerBuffer::~AssemblerBuffer() {
Vladimir Marko93205e32016-04-13 11:59:46 +010066 if (arena_->IsRunningOnMemoryTool()) {
67 arena_->MakeInaccessible(contents_, Capacity());
68 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070069}
70
71
72void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
73 AssemblerFixup* fixup = fixup_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070074 while (fixup != nullptr) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070075 fixup->Process(region, fixup->position());
76 fixup = fixup->previous();
77 }
78}
79
80
81void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
82 // Copy the instructions from the buffer.
83 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
84 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070085 // Process fixups in the instructions.
86 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070087#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070088 fixups_processed_ = true;
89#endif
90}
91
92
Vladimir Markocf93a5c2015-06-16 11:33:24 +000093void AssemblerBuffer::ExtendCapacity(size_t min_capacity) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070094 size_t old_size = Size();
95 size_t old_capacity = Capacity();
Vladimir Marko9152fed2016-04-20 14:39:47 +010096 DCHECK_GT(min_capacity, old_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070097 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000098 new_capacity = std::max(new_capacity, min_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070099
100 // Allocate the new data area and copy contents of the old one to it.
Vladimir Marko93205e32016-04-13 11:59:46 +0100101 contents_ = reinterpret_cast<uint8_t*>(
102 arena_->Realloc(contents_, old_capacity, new_capacity, kArenaAllocAssembler));
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700103
104 // Update the cursor and recompute the limit.
Vladimir Marko93205e32016-04-13 11:59:46 +0100105 cursor_ = contents_ + old_size;
106 limit_ = ComputeLimit(contents_, new_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700107
108 // Verify internal state.
109 CHECK_EQ(Capacity(), new_capacity);
110 CHECK_EQ(Size(), old_size);
111}
112
David Srbeckydd973932015-04-07 20:29:48 +0100113void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() {
Vladimir Marko10ef6942015-10-22 15:25:54 +0100114 uint32_t pc = dchecked_integral_cast<uint32_t>(assembler_->CodeSize());
115 if (delay_emitting_advance_pc_) {
116 uint32_t stream_pos = dchecked_integral_cast<uint32_t>(opcodes_.size());
117 delayed_advance_pcs_.push_back(DelayedAdvancePC {stream_pos, pc});
118 } else {
119 AdvancePC(pc);
120 }
David Srbeckydd973932015-04-07 20:29:48 +0100121}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700122
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700123} // namespace art