1*9880d681SAndroid Build Coastguard Worker#!/usr/bin/python 2*9880d681SAndroid Build Coastguard Worker 3*9880d681SAndroid Build Coastguard Worker# This creates a CSV file from the output of the debug output of subtarget: 4*9880d681SAndroid Build Coastguard Worker# llvm-tblgen --gen-subtarget --debug-only=subtarget-emitter 5*9880d681SAndroid Build Coastguard Worker# With thanks to Dave Estes for mentioning the idea at 2014 LLVM Developers' Meeting 6*9880d681SAndroid Build Coastguard Worker 7*9880d681SAndroid Build Coastguard Workerimport os; 8*9880d681SAndroid Build Coastguard Workerimport sys; 9*9880d681SAndroid Build Coastguard Workerimport re; 10*9880d681SAndroid Build Coastguard Workerimport operator; 11*9880d681SAndroid Build Coastguard Worker 12*9880d681SAndroid Build Coastguard Workertable = {} 13*9880d681SAndroid Build Coastguard Workermodels = set() 14*9880d681SAndroid Build Coastguard Workerfilt = None 15*9880d681SAndroid Build Coastguard Worker 16*9880d681SAndroid Build Coastguard Workerdef add(instr, model, resource=None): 17*9880d681SAndroid Build Coastguard Worker global table, models 18*9880d681SAndroid Build Coastguard Worker 19*9880d681SAndroid Build Coastguard Worker entry = table.setdefault(instr, dict()) 20*9880d681SAndroid Build Coastguard Worker entry[model] = resource 21*9880d681SAndroid Build Coastguard Worker models.add(model) 22*9880d681SAndroid Build Coastguard Worker 23*9880d681SAndroid Build Coastguard Workerdef filter_model(m): 24*9880d681SAndroid Build Coastguard Worker global filt 25*9880d681SAndroid Build Coastguard Worker if m and filt: 26*9880d681SAndroid Build Coastguard Worker return filt.search(m) != None 27*9880d681SAndroid Build Coastguard Worker else: 28*9880d681SAndroid Build Coastguard Worker return True 29*9880d681SAndroid Build Coastguard Worker 30*9880d681SAndroid Build Coastguard Worker 31*9880d681SAndroid Build Coastguard Workerdef display(): 32*9880d681SAndroid Build Coastguard Worker global table, models 33*9880d681SAndroid Build Coastguard Worker 34*9880d681SAndroid Build Coastguard Worker ordered_table = sorted(table.items(), key=operator.itemgetter(0)) 35*9880d681SAndroid Build Coastguard Worker ordered_models = filter(filter_model, sorted(models)) 36*9880d681SAndroid Build Coastguard Worker 37*9880d681SAndroid Build Coastguard Worker # print header 38*9880d681SAndroid Build Coastguard Worker sys.stdout.write("instruction") 39*9880d681SAndroid Build Coastguard Worker for model in ordered_models: 40*9880d681SAndroid Build Coastguard Worker if not model: model = "default" 41*9880d681SAndroid Build Coastguard Worker sys.stdout.write(", {}".format(model)) 42*9880d681SAndroid Build Coastguard Worker sys.stdout.write(os.linesep) 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard Worker for (instr, mapping) in ordered_table: 45*9880d681SAndroid Build Coastguard Worker sys.stdout.write(instr) 46*9880d681SAndroid Build Coastguard Worker for model in ordered_models: 47*9880d681SAndroid Build Coastguard Worker if model in mapping: 48*9880d681SAndroid Build Coastguard Worker sys.stdout.write(", {}".format(mapping[model])) 49*9880d681SAndroid Build Coastguard Worker else: 50*9880d681SAndroid Build Coastguard Worker sys.stdout.write(", ") 51*9880d681SAndroid Build Coastguard Worker sys.stdout.write(os.linesep) 52*9880d681SAndroid Build Coastguard Worker 53*9880d681SAndroid Build Coastguard Worker 54*9880d681SAndroid Build Coastguard Workerdef machineModelCover(path): 55*9880d681SAndroid Build Coastguard Worker # The interesting bits 56*9880d681SAndroid Build Coastguard Worker re_sched_default = re.compile("SchedRW machine model for ([^ ]*) (.*)\n"); 57*9880d681SAndroid Build Coastguard Worker re_sched_no_default = re.compile("No machine model for ([^ ]*)\n"); 58*9880d681SAndroid Build Coastguard Worker re_sched_spec = re.compile("InstRW on ([^ ]*) for ([^ ]*) (.*)\n"); 59*9880d681SAndroid Build Coastguard Worker re_sched_no_spec = re.compile("No machine model for ([^ ]*) on processor (.*)\n"); 60*9880d681SAndroid Build Coastguard Worker 61*9880d681SAndroid Build Coastguard Worker # scan the file 62*9880d681SAndroid Build Coastguard Worker with open(path, 'r') as f: 63*9880d681SAndroid Build Coastguard Worker for line in f.readlines(): 64*9880d681SAndroid Build Coastguard Worker match = re_sched_default.match(line) 65*9880d681SAndroid Build Coastguard Worker if match: add(match.group(1), None, match.group(2)) 66*9880d681SAndroid Build Coastguard Worker match = re_sched_no_default.match(line) 67*9880d681SAndroid Build Coastguard Worker if match: add(match.group(1), None) 68*9880d681SAndroid Build Coastguard Worker match = re_sched_spec.match(line) 69*9880d681SAndroid Build Coastguard Worker if match: add(match.group(2), match.group(1), match.group(3)) 70*9880d681SAndroid Build Coastguard Worker match = re_sched_no_default.match(line) 71*9880d681SAndroid Build Coastguard Worker if match: add(match.group(1), None) 72*9880d681SAndroid Build Coastguard Worker 73*9880d681SAndroid Build Coastguard Worker display() 74*9880d681SAndroid Build Coastguard Worker 75*9880d681SAndroid Build Coastguard Workerif len(sys.argv) > 2: 76*9880d681SAndroid Build Coastguard Worker filt = re.compile(sys.argv[2], re.IGNORECASE) 77*9880d681SAndroid Build Coastguard WorkermachineModelCover(sys.argv[1]) 78