rrcc/src/Parser.cpp
DataHoarder 5d9ed3cd82
Some checks failed
continuous-integration/drone Build is running
continuous-integration/drone/push Build is failing
Fixed AsValue and AsIndirect
2021-08-06 00:14:58 +02:00

383 lines
22 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.
*****************************************************************************/
#include <iostream>
#include "Parser.h"
#include "BaseRegisters.h"
void Parser::parse() {
offset = 0;
functions.clear();
std::string currentValue;
uint8_t c;
currentValue.reserve(16);
uint32_t lineNumber = 1;
std::vector<std::vector<std::pair<Token::Type, size_t>>> states = {
/* 0 */ {{Token::Type::Label, 1}, {Token::Type::Directive, 4}},
/* 1 */
{{Token::Type::LocalLabel, 2}, {Token::Type::Operator, 3}},
/* 2 */
{{Token::Type::LocalLabel, 2}, {Token::Type::Operator, 3}, {Token::Type::Label, 1}, {Token::Type::Directive, 4}},
/* 3 */
{{Token::Type::Offset, 3}, {Token::Type::CodeLocation, 3}, {Token::Type::CodeLabel, 3}, {Token::Type::RegisterLocation, 3}, {Token::Type::Immediate, 3}, {Token::Type::BareValue, 3}, {Token::Type::AsIndirect, 3}, {Token::Type::AsValue, 3}},
/* 4 */
{{Token::Type::BareValue, 5}},
/* 5 */
{{Token::Type::Offset, 3}, {Token::Type::CodeLocation, 5}, {Token::Type::CodeLabel, 5}, {Token::Type::RegisterLocation, 5}, {Token::Type::Immediate, 5}, {Token::Type::BareValue, 5}, {Token::Type::AsIndirect, 3}, {Token::Type::AsValue, 3}},
};
size_t currentStateIndex = 0;
const size_t resetStateIndex = 2;
bool isComment = false;
Function currentFunction;
Declaration currentDeclaration;
std::string currentComment;
bool isDocComment = false;
while (true) {
try{
c = getByte();
if (c == '\n' || c == '\0' || (!isComment && (c == ' ' || c == '\t' || c == ',' || c == ';'))) {
//End token
//TODO: identify Token
if (!currentValue.empty()) {
bool foundToken = false;
for (auto &t : states[currentStateIndex]) {
switch (t.first) {
case Token::Type::Label:
if (isLabel(currentValue)) {
if (!currentFunction.label.empty()) {
functions.emplace_back(std::move(currentFunction));
currentFunction = Function();
}
currentFunction.label = currentValue.substr(0, currentValue.size() - 1);
foundToken = true;
}
break;
case Token::Type::LocalLabel:
if (isLocalLabel(currentValue)) {
currentDeclaration.label = currentValue.substr(0, currentValue.size() - 1);
foundToken = true;
}
break;
case Token::Type::Operator:
if (isOperator(currentValue)) {
currentDeclaration.tokens.emplace_back(Token(t.first, currentValue));
foundToken = true;
}
break;
case Token::Type::RegisterLocation:
if (isRegisterLocation(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos),
std::stoul(currentValue.substr(1, std::string::npos), nullptr, 0)));
foundToken = true;
}
break;
case Token::Type::CodeLabel:
if (isCodeLabel(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos)));
foundToken = true;
}
break;
case Token::Type::CodeLocation:
if (isCodeLocation(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos),
std::stoul(currentValue.substr(1, std::string::npos), nullptr, 0)));
foundToken = true;
}
break;
case Token::Type::Immediate:
if (isImmediate(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue, std::stoul(currentValue, nullptr, 0)));
foundToken = true;
}
break;
case Token::Type::AsValue:
if (currentValue == "&" && !currentDeclaration.tokens.empty()) {
auto& prevValue = currentDeclaration.tokens.at(currentDeclaration.tokens.size() - 1);
if(prevValue.getType() == Token::Type::Immediate || prevValue.getType() == Token::Type::CodeLabel || prevValue.getType() == Token::Type::CodeLocation || prevValue.getType() == Token::Type::RegisterLocation){
prevValue.type = Token::Type::Immediate;
}else{
throw std::runtime_error("Cannot apply AsValue to value not numeric");
}
foundToken = true;
}
break;
case Token::Type::AsIndirect:
if (currentValue == "*" && !currentDeclaration.tokens.empty()) {
auto& prevValue = currentDeclaration.tokens.at(currentDeclaration.tokens.size() - 1);
if(prevValue.getType() == Token::Type::Immediate || prevValue.getType() == Token::Type::CodeLabel || prevValue.getType() == Token::Type::CodeLocation || prevValue.getType() == Token::Type::RegisterLocation){
prevValue.type = Token::Type::RegisterLocation;
}else{
throw std::runtime_error("Cannot apply AsIndirect to value not numeric");
}
foundToken = true;
}
break;
case Token::Type::Offset:
if (isOffset(currentValue) && !currentDeclaration.tokens.empty()) {
auto& prevValue = currentDeclaration.tokens.at(currentDeclaration.tokens.size() - 1);
if(prevValue.getType() == Token::Type::Immediate || prevValue.getType() == Token::Type::CodeLabel || prevValue.getType() == Token::Type::CodeLocation || prevValue.getType() == Token::Type::RegisterLocation){
auto s = currentValue.at(0);
if(s == '+'){
prevValue.valueImmediate += std::stoi(currentValue.substr(1), nullptr, 0);
}else if(s == '-'){
prevValue.valueImmediate -= std::stoi(currentValue.substr(1), nullptr, 0);
}else if(s == '<'){
prevValue.valueImmediate <<= std::stoi(currentValue.substr(1), nullptr, 0);
}else {
prevValue.valueImmediate += std::stoi(currentValue, nullptr, 0);
}
}else{
throw std::runtime_error("Cannot apply Offset to value not numeric");
}
foundToken = true;
}
break;
case Token::Type::Directive:
if (isDirective(currentValue)) {
if(currentValue == ".constant" || currentValue == ".reserve" || currentValue == ".bitfield"){
if (!currentFunction.label.empty()) {
functions.emplace_back(std::move(currentFunction));
currentFunction = Function();
}
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue));
foundToken = true;
}
}
break;
case Token::Type::BareValue:
if (isAlphaNumeric(currentValue)) {
if (currentDeclaration.tokens.size() == 1 &&
currentDeclaration.tokens.at(0).getType() == Token::Type::Directive) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue));
} else {
if (variables.find(currentValue) != variables.end()) {
for (auto &token : variables[currentValue]) {
currentDeclaration.tokens.emplace_back(token);
}
} else {
throw std::runtime_error("Unknown variable " + currentValue);
}
}
foundToken = true;
}
break;
default:
break;
}
if (foundToken) {
currentValue.clear();
currentStateIndex = t.second;
break;
}
}
if (!foundToken) {
//TODO: ERROR
throw std::runtime_error("Could not find new Token state, unexpected value = " + currentValue);
}
}
if (c == '\n' || c == '\0') {
isComment = false;
isDocComment = false;
if (resetStateIndex < currentStateIndex) {
currentStateIndex = resetStateIndex;
}
if (!currentDeclaration.tokens.empty()) {
if (currentDeclaration.tokens.size() >= 2 && currentDeclaration.tokens.at(0).getType() == Token::Type::Directive && currentDeclaration.tokens.at(0).getTextValue() == ".reserve") {
if (currentDeclaration.tokens.at(0).getTextValue() == ".reserve") {
uint32_t addr;
bool foundValue = false;
if(currentDeclaration.tokens.size() == 2 || currentDeclaration.tokens.at(2).getNumericValue() == 0){
uint32_t scratchSize = (uint32_t) MgmtRegisters::BSM_SCRATCH_END - (uint32_t) MgmtRegisters::BSM_SCRATCH_START;
addr = 0;
for(; addr <= scratchSize; ++addr){
if(reservations.find(addr) == reservations.end()){
//Found value!
foundValue = true;
break;
}
}
}else{
addr = currentDeclaration.tokens.at(2).getNumericValue();
foundValue = true;
}
if(!foundValue){
throw std::runtime_error("Could not find reservation " + currentDeclaration.tokens.at(1).getTextValue() + " for value " + std::to_string(addr));
}else if(reservations.find(addr) == reservations.end()){
reservations[addr] = currentDeclaration.tokens.at(1).getTextValue();
variables[currentDeclaration.tokens.at(1).getTextValue()] = {Token(Token::Type::RegisterLocation, std::to_string((uint32_t) MgmtRegisters::BSM_SCRATCH_START + addr), (uint32_t) MgmtRegisters::BSM_SCRATCH_START + addr)};
}else{
//Collision
throw std::runtime_error("Reservation " + currentDeclaration.tokens.at(1).getTextValue() + " collides value " + std::to_string(currentDeclaration.tokens.at(2).getNumericValue()));
}
}
currentDeclaration = Declaration();
currentStateIndex = 0;
}else if (currentDeclaration.tokens.size() >= 3 && currentDeclaration.tokens.at(0).getType() == Token::Type::Directive) {
if (currentDeclaration.tokens.at(0).getTextValue() == ".constant") {
std::vector<Token> tokens;
for (auto it = currentDeclaration.tokens.begin() + 2;
it < currentDeclaration.tokens.end(); ++it) {
tokens.push_back(*it);
}
variables[currentDeclaration.tokens.at(1).getTextValue()] = tokens;
}else if (currentDeclaration.tokens.at(0).getTextValue() == ".bitfield") {
auto& fieldName = currentDeclaration.tokens.at(1).getTextValue();
uint32_t fieldBitOffset = currentDeclaration.tokens.at(2).getNumericValue();
uint32_t fieldBitSize = currentDeclaration.tokens.size() > 3 ? currentDeclaration.tokens.at(3).getNumericValue() : 1;
uint32_t fieldBitMask = (1 << fieldBitSize) - 1;
variables[fieldName] = {Token(Token::Type::Immediate, std::to_string(fieldBitMask << fieldBitOffset), fieldBitMask << fieldBitOffset)};
variables[fieldName + ".not"] = {Token(Token::Type::Immediate, std::to_string((~(fieldBitMask << fieldBitOffset)) & 0xFFFFFFFF), (~(fieldBitMask << fieldBitOffset)) & 0xFFFFFFFF)};
variables[fieldName + ".offset"] = {Token(Token::Type::Immediate, std::to_string(fieldBitOffset), fieldBitOffset)};
variables[fieldName + ".size"] = {Token(Token::Type::Immediate, std::to_string(fieldBitSize), fieldBitSize)};
variables[fieldName + ".mask"] = {Token(Token::Type::Immediate, std::to_string(fieldBitMask), fieldBitMask)};
}
currentDeclaration = Declaration();
currentStateIndex = 0;
} else {
currentDeclaration.comment = currentComment;
currentComment.clear();
currentFunction.declarations.emplace_back(std::move(currentDeclaration));
currentDeclaration = Declaration();
}
}else if (!currentComment.empty()){
currentComment += "\n";
}
if (c == '\0') {
functions.emplace_back(std::move(currentFunction));
break;
}else{
lineNumber++;
}
}
if (c == ';') {
isComment = true;
}
} else if (!isComment) {
currentValue += c;
} else if (!isDocComment){
if((currentComment.empty() || currentComment.back() == '\n') && c == ';'){ //DocComment, should not appear
isDocComment = true;
}else{
currentComment += c;
}
}
}catch (const std::exception& e){
std::cerr << "Exception: " << e.what() << " on " << currentFunction.label << " (line# " << std::dec << lineNumber << ")\n";
throw e;
}
}
}
bool Parser::isIteratorAllowedValues(std::string::const_iterator it, std::string::const_iterator end,
const std::vector<uint8_t> &values) {
while (it != end) {
if (!isAllowedValues(*it, values)) {
return false;
}
it++;
}
return true;
}
bool Parser::isAllowedValues(uint8_t c, const std::vector<uint8_t> &values) {
return std::find(values.begin(), values.end(), c) != values.end();
}
bool Parser::isOperator(const std::string &v) {
return isIteratorAllowedValues(v.begin(), v.end(), getOperatorValues());
}
bool Parser::isRegisterLocation(const std::string &v) {
return v.size() >= 2 && v.at(0) == '%' && isIteratorAllowedValues(v.begin() + 1, v.end(), getNumericValues());
}
bool Parser::isCodeLabel(const std::string &v) {
return v.size() >= 2 && v.at(0) == '@' &&
((v.at(1) == '.' && isIteratorAllowedValues(v.begin() + 2, v.end(), getAlphanumericValues())) ||
isIteratorAllowedValues(v.begin() + 1, v.end(), getAlphanumericValues()));
}
bool Parser::isCodeLocation(const std::string &v) {
return v.size() >= 2 && v.at(0) == '@' && isIteratorAllowedValues(v.begin() + 1, v.end(), getNumericValues());
}
bool Parser::isImmediate(const std::string &v) {
return isIteratorAllowedValues(v.begin(), v.end(), getNumericValues());
}
bool Parser::isLocalLabel(const std::string &v) {
return v.size() >= 3 && v.at(0) == '.' && isAllowedValues(v.at(1), getAlphaValues()) && v.at(v.size() - 1) == ':' &&
isIteratorAllowedValues(v.begin() + 2, v.end() - 1, getAlphanumericValues());
}
bool Parser::isLabel(const std::string &v) {
return v.size() >= 2 && isAllowedValues(v.at(0), getAlphaValues()) && v.at(v.size() - 1) == ':' &&
isIteratorAllowedValues(v.begin() + 1, v.end() - 1, getAlphanumericValues());
}
bool Parser::isOffset(const std::string &v) {
return v.size() >= 2 && (v.at(0) == '+' || v.at(0) == '-' || v.at(0) == '<') && isIteratorAllowedValues(v.begin() + 1, v.end(), getNumericValues());
}
bool Parser::isDirective(const std::string &v) {
return v.size() >= 2 && v.at(0) == '.' && isIteratorAllowedValues(v.begin() + 1, v.end(), getAlphanumericValues());
}
bool Parser::isAlphaNumeric(const std::string &v) {
return isIteratorAllowedValues(v.begin(), v.end(), getAlphanumericValues());
}