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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# Copyright 2015 David Malcolm <dmalcolm@redhat.com>
# Copyright 2015 Red Hat, Inc.
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
# A compiler for the "bf" language
import sys
import gccjit
class Paren:
def __init__(self, b_test, b_body, b_after):
self.b_test = b_test
self.b_body = b_body
self.b_after = b_after
class CompileError(Exception):
def __init__(self, compiler, msg):
self.filename = compiler.filename
self.line = compiler.line
self.column = compiler.column
self.msg = msg
def __str__(self):
return ("%s:%i:%i: %s"
% (self.filename, self.line, self.column, self.msg))
class Compiler:
def __init__(self): #, filename):
self.ctxt = gccjit.Context()
if 1:
self.ctxt.set_int_option(gccjit.IntOption.OPTIMIZATION_LEVEL,
3);
self.ctxt.set_bool_option(gccjit.BoolOption.DUMP_INITIAL_GIMPLE,
0);
self.ctxt.set_bool_option(gccjit.BoolOption.DUMP_GENERATED_CODE,
0);
self.ctxt.set_bool_option(gccjit.BoolOption.DEBUGINFO,
1);
self.ctxt.set_bool_option(gccjit.BoolOption.DUMP_EVERYTHING,
0);
self.ctxt.set_bool_option(gccjit.BoolOption.KEEP_INTERMEDIATES,
0);
self.void_type = self.ctxt.get_type(gccjit.TypeKind.VOID)
self.int_type = self.ctxt.get_type(gccjit.TypeKind.INT)
self.byte_type = self.ctxt.get_type(gccjit.TypeKind.UNSIGNED_CHAR)
self.array_type = self.ctxt.new_array_type(self.byte_type,
30000)
self.func_getchar = (
self.ctxt.new_function(gccjit.FunctionKind.IMPORTED,
self.int_type,
b"getchar", []))
self.func_putchar = (
self.ctxt.new_function(gccjit.FunctionKind.IMPORTED,
self.void_type,
b"putchar",
[self.ctxt.new_param(self.int_type,
b"c")]))
self.func = self.ctxt.new_function(gccjit.FunctionKind.EXPORTED,
self.void_type, b'func', [])
self.curblock = self.func.new_block(b"initial")
self.int_zero = self.ctxt.zero(self.int_type)
self.int_one = self.ctxt.one(self.int_type)
self.byte_zero = self.ctxt.zero(self.byte_type)
self.byte_one = self.ctxt.one(self.byte_type)
self.data_cells = self.ctxt.new_global(gccjit.GlobalKind.INTERNAL,
self.array_type,
b"data_cells")
self.idx = self.func.new_local(self.int_type,
b"idx")
self.open_parens = []
self.curblock.add_comment(b"idx = 0;")
self.curblock.add_assignment(self.idx,
self.int_zero)
def get_current_data(self, loc):
"""Get 'data_cells[idx]' as an lvalue. """
return self.ctxt.new_array_access(self.data_cells,
self.idx,
loc)
def current_data_is_zero(self, loc):
"""Get 'data_cells[idx] == 0' as a boolean rvalue."""
return self.ctxt.new_comparison(gccjit.Comparison.EQ,
self.get_current_data(loc),
self.byte_zero,
loc)
def compile_char(self, ch):
"""Compile one bf character."""
loc = self.ctxt.new_location(self.filename,
self.line,
self.column)
# Turn this on to trace execution, by injecting putchar()
# of each source char.
if 0:
arg = self.ctxt.new_rvalue_from_int (self.int_type,
ch)
call = self.ctxt.new_call (self.func_putchar,
[arg],
loc)
self.curblock.add_eval (call, loc)
if ch == '>':
self.curblock.add_comment(b"'>': idx += 1;", loc)
self.curblock.add_assignment_op(self.idx,
gccjit.BinaryOp.PLUS,
self.int_one,
loc)
elif ch == '<':
self.curblock.add_comment(b"'<': idx -= 1;", loc)
self.curblock.add_assignment_op(self.idx,
gccjit.BinaryOp.MINUS,
self.int_one,
loc)
elif ch == '+':
self.curblock.add_comment(b"'+': data[idx] += 1;", loc)
self.curblock.add_assignment_op(self.get_current_data (loc),
gccjit.BinaryOp.PLUS,
self.byte_one,
loc)
elif ch == '-':
self.curblock.add_comment(b"'-': data[idx] -= 1;", loc)
self.curblock.add_assignment_op(self.get_current_data(loc),
gccjit.BinaryOp.MINUS,
self.byte_one,
loc)
elif ch == '.':
arg = self.ctxt.new_cast(self.get_current_data(loc),
self.int_type,
loc)
call = self.ctxt.new_call(self.func_putchar,
[arg],
loc)
self.curblock.add_comment(b"'.': putchar ((int)data[idx]);",
loc)
self.curblock.add_eval(call, loc)
elif ch == ',':
call = self.ctxt.new_call(self.func_getchar, [], loc)
self.curblock.add_comment(b"',': data[idx] = (unsigned char)getchar ();",
loc)
self.curblock.add_assignment(self.get_current_data(loc),
self.ctxt.new_cast(call,
self.byte_type,
loc),
loc)
elif ch == '[':
loop_test = self.func.new_block()
on_zero = self.func.new_block()
on_non_zero = self.func.new_block()
self.curblock.end_with_jump(loop_test, loc)
loop_test.add_comment(b"'['", loc)
loop_test.end_with_conditional(self.current_data_is_zero(loc),
on_zero,
on_non_zero,
loc)
self.open_parens.append(Paren(loop_test, on_non_zero, on_zero))
self.curblock = on_non_zero;
elif ch == ']':
self.curblock.add_comment(b"']'", loc)
if not self.open_parens:
raise CompileError(self, "mismatching parens")
paren = self.open_parens.pop()
self.curblock.end_with_jump(paren.b_test)
self.curblock = paren.b_after
elif ch == '\n':
self.line +=1;
self.column = 0;
if ch != '\n':
self.column += 1
def parse_into_ctxt(self, filename):
"""
Parse the given .bf file into the gccjit.Context, containing a
single "main" function suitable for compiling into an executable.
"""
self.filename = filename;
self.line = 1
self.column = 0
with open(filename) as f_in:
for ch in f_in.read():
self.compile_char(ch)
self.curblock.end_with_void_return()
# Compiling to an executable
def compile_to_file(self, output_path):
# Wrap "func" up in a "main" function
mainfunc, argv, argv = gccjit.make_main(self.ctxt)
block = mainfunc.new_block()
block.add_eval(self.ctxt.new_call(self.func, []))
block.end_with_return(self.int_zero)
self.ctxt.compile_to_file(gccjit.OutputKind.EXECUTABLE,
output_path)
# Running the generated code in-process
def run(self):
import ctypes
result = self.ctxt.compile()
py_func_type = ctypes.CFUNCTYPE(None)
py_func = py_func_type(result.get_code(b'func'))
py_func()
# Entrypoint
def main(argv):
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-o", "--output", dest="outputfile",
help="compile to FILE", metavar="FILE")
(options, args) = parser.parse_args()
if len(args) != 1:
raise ValueError('No input file')
inputfile = args[0]
c = Compiler()
c.parse_into_ctxt(inputfile)
if options.outputfile:
c.compile_to_file(options.outputfile)
else:
c.run()
if __name__ == '__main__':
try:
main(sys.argv)
except Exception as exc:
print(exc)
sys.exit(1)
|