From 6aa2e2a969297cc64215d94cf83612cac1644e7f Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sun, 25 Oct 2020 02:40:56 -0600 Subject: do more than just emit QBE IR --- crowbar_reference_compiler/__init__.py | 41 +++++++++++++++++++++++++++++----- pyproject.toml | 2 +- 2 files changed, 37 insertions(+), 6 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__': diff --git a/pyproject.toml b/pyproject.toml index d1863aa..d7ef861 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "crowbar-reference-compiler" -version = "0.0.2" +version = "0.0.3" description = "the reference compiler for the Crowbar programming language" authors = ["Melody Horn "] license = "BlueOak-1.0.0" -- cgit v1.2.3