new codegen is borked

main
Brett 2023-11-20 19:24:45 -05:00
parent 2bcd08df90
commit 53c0961a1a
5 changed files with 340 additions and 38 deletions

View File

@ -26,11 +26,14 @@ enum class bf_token
struct token_t
{
bf_token token;
bf_token type;
int64_t offset;
std::optional<uint64_t> 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<std::reference_wrapper<const token_t>> 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

281
mips2.asm Normal file
View File

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

View File

@ -30,14 +30,13 @@ void process_print(const std::vector<token_t>& 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;
}
}

View File

@ -8,21 +8,28 @@
#include <random>
#include <iostream>
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<int> 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)
{
size_t index = 0;
@ -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<int> dis(0, 15);
for (int i = 0; i < 24; i++)
ss << dis(gen);
return ss.str();
}

View File

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