-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Asm] Separate Value class source file
- Loading branch information
Showing
9 changed files
with
139 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 \ | ||
|
Oops, something went wrong.