Skip to content

Commit

Permalink
Expose all parsers and formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Oct 17, 2024
1 parent bd58ae4 commit 6e574b8
Show file tree
Hide file tree
Showing 46 changed files with 983 additions and 808 deletions.
1 change: 0 additions & 1 deletion src/asm_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "error_reporter.h"
#include "insn_base.h"
#include "option_base.h"
#include "parsers.h"
#include "pseudos.h"
#include "symbol_table.h"
#include "value_parser.h"
Expand Down
89 changes: 1 addition & 88 deletions src/asm_cdp1802.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,61 +39,9 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

/**
* RCA style numbers are the same as IBM plus '#hh' for hexadecimal.
*/
struct RcaNumberParser final : NumberParser, Singleton<RcaNumberParser> {
Error parseNumber(StrScanner &scan, Value &val, Radix defaultRadix) const override {
auto p = scan;
if (*p == '#' && isxdigit(p[1])) {
const auto error = val.read(++p, RADIX_16);
if (error == OK)
scan = p;
return error;
}
return _ibm.parseNumber(scan, val, defaultRadix);
}

private:
const IbmNumberParser _ibm{'X', 'B', 0, 'D'};
};

struct RcaCommentParser final : CommentParser, Singleton<RcaCommentParser> {
bool commentLine(StrScanner &scan) const override {
static constexpr char TEXT_DOTDOT[] PROGMEM = "..";
return scan.iexpectText_P(TEXT_DOTDOT) || endOfLine(scan);
}
bool endOfLine(StrScanner &scan) const override {
return SemicolonCommentParser::singleton().endOfLine(scan);
}
};

struct RcaSymbolParser final : SymbolParser, Singleton<RcaSymbolParser> {
bool functionNameLetter(char c) const override { return symbolLetter(c) || c == '.'; }
bool instructionLetter(char c) const override {
return SymbolParser::instructionLetter(c) || c == '=';
}
bool instructionTerminator(char c) const override { return c == '='; }
};

struct RcaLetterParser final : LetterParser, Singleton<RcaLetterParser> {
bool letterPrefix(StrScanner &scan) const override {
scan.iexpect('T'); // optional
return true;
}
bool stringPrefix(StrScanner &scan) const override {
scan.iexpect('T'); // optional
return true;
}
};

struct RcaFunctionTable final : FunctionTable, Singleton<RcaFunctionTable> {
const Functor *lookupFunction(const StrScanner &name) const override;
};

} // namespace

const ValueParser::Plugins &AsmCdp1802::defaultPlugins() {
const ValueParser::Plugins &defaultPlugins() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return RcaNumberParser::singleton(); }
const CommentParser &comment() const override { return RcaCommentParser::singleton(); }
Expand Down Expand Up @@ -123,41 +71,6 @@ Error AsmCdp1802::setUseReg(bool enable) {

namespace {

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
// Mark that this is 2 bytes value.
stack.pushUnsigned((stack.pop().getUnsigned() & UINT16_MAX) | UINT32_C(0x1000'0000));
return OK;
}
} FN_A;

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned(stack.pop().getUnsigned() & UINT8_MAX);
return OK;
}
} FN_A0;

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() >> 8) & UINT8_MAX);
return OK;
}
} FN_A1;

const Functor *RcaFunctionTable::lookupFunction(const StrScanner &name) const {
if (name.iequals_P(PSTR("A.0")))
return &FN_A0;
if (name.iequals_P(PSTR("A.1")))
return &FN_A1;
if (name.iequals_P(PSTR("A")))
return &FN_A;
return nullptr;
}

constexpr Config::opcode_t BR = 0x30;
constexpr Config::opcode_t LBR = 0xC0;

Expand Down
3 changes: 2 additions & 1 deletion src/asm_cdp1802.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
namespace libasm {
namespace cdp1802 {

const ValueParser::Plugins &defaultPlugins();

struct AsmCdp1802 final : Assembler, Config {
AsmCdp1802(const ValueParser::Plugins &plugins = defaultPlugins());

Expand All @@ -44,7 +46,6 @@ struct AsmCdp1802 final : Assembler, Config {
Error encodeImpl(StrScanner &scan, Insn &insn) const override;
const ConfigBase &config() const override { return *this; }
ConfigSetter &configSetter() override { return *this; }
static const ValueParser::Plugins &defaultPlugins();
};

} // namespace cdp1802
Expand Down
8 changes: 7 additions & 1 deletion src/asm_f3850.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};
} // namespace

const ValueParser::Plugins &AsmF3850::defaultPlugins() {
return ValueParser::Plugins::fairchild();
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return FairchildNumberParser::singleton(); }
const SymbolParser &symbol() const override { return FairchildSymbolParser::singleton(); }
const CommentParser &comment() const override { return StarCommentParser::singleton(); }
const LetterParser &letter() const override { return FairchildLetterParser::singleton(); }
} PLUGINS{};
return PLUGINS;
}

AsmF3850::AsmF3850(const ValueParser::Plugins &plugins)
Expand Down
7 changes: 1 addition & 6 deletions src/asm_i8051.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};
} // namespace

const ValueParser::Plugins &AsmI8051::defaultPlugins() {
static struct final : ValueParser::IntelPlugins {
const SymbolParser &symbol() const override {
return UnderQuestionSymbolParser::singleton();
}
} PLUGINS;
return PLUGINS;
return ValueParser::Plugins::intel();
}

AsmI8051::AsmI8051(const ValueParser::Plugins &plugins)
Expand Down
4 changes: 1 addition & 3 deletions src/asm_i8086.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ struct I8086SymbolParser final : SimpleSymbolParser, Singleton<I8086SymbolParser
} // namespace

const ValueParser::Plugins &AsmI8086::defaultPlugins() {
static struct final : ValueParser::Plugins {
const NumberParser &number() const { return IntelNumberParser::singleton(); }
static struct final : ValueParser::IntelPlugins {
const SymbolParser &symbol() const { return I8086SymbolParser::singleton(); }
const OperatorParser &operators() const { return IntelOperatorParser::singleton(); }
} PLUGINS;
return PLUGINS;
}
Expand Down
7 changes: 1 addition & 6 deletions src/asm_i8096.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};
} // namespace

const ValueParser::Plugins &AsmI8096::defaultPlugins() {
static struct final : ValueParser::IntelPlugins {
const SymbolParser &symbol() const override {
return UnderQuestionSymbolParser::singleton();
}
} PLUGINS;
return PLUGINS;
return ValueParser::Plugins::intel();
}

AsmI8096::AsmI8096(const ValueParser::Plugins &plugins)
Expand Down
43 changes: 0 additions & 43 deletions src/asm_ins8060.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct Ins8060FunctionTable final : FunctionTable, Singleton<Ins8060FunctionTable> {
const Functor *lookupFunction(const StrScanner &) const override;
};

} // namespace

const ValueParser::Plugins &AsmIns8060::defaultPlugins() {
Expand All @@ -58,45 +54,6 @@ AsmIns8060::AsmIns8060(const ValueParser::Plugins &plugins)
reset();
}

namespace {

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() >> 8) & 0xFF);
return OK;
}
} FN_HIGH;

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned(stack.pop().getUnsigned() & 0xFF);
return OK;
}
} FN_LOW;

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
const auto v = stack.pop().getUnsigned();
stack.pushUnsigned(page(v) | offset(v - 1));
return OK;
}
} FN_ADDR;

const Functor *Ins8060FunctionTable::lookupFunction(const StrScanner &name) const {
if (name.iequals_P(TEXT_FN_H))
return &FN_HIGH;
if (name.iequals_P(TEXT_FN_L))
return &FN_LOW;
if (name.iequals_P(TEXT_FN_ADDR))
return &FN_ADDR;
return nullptr;
}

} // namespace

void AsmIns8060::encodeRel8(AsmInsn &insn, const Operand &op) const {
Config::ptrdiff_t delta;
if (op.mode == M_DISP) {
Expand Down
42 changes: 0 additions & 42 deletions src/asm_ins8070.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct Ins8070FunctionTable final : FunctionTable, Singleton<Ins8070FunctionTable> {
const Functor *lookupFunction(const StrScanner &) const override;
};

} // namespace

const ValueParser::Plugins &AsmIns8070::defaultPlugins() {
Expand All @@ -58,44 +54,6 @@ AsmIns8070::AsmIns8070(const ValueParser::Plugins &plugins)
reset();
}

namespace {

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() >> 8) & 0xFF);
return OK;
}
} FN_HIGH;

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned(stack.pop().getUnsigned() & 0xFF);
return OK;
}
} FN_LOW;

const struct : Functor {
int_fast8_t nargs() const override { return 1; }
Error eval(ValueStack &stack, ParserContext &, uint_fast8_t) const override {
stack.pushUnsigned((stack.pop().getUnsigned() - 1) & 0xFFFF);
return OK;
}
} FN_ADDR;

const Functor *Ins8070FunctionTable::lookupFunction(const StrScanner &name) const {
if (name.iequals_P(TEXT_FN_H))
return &FN_HIGH;
if (name.iequals_P(TEXT_FN_L))
return &FN_LOW;
if (name.iequals_P(TEXT_FN_ADDR))
return &FN_ADDR;
return nullptr;
}

} // namespace

void AsmIns8070::emitAbsolute(AsmInsn &insn, const Operand &op) const {
// PC will be +1 before fetching instruction.
const auto target = op.getError() ? 0 : op.val.getUnsigned() - 1;
Expand Down
10 changes: 0 additions & 10 deletions src/asm_mc68000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct Mc68000SymbolParser final : PrefixSymbolParser, Singleton<Mc68000SymbolParser> {
Mc68000SymbolParser() : PrefixSymbolParser(PSTR_DOT, PSTR_UNDER_DOT_DOLLAR) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
bool instructionLetter(char c) const override {
return PrefixSymbolParser::instructionLetter(c) || c == '.';
}
};

} // namespace

const ValueParser::Plugins &AsmMc68000::defaultPlugins() {
Expand Down
14 changes: 2 additions & 12 deletions src/asm_mn1610.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,12 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct Mn1610NumberParser final : IbmNumberParser, Singleton<Mn1610NumberParser> {
Mn1610NumberParser() : IbmNumberParser('X', 0, 0, 0) {}
};

struct Mn1610SymbolParser final : SymbolParser, Singleton<Mn1610SymbolParser> {
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
};

} // namespace

const ValueParser::Plugins &AsmMn1610::defaultPlugins() {
static const struct final : ValueParser::Plugins {
const NumberParser &number() const override { return Mn1610NumberParser::singleton(); }
const SymbolParser &symbol() const override { return Mn1610SymbolParser::singleton(); }
const NumberParser &number() const override { return PanasonicNumberParser::singleton(); }
const SymbolParser &symbol() const override { return PanasonicSymbolParser::singleton(); }
const CommentParser &comment() const override { return StarCommentParser::singleton(); }
const LetterParser &letter() const override { return IbmLetterParser::singleton(); }
} PLUGINS{};
Expand Down
31 changes: 0 additions & 31 deletions src/asm_mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,6 @@ constexpr Pseudo PSEUDOS[] PROGMEM = {
// clang-format on
PROGMEM constexpr Pseudos PSEUDO_TABLE{ARRAY_RANGE(PSEUDOS)};

struct MostekCommentParser final : CommentParser, Singleton<MostekCommentParser> {
bool commentLine(StrScanner &scan) const override {
return SemicolonCommentParser::singleton().commentLine(scan);
}
bool endOfLine(StrScanner &scan) const override {
return StarCommentParser::singleton().endOfLine(scan) ||
SemicolonCommentParser::singleton().endOfLine(scan);
}
};

struct MostekSymbolParser final : SimpleSymbolParser, Singleton<MostekSymbolParser> {
MostekSymbolParser() : SimpleSymbolParser(PSTR_UNDER) {}
bool locationSymbol(StrScanner &scan) const override {
return SymbolParser::locationSymbol(scan, '*');
}
bool instructionLetter(char c) const override {
return SymbolParser::instructionLetter(c) || c == '.' || c == '=' || c == '*' || c == ':';
}
bool instructionTerminator(char c) const override { return c == '='; }
};

struct MostekLetterParser final : LetterParser, Singleton<MostekLetterParser> {
Error parseLetter(StrScanner &scan, char &letter) const {
return MotorolaLetterParser::singleton().parseLetter(scan, letter);
}
char stringDelimiter(StrScanner &scan) const {
const auto c = scan.expect('\'');
return c ? c : scan.expect('"');
}
};

} // namespace

const ValueParser::Plugins &AsmMos6502::defaultPlugins() {
Expand Down
Loading

0 comments on commit 6e574b8

Please sign in to comment.