TODO:Macro expand
This commit is contained in:
1 parent
d4bbf1b0a0
commit
6189b15370
11 files changed
+215
-122
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
||||
BasedOnStyle: Google
|
||||
BasedOnStyle: Webkit
|
||||
IndentWidth: 4
|
||||
UseTab: Always
|
||||
PointerAlignment: Right
|
||||
+2
-3
@@ -5,9 +5,8 @@ set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
file(GLOB_RECURSE SOURCES "src/*.cc")
|
||||
|
||||
add_library(r8asm_core STATIC ${SOURCES})
|
||||
add_library(r8asm_core src/r8asm_core.cc src/r8asm_asm.cc
|
||||
src/r8asm_macro.cc)
|
||||
|
||||
add_executable(r8asm_test tests/r8_test.cc)
|
||||
target_link_libraries(r8asm_test r8asm_core)
|
||||
+2
-44
@@ -1,49 +1,7 @@
|
||||
#ifndef R8ASM
|
||||
#define R8ASM
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
typedef uint32_t r8asm_label;
|
||||
typedef uint32_t r8asm_data;
|
||||
typedef uint32_t unit_size_type;
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<uint8_t>(bc)])
|
||||
|
||||
enum class rot8_bytecode { STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP };
|
||||
enum class r8asm_macro {
|
||||
LOOP,
|
||||
ENDLOOP,
|
||||
TP,
|
||||
XOR,
|
||||
};
|
||||
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
|
||||
using R8MetaOp = std::variant<r8asm_macro, rot8_bytecode>;
|
||||
|
||||
struct R8Instruction {
|
||||
R8MetaOp op;
|
||||
R8Operand arg = std::monostate{};
|
||||
};
|
||||
|
||||
class R8Macro {
|
||||
std::vector<R8Operand> args = {std::monostate{}};
|
||||
std::vector<R8Instruction> ins;
|
||||
};
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops);
|
||||
|
||||
extern std::map<std::string, r8asm_label> labels;
|
||||
extern std::map<std::string, r8asm_data> datas;
|
||||
extern std::map<std::string, std::vector<R8Macro>> macros;
|
||||
|
||||
extern unit_size_type UNIT_SIZE;
|
||||
// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8)
|
||||
#define DEFAULT_UNIT_SIZE 17 // Just for test.
|
||||
#define UNIT_MAX (((uint64_t)1 << UNIT_SIZE) - 1)
|
||||
#include "r8asm/r8asm_asm.hh" // IWYU pragma: export
|
||||
#include "r8asm_macro.hh" // IWYU pragma: export
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef R8ASM_ASM
|
||||
#define R8ASM_ASM
|
||||
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
#include <span>
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
#ifndef R8ASM_CORE
|
||||
#define R8ASM_CORE
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
typedef uint32_t r8asm_label;
|
||||
typedef uint32_t r8asm_data;
|
||||
typedef uint32_t unit_size_type;
|
||||
typedef int32_t unit_offset_type;
|
||||
typedef uint32_t mem_size_type;
|
||||
typedef uint8_t argc_type;
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<uint8_t>(bc)])
|
||||
|
||||
enum class rot8_bytecode { STP,
|
||||
BTP,
|
||||
ROR,
|
||||
FLB,
|
||||
BIZ,
|
||||
RNZ,
|
||||
OUT,
|
||||
INP,
|
||||
};
|
||||
enum class r8asm_macro {
|
||||
LOOP,
|
||||
ENDLOOP,
|
||||
TP,
|
||||
XOR,
|
||||
ANCHOR,
|
||||
MACRO,
|
||||
ENDMACRO,
|
||||
};
|
||||
|
||||
using R8Operand = std::variant<std::monostate, r8asm_data, std::string>;
|
||||
using R8MetaOp = std::variant<r8asm_macro, rot8_bytecode>;
|
||||
|
||||
struct R8Instruction {
|
||||
R8MetaOp op;
|
||||
R8Operand arg = std::monostate {};
|
||||
};
|
||||
|
||||
class R8Macro {
|
||||
argc_type argc;
|
||||
std::vector<R8Instruction> ins;
|
||||
};
|
||||
|
||||
extern std::map<std::string, r8asm_label> labels;
|
||||
extern std::map<std::string, r8asm_data> datas;
|
||||
extern std::map<std::string, R8Macro> macros;
|
||||
extern mem_size_type dataptr;
|
||||
|
||||
extern unit_size_type UNIT_SIZE;
|
||||
// #define DEFAULT_UNIT_SIZE (sizeof(unit_size_type) * 8)
|
||||
#define DEFAULT_UNIT_SIZE 17 // Just for test.
|
||||
#define UNIT_MAX (((uint64_t)1 << UNIT_SIZE) - 1)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef R8ASM_MACRO
|
||||
#define R8ASM_MACRO
|
||||
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
|
||||
std::vector<R8Instruction> expand_macro(std::string macro_name, std::vector<R8Operand> argv);
|
||||
|
||||
#endif
|
||||
@@ -1,69 +0,0 @@
|
||||
#include "r8asm/r8asm.hh"
|
||||
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE;
|
||||
|
||||
std::map<std::string, r8asm_label> labels = {{std::string("main"), 0}};
|
||||
std::map<std::string, r8asm_data> datas = {
|
||||
{std::string("_UNIT_MAX"), UNIT_MAX}};
|
||||
|
||||
r8asm_data read_opr(R8Operand arg, r8asm_data default_opr = 1) {
|
||||
if (const std::string *operand = std::get_if<std::string>(&arg))
|
||||
return (datas[*operand]);
|
||||
else if (const r8asm_data *operand = std::get_if<r8asm_data>(&arg))
|
||||
return (*operand);
|
||||
return (default_opr);
|
||||
}
|
||||
|
||||
void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
for (unit_size_type i = 0; i < UNIT_SIZE; i++) {
|
||||
if ((opr & 1) == 1) tape.push_back(rot8_bytecode::FLB);
|
||||
tape.push_back(rot8_bytecode::ROR);
|
||||
opr >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
r8asm_data opr = read_opr(arg, UNIT_MAX);
|
||||
|
||||
tape.push_back(rot8_bytecode::STP);
|
||||
expand_xor(tape, opr);
|
||||
tape.push_back(rot8_bytecode::BIZ);
|
||||
tape.push_back(rot8_bytecode::BTP); // >(xor)[<
|
||||
}
|
||||
|
||||
void expand_endloop(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
std::vector<rot8_bytecode> tmp = {rot8_bytecode::STP, rot8_bytecode::FLB,
|
||||
rot8_bytecode::ROR, rot8_bytecode::RNZ,
|
||||
rot8_bytecode::BTP}; // >-+]<
|
||||
tape.insert(tape.end(), tmp.begin(), tmp.end());
|
||||
}
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops) {
|
||||
std::vector<rot8_bytecode> tape;
|
||||
for (const auto it : ops) {
|
||||
if (const rot8_bytecode *bc = std::get_if<rot8_bytecode>(&it.op)) {
|
||||
r8asm_data repeat = read_opr(it.arg);
|
||||
for (r8asm_data i = 0; i < repeat; i++) tape.push_back(*bc);
|
||||
} else if (const r8asm_macro *macro = std::get_if<r8asm_macro>(&it.op))
|
||||
switch (*macro) {
|
||||
case r8asm_macro::LOOP:
|
||||
expand_loop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::ENDLOOP:;
|
||||
expand_endloop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::TP:;
|
||||
break;
|
||||
case r8asm_macro::XOR:;
|
||||
expand_xor(tape, it.arg);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
return (tape);
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
#include "r8asm/r8asm_asm.hh"
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
static r8asm_data
|
||||
read_opr(R8Operand arg, r8asm_data default_opr = 0)
|
||||
{
|
||||
if (const std::string *operand = std::get_if<std::string>(&arg)) {
|
||||
if ((*operand) == "$")
|
||||
return dataptr;
|
||||
return (datas[*operand]);
|
||||
} else if (const r8asm_data *operand = std::get_if<r8asm_data>(&arg))
|
||||
return (*operand);
|
||||
return (default_opr);
|
||||
}
|
||||
|
||||
static void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
for (unit_size_type i = 0; i < UNIT_SIZE; i++) {
|
||||
if ((opr & 1) == 1)
|
||||
tape.push_back(rot8_bytecode::FLB);
|
||||
tape.push_back(rot8_bytecode::ROR);
|
||||
opr >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
r8asm_data opr = read_opr(arg, UNIT_MAX);
|
||||
|
||||
tape.push_back(rot8_bytecode::STP);
|
||||
expand_xor(tape, opr);
|
||||
tape.push_back(rot8_bytecode::BIZ);
|
||||
tape.push_back(rot8_bytecode::BTP); // >(xor)[<
|
||||
}
|
||||
|
||||
static void expand_endloop(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
std::vector<rot8_bytecode> tmp = { rot8_bytecode::STP,
|
||||
rot8_bytecode::FLB,
|
||||
rot8_bytecode::ROR,
|
||||
rot8_bytecode::RNZ,
|
||||
rot8_bytecode::BTP }; // >-+]<
|
||||
tape.insert(tape.end(), tmp.begin(), tmp.end());
|
||||
}
|
||||
|
||||
static void expand_tp(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
r8asm_data opr = read_opr(arg);
|
||||
|
||||
unit_offset_type offset = (unit_offset_type)opr - (unit_offset_type)dataptr;
|
||||
bool stepforward = (offset > 0);
|
||||
offset = abs(offset);
|
||||
for (unit_offset_type i = 0; i < offset; i++) {
|
||||
tape.push_back(stepforward ? rot8_bytecode::STP : rot8_bytecode::BTP);
|
||||
}
|
||||
dataptr = opr;
|
||||
}
|
||||
|
||||
static void expand_anchor(std::vector<rot8_bytecode> &tape, R8Operand arg)
|
||||
{
|
||||
if (const std::string *name = std::get_if<std::string>(&arg))
|
||||
datas.insert({ *name, dataptr });
|
||||
else if (const r8asm_data *name = std::get_if<r8asm_data>(&arg))
|
||||
datas.insert({ std::to_string(*name), dataptr });
|
||||
}
|
||||
|
||||
std::vector<rot8_bytecode>
|
||||
r8asm_tape_out(std::span<const R8Instruction> ops)
|
||||
{
|
||||
std::vector<rot8_bytecode> tape;
|
||||
for (const auto it : ops) {
|
||||
if (const rot8_bytecode *bc = std::get_if<rot8_bytecode>(&it.op)) {
|
||||
r8asm_data repeat = read_opr(it.arg);
|
||||
for (r8asm_data i = 0; i < repeat; i++) {
|
||||
tape.push_back(*bc);
|
||||
if (*bc == rot8_bytecode::STP)
|
||||
dataptr++;
|
||||
else if (*bc == rot8_bytecode::BTP)
|
||||
dataptr--;
|
||||
}
|
||||
} else if (const r8asm_macro *macro = std::get_if<r8asm_macro>(&it.op))
|
||||
switch (*macro) {
|
||||
case r8asm_macro::LOOP:
|
||||
expand_loop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::ENDLOOP:
|
||||
expand_endloop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::TP:;
|
||||
expand_tp(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::XOR:
|
||||
expand_xor(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::ANCHOR:
|
||||
expand_anchor(tape, it.arg);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
return (tape);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "r8asm/r8asm_core.hh"
|
||||
unit_size_type UNIT_SIZE = DEFAULT_UNIT_SIZE;
|
||||
mem_size_type dataptr = 0;
|
||||
|
||||
std::map<std::string, r8asm_label> labels = { { std::string("main"), 0 } };
|
||||
std::map<std::string, r8asm_data> datas = { { std::string("_UNIT_MAX"),
|
||||
UNIT_MAX } };
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "r8asm/r8asm_macro.hh"
|
||||
|
||||
std::vector<R8Instruction> expand_macro(std::string macro_name, std::vector<R8Operand> argv)
|
||||
{
|
||||
std::vector<R8Instruction> tape;
|
||||
return (tape);
|
||||
}
|
||||
+11
-5
@@ -1,12 +1,18 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "r8asm/r8asm.hh"
|
||||
int main() {
|
||||
|
||||
int main()
|
||||
{
|
||||
std::vector<R8Instruction> ins = {
|
||||
{rot8_bytecode::INP}, {r8asm_macro::LOOP}, {rot8_bytecode::BIZ},
|
||||
{rot8_bytecode::FLB}, {rot8_bytecode::RNZ}, {rot8_bytecode::ROR},
|
||||
{r8asm_macro::ENDLOOP}, {rot8_bytecode::OUT}};
|
||||
{ rot8_bytecode::INP }, { r8asm_macro::LOOP },
|
||||
{ rot8_bytecode::BIZ }, { rot8_bytecode::FLB },
|
||||
{ rot8_bytecode::RNZ }, { rot8_bytecode::ROR },
|
||||
{ r8asm_macro::ENDLOOP }, { rot8_bytecode::OUT }
|
||||
};
|
||||
std::vector<rot8_bytecode> tape = r8asm_tape_out(ins);
|
||||
for (const auto i : tape) std::cout << ROT8_BC_TO_CHAR(i);
|
||||
|
||||
for (const auto i : tape)
|
||||
std::cout << ROT8_BC_TO_CHAR(i);
|
||||
return (0);
|
||||
}
|
||||
Reference in new issue
Block a user