From abef3a711daba7936f5b812625bb08a9e5399885 Mon Sep 17 00:00:00 2001 From: Brian Gladman Date: Sat, 5 Dec 2009 16:13:05 +0000 Subject: [PATCH] Update gen_x86_insn.py to work in both Python 2 and 3 svn path=/trunk/yasm/; revision=2240 --- Mkfiles/vc9/modules/modules.vcproj | 4 +++ modules/arch/x86/gen_x86_insn.py | 58 +++++++++++++++++------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Mkfiles/vc9/modules/modules.vcproj b/Mkfiles/vc9/modules/modules.vcproj index 62db9028..77d9ab49 100644 --- a/Mkfiles/vc9/modules/modules.vcproj +++ b/Mkfiles/vc9/modules/modules.vcproj @@ -327,6 +327,10 @@ Name="Source Files" Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" > + + diff --git a/modules/arch/x86/gen_x86_insn.py b/modules/arch/x86/gen_x86_insn.py index 50efbe99..b7d6428a 100755 --- a/modules/arch/x86/gen_x86_insn.py +++ b/modules/arch/x86/gen_x86_insn.py @@ -25,6 +25,9 @@ # POSSIBILITY OF SUCH DAMAGE. # # NOTE: operands are arranged in NASM / Intel order (e.g. dest, src) + +from sys import stdout, version_info + rcstag = "$Id$" try: scriptname = rcstag.split()[1] @@ -56,6 +59,9 @@ XOPL0 = 0x80 XOPL1 = 0x84 XOPpp = 0x80 # OR with value +def lprint(s, f = stdout, e = '\n') : + f.write(s + e) + def cpu_lcd(cpu1, cpu2): """Find the lowest common denominator of two CPU sets.""" retval = set() @@ -112,7 +118,7 @@ class Operand(object): if kwargs: for arg in kwargs: - print "Warning: unrecognized arg %s" % arg + lprint("Warning: unrecognized arg %s" % arg) def __str__(self): return "{"+ ", ".join(["OPT_%s" % self.type, @@ -303,7 +309,7 @@ class GroupForm(object): if kwargs: for arg in kwargs: - print "Warning: unrecognized arg %s" % arg + lprint("Warning: unrecognized arg %s" % arg) def __str__(self): if hasattr(self, "opcode"): @@ -528,8 +534,8 @@ def add_prefix(name, groupname, value, parser=None, **kwargs): def finalize_insns(): unused_groups = set(groups.keys()) - for name, opts in insns.iteritems(): - for insn in opts: + for name in insns: + for insn in insns[name]: group = groups[insn.groupname] unused_groups.discard(insn.groupname) @@ -574,12 +580,12 @@ def finalize_insns(): unused_groups.discard("empty") unused_groups.discard("not64") if unused_groups: - print "warning: unused groups: %s" % ", ".join(unused_groups) + lprint("warning: unused groups: %s" % ", ".join(unused_groups)) def output_insns(f, parser, insns): - print >>f, "/* Generated by %s r%s, do not edit */" % \ - (scriptname, scriptrev) - print >>f, """%%ignore-case + lprint("/* Generated by %s r%s, do not edit */" % \ + (scriptname, scriptrev), f) + lprint("""%%ignore-case %%language=ANSI-C %%compare-strncmp %%readonly-tables @@ -588,9 +594,9 @@ def output_insns(f, parser, insns): %%define hash-function-name insnprefix_%s_hash %%define lookup-function-name insnprefix_%s_find struct insnprefix_parse_data; -%%%%""" % (parser, parser) +%%%%""" % (parser, parser), f) for keyword in sorted(insns): - print >>f, "%s,\t%s" % (keyword.lower(), insns[keyword]) + lprint("%s,\t%s" % (keyword.lower(), insns[keyword]), f) def output_gas_insns(f): output_insns(f, "gas", gas_insns) @@ -602,10 +608,11 @@ def output_groups(f): # Merge all operand lists into single list # Sort by number of operands to shorten output all_operands = [] - for form in sorted((form for g in groups.itervalues() for form in g), + gi = groups.itervalues() if version_info[0] == 2 else groups.values() + for form in sorted((form for g in gi for form in g), key=lambda x:len(x.operands), reverse=True): num_operands = len(form.operands) - for i in xrange(len(all_operands)): + for i in range(len(all_operands)): if all_operands[i:i+num_operands] == form.operands: form.all_operands_index = i break @@ -614,12 +621,12 @@ def output_groups(f): all_operands.extend(form.operands) # Output operands list - print >>f, "/* Generated by %s r%s, do not edit */" % \ - (scriptname, scriptrev) - print >>f, "static const x86_info_operand insn_operands[] = {" - print >>f, " ", - print >>f, ",\n ".join(str(x) for x in all_operands) - print >>f, "};\n" + lprint("/* Generated by %s r%s, do not edit */" % \ + (scriptname, scriptrev), f) + lprint("static const x86_info_operand insn_operands[] = {", f) + lprint(" ", f, '') + lprint(",\n ".join(str(x) for x in all_operands), f) + lprint("};\n", f) # Output groups seen = set() @@ -627,10 +634,10 @@ def output_groups(f): if name in seen: continue seen.add(name) - print >>f, "static const x86_insn_info %s_insn[] = {" % name - print >>f, " ", - print >>f, ",\n ".join(str(x) for x in groups[name]) - print >>f, "};\n" + lprint("static const x86_insn_info %s_insn[] = {" % name, f) + lprint(" ", f, '') + lprint(",\n ".join(str(x) for x in groups[name]), f) + lprint("};\n", f) ##################################################################### # General instruction groupings @@ -7518,6 +7525,7 @@ for val, suf in enumerate(["", "z", "y", "yz", "x", "xz", "xy", "xyz"]): # Output generation ##################################################################### -output_groups(file("x86insns.c", "wt")) -output_gas_insns(file("x86insn_gas.gperf", "wt")) -output_nasm_insns(file("x86insn_nasm.gperf", "wt")) +output_groups(open("x86insns.c", "wt")) +output_gas_insns(open("x86insn_gas.gperf", "wt")) +output_nasm_insns(open("x86insn_nasm.gperf", "wt")) + -- 2.40.0