SimObject.py (7811:a8fc35183c10) SimObject.py (8321:9f34cf472451)
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# Copyright (c) 2010 Advanced Micro Devices, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;

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

273 pdesc.name = name
274 cls._params[name] = pdesc
275 if hasattr(pdesc, 'default'):
276 cls._set_param(name, pdesc.default, pdesc)
277
278 def _set_param(cls, name, value, param):
279 assert(param.name == name)
280 try:
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# Copyright (c) 2010 Advanced Micro Devices, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;

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

273 pdesc.name = name
274 cls._params[name] = pdesc
275 if hasattr(pdesc, 'default'):
276 cls._set_param(name, pdesc.default, pdesc)
277
278 def _set_param(cls, name, value, param):
279 assert(param.name == name)
280 try:
281 cls._values[name] = param.convert(value)
281 value = param.convert(value)
282 except Exception, e:
283 msg = "%s\nError setting param %s.%s to %s\n" % \
284 (e, cls.__name__, name, value)
285 e.args = (msg, )
286 raise
282 except Exception, e:
283 msg = "%s\nError setting param %s.%s to %s\n" % \
284 (e, cls.__name__, name, value)
285 e.args = (msg, )
286 raise
287 cls._values[name] = value
288 # if param value is a SimObject, make it a child too, so that
289 # it gets cloned properly when the class is instantiated
290 if isSimObjectOrVector(value) and not value.has_parent():
291 cls._add_cls_child(name, value)
287
292
293 def _add_cls_child(cls, name, child):
294 # It's a little funky to have a class as a parent, but these
295 # objects should never be instantiated (only cloned, which
296 # clears the parent pointer), and this makes it clear that the
297 # object is not an orphan and can provide better error
298 # messages.
299 child.set_parent(cls, name)
300 cls._children[name] = child
301
288 def _new_port(cls, name, port):
289 # each port should be uniquely assigned to one variable
290 assert(not hasattr(port, 'name'))
291 port.name = name
292 cls._ports[name] = port
293 if hasattr(port, 'default'):
294 cls._cls_get_port_ref(name).connect(port.default)
295

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

329 # check for param
330 param = cls._params.get(attr)
331 if param:
332 cls._set_param(attr, value, param)
333 return
334
335 if isSimObjectOrSequence(value):
336 # If RHS is a SimObject, it's an implicit child assignment.
302 def _new_port(cls, name, port):
303 # each port should be uniquely assigned to one variable
304 assert(not hasattr(port, 'name'))
305 port.name = name
306 cls._ports[name] = port
307 if hasattr(port, 'default'):
308 cls._cls_get_port_ref(name).connect(port.default)
309

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

343 # check for param
344 param = cls._params.get(attr)
345 if param:
346 cls._set_param(attr, value, param)
347 return
348
349 if isSimObjectOrSequence(value):
350 # If RHS is a SimObject, it's an implicit child assignment.
337 cls._children[attr] = coerceSimObjectOrVector(value)
351 cls._add_cls_child(attr, coerceSimObjectOrVector(value))
338 return
339
340 # no valid assignment... raise exception
341 raise AttributeError, \
342 "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
343
344 def __getattr__(cls, attr):
345 if attr == 'cxx_class_path':

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

503
504 # initialize required attributes
505 self._parent = None
506 self._name = None
507 self._ccObject = None # pointer to C++ object
508 self._ccParams = None
509 self._instantiated = False # really "cloned"
510
352 return
353
354 # no valid assignment... raise exception
355 raise AttributeError, \
356 "Class %s has no parameter \'%s\'" % (cls.__name__, attr)
357
358 def __getattr__(cls, attr):
359 if attr == 'cxx_class_path':

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

517
518 # initialize required attributes
519 self._parent = None
520 self._name = None
521 self._ccObject = None # pointer to C++ object
522 self._ccParams = None
523 self._instantiated = False # really "cloned"
524
525 # Clone children specified at class level. No need for a
526 # multidict here since we will be cloning everything.
527 # Do children before parameter values so that children that
528 # are also param values get cloned properly.
529 self._children = {}
530 for key,val in ancestor._children.iteritems():
531 self.add_child(key, val(_memo=memo_dict))
532
511 # Inherit parameter values from class using multidict so
512 # individual value settings can be overridden but we still
513 # inherit late changes to non-overridden class values.
514 self._values = multidict(ancestor._values)
515 # clone SimObject-valued parameters
516 for key,val in ancestor._values.iteritems():
517 val = tryAsSimObjectOrVector(val)
518 if val is not None:
519 self._values[key] = val(_memo=memo_dict)
520
533 # Inherit parameter values from class using multidict so
534 # individual value settings can be overridden but we still
535 # inherit late changes to non-overridden class values.
536 self._values = multidict(ancestor._values)
537 # clone SimObject-valued parameters
538 for key,val in ancestor._values.iteritems():
539 val = tryAsSimObjectOrVector(val)
540 if val is not None:
541 self._values[key] = val(_memo=memo_dict)
542
521 # Clone children specified at class level. No need for a
522 # multidict here since we will be cloning everything.
523 self._children = {}
524 for key,val in ancestor._children.iteritems():
525 self.add_child(key, val(_memo=memo_dict))
526
527 # clone port references. no need to use a multidict here
528 # since we will be creating new references for all ports.
529 self._port_refs = {}
530 for key,val in ancestor._port_refs.iteritems():
531 self._port_refs[key] = val.clone(self, memo_dict)
532 # apply attribute assignments from keyword args, if any
533 for key,val in kwargs.iteritems():
534 setattr(self, key, val)

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

609 try:
610 value = param.convert(value)
611 except Exception, e:
612 msg = "%s\nError setting param %s.%s to %s\n" % \
613 (e, self.__class__.__name__, attr, value)
614 e.args = (msg, )
615 raise
616 self._values[attr] = value
543 # clone port references. no need to use a multidict here
544 # since we will be creating new references for all ports.
545 self._port_refs = {}
546 for key,val in ancestor._port_refs.iteritems():
547 self._port_refs[key] = val.clone(self, memo_dict)
548 # apply attribute assignments from keyword args, if any
549 for key,val in kwargs.iteritems():
550 setattr(self, key, val)

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

625 try:
626 value = param.convert(value)
627 except Exception, e:
628 msg = "%s\nError setting param %s.%s to %s\n" % \
629 (e, self.__class__.__name__, attr, value)
630 e.args = (msg, )
631 raise
632 self._values[attr] = value
633 # implicitly parent unparented objects assigned as params
634 if isSimObjectOrVector(value) and not value.has_parent():
635 self.add_child(attr, value)
617 return
618
619 # if RHS is a SimObject, it's an implicit child assignment
620 if isSimObjectOrSequence(value):
621 self.add_child(attr, value)
622 return
623
624 # no valid assignment... raise exception

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

642 def set_parent(self, parent, name):
643 self._parent = parent
644 self._name = name
645
646 # Also implemented by SimObjectVector
647 def get_name(self):
648 return self._name
649
636 return
637
638 # if RHS is a SimObject, it's an implicit child assignment
639 if isSimObjectOrSequence(value):
640 self.add_child(attr, value)
641 return
642
643 # no valid assignment... raise exception

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

661 def set_parent(self, parent, name):
662 self._parent = parent
663 self._name = name
664
665 # Also implemented by SimObjectVector
666 def get_name(self):
667 return self._name
668
650 # use this rather than directly accessing _parent for symmetry
651 # with SimObjectVector
652 def get_parent(self):
653 return self._parent
669 # Also implemented by SimObjectVector
670 def has_parent(self):
671 return self._parent is not None
654
655 # clear out child with given name. This code is not likely to be exercised.
656 # See comment in add_child.
657 def clear_child(self, name):
658 child = self._children[name]
659 child.clear_parent(self)
660 del self._children[name]
661
662 # Add a new child to this object.
663 def add_child(self, name, child):
664 child = coerceSimObjectOrVector(child)
672
673 # clear out child with given name. This code is not likely to be exercised.
674 # See comment in add_child.
675 def clear_child(self, name):
676 child = self._children[name]
677 child.clear_parent(self)
678 del self._children[name]
679
680 # Add a new child to this object.
681 def add_child(self, name, child):
682 child = coerceSimObjectOrVector(child)
665 if child.get_parent():
666 raise RuntimeError, \
667 "add_child('%s'): child '%s' already has parent '%s'" % \
668 (name, child._name, child._parent)
683 if child.has_parent():
684 print "warning: add_child('%s'): child '%s' already has parent" % \
685 (name, child.get_name())
669 if self._children.has_key(name):
670 # This code path had an undiscovered bug that would make it fail
671 # at runtime. It had been here for a long time and was only
672 # exposed by a buggy script. Changes here will probably not be
673 # exercised without specialized testing.
674 self.clear_child(name)
675 child.set_parent(self, name)
676 self._children[name] = child
677
678 # Take SimObject-valued parameters that haven't been explicitly
679 # assigned as children and make them children of the object that
680 # they were assigned to as a parameter value. This guarantees
681 # that when we instantiate all the parameter objects we're still
682 # inside the configuration hierarchy.
683 def adoptOrphanParams(self):
684 for key,val in self._values.iteritems():
685 if not isSimObjectVector(val) and isSimObjectSequence(val):
686 # need to convert raw SimObject sequences to
686 if self._children.has_key(name):
687 # This code path had an undiscovered bug that would make it fail
688 # at runtime. It had been here for a long time and was only
689 # exposed by a buggy script. Changes here will probably not be
690 # exercised without specialized testing.
691 self.clear_child(name)
692 child.set_parent(self, name)
693 self._children[name] = child
694
695 # Take SimObject-valued parameters that haven't been explicitly
696 # assigned as children and make them children of the object that
697 # they were assigned to as a parameter value. This guarantees
698 # that when we instantiate all the parameter objects we're still
699 # inside the configuration hierarchy.
700 def adoptOrphanParams(self):
701 for key,val in self._values.iteritems():
702 if not isSimObjectVector(val) and isSimObjectSequence(val):
703 # need to convert raw SimObject sequences to
687 # SimObjectVector class so we can call get_parent()
704 # SimObjectVector class so we can call has_parent()
688 val = SimObjectVector(val)
689 self._values[key] = val
705 val = SimObjectVector(val)
706 self._values[key] = val
690 if isSimObjectOrVector(val) and not val.get_parent():
707 if isSimObjectOrVector(val) and not val.has_parent():
708 print "warning: %s adopting orphan SimObject param '%s'" \
709 % (self, key)
691 self.add_child(key, val)
692
693 def path(self):
694 if not self._parent:
710 self.add_child(key, val)
711
712 def path(self):
713 if not self._parent:
695 return '(orphan)'
714 return '<orphan %s>' % self.__class__
696 ppath = self._parent.path()
697 if ppath == 'root':
698 return self._name
699 return ppath + "." + self._name
700
701 def __str__(self):
702 return self.path()
703

--- 273 unchanged lines hidden ---
715 ppath = self._parent.path()
716 if ppath == 'root':
717 return self._name
718 return ppath + "." + self._name
719
720 def __str__(self):
721 return self.path()
722

--- 273 unchanged lines hidden ---