Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: src/assembler.cpp

Issue 476323004: Start adding an integrated assembler. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: simplify Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // Modified by the Subzero authors.
6 //
7 //===- subzero/src/assembler.cpp - Assembler base class -------------------===//
8 //
9 // The Subzero Code Generator
10 //
11 // This file is distributed under the University of Illinois Open Source
12 // License. See LICENSE.TXT for details.
13 //
14 //===----------------------------------------------------------------------===//
15 //
16 // This file implements the Assembler class.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "assembler.h"
21 #include "IceMemoryRegion.h"
22
23 namespace Ice {
24
25 static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) {
26 uintptr_t result = assembler.AllocateBytes(capacity);
27 #if defined(DEBUG)
28 // Initialize the buffer with kBreakPointInstruction to force a break
29 // point if we ever execute an uninitialized part of the code buffer.
30 Assembler::InitializeMemoryWithBreakpoints(result, capacity);
31 #endif
32 return result;
33 }
34
35 #if defined(DEBUG)
36 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) {
37 if (buffer->cursor() >= buffer->limit())
38 buffer->ExtendCapacity();
39 // In debug mode, we save the assembler buffer along with the gap
40 // size before we start emitting to the buffer. This allows us to
41 // check that any single generated instruction doesn't overflow the
42 // limit implied by the minimum gap size.
43 buffer_ = buffer;
44 gap_ = ComputeGap();
45 // Make sure that extending the capacity leaves a big enough gap
46 // for any kind of instruction.
47 assert(gap_ >= kMinimumGap);
48 // Mark the buffer as having ensured the capacity.
49 assert(!buffer->HasEnsuredCapacity()); // Cannot nest.
50 buffer->has_ensured_capacity_ = true;
51 }
52
53 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() {
54 // Unmark the buffer, so we cannot emit after this.
55 buffer_->has_ensured_capacity_ = false;
56 // Make sure the generated instruction doesn't take up more
57 // space than the minimum gap.
58 intptr_t delta = gap_ - ComputeGap();
59 assert(delta <= kMinimumGap);
60 }
61 #endif
62
63 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) {
64 const intptr_t OneKB = 1024;
65 static const intptr_t kInitialBufferCapacity = 4 * OneKB;
66 contents_ = NewContents(assembler_, kInitialBufferCapacity);
67 cursor_ = contents_;
68 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
69 fixup_ = NULL;
70 #if defined(DEBUG)
71 has_ensured_capacity_ = false;
72 fixups_processed_ = false;
73 #endif
74
75 // Verify internal state.
76 assert(Capacity() == kInitialBufferCapacity);
77 assert(Size() == 0);
78 }
79
80 AssemblerBuffer::~AssemblerBuffer() {}
81
82 void AssemblerBuffer::ProcessFixups(const MemoryRegion &region) {
83 AssemblerFixup *fixup = fixup_;
84 while (fixup != NULL) {
85 fixup->Process(region, fixup->position());
86 fixup = fixup->previous();
87 }
88 }
89
90 void AssemblerBuffer::FinalizeInstructions(const MemoryRegion &instructions) {
91 // Copy the instructions from the buffer.
92 MemoryRegion from(reinterpret_cast<void *>(contents()), Size());
93 instructions.CopyFrom(0, from);
94
95 // Process fixups in the instructions.
96 ProcessFixups(instructions);
97 #if defined(DEBUG)
98 fixups_processed_ = true;
99 #endif
100 }
101
102 void AssemblerBuffer::ExtendCapacity() {
103 intptr_t old_size = Size();
104 intptr_t old_capacity = Capacity();
105 const intptr_t OneMB = 1 << 20;
106 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB);
107 if (new_capacity < old_capacity) {
108 // FATAL
109 llvm_unreachable("Unexpected overflow in AssemblerBuffer::ExtendCapacity");
110 }
111
112 // Allocate the new data area and copy contents of the old one to it.
113 uintptr_t new_contents = NewContents(assembler_, new_capacity);
114 memmove(reinterpret_cast<void *>(new_contents),
115 reinterpret_cast<void *>(contents_), old_size);
116
117 // Compute the relocation delta and switch to the new contents area.
118 intptr_t delta = new_contents - contents_;
119 contents_ = new_contents;
120
121 // Update the cursor and recompute the limit.
122 cursor_ += delta;
123 limit_ = ComputeLimit(new_contents, new_capacity);
124
125 // Verify internal state.
126 assert(Capacity() == new_capacity);
127 assert(Size() == old_size);
128 }
129
130 } // end of namespace Ice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698