Chromium Code Reviews

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: lit test to check encodings, swap complexi8 Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 return result;
28 }
29
30 #if defined(DEBUG)
31 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) {
32 if (buffer->cursor() >= buffer->limit())
33 buffer->ExtendCapacity();
34 // In debug mode, we save the assembler buffer along with the gap
35 // size before we start emitting to the buffer. This allows us to
36 // check that any single generated instruction doesn't overflow the
37 // limit implied by the minimum gap size.
38 buffer_ = buffer;
39 gap_ = ComputeGap();
40 // Make sure that extending the capacity leaves a big enough gap
41 // for any kind of instruction.
42 assert(gap_ >= kMinimumGap);
43 // Mark the buffer as having ensured the capacity.
44 assert(!buffer->HasEnsuredCapacity()); // Cannot nest.
45 buffer->has_ensured_capacity_ = true;
46 }
47
48 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() {
49 // Unmark the buffer, so we cannot emit after this.
50 buffer_->has_ensured_capacity_ = false;
51 // Make sure the generated instruction doesn't take up more
52 // space than the minimum gap.
53 intptr_t delta = gap_ - ComputeGap();
54 assert(delta <= kMinimumGap);
55 }
56 #endif
57
58 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) {
59 const intptr_t OneKB = 1024;
60 static const intptr_t kInitialBufferCapacity = 4 * OneKB;
61 contents_ = NewContents(assembler_, kInitialBufferCapacity);
62 cursor_ = contents_;
63 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
64 fixup_ = NULL;
65 #if defined(DEBUG)
66 has_ensured_capacity_ = false;
67 fixups_processed_ = false;
68 #endif
69
70 // Verify internal state.
71 assert(Capacity() == kInitialBufferCapacity);
72 assert(Size() == 0);
73 }
74
75 AssemblerBuffer::~AssemblerBuffer() {}
76
77 void AssemblerBuffer::ProcessFixups(const MemoryRegion &region) {
78 AssemblerFixup *fixup = fixup_;
79 while (fixup != NULL) {
80 fixup->Process(region, fixup->position());
81 fixup = fixup->previous();
82 }
83 }
84
85 void AssemblerBuffer::FinalizeInstructions(const MemoryRegion &instructions) {
86 // Copy the instructions from the buffer.
87 MemoryRegion from(reinterpret_cast<void *>(contents()), Size());
88 instructions.CopyFrom(0, from);
89
90 // Process fixups in the instructions.
91 ProcessFixups(instructions);
92 #if defined(DEBUG)
93 fixups_processed_ = true;
94 #endif
95 }
96
97 void AssemblerBuffer::ExtendCapacity() {
98 intptr_t old_size = Size();
99 intptr_t old_capacity = Capacity();
100 const intptr_t OneMB = 1 << 20;
101 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB);
102 if (new_capacity < old_capacity) {
103 // FATAL
104 llvm_unreachable("Unexpected overflow in AssemblerBuffer::ExtendCapacity");
105 }
106
107 // Allocate the new data area and copy contents of the old one to it.
108 uintptr_t new_contents = NewContents(assembler_, new_capacity);
109 memmove(reinterpret_cast<void *>(new_contents),
110 reinterpret_cast<void *>(contents_), old_size);
111
112 // Compute the relocation delta and switch to the new contents area.
113 intptr_t delta = new_contents - contents_;
114 contents_ = new_contents;
115
116 // Update the cursor and recompute the limit.
117 cursor_ += delta;
118 limit_ = ComputeLimit(new_contents, new_capacity);
119
120 // Verify internal state.
121 assert(Capacity() == new_capacity);
122 assert(Size() == old_size);
123 }
124
125 } // end of namespace Ice
OLDNEW
« src/IceUtils.h ('K') | « src/assembler.h ('k') | src/assembler_constants_ia32.h » ('j') | no next file with comments »

Powered by Google App Engine