Compare commits

...
4 Commits
Author SHA1 Message Date
starcells 1af92eb156 r8asm: make .loop consume the bit it just cleared
.endloop expanded to >-+]< : flb clears the LSB and rnz then tests that
very same bit, so it always sees 0 and the body ran exactly once no
matter how many 1s the counter cell held.  Measured: a counter of 0b111
came out as 0b110 after a single pass.

Putting a ror after the flb pulls the next bit down before the test,
which is what SPEC describes -- consecutive 1s from the LSB, one per
iteration.  Measured:
    0b0000_0111 -> 3 passes, counter 0
    0b1111_1111 -> 8 passes, counter 0 (a bare ror would spin forever)
Nothing under asm/ uses .loop, so no existing tape changes.
2026-11-05 22:49:54 +08:00
starcells 3f3e431d64 r8asm: say what is wrong instead of dying or lying
A typo used to end in one of three ways: std::out_of_range aborting the
process (unknown macro, unknown directive), a silent 0 (unknown name used
as an address, which quietly mis-assembles), or a silent empty tape (a
file or %include that could not be opened).  All four now go through
ast::fail(), print a line on stderr, set the error flag and make the CLI
exit with 1.  Names that are reported repeatedly are only printed once.

While here: import_file set the cell size *after* parsing, so the
%include handler read main_tree.CELL_SIZE before it had ever been set and
handed 0 to included files.  It worked by accident because the top-level
call ran again at the end.  Moved it before the parse.

r8asm_test.cc came from the working tree (argv instead of a hard-coded
path); it now also checks the arguments and the error flag.
2026-11-05 22:49:54 +08:00
starcells f68d68d497 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.
2026-11-05 22:49:54 +08:00
starcells 4927c7779d asm: add a fibonacci demo that does not hard-code 8 bits
The counter is _CELL_MAX ones and one bit is eaten per round, so the loop runs _CELL_SIZE times on any width, and the address layout is built from a named UNIT instead of scattered 2s. Verified against ROT-8: 1 1 2 3 5 8 13 21 34 55.
2026-11-05 22:43:07 +08:00
6 changed files with 181 additions and 19 deletions

No files matched your search

+3
View File
@@ -2,3 +2,6 @@ build/
.cache/
.continue/
.vscode/
# 汇编产物
*.rot8
+12 -1
View File
@@ -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($<$<CONFIG:Release>:-march=native>)
endif()
add_executable(r8asm_test tests/r8_test.cc)
target_link_libraries(r8asm_test r8asm_core)
+88
View File
@@ -0,0 +1,88 @@
; fibonacci.r8asm
;
; 斐波那契发生器,顺便当 r8asm 的一份冒烟测试:
; 宏、%include、表达式、.var/.anchor/.tp、.xor、biz/rnz、raw_add 都用上了。
;
; 这里不写死 8 位:计数器用 _CELL_MAX,循环长度自然等于 _CELL_SIZE。
; 8 位时的预期输出(用 rot8_test 跑出来的十进制数):
; 1 1 2 3 5 8 13 21 34 55
;
; 数据布局:每个变量占一个 Unit(数据 Cell + 辅助 Cell),
; 辅助 Cell 留给 copy 当位缓冲,raw_add 也会用到相邻的草稿 Cell。
;
; A a
; B b
; S a + b
; X a 的副本 —— raw_add 会把两个操作数都吃掉,所以每圈都得重新复印
; Y b 的副本
; Z 常数 0,当进位输入用
; CO 进位
; T 进位用的一位草稿
; CNT 循环计数器
; BUF copy 用的位缓冲
%include "asm/raw_add.r8asm"
; 一个 Unit 两个 Cell:数据 Cell + 辅助 Cell
.var UNIT 2
.var A 0
.var B (A + UNIT)
.var S (B + UNIT)
.var X (S + UNIT)
.var Y (X + UNIT)
.var Z (Y + UNIT)
.var CO (Z + UNIT)
.var T (CO + UNIT)
.var CNT (T + UNIT)
.var BUF (CNT + UNIT)
; ---------------------------------------------------------------- 前两项
; a = 1,b = 1,先打出来
.tp A
.xor 1
.tp B
.xor 1
output A
output B
; ---------------------------------------------------------------- 循环入口
; CNT 里塞满 _CELL_SIZE 个 1:一个 1 就是一圈。
; biz/rnz 在 rot-8 里就是一对 [ ],运行时按括号配对,
; 所以这里不用写标签,位置本身就是标签。
.tp CNT
.xor _CELL_MAX
biz
; ---------------------------------------------------------------- 循环体
; s = a + b
; raw_add 是破坏性的,先把它要吃的 a、b 各复印一份
.tp A
copy X BUF
.tp B
copy Y BUF
; 进位输入和进位输出都先清零
erase Z
erase CO
; S = X + Y + Z,进位从 CO 出来
raw_add X Y Z S CO T
; 打印新的一项
.tp S
output S
; 滚动:a <- b,b <- s
.tp Y
copy A BUF
.tp S
copy B BUF
; ------------------------------------------------------------ 计数
; 吃掉最低位的 1,再把下一位转下来,
; 等 _CELL_SIZE 个 1 都吃干净,最低位变成 0,循环结束
.tp CNT
flb
ror
rnz
+20 -1
View File
@@ -3,6 +3,7 @@
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
@@ -54,12 +55,18 @@ private:
token_stream(const std::string &filename) {
this->filename = filename;
std::ifstream file(filename); // TODO: when open failed
std::ifstream file(filename);
if (!file.is_open()) {
ok = false;
return;
}
std::stringstream buf;
buf << file.rdbuf();
lines = lex(buf.str());
}
std::vector<token_line> lex(const std::string &char_stream);
bool ok = true; // false when the file could not be read
};
enum class ast_node_t {
@@ -87,10 +94,20 @@ private:
// I know template class is the most elegant solution of CELL_SIZE
// but I can't be bother to add that.
// The assembler has no exceptions: anything that cannot be assembled
// sets this flag and says why on stderr.
bool error = false;
void fail(const std::string &message) {
std::cerr << "r8asm: " << message << std::endl;
error = true;
}
private:
addr dataptr = 0;
std::vector<std::map<std::string, general_data>> scopes;
std::map<std::string, ast_node> macros;
std::set<std::string> reported; // keep a typo from being printed n times
enum class builtin_t {
ANCHOR,
@@ -179,4 +196,6 @@ private:
public:
std::string export_rot8();
bool import_file(const std::string &filename, size_t cell_size);
// True once anything (a missing file, an unknown name, ...) has failed.
bool failed() const { return main_tree.error; }
};
+45 -13
View File
@@ -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<general_data>(cell_size)},
{"_CELL_MAX", static_cast<general_data>((1 << cell_size) - 1)},
{"_ROL_STEP", static_cast<general_data>(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: {
@@ -270,8 +281,13 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
// >[<
} break;
case builtin_t::ENDLOOP: {
// Consume one '1' from the auxiliary cell and pull the next bit down.
// Without the ror, the rnz could only ever see the bit that flb just
// cleared, so a .loop over 0b111 ran once instead of three times
// (SPEC: "consecutive 1s starting from the LSB").
to_return.children.push_back(ROT8_NODE("stp"));
to_return.children.push_back(ROT8_NODE("flb"));
to_return.children.push_back(ROT8_NODE("ror"));
to_return.children.push_back(ROT8_NODE("rnz"));
to_return.children.push_back(ROT8_NODE("btp"));
// >-+]<
@@ -348,6 +364,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 +433,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 +723,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);
}
+13 -4
View File
@@ -1,9 +1,18 @@
#include "r8asm/r8asm.hh"
#include <iostream>
int main() {
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "usage: r8asm_test <file.r8asm>" << std::endl;
return (2);
}
r8asm a;
a.import_file("asm/test.r8asm", 8);
std::cout << a.export_rot8();
if (!a.import_file(argv[1], 8))
return (1);
std::string tape = a.export_rot8();
if (a.failed())
return (1);
std::cout << tape;
return (0);
}
}