diff --git a/mips.asm b/mips.asm new file mode 100644 index 0000000..4695ee5 --- /dev/null +++ b/mips.asm @@ -0,0 +1,284 @@ +.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) + addi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + beqz $t1, BF_CLOSE_03e0a731165036472c4dae07_0 +BF_OPEN_03e0a731165036472c4dae07_0: + 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_972059b874ce5ef469e931bb_1 +BF_OPEN_972059b874ce5ef469e931bb_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) + 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_972059b874ce5ef469e931bb_1 +BF_CLOSE_972059b874ce5ef469e931bb_1: + 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_4f2d57da98e6bb1619411ff9_1 +BF_OPEN_4f2d57da98e6bb1619411ff9_1: + subi $t0, $t0, 1 + lb $t1, ($t0) + bnez $t1, BF_OPEN_4f2d57da98e6bb1619411ff9_1 +BF_CLOSE_4f2d57da98e6bb1619411ff9_1: + subi $t0, $t0, 1 + lb $t1, ($t0) + subi $t1, $t1, 1 + sb $t1, ($t0) + lb $t1, ($t0) + bnez $t1, BF_OPEN_03e0a731165036472c4dae07_0 +BF_CLOSE_03e0a731165036472c4dae07_0: + 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 752d693..9361acd 100644 --- a/src/bf_mips_codegen.cpp +++ b/src/bf_mips_codegen.cpp @@ -6,10 +6,30 @@ #include #include +std::string preamble = ".data\n" + "\tdata_address: \t.word \t0\n" + "\tdata_pointer:\t.word\t0\n" + "\tnewline: .asciiz \"\\n\"\n" + ".text\n" + "setup:\n" + "\tli $v0, 9\n" + "\tli $a0, 30000\n" + "\tsyscall\n" + "\t\n" + "\tla $t1, data_address\n" + "\tsw $v0, ($t1)\n" + "\t\n" + "\t# t0 - current address (data_pointer)\n" + "\t# t1 -\n" + "\t# t2 - temp\n" + "\tmove $t0, $v0\n" + "bf:\n"; + void process_print(const std::vector& tokens, size_t index); void codegen(const std::vector& tokens, std::ostream& out) { + out << preamble; size_t index = 0; // skip past comments if (tokens[index].token == bf_token::OPEN) @@ -28,40 +48,40 @@ void codegen(const std::vector& tokens, std::ostream& out) switch (token.token) { case bf_token::INC_DP: - std::cout << "\taddi $t0, $t0, 1\n"; + out << "\taddi $t0, $t0, 1\n"; break; case bf_token::DEC_DP: - std::cout << "\tsubi $t0, $t0, 1\n"; + out << "\tsubi $t0, $t0, 1\n"; break; case bf_token::INC_DV: - std::cout << "\tlb $t1, ($t0)\n" + out << "\tlb $t1, ($t0)\n" << "\taddi $t1, $t1, 1\n" << "\tsb $t1, ($t0)\n"; break; case bf_token::DEC_DV: - std::cout << "\tlb $t1, ($t0)\n" + out << "\tlb $t1, ($t0)\n" << "\tsubi $t1, $t1, 1\n" << "\tsb $t1, ($t0)\n"; break; case bf_token::PRINT: - std::cout << "\tli $v0, 11\n" + out << "\tli $v0, 11\n" << "\tlb $a0, ($t0)\n" << "\tsyscall\n"; break; case bf_token::READ: - std::cout << "\tli $v0, 12\n" + out << "\tli $v0, 12\n" << "\tsyscall\n" << "\tsb $v0, ($t0)\n"; break; case bf_token::OPEN: - std::cout << "\tlb $t1, ($t0)\n" + out << "\tlb $t1, ($t0)\n" << "\tbeqz $t1, BF_CLOSE_" << name << "_" << std::to_string(sp) << '\n' << "BF_OPEN_" << name << "_" << std::to_string(sp) << ":\n"; sp++; break; case bf_token::CLOSE: sp--; - std::cout << "\tlb $t1, ($t0)\n" + out << "\tlb $t1, ($t0)\n" << "\tbnez $t1, BF_OPEN_" << name << "_" << std::to_string(sp) << '\n' << "BF_CLOSE_" << name << "_" << std::to_string(sp) << ":\n"; break; diff --git a/src/main.cpp b/src/main.cpp index 083b31f..abc38d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,9 +13,11 @@ int main(int argc, const char** argv) file = argv[1]; auto program = blt::fs::loadBrainFuckFile(file); + std::ofstream out{"../mips.asm"}; + auto tokens = tokenize(program); bf_name(tokens); - codegen(tokens, std::cout); + codegen(tokens, out); return 0; } \ No newline at end of file