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  */
16 
17 #include "assembler.h"
18 
19 #include <algorithm>
20 #include <vector>
21 
22 #include "base/casts.h"
23 #include "base/globals.h"
24 #include "base/memory_region.h"
25 
26 namespace art HIDDEN {
27 
AssemblerBuffer(ArenaAllocator * allocator)28 AssemblerBuffer::AssemblerBuffer(ArenaAllocator* allocator)
29     : allocator_(allocator) {
30   static const size_t kInitialBufferCapacity = 4 * KB;
31   contents_ = allocator_->AllocArray<uint8_t>(kInitialBufferCapacity, kArenaAllocAssembler);
32   cursor_ = contents_;
33   limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
34   fixup_ = nullptr;
35   slow_path_ = nullptr;
36 #ifndef NDEBUG
37   has_ensured_capacity_ = false;
38   fixups_processed_ = false;
39 #endif
40 
41   // Verify internal state.
42   CHECK_EQ(Capacity(), kInitialBufferCapacity);
43   CHECK_EQ(Size(), 0U);
44 }
45 
46 
~AssemblerBuffer()47 AssemblerBuffer::~AssemblerBuffer() {
48   if (allocator_->IsRunningOnMemoryTool()) {
49     allocator_->MakeInaccessible(contents_, Capacity());
50   }
51 }
52 
53 
ProcessFixups(const MemoryRegion & region)54 void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) {
55   AssemblerFixup* fixup = fixup_;
56   while (fixup != nullptr) {
57     fixup->Process(region, fixup->position());
58     fixup = fixup->previous();
59   }
60 #ifndef NDEBUG
61   fixups_processed_ = true;
62 #endif
63 }
64 
65 
ProcessFixups()66 void AssemblerBuffer::ProcessFixups() {
67   MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
68   ProcessFixups(from);
69 }
70 
71 
CopyInstructions(const MemoryRegion & instructions)72 void AssemblerBuffer::CopyInstructions(const MemoryRegion& instructions) {
73   MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
74   instructions.CopyFrom(0, from);
75 }
76 
77 
ExtendCapacity(size_t min_capacity)78 void AssemblerBuffer::ExtendCapacity(size_t min_capacity) {
79   size_t old_size = Size();
80   size_t old_capacity = Capacity();
81   DCHECK_GT(min_capacity, old_capacity);
82   size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB);
83   new_capacity = std::max(new_capacity, min_capacity);
84 
85   // Allocate the new data area and copy contents of the old one to it.
86   contents_ = reinterpret_cast<uint8_t*>(
87       allocator_->Realloc(contents_, old_capacity, new_capacity, kArenaAllocAssembler));
88 
89   // Update the cursor and recompute the limit.
90   cursor_ = contents_ + old_size;
91   limit_ = ComputeLimit(contents_, new_capacity);
92 
93   // Verify internal state.
94   CHECK_EQ(Capacity(), new_capacity);
95   CHECK_EQ(Size(), old_size);
96 }
97 
ImplicitlyAdvancePC()98 void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() {
99   uint32_t pc = dchecked_integral_cast<uint32_t>(assembler_->CodeSize());
100   if (delay_emitting_advance_pc_) {
101     uint32_t stream_pos = dchecked_integral_cast<uint32_t>(opcodes_.size());
102     delayed_advance_pcs_.push_back(DelayedAdvancePC {stream_pos, pc});
103   } else {
104     AdvancePC(pc);
105   }
106 }
107 
108 }  // namespace art
109