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 | |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 22 | #ifdef ART_ENABLE_CODEGEN_arm |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 23 | #include "arm/assembler_arm32.h" |
| 24 | #include "arm/assembler_thumb2.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 25 | #endif |
| 26 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 27 | #include "arm64/assembler_arm64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 28 | #endif |
| 29 | #ifdef ART_ENABLE_CODEGEN_mips |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 30 | #include "mips/assembler_mips.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 31 | #endif |
| 32 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 33 | #include "mips64/assembler_mips64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 34 | #endif |
| 35 | #ifdef ART_ENABLE_CODEGEN_x86 |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 36 | #include "x86/assembler_x86.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 37 | #endif |
| 38 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 39 | #include "x86_64/assembler_x86_64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 40 | #endif |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 41 | #include "base/casts.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 42 | #include "globals.h" |
| 43 | #include "memory_region.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 44 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 45 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 46 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 47 | AssemblerBuffer::AssemblerBuffer(ArenaAllocator* arena) |
| 48 | : arena_(arena) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 49 | static const size_t kInitialBufferCapacity = 4 * KB; |
Vladimir Marko | 9152fed | 2016-04-20 14:39:47 +0100 | [diff] [blame] | 50 | contents_ = arena_->AllocArray<uint8_t>(kInitialBufferCapacity, kArenaAllocAssembler); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 51 | cursor_ = contents_; |
| 52 | limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 53 | fixup_ = nullptr; |
| 54 | slow_path_ = nullptr; |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 55 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 56 | has_ensured_capacity_ = false; |
| 57 | fixups_processed_ = false; |
| 58 | #endif |
| 59 | |
| 60 | // Verify internal state. |
| 61 | CHECK_EQ(Capacity(), kInitialBufferCapacity); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 62 | CHECK_EQ(Size(), 0U); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | AssemblerBuffer::~AssemblerBuffer() { |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 67 | if (arena_->IsRunningOnMemoryTool()) { |
| 68 | arena_->MakeInaccessible(contents_, Capacity()); |
| 69 | } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) { |
| 74 | AssemblerFixup* fixup = fixup_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 75 | while (fixup != nullptr) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 76 | fixup->Process(region, fixup->position()); |
| 77 | fixup = fixup->previous(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) { |
| 83 | // Copy the instructions from the buffer. |
| 84 | MemoryRegion from(reinterpret_cast<void*>(contents()), Size()); |
| 85 | instructions.CopyFrom(0, from); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 86 | // Process fixups in the instructions. |
| 87 | ProcessFixups(instructions); |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 88 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 89 | fixups_processed_ = true; |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 94 | void AssemblerBuffer::ExtendCapacity(size_t min_capacity) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 95 | size_t old_size = Size(); |
| 96 | size_t old_capacity = Capacity(); |
Vladimir Marko | 9152fed | 2016-04-20 14:39:47 +0100 | [diff] [blame] | 97 | DCHECK_GT(min_capacity, old_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 98 | 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] | 99 | new_capacity = std::max(new_capacity, min_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 100 | |
| 101 | // 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] | 102 | contents_ = reinterpret_cast<uint8_t*>( |
| 103 | arena_->Realloc(contents_, old_capacity, new_capacity, kArenaAllocAssembler)); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 104 | |
| 105 | // Update the cursor and recompute the limit. |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 106 | cursor_ = contents_ + old_size; |
| 107 | limit_ = ComputeLimit(contents_, new_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 108 | |
| 109 | // Verify internal state. |
| 110 | CHECK_EQ(Capacity(), new_capacity); |
| 111 | CHECK_EQ(Size(), old_size); |
| 112 | } |
| 113 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 114 | void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 115 | uint32_t pc = dchecked_integral_cast<uint32_t>(assembler_->CodeSize()); |
| 116 | if (delay_emitting_advance_pc_) { |
| 117 | uint32_t stream_pos = dchecked_integral_cast<uint32_t>(opcodes_.size()); |
| 118 | delayed_advance_pcs_.push_back(DelayedAdvancePC {stream_pos, pc}); |
| 119 | } else { |
| 120 | AdvancePC(pc); |
| 121 | } |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 122 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 123 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 124 | } // namespace art |