aboutsummaryrefslogtreecommitdiff
path: root/crowbar_reference_compiler
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2020-10-25 02:40:56 -0600
committerMelody Horn <melody@boringcactus.com>2020-10-25 02:40:56 -0600
commit6aa2e2a969297cc64215d94cf83612cac1644e7f (patch)
tree062e7ced1f133305be100ff00cad638602ed6063 /crowbar_reference_compiler
parent2a9b959245829efff2f0a2df1bd72180e75fc48a (diff)
downloadreference-compiler-6aa2e2a969297cc64215d94cf83612cac1644e7f.tar.gz
reference-compiler-6aa2e2a969297cc64215d94cf83612cac1644e7f.zip
do more than just emit QBE IR
Diffstat (limited to 'crowbar_reference_compiler')
-rw-r--r--crowbar_reference_compiler/__init__.py41
1 files changed, 36 insertions, 5 deletions
diff --git a/crowbar_reference_compiler/__init__.py b/crowbar_reference_compiler/__init__.py
index 92b9497..87f5aa7 100644
--- a/crowbar_reference_compiler/__init__.py
+++ b/crowbar_reference_compiler/__init__.py
@@ -5,24 +5,55 @@ from .ssagen import compile_to_ssa
def main():
import argparse
+ import subprocess
+ import sys
args = argparse.ArgumentParser(description='The reference compiler for the Crowbar programming language')
- args.add_argument('-V', '--version', action='version', version='%(prog)s 0.0.2')
+ args.add_argument('-V', '--version', action='version', version='%(prog)s 0.0.3')
args.add_argument('-g', '--include-debug-info', action='store_true')
+ args.add_argument('--stop-at-parse-tree', action='store_true')
+ args.add_argument('--stop-at-qbe-ssa', action='store_true')
args.add_argument('-S', '--stop-at-assembly', action='store_true')
args.add_argument('-o', '--out', help='output file')
args.add_argument('input', help='input file')
args = args.parse_args()
- if args.out is None:
- args.out = args.input.replace('.cro', '.ssa')
with open(args.input, 'r', encoding='utf-8') as input_file:
input_code = input_file.read()
tokens = scan(input_code)
parse_tree = parse_implementation(tokens)
+ if args.stop_at_parse_tree:
+ if args.out is None:
+ args.out = args.input.replace('.cro', '.cro.txt')
+ with open(args.out, 'w', encoding='utf-8') as output_file:
+ output_file.write(str(parse_tree))
+ return
ssa = compile_to_ssa(parse_tree)
- with open(args.out, 'w', encoding='utf-8') as output_file:
- output_file.write(ssa)
+ if args.stop_at_qbe_ssa:
+ if args.out is None:
+ args.out = args.input.replace('.cro', '.ssa')
+ with open(args.out, 'w', encoding='utf-8') as output_file:
+ output_file.write(ssa)
+ return
+ # TODO bundle the qbe binary or something
+ qbe_result = subprocess.run(['qbe', '-'], input=ssa, capture_output=True, text=True)
+ if qbe_result.returncode != 0:
+ if args.out is None:
+ args.out = args.input.replace('.cro', '.s')
+ print(qbe_result.stderr, file=sys.stderr)
+ sys.exit(1)
+ asm = qbe_result.stdout
+ if args.stop_at_assembly:
+ if args.out is None:
+ args.out = args.input.replace('.cro', '.o')
+ with open(args.out, 'w', encoding='utf-8') as output_file:
+ output_file.write(ssa)
+ return
+ if args.out is None:
+ args.out = args.input.replace('.cro', '.out')
+ # TODO don't assume gcc is always the right thing
+ gcc_result = subprocess.run(['gcc', '-x', 'assembler', '-o', args.out, '-'], input=asm, text=True)
+ sys.exit(gcc_result.returncode)
if __name__ == '__main__':