-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (45 loc) · 1.25 KB
/
Copy pathmain.py
File metadata and controls
51 lines (45 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import ctypes
import sys
from pathlib import Path
import llvmlite.binding as llvm
from pascal_llvm.compiler import Compiler
from pascal_llvm.parser import parse
from pascal_llvm.tokenizer import tokenize
from pascal_llvm.type_system.visitor import TypeSystem
# read
source = Path(sys.argv[1]).read_text()
# compile
tokens = tokenize(source)
program = parse(tokens)
ts = TypeSystem()
ts.visit(program)
compiler = Compiler(ts)
compiler.visit(program)
module = compiler.module
# translate
module = llvm.parse_assembly(str(module))
module.verify()
# optimize
pm_builder = llvm.PassManagerBuilder()
pm = llvm.ModulePassManager()
pm_builder.populate(pm)
# add passes
pm.add_constant_merge_pass()
pm.add_instruction_combining_pass()
pm.add_reassociate_expressions_pass()
pm.add_gvn_pass()
pm.add_cfg_simplification_pass()
pm.add_loop_simplification_pass()
pm.run(module)
# run
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
target = llvm.Target.from_default_triple()
machine = target.create_target_machine()
engine = llvm.create_mcjit_compiler(llvm.parse_assembly(""), machine)
engine.add_module(module)
engine.finalize_object()
engine.run_static_constructors()
main = ctypes.CFUNCTYPE(None)(engine.get_function_address('.main'))
main()