Beispiel #1
0
    def get_or_create_indexed_instance(self, index):
        """ index may be a number or 'next'.
            Create instance if it does not exist, IF it is the next to be created.
        """
        if index != 'next' and index != 'last': index = get_integer(index)


        # Create a new header_inst if index is next or else it is a value equal to
        # the next index to be used in the stack.
        if index == 'next' or index == len(self.stack):
            if len(self.stack) == self.stack_max_size:  # full - return None
                print "Referenced instance",index,"of stack %s but stack is full." % self.hdr_inst_name
                return None
            # Create new header instance
            #print "Creating hdr inst %s[%d]" % (self.hdr_inst_name, len(self.stack))
            h_i = Header_Instance(  self.string, self.loc,
                                    self.hdr_type_name,
                                    self.hdr_is_metadata,
                                    "%s[%d]" % (self.hdr_inst_name, len(self.stack))
                                  )
            self.stack.append(h_i)
            return h_i

        if index == 'last':
            if len(self.stack) == 0:
                print "Referenced 'last' hdr_inst of stack %s but stack is empty." % self.hdr_inst_name
                return None
            return self.stack[-1]

        # Get actual numbered entry
        if index >= len(self.stack):
            print "Indexed entry %d of stack %s but stack only has %d entries." % (
                    index, self.hdr_inst_name, len(self.stack) )
            return None
        return self.stack[index]
    def __init__(self, string, loc, hdr_name , hdr_body ):

        assert len(hdr_body)>0 and len(hdr_body)<4
        
        super(Header_Declaration, self).__init__(string, loc, 'header_declaration')

        self.name        = hdr_name
        self.fields      = hdr_body[0]  # [ Field objects ]
        self.field_names = []           # ordered list of field names
        self.length_expr = None         # list of strings. expression to calc length.
        self.max_length  = 0            # Integer. max length for header
        
        hdr_len_undefined = False       # True if we see a field with bit_width '*'   (0)
        bit_count = 0
        for f in self.fields:
            if f.name in self.field_names:
                print_syntax_err('Field name "%s" already defined in header "%s"' %
                                (f.name, self.name), string, loc)

            if f.bit_width == 0:
                if hdr_len_undefined:
                    print_syntax_err('In header "%s" two or more fields have undefined width.' %
                                     self.name, string, loc)
                
                hdr_len_undefined = True 

            bit_count += f.bit_width

            self.field_names.append(f.name)

        if ( bit_count % 8 ) != 0:
            if not hdr_len_undefined:
                print_syntax_err('In header "%s" total field bit_width not byte aligned: %d bits.' %
                                     (self.name, bit_count), string, loc)



        # Process options

        if len(hdr_body)>1:
            for opt in hdr_body[1:]: 
                # print "  opt:", opt
                assert len(opt)>1  # [0]=opt name, [1] = value
                opt_name = opt[0]
                if opt_name == 'length':
                    self.length_expr = opt.asList()[1:]
                if opt_name == 'max_length':
                    self.max_length = get_integer(opt[1])

        if hdr_len_undefined:
            if not  self.length_expr:
                 print_syntax_err('In header "%s" a field has width "*" so "length" must be defined.' %
                                     self.name, string, loc)
Beispiel #3
0
 def get_indexed_instance(self, index):
     index = get_integer(index)
     if len(self.stack) <= index: return None
     else: return self.stack[index]
Beispiel #4
0
 def is_legal_index(self, index):
     if index == 'last': return True
     if index == 'next':
         return len(self.stack) < self.stack_max_size
     return get_integer(index) < self.stack_max_size