Skip to content

Commit

Permalink
[ASM] Fix list-radix handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jan 6, 2025
1 parent f090c34 commit 1e2502d
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 49 deletions.
9 changes: 3 additions & 6 deletions cli/asm_commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/

#include "asm_commander.h"

#include <cstring>
#include "asm_directive.h"
#include "file_printer.h"
#include "file_sources.h"
#include "intel_hex.h"
#include "moto_srec.h"
#include "stored_printer.h"

#include <cstring>

namespace libasm {
namespace cli {

Expand Down Expand Up @@ -60,12 +58,12 @@ int AsmCommander::assemble() {
fprintf(stderr, "%s: Pass %d\n", _input_name, ++pass);
prev.swap(memory);
symbols.copy(_driver.symbols());
_driver.symbols().clearFunctions();
memory.clear();
listout.clear();
errorout.clear();
_driver.setUpperHex(_upper_hex);
_driver.setLineNumber(_line_number);
_driver.reset();
for (const auto &it : _options) {
_driver.setOption(it.first.c_str(), it.second.c_str());
}
Expand Down Expand Up @@ -174,8 +172,7 @@ usage: %s [-o <output>] [-l <list>] <input>
for (const auto *dir : _driver)
longOptions |= (dir->commonOptions().head() || dir->options().head());
if (longOptions) {
fprintf(stderr,
" --<name>=<vale> : extra options (<type> [, <CPU>])\n");
fprintf(stderr, " --<name>=<vale> : extra options (<type> [, <CPU>])\n");
const auto dir = *_driver.begin();
for (const auto *opt = dir->commonOptions().head(); opt; opt = opt->next()) {
fprintf(stderr, " --%-16s: %s (%s)\n", opt->name_P(), opt->description_P(),
Expand Down
10 changes: 2 additions & 8 deletions driver/asm_directive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/

#include "asm_directive.h"

#include <algorithm>
#include <cstring>
#include "asm_driver.h"
#include "asm_sources.h"
#include "intel_hex.h"
#include "moto_srec.h"
#include "str_scanner.h"

#include <algorithm>
#include <cstring>

namespace libasm {
namespace driver {

Expand Down Expand Up @@ -79,10 +77,6 @@ uint32_t AsmDirective::currentLocation() const {
return _assembler.currentLocation();
}

void AsmDirective::setListRadix(Radix listRadix) {
_assembler.setListRadix(listRadix);
}

Error AsmDirective::encode(StrScanner &scan, Insn &insn, Context &context) {
setOK();
_assembler.setCurrentLocation(insn.address());
Expand Down
1 change: 0 additions & 1 deletion driver/asm_directive.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ struct AsmDirective : ErrorAt {

void reset(CpuSwitcher &switcher);
void setOption(const char *name, const char *value);
void setListRadix(Radix listRadix);
Radix listRadix() const;
uint32_t currentLocation() const;

Expand Down
24 changes: 12 additions & 12 deletions driver/asm_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,21 @@
*/

#include "asm_driver.h"

#include <algorithm>
#include <cstring>
#include "asm_directive.h"
#include "asm_formatter.h"
#include "asm_sources.h"

#include <algorithm>
#include <cstring>

namespace libasm {
namespace driver {

AsmDriver::AsmDriver(std::initializer_list<AsmDirective *> directives)
: _directives(), _current(nullptr), _symbols(), _origin(0) {
for (auto dir : directives) {
dir->reset(*this);
_directives.push_back(dir);
}
: _directives(directives), _current(nullptr), _symbols(), _origin(0) {
_current = _directives.front();
setUpperHex(true);
setLineNumber(false);
reset();
}

namespace {
Expand Down Expand Up @@ -74,9 +69,7 @@ std::list<std::string> AsmDriver::listCpu() const {
bool AsmDriver::setCpu(const char *cpu) {
for (auto dir : _directives) {
if (dir->setCpu(cpu)) {
const auto *prev = _current;
_current = dir;
dir->setListRadix(prev->listRadix());
return true;
}
}
Expand All @@ -100,6 +93,13 @@ void AsmDriver::setLineNumber(bool lineNumber) {
_lineNumber = lineNumber;
}

void AsmDriver::reset() {
symbols().clearFunctions();
for (auto dir : _directives) {
dir->reset(*this);
}
}

void AsmDriver::setOption(const char *name, const char *value) {
for (auto dir : _directives) {
dir->setOption(name, value);
Expand Down Expand Up @@ -137,7 +137,7 @@ int AsmDriver::assemble(AsmSources &sources, BinMemory &memory, TextPrinter &lis
_origin = directive.currentLocation();

formatter.set(*line, directive, config, &context.value);
formatter.setListRadix(current()->listRadix());
formatter.setListRadix(directive.listRadix());
if (formatter.hasError())
++errors;
while (formatter.hasNextLine()) {
Expand Down
1 change: 1 addition & 0 deletions driver/asm_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct AsmDriver final : CpuSwitcher {

void setUpperHex(bool upperHex);
void setLineNumber(bool enable);
void reset();
void setOption(const char *name, const char *value);

int assemble(AsmSources &sources, BinMemory &memory, TextPrinter &listout,
Expand Down
19 changes: 9 additions & 10 deletions test/driver/test_asm_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/

#include "test_formatter_helper.h"

#include "asm_i8080.h"
#include "asm_ins8060.h"
#include "asm_mc6809.h"
#include "asm_mos6502.h"
#include "asm_z80.h"
#include "test_formatter_helper.h"

namespace libasm {
namespace driver {
Expand Down Expand Up @@ -176,7 +175,6 @@ label3: equ 1 * comment
mc6809:4:25: error: Undefined symbol: "comment"
1000 : =0 label3: equ 1 * comment
)");

}

void test_switch_cpu() {
Expand All @@ -190,6 +188,7 @@ void test_switch_cpu() {
ZilogDirective dirz80(asmz80);
AsmDriver driver{&dir6809, &dir6502, &dir8080, &dirz80};
TestSources sources;
Options options;

ASM("switch cpu",
R"( cpu mc6809
Expand Down Expand Up @@ -245,10 +244,10 @@ void test_list_radix() {
ZilogDirective dirz80(asmz80);
AsmDriver driver{&dir6502, &dirz80};
TestSources sources;
Options options;

ASM("list-radix",
R"( option "list-radix", 8
cpu z80
ASM("list-radix", R"( cpu z80
option "list-radix", 8
org 1234Q
ld hl, 1234H
option "list-radix", 16
Expand All @@ -263,8 +262,8 @@ void test_list_radix() {
cpu z80
res 0, (iy-128)
)",
R"( 0 : option "list-radix", 8
0 : cpu z80
R"( 0 : cpu z80
0 : option "list-radix", 8
1234 : org 1234Q
1234 : 041 064 022 ld hl, 1234H
29F : option "list-radix", 16
Expand All @@ -277,7 +276,7 @@ void test_list_radix() {
3456 : *=@3456
3456 : 242 022 ldx #$12
3460 : cpu z80
3460 : 375 313 200 206 res 0, (iy-128)
730 : FD CB 80 86 res 0, (iy-128)
)");
}

Expand Down Expand Up @@ -371,7 +370,7 @@ void test_include() {

driver.setUpperHex(true);
driver.setLineNumber(true);
driver.setOption("smart-branch", "on");
options.set("smart-branch", "on");

ASM("mc6809",
R"( cpu mc6809
Expand Down
7 changes: 3 additions & 4 deletions test/driver/test_formatter_cdp1802.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@ symbol = x'1234'
abd7 : c0 ab 23 lbr x'ab23'
)");

driver.setOption("use-register", "on");
driver.setOption("smart-branch", "on");
options.set("use-register", "on");
options.set("smart-branch", "on");

ASM("cdp1804",
R"( org x'abcd'
scal r3, x'8485'
lbr x'ab23'
br x'ac23'
)",
br x'ac23')",
R"( abcd : org x'abcd'
abcd : 68 83 84 85 scal r3, x'8485'
abd1 : 30 23 lbr x'ab23'
Expand Down
28 changes: 25 additions & 3 deletions test/driver/test_formatter_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef __LIBASM_TEST_FORMATTER_HELPER_H__
#define __LIBASM_TEST_FORMATTER_HELPER_H__

#include <map>
#include <string>
#include "asm_directive.h"
#include "asm_driver.h"
#include "dis_driver.h"
Expand All @@ -26,11 +28,29 @@
#include "test_reader.h"
#include "test_sources.h"

namespace libasm {
namespace driver {
namespace test {
struct Options {
void set(const char *name, const char *value) { _opts.emplace(name, value); }
void set(AsmDriver &driver) const {
for (auto &it : _opts)
driver.setOption(it.first.c_str(), it.second.c_str());
};

private:
std::map<std::string, std::string> _opts;
};
} // namespace test
} // namespace driver
} // namespace libasm

#define PREP_ASM(type_of_assembler, type_of_directive) \
type_of_assembler assembler; \
type_of_directive directive{assembler}; \
AsmDriver driver{&directive}; \
TestSources sources
TestSources sources; \
Options options

#define ASM(_cpu, _source, _expected) \
do { \
Expand All @@ -43,12 +63,14 @@
StoredPrinter list, error; \
bool reportError = false; \
do { \
sources.open(source.name().c_str()); \
prev.swap(memory); \
memory.clear(); \
symbols.copy(driver.symbols()); \
memory.clear(); \
list.clear(); \
error.clear(); \
sources.open(source.name().c_str()); \
driver.reset(); \
options.set(driver); \
driver.setCpu(_cpu); \
driver.assemble(sources, memory, list, error, reportError); \
reportError = true; \
Expand Down
2 changes: 1 addition & 1 deletion test/driver/test_formatter_i8051.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void test_asm_i8051() {
PREP_ASM(i8051::AsmI8051, IntelDirective);

driver.setUpperHex(false);
driver.setOption("smart-branch", "on");
options.set("smart-branch", "on");

ASM("i8051",
R"( cpu i8051
Expand Down
2 changes: 1 addition & 1 deletion test/driver/test_formatter_i8096.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void test_asm_i8096() {
PREP_ASM(i8096::AsmI8096, IntelDirective);

driver.setUpperHex(false);
driver.setOption("smart-branch", "on");
options.set("smart-branch", "on");

ASM("i8096",
R"( cpu i8096
Expand Down
2 changes: 1 addition & 1 deletion test/driver/test_formatter_mc6809.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void test_asm_mc6809() {

driver.setUpperHex(true);
driver.setLineNumber(true);
driver.setOption("smart-branch", "on");
options.set("smart-branch", "on");

ASM("mc6809",
R"( cpu mc6809
Expand Down
2 changes: 1 addition & 1 deletion test/driver/test_formatter_mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void test_asm_w65816() {
PREP_ASM(mos6502::AsmMos6502, MostekDirective);

driver.setUpperHex(false);
driver.setOption("smart-branch", "on");
options.set("smart-branch", "on");

ASM("w65c816",
R"( cpu w65c816
Expand Down
2 changes: 1 addition & 1 deletion test/driver/test_formatter_tlcs90.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void test_asm_tlcs90() {
PREP_ASM(tlcs90::AsmTlcs90, ZilogDirective);

driver.setUpperHex(false);
driver.setOption("smart-branch", "on");
options.set("smart-branch", "on");

ASM("tlcs90",
R"( cpu tlcs90
Expand Down

0 comments on commit 1e2502d

Please sign in to comment.