-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8d9a58
commit eee7d50
Showing
11 changed files
with
3,136 additions
and
2,738 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ name = "pypi" | |
|
||
[packages] | ||
sly = "*" | ||
llvmlite = "*" | ||
|
||
[dev-packages] | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,52 @@ | ||
import sys | ||
from src.Types.TokenTypes import TokenTypes | ||
from src.SymbolTable import SymbolTable | ||
from src.Parser import ZParser | ||
from src.Tokenizer import ZTokenizer | ||
from src.Codegen.CodeGen import CodeGen | ||
|
||
if __name__ == '__main__': | ||
|
||
if len(sys.argv) < 2: | ||
raise ValueError("No input file") | ||
|
||
# Get file path and read content | ||
file_path = sys.argv[1] | ||
|
||
if file_path[-2:] != '.z': | ||
raise ValueError('Invalid file type') | ||
|
||
with open(file=file_path, mode='r') as file: | ||
file_content = file.read() | ||
|
||
# Instantiate lexer and parse | ||
lexer = ZTokenizer() | ||
parser = ZParser() | ||
codegen = CodeGen() | ||
|
||
# Get tokens from file | ||
tokens = lexer.tokenize(file_content) | ||
with open('test_tokens.out', 'w') as tmp: | ||
for token in tokens: | ||
tmp.write(str(token) + '\n') | ||
|
||
# Tokenize and parse language | ||
tokens = lexer.tokenize(file_content) | ||
|
||
root = parser.parse(tokens) | ||
|
||
# Set functions declarations to symbol table | ||
symbol_table = SymbolTable() | ||
root.Evaluate(symbol_table) | ||
|
||
# Evaluate main function | ||
main_func = symbol_table.get_function('main') | ||
main_func_node = main_func.get("value", None) | ||
if main_func_node is None: | ||
raise ValueError('main function was not defined') | ||
returned_data = main_func_node.statements.Evaluate(symbol_table) | ||
|
||
# Check main function returned data type | ||
if returned_data is None: | ||
raise ValueError('main function did not return an int') | ||
|
||
main_func_node.statements.Evaluate(symbol_table) | ||
returned_type = returned_data[0] | ||
if returned_type != TokenTypes.INT: | ||
raise ValueError('main did not return an int') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from llvmlite import ir, binding | ||
|
||
|
||
class CodeGen(): | ||
def __init__(self): | ||
self.binding = binding | ||
self.binding.initialize() | ||
self.binding.initialize_native_target() | ||
self.binding.initialize_native_asmprinter() | ||
self._config_llvm() | ||
self._create_execution_engine() | ||
self._declare_print_function() | ||
|
||
def _config_llvm(self): | ||
# Config LLVM | ||
self.module = ir.Module(name=__file__) | ||
self.module.triple = self.binding.get_default_triple() | ||
func_type = ir.FunctionType(ir.VoidType(), [], False) | ||
base_func = ir.Function(self.module, func_type, name="main") | ||
block = base_func.append_basic_block(name="entry") | ||
self.builder = ir.IRBuilder(block) | ||
|
||
def _create_execution_engine(self): | ||
""" | ||
Create an ExecutionEngine suitable for JIT code generation on | ||
the host CPU. The engine is reusable for an arbitrary number of | ||
modules. | ||
""" | ||
target = self.binding.Target.from_default_triple() | ||
target_machine = target.create_target_machine() | ||
# And an execution engine with an empty backing module | ||
backing_mod = binding.parse_assembly("") | ||
engine = binding.create_mcjit_compiler(backing_mod, target_machine) | ||
self.engine = engine | ||
|
||
def _declare_print_function(self): | ||
# Declare Printf function | ||
voidptr_ty = ir.IntType(8).as_pointer() | ||
printf_ty = ir.FunctionType(ir.IntType(32), [voidptr_ty], var_arg=True) | ||
printf = ir.Function(self.module, printf_ty, name="zPrint") | ||
self.printf = printf | ||
|
||
def _compile_ir(self): | ||
""" | ||
Compile the LLVM IR string with the given engine. | ||
The compiled module object is returned. | ||
""" | ||
# Create a LLVM module object from the IR | ||
self.builder.ret_void() | ||
llvm_ir = str(self.module) | ||
mod = self.binding.parse_assembly(llvm_ir) | ||
mod.verify() | ||
# Now add the module and make sure it is ready for execution | ||
self.engine.add_module(mod) | ||
self.engine.finalize_object() | ||
self.engine.run_static_constructors() | ||
return mod | ||
|
||
def create_ir(self): | ||
self._compile_ir() | ||
|
||
def save_ir(self, filename): | ||
with open(filename, 'w') as output_file: | ||
output_file.write(str(self.module)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.