sexy new codegen

main
Brett 2023-11-20 20:33:51 -05:00
parent 53c0961a1a
commit 7fe860d5d4
4 changed files with 112 additions and 215 deletions

View File

@ -38,8 +38,11 @@ bf:
addi $t1, $t1, 1 addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
beqz $t1, BF_CLOSE_1_0 addi $t1, $t1, 1
BF_OPEN_1_0: sb $t1, ($t0)
lb $t1, ($t0)
beqz $t1, BF_CLOSE_03e0a731165036472c4dae07_0
BF_OPEN_03e0a731165036472c4dae07_0:
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 1
@ -54,8 +57,8 @@ BF_OPEN_1_0:
addi $t1, $t1, 1 addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
beqz $t1, BF_CLOSE_2_1 beqz $t1, BF_CLOSE_972059b874ce5ef469e931bb_1
BF_OPEN_2_1: BF_OPEN_972059b874ce5ef469e931bb_1:
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 1
@ -95,8 +98,8 @@ BF_OPEN_2_1:
subi $t1, $t1, 1 subi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
bnez $t1, BF_OPEN_2_1 bnez $t1, BF_OPEN_972059b874ce5ef469e931bb_1
BF_CLOSE_2_1: BF_CLOSE_972059b874ce5ef469e931bb_1:
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 1
@ -115,19 +118,19 @@ BF_CLOSE_2_1:
addi $t1, $t1, 1 addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
beqz $t1, BF_CLOSE_3_1 beqz $t1, BF_CLOSE_4f2d57da98e6bb1619411ff9_1
BF_OPEN_3_1: BF_OPEN_4f2d57da98e6bb1619411ff9_1:
subi $t0, $t0, 1 subi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
bnez $t1, BF_OPEN_3_1 bnez $t1, BF_OPEN_4f2d57da98e6bb1619411ff9_1
BF_CLOSE_3_1: BF_CLOSE_4f2d57da98e6bb1619411ff9_1:
subi $t0, $t0, 1 subi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
subi $t1, $t1, 1 subi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
bnez $t1, BF_OPEN_1_0 bnez $t1, BF_OPEN_03e0a731165036472c4dae07_0
BF_CLOSE_1_0: BF_CLOSE_03e0a731165036472c4dae07_0:
addi $t0, $t0, 1 addi $t0, $t0, 1
addi $t0, $t0, 1 addi $t0, $t0, 1
li $v0, 11 li $v0, 11

179
mips2.asm
View File

@ -1,96 +1,48 @@
.data .data
data_address: .word 0
data_pointer: .word 0 data_pointer: .word 0
newline: .asciiz "\n"
.text .text
setup: setup:
li $v0, 9 li $v0, 9
li $a0, 30000 li $a0, 30000
syscall syscall
la $t1, data_address la $t1, data_pointer
sw $v0, ($t1) sw $v0, ($t1)
# t0 - current address (data_pointer) # t0 - current address (data_pointer)
# t1 -
# t2 - temp
move $t0, $v0 move $t0, $v0
bf: bf:
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 8
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) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
beqz $t1, BF_CLOSE_1 beqz $t1, BF_CLOSE_1
BF_OPEN_1: BF_OPEN_1:
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 4
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) sb $t1, ($t0)
lb $t1, ($t0) lb $t1, ($t0)
beqz $t1, BF_CLOSE_2 beqz $t1, BF_CLOSE_2
BF_OPEN_2: BF_OPEN_2:
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 2
sb $t1, ($t0) sb $t1, ($t0)
addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 3
sb $t1, ($t0)
addi $t0, $t0, 1
lb $t1, ($t0)
addi $t1, $t1, 3
sb $t1, ($t0) sb $t1, ($t0)
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
lb $t1, ($t0) subi $t0, $t0, 4
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) lb $t1, ($t0)
subi $t1, $t1, 1 subi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
@ -109,8 +61,7 @@ BF_CLOSE_2:
lb $t1, ($t0) lb $t1, ($t0)
subi $t1, $t1, 1 subi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
addi $t0, $t0, 1 addi $t0, $t0, 2
addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
@ -128,65 +79,30 @@ BF_CLOSE_3:
lb $t1, ($t0) lb $t1, ($t0)
bnez $t1, BF_OPEN_1 bnez $t1, BF_OPEN_1
BF_CLOSE_1: BF_CLOSE_1:
addi $t0, $t0, 1 addi $t0, $t0, 2
addi $t0, $t0, 1
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
subi $t1, $t1, 1 subi $t1, $t1, 3
sb $t1, ($t0)
lb $t1, ($t0)
subi $t1, $t1, 1
sb $t1, ($t0)
lb $t1, ($t0)
subi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 7
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) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
li $v0, 11
lb $a0, ($t0)
syscall
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 3
sb $t1, ($t0)
lb $t1, ($t0)
addi $t1, $t1, 1
sb $t1, ($t0)
lb $t1, ($t0)
addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
addi $t0, $t0, 1 addi $t0, $t0, 2
addi $t0, $t0, 1
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
@ -202,67 +118,24 @@ BF_CLOSE_1:
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 3
sb $t1, ($t0)
lb $t1, ($t0)
addi $t1, $t1, 1
sb $t1, ($t0)
lb $t1, ($t0)
addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
lb $t1, ($t0) lb $t1, ($t0)
subi $t1, $t1, 1 subi $t1, $t1, 6
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) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
lb $t1, ($t0) lb $t1, ($t0)
subi $t1, $t1, 1 subi $t1, $t1, 8
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) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
addi $t0, $t0, 1 addi $t0, $t0, 2
addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
@ -271,11 +144,11 @@ BF_CLOSE_1:
syscall syscall
addi $t0, $t0, 1 addi $t0, $t0, 1
lb $t1, ($t0) lb $t1, ($t0)
addi $t1, $t1, 1 addi $t1, $t1, 2
sb $t1, ($t0)
lb $t1, ($t0)
addi $t1, $t1, 1
sb $t1, ($t0) sb $t1, ($t0)
li $v0, 11 li $v0, 11
lb $a0, ($t0) lb $a0, ($t0)
syscall syscall
li $v0, 11
lb $a0, ($t0)
syscall

View File

@ -6,24 +6,21 @@
#include <bf_mips_codegen.h> #include <bf_mips_codegen.h>
#include <iostream> #include <iostream>
std::string preamble = ".data\n" std::string preamble = R"(.data
"\tdata_address: \t.word \t0\n" data_pointer: .word 0
"\tdata_pointer:\t.word\t0\n" .text
"\tnewline: .asciiz \"\\n\"\n" setup:
".text\n" li $v0, 9
"setup:\n" li $a0, 30000
"\tli $v0, 9\n" syscall
"\tli $a0, 30000\n"
"\tsyscall\n" la $t1, data_pointer
"\t\n" sw $v0, ($t1)
"\tla $t1, data_address\n"
"\tsw $v0, ($t1)\n" # t0 - current address (data_pointer)
"\t\n" move $t0, $v0
"\t# t0 - current address (data_pointer)\n" bf:
"\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);
@ -33,12 +30,12 @@ void codegen(tokenizer& tokenizer, std::ostream& out)
if (!tokenizer.hasNext()) if (!tokenizer.hasNext())
throw std::runtime_error("You failed to provide valid BF code"); throw std::runtime_error("You failed to provide valid BF code");
// skip past comments // skip past comments
if (tokenizer.next().type == bf_token::OPEN) if (tokenizer.peek().type == bf_token::OPEN)
while (tokenizer.hasNext() && tokenizer.next().type != bf_token::CLOSE); while (tokenizer.hasNext() && tokenizer.peek().type != bf_token::CLOSE)
tokenizer.advance();
tokenizer.print(); tokenizer.print();
size_t sp = 0;
while (tokenizer.hasNext()) while (tokenizer.hasNext())
{ {
auto& token = tokenizer.next(); auto& token = tokenizer.next();
@ -48,19 +45,19 @@ void codegen(tokenizer& tokenizer, std::ostream& out)
switch (token.type) switch (token.type)
{ {
case bf_token::INC_DP: case bf_token::INC_DP:
out << "\taddi $t0, $t0, 1\n"; out << "\taddi $t0, $t0, " << token.offset << "\n";
break; break;
case bf_token::DEC_DP: case bf_token::DEC_DP:
out << "\tsubi $t0, $t0, 1\n"; out << "\tsubi $t0, $t0, " << token.offset << "\n";
break; break;
case bf_token::INC_DV: case bf_token::INC_DV:
out << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\taddi $t1, $t1, 1\n" << "\taddi $t1, $t1, " << static_cast<uint8_t>(token.offset) << "\n"
<< "\tsb $t1, ($t0)\n"; << "\tsb $t1, ($t0)\n";
break; break;
case bf_token::DEC_DV: case bf_token::DEC_DV:
out << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\tsubi $t1, $t1, 1\n" << "\tsubi $t1, $t1, " << static_cast<uint8_t>(token.offset) << "\n"
<< "\tsb $t1, ($t0)\n"; << "\tsb $t1, ($t0)\n";
break; break;
case bf_token::PRINT: case bf_token::PRINT:
@ -77,10 +74,8 @@ void codegen(tokenizer& tokenizer, std::ostream& out)
out << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\tbeqz $t1, BF_CLOSE_" << name << '\n' << "\tbeqz $t1, BF_CLOSE_" << name << '\n'
<< "BF_OPEN_" << name << ":\n"; << "BF_OPEN_" << name << ":\n";
sp++;
break; break;
case bf_token::CLOSE: case bf_token::CLOSE:
sp--;
out << "\tlb $t1, ($t0)\n" out << "\tlb $t1, ($t0)\n"
<< "\tbnez $t1, BF_OPEN_" << name << '\n' << "\tbnez $t1, BF_OPEN_" << name << '\n'
<< "BF_CLOSE_" << name << ":\n"; << "BF_CLOSE_" << name << ":\n";

View File

@ -8,64 +8,90 @@
#include <random> #include <random>
#include <iostream> #include <iostream>
class characterizer { struct char_pair
{
char type;
size_t count;
};
class characterizer
{
private: private:
std::string program; std::string program;
size_t currentIndex = 0; size_t currentIndex = 0;
public: public:
inline bool hasNext(){ explicit characterizer(std::string program): program(std::move(program))
{}
inline bool hasNext()
{
return currentIndex < program.size(); return currentIndex < program.size();
} }
inline size_t advance(){ inline size_t advance()
{
return currentIndex++; return currentIndex++;
} }
inline char next(){ inline char next()
{
return program[advance()]; return program[advance()];
} }
inline char peek(){ inline char_pair fetch()
{
size_t start = currentIndex;
char type = next();
while (peek() == type)
advance();
return {type, currentIndex - start};
}
inline char peek()
{
return program[currentIndex]; return program[currentIndex];
} }
}; };
void tokenizer::tokenize(const std::string& program) void tokenizer::tokenize(const std::string& program)
{ {
size_t index = 0; characterizer tk{program};
while (index < program.size()) while (tk.hasNext())
{ {
auto c = program[index]; auto dv = tk.fetch();
switch (c) bf_token type = bf_token::PRINT;
switch (dv.type)
{ {
case '>': case '>':
tokens.emplace_back(bf_token::INC_DP); type = (bf_token::INC_DP);
break; break;
case '<': case '<':
tokens.emplace_back(bf_token::DEC_DP); type = (bf_token::DEC_DP);
break; break;
case '+': case '+':
tokens.emplace_back(bf_token::INC_DV); type = (bf_token::INC_DV);
break; break;
case '-': case '-':
tokens.emplace_back(bf_token::DEC_DV); type = (bf_token::DEC_DV);
break; break;
case '.': case '.':
tokens.emplace_back(bf_token::PRINT); type = (bf_token::PRINT);
break; break;
case ',': case ',':
tokens.emplace_back(bf_token::READ); type = (bf_token::READ);
break; break;
case '[': case '[':
tokens.emplace_back(bf_token::OPEN); type = (bf_token::OPEN);
break; break;
case ']': case ']':
tokens.emplace_back(bf_token::CLOSE); type = (bf_token::CLOSE);
break; break;
default: default:
break; break;
} }
index++; tokens.emplace_back(type, dv.count);
} }
} }
@ -111,19 +137,19 @@ void tokenizer::print(size_t index)
{ {
case bf_token::INC_DP: case bf_token::INC_DP:
tabulate(sp); tabulate(sp);
std::cout << "Increase DP\n"; std::cout << "Increase DP " << token.offset << "\n";
break; break;
case bf_token::DEC_DP: case bf_token::DEC_DP:
tabulate(sp); tabulate(sp);
std::cout << "Decrease DP\n"; std::cout << "Decrease DP " << token.offset << "\n";
break; break;
case bf_token::INC_DV: case bf_token::INC_DV:
tabulate(sp); tabulate(sp);
std::cout << "Increase DV\n"; std::cout << "Increase DV " << token.offset << "\n";
break; break;
case bf_token::DEC_DV: case bf_token::DEC_DV:
tabulate(sp); tabulate(sp);
std::cout << "Decrease DV\n"; std::cout << "Decrease DV " << token.offset << "\n";
break; break;
case bf_token::PRINT: case bf_token::PRINT:
tabulate(sp); tabulate(sp);