+/* Print target description in C. */
+
+class print_c_tdesc : public tdesc_element_visitor
+{
+public:
+ print_c_tdesc (std::string &filename_after_features)
+ : m_filename_after_features (filename_after_features)
+ {
+ const char *inp;
+ char *outp;
+ const char *filename = lbasename (m_filename_after_features.c_str ());
+
+ m_function = (char *) xmalloc (strlen (filename) + 1);
+ for (inp = filename, outp = m_function; *inp != '\0'; inp++)
+ if (*inp == '.')
+ break;
+ else if (*inp == '-')
+ *outp++ = '_';
+ else
+ *outp++ = *inp;
+ *outp = '\0';
+
+ /* Standard boilerplate. */
+ printf_unfiltered ("/* THIS FILE IS GENERATED. "
+ "-*- buffer-read-only: t -*- vi"
+ ":set ro:\n");
+ }
+
+ ~print_c_tdesc ()
+ {
+ xfree (m_function);
+ }
+
+ void visit_pre (const target_desc *e) override
+ {
+ printf_unfiltered (" Original: %s */\n\n",
+ lbasename (m_filename_after_features.c_str ()));
+
+ printf_unfiltered ("#include \"defs.h\"\n");
+ printf_unfiltered ("#include \"osabi.h\"\n");
+ printf_unfiltered ("#include \"target-descriptions.h\"\n");
+ printf_unfiltered ("\n");
+
+ printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
+ printf_unfiltered ("static void\n");
+ printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
+ printf_unfiltered ("{\n");
+ printf_unfiltered
+ (" struct target_desc *result = allocate_target_description ();\n");
+
+ if (tdesc_architecture (e) != NULL)
+ {
+ printf_unfiltered
+ (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
+ tdesc_architecture (e)->printable_name);
+ printf_unfiltered ("\n");
+ }
+ if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
+ && tdesc_osabi (e) < GDB_OSABI_INVALID)
+ {
+ printf_unfiltered
+ (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
+ gdbarch_osabi_name (tdesc_osabi (e)));
+ printf_unfiltered ("\n");
+ }
+
+ for (const bfd_arch_info_type *compatible : e->compatible)
+ printf_unfiltered
+ (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
+ compatible->printable_name);
+
+ if (!e->compatible.empty ())
+ printf_unfiltered ("\n");
+
+ for (const property &prop : e->properties)
+ printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
+ prop.key.c_str (), prop.value.c_str ());
+
+ printf_unfiltered (" struct tdesc_feature *feature;\n");
+ }
+
+ void visit_pre (const tdesc_feature *e) override
+ {
+ printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
+ e->name.c_str ());
+ }
+
+ void visit_post (const tdesc_feature *e) override
+ {}
+
+ void visit_post (const target_desc *e) override
+ {
+ printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
+ printf_unfiltered ("}\n");
+ }
+
+ void visit (const tdesc_type_builtin *type) override
+ {
+ error (_("C output is not supported type \"%s\"."), type->name.c_str ());
+ }
+
+ void visit (const tdesc_type_vector *type) override
+ {
+ if (!m_printed_element_type)
+ {
+ printf_unfiltered (" tdesc_type *element_type;\n");
+ m_printed_element_type = true;
+ }
+
+ printf_unfiltered
+ (" element_type = tdesc_named_type (feature, \"%s\");\n",
+ type->element_type->name.c_str ());
+ printf_unfiltered
+ (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
+ type->name.c_str (), type->count);
+
+ printf_unfiltered ("\n");
+ }
+
+ void visit (const tdesc_type_with_fields *type) override
+ {
+ if (!m_printed_type_with_fields)
+ {
+ printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
+ m_printed_type_with_fields = true;
+ }
+
+ switch (type->kind)
+ {
+ case TDESC_TYPE_STRUCT:
+ case TDESC_TYPE_FLAGS:
+ if (type->kind == TDESC_TYPE_STRUCT)
+ {
+ printf_unfiltered
+ (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
+ type->name.c_str ());
+ if (type->size != 0)
+ printf_unfiltered
+ (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
+ }
+ else
+ {
+ printf_unfiltered
+ (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
+ type->name.c_str (), type->size);
+ }
+ for (const tdesc_type_field &f : type->fields)
+ {
+ const char *type_name;
+
+ gdb_assert (f.type != NULL);
+ type_name = f.type->name.c_str ();
+
+ /* To minimize changes to generated files, don't emit type
+ info for fields that have defaulted types. */
+ if (f.start != -1)
+ {
+ gdb_assert (f.end != -1);
+ if (f.type->kind == TDESC_TYPE_BOOL)
+ {
+ gdb_assert (f.start == f.end);
+ printf_unfiltered
+ (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
+ f.start, f.name.c_str ());
+ }
+ else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
+ || (type->size == 8
+ && f.type->kind == TDESC_TYPE_UINT64))
+ {
+ printf_unfiltered
+ (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
+ f.name.c_str (), f.start, f.end);
+ }
+ else
+ {
+ printf_field_type_assignment
+ ("tdesc_named_type (feature, \"%s\");\n",
+ type_name);
+ printf_unfiltered
+ (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
+ " %d, %d, field_type);\n",
+ f.name.c_str (), f.start, f.end);
+ }
+ }
+ else /* Not a bitfield. */
+ {
+ gdb_assert (f.end == -1);
+ gdb_assert (type->kind == TDESC_TYPE_STRUCT);
+ printf_field_type_assignment
+ ("tdesc_named_type (feature, \"%s\");\n", type_name);
+ printf_unfiltered
+ (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
+ f.name.c_str ());
+ }
+ }
+ break;
+ case TDESC_TYPE_UNION:
+ printf_unfiltered
+ (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
+ type->name.c_str ());
+ for (const tdesc_type_field &f : type->fields)
+ {
+ printf_field_type_assignment
+ ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
+ printf_unfiltered
+ (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
+ f.name.c_str ());
+ }
+ break;
+ case TDESC_TYPE_ENUM:
+ printf_unfiltered
+ (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
+ type->name.c_str (), type->size);
+ for (const tdesc_type_field &f : type->fields)
+ printf_unfiltered
+ (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
+ f.start, f.name.c_str ());
+ break;
+ default:
+ error (_("C output is not supported type \"%s\"."), type->name.c_str ());
+ }
+
+ printf_unfiltered ("\n");
+ }
+
+ void visit (const tdesc_reg *reg) override
+ {
+ printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
+ reg->name.c_str (), reg->target_regnum,
+ reg->save_restore);
+ if (!reg->group.empty ())
+ printf_unfiltered ("\"%s\", ", reg->group.c_str ());
+ else
+ printf_unfiltered ("NULL, ");
+ printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
+ }
+
+protected:
+ std::string m_filename_after_features;
+
+private:
+
+ /* Print an assignment to the field_type variable. Print the declaration
+ of field_type if that has not been done yet. */
+ ATTRIBUTE_PRINTF (2, 3)
+ void printf_field_type_assignment (const char *fmt, ...)
+ {
+ if (!m_printed_field_type)
+ {
+ printf_unfiltered (" tdesc_type *field_type;\n");
+ m_printed_field_type = true;
+ }
+
+ printf_unfiltered (" field_type = ");
+
+ va_list args;
+ va_start (args, fmt);
+ vprintf_unfiltered (fmt, args);
+ va_end (args);
+ }
+
+ char *m_function;
+
+ /* Did we print "struct tdesc_type *element_type;" yet? */
+ bool m_printed_element_type = false;
+
+ /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
+ bool m_printed_type_with_fields = false;
+
+ /* Did we print "struct tdesc_type *field_type;" yet? */
+ bool m_printed_field_type = false;
+};
+
+/* Print target description feature in C. */
+
+class print_c_feature : public print_c_tdesc