Exemplo n.º 1
0
	def parseInstructions(self):

		# Read line by line in file, stripping the newline character
		lines = [line.strip() for line in self.inputFile]

		# Iterate line by line
		for line in lines:

			# Hexadecimal operand/imm?
			ishex = False
			if("0x" in line):
				ishex = True

			# Find assembly instructions (ignoring whitespace lines, C-code and assembler directives)
			# Normal instruction
			#if('.rodata:' in line or 'Disassembly of section .data:' in line): # hycheah -- commented out ???
			if('.rodata:' in line or '.data:' in line):
				self.parseDataMem = True
			if(self.parseDataMem == False):
				# Look for <main> start address
				if(self.foundMain == False):
					match = re.match('([0-9a-fA-F]+)' + '\s+' + '<main>:', line)
					# Found the <main> start address
					if match:
						self.mainAddr = int(match.group(1),16)
						self.foundMain = True
				match = re.match('([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+' + '(\w+)' + '\s+' + '((\w+)(,\s*-?\w+)*)(\([a-zA-Z0-9_]+\))*', line)
				if match:
					operands = [x.strip() for x in match.group(4).split(',')]
					mnemonic = match.group(3)
					# If hex operand, convert to decimal
					if(ishex or mnemonic in ['j', 'jal', 'bne', 'beq', 'bltz', 'bgtz', 'bnez', 'beqz', 'blez', 'bgez']) :
						operands[-1] = str(int(str(operands[-1]), 16))
					self.instructions.append(elf32instr('', match.group(1), match.group(2), match.group(3), len(operands), operands, match.group(7)))
					continue
				# NOPs and instructions without operands
				match = re.match('([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+' + '(\w+)' + '\s?', line)
				if match:
					self.instructions.append(elf32instr('', match.group(1), match.group(2), match.group(3), 0, [], None))	
			# Parse datamemory
			elif(self.parseDataMem == True):
				match = re.match('([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+?' + '(\w+)?' + '\s+?' + '((\w+)(,\s*-?\w+)*)(\([a-zA-Z0-9_]+\))*', line)
				if match:
					self.dataMemory[int(str(match.group(1)),16)] = int(str(match.group(2)),16)

			# Debug -- hycheah
			#for key in self.dataMemory:
			#	print "HY", key, self.dataMemory[key]
		return
Exemplo n.º 2
0
    def parseInstructions(self):

        # Read line by line in file, stripping the newline character
        lines = [line.strip() for line in self.inputFile]

        # List of core instructions
        self.CCoreInstr = []
        self.coreInstr = False

        # Iterate line by line
        for line in lines:

            # Hexadecimal operand/imm?
            ishex = False
            if ("0x" in line):
                ishex = True

            # Find assembly instructions (ignoring whitespace lines, C-code and assembler directives)
            # Normal instruction
            if ('.rodata:' in line or 'Disassembly of section .data:' in line):
                self.parseDataMem = True
            # Mark "computation core" instructions from now on (after START_CCORE flag)
            if ('START_CCORE' in line and self.coreInstr is False):
                print "STARTING CORE CYCLE COUNT"
                self.coreInstr = True
            # Quit marking instructions as core instructions (after END_CCORE flag)
            if ('END_CCORE' in line and self.coreInstr is True):
                print "ENDING CORE CYCLE COUNT"
                self.coreInstr = False

            # Parse Instruction Memory
            if (self.parseDataMem == False):
                # Look for <main> start address
                if (self.foundMain == False):
                    match = re.match('([0-9a-fA-F]+)' + '\s+' + '<main>:',
                                     line)
                    # Found the <main> start address
                    if match:
                        self.mainAddr = int(match.group(1), 16)
                        self.foundMain = True
                match = re.match(
                    '([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+' +
                    '(\w+)' + '\s+' +
                    '((\w+)(,\s*-?\w+)*)(\([a-zA-Z0-9_]+\))*', line)
                if match:
                    operands = [x.strip() for x in match.group(4).split(',')]
                    mnemonic = match.group(3)
                    # If hex operand, convert to decimal
                    if (ishex or mnemonic in [
                            'j', 'jal', 'bne', 'beq', 'bltz', 'bgtz', 'bnez',
                            'beqz', 'blez', 'bgez'
                    ]):
                        operands[-1] = str(int(str(operands[-1]), 16))
                    self.instructions.append(
                        elf32instr('', match.group(1), match.group(2),
                                   match.group(3), len(operands), operands,
                                   match.group(7)))
                    self.CCoreInstr.append(self.coreInstr)
                    continue
                # NOPs and instructions without operands
                match = re.match(
                    '([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+' +
                    '(\w+)' + '\s?', line)
                if match:
                    self.instructions.append(
                        elf32instr('', match.group(1), match.group(2),
                                   match.group(3), 0, [], None))
                    self.CCoreInstr.append(self.coreInstr)

            # Parse Data Memory
            elif (self.parseDataMem == True):
                match = re.match(
                    '([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' +
                    '\s+?' + '(\w+)?' + '\s+?' +
                    '((\w+)(,\s*-?\w+)*)(\([a-zA-Z0-9_]+\))*', line)
                if match:
                    self.dataMemory[int(str(match.group(1)),
                                        16)] = int(str(match.group(2)), 16)
        return
Exemplo n.º 3
0
	def parseInstructions(self):

		# Read line by line in file, stripping the newline character
		lines = [line.strip() for line in self.inputFile]

		# List of core instructions
		self.CCoreInstr = []
		self.coreInstr = False

		# Iterate line by line
		for line in lines:

			# Hexadecimal operand/imm?
			ishex = False
			if("0x" in line):
				ishex = True

			# Find assembly instructions (ignoring whitespace lines, C-code and assembler directives)
			# Normal instruction
			if('.rodata:' in line or 'Disassembly of section .data:' in line):
				self.parseDataMem = True
			# Mark "computation core" instructions from now on (after START_CCORE flag)
			if('START_CCORE' in line and self.coreInstr is False):
				print "STARTING CORE CYCLE COUNT"
				self.coreInstr = True
			# Quit marking instructions as core instructions (after END_CCORE flag)
			if('END_CCORE' in line and self.coreInstr is True):
				print "ENDING CORE CYCLE COUNT"
				self.coreInstr = False

			# Parse Instruction Memory
			if(self.parseDataMem == False):
				# Look for <main> start address
				if(self.foundMain == False):
					match = re.match('([0-9a-fA-F]+)' + '\s+' + '<main>:', line)
					# Found the <main> start address
					if match:
						self.mainAddr = int(match.group(1),16)
						self.foundMain = True
				match = re.match('([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+' + '(\w+)' + '\s+' + '((\w+)(,\s*-?\w+)*)(\([a-zA-Z0-9_]+\))*', line)
				if match:
					operands = [x.strip() for x in match.group(4).split(',')]
					mnemonic = match.group(3)
					# If hex operand, convert to decimal
					if(ishex or mnemonic in ['j', 'jal', 'bne', 'beq', 'bltz', 'bgtz', 'bnez', 'beqz', 'blez', 'bgez']) :
						operands[-1] = str(int(str(operands[-1]), 16))
					self.instructions.append(elf32instr('', match.group(1), match.group(2), match.group(3), len(operands), operands, match.group(7)))
					self.CCoreInstr.append(self.coreInstr)
					continue
				# NOPs and instructions without operands
				match = re.match('([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+' + '(\w+)' + '\s?', line)
				if match:
					self.instructions.append(elf32instr('', match.group(1), match.group(2), match.group(3), 0, [], None))
					self.CCoreInstr.append(self.coreInstr)
			
			# Parse Data Memory
			elif(self.parseDataMem == True):
				match = re.match('([0-9a-fA-F]+)' + ':' + '\s+' + '([0-9a-fA-F]+)' + '\s+?' + '(\w+)?' + '\s+?' + '((\w+)(,\s*-?\w+)*)(\([a-zA-Z0-9_]+\))*', line)
				if match:
					self.dataMemory[int(str(match.group(1)),16)] = int(str(match.group(2)),16)
		return