README added
This commit is contained in:
1 parent
7c57f4e628
commit
bef8310558
6 files changed
+111
-17
No files matched your search
@@ -0,0 +1,58 @@
|
|||||||
|
# R8ASM
|
||||||
|
|
||||||
|
基于[ROT-8](https://gitea.starcells.ltd/starcells/ROT-8.git)的汇编语言\
|
||||||
|
和用于汇编汇编的汇编器类
|
||||||
|
|
||||||
|
## 作为语言的R8ASM
|
||||||
|
|
||||||
|
### 简介
|
||||||
|
|
||||||
|
rot8指令集是图灵完备的(假如tape无限长),因此就像大多数计算工具一样,rot8纸带"能表示任何计算任务" \
|
||||||
|
但这显然不是个令人满意的答案,理论上的成立从来不意味着工程上的可行,而实践表明用rot8本身编写处理复杂任务的程序在工程上确实几乎是不可行的 \
|
||||||
|
因此rot8也需要自己的汇编--r8asm,r8asm汇编的存在使得全加器纸带这类规模动辄成千上万条指令的复杂计算得以实现,且具有不错的复用能力,同时提供了抽象层以供[别的怪东西](https://gitea.starcells.ltd/starcells/R8VeritasCC)发挥威力 \
|
||||||
|
r8asm的语法很大程度上参考了nasm,因为这样在vscode里把.r8asm文件的语言模式绑定到nasm就可以享受语法高亮了,仅此而已
|
||||||
|
|
||||||
|
### 语法规范
|
||||||
|
|
||||||
|
见[SPEC](SPEC.md)
|
||||||
|
|
||||||
|
## 作为汇编器的R8ASM
|
||||||
|
|
||||||
|
### 简介
|
||||||
|
|
||||||
|
一个简单的汇编器实现,以class的形式封装 \
|
||||||
|
因为是重构,着急拿来干活,所以一些细节实现得比较糟糕 \
|
||||||
|
当然SPEC里提到的功能肯定是已实现的
|
||||||
|
|
||||||
|
### 标准宏
|
||||||
|
|
||||||
|
在[asm](asm)里提供了一些标准宏实现可供取用.标准宏命名规范,定义和功能同样见[SPEC](SPEC.md)
|
||||||
|
|
||||||
|
### 示例
|
||||||
|
|
||||||
|
``` CPP
|
||||||
|
// 若以库的形式调用r8asm
|
||||||
|
#include "r8asm/r8asm.hh"
|
||||||
|
#include <iostream>
|
||||||
|
int main() {
|
||||||
|
r8asm a;
|
||||||
|
size_t cell_size = 8;
|
||||||
|
a.import_file("asm/test.r8asm", cell_size);
|
||||||
|
std::cout << a.export_rot8();
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
``` SH
|
||||||
|
# 若执行r8asm_test以汇编asm/test.r8asm
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake ..
|
||||||
|
make -j$(nproc)
|
||||||
|
r8asm_test > test.rot8
|
||||||
|
```
|
||||||
|
|
||||||
|
### TODO
|
||||||
|
|
||||||
|
1. 各种Warning/Error提示
|
||||||
|
2. r8asm类的分割(架构调整)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# R8ASM Specification
|
||||||
|
|
||||||
|
## 作用域规则
|
||||||
|
|
||||||
|
当调用宏时,宏体内会被视为新的作用域 \
|
||||||
|
在子作用域内,父作用域的符号是可读的,
|
||||||
|
|
||||||
|
### TODO
|
||||||
|
|
||||||
|
## 作为变量的编译期常量
|
||||||
|
|
||||||
|
由于rot8本身的限制,r8asm在运行时是几乎没有自由度可言的.内存中的数据难以参与控制流,r8asm自然也就无法实现灵活的各类取值运算 \
|
||||||
|
因此,r8asm采用
|
||||||
|
|
||||||
|
### TODO
|
||||||
|
|
||||||
|
## 运算符优先级
|
||||||
|
|
||||||
|
r8asm的运算符优先级与nasm一致,具体来讲:
|
||||||
|
|
||||||
|
|运算符|含义|优先级|
|
||||||
|
|---|---|---|
|
||||||
|
|~|按位取反|0|
|
||||||
|
|*|乘|1|
|
||||||
|
|/|除|1|
|
||||||
|
|+|加|2|
|
||||||
|
|-|减|2|
|
||||||
|
|&|按位与|3|
|
||||||
|
|^|按位异或|4|
|
||||||
|
|\||按位或|5|
|
||||||
|
|
||||||
|
## 标准宏
|
||||||
+1
-1
@@ -46,7 +46,7 @@ flb
|
|||||||
; Macro rol
|
; Macro rol
|
||||||
; %this: Cell operand
|
; %this: Cell operand
|
||||||
%macro rol 0
|
%macro rol 0
|
||||||
%rep _ROL_STEP
|
%rep _CELL_SIZE - 1
|
||||||
ror
|
ror
|
||||||
%endrep
|
%endrep
|
||||||
%endmacro
|
%endmacro
|
||||||
|
|||||||
+6
-11
@@ -9,12 +9,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
constexpr size_t CELL_SIZE = 8;
|
|
||||||
constexpr size_t CELL_MAX(((uint64_t)1 << CELL_SIZE) - 1);
|
|
||||||
constexpr size_t ROL_STEP(CELL_SIZE - 1);
|
|
||||||
|
|
||||||
namespace r8asm_maps {}
|
|
||||||
|
|
||||||
class r8asm {
|
class r8asm {
|
||||||
using data = int16_t;
|
using data = int16_t;
|
||||||
using parameter_index = uint8_t;
|
using parameter_index = uint8_t;
|
||||||
@@ -89,13 +83,13 @@ private:
|
|||||||
class ast {
|
class ast {
|
||||||
public:
|
public:
|
||||||
ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}};
|
ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}};
|
||||||
|
size_t CELL_SIZE;
|
||||||
|
// I know template class is the most elegant solution of CELL_SIZE
|
||||||
|
// but I can't be bother to add that.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
addr dataptr = 0;
|
addr dataptr = 0;
|
||||||
std::vector<std::map<std::string, general_data>> scopes = {
|
std::vector<std::map<std::string, general_data>> scopes;
|
||||||
{{"_CELL_SIZE", CELL_SIZE},
|
|
||||||
{"_CELL_MAX", CELL_MAX},
|
|
||||||
{"_ROL_STEP", ROL_STEP}}};
|
|
||||||
std::map<std::string, ast_node> macros;
|
std::map<std::string, ast_node> macros;
|
||||||
|
|
||||||
enum class builtin_t {
|
enum class builtin_t {
|
||||||
@@ -159,6 +153,7 @@ private:
|
|||||||
ast_node expand_node_to_ins(const ast_node &node);
|
ast_node expand_node_to_ins(const ast_node &node);
|
||||||
std::string expand_root();
|
std::string expand_root();
|
||||||
bool import_macro(const std::string ¯o_name, const ast_node &node);
|
bool import_macro(const std::string ¯o_name, const ast_node &node);
|
||||||
|
void set_cell_size(size_t cell_size);
|
||||||
};
|
};
|
||||||
|
|
||||||
ast main_tree;
|
ast main_tree;
|
||||||
@@ -183,5 +178,5 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
std::string export_rot8();
|
std::string export_rot8();
|
||||||
bool import_file(const std::string &filename);
|
bool import_file(const std::string &filename, size_t cell_size);
|
||||||
};
|
};
|
||||||
+13
-4
@@ -1,5 +1,6 @@
|
|||||||
#include "r8asm/r8asm.hh"
|
#include "r8asm/r8asm.hh"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <list>
|
#include <list>
|
||||||
@@ -60,6 +61,13 @@ std::string peel(std::string &str) { // Peel the first word from string.
|
|||||||
#undef EMPTY_CHARS
|
#undef EMPTY_CHARS
|
||||||
#undef OPERATORS
|
#undef OPERATORS
|
||||||
|
|
||||||
|
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}}};
|
||||||
|
}
|
||||||
|
|
||||||
r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) {
|
r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) {
|
||||||
general_data to_return = 0;
|
general_data to_return = 0;
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
@@ -248,7 +256,7 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
|
|||||||
ror
|
ror
|
||||||
*/
|
*/
|
||||||
data d = expand_node_to_data(node.children[0]);
|
data d = expand_node_to_data(node.children[0]);
|
||||||
for (auto i = 0; i < CELL_SIZE; i++) {
|
for (auto i = 0; i < scopes[0].at("_CELL_SIZE"); i++) {
|
||||||
if (d & 1)
|
if (d & 1)
|
||||||
to_return.children.push_back(ROT8_NODE("flb"));
|
to_return.children.push_back(ROT8_NODE("flb"));
|
||||||
to_return.children.push_back(ROT8_NODE("ror"));
|
to_return.children.push_back(ROT8_NODE("ror"));
|
||||||
@@ -404,8 +412,8 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
|||||||
filename += tp->value;
|
filename += tp->value;
|
||||||
// now tp points at RQUOTE
|
// now tp points at RQUOTE
|
||||||
|
|
||||||
import_file(
|
import_file(filename, main_tree.CELL_SIZE); // TODO: check whether the
|
||||||
filename); // TODO: check whether the file is open successfully
|
// file is open successfully
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -692,9 +700,10 @@ r8asm::token_stream::lex(const std::string &char_stream) {
|
|||||||
}
|
}
|
||||||
#undef OPERATORS
|
#undef OPERATORS
|
||||||
|
|
||||||
bool r8asm::import_file(const std::string &filename) {
|
bool r8asm::import_file(const std::string &filename, size_t cell_size) {
|
||||||
for (const auto &token_it : parse(token_stream(filename)).children) {
|
for (const auto &token_it : parse(token_stream(filename)).children) {
|
||||||
main_tree.root.children.push_back(token_it);
|
main_tree.root.children.push_back(token_it);
|
||||||
}
|
}
|
||||||
|
main_tree.set_cell_size(cell_size);
|
||||||
return true; // TODO: ERROR CHECK
|
return true; // TODO: ERROR CHECK
|
||||||
}
|
}
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
int main() {
|
int main() {
|
||||||
r8asm a;
|
r8asm a;
|
||||||
a.import_file("asm/test.r8asm");
|
a.import_file("asm/test.r8asm", 8);
|
||||||
std::cout << a.export_rot8();
|
std::cout << a.export_rot8();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user