+/* Compute the alignment required by a type. */
+
+static int
+mn10300_type_align (struct type *type)
+{
+ int i, align = 1;
+
+ switch (TYPE_CODE (type))
+ {
+ 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:
+ 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_NFIELDS (type); i++)
+ {
+ int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));
+ while (align < falign)
+ align <<= 1;
+ }
+ return align;
+
+ case TYPE_CODE_ARRAY:
+ /* HACK! Structures containing arrays, even small ones, are not
+ elligible 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 (type))
+ {
+ case TYPE_CODE_STRUCT:
+ case TYPE_CODE_UNION:
+ /* Structures with a single field are handled as the field
+ itself. */
+ if (TYPE_NFIELDS (type) == 1)
+ return mn10300_use_struct_convention (TYPE_FIELD_TYPE (type, 0));
+
+ /* 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 void *valbuf)
+{
+ int len = TYPE_LENGTH (type);
+ int reg, regsz;
+
+ if (TYPE_CODE (type) == TYPE_CODE_PTR)
+ reg = 4;
+ else
+ reg = 0;
+
+ regsz = register_size (gdbarch, reg);
+
+ if (len <= regsz)
+ regcache_raw_write_part (regcache, reg, 0, len, valbuf);
+ else if (len <= 2 * regsz)
+ {
+ regcache_raw_write (regcache, reg, valbuf);
+ gdb_assert (regsz == register_size (gdbarch, reg + 1));
+ regcache_raw_write_part (regcache, reg+1, 0,
+ len - regsz, (char *) 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)
+{
+ char buf[MAX_REGISTER_SIZE];
+ int len = TYPE_LENGTH (type);
+ int reg, regsz;
+
+ if (TYPE_CODE (type) == TYPE_CODE_PTR)
+ reg = 4;
+ else
+ reg = 0;
+
+ regsz = register_size (gdbarch, reg);
+ if (len <= regsz)
+ {
+ regcache_raw_read (regcache, reg, buf);
+ memcpy (valbuf, buf, len);
+ }
+ else if (len <= 2 * regsz)
+ {
+ regcache_raw_read (regcache, reg, buf);
+ memcpy (valbuf, buf, regsz);
+ gdb_assert (regsz == register_size (gdbarch, reg + 1));
+ regcache_raw_read (regcache, reg + 1, buf);
+ memcpy ((char *) valbuf + regsz, buf, len - regsz);
+ }
+ else
+ internal_error (__FILE__, __LINE__,
+ _("Cannot extract return value %d bytes long."), len);
+}
+
+/* Determine, for architecture GDBARCH, how a return value of TYPE
+ should be returned. If it is supposed to be returned in registers,
+ and READBUF is non-zero, read the appropriate value from REGCACHE,
+ and copy it into READBUF. If WRITEBUF is non-zero, write the value
+ from WRITEBUF into REGCACHE. */
+
+static enum return_value_convention
+mn10300_return_value (struct gdbarch *gdbarch, struct type *func_type,
+ struct type *type, struct regcache *regcache,
+ gdb_byte *readbuf, const gdb_byte *writebuf)
+{
+ if (mn10300_use_struct_convention (type))
+ return RETURN_VALUE_STRUCT_CONVENTION;
+
+ if (readbuf)
+ mn10300_extract_return_value (gdbarch, type, regcache, readbuf);
+ if (writebuf)
+ mn10300_store_return_value (gdbarch, type, regcache, writebuf);