Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 17 | #include "assembler.h" |
| 18 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <vector> |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 21 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 22 | #include "base/casts.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 23 | #include "base/globals.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 24 | #include "base/memory_region.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 25 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 26 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 27 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 28 | AssemblerBuffer::AssemblerBuffer(ArenaAllocator* allocator) |
| 29 | : allocator_(allocator) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 30 | static const size_t kInitialBufferCapacity = 4 * KB; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 31 | contents_ = allocator_->AllocArray<uint8_t>(kInitialBufferCapacity, kArenaAllocAssembler); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 32 | cursor_ = contents_; |
| 33 | limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 34 | fixup_ = nullptr; |
| 35 | slow_path_ = nullptr; |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 36 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 37 | has_ensured_capacity_ = false; |
| 38 | fixups_processed_ = false; |
| 39 | #endif |
| 40 | |
| 41 | // Verify internal state. |
| 42 | CHECK_EQ(Capacity(), kInitialBufferCapacity); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 43 | CHECK_EQ(Size(), 0U); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | AssemblerBuffer::~AssemblerBuffer() { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 48 | if (allocator_->IsRunningOnMemoryTool()) { |
| 49 | allocator_->MakeInaccessible(contents_, Capacity()); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 50 | } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) { |
| 55 | AssemblerFixup* fixup = fixup_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 56 | while (fixup != nullptr) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 57 | fixup->Process(region, fixup->position()); |
| 58 | fixup = fixup->previous(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void 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 Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 67 | // Process fixups in the instructions. |
| 68 | ProcessFixups(instructions); |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 69 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 70 | fixups_processed_ = true; |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 75 | void AssemblerBuffer::ExtendCapacity(size_t min_capacity) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 76 | size_t old_size = Size(); |
| 77 | size_t old_capacity = Capacity(); |
Vladimir Marko | 9152fed | 2016-04-20 14:39:47 +0100 | [diff] [blame] | 78 | DCHECK_GT(min_capacity, old_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 79 | size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 80 | new_capacity = std::max(new_capacity, min_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 81 | |
| 82 | // Allocate the new data area and copy contents of the old one to it. |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 83 | contents_ = reinterpret_cast<uint8_t*>( |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 84 | allocator_->Realloc(contents_, old_capacity, new_capacity, kArenaAllocAssembler)); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 85 | |
| 86 | // Update the cursor and recompute the limit. |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 87 | cursor_ = contents_ + old_size; |
| 88 | limit_ = ComputeLimit(contents_, new_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 89 | |
| 90 | // Verify internal state. |
| 91 | CHECK_EQ(Capacity(), new_capacity); |
| 92 | CHECK_EQ(Size(), old_size); |
| 93 | } |
| 94 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 95 | void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 96 | 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 Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 103 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 104 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 105 | } // namespace art |