def __init__(self, bank: int = 0, register: int = 0, immediate: int = None, type: str = 'dec'): self.setValue(bank, register, immediate, type) self.processor = Processor()
def __init__(self): self.processor = Processor() self._TestInstructions = DataBank().getTestInstructions( ) # List of Instruction combos (TestInstruction) self._instructionsList = [ ] # List of Testinstr to run (randomized from _TestInstructions) self.max_level = int(self.processor.instance.register[REG_FILE_SIZE] ) - 2 # Num registers - 2 self.initRegCounter = 0
def _setInstructionAssembly(self): if self.mutable: self.overrideMandatoryFeatures() self._instruction = Processor().getInstructionAssemblyString(self.parsestring[0], self._enabledfeatures) if OPERANDS.BRANCH_INDEX.value in self._instruction: if OPERANDS.BRANCH_INDEX.value in self._operandAttr: self._instruction = self._instruction.replace(OPERANDS.BRANCH_INDEX.value, str(self._operandAttr[OPERANDS.BRANCH_INDEX.value])) n=3
def set_random_features(self, immediateProbability=0 ): # probability = prob of immediate features = Processor().getAvailableInstructionFeatures( self.instruction) # features = self._originalInstructions[0].get_features() # dict of features feature_stats = Processor().random_enabled_features( self.instruction, immediateProbability) self._enabledFeatures = feature_stats
def getFeatures(self): instructionName = None if len(self.parsestring) > 2: instructionName = self.inst_name try: features = Processor().getAvailableInstructionFeatures(instructionName) # overwrite mandatory features for feature in self.mandatoryEnabledFeatures: features[feature] = [Processor().getFeatureValue(feature, self.mandatoryEnabledFeatures[feature])] return features except KeyError: return None
def getAssemblerFeatures(self): features = Processor().getAvailableInstructionFeatures( self.instruction) result = {} for key in self._enabledFeatures: result[key] = features[key][self._enabledFeatures[key]] return result
def setEnableFeatureIndex(self, feature, index): """Set a Feature to a Index (integer position of feature in Available Features of the instruction). Be careful this function should only be used for debugging""" features = Processor().getAvailableInstructionFeatures( self.instruction) featureAssembly = features[feature][index] self._enabledFeatures[feature] = featureAssembly
class Operand: def __init__(self, bank: int = 0, register: int = 0, immediate: int = None, type: str = 'dec'): self.setValue(bank, register, immediate, type) self.processor = Processor() def string(self): if self._type == OPERAND_TYPE.Immediate: return '#' + str(self._immediate) elif self._type == OPERAND_TYPE.Register: reg_format = self.processor.get_register_rule()[FORMAT] reg_format = reg_format.replace('_regFile_', str(self._bank)) reg_format = reg_format.replace('_register_', str(self._register)) return reg_format def setValue(self, bank: int, register: int, immediate: int, type: str): if immediate is not None: self._type = OPERAND_TYPE.Immediate if type == 'dec': self._immediate = immediate elif type == 'bin': self._immediate = bin(immediate) elif type == 'hex': self._immediate = hex(immediate) else: self._type = OPERAND_TYPE.Register self._bank = bank self._register = register
def initialize(self): self.immediateAssembly = None file = open(Constants.PATH_INSTRUCTION) xml_content = xmltodict.parse(file.read()) for group in xml_content[Constants.INST_LIST]: if group == Constants.CONDITIONAL_READ: conditionVariants = xml_content[Constants.INST_LIST][Constants.CONDITIONAL_READ] for condition in conditionVariants: self.conditionalData[condition] = {} for conditionFlag in conditionVariants[condition]: self.conditionalData[condition][conditionFlag] ={} simd = Constants.vMutable if conditionVariants[condition][conditionFlag]: if Constants.INSTR in conditionVariants[condition][conditionFlag]: self.conditionalData[condition][conditionFlag][simd] = self._createInstructionList(conditionVariants[condition][conditionFlag][ Constants.INSTR]) n=3 elif Constants.REVERSE in conditionVariants[condition][conditionFlag]: self.conditionalData[condition][conditionFlag][simd] = self._createInstructionList( conditionVariants[condition][conditionFlag][ Constants.REVERSE]) n=3 else: for simdVariant in conditionVariants[condition][conditionFlag]: simdAssembly = Processor().getProcessorFeatureAssembly(Constants.SIMD, simdVariant) self.conditionalData[condition][conditionFlag][ simdAssembly] = self._createInstructionList( conditionVariants[condition][conditionFlag][simdVariant][ Constants.REVERSE]) # simd = # self.conditionalData[condition][conditionFlag] = ] n=3 n=3
def overrideMandatoryFeatures(self): for feature in self.mandatoryEnabledFeatures: self._enabledfeatures[feature] = Processor().getFeatureValue(feature, self.mandatoryEnabledFeatures[feature]) for key in self.globalMandatoryFeatures: if key in self._enabledfeatures and key in self.mandatoryEnabledFeatures: self._enabledfeatures[key] = self.globalMandatoryFeatures[key]
def _extractReverseSIMD(self, temp): reverse = temp[REVERSE] reverseDict = {} if isinstance(reverse, dict): simdVersions = Processor().getSIMD() for (simdKey, simdValue) in simdVersions.items(): if simdKey in reverse: reverseDict[simdValue] = [ reverse[simdKey][REVERSE] ] if isinstance(reverse[simdKey][REVERSE], str) else reverse[simdKey][REVERSE] else: reverseList = [reverse] if isinstance(reverse, str) else reverse reverseDict[vMutable] = reverseList return reverseDict
def isEnabled(self, enabledFlags): if Constants.CONDITIONAL in enabledFlags: enabledCondition = None != enabledFlags[Constants.CONDITIONAL] if enabledCondition: return Constants.CONDITIONAL_READ == Processor().getAssemlby2Feature(enabledFlags[Constants.CONDITIONAL]) else: return False else: return False
def setEnableFeature(self, feature, featureAssembly): """ Set a Feature to a Value. Value is based on writing in Processor description. Be careful this function should only be used for debugging :param feature: :param featureAssembly: :return: """ features = Processor().get_instr_features(self.instruction) self._enabledFeatures[feature] = featureAssembly
def _importSpecialization(self, specialization): self.specialization = {} if specialization is not None: for category in specialization: self.specialization[category] = {} for featureName in specialization[category]: # try: originalInstruction = [ specialization[category][featureName][INSTR] ] if isinstance( specialization[category][featureName][INSTR], str) else specialization[category][featureName][INSTR] # except: # n=3 reversiInstruction = {} if isinstance( specialization[category][featureName][REVERSE], dict): # if category == CONDITIONAL_READ: for simd in specialization[category][featureName][ REVERSE]: assemlbySIMD = Processor( ).getProcessorFeatureAssembly(SIMD, simd) reversiInstruction[assemlbySIMD] = [ specialization[category][featureName][REVERSE][simd][REVERSE]] if isinstance( specialization[category][featureName][REVERSE][simd][REVERSE], str) else \ specialization[category][featureName][REVERSE][simd][REVERSE] n = 3 else: reversiInstruction[vMutable] = [specialization[category][featureName][REVERSE]] if isinstance( specialization[category][featureName][REVERSE], str) else \ specialization[category][featureName][REVERSE] feature = Processor().getProcessorFeatureAssembly( category, featureName) self.specialization[category][feature] = TestInstruction( originalInstruction, reversiInstruction, self.instruction, {}, self.validInstructionList, self.immediateReverseAssembly) n = 3
def init_registers(self) -> str: # Initialize all Registers with random value defaultFeatures = Processor().getAvailableInstructionFeatures() enabledfeatures = { IMMEDIATE: Processor().getAvailableInstructionFeatures()[IMMEDIATE].index( IMMEDIATE_LONG) } reg_rule = self.processor.get_register_rule() max_reg_file = int(reg_rule[NUM_REG_FILES]) max_reg_size = int(reg_rule[REG_FILE_SIZE]) init_csv = DataBank().getInitRegister() code = '' for bank in range(max_reg_file + 1): for register in range(max_reg_size + 1): reg_operand = self.processor.create_register_operand( bank, register) for instr in init_csv: immediate = self.processor.createRandImmediate( IMMEDIATE_LONG) instr = instr.replace(TARGET_REGISTER, reg_operand) instr = instr.replace(RAND_VALUE, immediate) code += str(instr) code += '\n' code += "\n" if isinstance(DataBank().getPostInitCode(), str): postInit = DataBank().getPostInitCode() # code += DataBank().getPostInitCode() else: postInit = "\n".join(DataBank().getPostInitCode()) if OPERANDS.BRANCH_INDEX.value in postInit: postInit = postInit.replace(OPERANDS.BRANCH_INDEX.value, str(self.initRegCounter)) code += postInit code += "\n" code += "\n" self.initRegCounter += 1 return code
def _isReadConditionEnabled(self): if CONDITIONAL in self._enabledFeatures: foundCondition = None != self._enabledFeatures[CONDITIONAL] if foundCondition: return Processor().getAssemlby2Feature( self._enabledFeatures[CONDITIONAL]) == CONDITIONAL_READ else: return False else: return False
def createSingleInstructionTest(self, level=1, immediateProbability=0, singleInstruction=""): result = [] self.generate_instructions_list(level, singleInstruction) self.randomizeInstructionListFeatures(immediateProbability) for index in range(len(self._instructionsList)): testInstruction = self._instructionsList[index] v = Processor().getAvailableInstructionFeatures( testInstruction.getInstruction()) testInstruction.generateRandomOperands() code = self.init_registers() code += testInstruction.generateCode() code += testInstruction.generateReversiCode() code += self.generate_comparison_code(index, index) Processor().reset() result.append([testInstruction.getInstruction(), code]) return result
def _getSpecializationTestInstruction(self): instructionFeatures = Processor().getAvailableInstructionFeatures( self.instruction) for feature in self._enabledFeatures: if feature in self.specialization: if self._isEnabledFeature(feature): assemblerFeatureName = self._enabledFeatures[ feature] # Processor().getFeature(feature, ) specializedTestInstruction = self.specialization[feature][ assemblerFeatureName] return specializedTestInstruction
def create_interleavingInstructions(self, level: int = 1, immediateProbability=0): code = self.init_registers() # Initialize rand value in register testCode, instructionCount = self._create_interleavingInstructions( level, immediateProbability) code += testCode # reset processor for next independent execution self.resetInstructionList() Processor().reset() return code, instructionCount
def _enabledFeaturesUseSpecialization(self): if len(self.specialization.items()) == 0: return False if CONDITIONAL_READ in self.specialization: if Processor().getProcessorFeatureAssembly( CONDITIONAL, CONDITIONAL_READ) == self._enabledFeatures[CONDITIONAL]: return self._enabledFeatures[ CONDITIONAL_READ] in self.specialization[CONDITIONAL_READ] instructionFeatures = Processor().getAvailableInstructionFeatures( self.instruction) for feature in self._enabledFeatures: if self._isEnabledFeature(feature): if feature in self.specialization: assemblerFeatureName = self._enabledFeatures[ feature] # Processor().getFeature(feature, ) if assemblerFeatureName in instructionFeatures[feature]: return assemblerFeatureName in self.specialization[ feature] # return instructionFeatures[feature] in self.specialization[feature] return False
def create_singleInstructions(self, level: int = 1, immediateProbability=0): code = self.init_registers() self.generate_instructions_list(level) self.randomizeInstructionListFeatures(immediateProbability) self.generateRandomOperandsInstructionListFeatures() for index in range(len(self._instructionsList)): self._instructionsList[index].generateRandomOperands(None) code += self._instructionsList[index].generateCode() code += self._instructionsList[index].generateReversiCode() code += self.generate_comparison_code(index, index) Processor().reset() self.resetInstructionList() return code
def __init__(self, assembly: str, mutable=True): # set default enabled features self.isRandValueRandomImmediate = False self.globalMandatoryFeatures = {} self.mutable = mutable self._assembly = assembly self.parsestring = Processor().parseInstruction(assembly) self.mandatoryEnabledFeatures = Processor().getMandatoryFeatures(assembly) self.inst_name = self.parsestring[0] self.features = self.getFeatures() self._enabledfeatures = {} # self.reset_features() # Operands self._operandAttr = {} # TARGET_REGISTER, FOCUS_REGISTER, RAND_VALUE # parse assembly for return self._instruction = self.parsestring[0] self._operandStrings = self.parsestring[1] self._originalOperandString = copy.deepcopy(self.parsestring[1]) self.interleavingTargetRegister = None
def setFeatures(self, features): """ Reset all features and set them new. :param features: to set of the instruction. :return: """ self._enabledfeatures = {} availableFeatures = Processor().getAvailableInstructionFeatures(self.inst_name) # self._enabledfeatures = features for key in features: if key in availableFeatures: assemblyFeatureString = features[key] if assemblyFeatureString in availableFeatures[key]: self._enabledfeatures[key] = assemblyFeatureString self._setInstructionAssembly()
def __init__(self, originalInstructions: List[str], reverseInstructions: Dict[str, List[str]], instruction: str, specialization, validInstructionList, immediateReverseAssembly: List[Instruction]): self.instruction = instruction self.validInstructionList = validInstructionList self.immediateReverseAssembly = immediateReverseAssembly originalInstr = [] self.interleavingTargetRegister = None self.globalMandatoryFeatures = {} self.globalMandatoryFeatures[ISSUE_SLOT] = Processor( ).getRandomIssueSlot() for originalInstruction in originalInstructions: originalInstr.append(Instruction(originalInstruction)) self._originalInstructions = originalInstr # List of OriginalInstruction classes self._importReverseInstructions(reverseInstructions) self._importSpecialization(specialization) self.instructionCount = 0
def printdatabase(self): for k, v in self.instance._dict.items(): print("-------------- %s" % (k)) print(v) print(Processor().getAvailableInstructionFeatures(k)) # find TestInstruction for testInstr in self.instance.testinstruction_list: if k == testInstr.getInstruction(): # print Instructions for instruction in testInstr.getAssemblyInstructions(): print(instruction.getRawString()) print() for simdVariant in testInstr.getReversiAssemblyInstructions( ): print("SIMD: ", simdVariant) for instruction in testInstr.getReversiAssemblyInstructions( )[simdVariant]: print(instruction.getRawString()) print("\n\n")
def generateRandomOperands(self, FocusRegister=None): """ Get Random operands. :param FocusRegister: Provide a Focusregister, if it is empty, a random focusregister will be selected. :return: """ generateFocusRegister = True if FocusRegister is not None: generateFocusRegister = False self.operandAttributes = Processor().generateRandomOperands( self._enabledFeatures, generateFocusRegister, FocusRegister) if FocusRegister is not None: # self.focusReg = FocusRegister self.operandAttributes[FOCUS_REGISTER] = FocusRegister self._setOperationRegister(self.operandAttributes) self._setOperandsReversi()
def __parseInstructions(self, dictInstruction): result = [] instructions = dictInstruction if not isinstance(dictInstruction, list): instructions = [dictInstruction] # instructions = [dictInstruction] if isinstance(dictInstruction, str) else dictInstruction for instr in instructions: i = Instruction(instr) mandatoryFeatures = {IMMEDIATE: IMMEDIATE_LONG} if isinstance(instr, dict): if instr[MANDATORY_FEATURE]: mandatoryFeatures = {} for key, value in instr[MANDATORY_FEATURE].items(): mandatoryFeatures[key] = Processor( ).getProcessorFeatureList(key)[value] i.setGlobalMandatoryFeatures(mandatoryFeatures) result.append(i) return result
def _readImmediateAssembly(self): self.immediateAssembly = None file = open(Constants.PATH_INSTRUCTION) xml_content = xmltodict.parse(file.read()) for group in xml_content[INST_LIST]: if group == IMMEDIATE_ASSEMBLY: immidateVariants = xml_content[INST_LIST][ IMMEDIATE_ASSEMBLY] self.immediateAssembly = {} for imm in immidateVariants: assemlby = Processor().getProcessorFeatureAssembly( IMMEDIATE, imm) self.immediateAssembly[assemlby] = {} if INSTR in xml_content[INST_LIST][IMMEDIATE_ASSEMBLY][ imm]: instr = xml_content[INST_LIST][IMMEDIATE_ASSEMBLY][ imm][INSTR] instructions = self.__parseInstructions(instr) self.immediateAssembly[assemlby][ vMutable] = instructions else: for simd in xml_content[INST_LIST][ IMMEDIATE_ASSEMBLY][imm]: instr = xml_content[INST_LIST][ IMMEDIATE_ASSEMBLY][imm][simd][INSTR] instructions = self.__parseInstructions(instr) self.immediateAssembly[assemlby][ simd] = instructions # instruction = [instr] if isinstance(instr, str) else instr # for instr in instruction: # i = Instruction_v1(instr) # i.setGlobalMandatoryFeatures({IMMEDIATE: IMMEDIATE_LONG}) # self.immediateAssembly[assemlby].append(i) n = 3
def getOperandAssembly(self): # in processor isPragma = Processor().isPragma(self.inst_name) # v = Processor().getOperandAssembly(self.parsestring[1], self._operandAttr, self.interleavingTargetRegister, isPragma=isPragma) return Processor().getOperandAssembly(self.parsestring[1], self._operandAttr, self.interleavingTargetRegister, isPragma=isPragma, isRandValueRandomImmediate=self.isRandValueRandomImmediate)
class Instruction: def __init__(self, assembly: str, mutable=True): # set default enabled features self.isRandValueRandomImmediate = False self.globalMandatoryFeatures = {} self.mutable = mutable self._assembly = assembly self.parsestring = Processor().parseInstruction(assembly) self.mandatoryEnabledFeatures = Processor().getMandatoryFeatures(assembly) self.inst_name = self.parsestring[0] self.features = self.getFeatures() self._enabledfeatures = {} # self.reset_features() # Operands self._operandAttr = {} # TARGET_REGISTER, FOCUS_REGISTER, RAND_VALUE # parse assembly for return self._instruction = self.parsestring[0] self._operandStrings = self.parsestring[1] self._originalOperandString = copy.deepcopy(self.parsestring[1]) self.interleavingTargetRegister = None def getFeatures(self): instructionName = None if len(self.parsestring) > 2: instructionName = self.inst_name try: features = Processor().getAvailableInstructionFeatures(instructionName) # overwrite mandatory features for feature in self.mandatoryEnabledFeatures: features[feature] = [Processor().getFeatureValue(feature, self.mandatoryEnabledFeatures[feature])] return features except KeyError: return None def get_operand_attr(self): return self._operandAttr def getEnabledFeatures(self): return self._enabledfeatures def setFeatures(self, features): """ Reset all features and set them new. :param features: to set of the instruction. :return: """ self._enabledfeatures = {} availableFeatures = Processor().getAvailableInstructionFeatures(self.inst_name) # self._enabledfeatures = features for key in features: if key in availableFeatures: assemblyFeatureString = features[key] if assemblyFeatureString in availableFeatures[key]: self._enabledfeatures[key] = assemblyFeatureString self._setInstructionAssembly() def getOperandAssembly(self): # in processor isPragma = Processor().isPragma(self.inst_name) # v = Processor().getOperandAssembly(self.parsestring[1], self._operandAttr, self.interleavingTargetRegister, isPragma=isPragma) return Processor().getOperandAssembly(self.parsestring[1], self._operandAttr, self.interleavingTargetRegister, isPragma=isPragma, isRandValueRandomImmediate=self.isRandValueRandomImmediate) def __str__(self) -> str: self._setInstructionAssembly() return self._instruction + " " + self.getOperandAssembly() def string(self) -> str: return self.__str__() def getRawString(self): return self.inst_name + " " + self._originalOperandString def getName(self) -> str: return self.inst_name def getAssembly(self, enabledFeatures, globalMandatoryFeatures={}): self.setFeatures(enabledFeatures) self.setGlobalMandatoryFeatures(globalMandatoryFeatures) return self.string() def setEnabledFeatures(self, key: int, value: int): self._enabledfeatures[key] = value #if value in self.features[key]: # self._enabledfeatures[key] = value #else: # self._enabledfeatures[key] = None self._setInstructionAssembly() def resetFeatures(self): if self.features is None: return for key in self.features: self._enabledfeatures[key] = self.features[key][0] self._setInstructionAssembly() def _setInstructionAssembly(self): if self.mutable: self.overrideMandatoryFeatures() self._instruction = Processor().getInstructionAssemblyString(self.parsestring[0], self._enabledfeatures) if OPERANDS.BRANCH_INDEX.value in self._instruction: if OPERANDS.BRANCH_INDEX.value in self._operandAttr: self._instruction = self._instruction.replace(OPERANDS.BRANCH_INDEX.value, str(self._operandAttr[OPERANDS.BRANCH_INDEX.value])) n=3 def overrideMandatoryFeatures(self): for feature in self.mandatoryEnabledFeatures: self._enabledfeatures[feature] = Processor().getFeatureValue(feature, self.mandatoryEnabledFeatures[feature]) for key in self.globalMandatoryFeatures: if key in self._enabledfeatures and key in self.mandatoryEnabledFeatures: self._enabledfeatures[key] = self.globalMandatoryFeatures[key] def setGlobalMandatoryFeatures(self, globalMandatoryFeature): self.globalMandatoryFeatures = globalMandatoryFeature def __eq__(self, other): if type(self) != type(other): return False return self.__str__() == other.__str__() def setOperands(self, operands): self._operandAttr = operands def setOverrideTargetOperand(self, overRidingTargetOperand): self.interleavingTargetRegister = overRidingTargetOperand def enableRandValueRandomImmediate(self): self.isRandValueRandomImmediate = True