Skip to content

Commit

Permalink
executable
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Chen committed Oct 8, 2023
1 parent bdcbe2e commit d199e2e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 66 deletions.
19 changes: 18 additions & 1 deletion src/AST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <vector>

static uint32_t codeGenScopeIndex = 0U;

#define asm_debug
#ifdef asm_debug
#define DEBUG(STR) \
std::cout << STR << std::endl;
Expand Down Expand Up @@ -41,6 +41,23 @@ std::unique_ptr<ExpressionAST> RHS;

virtual std::vector<uint8_t> codegen() override {
std::vector<uint8_t> result;
// move LHS to R9
addAssemblyToExecutable(result, LHS->codegen());
addAssemblyToExecutable(result,mov_register_register(10U, 9U)); // LHS to 10
DEBUG("addAssemblyToExecutable(executable, mov_register_register(10, 9));");
// mov RHS to R9
addAssemblyToExecutable(result, RHS->codegen());
if (op == Token::tok_positive) {
// x + y
addAssemblyToExecutable(result, add_register_register(9, 10, 9));
DEBUG("addAssemblyToExecutable(executable, add_register_register(9, 10, 9));");
} else if (op == Token::tok_neg) {
// x - y
addAssemblyToExecutable(result, sub_register_register(9, 10, 9));
DEBUG("addAssemblyToExecutable(executable, sub_register_register(9, 10, 9));");
} else {
throw std::runtime_error("unsupported binary operator");
}
return result;
}
};
Expand Down
6 changes: 2 additions & 4 deletions tests/cases/binaryExpression.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
var a:Integer;
var b:Integer;
var c:Integer;
var d:Integer;
begin
a := 11;
b := 22;
c := 33;
d := (a+(c-b));
writeln(d);
c := (a+b);
writeln(c);
end.
114 changes: 53 additions & 61 deletions tests/e2e.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,80 +29,72 @@ const std::filesystem::path workspace =
// auto fun = createJit(program->codegen());
// fun();
// }
template< typename T>
void printAssemblyMachineCode(T&& runtime) {
template <typename T> void printAssemblyMachineCode(T &&runtime) {
int index = 0;
for(uint8_t code : runtime) {
for (uint8_t code : runtime) {
std::cout << std::hex << static_cast<uint32_t>(code);
index++;
if (index >= 4) {
index = 0;
std::cout << std::endl;
} else {
std::cout<< "_";
std::cout << "_";
}
}
}

TEST(compiler_e2e, parser) {
std::ifstream t(workspace / "tests" / "cases" / "binaryExpression.pas");
std::stringstream buffer;
buffer << t.rdbuf();
Tokenizer tokenizer(buffer.str());
Parser parser(tokenizer, std::make_shared<Runtime>());
parser.debug = true;
auto program = parser.parse();
auto fun = createJit(program->codegen());
std::cout << "ptr: " << std::hex<< (void*) fun << std::endl;
// fun();
// printAssemblyMachineCode(program->codegen());
}

// void mylog(char* c){
// printf("%s\n", c);
// }

// void mylogi(int c) {
// printf("%d\n", c);
// TEST(compiler_e2e, parser) {
// std::ifstream t(workspace / "tests" / "cases" / "binaryExpression.pas");
// std::stringstream buffer;
// buffer << t.rdbuf();
// Tokenizer tokenizer(buffer.str());
// Parser parser(tokenizer, std::make_shared<Runtime>());
// parser.debug = true;
// auto program = parser.parse();
// auto fun = createJit(program->codegen());
// std::cout << "ptr: " << std::hex << (void *)fun << std::endl;
// fun();
// // printAssemblyMachineCode(program->codegen());
// }

// TEST(compiler_e2e, local_variable) {
// std::vector<uint8_t> executable;
// const char* test = "you are a good boy";
// const char* test2 = "I am jesse";
// addAssemblyToExecutable(executable, storeX29X30());
// addAssemblyToExecutable(executable, mov_register_register_from_to_sp(29, 31));
// addAssemblyToExecutable(executable, sub_register_imm(31, 31, 0x10));
// addAssemblyToExecutable(executable, insertPtrToRegister(9,test));
// addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x8));
// addAssemblyToExecutable(executable, str_register_register_offset(9, 10, 0));
// addAssemblyToExecutable(executable, insertPtrToRegister(9,test2));
// addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x10));
// addAssemblyToExecutable(executable, str_register_register_offset(9, 10, 0));
// addAssemblyToExecutable(executable, sub_register_imm(31, 31, 0x10));
// addAssemblyToExecutable(executable, insertIntegerToRegister(9,42));
// addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x18));
// addAssemblyToExecutable(executable, str_register_register_offset(9, 10, 0));
// addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x8));
// addAssemblyToExecutable(executable, ldr_register_register_offset(9, 10, 0));
// addAssemblyToExecutable(executable, mov_register_register(0, 9));
// addAssemblyToExecutable(executable, insertPtrToRegister(9, (void*)mylog));
// addAssemblyToExecutable(executable, callRegister(9));
// addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x18));
// addAssemblyToExecutable(executable, ldr_register_register_offset(9, 10, 0));
// addAssemblyToExecutable(executable, mov_register_register(0, 9));
// addAssemblyToExecutable(executable, insertPtrToRegister(9,(void*) mylogi));
// addAssemblyToExecutable(executable, callRegister(9));
// addAssemblyToExecutable(executable, mov_register_register_from_to_sp(31, 29));
// addAssemblyToExecutable(executable, loadX29X30());
// addAssemblyToExecutable(executable, ret());
// // asm("mov x0, #0xffffffff");
// // addAssemblyToExecutable(executable, insertIntegerToRegister(0, 0xffffffff));
// // printAssemblyMachineCode(executable);
// auto fun = createJit(executable);
// // std::cout << "ptr: 0x" << std::hex<< (void*) fun << std::endl;
// // fun();
// }
void mylog(char *c) { printf("%s\n", c); }

void mylogi(int c) { printf("%d\n", c); }

TEST(compiler_e2e, local_variable) {
std::vector<uint8_t> executable;
const char *test = "you are a good boy";
const char *test2 = "I am jesse";
addAssemblyToExecutable(executable, storeX29X30());
addAssemblyToExecutable(executable, mov_register_register_from_to_sp(29, 31));
addAssemblyToExecutable(executable, sub_register_imm(31, 31, 0x10));
addAssemblyToExecutable(executable, sub_register_imm(31, 31, 0x10));
addAssemblyToExecutable(executable, insertIntegerToRegister(9, 11));
addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x8));
addAssemblyToExecutable(executable, str_register_register_offset(9, 10, 0));
addAssemblyToExecutable(executable, insertIntegerToRegister(9, 22));
addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x10));
addAssemblyToExecutable(executable, str_register_register_offset(9, 10, 0));
addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x8));
addAssemblyToExecutable(executable, ldr_register_register_offset(9, 10, 0));
addAssemblyToExecutable(executable, mov_register_register(10, 9));
addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x10));
addAssemblyToExecutable(executable, ldr_register_register_offset(9, 10, 0));
addAssemblyToExecutable(executable, add_register_register(9, 10, 9));
addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x18));
addAssemblyToExecutable(executable, str_register_register_offset(9, 10, 0));
addAssemblyToExecutable(executable, sub_register_imm(10, 29, 0x18));
addAssemblyToExecutable(executable, ldr_register_register_offset(9, 10, 0));
addAssemblyToExecutable(executable, mov_register_register(0, 9));
addAssemblyToExecutable(executable, insertPtrToRegister(9, (void *)mylogi));
addAssemblyToExecutable(executable, callRegister(9));
addAssemblyToExecutable(executable, mov_register_register_from_to_sp(31, 29));
addAssemblyToExecutable(executable, loadX29X30());
addAssemblyToExecutable(executable, ret());
auto fun = createJit(executable);
// std::cout << "ptr: 0x" << std::hex<< (void*) fun << std::endl;
fun();
}

// TEST(compiler_e2e, variable) {
// std::vector<uint8_t> result;
Expand Down

0 comments on commit d199e2e

Please sign in to comment.