Ejemplo n.º 1
0
    def test_remove_comments(self):
        """try and remove all comments from a buffer"""

        bufin = "not comment /*comment\n\n*/\n\n//comment\n\n/*\nabc\n*/soemthing//comment"
        #print "input buffer:\n" + bufin
        output_buffer = saputils.remove_comments(bufin)
        #print "output buffer:\n" + bufout

        self.assertEqual(len(output_buffer) > 0, True)
Ejemplo n.º 2
0
	def test_remove_comments(self):
		"""try and remove all comments from a buffer"""
		import saputils
		bufin = "not comment /*comment\n\n*/\n\n//comment\n\n/*\nabc\n*/soemthing//comment"
		#print "input buffer:\n" + bufin
		output_buffer = saputils.remove_comments(bufin)
		#print "output buffer:\n" + bufout
		
		self.assertEqual(len(output_buffer) > 0, True)
Ejemplo n.º 3
0
	def is_module_in_file(self, filename, module_name, debug = False):
		"""check the file for the module"""
		
		fbuf = ""
		#the name is a verilog file, try and open is
		try:
			filein = open(filename)
			fbuf = filein.read()
			filein.close()
		except IOError as err:
			if debug:
				print "the file is not a full path... searching RTL"
			#didn't find with full path, search for it
			try: 
				filepath = saputils.find_rtl_file_location(filename)
				filein = open(filepath)	
				fbuf = filein.read()
				filein.close()
			except IOError as err_int:
				if debug:
					print "couldn't find file in the RTL directory"
				return False
		if debug:
			print "opened file: " + filename
		fbuf = saputils.remove_comments(fbuf)
		done = False
		module_string = fbuf.partition("module")[2]
		while (not done):
#			if debug:
			module_string = module_string.partition("(")[0]
			module_string = module_string.strip("#")
			module_string = module_string.strip()
			if debug:
				print "searching through: " + module_string
#			module_string = module_string.strip()
#			module_string = module_string.partition(" ")[0]
			if (len(module_string) == 0):
				done = True
			if (module_string.endswith("(")):
				module_string = module_string.strip("(")
			if debug:
				print "looking at: " + module_string

			if (module_string == module_name):
				if debug:
					print "found " + module_string + " in " + filename
				return True
				
			elif(len(module_string.partition("module")[2]) > 0):
				if debug:
					print "found another module in the file"
				module_string = module_string.partition("module")[2]
			else:
				done = True

		return False
Ejemplo n.º 4
0
	def has_dependencies(self, filename, debug = False):
		"""look in a verilog module, and search for anything that requires a depency, return true if found"""

		if debug:
			print "input file: " + filename
		#filename needs to be a verilog file
		if (filename.partition(".")[2] != "v"):
			if debug:
				print "File is not a recognized verilog source"
			return False

		fbuf = ""
		#the name is a verilog file, try and open is
		try:
			filein = open(filename)
			fbuf = filein.read()
			filein.close()
		except IOError as err:
			if debug:
				print "the file is not a full path, searching RTL... ", 
			#didn't find with full path, search for it
			try: 
				filepath = saputils.find_rtl_file_location(filename)
				filein = open(filepath)	
				fbuf = filein.read()
				filein.close()
			except IOError as err_int:
				if debug:
					print "couldn't find file in the RTL directory"
				return False


		#we have an open file!
		if debug:
			print "found file!"

		#strip out everything we can't use
		fbuf = saputils.remove_comments(fbuf)

		#modules have lines that start with a '.'
		str_list = fbuf.splitlines()

		for item in str_list:
			item = item.strip()
			if (item.startswith(".")):
				if debug:
					print "found a module!"
				return True

		return False
Ejemplo n.º 5
0
def generate_define_table(filestring="", debug = False):
	"""Read in a file in string format and create a dictionary relating define name and value""" 
	import saputils
	define_dict = {}
	#from a file string find all the defines and generate an entry into a dictionary
	filestring = saputils.remove_comments(filestring) 
	str_list = filestring.splitlines()

	for item in str_list:
		item = item.strip()
		if item.startswith("`include"):
			if debug:
				print "found an include: " + item
			#read int the include file, strip away the comments
			#then append everything to the end
			item = item.partition("`include")[2]
			item = item.strip()
			item = item.strip("\"")
			inc_file = saputils.find_rtl_file_location(item)	
			if debug:
				print "include file location: " + inc_file
			try:
				ifile = open(inc_file)
				fs = ifile.read()
				ifile.close()
				if debug:
					print "got the new file string"
				include_defines = generate_define_table(fs)
				if debug:
					print "after include_define"
					print "length of include defines: " + str(len(include_defines.keys()))
				for key in include_defines.keys():
					#append the values found in the include back in the local dictionary
					if debug:
						print "working on: " + key
					if (not define_dict.has_key(key)):
						define_dict[key] = include_defines[key]

				
				if debug:
					print "added new items onto the list"
			except TypeError as terr:
				print "Type Error: " + str(terr)
			except:
				print "error while processing : ", item, ": ",  sys.exc_info()[0]
			continue

		if item.startswith("`define"):
			#if the string starts with `define split the name and value into the dictionary
#			if debug:
#				print "found a define: " + item
			item = item.partition("`define")[2]
			item = item.strip()
			if (len(item.partition(" ")[2]) > 0):
				name = item.partition(" ")[0].strip()
				value = item.partition(" ")[2].strip()
				if debug:
					print "added " + name + "\n\tWith value: " + value
				define_dict[name] = value
				continue
			if (len(item.partition("\t")[2]) > 0):
				name = item.partition("\t")[0].strip()
				value = item.partition("\t")[2].strip()
				if debug:
					print "added " + name + "\n\tWith value: " + value
				define_dict[name] = value
				continue
			if debug:
				print "found a define without a value: " + item

	return define_dict
Ejemplo n.º 6
0
	def get_list_of_dependencies(self, filename, debug=False):
		deps = []
		if debug:
			print "input file: " + filename
		#filename needs to be a verilog file
		if (filename.partition(".")[2] != "v"):
			if debug:
				print "File is not a recognized verilog source"
			return False

		fbuf = ""
		#the name is a verilog file, try and open is
		try:
			filein = open(filename)
			fbuf = filein.read()
			filein.close()
		except IOError as err:
			#if debug:
			#	print "the file is not a full path... searching RTL"
			#didn't find with full path, search for it
			try: 
				filepath = saputils.find_rtl_file_location(filename)
				filein = open(filepath)	
				fbuf = filein.read()
				filein.close()
			except IOError as err_int:
				#if debug:
				#	print "couldn't find file in the RTL directory"
				return False


		#we have an open file!
		if debug:
			print "found file!"

		#strip out everything we can't use
		fbuf = saputils.remove_comments(fbuf)

		include_fbuf = fbuf
		#search for `include
		while (not len(include_fbuf.partition("`include")[2]) == 0):
			ifile_name = include_fbuf.partition("`include")[2]
			ifile_name = ifile_name.splitlines()[0]
			ifile_name = ifile_name.strip()
			ifile_name = ifile_name.strip("\"")
			print "\t\tfound an include " + ifile_name + " ",
			if (not self.verilog_dependency_list.__contains__(ifile_name) and
				not self.verilog_file_list.__contains__(ifile_name)):
				self.verilog_dependency_list.append(ifile_name)
				if debug:
					print "adding " + ifile_name + " to the dependency list"
			else:
				print "... already in have it"
			include_fbuf = include_fbuf.partition("`include")[2]

		#remove the ports list and the module name
		fbuf = fbuf.partition(")")[2]

		#modules have lines that start with a '.'
		str_list = fbuf.splitlines()
	
		module_token = ""
		done = False
		while (not done): 
			for i in range (0, len(str_list)):
				line = str_list[i]
				#remove white spaces
				line = line.strip()
				if (line.startswith(".") and line.endswith(",")):
					#if debug:
					#	print "found a possible module... with token: " + line
					module_token = line
					break
				#check if we reached the last line
				if (i >= len(str_list) - 1):
					done = True

			if (not done):
				#found a possible module
				#partitoin the fbuf
				#if debug:
				#	print "module token " + module_token
				module_string = fbuf.partition(module_token)[0]
				fbuf = fbuf.partition(module_token)[2]
				fbuf = fbuf.partition(";")[2]
				str_list = fbuf.splitlines()

				#get rid of everything before the possible module
				while (len(module_string.partition(";")[2]) > 0):
					module_string = module_string.partition(";")[2]
							
				module_string = module_string.partition("(")[0]	
				module_string = module_string.strip("#")
				module_string = module_string.strip()

				m_name = module_string.partition(" ")[0]
				if debug:
					print "module name: " + m_name

				if (not deps.__contains__(m_name)):
					print "adding it to the deps list"
					deps.append(module_string.partition(" ")[0])
				

				#mlist = module_string.splitlines()
				#work backwords
				#look for the last line that has a '('
				#for i in range (0, len(mlist)):
				#	mstr = mlist[i]
				#	print "item: " + mlist[i]
				#	#mstr = mlist[len(mlist) - 1 - i]
				#	#mstr = mstr.strip()
				#	if (mstr.__contains__(" ")):
				#		if debug:
				#			print "found: " + mstr.partition(" ")[0]
				#		deps.append(mstr.partition(" ")[0])
				#		break
				

		return deps
Ejemplo n.º 7
0
    def get_list_of_dependencies(self, filename, debug=False):
        """get_list_of_dependencies

    return a list of the files that this file depends on

    Args:
      filename: the name of the file to analyze

    Return:
      A list of files that specify the dependenies

    Raises:
      IOError
    """
        deps = []
        if debug:
            print "input file: " + filename
        #filename needs to be a verilog file
        if (filename.partition(".")[2] != "v"):
            if debug:
                print "File is not a recognized verilog source"
            return False

        fbuf = ""
        #the name is a verilog file, try and open is
        try:
            filein = open(filename)
            fbuf = filein.read()
            filein.close()
        except IOError as err:
            #if debug:
            #  print "the file is not a full path... searching RTL"
            #didn't find with full path, search for it
            try:
                filepath = saputils.find_rtl_file_location(filename)
                filein = open(filepath)
                fbuf = filein.read()
                filein.close()
            except IOError as err_int:
                ModuleFactoryError(
                    "Couldn't find file %s in the RTL directory" % filename)

        #we have an open file!
        if debug:
            print "found file!"

        #strip out everything we can't use
        fbuf = saputils.remove_comments(fbuf)

        include_fbuf = fbuf
        #search for `include
        while (not len(include_fbuf.partition("`include")[2]) == 0):
            ifile_name = include_fbuf.partition("`include")[2]
            ifile_name = ifile_name.splitlines()[0]
            ifile_name = ifile_name.strip()
            ifile_name = ifile_name.strip("\"")
            if debug:
                print "found an include " + ifile_name + " ",
            if (not self.verilog_dependency_list.__contains__(ifile_name)
                    and not self.verilog_file_list.__contains__(ifile_name)):
                self.verilog_dependency_list.append(ifile_name)
                if debug:
                    print "adding " + ifile_name + " to the dependency list"
            else:
                if debug:
                    print "... already in have it"
            include_fbuf = include_fbuf.partition("`include")[2]

        #remove the ports list and the module name
        fbuf = fbuf.partition(")")[2]

        #modules have lines that start with a '.'
        str_list = fbuf.splitlines()

        module_token = ""
        done = False
        while (not done):
            for i in range(0, len(str_list)):
                line = str_list[i]
                #remove white spaces
                line = line.strip()
                if (line.startswith(".") and line.endswith(",")):
                    #if debug:
                    #  print "found a possible module... with token: " + line
                    module_token = line
                    break
                #check if we reached the last line
                if (i >= len(str_list) - 1):
                    done = True

            if (not done):
                #found a possible module
                #partitoin the fbuf
                #if debug:
                #  print "module token " + module_token
                module_string = fbuf.partition(module_token)[0]
                fbuf = fbuf.partition(module_token)[2]
                fbuf = fbuf.partition(";")[2]
                str_list = fbuf.splitlines()

                #get rid of everything before the possible module
                while (len(module_string.partition(";")[2]) > 0):
                    module_string = module_string.partition(";")[2]

                module_string = module_string.partition("(")[0]
                module_string = module_string.strip("#")
                module_string = module_string.strip()

                m_name = module_string.partition(" ")[0]
                if debug:
                    print "module name: " + m_name

                if (not deps.__contains__(m_name)):
                    if debug:
                        print "adding it to the deps list"
                    deps.append(module_string.partition(" ")[0])

                #mlist = module_string.splitlines()
                #work backwords
                #look for the last line that has a '('
                #for i in range (0, len(mlist)):
                #  mstr = mlist[i]
                #  print "item: " + mlist[i]
                #  #mstr = mlist[len(mlist) - 1 - i]
                #  #mstr = mstr.strip()
                #  if (mstr.__contains__(" ")):
                #    if debug:
                #      print "found: " + mstr.partition(" ")[0]
                #    deps.append(mstr.partition(" ")[0])
                #    break

        return deps
Ejemplo n.º 8
0
    def has_dependencies(self, filename, debug=False):
        """has_dependencies

    returns true if the file specified has dependencies

    Args:
      filename: search for dependencies with this filename

    Return:
      True: The file has dependencies.
      False: The file doesn't have dependencies

    Raises:
      IOError
    """

        if debug:
            print "input file: " + filename
        #filename needs to be a verilog file
        if (filename.partition(".")[2] != "v"):
            if debug:
                print "File is not a recognized verilog source"
            return False

        fbuf = ""
        #the name is a verilog file, try and open is
        try:
            filein = open(filename)
            fbuf = filein.read()
            filein.close()
        except IOError as err:
            if debug:
                print "the file is not a full path, searching RTL... ",
            #didn't find with full path, search for it
            try:
                filepath = saputils.find_rtl_file_location(filename)
                filein = open(filepath)
                fbuf = filein.read()
                filein.close()
            except IOError as err_int:
                if debug:
                    print "couldn't find file in the RTL directory"
                ModuleFactoryError(
                    "Couldn't find file %s in the RTL directory" % filename)

        #we have an open file!
        if debug:
            print "found file!"

        #strip out everything we can't use
        fbuf = saputils.remove_comments(fbuf)

        #modules have lines that start with a '.'
        str_list = fbuf.splitlines()

        for item in str_list:
            item = item.strip()
            if (item.startswith(".")):
                if debug:
                    print "found a module!"
                return True
        return False
Ejemplo n.º 9
0
def generate_define_table(filestring="", debug=False):
    """Reads in a module as a buffer and returns a dictionary of defines

  Generates a table of defines that can be used to resolve values.
  If all the defines cannot be evaluated directly by the
  current module then this will search all the included modules 

  Args:
    filestring: A buffer from the module's file

  Returns:
    A dictionary of defines

  Raises:
    PreProcessorError 
  """
    import saputils
    define_dict = {}
    #from a file string find all the defines and generate an entry into a dictionary
    filestring = saputils.remove_comments(filestring)
    str_list = filestring.splitlines()

    for item in str_list:
        item = item.strip()
        #look for include files
        if item.startswith("`include"):
            if debug:
                print "found an include: " + item
            #read int the include file, strip away the comments
            #then append everything to the end
            item = item.partition("`include")[2]
            item = item.strip()
            item = item.strip("\"")
            inc_file = saputils.find_rtl_file_location(item)
            if debug:
                print "include file location: " + inc_file

            #try and open the include file
            try:
                ifile = open(inc_file)
                fs = ifile.read()
                ifile.close()
            except:
                if item != "project_defines.v":
                    raise PreProcessor(
                        "Error while attempting to the include file: %s" %
                        inc_file)

            try:
                if debug:
                    print "got the new file string"
                include_defines = generate_define_table(fs)
                if debug:
                    print "after include_define"
                    print "length of include defines: " + str(
                        len(include_defines.keys()))
                for key in include_defines.keys():
                    #append the values found in the include back in the local dictionary
                    if debug:
                        print "working on: " + key
                    if (not define_dict.has_key(key)):
                        define_dict[key] = include_defines[key]

                if debug:
                    print "added new items onto the list"


#      except TypeError as terr:
#        print "Type Error: " + str(terr)
            except:
                if item != "project_defines.v":
                    raise PreProcessorError("Error while processing: %s: %s" %
                                            (item, sys.exc_info()[0]))
                    #print "error while processing : ", item, ": ",  sys.exc_info()[0]
            continue

        if item.startswith("`define"):
            #if the string starts with `define split the name and value into the dictionary
            #      if debug:
            #        print "found a define: " + item
            item = item.partition("`define")[2]
            item = item.strip()
            if (len(item.partition(" ")[2]) > 0):
                name = item.partition(" ")[0].strip()
                value = item.partition(" ")[2].strip()
                if debug:
                    print "added " + name + "\n\tWith value: " + value
                define_dict[name] = value
                continue
            if (len(item.partition("\t")[2]) > 0):
                name = item.partition("\t")[0].strip()
                value = item.partition("\t")[2].strip()
                if debug:
                    print "added " + name + "\n\tWith value: " + value
                define_dict[name] = value
                continue
            if debug:
                print "found a define without a value: " + item

    return define_dict
Ejemplo n.º 10
0
def generate_define_table(filestring="", debug = False):
  """Reads in a module as a buffer and returns a dictionary of defines

  Generates a table of defines that can be used to resolve values.
  If all the defines cannot be evaluated directly by the
  current module then this will search all the included modules 

  Args:
    filestring: A buffer from the module's file

  Returns:
    A dictionary of defines

  Raises:
    PreProcessorError 
  """
  import saputils
  define_dict = {}
  #from a file string find all the defines and generate an entry into a dictionary
  filestring = saputils.remove_comments(filestring)
  str_list = filestring.splitlines()

  for item in str_list:
    item = item.strip()
    #look for include files
    if item.startswith("`include"):
      if debug:
        print "found an include: " + item
      #read int the include file, strip away the comments
      #then append everything to the end
      item = item.partition("`include")[2]
      item = item.strip()
      item = item.strip("\"")
      inc_file = saputils.find_rtl_file_location(item)
      if debug:
        print "include file location: " + inc_file

      #try and open the include file
      try:
        ifile = open(inc_file)
        fs = ifile.read()
        ifile.close()
      except:
        if item != "project_defines.v":
          raise PreProcessor("Error while attempting to the include file: %s" % inc_file)

      try:
        if debug:
          print "got the new file string"
        include_defines = generate_define_table(fs)
        if debug:
          print "after include_define"
          print "length of include defines: " + str(len(include_defines.keys()))
        for key in include_defines.keys():
          #append the values found in the include back in the local dictionary
          if debug:
            print "working on: " + key
          if (not define_dict.has_key(key)):
            define_dict[key] = include_defines[key]


        if debug:
          print "added new items onto the list"
#      except TypeError as terr:
#        print "Type Error: " + str(terr)
      except:
        if item != "project_defines.v":
          raise PreProcessorError("Error while processing: %s: %s" %(item, sys.exc_info()[0]))
          #print "error while processing : ", item, ": ",  sys.exc_info()[0]
      continue

    if item.startswith("`define"):
      #if the string starts with `define split the name and value into the dictionary
#      if debug:
#        print "found a define: " + item
      item = item.partition("`define")[2]
      item = item.strip()
      if (len(item.partition(" ")[2]) > 0):
        name = item.partition(" ")[0].strip()
        value = item.partition(" ")[2].strip()
        if debug:
          print "added " + name + "\n\tWith value: " + value
        define_dict[name] = value
        continue
      if (len(item.partition("\t")[2]) > 0):
        name = item.partition("\t")[0].strip()
        value = item.partition("\t")[2].strip()
        if debug:
          print "added " + name + "\n\tWith value: " + value
        define_dict[name] = value
        continue
      if debug:
        print "found a define without a value: " + item

  return define_dict