Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1af92eb156 | ||
|
|
3f3e431d64 | ||
|
|
f68d68d497 | ||
|
|
4927c7779d |
No files matched your search
@@ -2,3 +2,6 @@ build/
|
||||
.cache/
|
||||
.continue/
|
||||
.vscode/
|
||||
|
||||
# 汇编产物
|
||||
*.rot8
|
||||
+12
-1
@@ -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)
|
||||
@@ -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
@@ -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
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user