From 9922b985d6ca929d4e95c1a981b17f7a26e4783f Mon Sep 17 00:00:00 2001 From: Lev Bagryansky <32939651+levBagryansky@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:43:17 +0300 Subject: [PATCH] #15 Generating ChaiVM/interpreter/autogen/operations.hpp from yaml using python --- .gitignore | 1 + CMakeLists.txt | 9 +++ Makefile | 4 +- include/ChaiVM/interpreter/instruction.hpp | 2 +- include/ChaiVM/interpreter/operations.hpp | 67 ------------------- include/ChaiVM/utils/instr2Raw.hpp | 2 +- src/ChaiVM/interpreter/decoder.cpp | 4 +- test/ChaiVM/interpreter/decoder-test.cpp | 2 +- tools/opcode2operation-generator.py | 78 +++++++++++++++------- 9 files changed, 73 insertions(+), 96 deletions(-) delete mode 100644 include/ChaiVM/interpreter/operations.hpp diff --git a/.gitignore b/.gitignore index 8f58214..4664432 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ build/ # ide stuff .idea cmake-build-debug +include/ChaiVM/interpreter/autogen diff --git a/CMakeLists.txt b/CMakeLists.txt index e7a2540..5ee0345 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,16 @@ 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}" + 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/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/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/interpreter/operations.hpp b/include/ChaiVM/interpreter/operations.hpp deleted file mode 100644 index 49b91bc..0000000 --- a/include/ChaiVM/interpreter/operations.hpp +++ /dev/null @@ -1,67 +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, - -}; - -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/include/ChaiVM/utils/instr2Raw.hpp b/include/ChaiVM/utils/instr2Raw.hpp index c210b97..0dfdedb 100644 --- a/include/ChaiVM/utils/instr2Raw.hpp +++ b/include/ChaiVM/utils/instr2Raw.hpp @@ -2,8 +2,8 @@ #include +#include "ChaiVM/interpreter/autogen/operations.hpp" #include "ChaiVM/interpreter/instruction.hpp" -#include "ChaiVM/interpreter/operations.hpp" #include "ChaiVM/types.hpp" using namespace chai::interpreter; diff --git a/src/ChaiVM/interpreter/decoder.cpp b/src/ChaiVM/interpreter/decoder.cpp index ab0282c..85b10e3 100644 --- a/src/ChaiVM/interpreter/decoder.cpp +++ b/src/ChaiVM/interpreter/decoder.cpp @@ -1,12 +1,14 @@ #include "ChaiVM/interpreter/decoder.hpp" #include "ChaiVM/utils/bit-magic.hpp" +#include 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..ce54acf 100644 --- a/tools/opcode2operation-generator.py +++ b/tools/opcode2operation-generator.py @@ -1,28 +1,60 @@ -# @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) +import os + +def main() -> int: + if len(sys.argv) != 3: + raise RuntimeError('Should be 2 args, received ' + str(len(sys.argv))) + with open(sys.argv[2]) 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 { + Inv = 0, +{% for n, item in enumerate(items, 1) %} + {{ item.mnemonic }} = {{ item.fixedvalue }}, +{% endfor %} +}; +} // namespace chai::interpreter -instructions = full["instructions"] -opcodes = set() -print(instructions) -opcodes_arr = ["Inv"] * 255 +""" + 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, + } -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)) + directory = os.path.dirname(sys.argv[1]) + if not os.path.exists(directory): + os.makedirs(directory) + fp = open(sys.argv[1], 'w') + fp.write( + jinja2 + .Template(tpl, trim_blocks=True) + .render(content) + ) + fp.close() + return 0 -for instruction in instructions: - print(instruction["mnemonic"] + ",") -print("{", end=" ") -for operation in opcodes_arr: - print(operation + ",", end=" ") -print("}") +if __name__ == '__main__': + sys.exit(main())