blob: d1d2a3d5567953b23806adacb9f8fef4fdb14f19 [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
Vladimir Marko10ef6942015-10-22 15:25:54 +010022#include "base/casts.h"
David Sehr1979c642018-04-26 14:41:18 -070023#include "base/globals.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070024#include "base/memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070025
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070026namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070027
Vladimir Markoca6fff82017-10-03 14:49:14 +010028AssemblerBuffer::AssemblerBuffer(ArenaAllocator* allocator)
29 : allocator_(allocator) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070030 static const size_t kInitialBufferCapacity = 4 * KB;
Vladimir Markoca6fff82017-10-03 14:49:14 +010031 contents_ = allocator_->AllocArray<uint8_t>(kInitialBufferCapacity, kArenaAllocAssembler);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032 cursor_ = contents_;
33 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070034 fixup_ = nullptr;
35 slow_path_ = nullptr;
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070036#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070037 has_ensured_capacity_ = false;
38 fixups_processed_ = false;
39#endif
40
41 // Verify internal state.
42 CHECK_EQ(Capacity(), kInitialBufferCapacity);
Elliott Hughes1f359b02011-07-17 14:27:17 -070043 CHECK_EQ(Size(), 0U);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070044}
45
46
47AssemblerBuffer::~AssemblerBuffer() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010048 if (allocator_->IsRunningOnMemoryTool()) {
49 allocator_->MakeInaccessible(contents_, Capacity());
Vladimir Marko93205e32016-04-13 11:59:46 +010050 }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070051}
52
53
54void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
55 AssemblerFixup* fixup = fixup_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070056 while (fixup != nullptr) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070057 fixup->Process(region, fixup->position());
58 fixup = fixup->previous();
59 }
60}
61
62
63void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
64 // Copy the instructions from the buffer.
65 MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
66 instructions.CopyFrom(0, from);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070067 // Process fixups in the instructions.
68 ProcessFixups(instructions);
Elliott Hughes31f1f4f2012-03-12 13:57:36 -070069#ifndef NDEBUG
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070070 fixups_processed_ = true;
71#endif
72}
73
74
Vladimir Markocf93a5c2015-06-16 11:33:24 +000075void AssemblerBuffer::ExtendCapacity(size_t min_capacity) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070076 size_t old_size = Size();
77 size_t old_capacity = Capacity();
Vladimir Marko9152fed2016-04-20 14:39:47 +010078 DCHECK_GT(min_capacity, old_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070079 size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000080 new_capacity = std::max(new_capacity, min_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070081
82 // Allocate the new data area and copy contents of the old one to it.
Vladimir Marko93205e32016-04-13 11:59:46 +010083 contents_ = reinterpret_cast<uint8_t*>(
Vladimir Markoca6fff82017-10-03 14:49:14 +010084 allocator_->Realloc(contents_, old_capacity, new_capacity, kArenaAllocAssembler));
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070085
86 // Update the cursor and recompute the limit.
Vladimir Marko93205e32016-04-13 11:59:46 +010087 cursor_ = contents_ + old_size;
88 limit_ = ComputeLimit(contents_, new_capacity);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070089
90 // Verify internal state.
91 CHECK_EQ(Capacity(), new_capacity);
92 CHECK_EQ(Size(), old_size);
93}
94
David Srbeckydd973932015-04-07 20:29:48 +010095void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() {
Vladimir Marko10ef6942015-10-22 15:25:54 +010096 uint32_t pc = dchecked_integral_cast<uint32_t>(assembler_->CodeSize());
97 if (delay_emitting_advance_pc_) {
98 uint32_t stream_pos = dchecked_integral_cast<uint32_t>(opcodes_.size());
99 delayed_advance_pcs_.push_back(DelayedAdvancePC {stream_pos, pc});
100 } else {
101 AdvancePC(pc);
102 }
David Srbeckydd973932015-04-07 20:29:48 +0100103}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700104
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700105} // namespace art