1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#include "stdafx.h"
#include "Output.h"
#include "Asm.h"
#include "AsmOut.h"
#include "Gc/DwarfTable.h"
#include "Code/Binary.h"
#include "Code/Dwarf/Stream.h"
#include "Utils/Bitwise.h"
namespace code {
namespace arm64 {
// Code are always 32-bit long, we use that fact.
const Nat codeAlignment = 4;
// We assume data alignment of -8.
const Int dataAlignment = -8;
// Convert to DWARF registers.
Nat dwarfRegister(Reg reg) {
Nat r = intRegNumber(reg);
// SP is 31
if (r == 32)
return 31;
return r;
}
// Convert from DWARF register.
Reg fromDwarfRegister(Nat reg) {
// We only need to switch SP.
if (reg == 31)
reg = 32;
return ptrr(reg);
}
void initCIE(CIE *cie) {
// Return value is in x30
Nat pos = code::dwarf::initStormCIE(cie, codeAlignment, dataAlignment, 30);
code::dwarf::DStream out(cie->data, CIE_DATA, pos);
// All functions on ARM start with sp pointing directly at the CFA. Register 31 is SP.
out.putUOp(DW_CFA_def_cfa, 31, 0);
}
#ifndef DEBUG
#undef assert
#define assert(...)
#endif
CodeOut::CodeOut(Binary *owner, Array<Nat> *lbls, Nat size, Nat numRefs) {
// Properly align 'size'.
this->size = size = roundUp(size, Nat(sizeof(void *)));
// Initialize our members.
this->owner = owner;
codeRefs = new (this) Array<TObject *>();
code = (byte *)runtime::allocCode(engine(), size, numRefs + 3);
labels = lbls;
pos = 0;
ref = 3;
GcCode *refs = runtime::codeRefs(code);
// Store the binary (owner) first.
refs->refs[0].offset = 0;
refs->refs[0].kind = GcCodeRef::ptrStorage;
refs->refs[0].pointer = owner;
// Store 'codeRefs' to keep the updaters alive.
refs->refs[1].offset = 0;
refs->refs[1].kind = GcCodeRef::ptrStorage;
refs->refs[1].pointer = codeRefs;
// An entry for the DWARF unwinding information.
FDE *unwind = storm::dwarfTable().alloc(code, &initCIE);
fnInfo.set(unwind, codeAlignment, dataAlignment, true, &dwarfRegister);
refs->refs[2].offset = 0;
refs->refs[2].kind = GcCodeRef::dwarfInfo;
refs->refs[2].pointer = unwind;
}
void CodeOut::putByte(Byte b) {
assert(pos < size);
code[pos++] = b;
}
void CodeOut::putInt(Nat w) {
assert(pos + 3 < size);
Nat *to = (Nat *)&code[pos];
*to = w;
pos += 4;
}
void CodeOut::putLong(Word w) {
assert(pos + 7 < size);
Word *to = (Word *)&code[pos];
*to = w;
pos += 8;
}
void CodeOut::putPtr(Word w) {
assert(pos + 7 < size);
Word *to = (Word *)&code[pos];
*to = w;
pos += 8;
}
void CodeOut::align(Nat to) {
pos = roundUp(pos, to);
}
void CodeOut::putGc(GcCodeRef::Kind kind, Nat size, Word w) {
GcCode *refs = runtime::codeRefs(code);
assert(ref < refs->refCount);
refs->refs[ref].offset = pos;
refs->refs[ref].kind = kind;
refs->refs[ref].pointer = (void *)w;
ref++;
// The actual contents will be updated later...
pos += size;
}
void CodeOut::markGc(GcCodeRef::Kind kind, Nat size, Word w) {
GcCode *refs = runtime::codeRefs(code);
assert(ref < refs->refCount);
assert(pos >= size);
refs->refs[ref].offset = pos - size;
refs->refs[ref].kind = kind;
refs->refs[ref].pointer = (void *)w;
ref++;
}
void CodeOut::putGcPtr(Word w) {
GcCode *refs = runtime::codeRefs(code);
assert(ref < refs->refCount);
refs->refs[ref].offset = pos;
refs->refs[ref].kind = GcCodeRef::rawPtr;
refs->refs[ref].pointer = (void *)w;
ref++;
putPtr(w);
}
void CodeOut::putGcRelative(Word w) {
GcCode *refs = runtime::codeRefs(code);
assert(ref < refs->refCount);
refs->refs[ref].offset = pos;
refs->refs[ref].kind = GcCodeRef::relativePtr;
refs->refs[ref].pointer = (void *)w;
ref++;
putPtr(0); // Will be updated later...
}
void CodeOut::putRelativeStatic(Word w) {
GcCode *refs = runtime::codeRefs(code);
assert(ref < refs->refCount);
refs->refs[ref].offset = pos;
refs->refs[ref].kind = GcCodeRef::relative;
refs->refs[ref].pointer = (void *)w;
ref++;
putPtr(0); // Will be updated later.
}
void CodeOut::putPtrSelf(Word w) {
GcCode *refs = runtime::codeRefs(code);
assert(ref < refs->refCount);
refs->refs[ref].offset = pos;
refs->refs[ref].kind = GcCodeRef::inside;
refs->refs[ref].pointer = (void *)(w - Word(codePtr()));
ref++;
putPtr(w);
}
Nat CodeOut::tell() const {
return pos;
}
void *CodeOut::codePtr() const {
return code;
}
void CodeOut::markLabel(Nat id) {
// No need. This should already be done for us.
}
void CodeOut::markGcRef(Ref r) {
if (ref == 0)
return;
codeRefs->push(new (this) CodeUpdater(r, owner, code, ref - 1));
}
void CodeOut::markRef(OffsetRef r, Nat data) {
codeRefs->push(new (this) ArmOffsetUpdater(r, owner, code, pos - sizeof(Nat), data));
}
Nat CodeOut::labelOffset(Nat id) {
if (id < labels->count()) {
return labels->at(id);
} else {
assert(false, L"Unknown label id: " + ::toS(id));
return 0;
}
}
Nat CodeOut::toRelative(Nat offset) {
return offset - (pos + 4); // NOTE: All relative things on the X86-64 are 4 bytes long, not 8!
}
}
}
|