git @ Cat's Eye Technologies SixtyPical / 4bba758
--output cmdline argument, serialize() returns an array of bytes. Chris Pressey 3 years ago
8 changed file(s) with 34 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
44 ----
55
66 * Split TODO off into own file.
7 * `sixtypical` no longer writes the compiled binary to standard
8 output. The `--output` command-line argument should be given.
9 * Many tests pass when `sixtypical` is run with Python 3.
710
811 0.16
912 ----
0 #!/usr/bin/env python3
0 #!/usr/bin/env python
11
22 """Usage: sixtypical [OPTIONS] FILES
33
7878 if options.analyze_only:
7979 return
8080
81 fh = sys.stdout
81 fh = open(options.output, 'wb')
8282
8383 if options.output_format == 'raw':
8484 start_addr = 0x0000
110110 # If we are outputting a .PRG, we output the load address first.
111111 # We don't use the Emitter for this b/c not part of addr space.
112112 if options.output_format in ('prg', 'c64-basic-prg', 'vic20-basic-prg'):
113 fh.write(Word(start_addr).serialize(0))
113 fh.write(bytearray(Word(start_addr).serialize()))
114114
115115 emitter = Emitter(start_addr)
116116 for byte in prelude:
139139 help="The SixtyPical source files to compile."
140140 )
141141
142 argparser.add_argument(
143 "--output", "-o", type=str, metavar='FILENAME',
144 help="File to which generated 6502 code will be written."
145 )
142146 argparser.add_argument(
143147 "--origin", type=str, default=None,
144148 help="Location in memory where the `main` routine will be "
2323 elif [ "X$arch" = "Xapple2" ]; then
2424 src="$1"
2525 out=/tmp/a-out.bin
26 bin/sixtypical --traceback --origin=0x2000 --output-format=raw $src > $out || exit 1
26 bin/sixtypical --traceback --origin=0x2000 --output-format=raw $src --output $out || exit 1
2727 ls -la $out
2828 cp ~/scratchpad/linapple/res/Master.dsk sixtypical.dsk
2929 # TODO: replace HELLO with something that does like
5252 ### do it ###
5353
5454 out=/tmp/a-out.prg
55 bin/sixtypical --traceback --output-format=$output_format $src > $out || exit 1
55 bin/sixtypical --traceback --output-format=$output_format $src --output $out || exit 1
5656 ls -la $out
5757 $emu $out
5858 rm -f $out
77 raise NotImplementedError
88
99 def serialize(self, addr):
10 """Should return an array of unsigned bytes (integers from 0 to 255.)"""
1011 raise NotImplementedError
1112
1213
2425 return 1
2526
2627 def serialize(self, addr=None):
27 return chr(self.value)
28 return [self.value]
2829
2930 def __repr__(self):
3031 return "%s(%r)" % (self.__class__.__name__, self.value)
4243 word = self.value
4344 low = word & 255
4445 high = (word >> 8) & 255
45 return chr(low) + chr(high)
46 return [low, high]
4647
4748 def __repr__(self):
4849 return "%s(%r)" % (self.__class__.__name__, self.value)
5960 return self._size
6061
6162 def serialize(self, addr=None):
62 buf = ''.join([emittable.serialize() for emittable in self.value])
63 buf = []
64 for emittable in self.value:
65 buf.extend(emittable.serialize())
6366 while len(buf) < self.size():
64 buf += chr(0)
67 buf.append(0)
6568 return buf
6669
6770 def __repr__(self):
129132 return 1
130133
131134 def serialize(self, addr=None):
132 return self.label.serialize()[0]
135 return [self.label.serialize()[0]]
133136
134137 def __repr__(self):
135138 return "%s(%r)" % (self.__class__.__name__, self.label)
144147 return 1
145148
146149 def serialize(self, addr=None):
147 return self.label.serialize()[1]
150 return [self.label.serialize()[1]]
148151
149152 def __repr__(self):
150153 return "%s(%r)" % (self.__class__.__name__, self.label)
166169 self.addr += thing.size()
167170
168171 def serialize(self, stream):
172 """`stream` should be a file opened in binary mode."""
169173 addr = self.start_addr
170174 for emittable in self.accum:
171175 chunk = emittable.serialize(addr)
172 stream.write(chunk)
176 stream.write(bytearray(chunk))
173177 addr += len(chunk)
174178
175179 def make_label(self, name=None):
1919 return 0
2020
2121 def serialize(self, addr=None):
22 return ''
22 return []
2323
2424 def __repr__(self):
2525 return "%s()" % (self.__class__.__name__)
108108 return 1 + self.operand.size() if self.operand else 0
109109
110110 def serialize(self, addr=None):
111 return (
112 chr(self.opcodes[self.operand.__class__]) +
113 self.operand.serialize(addr)
114 )
111 serialized_operand = self.operand.serialize(addr)
112 assert isinstance(serialized_operand, list), self.operand.__class__
113 return [self.opcodes[self.operand.__class__]] + serialized_operand
115114
116115 def __repr__(self):
117116 return "%s(%r)" % (self.__class__.__name__, self.operand)
22752275
22762276 a, z, and n are trashed, and must be declared as such.
22772277
2278 (Note, both n and z are forbidden writes in this tests.)
2278 (Note, both n and z are forbidden writes in this test.)
22792279
22802280 | byte lives
22812281 | routine main
22872287
22882288 a, z, and n are trashed, and must not be declared as outputs.
22892289
2290 (Note, both n and a are unmeaningful outputs in this test.)
2291
22902292 | byte lives
22912293 | routine main
22922294 | outputs lives, a, z, n
22932295 | {
22942296 | copy 0, lives
22952297 | }
2296 ? UnmeaningfulOutputError: n
2298 ? UnmeaningfulOutputError
22972299
22982300 Unless of course you subsequently initialize them.
22992301
66 [Falderal]: http://catseye.tc/node/Falderal
77
88 -> Functionality "Compile SixtyPical program" is implemented by
9 -> shell command "bin/sixtypical --output-format=c64-basic-prg --traceback %(test-body-file) >/tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
9 -> shell command "bin/sixtypical --output-format=c64-basic-prg --traceback %(test-body-file) --output /tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
1010
1111 -> Tests for functionality "Compile SixtyPical program"
1212
4747 - Remove all elements occurring in C, from R.
4848 - Repeat until R is empty.
4949
50 When times comes to generate code, generate it in the order given by L.
50 When time comes to generate code, generate it in the order given by L.
5151 In addition, each sublist in L represents a number of routines to
5252 generate; all except the final routine in such a sublist need not have
5353 any jump instruction generated for its final `goto`.
6464 -> shell command "bin/sixtypical --optimize-fallthru --dump-fallthru-info --analyze-only --traceback %(test-body-file)"
6565
6666 -> Functionality "Compile SixtyPical program with fallthru optimization" is implemented by
67 -> shell command "bin/sixtypical --output-format=c64-basic-prg --optimize-fallthru --traceback %(test-body-file) >/tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
67 -> shell command "bin/sixtypical --output-format=c64-basic-prg --optimize-fallthru --traceback %(test-body-file) --output /tmp/foo && tests/appliances/bin/dcc6502-adapter </tmp/foo"
6868
6969 -> Tests for functionality "Dump fallthru info for SixtyPical program"
7070