rrcc/src/Assembler.h

196 lines
7.1 KiB
C++

/*****************************************************************************
* Copyright (c) 2020, rrcc FM10K-Documentation Contributors
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#pragma once
#include "Parser.h"
#include "instructions/Instruction.h"
class Assembler {
private:
const Parser &parser;
std::vector<uint8_t> bytes;
std::unordered_map<std::string, uint32_t> knownRegisters;
std::unordered_map<std::string, std::pair<uint32_t, bool>> entries;
uint32_t placeFunction(uint32_t base, const Function &function) {
std::unordered_map<std::string, uint32_t> addr;
auto declarations = function.declarations; //Copy is necessary
uint32_t offset = base;
//Resolve local labels
for (auto &d : declarations) {
if (!d.label.empty()) {
addr[d.label] = offset;
}
bool hasOperator = false;
for (auto &t : d.tokens) {
if (t.getType() == Token::Type::Operator) {
hasOperator = true;
break;
}
}
if (!hasOperator) {
continue;;
}
auto instr = Instruction::Instruction::getCommandForDeclaration(d);
if (instr == nullptr) {
throw std::runtime_error("Unknown or invalid declaration");
}
auto b = instr->toBytes();
offset += b.size();
}
offset = base;
bool needsReLabeling = false;
for (auto &d : declarations) {
for (auto &t : d.tokens) {
if (t.getType() == Token::Type::CodeLabel) {
if (t.getTextValue().at(0) == '.') {
if (addr.find(t.getTextValue()) != addr.end()) {
t.valueImmediate = addr.at(t.getTextValue());
} else {
throw std::runtime_error("Could not find local label " + t.getTextValue());
}
} else if (entries.find(t.getTextValue()) != entries.end()) {
t.valueImmediate = entries.at(t.getTextValue()).first;
} else {
needsReLabeling = true;
}
} else if (t.getType() == Token::Type::RegisterName) {
if (knownRegisters.find(t.getTextValue()) != knownRegisters.end()) {
t.valueImmediate = knownRegisters.at(t.getTextValue());
} else {
throw std::runtime_error("Could not find named register " + t.getTextValue());
}
}
}
}
for (auto &d : declarations) {
auto instr = Instruction::Instruction::getCommandForDeclaration(d);
if (instr == nullptr) {
throw std::runtime_error("Unknown or invalid declaration");
}
if (!d.label.empty()) {
addr[d.label] = offset;
}
auto data = instr->toBytes();
if (offset + data.size() > +bytes.size()) {
throw std::runtime_error("Instruction encoding exceeds image size");
}
std::copy(data.begin(), data.end(), bytes.begin() + offset);
offset += data.size();
}
entries[function.label] = {base, needsReLabeling};
return offset + 4; //Free bytes between functions
}
const Function *getFunctionByLabel(const std::string &l) {
for (auto &f : parser.getTree()) {
if (f.label == l) {
return &f;
}
}
return nullptr;
}
public:
Assembler(const Parser &parser) : parser(parser) {
}
//Default max size 1 MiB
void assemble(uint32_t baseAddress = 0x08000, uint32_t imageSize = 0x100000) {
entries.clear();
bytes.clear();
bytes.resize(imageSize - 1, (uint8_t) Instruction::Instruction::CommandOp::END);
uint32_t freeOffsetLocation = baseAddress;
auto entrypoint = getFunctionByLabel("entrypoint");
if (entrypoint == nullptr) {
throw std::runtime_error("Entrypoint function not found");
}
freeOffsetLocation = placeFunction(freeOffsetLocation, *entrypoint);
while (true) {
bool changed = false;
for (auto &fEntry : entries) {
if (fEntry.second.second) {
auto f = getFunctionByLabel(fEntry.first);
for (auto &d : f->declarations) {
for (auto &t : d.tokens) {
if (t.getType() == Token::Type::CodeLabel && t.getTextValue().at(0) != '.') {
if (entries.find(t.getTextValue()) == entries.end()) {
changed = true;
auto fUsed = getFunctionByLabel(t.getTextValue());
if (fUsed == nullptr) {
throw std::runtime_error(
t.getTextValue() + " function not found, in use by " + fEntry.first);
}
freeOffsetLocation = placeFunction(freeOffsetLocation, *fUsed);
}
}
}
}
placeFunction(fEntry.second.first, *f);
}
}
if (!changed) {
break;
}
}
}
const auto &getImage() const {
return bytes;
}
};