def __init__(self, path: str, encoding_schemes: Optional[EncSchemes], yml: object) -> None: yd = check_keys(yml, 'insn-group', ['key', 'title', 'doc', 'insns'], []) self.key = check_str(yd['key'], 'insn-group key') self.title = check_str(yd['title'], 'insn-group title') self.doc = check_str(yd['doc'], 'insn-group doc') insns_what = 'insns field for {!r} instruction group'.format(self.key) insns_rel_path = check_str(yd['insns'], insns_what) insns_path = os.path.normpath( os.path.join(os.path.dirname(path), insns_rel_path)) insns_yaml = load_yaml(insns_path, insns_what) try: self.insns = [ Insn(i, encoding_schemes) for i in check_list(insns_yaml, insns_what) ] except ValueError as err: raise RuntimeError( 'Invalid schema in YAML file at {!r}: {}'.format( insns_path, err)) from None
def __init__(self, yml: object, mnemonic: str, insn_encoding: Optional[Encoding]) -> None: # The YAML representation should be a string (a bare operand name) or a # dict. what = 'operand for {!r} instruction'.format(mnemonic) if isinstance(yml, str): name = yml.lower() abbrev = None op_type = None doc = None pc_rel = False op_what = '{!r} {}'.format(name, what) elif isinstance(yml, dict): yd = check_keys(yml, what, ['name'], ['type', 'pc-rel', 'doc', 'abbrev']) name = check_str(yd['name'], 'name of ' + what).lower() op_what = '{!r} {}'.format(name, what) abbrev = get_optional_str(yd, 'abbrev', op_what) if abbrev is not None: abbrev = abbrev.lower() op_type = get_optional_str(yd, 'type', op_what) pc_rel = check_bool(yd.get('pc-rel', False), 'pc-rel field of ' + op_what) doc = get_optional_str(yd, 'doc', op_what) # If there is an encoding, look up the encoding scheme field that # corresponds to this operand. enc_scheme_field = None if insn_encoding is not None: field_name = insn_encoding.op_to_field_name.get(name) if field_name is None: raise ValueError('The {!r} instruction has an operand called ' '{!r}, but the associated encoding has no ' 'field that encodes it.'.format( mnemonic, name)) enc_scheme_field = insn_encoding.fields[field_name].scheme_field if abbrev is not None: if name == abbrev: raise ValueError('Operand {!r} of the {!r} instruction has ' 'an abbreviated name the same as its ' 'actual name.'.format(name, mnemonic)) self.name = name self.abbrev = abbrev self.op_type = make_operand_type(op_type, pc_rel, name, mnemonic, enc_scheme_field) self.doc = doc
def _parse_iflow_test(test_yml: object, what: str, operands: List[Operand]) -> InsnInformationFlowTest: '''Parses an item in the "test" list of a YAML information-flow rule. Tests are expected to be strings of the form "<operand> <comparison> <value>", where: - <operand> is the name of one of the instruction's operands - <comparison> is "==", "!=", ">=", or "<=" - <value> is an integer that is within the allowed range of values for this operand (for instance, for a flag group it would be 0 or 1, and for a register it would be between 0 and 31). Returns a function that takes operand values as input and returns true if the test passes. ''' test = check_str(test_yml, what) test = test.lower() test_split = test.split(' ') if len(test_split) != 3: raise ValueError( 'Invalid information flow test format (expected "<operand> ' '<comparison> <value>"): got {} (for {})'.format(test, what)) opname, comparison, value_str = test_split opnames = [op.name for op in operands] if opname not in opnames: raise ValueError( 'Invalid information flow test format for {}: operand {} not ' 'found in operands list: {}'.format(what, opname, opnames)) try: value = int(value_str, 0) except ValueError: raise ValueError( 'Value {} in test {} for {} must be an integer.'.format( value_str, test, what)) constructors = { '==': EqTest, '!=': NotEqTest, '>=': GeqTest, '<=': LeqTest, } constructor = constructors.get(comparison, None) if constructor is None: raise ValueError('Unrecognized comparison {} for {}'.format( comparison, what)) return constructor(test, opname, value)
def __init__(self, yml: object, importer_name: str) -> None: as_str = check_str( yml, 'value for import in encoding scheme {!r}'.format(importer_name)) # The supported syntax is # # - parent0(field0=b111, field1=b10) # - parent1() # - parent2 match = re.match(r'([^ (]+)[ ]*(?:\(([^)]+)\))?$', as_str) if not match: raise ValueError('Malformed encoding scheme ' 'inheritance by scheme {!r}: {!r}.'.format( importer_name, as_str)) self.parent = match.group(1) self.settings = {} # type: Dict[str, BoolLiteral] when = ('When inheriting from {!r} in encoding scheme {!r}'.format( self.parent, importer_name)) if match.group(2) is not None: args = match.group(2).split(',') for arg in args: arg = arg.strip() arg_parts = arg.split('=') if len(arg_parts) != 2: raise ValueError( '{}, found an argument with {} ' 'equals signs (should have exactly one).'.format( when, len(arg_parts) - 1)) field_name = arg_parts[0] field_what = ('literal value for field {!r} when inheriting ' 'from {!r} in encoding scheme {!r}'.format( arg_parts[0], self.parent, importer_name)) field_value = BoolLiteral.from_string(arg_parts[1], field_what) if field_name in self.settings: raise ValueError('{}, found multiple arguments assigning ' 'values to the field {!r}.'.format( when, field_name)) self.settings[field_name] = field_value
def __init__(self, item_num: int, yml: object): if isinstance(yml, str): cfgs = yml weight = 1.0 else: yd = check_keys(yml, 'parent', ['cfgs'], ['weight']) cfgs = check_str(yd['cfgs'], 'cfgs field for inheritance list {}' .format(item_num)) yw = yd.get('weight', 1.0) if isinstance(yw, float) or isinstance(yw, int): weight = float(yw) elif isinstance(yw, str): try: weight = float(yw) except ValueError: raise ValueError('The weight in inheritance list {} is ' '{!r}, not a valid float.' .format(item_num, yw)) else: raise ValueError('The weight in inheritance list {} is ' 'not a number or a string.' .format(item_num)) if weight <= 0: raise ValueError('The weight in inheritance list {} is ' '{}, which is not positive.' .format(item_num, weight)) # cfgs should be a nonempty list of config names, separated by '+' # signs. if not cfgs: raise ValueError('Empty list of config names') self.names = cfgs.split('+') self.weight = weight # Check that each of the names in our list is nonempty (we'll get a # less confusing error if we spot that here) for n in self.names: if not n: raise ValueError('Empty name in list of config names: {}' .format(self.names))
def __init__(self, yml: object, encoding_schemes: Optional[EncSchemes]) -> None: yd = check_keys(yml, 'instruction', ['mnemonic', 'operands'], [ 'group', 'rv32i', 'synopsis', 'syntax', 'doc', 'errs', 'note', 'encoding', 'glued-ops', 'literal-pseudo-op', 'python-pseudo-op', 'lsu', 'straight-line' ]) self.mnemonic = check_str(yd['mnemonic'], 'mnemonic for instruction') what = 'instruction with mnemonic {!r}'.format(self.mnemonic) encoding_yml = yd.get('encoding') self.encoding = None if encoding_yml is not None: if encoding_schemes is None: raise ValueError( '{} specifies an encoding, but the file ' 'didn\'t specify any encoding schemes.'.format(what)) self.encoding = Encoding(encoding_yml, encoding_schemes, self.mnemonic) self.operands = [ Operand(y, self.mnemonic, self.encoding) for y in check_list(yd['operands'], 'operands for ' + what) ] self.name_to_operand = index_list('operands for ' + what, self.operands, lambda op: op.name) # The call to index_list has checked that operand names are distinct. # We also need to check that no operand abbreviation clashes with # anything else. operand_names = set(self.name_to_operand.keys()) for op in self.operands: if op.abbrev is not None: if op.abbrev in operand_names: raise ValueError('The name {!r} appears as an operand or ' 'abbreviation more than once for ' 'instruction {!r}.'.format( op.abbrev, self.mnemonic)) operand_names.add(op.abbrev) if self.encoding is not None: # If we have an encoding, we passed it to the Operand constructors # above. This ensured that each operand has a field. However, it's # possible that there are some operand names the encoding mentions # that don't actually have an operand. Check for that here. missing_ops = (set(self.encoding.op_to_field_name.keys()) - set(self.name_to_operand.keys())) if missing_ops: raise ValueError('Encoding scheme for {} specifies ' 'some non-existent operands: {}.'.format( what, ', '.join(list(missing_ops)))) self.rv32i = check_bool(yd.get('rv32i', False), 'rv32i flag for ' + what) self.glued_ops = check_bool(yd.get('glued-ops', False), 'glued-ops flag for ' + what) self.synopsis = get_optional_str(yd, 'synopsis', what) self.doc = get_optional_str(yd, 'doc', what) self.note = get_optional_str(yd, 'note', what) self.errs = None if 'errs' in yd: errs_what = 'errs field for ' + what y_errs = check_list(yd.get('errs'), errs_what) self.errs = [] for idx, err_desc in enumerate(y_errs): self.errs.append( check_str(err_desc, 'element {} of the {}'.format(idx, errs_what))) raw_syntax = get_optional_str(yd, 'syntax', what) if raw_syntax is not None: self.syntax = InsnSyntax.from_yaml(self.mnemonic, raw_syntax.strip()) else: self.syntax = InsnSyntax.from_list( [op.name for op in self.operands]) pattern, op_to_grp = self.syntax.asm_pattern() self.asm_pattern = re.compile(pattern) self.pattern_op_to_grp = op_to_grp # Make sure we have exactly the operands we expect. if set(self.name_to_operand.keys()) != self.syntax.op_set: raise ValueError("Operand syntax for {!r} doesn't have the " "same list of operands as given in the " "operand list. The syntax uses {}, " "but the list of operands gives {}.".format( self.mnemonic, list(sorted(self.syntax.op_set)), list(sorted(self.name_to_operand)))) self.python_pseudo_op = check_bool(yd.get('python-pseudo-op', False), 'python-pseudo-op flag for ' + what) if self.python_pseudo_op and self.encoding is not None: raise ValueError('{} specifies an encoding and also sets ' 'python-pseudo-op.'.format(what)) lpo = yd.get('literal-pseudo-op') if lpo is None: self.literal_pseudo_op = None else: lpo_lst = check_list(lpo, 'literal-pseudo-op flag for ' + what) for idx, item in enumerate(lpo_lst): if not isinstance(item, str): raise ValueError( 'Item {} of literal-pseudo-op list for ' '{} is {!r}, which is not a string.'.format( idx, what, item)) self.literal_pseudo_op = cast(Optional[List[str]], lpo_lst) if self.python_pseudo_op: raise ValueError('{} specifies both python-pseudo-op and ' 'literal-pseudo-op.'.format(what)) if self.encoding is not None: raise ValueError('{} specifies both an encoding and ' 'literal-pseudo-op.'.format(what)) lsu_yaml = yd.get('lsu', None) if lsu_yaml is None: self.lsu = None else: self.lsu = LSUDesc.from_yaml(lsu_yaml, 'lsu field for {}'.format(what)) for idx, op_name in enumerate(self.lsu.target): if op_name not in self.name_to_operand: raise ValueError('element {} of the target for the lsu ' 'field for {} is {!r}, which is not a ' 'operand name of the instruction.'.format( idx, what, op_name)) self.straight_line = yd.get('straight-line', True)