blob: 931998e2b72c73dae1732b32d0aa9df2b0a53e67 [file] [log] [blame]
Jan Voungf76fd372014-10-16 15:39:22 -07001//===- subzero/src/assembler.cpp - Assembler base class -------------------===//
Jan Voung8acded02014-09-22 18:02:25 -07002// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3// for details. All rights reserved. Use of this source code is governed by a
4// BSD-style license that can be found in the LICENSE file.
5//
6// Modified by the Subzero authors.
7//
Jan Voung33a5f412015-02-03 16:06:42 -08008// This is forked from Dart revision 39313.
9// Please update the revision if we merge back changes from Dart.
10// https://code.google.com/p/dart/wiki/GettingTheSource
11//
Jan Voungf76fd372014-10-16 15:39:22 -070012//===----------------------------------------------------------------------===//
Jan Voung8acded02014-09-22 18:02:25 -070013//
14// The Subzero Code Generator
15//
16// This file is distributed under the University of Illinois Open Source
17// License. See LICENSE.TXT for details.
18//
19//===----------------------------------------------------------------------===//
20//
21// This file implements the Assembler class.
22//
23//===----------------------------------------------------------------------===//
24
25#include "assembler.h"
Jan Voung0faec4c2014-11-05 17:29:56 -080026#include "IceGlobalContext.h"
Jan Voung0faec4c2014-11-05 17:29:56 -080027#include "IceOperand.h"
Jan Voung8acded02014-09-22 18:02:25 -070028
29namespace Ice {
30
31static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) {
32 uintptr_t result = assembler.AllocateBytes(capacity);
33 return result;
34}
35
Jan Voungec270732015-01-12 17:00:22 -080036AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind,
37 const Constant *Value) {
38 AssemblerFixup *F =
39 new (assembler_.Allocate<AssemblerFixup>()) AssemblerFixup();
40 F->set_position(0);
41 F->set_kind(Kind);
42 F->set_value(Value);
Jim Stichnoth9f42d8c2015-02-20 09:20:14 -080043 if (!assembler_.getPreliminary())
44 fixups_.push_back(F);
Jan Voungec270732015-01-12 17:00:22 -080045 return F;
46}
47
Jan Voungf76fd372014-10-16 15:39:22 -070048#ifndef NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -070049AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) {
50 if (buffer->cursor() >= buffer->limit())
51 buffer->ExtendCapacity();
52 // In debug mode, we save the assembler buffer along with the gap
53 // size before we start emitting to the buffer. This allows us to
54 // check that any single generated instruction doesn't overflow the
55 // limit implied by the minimum gap size.
56 buffer_ = buffer;
57 gap_ = ComputeGap();
58 // Make sure that extending the capacity leaves a big enough gap
59 // for any kind of instruction.
60 assert(gap_ >= kMinimumGap);
61 // Mark the buffer as having ensured the capacity.
62 assert(!buffer->HasEnsuredCapacity()); // Cannot nest.
63 buffer->has_ensured_capacity_ = true;
64}
65
66AssemblerBuffer::EnsureCapacity::~EnsureCapacity() {
67 // Unmark the buffer, so we cannot emit after this.
68 buffer_->has_ensured_capacity_ = false;
69 // Make sure the generated instruction doesn't take up more
70 // space than the minimum gap.
71 intptr_t delta = gap_ - ComputeGap();
72 assert(delta <= kMinimumGap);
73}
Jan Voungf76fd372014-10-16 15:39:22 -070074#endif // !NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -070075
76AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) {
77 const intptr_t OneKB = 1024;
78 static const intptr_t kInitialBufferCapacity = 4 * OneKB;
79 contents_ = NewContents(assembler_, kInitialBufferCapacity);
80 cursor_ = contents_;
81 limit_ = ComputeLimit(contents_, kInitialBufferCapacity);
Jan Voungf76fd372014-10-16 15:39:22 -070082#ifndef NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -070083 has_ensured_capacity_ = false;
Jan Voungf76fd372014-10-16 15:39:22 -070084#endif // !NDEBUG
Jan Voung8acded02014-09-22 18:02:25 -070085
86 // Verify internal state.
87 assert(Capacity() == kInitialBufferCapacity);
88 assert(Size() == 0);
89}
90
91AssemblerBuffer::~AssemblerBuffer() {}
92
Jan Voung8acded02014-09-22 18:02:25 -070093void AssemblerBuffer::ExtendCapacity() {
94 intptr_t old_size = Size();
95 intptr_t old_capacity = Capacity();
96 const intptr_t OneMB = 1 << 20;
97 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB);
98 if (new_capacity < old_capacity) {
Jan Voungec270732015-01-12 17:00:22 -080099 llvm::report_fatal_error(
100 "Unexpected overflow in AssemblerBuffer::ExtendCapacity");
Jan Voung8acded02014-09-22 18:02:25 -0700101 }
102
103 // Allocate the new data area and copy contents of the old one to it.
104 uintptr_t new_contents = NewContents(assembler_, new_capacity);
105 memmove(reinterpret_cast<void *>(new_contents),
106 reinterpret_cast<void *>(contents_), old_size);
107
108 // Compute the relocation delta and switch to the new contents area.
109 intptr_t delta = new_contents - contents_;
110 contents_ = new_contents;
111
112 // Update the cursor and recompute the limit.
113 cursor_ += delta;
114 limit_ = ComputeLimit(new_contents, new_capacity);
115
116 // Verify internal state.
117 assert(Capacity() == new_capacity);
118 assert(Size() == old_size);
119}
120
Jan Voung08c3bcd2014-12-01 17:55:16 -0800121llvm::StringRef Assembler::getBufferView() const {
122 return llvm::StringRef(reinterpret_cast<const char *>(buffer_.contents()),
123 buffer_.Size());
124}
125
Jan Voung0faec4c2014-11-05 17:29:56 -0800126void Assembler::emitIASBytes(GlobalContext *Ctx) const {
127 Ostream &Str = Ctx->getStrEmit();
128 intptr_t EndPosition = buffer_.Size();
129 intptr_t CurPosition = 0;
130 const intptr_t FixupSize = 4;
Jan Voungec270732015-01-12 17:00:22 -0800131 for (const AssemblerFixup *NextFixup : fixups()) {
Jan Voung0faec4c2014-11-05 17:29:56 -0800132 intptr_t NextFixupLoc = NextFixup->position();
133 for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) {
134 Str << "\t.byte 0x";
135 Str.write_hex(buffer_.Load<uint8_t>(i));
136 Str << "\n";
137 }
138 Str << "\t.long ";
Jim Stichnoth927f7cc2015-03-19 23:23:00 -0700139 NextFixup->emit(Ctx, buffer_.Load<RelocOffsetT>(NextFixupLoc));
Jan Voungec270732015-01-12 17:00:22 -0800140 if (fixupIsPCRel(NextFixup->kind()))
Jim Stichnoth927f7cc2015-03-19 23:23:00 -0700141 Str << " - .";
Jan Voung0faec4c2014-11-05 17:29:56 -0800142 Str << "\n";
143 CurPosition = NextFixupLoc + FixupSize;
144 assert(CurPosition <= EndPosition);
145 }
146 // Handle any bytes that are not prefixed by a fixup.
147 for (intptr_t i = CurPosition; i < EndPosition; ++i) {
148 Str << "\t.byte 0x";
149 Str.write_hex(buffer_.Load<uint8_t>(i));
150 Str << "\n";
151 }
152}
153
Jan Voung8acded02014-09-22 18:02:25 -0700154} // end of namespace Ice