def _generate_constraint_testers(decoder, values, out): """Generates the testers needed to implement the constraints associated with each row having a pattern. """ rows = _rows_to_test(decoder, values, with_rules=False) values['constraint_rows'] = _index_neutral_map(rows) for r in rows: _install_row_cases(r, values) row = _row_filter_interesting_patterns(r) action = _install_test_row(row, decoder, values) safety_to_check = _safety_to_check(action.safety()) defs_to_check = action.defs() out.write(CONSTRAINT_TESTER_CLASS_HEADER % values) if row.patterns or action.constraints().restrictions: out.write(CONSTRAINT_TESTER_RESTRICTIONS_HEADER % values); if safety_to_check or defs_to_check: out.write(CONSTRAINT_TESTER_SANITY_HEADER % values) out.write(CONSTRAINT_TESTER_CLASS_CLOSE % values) if row.patterns or action.constraints().restrictions: out.write(CONSTRAINT_TESTER_PARSE_HEADER % values) if row.patterns: out.write(ROW_CONSTRAINTS_HEADER % values); for p in row.patterns: not_p = p.negate() values['comment'] = dgen_output.commented_string(repr(not_p), ' ') values['code'] = not_p.to_bool() out.write(CONSTRAINT_CHECK % values) if action.constraints().restrictions: out.write(PATTERN_CONSTRAINT_RESTRICTIONS_HEADER) for c in action.constraints().restrictions: not_c = c.negate() values['comment'] = dgen_output.commented_string(repr(not_c), ' ') values['code'] = not_c.to_bool() out.write(CONSTRAINT_CHECK % values) out.write(CONSTRAINT_TESTER_CLASS_FOOTER % values) if safety_to_check or defs_to_check: out.write(SAFETY_TESTER_HEADER % values) for check in safety_to_check: values['comment'] = dgen_output.commented_string( repr(check), ' ') values['code'] = check.to_bool() out.write(SAFETY_TESTER_CHECK % values) if defs_to_check: values['comment'] = dgen_output.commented_string( repr(defs_to_check), ' ') values['code'] = defs_to_check.to_register_list() out.write(DEFS_SAFETY_CHECK % values) out.write(SAFETY_TESTER_FOOTER % values)
def _generate_constraint_testers(decoder, values, out): """Generates the testers needed to implement the constraints associated with each row having a pattern. """ rows = _rows_to_test(decoder, values) values['constraint_rows'] = _index_neutral_map(rows) for r in rows: _install_row_cases(r, values) row = _row_filter_interesting_patterns(r) action = _install_test_row(row, decoder, values) out.write(CONSTRAINT_TESTER_CLASS_HEADER % values) if _row_action_has_parse_restrictions(row, action): out.write(CONSTRAINT_TESTER_RESTRICTIONS_HEADER % values); out.write(CONSTRAINT_TESTER_CLASS_CLOSE % values) if _row_action_has_parse_restrictions(row, action): out.write(CONSTRAINT_TESTER_PARSE_HEADER % values) if row.patterns: out.write(ROW_CONSTRAINTS_HEADER % values); for p in row.patterns: not_p = p.negate() values['comment'] = dgen_output.commented_string(repr(not_p), ' ') values['code'] = not_p.to_bool() out.write(CONSTRAINT_CHECK % values) if _action_pattern_defines_condition(action): # Special case where 'cccc' part of pattern can't be 1111. out.write(CONSTRAINT_TESTER_COND_TEST % values) out.write(CONSTRAINT_TESTER_CLASS_FOOTER % values)
def _install_test_row(row, decoder, values): """Installs data associated with the given row into the values map.""" action = _filter_test_action(row.action) values['row_comment'] = dgen_output.commented_string( repr(row.copy_with_action(action))) _install_action(decoder, action, values) return action
def _DefineSafety(out, safety, values): # Use baseline to implement action if defined. _DefineMethod(out, DECODER_SAFETY_DEF_HEADER, values, '???') for s in safety: values['neutral_rep'] = dgen_output.commented_string( '%s' % s.neutral_repr(), ' ') values['safety_test'] = s.test().to_bool() values['safety_action'] = s.action() out.write(DECODER_SAFETY_DEF_CHECK % values) out.write(DECODER_SAFETY_DEF_FOOTER % values)
def _DefineCodeReplace(out, dyn_code, values): _DefineMethod(out, DECODER_CODE_REPLACE_DEF_HEADER, values, '???') for v in dyn_code.args(): bitfield = v.to_bitfield(v) values['neutral_rep'] = dgen_output.commented_string( bitfield.neutral_repr(), ' ') values['hi_bit'] = bitfield.hi() values['lo_bit'] = bitfield.lo() out.write(DECODER_CODE_REPLACE_DEF_UPDATE % values) out.write(DECODER_CODE_REPLACE_DEF_FOOTER % values)
def _install_test_row(row, decoder, values, with_patterns=False, with_rules=True): """Installs data associated with the given row into the values map. Installs the baseline class, rule name, and constraints associated with the row. If with_patterns is specified, then pattern information and actual class information is also inserted. """ action = _filter_test_action(row.action, with_patterns, with_rules) values['row_comment'] = dgen_output.commented_string( repr(row.copy_with_action(action))) _install_action(decoder, action, values) return action
def DefineDecoder(action, decoder_name, out): """Generates the C++ class definition for the given action. action: The decoder action to define. decoder_name: The name for the C++ decoder class. out: The output stream to write the class declation to. """ values = {'decoder_name': decoder_name} for method in METHODS: if _IsMethodDefined(action, method): method_body = _FindMethodBody(action, method) values['neutral_rep'] = ( '%s: %s' % (method, dgen_output.commented_string( repr(dgen_core.neutral_repr(method_body)), ' '))) DefineMethodFcn(method)(out, method_body, values)
def _DefineDiagnostics(out, violations, values): out.write(DIAGNOSTICS_HEADER % values) for v in violations: if not isinstance(v, dgen_core.Violation): continue values['neutral_rep'] = dgen_output.commented_string( '%s' % v.neutral_repr(), ' ') values['violation_test'] = v.test().to_bool() out.write(DIAGNOSTICS_ADD_HEADER % values) is_first = True for arg in v.print_args(): if is_first: values['printf_arg'] = arg.to_cstring() is_first = False else: values['printf_arg'] = arg.to_uint32() out.write(DIAGNOSTICS_ADD_PRINTF_ARG % values) out.write(DIAGNOSTICS_ADD_FOOTER % values) out.write(DIAGNOSTICS_FOOTER % values)
def _DefineViolations(out, violations, values): out.write(VIOLATIONS_HEADER % values) is_first_violation = True for v in violations: if isinstance(v, dgen_core.Implicit): if 'is_literal_pool_head' in v.methods(): out.write(VIOLATIONS_POOL % values) if 'target' in v.methods(): out.write(VIOLATIONS_TARGET % values) if 'relative' in v.methods(): out.write(VIOLATIONS_RELATIVE % values) if 'base' in v.methods(): out.write(VIOLATIONS_BASE % values) continue assert isinstance(v, dgen_core.Violation) if is_first_violation: is_first_violation = False out.write(VIOLATIONS_SUBHEADER % values) values['neutral_rep'] = dgen_output.commented_string( '%s' % v.neutral_repr(), ' ') values['violation_test'] = v.test().to_bool() out.write(VIOLATIONS_CHECK % values) out.write(VIOLATIONS_FOOTER % values)
def commented_decoder_neutral_repr(decoder): """Returns a C++ comment describing the neutral representation of the given decoder.""" return dgen_output.commented_string( decoder.neutral_repr().replace('= ', ' ', 1))