#!/usr/bin/python import math import sys import tokenize match = {} mask = {} args = {} args['ra'] = (24,20) args['rb'] = (19,15) args['rc'] = (4,0) args['rd'] = (9,5) args['imm27'] = (26,0) args['imm20'] = (19,0) args['imm'] = (11,0) args['shamt'] = (10,5) args['shamtw'] = (9,5) def binary(n, digits=0): rep = bin(n)[2:] return rep if digits == 0 else ('0' * (digits - len(rep))) + rep def make_disasm_table(match,mask): print '/* Automatically generated by parse-opcodes */' for name,match in match.iteritems(): name2 = name.upper().replace('.','_') print '#define MATCH_%s %s' % (name2, hex(match)) print '#define MASK_%s %s' % (name2, hex(mask[name])) def make_switch(match,mask): opcode_base = 25 opcode_size = 7 funct_base = 12 funct_size = 3 opcode_mask = ((1<<(opcode_base+opcode_size))-(1<> 0x%x) & 0x%x)' % (opcode_base,(1<> 0x%x) & 0x%x)' % (funct_base,(1<= 2 name = tokens[0] mymatch = 0 mymask = 0 cover = 0 for token in tokens[1:len(tokens)]: if len(token.split('=')) == 2: tmp = token.split('=') val = int(tmp[1],0) if len(tmp[0].split('..')) == 2: tmp = tmp[0].split('..') hi = int(tmp[0]) lo = int(tmp[1]) if hi <= lo: sys.exit("%s: bad range %d..%d" % (name,hi,lo)) else: hi = lo = int(tmp[0]) if val >= (1 << (hi-lo+1)): sys.exit("%s: bad value %d for range %d..%d" % (name,val,hi,lo)) mymatch = mymatch | (val << lo) mymask = mymask | ((1<<(hi+1))-(1<