Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15 autogen #48

Merged
merged 7 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build/
# ide stuff
.idea
cmake-build-debug
include/ChaiVM/interpreter/autogen
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion include/ChaiVM/interpreter/instruction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <cstdint>

#include "operations.hpp"
#include "ChaiVM/interpreter/autogen/operations.hpp"

namespace chai::interpreter {

Expand Down
67 changes: 0 additions & 67 deletions include/ChaiVM/interpreter/operations.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion include/ChaiVM/utils/instr2Raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <cstdint>

#include "ChaiVM/interpreter/autogen/operations.hpp"
#include "ChaiVM/interpreter/instruction.hpp"
#include "ChaiVM/interpreter/operations.hpp"
#include "ChaiVM/types.hpp"

using namespace chai::interpreter;
Expand Down
4 changes: 3 additions & 1 deletion src/ChaiVM/interpreter/decoder.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "ChaiVM/interpreter/decoder.hpp"
#include "ChaiVM/utils/bit-magic.hpp"
#include <cassert>

namespace chai::interpreter::decoder {

Instruction parse(bytecode_t word) {
const Opcode opcode = utils::ExtractBits<bytecode_t, 8, 0>(word);
assert(opcode <= IcCos);
return Instruction{
.operation = opcodes2operation[opcode],
.operation = static_cast<Operation>(opcode),
.immidiate = utils::ExtractBits<bytecode_t, Immidiate, 16, 16>(word),
.r1 = utils::ExtractBits<bytecode_t, RegisterId, 8, 8>(word),
.r2 = utils::ExtractBits<bytecode_t, RegisterId, 8, 16>(word),
Expand Down
2 changes: 1 addition & 1 deletion test/ChaiVM/interpreter/decoder-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
78 changes: 55 additions & 23 deletions tools/opcode2operation-generator.py
Original file line number Diff line number Diff line change
@@ -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())