#!/usr/bin/python# This creates a CSV file from the output of the debug output of subtarget:# llvm-tblgen --gen-subtarget --debug-only=subtarget-emitter# With thanks to Dave Estes for mentioning the idea at 2014 LLVM Developers' Meetingimportos;importsys;importre;importoperator;table={}models=set()filt=Nonedefadd(instr,model,resource=None):globaltable,modelsentry=table.setdefault(instr,dict())entry[model]=resourcemodels.add(model)deffilter_model(m):globalfiltifmandfilt:returnfilt.search(m)!=Noneelse:returnTruedefdisplay():globaltable,models# remove default and itinerary so we can control their sort order to make# them firstmodels.discard("default")models.discard("itinerary")ordered_table=sorted(table.items(),key=operator.itemgetter(0))ordered_models=["itinerary","default"]ordered_models.extend(sorted(models))ordered_models=filter(filter_model,ordered_models)# print headersys.stdout.write("instruction")formodelinordered_models:sys.stdout.write(", {}".format(model))sys.stdout.write(os.linesep)for(instr,mapping)inordered_table:sys.stdout.write(instr)formodelinordered_models:ifmodelinmappingandmapping[model]isnotNone:sys.stdout.write(", {}".format(mapping[model]))else:sys.stdout.write(", ")sys.stdout.write(os.linesep)defmachineModelCover(path):# The interesting bitsre_sched_default=re.compile("SchedRW machine model for ([^ ]*) (.*)\n");re_sched_no_default=re.compile("No machine model for ([^ ]*)\n");re_sched_spec=re.compile("InstRW on ([^ ]*) for ([^ ]*) (.*)\n");re_sched_no_spec=re.compile("No machine model for ([^ ]*) on processor (.*)\n");re_sched_itin=re.compile("Itinerary for ([^ ]*): ([^ ]*)\n")# scan the filewithopen(path,'r')asf:forlineinf.readlines():match=re_sched_default.match(line)ifmatch:add(match.group(1),"default",match.group(2))match=re_sched_no_default.match(line)ifmatch:add(match.group(1),"default")match=re_sched_spec.match(line)ifmatch:add(match.group(2),match.group(1),match.group(3))match=re_sched_no_spec.match(line)ifmatch:add(match.group(1),match.group(2))match=re_sched_itin.match(line)ifmatch:add(match.group(1),"itinerary",match.group(2))display()iflen(sys.argv)>2:filt=re.compile(sys.argv[2],re.IGNORECASE)machineModelCover(sys.argv[1])