Skip to content

Commit

Permalink
return
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Chen committed Oct 9, 2023
1 parent 4b198f3 commit f04c7f8
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enable_testing()
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src sources)

add_executable(pascal_jit_compiler ${sources} main.cpp)

if(ENABLE_TEST)
add_subdirectory(tests)
endif()
Expand Down
13 changes: 13 additions & 0 deletions src/AST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ class AssignmentExpressionAST: public ExpressionAST {
}
};

class ResultExpressionAST : public ExpressionAST{
public:
std::unique_ptr<ExpressionAST> assignment;
std::vector<uint8_t> codegen() override {
std::vector<uint8_t> result;
auto assignmentR9 = assignment->codegen();
addAssemblyToExecutable(result, assignmentR9);
addAssemblyToExecutable(result, mov_register_register(0, 9));
DEBUG("addAssemblyToExecutable(executable, mov_register_register(0, 9));");
return result;
}
};

// TODO find call type with signature of callee
class CallExpressionAST : public ExpressionAST {
public:
Expand Down
9 changes: 9 additions & 0 deletions src/JIT.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@

typedef void (*FuncPtr)();

typedef int (*IntFuncPtr)();

static FuncPtr createJit(const std::vector<uint8_t>& executable){
void *jitPtr = mmap(nullptr, 4096, PROT_READ | PROT_EXEC | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
std::memcpy(jitPtr, executable.data(), executable.size());
return (FuncPtr)jitPtr;
}

static IntFuncPtr createJitInt(const std::vector<uint8_t>& executable){
void *jitPtr = mmap(nullptr, 4096, PROT_READ | PROT_EXEC | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
std::memcpy(jitPtr, executable.data(), executable.size());
return (IntFuncPtr)jitPtr;
}
#endif
14 changes: 14 additions & 0 deletions src/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,25 @@ class Parser {
}
}

std::unique_ptr<ExpressionAST> parseResultExpression(){
std::unique_ptr<ResultExpressionAST> resultExpr = std::make_unique<ResultExpressionAST>();
getNextToken(); // eat Result
if(currentToken == Token::tok_assign) {
getNextToken(); // eat assign
resultExpr->assignment = parseExpression();
} else {
throw std::runtime_error("Result without assignment");
}
return resultExpr;
}

std::unique_ptr<ExpressionAST> parseExpression() {
// parse call
switch (currentToken) {
case tok_identifier:
return parseIdentifierExpression();
case tok_result:
return parseResultExpression();
case tok_string_literal: {
std::string strValue = tokenizer.stringLiteral;
getNextToken();
Expand Down
7 changes: 6 additions & 1 deletion src/Token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum Token : int {
tok_assign = -15,
tok_assign_type = -16,

tok_result = -17,
};

class Tokenizer {
Expand Down Expand Up @@ -89,6 +90,10 @@ class Tokenizer {
return Token::tok_program;
}

if (identifier == "Result") {
return Token::tok_result;
}

if (identifier == "begin") {
return Token::tok_begin;
}
Expand Down Expand Up @@ -148,7 +153,7 @@ class Tokenizer {
nextChar();
return Token::tok_neg;
}
if(_lastChar == '+') {
if (_lastChar == '+') {
nextChar();
return Token::tok_positive;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/acceptance/return.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
program ReturnInt;
begin
Result := 42;
end.
4 changes: 4 additions & 0 deletions tests/cases/return.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
program ReturnInt;
begin
Result := 42;
end.
7 changes: 4 additions & 3 deletions tests/e2e.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ template <typename T> void printAssemblyMachineCode(T &&runtime) {
}

TEST(compiler_e2e, parser) {
std::ifstream t(workspace / "tests" / "cases" / "binaryExpression.pas");
std::ifstream t(workspace / "tests" / "cases" / "return.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());
auto fun = createJitInt(program->codegen());
std::cout << "ptr: " << std::hex << (void *)fun << std::endl;
fun();
const int result = fun();
std::cout << std::dec << result << std::endl;
// printAssemblyMachineCode(program->codegen());
}

Expand Down

0 comments on commit f04c7f8

Please sign in to comment.