diff --git a/include/bf_tokenizer.h b/include/bf_tokenizer.h index e2c7b46..266aa55 100644 --- a/include/bf_tokenizer.h +++ b/include/bf_tokenizer.h @@ -26,11 +26,14 @@ enum class bf_token struct token_t { - bf_token token; + bf_token type; + int64_t offset; std::optional name = {}; - explicit token_t(bf_token token): token(token) + explicit token_t(bf_token type, int64_t offset = 1): type(type), offset(offset) {} + + }; typedef std::optional> optional_token; @@ -57,20 +60,22 @@ class tokenizer return currentIndex < tokens.size(); } - inline void advance(){ - currentIndex++; + inline size_t advance(){ + return currentIndex++; } inline const token_t& next() { - return tokens[currentIndex++]; + return tokens[advance()]; } inline const token_t& peek(){ return tokens[currentIndex]; } - void print(); - void print(size_t index = 0); + void print(size_t index); + inline void print(){ + print(currentIndex); + } }; #endif //BRAINFUCK_MISC_BF_TOKENIZER_H diff --git a/mips2.asm b/mips2.asm new file mode 100644 index 0000000..c15b3d6 --- /dev/null +++ b/mips2.asm @@ -0,0 +1,281 @@ +.data + data_address: .word 0 + data_pointer: .word 0 + newline: .asciiz "\n" +.text +setup: + li $v0, 9 + li $a0, 30000 + syscall + + la $t1, data_address + sw $v0, ($t1) + + # t0 - current address (data_pointer) + # t1 - + # t2 - temp + move $t0, $v0 +bf: + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + beqz $t1, BF_CLOSE_1 +BF_OPEN_1: + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + beqz $t1, BF_CLOSE_2 +BF_OPEN_2: + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + subi $t0, $t0, 1 + subi $t0, $t0, 1 + subi $t0, $t0, 1 + subi $t0, $t0, 1 + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + bnez $t1, BF_OPEN_2 +BF_CLOSE_2: + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + addi $t0, $t0, 1 + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + addi $t0, $t0, 1 + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + beqz $t1, BF_CLOSE_3 +BF_OPEN_3: + subi $t0, $t0, 1 + lb $t1, ($t0) + bnez $t1, BF_OPEN_3 +BF_CLOSE_3: + subi $t0, $t0, 1 + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + bnez $t1, BF_OPEN_1 +BF_CLOSE_1: + addi $t0, $t0, 1 + addi $t0, $t0, 1 + li $v0, 11 + lb $a0, ($t0) + syscall + addi $t0, $t0, 1 + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + li $v0, 11 + lb $a0, ($t0) + syscall + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + addi $t0, $t0, 1 + addi $t0, $t0, 1 + li $v0, 11 + lb $a0, ($t0) + syscall + subi $t0, $t0, 1 + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + subi $t0, $t0, 1 + li $v0, 11 + lb $a0, ($t0) + syscall + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + addi $t0, $t0, 1 + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall + addi $t0, $t0, 1 + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + addi $t1, $t1, 1 + sb $t1, ($t0) + li $v0, 11 + lb $a0, ($t0) + syscall diff --git a/src/bf_mips_codegen.cpp b/src/bf_mips_codegen.cpp index 4a664e8..6776bd0 100644 --- a/src/bf_mips_codegen.cpp +++ b/src/bf_mips_codegen.cpp @@ -30,14 +30,13 @@ void process_print(const std::vector& tokens, size_t index); void codegen(tokenizer& tokenizer, std::ostream& out) { out << preamble; - size_t index = 0; if (!tokenizer.hasNext()) throw std::runtime_error("You failed to provide valid BF code"); // skip past comments - if (tokenizer.next().token == bf_token::OPEN) - while (tokenizer.hasNext() && tokenizer.next().token != bf_token::CLOSE); + if (tokenizer.next().type == bf_token::OPEN) + while (tokenizer.hasNext() && tokenizer.next().type != bf_token::CLOSE); - tokenizer.print(index); + tokenizer.print(); size_t sp = 0; while (tokenizer.hasNext()) @@ -46,7 +45,7 @@ void codegen(tokenizer& tokenizer, std::ostream& out) uint64_t name = 0; if (token.name.has_value()) name = token.name.value(); - switch (token.token) + switch (token.type) { case bf_token::INC_DP: out << "\taddi $t0, $t0, 1\n"; @@ -76,15 +75,15 @@ void codegen(tokenizer& tokenizer, std::ostream& out) break; case bf_token::OPEN: out << "\tlb $t1, ($t0)\n" - << "\tbeqz $t1, BF_CLOSE_" << name << "_" << sp << '\n' - << "BF_OPEN_" << name << "_" << sp << ":\n"; + << "\tbeqz $t1, BF_CLOSE_" << name << '\n' + << "BF_OPEN_" << name << ":\n"; sp++; break; case bf_token::CLOSE: sp--; out << "\tlb $t1, ($t0)\n" - << "\tbnez $t1, BF_OPEN_" << name << "_" << sp << '\n' - << "BF_CLOSE_" << name << "_" << sp << ":\n"; + << "\tbnez $t1, BF_OPEN_" << name << '\n' + << "BF_CLOSE_" << name << ":\n"; break; } } diff --git a/src/bf_tokenizer.cpp b/src/bf_tokenizer.cpp index 64aeddc..d160c1c 100644 --- a/src/bf_tokenizer.cpp +++ b/src/bf_tokenizer.cpp @@ -8,20 +8,27 @@ #include #include -std::string generateName() -{ - std::stringstream ss; - ss << std::hex; - static std::random_device rd; - static std::seed_seq seed{rd(), rd(), rd(), rd()}; - static std::mt19937_64 gen(seed); - static std::uniform_int_distribution dis(0, 15); - - for (int i = 0; i < 24; i++) - ss << dis(gen); - - return ss.str(); -} +class characterizer { + private: + std::string program; + size_t currentIndex = 0; + public: + inline bool hasNext(){ + return currentIndex < program.size(); + } + + inline size_t advance(){ + return currentIndex++; + } + + inline char next(){ + return program[advance()]; + } + + inline char peek(){ + return program[currentIndex]; + } +}; void tokenizer::tokenize(const std::string& program) { @@ -67,7 +74,7 @@ void tokenizer::bf_name() size_t search_index = 0; while (search_index < tokens.size()) { - if (tokens[search_index].token == bf_token::OPEN) + if (tokens[search_index].type == bf_token::OPEN) { auto name = ++conditionalCount; size_t sp = 1; @@ -75,9 +82,9 @@ void tokenizer::bf_name() tokens[search_index].name = name; while (++search_2 < tokens.size()) { - if (tokens[search_2].token == bf_token::OPEN) + if (tokens[search_2].type == bf_token::OPEN) sp++; - if (tokens[search_2].token == bf_token::CLOSE) + if (tokens[search_2].type == bf_token::CLOSE) sp--; if (sp == 0) break; @@ -100,7 +107,7 @@ void tokenizer::print(size_t index) while (index < tokens.size()) { auto& token = tokens[index++]; - switch (token.token) + switch (token.type) { case bf_token::INC_DP: tabulate(sp); @@ -148,7 +155,17 @@ void tokenizer::print(size_t index) } } -void tokenizer::print() +std::string generateName() { - print(currentIndex); -} + std::stringstream ss; + ss << std::hex; + static std::random_device rd; + static std::seed_seq seed{rd(), rd(), rd(), rd()}; + static std::mt19937_64 gen(seed); + static std::uniform_int_distribution dis(0, 15); + + for (int i = 0; i < 24; i++) + ss << dis(gen); + + return ss.str(); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index da12b99..5242e2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ int main(int argc, const char** argv) file = argv[1]; auto program = blt::fs::loadBrainFuckFile(file); - std::ofstream out{"../mips.asm"}; + std::ofstream out{"../mips2.asm"}; tokenizer tokenizer(program); codegen(tokenizer, out);