output to file

main
Brett 2023-11-17 02:12:11 -05:00
parent df23c32c69
commit 308f7876ba
3 changed files with 315 additions and 9 deletions

284
mips.asm Normal file
View File

@ -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

View File

@ -6,10 +6,30 @@
#include <bf_mips_codegen.h> #include <bf_mips_codegen.h>
#include <iostream> #include <iostream>
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<token_t>& tokens, size_t index); void process_print(const std::vector<token_t>& tokens, size_t index);
void codegen(const std::vector<token_t>& tokens, std::ostream& out) void codegen(const std::vector<token_t>& tokens, std::ostream& out)
{ {
out << preamble;
size_t index = 0; size_t index = 0;
// skip past comments // skip past comments
if (tokens[index].token == bf_token::OPEN) if (tokens[index].token == bf_token::OPEN)
@ -28,40 +48,40 @@ void codegen(const std::vector<token_t>& tokens, std::ostream& out)
switch (token.token) switch (token.token)
{ {
case bf_token::INC_DP: case bf_token::INC_DP:
std::cout << "\taddi $t0, $t0, 1\n"; out << "\taddi $t0, $t0, 1\n";
break; break;
case bf_token::DEC_DP: case bf_token::DEC_DP:
std::cout << "\tsubi $t0, $t0, 1\n"; out << "\tsubi $t0, $t0, 1\n";
break; break;
case bf_token::INC_DV: case bf_token::INC_DV:
std::cout << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\taddi $t1, $t1, 1\n" << "\taddi $t1, $t1, 1\n"
<< "\tsb $t1, ($t0)\n"; << "\tsb $t1, ($t0)\n";
break; break;
case bf_token::DEC_DV: case bf_token::DEC_DV:
std::cout << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\tsubi $t1, $t1, 1\n" << "\tsubi $t1, $t1, 1\n"
<< "\tsb $t1, ($t0)\n"; << "\tsb $t1, ($t0)\n";
break; break;
case bf_token::PRINT: case bf_token::PRINT:
std::cout << "\tli $v0, 11\n" out << "\tli $v0, 11\n"
<< "\tlb $a0, ($t0)\n" << "\tlb $a0, ($t0)\n"
<< "\tsyscall\n"; << "\tsyscall\n";
break; break;
case bf_token::READ: case bf_token::READ:
std::cout << "\tli $v0, 12\n" out << "\tli $v0, 12\n"
<< "\tsyscall\n" << "\tsyscall\n"
<< "\tsb $v0, ($t0)\n"; << "\tsb $v0, ($t0)\n";
break; break;
case bf_token::OPEN: 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' << "\tbeqz $t1, BF_CLOSE_" << name << "_" << std::to_string(sp) << '\n'
<< "BF_OPEN_" << name << "_" << std::to_string(sp) << ":\n"; << "BF_OPEN_" << name << "_" << std::to_string(sp) << ":\n";
sp++; sp++;
break; break;
case bf_token::CLOSE: case bf_token::CLOSE:
sp--; sp--;
std::cout << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\tbnez $t1, BF_OPEN_" << name << "_" << std::to_string(sp) << '\n' << "\tbnez $t1, BF_OPEN_" << name << "_" << std::to_string(sp) << '\n'
<< "BF_CLOSE_" << name << "_" << std::to_string(sp) << ":\n"; << "BF_CLOSE_" << name << "_" << std::to_string(sp) << ":\n";
break; break;

View File

@ -13,9 +13,11 @@ int main(int argc, const char** argv)
file = argv[1]; file = argv[1];
auto program = blt::fs::loadBrainFuckFile(file); auto program = blt::fs::loadBrainFuckFile(file);
std::ofstream out{"../mips.asm"};
auto tokens = tokenize(program); auto tokens = tokenize(program);
bf_name(tokens); bf_name(tokens);
codegen(tokens, std::cout); codegen(tokens, out);
return 0; return 0;
} }