From 5e068e6fb00e360c05d95d2f6d691e50ae2572df Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 00:15:41 +0300 Subject: [PATCH 1/7] .operation = static_cast(opcode) --- Makefile | 4 ++-- src/ChaiVM/interpreter/decoder.cpp | 4 +++- test/ChaiVM/interpreter/decoder-test.cpp | 2 +- tools/opcode2operation-generator.py | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 029dcec..d316c74 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ format: ./tools/clang-format.sh $(PWD)/$(BENCH_DIR) .PHONY: build build: init - cmake -S $(PWD) -B $(PWD)/$(BUILD_DIR) -DCHAIVM_ADD_SANITIZERS=OFF + cmake -S $(PWD) -B $(PWD)/$(BUILD_DIR) -DCMAKE_BUILD_TYPE=DEBUG -DCHAIVM_ADD_SANITIZERS=OFF cmake --build $(PWD)/$(BUILD_DIR) --parallel $(JOBS) .PHONY: test @@ -45,7 +45,7 @@ test-extended: init .PHONY: bench bench: init - cmake -S $(PWD) -B $(PWD)/$(BUILD_DIR) -DCHAI_BENCH=1 + cmake -S $(PWD) -B $(PWD)/$(BUILD_DIR) -DCHAI_BENCH=1 -DCMAKE_BUILD_TYPE=RELEASE cmake --build $(PWD)/$(BUILD_DIR) --parallel $(JOBS) cd $(PWD)/$(BUILD_DIR) && make run_bench && cd $(PWD) diff --git a/src/ChaiVM/interpreter/decoder.cpp b/src/ChaiVM/interpreter/decoder.cpp index ab0282c..5751dbf 100644 --- a/src/ChaiVM/interpreter/decoder.cpp +++ b/src/ChaiVM/interpreter/decoder.cpp @@ -1,3 +1,4 @@ +#include #include "ChaiVM/interpreter/decoder.hpp" #include "ChaiVM/utils/bit-magic.hpp" @@ -5,8 +6,9 @@ namespace chai::interpreter::decoder { Instruction parse(bytecode_t word) { const Opcode opcode = utils::ExtractBits(word); + assert(opcode <= IcCos); return Instruction{ - .operation = opcodes2operation[opcode], + .operation = static_cast(opcode), .immidiate = utils::ExtractBits(word), .r1 = utils::ExtractBits(word), .r2 = utils::ExtractBits(word), diff --git a/test/ChaiVM/interpreter/decoder-test.cpp b/test/ChaiVM/interpreter/decoder-test.cpp index 529ef5a..50ad4d7 100644 --- a/test/ChaiVM/interpreter/decoder-test.cpp +++ b/test/ChaiVM/interpreter/decoder-test.cpp @@ -90,6 +90,6 @@ TEST_F(Decoder_RR, NOT_MOV) { } TEST_F(Decoder_RR, INVALID_OPERATION) { - Instruction parsed = decoder::parse(0xAB430CFA); + Instruction parsed = decoder::parse(0xAB430C00); EXPECT_EQ(parsed.operation, Inv); } diff --git a/tools/opcode2operation-generator.py b/tools/opcode2operation-generator.py index a06e875..28617ed 100644 --- a/tools/opcode2operation-generator.py +++ b/tools/opcode2operation-generator.py @@ -26,3 +26,17 @@ for operation in opcodes_arr: print(operation + ",", end=" ") print("}") + + +print("Start templating") +import jinja2 +tpl = """ +{{ title }} +{{ '-' * title|length }} +{% for n, user in enumerate(users, 1) %} +{{ n }}. {{ user.name }} - должность: {{ user.status }}, оклад: ${{ user.salary }} +{% endfor %} + """ + +content = {} + From bded05fc64b94e60fd8ba3ab11f77a6df0786965 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 01:18:33 +0300 Subject: [PATCH 2/7] implemented python script that generate operations.hpp --- .gitignore | 1 + include/ChaiVM/interpreter/operations.hpp | 28 ------- tools/opcode2operation-generator.py | 90 +++++++++++++---------- 3 files changed, 53 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 8f58214..8417fc9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ build/ # ide stuff .idea cmake-build-debug +./include/ChaiVM/interpreter/operations.hpp diff --git a/include/ChaiVM/interpreter/operations.hpp b/include/ChaiVM/interpreter/operations.hpp index 49b91bc..dfa749f 100644 --- a/include/ChaiVM/interpreter/operations.hpp +++ b/include/ChaiVM/interpreter/operations.hpp @@ -36,32 +36,4 @@ enum Operation { }; -constexpr Operation opcodes2operation[] = { - Inv, Nop, Ret, Mov, Ldia, Ldra, Star, Add, Addi, Sub, - Subi, Mul, Muli, Div, Divi, Ldiaf, Addf, Addif, Subf, Subif, - Mulf, Mulif, Divf, Divif, IcPrint, IcScani, IcScanf, IcSqrt, IcSin, IcCos, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, Inv, - Inv, Inv, Inv, Inv, Inv, -}; } // namespace chai::interpreter diff --git a/tools/opcode2operation-generator.py b/tools/opcode2operation-generator.py index 28617ed..032276f 100644 --- a/tools/opcode2operation-generator.py +++ b/tools/opcode2operation-generator.py @@ -1,42 +1,56 @@ -# @todo #8:90m Finish this script and genereate operations.hpp via yml file - +import sys +from datetime import datetime +import jinja2 import yaml -with open("resources/instructions.yml") as file: - full = yaml.safe_load(file) - -instructions = full["instructions"] -opcodes = set() -print(instructions) -opcodes_arr = ["Inv"] * 255 - -for instruction in instructions: - print(instruction) - old_len = len(opcodes) - opcodes.add(instruction["fixedvalue"]) - new_len = len(opcodes) - if(new_len == old_len): - raise RuntimeError("opcode " + string(instruction["fixedvalue"]) + " repeats") - opcodes_arr[instruction["fixedvalue"]] = instruction["mnemonic"] -print(max(opcodes)) - -for instruction in instructions: - print(instruction["mnemonic"] + ",") -print("{", end=" ") -for operation in opcodes_arr: - print(operation + ",", end=" ") -print("}") - -print("Start templating") -import jinja2 -tpl = """ -{{ title }} -{{ '-' * title|length }} -{% for n, user in enumerate(users, 1) %} -{{ n }}. {{ user.name }} - должность: {{ user.status }}, оклад: ${{ user.salary }} +def main() -> int: + if len(sys.argv) != 2: + raise RuntimeError('Should be 2 args, received ' + str(len(sys.argv))) + with open("resources/instructions.yml") as file: + full = yaml.safe_load(file) + instructions = full["instructions"] + opcodes = set() + # print(instructions) + opcodes_arr = ["Inv"] * 255 + + # Validating yaml file + for instruction in instructions: + # print(instruction) + old_len = len(opcodes) + opcodes.add(instruction["fixedvalue"]) + new_len = len(opcodes) + if (new_len == old_len): + raise RuntimeError("opcode " + str(instruction["fixedvalue"]) + " repeats") + opcodes_arr[instruction["fixedvalue"]] = instruction["mnemonic"] + # print(max(opcodes)) + tpl = """{{ disclaimer }} +#pragma once +namespace chai::interpreter { +enum Operation { +{% for n, item in enumerate(items, 1) %} + {{ item.mnemonic }} = {{ item.fixedvalue }}, {% endfor %} - """ - -content = {} - +}; +} // namespace chai::interpreter + +""" + content = { + 'disclaimer': '/* This file was automatically generated by the script ' + + sys.argv[0] + ' at ' + datetime.now().strftime("%d.%m.%Y %H:%M:%S") + ' */', + 'items': instructions, + 'enumerate': enumerate, + } + + fp = open(sys.argv[1], 'w') + fp.write( + jinja2 + .Template(tpl, trim_blocks=True) + .render(content) + ) + fp.close() + return 0 + + +if __name__ == '__main__': + sys.exit(main()) From 4ea024629288ec1582545693cf8a04b2b2fbc59f Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 01:19:01 +0300 Subject: [PATCH 3/7] deleted operations.hpp --- include/ChaiVM/interpreter/operations.hpp | 39 ----------------------- 1 file changed, 39 deletions(-) delete mode 100644 include/ChaiVM/interpreter/operations.hpp diff --git a/include/ChaiVM/interpreter/operations.hpp b/include/ChaiVM/interpreter/operations.hpp deleted file mode 100644 index dfa749f..0000000 --- a/include/ChaiVM/interpreter/operations.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -namespace chai::interpreter { - -enum Operation { - Inv, - Nop, - Ret, - Mov, - Ldia, - Ldra, - Star, - Add, - Addi, - Sub, - Subi, - Mul, - Muli, - Div, - Divi, - Ldiaf, - Addf, - Addif, - Subf, - Subif, - Mulf, - Mulif, - Divf, - Divif, - IcPrint, - IcScani, - IcScanf, - IcSqrt, - IcSin, - IcCos, - -}; - -} // namespace chai::interpreter From 3d8c7734c6b9474b74e3f33fc61808118c9c6b2f Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 02:04:47 +0300 Subject: [PATCH 4/7] Completed autogen --- .gitignore | 2 +- CMakeLists.txt | 8 ++++++++ include/ChaiVM/interpreter/instruction.hpp | 2 +- include/ChaiVM/utils/instr2Raw.hpp | 2 +- tools/opcode2operation-generator.py | 15 ++++++++++++--- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8417fc9..4664432 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ build/ # ide stuff .idea cmake-build-debug -./include/ChaiVM/interpreter/operations.hpp +include/ChaiVM/interpreter/autogen diff --git a/CMakeLists.txt b/CMakeLists.txt index e7a2540..90670d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,15 @@ if (CHAI_BENCH) add_subdirectory(bench) endif () +set(OPERATIONS_DIR ${CMAKE_SOURCE_DIR}/include/ChaiVM/interpreter/autogen/operations.hpp) +add_custom_target(gen_operations_header + COMMENT "Generating ${OPERATIONS_DIR}" + OUTPUT ${OPERATIONS_DIR} + COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/opcode2operation-generator.py ${OPERATIONS_DIR} ${CMAKE_SOURCE_DIR}/tools/resources/instructions.yml + ) + add_library(chai_include INTERFACE) +add_dependencies(chai_include gen_operations_header) target_include_directories(chai_include INTERFACE include ) diff --git a/include/ChaiVM/interpreter/instruction.hpp b/include/ChaiVM/interpreter/instruction.hpp index 51116b8..6a520be 100644 --- a/include/ChaiVM/interpreter/instruction.hpp +++ b/include/ChaiVM/interpreter/instruction.hpp @@ -2,7 +2,7 @@ #include -#include "operations.hpp" +#include "ChaiVM/interpreter/autogen/operations.hpp" namespace chai::interpreter { diff --git a/include/ChaiVM/utils/instr2Raw.hpp b/include/ChaiVM/utils/instr2Raw.hpp index c210b97..267fa97 100644 --- a/include/ChaiVM/utils/instr2Raw.hpp +++ b/include/ChaiVM/utils/instr2Raw.hpp @@ -3,7 +3,7 @@ #include #include "ChaiVM/interpreter/instruction.hpp" -#include "ChaiVM/interpreter/operations.hpp" +#include "ChaiVM/interpreter/autogen/operations.hpp" #include "ChaiVM/types.hpp" using namespace chai::interpreter; diff --git a/tools/opcode2operation-generator.py b/tools/opcode2operation-generator.py index 032276f..4a156d8 100644 --- a/tools/opcode2operation-generator.py +++ b/tools/opcode2operation-generator.py @@ -2,12 +2,12 @@ from datetime import datetime import jinja2 import yaml - +import os def main() -> int: - if len(sys.argv) != 2: + if len(sys.argv) != 3: raise RuntimeError('Should be 2 args, received ' + str(len(sys.argv))) - with open("resources/instructions.yml") as file: + with open(sys.argv[2]) as file: full = yaml.safe_load(file) instructions = full["instructions"] opcodes = set() @@ -28,6 +28,7 @@ def main() -> int: #pragma once namespace chai::interpreter { enum Operation { + Inv = 0, {% for n, item in enumerate(items, 1) %} {{ item.mnemonic }} = {{ item.fixedvalue }}, {% endfor %} @@ -42,7 +43,15 @@ def main() -> int: 'enumerate': enumerate, } + directory = os.path.dirname(sys.argv[1]) + if not os.path.exists(directory): + os.makedirs(directory) fp = open(sys.argv[1], 'w') + print('ABOBA') + print(sys.argv[1]) + print(jinja2 + .Template(tpl, trim_blocks=True) + .render(content)) fp.write( jinja2 .Template(tpl, trim_blocks=True) From f6e7920dc3b300942cafc16d1e424c262cc02a88 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 02:09:28 +0300 Subject: [PATCH 5/7] make format --- include/ChaiVM/utils/instr2Raw.hpp | 2 +- src/ChaiVM/interpreter/decoder.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ChaiVM/utils/instr2Raw.hpp b/include/ChaiVM/utils/instr2Raw.hpp index 267fa97..0dfdedb 100644 --- a/include/ChaiVM/utils/instr2Raw.hpp +++ b/include/ChaiVM/utils/instr2Raw.hpp @@ -2,8 +2,8 @@ #include -#include "ChaiVM/interpreter/instruction.hpp" #include "ChaiVM/interpreter/autogen/operations.hpp" +#include "ChaiVM/interpreter/instruction.hpp" #include "ChaiVM/types.hpp" using namespace chai::interpreter; diff --git a/src/ChaiVM/interpreter/decoder.cpp b/src/ChaiVM/interpreter/decoder.cpp index 5751dbf..85b10e3 100644 --- a/src/ChaiVM/interpreter/decoder.cpp +++ b/src/ChaiVM/interpreter/decoder.cpp @@ -1,6 +1,6 @@ -#include #include "ChaiVM/interpreter/decoder.hpp" #include "ChaiVM/utils/bit-magic.hpp" +#include namespace chai::interpreter::decoder { From 9461260c58ef0ed5761dafc1963f25f622e07870 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 13:33:44 +0300 Subject: [PATCH 6/7] removed prints --- tools/opcode2operation-generator.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tools/opcode2operation-generator.py b/tools/opcode2operation-generator.py index 4a156d8..ce54acf 100644 --- a/tools/opcode2operation-generator.py +++ b/tools/opcode2operation-generator.py @@ -47,11 +47,6 @@ def main() -> int: if not os.path.exists(directory): os.makedirs(directory) fp = open(sys.argv[1], 'w') - print('ABOBA') - print(sys.argv[1]) - print(jinja2 - .Template(tpl, trim_blocks=True) - .render(content)) fp.write( jinja2 .Template(tpl, trim_blocks=True) From 5d3abad20beb6802df9df5b3a518b76214d46ab3 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 31 Oct 2023 13:40:25 +0300 Subject: [PATCH 7/7] removed prints --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 90670d8..5ee0345 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ if (CHAI_BENCH) add_subdirectory(bench) endif () +# @todo #15:90min Generate autogen/operations.hpp into build directory. set(OPERATIONS_DIR ${CMAKE_SOURCE_DIR}/include/ChaiVM/interpreter/autogen/operations.hpp) add_custom_target(gen_operations_header COMMENT "Generating ${OPERATIONS_DIR}"