Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TartanLlama committed Mar 11, 2024
0 parents commit 50432c4
Show file tree
Hide file tree
Showing 15 changed files with 5,379 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required (VERSION 3.8)

project ("dwarbf")

add_subdirectory("dwarfas")
add_subdirectory("dwarfter")
add_subdirectory("dwarbf")
7 changes: 7 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# dwarbf - A Brainfuck interpreter written in DWARF debug information

Run the BF program with:

```shell
$ gdb --eval-command=starti --eval-command="p (int)&dwarbf_program" fib --batch
```
12 changes: 12 additions & 0 deletions dwarbf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_executable (dwarbf "dwarbf.cpp")
target_link_libraries(dwarbf PRIVATE dwarfas dwarfter)

file(READ ${CMAKE_CURRENT_SOURCE_DIR}/dwarbf.dw INTERPRETER_TEXT)
file(TOUCH ${CMAKE_CURRENT_SOURCE_DIR}/dwarbf_interpreter.hpp.in)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/dwarbf_interpreter.hpp.in
${CMAKE_CURRENT_BINARY_DIR}/dwarbf_interpreter.gen.hpp
@ONLY
)

target_include_directories(dwarbf PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
42 changes: 42 additions & 0 deletions dwarbf/dwarbf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <string>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <dwarfas.hpp>
#include <dwarfter.hpp>
#include "dwarbf_interpreter.gen.hpp"

void print_usage() {
std::cerr << "Usage: dwarbf <input_file> [--run/--debug]\n";
}
int main(int argc, const char** argv) {
if (argc < 2 or !std::filesystem::exists(argv[1])) {
print_usage();
return -1;
}
std::filesystem::path program_path = argv[1];
std::ifstream program_file(program_path);
std::ostringstream program_text;
program_text << program_file.rdbuf();

auto dwarf_program = dwas::assemble_dwarf_program(dwarbf::interpreter_text);
auto load_segment = dwas::create_elf(
dwarf_program, "dwarbf",
program_text.str(), "dwarbf_program",
program_path.replace_extension());

if (argc > 3) {
print_usage();
return -1;
}
if (argc == 3) {
if (std::string(argv[2]) == "--run" or std::string(argv[2]) == "--debug") {
auto debug = std::string(argv[2]) == "--debug";
dwter::eval(load_segment, 0x08048000, 0x08048025, dwarf_program, debug);
}
else {
print_usage();
return -1;
}
}
}
Loading

0 comments on commit 50432c4

Please sign in to comment.