generate.py

来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· Python 代码 · 共 531 行 · 第 1/2 页

PY
531
字号
            print >>out, '#include "params/%s.hh"' % obj        print >>out, '%}'        for pd in predecls:            print >>out, pd        enums = list(enums)        enums.sort()        for enum in enums:            print >>out, '%%include "enums/%s.hh"' % enum.__name__        print >>out        for obj in ordered_objs:            if obj.swig_objdecls:                for decl in obj.swig_objdecls:                    print >>out, decl                continue            code = ''            base = obj.get_base()            code += '// stop swig from creating/wrapping default ctor/dtor\n'            code += '%%nodefault %s;\n' % obj.cxx_class            code += 'class %s ' % obj.cxx_class            if base:                code += ': public %s' % base            code += ' {};\n'            klass = obj.cxx_class;            if hasattr(obj, 'cxx_namespace'):                new_code = 'namespace %s {\n' % obj.cxx_namespace                new_code += code                new_code += '}\n'                code = new_code                klass = '%s::%s' % (obj.cxx_namespace, klass)            print >>out, code        for obj in ordered_objs:            print >>out, '%%include "params/%s.hh"' % obj    def makeSwigInit(self, target, source, env):        f = file(str(target[0]), 'w')        print >>f, 'extern "C" {'        for module in source:            print >>f, '    void init_%s();' % module.get_contents()        print >>f, '}'        print >>f, 'void init_swig() {'        for module in source:            print >>f, '    init_%s();' % module.get_contents()        print >>f, '}'        f.close()    def compilePyFile(self, target, source, env):        '''Action function to compile a .py into a .pyc'''        py_compile.compile(str(source[0]), str(target[0]))    def buildPyZip(self, target, source, env):        '''Action function to build the zip archive.  Uses the        PyZipFile module included in the standard Python library.'''        py_compiled = {}        for s in self.py_sources:            compname = str(s.compiled)            assert compname not in py_compiled            py_compiled[compname] = s        zf = zipfile.ZipFile(str(target[0]), 'w')        for s in source:            zipname = str(s)            arcname = py_compiled[zipname].arcname            zf.write(zipname, arcname)        zf.close()    def traceFlagsPy(self, target, source, env):        assert(len(target) == 1)        f = file(str(target[0]), 'w')        allFlags = []        for s in source:            val = eval(s.get_contents())            allFlags.append(val)        print >>f, 'baseFlags = ['        for flag, compound, desc in allFlags:            if not compound:                print >>f, "    '%s'," % flag        print >>f, "    ]"        print >>f        print >>f, 'compoundFlags = ['        print >>f, "    'All',"        for flag, compound, desc in allFlags:            if compound:                print >>f, "    '%s'," % flag        print >>f, "    ]"        print >>f        print >>f, "allFlags = frozenset(baseFlags + compoundFlags)"        print >>f        print >>f, 'compoundFlagMap = {'        all = tuple([flag for flag,compound,desc in allFlags if not compound])        print >>f, "    'All' : %s," % (all, )        for flag, compound, desc in allFlags:            if compound:                print >>f, "    '%s' : %s," % (flag, compound)        print >>f, "    }"        print >>f        print >>f, 'flagDescriptions = {'        print >>f, "    'All' : 'All flags',"        for flag, compound, desc in allFlags:            print >>f, "    '%s' : '%s'," % (flag, desc)        print >>f, "    }"        f.close()    def traceFlagsCC(self, target, source, env):        assert(len(target) == 1)        f = file(str(target[0]), 'w')        allFlags = []        for s in source:            val = eval(s.get_contents())            allFlags.append(val)        # file header        print >>f, '''/* * DO NOT EDIT THIS FILE! Automatically generated */#include "base/traceflags.hh"using namespace Trace;const char *Trace::flagStrings[] ={'''        # The string array is used by SimpleEnumParam to map the strings        # provided by the user to enum values.        for flag, compound, desc in allFlags:            if not compound:                print >>f, '    "%s",' % flag        print >>f, '    "All",'        for flag, compound, desc in allFlags:            if compound:                print >>f, '    "%s",' % flag        print >>f, '};'        print >>f        print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)        print >>f        #        # Now define the individual compound flag arrays.  There is an array        # for each compound flag listing the component base flags.        #        all = tuple([flag for flag,compound,desc in allFlags if not compound])        print >>f, 'static const Flags AllMap[] = {'        for flag, compound, desc in allFlags:            if not compound:                print >>f, "    %s," % flag        print >>f, '};'        print >>f        for flag, compound, desc in allFlags:            if not compound:                continue            print >>f, 'static const Flags %sMap[] = {' % flag            for flag in compound:                print >>f, "    %s," % flag            print >>f, "    (Flags)-1"            print >>f, '};'            print >>f        #        # Finally the compoundFlags[] array maps the compound flags        # to their individual arrays/        #        print >>f, 'const Flags *Trace::compoundFlags[] ='        print >>f, '{'        print >>f, '    AllMap,'        for flag, compound, desc in allFlags:            if compound:                print >>f, '    %sMap,' % flag        # file trailer        print >>f, '};'        f.close()    def traceFlagsHH(self, target, source, env):        assert(len(target) == 1)        f = file(str(target[0]), 'w')        allFlags = []        for s in source:            val = eval(s.get_contents())            allFlags.append(val)        # file header boilerplate        print >>f, '''/* * DO NOT EDIT THIS FILE! * * Automatically generated from traceflags.py */#ifndef __BASE_TRACE_FLAGS_HH__#define __BASE_TRACE_FLAGS_HH__namespace Trace {enum Flags {'''        # Generate the enum.  Base flags come first, then compound flags.        idx = 0        for flag, compound, desc in allFlags:            if not compound:                print >>f, '    %s = %d,' % (flag, idx)                idx += 1        numBaseFlags = idx        print >>f, '    NumFlags = %d,' % idx        # put a comment in here to separate base from compound flags        print >>f, '''// The remaining enum values are *not* valid indices for Trace::flags.// They are "compound" flags, which correspond to sets of base// flags, and are used by changeFlag.'''        print >>f, '    All = %d,' % idx        idx += 1        for flag, compound, desc in allFlags:            if compound:                print >>f, '    %s = %d,' % (flag, idx)                idx += 1        numCompoundFlags = idx - numBaseFlags        print >>f, '    NumCompoundFlags = %d' % numCompoundFlags        # trailer boilerplate        print >>f, '''\}; // enum Flags// Array of strings for SimpleEnumParamextern const char *flagStrings[];extern const int numFlagStrings;// Array of arraay pointers: for each compound flag, gives the list of// base flags to set.  Inidividual flag arrays are terminated by -1.extern const Flags *compoundFlags[];/* namespace Trace */ }#endif // __BASE_TRACE_FLAGS_HH__'''        f.close()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?