From f68d68d4970c74a16c26109ed8fb6af10a6b3ca0 Mon Sep 17 00:00:00 2001 From: StarCells Date: Thu, 5 Nov 2026 22:43:07 +0800 Subject: [PATCH] build: stop throwing the compiler's default flags away set(CMAKE_CXX_FLAGS ...) replaced the whole flag set. On MSVC that dropped /EHsc, which this code needs (std::map::at, std::stoi throw), and it applied -O3 to every configuration -- a Debug build then passes /RTC1 and MSVC refuses to combine that with an optimisation level (D8016). CMake's Release configuration already passes -O3 / /O2 and defines NDEBUG, so the block is gone. The only flag worth keeping is -march=native, and only for a Release build of a non-MSVC compiler. Also cast the cell-size constants so the size_t -> int32_t narrowing warnings go away. --- CMakeLists.txt | 13 ++++++++++++- src/r8asm.cc | 53 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 42da1e3..5b98775 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,18 @@ include_directories(include) add_library(r8asm_core src/r8asm.cc) -set(CMAKE_CXX_FLAGS "-O3 -march=native -DNDEBUG") +# The old line here was +# set(CMAKE_CXX_FLAGS "-O3 -march=native -DNDEBUG") +# which replaced CMake's whole flag set: on MSVC that drops /EHsc (this +# code uses std::map::at, std::stoi and friends) and it also pushed -O3 into +# Debug builds, where MSVC's /RTC1 and /O2 refuse to coexist (D8016). +# +# CMake's own Release configuration already passes -O3 / /O2 and defines +# NDEBUG, so the only thing worth keeping is -march=native, and only for a +# release build of a non-MSVC compiler. +if(NOT MSVC) + add_compile_options($<$:-march=native>) +endif() add_executable(r8asm_test tests/r8_test.cc) target_link_libraries(r8asm_test r8asm_core) diff --git a/src/r8asm.cc b/src/r8asm.cc index 32e1798..e90efb7 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -63,9 +63,9 @@ std::string peel(std::string &str) { // Peel the first word from string. void r8asm::ast::set_cell_size(size_t cell_size) { CELL_SIZE = cell_size; - scopes = {{{"_CELL_SIZE", cell_size}, - {"_CELL_MAX", (1 << cell_size) - 1}, - {"_ROL_STEP", cell_size - 1}}}; + scopes = {{{"_CELL_SIZE", static_cast(cell_size)}, + {"_CELL_MAX", static_cast((1 << cell_size) - 1)}, + {"_ROL_STEP", static_cast(cell_size - 1)}}}; } r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { @@ -77,16 +77,19 @@ r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) { case ast_node_t::PARAMETER: to_return = scopes.back().at('%' + node.value); break; - case ast_node_t::VARIABLE: + case ast_node_t::VARIABLE: { + bool found = false; for (auto i = scopes.rbegin(); i != scopes.rend(); i++) { auto tmp = i->find(node.value); if (tmp != i->end()) { to_return = tmp->second; + found = true; break; } - // TODO: VAR NOT FOUND } - break; + if (!found && reported.insert(node.value).second) + fail("unknown name '" + node.value + "'"); + } break; case ast_node_t::OPERATOR: { switch (operator_map.at(node.value)) { case operator_t::ADD: @@ -191,8 +194,11 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { // %1 in macro means children[0] expand_node_to_data(node.children[i])); scopes.push_back(tmp_scope); - for (const auto &it : expand_node_to_ins(macros.at(node.value)).children) - to_return.children.push_back(it); + if (macros.find(node.value) == macros.end()) + fail("unknown macro '" + node.value + "'"); + else + for (const auto &it : expand_node_to_ins(macros.at(node.value)).children) + to_return.children.push_back(it); scopes.pop_back(); } break; @@ -213,6 +219,11 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) { value: type children: args */ + if (builtin_map.find(node.value) == builtin_map.end()) { + fail("unknown builtin '." + + node.value.substr(std::string("builtin_").size()) + "'"); + return to_return; + } switch (builtin_map.at(node.value)) { // WARNING: node.value HAS BEEN USED!!! DO NOT CONSUME IT AGAIN!!! case builtin_t::ANCHOR: { @@ -348,6 +359,11 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { include filename 0 1 */ + if (directive_keywords.find(it->tokens[0].value) == + directive_keywords.end()) { + main_tree.fail("unknown directive '%" + it->tokens[0].value + "'"); + break; + } switch (directive_keywords.at(it->tokens[0].value)) { // match an entire operation (not a cut for arguments) first case directive_t::MACRO: { @@ -412,8 +428,9 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { filename += tp->value; // now tp points at RQUOTE - import_file(filename, main_tree.CELL_SIZE); // TODO: check whether the - // file is open successfully + import_file(filename, main_tree.CELL_SIZE); + // import_file has already said why if it failed; the error flag is + // set, so this file will not be reported as assembled. } break; default: break; @@ -701,9 +718,19 @@ r8asm::token_stream::lex(const std::string &char_stream) { #undef OPERATORS bool r8asm::import_file(const std::string &filename, size_t cell_size) { - for (const auto &token_it : parse(token_stream(filename)).children) { + token_stream stream(filename); + if (!stream.ok) { + main_tree.fail("cannot open '" + filename + "'"); + return (false); + } + + // Before parsing: %include asks main_tree.CELL_SIZE for the width to hand + // to the included file, and that used to answer 0 because the size was + // only set at the end of this function. + main_tree.set_cell_size(cell_size); + + for (const auto &token_it : parse(stream).children) { main_tree.root.children.push_back(token_it); } - main_tree.set_cell_size(cell_size); - return true; // TODO: ERROR CHECK + return (!main_tree.error); } \ No newline at end of file