+/* Compute the alignment required by a type. */
+
+static int
+mn10300_type_align (struct type *type)
+{
+ int i, align = 1;
+
+ switch (type->code ())
+ {
+ case TYPE_CODE_INT:
+ case TYPE_CODE_ENUM:
+ case TYPE_CODE_SET:
+ case TYPE_CODE_RANGE:
+ case TYPE_CODE_CHAR:
+ case TYPE_CODE_BOOL:
+ case TYPE_CODE_FLT:
+ case TYPE_CODE_PTR:
+ case TYPE_CODE_REF:
+ case TYPE_CODE_RVALUE_REF:
+ return TYPE_LENGTH (type);
+
+ case TYPE_CODE_COMPLEX:
+ return TYPE_LENGTH (type) / 2;
+
+ case TYPE_CODE_STRUCT:
+ case TYPE_CODE_UNION:
+ for (i = 0; i < type->num_fields (); i++)
+ {
+ int falign = mn10300_type_align (type->field (i).type ());
+ while (align < falign)
+ align <<= 1;
+ }
+ return align;
+
+ case TYPE_CODE_ARRAY:
+ /* HACK! Structures containing arrays, even small ones, are not
+ eligible for returning in registers. */
+ return 256;
+
+ case TYPE_CODE_TYPEDEF:
+ return mn10300_type_align (check_typedef (type));
+
+ default:
+ internal_error (__FILE__, __LINE__, _("bad switch"));
+ }
+}
+
+/* Should call_function allocate stack space for a struct return? */
+static int
+mn10300_use_struct_convention (struct type *type)
+{
+ /* Structures bigger than a pair of words can't be returned in
+ registers. */
+ if (TYPE_LENGTH (type) > 8)
+ return 1;
+
+ switch (type->code ())
+ {
+ case TYPE_CODE_STRUCT:
+ case TYPE_CODE_UNION:
+ /* Structures with a single field are handled as the field
+ itself. */
+ if (type->num_fields () == 1)
+ return mn10300_use_struct_convention (type->field (0).type ());
+
+ /* Structures with word or double-word size are passed in memory, as
+ long as they require at least word alignment. */
+ if (mn10300_type_align (type) >= 4)
+ return 0;
+
+ return 1;
+
+ /* Arrays are addressable, so they're never returned in
+ registers. This condition can only hold when the array is
+ the only field of a struct or union. */
+ case TYPE_CODE_ARRAY:
+ return 1;
+
+ case TYPE_CODE_TYPEDEF:
+ return mn10300_use_struct_convention (check_typedef (type));
+
+ default:
+ return 0;
+ }
+}
+
+static void
+mn10300_store_return_value (struct gdbarch *gdbarch, struct type *type,
+ struct regcache *regcache, const gdb_byte *valbuf)
+{
+ int len = TYPE_LENGTH (type);
+ int reg, regsz;
+
+ if (type->code () == TYPE_CODE_PTR)
+ reg = 4;
+ else
+ reg = 0;
+
+ regsz = register_size (gdbarch, reg);
+
+ if (len <= regsz)
+ regcache->raw_write_part (reg, 0, len, valbuf);
+ else if (len <= 2 * regsz)
+ {
+ regcache->raw_write (reg, valbuf);
+ gdb_assert (regsz == register_size (gdbarch, reg + 1));
+ regcache->raw_write_part (reg + 1, 0, len - regsz, valbuf + regsz);
+ }
+ else
+ internal_error (__FILE__, __LINE__,
+ _("Cannot store return value %d bytes long."), len);
+}
+
+static void
+mn10300_extract_return_value (struct gdbarch *gdbarch, struct type *type,
+ struct regcache *regcache, void *valbuf)
+{
+ gdb_byte buf[MN10300_MAX_REGISTER_SIZE];
+ int len = TYPE_LENGTH (type);
+ int reg, regsz;
+
+ if (type->code () == TYPE_CODE_PTR)
+ reg = 4;
+ else
+ reg = 0;
+
+ regsz = register_size (gdbarch, reg);
+ gdb_assert (regsz <= MN10300_MAX_REGISTER_SIZE);
+ if (len <= regsz)
+ {
+ regcache->raw_read (reg, buf);
+ memcpy (valbuf, buf, len);
+ }
+ else if (len <= 2 * regsz)
+ {
+ regcache->raw_read (reg, buf);
+ memcpy (valbuf, buf, regsz);
+ gdb_assert (regsz == register_size (gdbarch, reg + 1));
+ regcache->raw_read (reg + 1, buf);
+ memcpy ((char *) valbuf + regsz, buf, len - regsz);
+ }
+ else
+ internal_error (__FILE__, __LINE__,
+ _("Cannot extract return value %d bytes long."), len);
+}