Skip to content

Commit

Permalink
[Asm] Separate Value class source file
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jul 1, 2024
1 parent 3a7d5ad commit 84e0494
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 108 deletions.
4 changes: 2 additions & 2 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ OBJS_com = \
file_reader.o file_printer.o list_formatter.o text_common.o
OBJS_asm = \
asm.o asm_commander.o asm_driver.o asm_directive.o asm_formatter.o \
asm_base.o config_base.o reg_base.o value_parser.o parsers.o operators.o \
function_store.o symbol_store.o \
asm_base.o config_base.o reg_base.o value.o value_parser.o parsers.o \
operators.o function_store.o symbol_store.o \
$(foreach a,$(ARCHS),$(if $(wildcard ../src/asm_$(a).cpp),asm_$(a).o))
OBJS_dis = \
dis.o dis_commander.o dis_driver.o dis_formatter.o dis_base.o \
Expand Down
2 changes: 1 addition & 1 deletion cli/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build asm: link $
file_printer.o file_reader.o bin_decoder.o bin_encoder.o intel_hex.o $
moto_srec.o asm_base.o config_base.o error_reporter.o formatters.o $
operators.o option_base.o parsers.o reg_base.o str_buffer.o str_scanner.o $
text_common.o value_formatter.o value_parser.o $
text_common.o value_formatter.o value.o value_parser.o $
asm_cdp1802.o reg_cdp1802.o table_cdp1802.o text_cdp1802.o $
asm_f3850.o reg_f3850.o table_f3850.o text_f3850.o $
asm_i8048.o reg_i8048.o table_i8048.o text_i8048.o $
Expand Down
1 change: 1 addition & 0 deletions src/ninja.rules
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build str_buffer.o: cxx ${root}/src/str_buffer.cpp
build str_scanner.o: cxx ${root}/src/str_scanner.cpp
build text_common.o: cxx ${root}/src/text_common.cpp
build value_formatter.o: cxx ${root}/src/value_formatter.cpp
build value.o: cxx ${root}/src/value.cpp
build value_parser.o: cxx ${root}/src/value_parser.cpp

# cdp1802
Expand Down
74 changes: 74 additions & 0 deletions src/value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2024 Tadashi G. Takaoka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "value.h"
#include <ctype.h>
#include <stdlib.h>

namespace libasm {

namespace {
bool isValidDigit(char c, Radix radix) {
if (radix == RADIX_16)
return isxdigit(c);
return c >= '0' && c < '0' + uint8_t(radix);
}

uint8_t toNumber(char c, Radix radix) {
if (radix == RADIX_16 && c >= 'A')
return (c & ~0x20) - 'A' + 10;
return c - '0';
}

template <typename VAL_T, VAL_T VAL_MAX>
Error strToNumber(StrScanner &scan, Radix radix, VAL_T &value) {
auto p = scan;
if (!isValidDigit(*p, radix))
return NOT_AN_EXPECTED;
const VAL_T limit = VAL_MAX / uint8_t(radix);
const uint8_t limit_digit = VAL_MAX % uint8_t(radix);
Error error = OK;
VAL_T v = 0;
while (isValidDigit(*p, radix)) {
const auto n = toNumber(*p, radix);
if (v > limit || (v == limit && n > limit_digit))
error = OVERFLOW_RANGE;
v *= uint8_t(radix);
v += n;
++p;
}
scan = p;
value = v;
return error;
}
} // namespace

Error Value::parseNumber(StrScanner &scan, Radix radix, uint32_t &value) {
return strToNumber<uint32_t, UINT32_MAX>(scan, radix, value);
}

Error Value::parseNumber(StrScanner &scan, Radix radix, uint64_t &value) {
return strToNumber<uint64_t, UINT64_MAX>(scan, radix, value);
}

} // namespace libasm

// Local Variables:
// mode: c++
// c-basic-offset: 4
// tab-width: 4
// End:
// vim: set ft=cpp et ts=4 sw=4:
44 changes: 0 additions & 44 deletions src/value_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,6 @@

namespace libasm {

namespace {
bool isValidDigit(char c, Radix radix) {
if (radix == RADIX_16)
return isxdigit(c);
return c >= '0' && c < '0' + uint8_t(radix);
}

uint8_t toNumber(char c, Radix radix) {
if (radix == RADIX_16 && c >= 'A')
return (c & ~0x20) - 'A' + 10;
return c - '0';
}

template <typename VAL_T, VAL_T VAL_MAX>
Error strToNumber(StrScanner &scan, Radix radix, VAL_T &value) {
auto p = scan;
if (!isValidDigit(*p, radix))
return NOT_AN_EXPECTED;
const VAL_T limit = VAL_MAX / uint8_t(radix);
const uint8_t limit_digit = VAL_MAX % uint8_t(radix);
Error error = OK;
VAL_T v = 0;
while (isValidDigit(*p, radix)) {
const auto n = toNumber(*p, radix);
if (v > limit || (v == limit && n > limit_digit))
error = OVERFLOW_RANGE;
v *= uint8_t(radix);
v += n;
++p;
}
scan = p;
value = v;
return error;
}
} // namespace

Error Value::parseNumber(StrScanner &scan, Radix radix, uint64_t &value) {
return strToNumber<uint64_t, UINT64_MAX>(scan, radix, value);
}

Error Value::parseNumber(StrScanner &scan, Radix radix, uint32_t &value) {
return strToNumber<uint32_t, UINT32_MAX>(scan, radix, value);
}

const NumberParser &ValueParser::Plugins::number() const {
return CStyleNumberParser::singleton();
}
Expand Down
6 changes: 3 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ CPPFLAGS = -I../src -I../driver -MD -MF [email protected]
OBJS_test = \
test_asserter.o error_reporter.o option_base.o str_buffer.o str_scanner.o
OBJS_asm = \
asm_base.o config_base.o reg_base.o test_asm_helper.o value_parser.o \
parsers.o operators.o text_common.o
asm_base.o config_base.o reg_base.o test_asm_helper.o value.o \
value_parser.o parsers.o operators.o text_common.o
OBJS_dis = \
dis_base.o config_base.o formatters.o reg_base.o test_dis_helper.o \
value_formatter.o text_common.o
OBJS_expr = \
config_base.o test_expr_helper.o formatters.o operators.o parsers.o \
value_formatter.o value_parser.o text_common.o
value_formatter.o value.o value_parser.o text_common.o
OBJS_gen = \
gen_driver.o test_generator.o tokenized_text.o dis_formatter.o \
config_base.o dis_base.o formatters.o reg_base.o value_formatter.o \
Expand Down
Loading

0 comments on commit 84e0494

Please sign in to comment.