2018-09-06 15:18:41 +00:00
|
|
|
#!/usr/bin/env python
|
2015-10-16 08:30:24 +00:00
|
|
|
|
|
|
|
"""Usage: sixtypical [OPTIONS] FILES
|
|
|
|
|
2018-02-05 13:17:23 +00:00
|
|
|
Analyzes and compiles a Sixtypical program.
|
2015-10-16 08:30:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from os.path import realpath, dirname, join
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.insert(0, join(dirname(realpath(sys.argv[0])), '..', 'src'))
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------- #
|
|
|
|
|
|
|
|
import codecs
|
2018-03-06 16:28:34 +00:00
|
|
|
from argparse import ArgumentParser
|
2015-10-22 14:45:16 +00:00
|
|
|
from pprint import pprint
|
2015-10-16 08:30:24 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2018-03-27 15:23:22 +00:00
|
|
|
from sixtypical.parser import Parser, ParsingContext
|
2015-10-21 14:45:14 +00:00
|
|
|
from sixtypical.analyzer import Analyzer
|
2015-10-17 11:28:39 +00:00
|
|
|
from sixtypical.emitter import Emitter, Byte, Word
|
2015-10-17 14:06:50 +00:00
|
|
|
from sixtypical.compiler import Compiler
|
2015-10-16 08:30:24 +00:00
|
|
|
|
|
|
|
|
2018-03-27 15:23:22 +00:00
|
|
|
def merge_programs(programs):
|
|
|
|
"""Assumes that the programs do not have any conflicts."""
|
|
|
|
|
|
|
|
from sixtypical.ast import Program
|
|
|
|
|
|
|
|
full = Program(1, defns=[], routines=[])
|
|
|
|
for p in programs:
|
|
|
|
full.defns.extend(p.defns)
|
|
|
|
full.routines.extend(p.routines)
|
|
|
|
|
|
|
|
return full
|
|
|
|
|
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
def process_input_files(filenames, options):
|
2018-03-27 15:23:22 +00:00
|
|
|
context = ParsingContext()
|
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
programs = []
|
|
|
|
|
|
|
|
for filename in options.filenames:
|
|
|
|
text = open(filename).read()
|
2018-03-27 15:23:22 +00:00
|
|
|
parser = Parser(context, text, filename)
|
|
|
|
if options.debug:
|
|
|
|
print(context)
|
2018-03-27 14:55:29 +00:00
|
|
|
program = parser.program()
|
|
|
|
programs.append(program)
|
|
|
|
|
|
|
|
if options.parse_only:
|
|
|
|
return
|
|
|
|
|
2018-03-27 15:23:22 +00:00
|
|
|
program = merge_programs(programs)
|
2018-03-27 14:55:29 +00:00
|
|
|
|
|
|
|
analyzer = Analyzer(debug=options.debug)
|
|
|
|
analyzer.analyze_program(program)
|
|
|
|
|
2018-04-05 09:52:14 +00:00
|
|
|
compilation_roster = None
|
2018-04-04 10:54:50 +00:00
|
|
|
if options.optimize_fallthru:
|
2018-04-04 14:09:48 +00:00
|
|
|
from sixtypical.fallthru import FallthruAnalyzer
|
|
|
|
|
2018-04-05 08:57:14 +00:00
|
|
|
def dump(data, label=None):
|
2018-04-04 13:13:53 +00:00
|
|
|
import json
|
2018-04-04 13:20:56 +00:00
|
|
|
if not options.dump_fallthru_info:
|
|
|
|
return
|
2018-04-04 13:13:53 +00:00
|
|
|
if label:
|
|
|
|
sys.stdout.write("*** {}:\n".format(label))
|
|
|
|
sys.stdout.write(json.dumps(data, indent=4, sort_keys=True))
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
|
2018-04-04 10:09:39 +00:00
|
|
|
fa = FallthruAnalyzer(debug=options.debug)
|
|
|
|
fa.analyze_program(program)
|
2018-04-05 09:52:14 +00:00
|
|
|
compilation_roster = fa.serialize()
|
|
|
|
dump(compilation_roster)
|
2018-04-04 14:09:48 +00:00
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
if options.analyze_only:
|
|
|
|
return
|
|
|
|
|
2018-09-06 15:18:41 +00:00
|
|
|
fh = open(options.output, 'wb')
|
2018-03-27 14:55:29 +00:00
|
|
|
|
2018-04-23 12:18:01 +00:00
|
|
|
if options.output_format == 'raw':
|
|
|
|
start_addr = 0x0000
|
|
|
|
prelude = []
|
|
|
|
elif options.output_format == 'prg':
|
|
|
|
start_addr = 0xc000
|
|
|
|
prelude = []
|
|
|
|
elif options.output_format == 'c64-basic-prg':
|
2018-03-27 14:55:29 +00:00
|
|
|
start_addr = 0x0801
|
|
|
|
prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
|
|
|
|
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
|
2018-04-23 12:18:01 +00:00
|
|
|
elif options.output_format == 'vic20-basic-prg':
|
2018-03-27 14:55:29 +00:00
|
|
|
start_addr = 0x1001
|
|
|
|
prelude = [0x0b, 0x10, 0xc9, 0x07, 0x9e, 0x34,
|
|
|
|
0x31, 0x30, 0x39, 0x00, 0x00, 0x00]
|
2018-04-23 12:18:01 +00:00
|
|
|
elif options.output_format == 'atari2600-cart':
|
2018-03-28 13:20:53 +00:00
|
|
|
start_addr = 0xf000
|
|
|
|
prelude = [0x78, 0xd8, 0xa2, 0xff, 0x9a, 0xa9,
|
2018-04-23 12:18:01 +00:00
|
|
|
0x00, 0x95, 0x00, 0xca, 0xd0, 0xfb]
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("Unknown output format: {}".format(options.output_format))
|
2018-03-28 13:20:53 +00:00
|
|
|
|
2018-04-23 12:18:01 +00:00
|
|
|
if options.origin is not None:
|
|
|
|
if options.origin.startswith('0x'):
|
|
|
|
start_addr = int(options.origin, 16)
|
|
|
|
else:
|
|
|
|
start_addr = int(options.origin, 10)
|
2018-03-27 14:55:29 +00:00
|
|
|
|
|
|
|
# If we are outputting a .PRG, we output the load address first.
|
|
|
|
# We don't use the Emitter for this b/c not part of addr space.
|
2018-04-23 12:18:01 +00:00
|
|
|
if options.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'):
|
2018-09-06 15:18:41 +00:00
|
|
|
fh.write(bytearray(Word(start_addr).serialize()))
|
2018-03-27 14:55:29 +00:00
|
|
|
|
|
|
|
emitter = Emitter(start_addr)
|
|
|
|
for byte in prelude:
|
|
|
|
emitter.emit(Byte(byte))
|
|
|
|
compiler = Compiler(emitter)
|
2018-04-05 09:52:14 +00:00
|
|
|
compiler.compile_program(program, compilation_roster=compilation_roster)
|
2018-03-28 13:20:53 +00:00
|
|
|
|
|
|
|
# If we are outputting a cartridge with boot and BRK address
|
|
|
|
# at the end, pad to ROM size minus 4 bytes, and emit addresses.
|
2018-04-23 12:18:01 +00:00
|
|
|
if options.output_format == 'atari2600-cart':
|
2018-03-28 13:20:53 +00:00
|
|
|
emitter.pad_to_size(4096 - 4)
|
|
|
|
emitter.emit(Word(start_addr))
|
|
|
|
emitter.emit(Word(start_addr))
|
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
if options.debug:
|
|
|
|
pprint(emitter.accum)
|
|
|
|
else:
|
|
|
|
emitter.serialize(fh)
|
|
|
|
|
|
|
|
|
2015-10-16 08:30:24 +00:00
|
|
|
if __name__ == '__main__':
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser = ArgumentParser(__doc__.strip())
|
2015-10-16 08:30:24 +00:00
|
|
|
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
'filenames', metavar='FILENAME', type=str, nargs='+',
|
|
|
|
help="The SixtyPical source files to compile."
|
|
|
|
)
|
2018-03-29 14:07:44 +00:00
|
|
|
|
2018-09-06 15:18:41 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--output", "-o", type=str, metavar='FILENAME',
|
|
|
|
help="File to which generated 6502 code will be written."
|
|
|
|
)
|
2018-03-13 13:33:01 +00:00
|
|
|
argparser.add_argument(
|
2018-04-23 12:18:01 +00:00
|
|
|
"--origin", type=str, default=None,
|
2018-03-13 13:33:01 +00:00
|
|
|
help="Location in memory where the `main` routine will be "
|
2018-04-23 12:18:01 +00:00
|
|
|
"located. Default: depends on output format."
|
2018-03-13 13:33:01 +00:00
|
|
|
)
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
2018-04-23 12:18:01 +00:00
|
|
|
"--output-format", type=str, default='raw',
|
|
|
|
help="Executable format to produce; also sets a default origin. "
|
|
|
|
"Options are: raw, prg, c64-basic-prg, vic20-basic-prg, atari2600-cart."
|
|
|
|
"Default: raw."
|
2018-03-06 16:28:34 +00:00
|
|
|
)
|
2018-03-29 14:07:44 +00:00
|
|
|
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
2018-03-29 14:07:44 +00:00
|
|
|
"--analyze-only",
|
2018-03-06 16:28:34 +00:00
|
|
|
action="store_true",
|
2018-03-29 14:07:44 +00:00
|
|
|
help="Only parse and analyze the program; do not compile it."
|
|
|
|
)
|
|
|
|
argparser.add_argument(
|
2018-04-04 10:54:50 +00:00
|
|
|
"--optimize-fallthru",
|
2018-03-29 14:07:44 +00:00
|
|
|
action="store_true",
|
2018-04-04 10:54:50 +00:00
|
|
|
help="Reorder the routines in the program to maximize the number of tail calls "
|
|
|
|
"that can be removed by having execution 'fall through' to the next routine."
|
2018-03-06 16:28:34 +00:00
|
|
|
)
|
2018-04-04 10:09:39 +00:00
|
|
|
argparser.add_argument(
|
2018-04-04 10:54:50 +00:00
|
|
|
"--dump-fallthru-info",
|
2018-04-04 10:09:39 +00:00
|
|
|
action="store_true",
|
2018-04-04 10:54:50 +00:00
|
|
|
help="Dump the fallthru map and ordering to stdout after analyzing the program."
|
2018-04-04 10:09:39 +00:00
|
|
|
)
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--parse-only",
|
|
|
|
action="store_true",
|
|
|
|
help="Only parse the program; do not analyze or compile it."
|
|
|
|
)
|
2018-03-29 14:07:44 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--debug",
|
|
|
|
action="store_true",
|
|
|
|
help="Display debugging information when analyzing and compiling."
|
|
|
|
)
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--traceback",
|
|
|
|
action="store_true",
|
|
|
|
help="When an error occurs, display a full Python traceback."
|
|
|
|
)
|
2015-10-16 08:30:24 +00:00
|
|
|
|
2018-03-06 16:28:34 +00:00
|
|
|
options, unknown = argparser.parse_known_args(sys.argv[1:])
|
|
|
|
remainder = ' '.join(unknown)
|
2015-10-16 08:30:24 +00:00
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
try:
|
|
|
|
process_input_files(options.filenames, options)
|
|
|
|
except Exception as e:
|
|
|
|
if options.traceback:
|
|
|
|
raise
|
2018-02-06 16:14:44 +00:00
|
|
|
else:
|
2018-03-27 14:55:29 +00:00
|
|
|
traceback.print_exception(e.__class__, e, None)
|
|
|
|
sys.exit(1)
|