Deleted Added
sdiff udiff text old ( 7502:3ef7ff12c788 ) new ( 7673:b28bd1fa9a35 )
full compact
1# -*- mode:python -*-
2
3# Copyright (c) 2004-2005 The Regents of The University of Michigan
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright

--- 245 unchanged lines hidden (view full) ---

254 if 'SConscript' in files:
255 build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
256 SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
257
258for opt in export_vars:
259 env.ConfigFile(opt)
260
261def makeTheISA(source, target, env):
262 f = file(str(target[0]), 'w')
263
264 isas = [ src.get_contents() for src in source ]
265 target = env['TARGET_ISA']
266 def define(isa):
267 return isa.upper() + '_ISA'
268
269 def namespace(isa):
270 return isa[0].upper() + isa[1:].lower() + 'ISA'
271
272
273 print >>f, '#ifndef __CONFIG_THE_ISA_HH__'
274 print >>f, '#define __CONFIG_THE_ISA_HH__'
275 print >>f
276 for i,isa in enumerate(isas):
277 print >>f, '#define %s %d' % (define(isa), i + 1)
278 print >>f
279 print >>f, '#define THE_ISA %s' % (define(target))
280 print >>f, '#define TheISA %s' % (namespace(target))
281 print >>f
282 print >>f, '#endif // __CONFIG_THE_ISA_HH__'
283
284env.Command('config/the_isa.hh', map(Value, all_isa_list), makeTheISA)
285
286########################################################################
287#
288# Prevent any SimObjects from being added after this point, they
289# should all have been added in the SConscripts above
290#
291SimObject.fixed = True

--- 50 unchanged lines hidden (view full) ---

342 mod.__file__ = source.abspath
343
344 exec file(source.abspath, 'r') in mod.__dict__
345
346 return mod
347
348import m5.SimObject
349import m5.params
350
351m5.SimObject.clear()
352m5.params.clear()
353
354# install the python importer so we can grab stuff from the source
355# tree itself. We can't have SimObjects added after this point or
356# else we won't know about them for the rest of the stuff.
357importer = DictImporter(PySource.modules)

--- 39 unchanged lines hidden (view full) ---

397# Commands for the basic automatically generated python files
398#
399
400# Generate Python file containing a dict specifying the current
401# buildEnv flags.
402def makeDefinesPyFile(target, source, env):
403 build_env, hg_info = [ x.get_contents() for x in source ]
404
405 code = m5.util.code_formatter()
406 code("""
407import m5.internal
408import m5.util
409
410buildEnv = m5.util.SmartDict($build_env)
411hgRev = '$hg_info'
412
413compileDate = m5.internal.core.compileDate
414_globals = globals()
415for key,val in m5.internal.core.__dict__.iteritems():
416 if key.startswith('flag_'):
417 flag = key[5:]
418 _globals[flag] = val
419del _globals
420""")
421 code.write(str(target[0]))
422
423defines_info = [ Value(build_env), Value(env['HG_INFO']) ]
424# Generate a file with all of the compile options in it
425env.Command('python/m5/defines.py', defines_info, makeDefinesPyFile)
426PySource('m5', 'python/m5/defines.py')
427
428# Generate python file containing info about the M5 source code
429def makeInfoPyFile(target, source, env):
430 f = file(str(target[0]), 'w')
431 for src in source:
432 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
433 print >>f, "%s = %s" % (src, repr(data))
434 f.close()
435
436# Generate a file that wraps the basic top level files
437env.Command('python/m5/info.py',
438 [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
439 makeInfoPyFile)
440PySource('m5', 'python/m5/info.py')
441
442# Generate the __init__.py file for m5.objects
443def makeObjectsInitFile(target, source, env):
444 f = file(str(target[0]), 'w')
445 print >>f, 'from params import *'
446 print >>f, 'from m5.SimObject import *'
447 for module in source:
448 print >>f, 'from %s import *' % module.get_contents()
449 f.close()
450
451# Generate an __init__.py file for the objects package
452env.Command('python/m5/objects/__init__.py',
453 map(Value, SimObject.modnames),
454 makeObjectsInitFile)
455PySource('m5.objects', 'python/m5/objects/__init__.py')
456
457########################################################################
458#
459# Create all of the SimObject param headers and enum headers
460#
461
462def createSimObjectParam(target, source, env):
463 assert len(target) == 1 and len(source) == 1
464
465 hh_file = file(target[0].abspath, 'w')
466 name = str(source[0].get_contents())
467 obj = sim_objects[name]
468
469 print >>hh_file, obj.cxx_decl()
470 hh_file.close()
471
472def createSwigParam(target, source, env):
473 assert len(target) == 1 and len(source) == 1
474
475 i_file = file(target[0].abspath, 'w')
476 name = str(source[0].get_contents())
477 param = all_params[name]
478
479 for line in param.swig_decl():
480 print >>i_file, line
481 i_file.close()
482
483def createEnumStrings(target, source, env):
484 assert len(target) == 1 and len(source) == 1
485
486 cc_file = file(target[0].abspath, 'w')
487 name = str(source[0].get_contents())
488 obj = all_enums[name]
489
490 print >>cc_file, obj.cxx_def()
491 cc_file.close()
492
493def createEnumParam(target, source, env):
494 assert len(target) == 1 and len(source) == 1
495
496 hh_file = file(target[0].abspath, 'w')
497 name = str(source[0].get_contents())
498 obj = all_enums[name]
499
500 print >>hh_file, obj.cxx_decl()
501 hh_file.close()
502
503# Generate all of the SimObject param struct header files
504params_hh_files = []
505for name,simobj in sorted(sim_objects.iteritems()):
506 py_source = PySource.modules[simobj.__module__]
507 extra_deps = [ py_source.tnode ]
508
509 hh_file = File('params/%s.hh' % name)

--- 23 unchanged lines hidden (view full) ---

533 env.Command(hh_file, Value(name), createEnumParam)
534 env.Depends(hh_file, depends + extra_deps)
535
536# Build the big monolithic swigged params module (wraps all SimObject
537# param structs and enum structs)
538def buildParams(target, source, env):
539 names = [ s.get_contents() for s in source ]
540 objs = [ sim_objects[name] for name in names ]
541 out = file(target[0].abspath, 'w')
542
543 ordered_objs = []
544 obj_seen = set()
545 def order_obj(obj):
546 name = str(obj)
547 if name in obj_seen:
548 return
549
550 obj_seen.add(name)
551 if str(obj) != 'SimObject':
552 order_obj(obj.__bases__[0])
553
554 ordered_objs.append(obj)
555
556 for obj in objs:
557 order_obj(obj)
558
559 enums = set()
560 predecls = []
561 pd_seen = set()
562
563 def add_pds(*pds):
564 for pd in pds:
565 if pd not in pd_seen:
566 predecls.append(pd)
567 pd_seen.add(pd)
568
569 for obj in ordered_objs:
570 params = obj._params.local.values()
571 for param in params:
572 ptype = param.ptype
573 if issubclass(ptype, m5.params.Enum):
574 if ptype not in enums:
575 enums.add(ptype)
576 pds = param.swig_predecls()
577 if isinstance(pds, (list, tuple)):
578 add_pds(*pds)
579 else:
580 add_pds(pds)
581
582 print >>out, '%module params'
583
584 print >>out, '%{'
585 for obj in ordered_objs:
586 print >>out, '#include "params/%s.hh"' % obj
587 print >>out, '%}'
588
589 for pd in predecls:
590 print >>out, pd
591
592 enums = list(enums)
593 enums.sort()
594 for enum in enums:
595 print >>out, '%%include "enums/%s.hh"' % enum.__name__
596 print >>out
597
598 for obj in ordered_objs:
599 if obj.swig_objdecls:
600 for decl in obj.swig_objdecls:
601 print >>out, decl
602 continue
603
604 class_path = obj.cxx_class.split('::')
605 classname = class_path[-1]
606 namespaces = class_path[:-1]
607 namespaces.reverse()
608
609 code = ''
610
611 if namespaces:
612 code += '// avoid name conflicts\n'
613 sep_string = '_COLONS_'
614 flat_name = sep_string.join(class_path)
615 code += '%%rename(%s) %s;\n' % (flat_name, classname)
616
617 code += '// stop swig from creating/wrapping default ctor/dtor\n'
618 code += '%%nodefault %s;\n' % classname
619 code += 'class %s ' % classname
620 if obj._base:
621 code += ': public %s' % obj._base.cxx_class
622 code += ' {};\n'
623
624 for ns in namespaces:
625 new_code = 'namespace %s {\n' % ns
626 new_code += code
627 new_code += '}\n'
628 code = new_code
629
630 print >>out, code
631
632 print >>out, '%%include "src/sim/sim_object_params.hh"' % obj
633 for obj in ordered_objs:
634 print >>out, '%%include "params/%s.hh"' % obj
635
636params_file = File('params/params.i')
637names = sorted(sim_objects.keys())
638env.Command(params_file, map(Value, names), buildParams)
639env.Depends(params_file, params_hh_files + params_i_files + depends)
640SwigSource('m5.objects', params_file)
641
642# Build all swig modules
643for swig in SwigSource.all:
644 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
645 '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
646 '-o ${TARGETS[0]} $SOURCES')
647 env.Depends(swig.py_source.tnode, swig.tnode)
648 env.Depends(swig.cc_source.tnode, swig.tnode)
649
650# Generate the main swig init file
651def makeSwigInit(target, source, env):
652 f = file(str(target[0]), 'w')
653 print >>f, 'extern "C" {'
654 for module in source:
655 print >>f, ' void init_%s();' % module.get_contents()
656 print >>f, '}'
657 print >>f, 'void initSwig() {'
658 for module in source:
659 print >>f, ' init_%s();' % module.get_contents()
660 print >>f, '}'
661 f.close()
662
663env.Command('python/swig/init.cc',
664 map(Value, sorted(s.module for s in SwigSource.all)),
665 makeSwigInit)
666Source('python/swig/init.cc')
667
668def getFlags(source_flags):
669 flagsMap = {}
670 flagsList = []

--- 13 unchanged lines hidden (view full) ---

684
685 flagsList.sort()
686 return flagsList
687
688
689# Generate traceflags.py
690def traceFlagsPy(target, source, env):
691 assert(len(target) == 1)
692
693 f = file(str(target[0]), 'w')
694
695 allFlags = getFlags(source)
696
697 print >>f, 'basic = ['
698 for flag, compound, desc in allFlags:
699 if not compound:
700 print >>f, " '%s'," % flag
701 print >>f, " ]"
702 print >>f
703
704 print >>f, 'compound = ['
705 print >>f, " 'All',"
706 for flag, compound, desc in allFlags:
707 if compound:
708 print >>f, " '%s'," % flag
709 print >>f, " ]"
710 print >>f
711
712 print >>f, "all = frozenset(basic + compound)"
713 print >>f
714
715 print >>f, 'compoundMap = {'
716 all = tuple([flag for flag,compound,desc in allFlags if not compound])
717 print >>f, " 'All' : %s," % (all, )
718 for flag, compound, desc in allFlags:
719 if compound:
720 print >>f, " '%s' : %s," % (flag, compound)
721 print >>f, " }"
722 print >>f
723
724 print >>f, 'descriptions = {'
725 print >>f, " 'All' : 'All flags',"
726 for flag, compound, desc in allFlags:
727 print >>f, " '%s' : '%s'," % (flag, desc)
728 print >>f, " }"
729
730 f.close()
731
732def traceFlagsCC(target, source, env):
733 assert(len(target) == 1)
734
735 f = file(str(target[0]), 'w')
736
737 allFlags = getFlags(source)
738
739 # file header
740 print >>f, '''
741/*
742 * DO NOT EDIT THIS FILE! Automatically generated
743 */
744
745#include "base/traceflags.hh"
746
747using namespace Trace;
748
749const char *Trace::flagStrings[] =
750{'''
751
752 # The string array is used by SimpleEnumParam to map the strings
753 # provided by the user to enum values.
754 for flag, compound, desc in allFlags:
755 if not compound:
756 print >>f, ' "%s",' % flag
757
758 print >>f, ' "All",'
759 for flag, compound, desc in allFlags:
760 if compound:
761 print >>f, ' "%s",' % flag
762
763 print >>f, '};'
764 print >>f
765 print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1)
766 print >>f
767
768 #
769 # Now define the individual compound flag arrays. There is an array
770 # for each compound flag listing the component base flags.
771 #
772 all = tuple([flag for flag,compound,desc in allFlags if not compound])
773 print >>f, 'static const Flags AllMap[] = {'
774 for flag, compound, desc in allFlags:
775 if not compound:
776 print >>f, " %s," % flag
777 print >>f, '};'
778 print >>f
779
780 for flag, compound, desc in allFlags:
781 if not compound:
782 continue
783 print >>f, 'static const Flags %sMap[] = {' % flag
784 for flag in compound:
785 print >>f, " %s," % flag
786 print >>f, " (Flags)-1"
787 print >>f, '};'
788 print >>f
789
790 #
791 # Finally the compoundFlags[] array maps the compound flags
792 # to their individual arrays/
793 #
794 print >>f, 'const Flags *Trace::compoundFlags[] ='
795 print >>f, '{'
796 print >>f, ' AllMap,'
797 for flag, compound, desc in allFlags:
798 if compound:
799 print >>f, ' %sMap,' % flag
800 # file trailer
801 print >>f, '};'
802
803 f.close()
804
805def traceFlagsHH(target, source, env):
806 assert(len(target) == 1)
807
808 f = file(str(target[0]), 'w')
809
810 allFlags = getFlags(source)
811
812 # file header boilerplate
813 print >>f, '''
814/*
815 * DO NOT EDIT THIS FILE!
816 *
817 * Automatically generated from traceflags.py
818 */
819
820#ifndef __BASE_TRACE_FLAGS_HH__
821#define __BASE_TRACE_FLAGS_HH__
822
823namespace Trace {
824
825enum Flags {'''
826
827 # Generate the enum. Base flags come first, then compound flags.
828 idx = 0
829 for flag, compound, desc in allFlags:
830 if not compound:
831 print >>f, ' %s = %d,' % (flag, idx)
832 idx += 1
833
834 numBaseFlags = idx
835 print >>f, ' NumFlags = %d,' % idx
836
837 # put a comment in here to separate base from compound flags
838 print >>f, '''
839// The remaining enum values are *not* valid indices for Trace::flags.
840// They are "compound" flags, which correspond to sets of base
841// flags, and are used by changeFlag.'''
842
843 print >>f, ' All = %d,' % idx
844 idx += 1
845 for flag, compound, desc in allFlags:
846 if compound:
847 print >>f, ' %s = %d,' % (flag, idx)
848 idx += 1
849
850 numCompoundFlags = idx - numBaseFlags
851 print >>f, ' NumCompoundFlags = %d' % numCompoundFlags
852
853 # trailer boilerplate
854 print >>f, '''\
855}; // enum Flags
856
857// Array of strings for SimpleEnumParam
858extern const char *flagStrings[];
859extern const int numFlagStrings;
860
861// Array of arraay pointers: for each compound flag, gives the list of
862// base flags to set. Inidividual flag arrays are terminated by -1.
863extern const Flags *compoundFlags[];
864
865/* namespace Trace */ }
866
867#endif // __BASE_TRACE_FLAGS_HH__
868'''
869
870 f.close()
871
872flags = map(Value, trace_flags.values())
873env.Command('base/traceflags.py', flags, traceFlagsPy)
874PySource('m5', 'base/traceflags.py')
875
876env.Command('base/traceflags.hh', flags, traceFlagsHH)
877env.Command('base/traceflags.cc', flags, traceFlagsCC)
878Source('base/traceflags.cc')

--- 5 unchanged lines hidden (view full) ---

884# inserts the result into the data section with symbols indicating the
885# beginning, and end (and with the size at the end)
886def objectifyPyFile(target, source, env):
887 '''Action function to compile a .py into a code object, marshal
888 it, compress it, and stick it into an asm file so the code appears
889 as just bytes with a label in the data section'''
890
891 src = file(str(source[0]), 'r').read()
892 dst = file(str(target[0]), 'w')
893
894 pysource = PySource.tnodes[source[0]]
895 compiled = compile(src, pysource.abspath, 'exec')
896 marshalled = marshal.dumps(compiled)
897 compressed = zlib.compress(marshalled)
898 data = compressed
899
900 # Some C/C++ compilers prepend an underscore to global symbol
901 # names, so if they're going to do that, we need to prepend that
902 # leading underscore to globals in the assembly file.
903 if env['LEADING_UNDERSCORE']:
904 sym = '_' + pysource.symname
905 else:
906 sym = pysource.symname
907
908 step = 16
909 print >>dst, ".data"
910 print >>dst, ".globl %s_beg" % sym
911 print >>dst, ".globl %s_end" % sym
912 print >>dst, "%s_beg:" % sym
913 for i in xrange(0, len(data), step):
914 x = array.array('B', data[i:i+step])
915 print >>dst, ".byte", ','.join([str(d) for d in x])
916 print >>dst, "%s_end:" % sym
917 print >>dst, ".long %d" % len(marshalled)
918
919for source in PySource.all:
920 env.Command(source.assembly, source.tnode, objectifyPyFile)
921 Source(source.assembly)
922
923# Generate init_python.cc which creates a bunch of EmbeddedPyModule
924# structs that describe the embedded python code. One such struct
925# contains information about the importer that python uses to get at
926# the embedded files, and then there's a list of all of the rest that
927# the importer uses to load the rest on demand.
928def pythonInit(target, source, env):
929 dst = file(str(target[0]), 'w')
930
931 def dump_mod(sym, endchar=','):
932 def c_str(string):
933 if string is None:
934 return "0"
935 return '"%s"' % string
936 pysource = PySource.symnames[sym]
937 print >>dst, ' { %s,' % c_str(pysource.arcname)
938 print >>dst, ' %s,' % c_str(pysource.abspath)
939 print >>dst, ' %s,' % c_str(pysource.modpath)
940 print >>dst, ' %s_beg, %s_end,' % (sym, sym)
941 print >>dst, ' %s_end - %s_beg,' % (sym, sym)
942 print >>dst, ' *(int *)%s_end }%s' % (sym, endchar)
943
944 print >>dst, '#include "sim/init.hh"'
945
946 for sym in source:
947 sym = sym.get_contents()
948 print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym)
949
950 print >>dst, "const EmbeddedPyModule embeddedPyImporter = "
951 dump_mod("PyEMB_importer", endchar=';');
952 print >>dst
953
954 print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {"
955 for i,sym in enumerate(source):
956 sym = sym.get_contents()
957 if sym == "PyEMB_importer":
958 # Skip the importer since we've already exported it
959 continue
960 dump_mod(sym)
961 print >>dst, " { 0, 0, 0, 0, 0, 0, 0 }"
962 print >>dst, "};"
963
964
965env.Command('sim/init_python.cc',
966 map(Value, (s.symname for s in PySource.all)),
967 pythonInit)
968Source('sim/init_python.cc')
969
970########################################################################
971#

--- 136 unchanged lines hidden ---