from openpower.decoder.pseudo.parser import GardenSnakeCompiler
from openpower.decoder.selectable_int import SelectableInt, selectconcat
from openpower.decoder.isa.caller import GPR, Mem
+from openpower.util import log
####### Test code #######
def convert_to_python(pcode, form, incl_carry, helper=False, filename="string"):
-
- print("form", form)
+ log("form", form)
gsc = GardenSnakeCompiler(form=form, incl_carry=incl_carry, helper=helper)
tree = gsc.compile(pcode, mode="exec", filename=filename)
tree = _compile(code, mode="single", filename="string")
tree = ast.fix_missing_locations(tree)
- print(ast.dump(tree))
+ log(ast.dump(tree))
- print("astor dump")
- print(astor.dump_tree(tree))
- print("to source")
+ log("astor dump")
+ log(astor.dump_tree(tree))
+ log("to source")
source = astor.to_source(tree)
- print(source)
+ log(source)
# sys.exit(0)
# Set up the GardenSnake run-time environment
def print_(*args):
- print("args", args)
- print("-->", " ".join(map(str, args)))
+ log("args", args)
+ log("-->", " ".join(map(str, args)))
from openpower.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
trunc_div, trunc_rem)
def process():
for ins in instr:
- print("0x{:X}".format(ins & 0xffffffff))
+ log("0x{:X}".format(ins & 0xffffffff))
# ask the decoder to decode this binary data (endian'd)
yield decode.bigendian.eq(0) # little / big?
# uninitialised regs, drop them into dict for function
for rname in gsc.parser.uninit_regs:
d[rname] = SelectableInt(0, 64) # uninitialised (to zero)
- print("uninitialised", rname, hex(d[rname].value))
+ log("uninitialised", rname, hex(d[rname].value))
# read regs, drop them into dict for function
for rname in gsc.parser.read_regs:
regidx = yield getattr(decode.sigforms['X'], rname)
d[rname] = gsc.gpr[regidx] # contents of regfile
d["_%s" % rname] = regidx # actual register value
- print("read reg", rname, regidx, hex(d[rname].value))
+ log("read reg", rname, regidx, hex(d[rname].value))
exec(compiled_code, d) # code gets executed here in dict "d"
- print("Done")
+ log("Done")
- print(d.keys()) # shows the variables that may have been created
+ log(d.keys()) # shows the variables that may have been created
- print(decode.sigforms['X'])
+ log(decode.sigforms['X'])
x = yield decode.sigforms['X'].RS
ra = yield decode.sigforms['X'].RA
rb = yield decode.sigforms['X'].RB
- print("RA", ra, d['RA'])
- print("RB", rb, d['RB'])
- print("RS", x)
+ log("RA", ra, d['RA'])
+ log("RB", rb, d['RB'])
+ log("RS", x)
for wname in gsc.parser.write_regs:
reg = getform[wname]
regidx = yield reg
- print("write regs", regidx, wname, d[wname], reg)
+ log("write regs", regidx, wname, d[wname], reg)
gsc.gpr[regidx] = d[wname]
sim.add_process(process)
for j in range(16):
hexstr.append("%02x" % gsc.mem.mem[i+j])
hexstr = ' '.join(hexstr)
- print("mem %4x" % i, hexstr)
+ log("mem %4x" % i, hexstr)
if __name__ == '__main__':
from copy import copy
import os
+from openpower.util import log
+
def get_isafn_dir():
fdir = os.path.abspath(os.path.dirname(__file__))
fdir = os.path.split(fdir)[0]
fdir = os.path.split(fdir)[0]
fdir = os.path.split(fdir)[0]
- print (fdir)
+ log(fdir)
return os.path.join(fdir, "openpower", "isafunctions")
def __init__(self):
self.fns = OrderedDict()
for pth in os.listdir(os.path.join(get_isafn_dir())):
- print("examining", get_isafn_dir(), pth)
+ log("examining", get_isafn_dir(), pth)
if "swp" in pth:
continue
if not pth.endswith(".mdwn"):
- print ("warning, file not .mdwn, skipping", pth)
+ log ("warning, file not .mdwn, skipping", pth)
continue
self.read_file(pth)
l = lines.pop(0).rstrip() # get first line
prefix_lines = 0
while lines:
- print(l)
+ log(l)
# look for HTML comment, if starting, skip line.
# XXX this is braindead! it doesn't look for the end
# so please put ending of comments on one line:
# <!-- line 1 comment -->
# <!-- line 2 comment -->
if l.strip().startswith('<!--'):
- # print ("skipping comment", l)
+ # log ("skipping comment", l)
l = lines.pop(0).rstrip() # get next line
prefix_lines += 1
continue
while True:
l = lines.pop(0).rstrip()
prefix_lines += 1
- print ("examining", repr(l))
+ log ("examining", repr(l))
if l.startswith(" "):
break
if l.startswith('<!--'):
li += [l[4:]] # first line detected with 4-space
while lines:
l = lines.pop(0).rstrip()
- print ("examining", repr(l))
+ log ("examining", repr(l))
if len(l) == 0:
li.append(l)
continue
def pprint(self):
for k, v in self.fns.items():
- print("# %s %s" % (k, v['desc']))
- print(v['pcode'])
- print()
+ log("# %s %s" % (k, v['desc']))
+ log(v['pcode'])
+ log()
if __name__ == '__main__':
from copy import copy
from ply import lex
from openpower.decoder.selectable_int import SelectableInt
+from openpower.util import log
class SyntaxError2(Exception):
implied_colon_needed = False
for token in tokens:
- #print ("track colon token", token, token.type)
+ #log ("track colon token", token, token.type)
if token.type == 'THEN':
# turn then into colon
indent = NO_INDENT
saw_colon = False
for token in tokens:
- #print ("track token", token, token.type)
+ #log ("track token", token, token.type)
token.at_line_start = at_line_start
if token.type == "COLON":
res.append('')
continue
if nwhite.startswith("case") or nwhite.startswith("default"):
- #print ("case/default", nwhite, spc_count, prev_spc_count)
+ #log ("case/default", nwhite, spc_count, prev_spc_count)
if (prev_spc_count is not None and
prev_spc_count == spc_count and
(res[-1].endswith(":") or res[-1].endswith(": fallthrough"))):
res[-1] += " fallthrough" # add to previous line
prev_spc_count = spc_count
else:
- #print ("notstarts", spc_count, nwhite)
+ #log ("notstarts", spc_count, nwhite)
prev_spc_count = None
res.append(l)
return '\n'.join(res)
prev_was_ws = False
for token in tokens:
if 0:
- print("Process", depth, token.indent, token,)
+ log("Process", depth, token.indent, token,)
if token.at_line_start:
- print("at_line_start",)
+ log("at_line_start",)
if token.must_indent:
- print("must_indent",)
- print
+ log("must_indent",)
# WS only occurs at the start of the line
# There may be WS followed by NEWLINE so
def t_STRING(self, t):
r"'([^\\']+|\\'|\\\\)*'" # I think this is right ...
- print(repr(t.value))
+ log(repr(t.value))
t.value = t.value[1:-1]
return t
raise_syntax_error("Unknown symbol %r" % (t.value[0],),
self.filename, t.lexer.lineno,
t.lexer.lexpos, t.lexer.lexdata)
- print("Skipping", repr(t.value[0]))
+ log("Skipping", repr(t.value[0]))
t.lexer.skip(1)
def input(self, s, add_endmarker=True):
s = annoying_case_hack_filter(s)
if self.debug:
- print(s)
+ log(s)
s += "\n"
self.lexer.paren_count = 0
self.lexer.brack_count = 0
# quick test/demo
#code = cnttzd
code = switchtest
- print(code)
+ log(code)
lexer = IndentLexer(debug=1)
# Give the lexer some input
- print("code")
- print(code)
+ log("code")
+ log(code)
lexer.input(code)
tokens = iter(lexer.token, None)
for token in tokens:
- print(token)
+ log(token)
from openpower.util import log
from openpower.decoder.orderedset import OrderedSet
+from openpower.util import log
+
from collections import namedtuple, OrderedDict
from copy import copy
import os
fdir = os.path.split(fdir)[0]
fdir = os.path.split(fdir)[0]
fdir = os.path.split(fdir)[0]
- # print (fdir)
+ # log (fdir)
return os.path.join(fdir, "openpower", "isa")
self.verbose = False
for pth in os.listdir(os.path.join(get_isa_dir())):
if self.verbose:
- print("examining", get_isa_dir(), pth)
+ log("examining", get_isa_dir(), pth)
if "swp" in pth:
continue
if not pth.endswith(".mdwn"):
rewrite.append(l)
while lines:
if self.verbose:
- print(l)
+ log(l)
# look for HTML comment, if starting, skip line.
# XXX this is braindead! it doesn't look for the end
# so please put ending of comments on one line:
# <!-- line 1 comment -->
# {some whitespace}<!-- line 2 comment -->
if l.strip().startswith('<!--'):
- # print ("skipping comment", l)
+ # log ("skipping comment", l)
l = lines.pop(0).rstrip() # get first line
continue
# whitespace expected
l = lines.pop(0).strip()
if self.verbose:
- print(repr(l))
+ log(repr(l))
assert len(l) == 0, ("blank line not found %s" % l)
rewrite.append(l)
while True:
l = lines.pop(0).rstrip()
if l.strip().startswith('<!--'):
- # print ("skipping comment", l)
+ # log ("skipping comment", l)
l = lines.pop(0).rstrip() # get first line
continue
rewrite.append(l)
l = lines.pop(0).rstrip() # get first line
while lines:
if self.verbose:
- print(l)
+ log(l)
# look for HTML comment, if starting, skip line.
# XXX this is braindead! it doesn't look for the end
# so please put ending of comments on one line:
# <!-- line 1 comment -->
# <!-- line 2 comment -->
if l.strip().startswith('<!--'):
- # print ("skipping comment", l)
+ # log ("skipping comment", l)
l = lines.pop(0).rstrip() # get next line
continue
# whitespace expected
l = lines.pop(0).strip()
if self.verbose:
- print(repr(l))
+ log(repr(l))
assert len(l) == 0, ("blank line not found %s" % l)
# Form expected
# whitespace expected
l = lines.pop(0).strip()
if self.verbose:
- print(repr(l))
+ log(repr(l))
assert len(l) == 0, ("blank line not found %s" % l)
extra_uninit_regs = OrderedSet()
pl = self.page.get(page, [])
self.page[page] = pl + [opcode]
- def pprint_ops(self):
+ def plog_ops(self):
for k, v in self.instr.items():
- print("# %s %s" % (v.opcode, v.desc))
- print("Form: %s Regs: %s" % (v.form, v.regs))
- print('\n'.join(map(lambda x: " %s" % x, v.pcode)))
- print("Specials")
- print('\n'.join(map(lambda x: " %s" % x, v.sregs)))
- print()
+ log("# %s %s" % (v.opcode, v.desc))
+ log("Form: %s Regs: %s" % (v.form, v.regs))
+ log('\n'.join(map(lambda x: " %s" % x, v.pcode)))
+ log("Specials")
+ log('\n'.join(map(lambda x: " %s" % x, v.sregs)))
+ log()
for k, v in isa.forms.items():
- print(k, v)
+ log(k, v)
if __name__ == '__main__':
isa = ISA()
- isa.pprint_ops()
+ isa.plog_ops()
# example on how to access cmp regs:
- print ("cmp regs:", isa.instr["cmp"].regs)
+ log ("cmp regs:", isa.instr["cmp"].regs)
IndentLexer, raise_syntax_error, SyntaxError2)
from openpower.decoder.orderedset import OrderedSet
from openpower.decoder.power_enums import BFP_FLAG_NAMES
+from openpower.util import log
# I use the Python AST
#from compiler import ast
def check_concat(node): # checks if the comparison is already a concat
- print("check concat", node)
+ log("check concat", node)
if not isinstance(node, ast.Call):
return [node]
node_func_id = getattr(node.func, "id", None)
- print("func", node_func_id)
+ log("func", node_func_id)
if node_func_id != 'concat':
return [node]
if node.keywords: # a repeated list-constant, don't optimise
[item] * something
these must specifically be returned as concat(item, repeat=something)
"""
- #print ("identify_sint_mul_pattern")
+ #log ("identify_sint_mul_pattern")
# for pat in p:
- # print(" ", astor.dump_tree(pat))
+ # log(" ", astor.dump_tree(pat))
if p[2] != '*': # multiply
return False
if (not isinstance(p[3], ast.Constant) and # rhs = Num
self.gprs = {}
if self.form is not None:
form = self.sd.sigforms[self.form]
- print(form)
+ log(form)
formkeys = form._asdict().keys()
else:
formkeys = []
def p_file_input_end(self, p):
"""file_input_end : file_input ENDMARKER"""
- print("end", p[1])
+ log("end", p[1])
p[0] = p[1]
def p_file_input(self, p):
# in-scope, for use to not overwrite them with auto-assign
self.fnparm_vars = set()
for arg in args:
- print("adding fn parm", arg)
+ log("adding fn parm", arg)
self.fnparm_vars.add(arg)
# varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] |
"""varargslist : varargslist COMMA NAME
| NAME"""
if len(p) == 4:
- print(p[1], p[3])
+ log(p[1], p[3])
p[0] = p[1] + [p[3]]
else:
p[0] = [p[1]]
"""expr_stmt : testlist ASSIGNEA testlist
| testlist ASSIGN testlist
| testlist """
- print("expr_stmt", p)
+ log("expr_stmt", p)
if len(p) == 2:
# a list of expressions
#p[0] = ast.Discard(p[1])
else:
iea_mode = p[2] == '<-iea'
if iea_mode:
- print ("iea_mode")
+ log ("iea_mode")
name = None
autoassign = False
if isinstance(p[1], ast.Name):
name = p[1].id
elif isinstance(p[1], ast.Subscript):
- print("assign subscript", p[1].value,
+ log("assign subscript", p[1].value,
self.declared_vars,
self.fnparm_vars,
self.special_regs)
- print(astor.dump_tree(p[1]))
- print(astor.dump_tree(p[3]))
+ log(astor.dump_tree(p[1]))
+ log(astor.dump_tree(p[3]))
if isinstance(p[1].value, ast.Name):
name = p[1].value.id
- print("assign subscript value to name", name)
+ log("assign subscript value to name", name)
if name in self.gprs:
# add to list of uninitialised
self.uninit_regs.add(name)
pass
elif isinstance(p[1], ast.Call) and p[1].func.id in \
['GPR', 'FPR', 'SPR']:
- print(astor.dump_tree(p[1]))
+ log(astor.dump_tree(p[1]))
# replace GPR(x) with GPR[x]
idx = p[1].args[0].id
if idx in regs + fregs:
if idx in self.gprs:
self.read_regs.add(idx) # add to list of regs to read
elif isinstance(p[1], ast.Call) and p[1].func.id == 'MEM':
- print("mem assign")
- print(astor.dump_tree(p[1]))
+ log("mem assign")
+ log(astor.dump_tree(p[1]))
p[1].func.id = "memassign" # change function name to set
p[1].args.append(p[3])
p[0] = p[1]
- print("mem rewrite")
- print(astor.dump_tree(p[0]))
+ log("mem rewrite")
+ log(astor.dump_tree(p[0]))
return
else:
- print(astor.dump_tree(p[1]))
+ log(astor.dump_tree(p[1]))
raise_syntax_error("help, help", self.filename,
p.slice[2].lineno, p.slice[2].lexpos,
self.input_text)
- print("expr assign", name, p[1], "to", p[3])
- print(astor.dump_tree(p[3]))
+ log("expr assign", name, p[1], "to", p[3])
+ log(astor.dump_tree(p[3]))
if isinstance(p[3], ast.Name):
toname = p[3].id
if toname in self.gprs:
"""switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT
"""
switchon = p[3]
- print("switch stmt")
- print(astor.dump_tree(p[1]))
+ log("switch stmt")
+ log(astor.dump_tree(p[1]))
cases = []
current_cases = [] # for deferral
for (case, suite) in p[8]:
- print("for", case, suite)
+ log("for", case, suite)
if suite is None:
for c in case:
current_cases.append(ast.Num(c))
break
for c in case:
current_cases.append(ast.Num(c))
- print("cases", current_cases)
+ log("cases", current_cases)
compare = ast.Compare(switchon, [ast.In()],
[ast.List(current_cases, ast.Load())])
current_cases = []
cases.append((compare, suite))
- print("ended", case, current_cases)
+ log("ended", case, current_cases)
if case == 'default':
if current_cases:
compare = ast.Compare(switchon, [ast.In()],
cases.reverse()
res = []
for compare, suite in cases:
- print("after rev", compare, suite)
+ log("after rev", compare, suite)
if compare is None:
assert len(res) == 0, "last case should be default"
res = suite
| comparison APPEND comparison
| power"""
if len(p) == 4:
- print(list(p))
+ log(list(p))
if p[2] == '<u':
p[0] = ast.Call(ast.Name("ltu", ast.Load()), (p[1], p[3]), [])
elif p[2] == '>u':
"""power : atom
| atom trailerlist"""
if len(p) == 2:
- print("power dump atom notrailer")
- print(astor.dump_tree(p[1]))
+ log("power dump atom notrailer")
+ log(astor.dump_tree(p[1]))
p[0] = p[1]
else:
- print("power dump atom")
- print(astor.dump_tree(p[1]))
- print("power dump trailerlist")
- print(astor.dump_tree(p[2]))
+ log("power dump atom")
+ log(astor.dump_tree(p[1]))
+ log("power dump trailerlist")
+ log(astor.dump_tree(p[2]))
p[0] = self.apply_trailer(p[1], p[2], self.read_regs)
if isinstance(p[1], ast.Name):
name = p[1].id
def p_atom_tuple(self, p):
"""atom : LPAR testlist RPAR"""
- print("tuple", p[2])
- print("astor dump")
- print(astor.dump_tree(p[2]))
+ log("tuple", p[2])
+ log("astor dump")
+ log(astor.dump_tree(p[2]))
if isinstance(p[2], ast.Name):
name = p[2].id
- print("tuple name", name)
+ log("tuple name", name)
if name in self.gprs:
self.read_regs.add(name) # add to list of regs to read
#p[0] = ast.Subscript(ast.Name("GPR", ast.Load()), ast.Str(p[2].id))
# *sigh* see class GPR. we need index itself not reg value
ridx = ast.Name("_%s" % rid, ast.Load())
p[0] = ast.Call(gprz, [ridx], [])
- print("tree", astor.dump_tree(p[0]))
+ log("tree", astor.dump_tree(p[0]))
else:
p[0] = p[2]
else:
p[0] = p[1]
def p_error(self, p):
- # print "Error!", repr(p)
+ # log("Error!", repr(p))
raise_syntax_error(str(p), self.filename, p.lineno, p.lexpos,
self.input_text)
def Assign(self, autoassign, assignname, left, right, iea_mode, eq_tok):
names = []
- print("Assign", autoassign, assignname, left, right)
+ log("Assign", autoassign, assignname, left, right)
if isinstance(left, ast.Name):
# Single assignment on left
# XXX when doing IntClass, which will have an "eq" function,
# dividend[0:32] = (RA)[0:32]
# the declaration makes the slice-assignment "work"
lower, upper, step = ls.lower, ls.upper, ls.step
- print("lower, upper, step", repr(lower), repr(upper), step)
+ log("lower, upper, step", repr(lower), repr(upper), step)
# XXX relax constraint that indices on auto-assign have
# to be constants x[0:32]
# if not isinstance(lower, ast.Constant) or \
ls = left.slice
if isinstance(ls, ast.Slice):
lower, upper, step = ls.lower, ls.upper, ls.step
- print("slice assign", lower, upper, step)
+ log("slice assign", lower, upper, step)
if step is None:
ls = (lower, upper, None)
else:
return ast.Call(ast.Name("selectassign", ast.Load()),
[left.value, ls, right], [])
else:
- print("Assign fail")
+ log("Assign fail")
raise_syntax_error("Can't do that yet", self.filename,
eq_tok.lineno, eq_tok.lexpos, self.input_text)
if attr is not None:
return ast.Attribute(atom, attr, ast.Load())
assert trailer[0] == "SUBS"
- print("subscript atom", trailer[1])
+ log("subscript atom", trailer[1])
#raise AssertionError("not implemented %s" % p[2][0])
subs = trailer[1]
if len(subs) == 1:
if isinstance(idx, ast.Name) and idx.id in regs + fregs:
read_regs.add(idx.id)
if isinstance(idx, ast.Name) and idx.id in regs:
- print("single atom subscript, underscored", idx.id)
+ log("single atom subscript, underscored", idx.id)
idx = ast.Name("_%s" % idx.id, ast.Load())
else:
idx = ast.Slice(subs[0], subs[1], None)
# if isinstance(atom, ast.Name) and atom.id == 'CR':
# atom.id = 'CR' # bad hack
- #print ("apply_trailer Subscript", atom.id, idx)
+ #log ("apply_trailer Subscript", atom.id, idx)
return ast.Subscript(atom, idx, ast.Load())
tree = self.parser.parse(code)
except SyntaxError2 as e:
e.raise_syntax_error()
- print("snake")
- pprint(tree)
+ log("snake")
+ # pprint(tree)
return tree
#misc.set_filename(filename, tree)
return compile(tree, mode="exec", filename="<string>")
from openpower.decoder.pseudo.functionreader import ISAFunctions, get_isafn_dir
from openpower.decoder.power_pseudo import (convert_to_pure_python,
check_in_gitignore)
-
+from openpower.util import log
def get_isafn_src_dir():
fdir = os.path.abspath(os.path.dirname(__file__))
f.write(header % function['desc']) # write out header
# go through all instructions
pcode = function['pcode']
- print(pcode)
+ log(pcode)
# check if the code has already been compiled
phash = hash(pcode)
if phash in sourcecache:
isa = PyISAFnWriter()
write_isa_class = True
if len(sys.argv) == 1: # quick way to do it
- print(dir(isa))
+ log(dir(isa))
sources = isa.fns.keys()
else:
sources = sys.argv[1:]
if sources[0] == "noall": # don't rewrite all.py
write_isa_class = False
sources.pop(0)
- print ("sources", write_isa_class, sources)
+ log("sources", write_isa_class, sources)
for source in sources:
isa.write_pysource(source)
if write_isa_class:
check_in_gitignore)
from openpower.decoder.orderedset import OrderedSet
from openpower.decoder.isa.caller import create_args, create_full_args
+from openpower.util import log
def get_isasrc_dir():
# go through all instructions
for page in instrs:
d = self.instr[page]
- print("page", pagename, page, fname, d.opcode)
+ log("page", pagename, page, fname, d.opcode)
pcode = '\n'.join(d.pcode) + '\n'
- print(pcode)
+ log(pcode)
incl_carry = pagename == 'fixedshift'
filename = os.path.join(get_isa_dir(), pagename + ".mdwn")
pycode, rused = convert_to_python(pcode, d.form, incl_carry,
isa = PyISAWriter()
write_isa_class = True
if len(sys.argv) == 1: # quick way to do it
- print(dir(isa))
+ log(dir(isa))
sources = isa.page.keys()
else:
sources = sys.argv[1:]
if sources[0] == "noall": # don't rewrite all.py
write_isa_class = False
sources.pop(0)
- print("sources", write_isa_class, sources)
+ log("sources", write_isa_class, sources)
isa.check_for_old_generated_files(sources)
for source in sources:
isa.write_pysource(source)