3 /* ddt_remote - Stuff needed to get datadescs about remote hosts */
5 /* Copyright (c) 2003 Olivier Aumage. */
6 /* Copyright (c) 2003, 2004 Martin Quinson. */
7 /* All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
12 /************************************************************************/
13 /* C combines the power of assembler with the portability of assembler. */
14 /************************************************************************/
16 #include "gras/DataDesc/datadesc_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_convert, gras_ddt,
19 "Inter-architecture convertions");
22 *** Table of all known architectures:
24 l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=0; gras_arch_name=little32;;
25 l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=1; gras_arch_name=little32_4;;
27 l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=2; gras_arch_name=little64;;
29 B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=3; gras_arch_name=big32;;
30 B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=4; gras_arch_name=big32_8_4;;
31 B_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=5; gras_arch_name=big32_4;;
32 B_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:) gras_arch=6; gras_arch_name=big32_2;;
34 B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=7; gras_arch_name=big64;;
38 const gras_arch_desc_t gras_arches[gras_arch_count] = {
40 {"little32_1", 0, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* little endian, 1 byte alignement (win32) */
41 {1, 1, 1, 1, 1, 1, 1, 1, 1}},
43 {"little32_2", 0, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* little endian, 2 bytes alignements (win32) */
44 {1, 2, 2, 2, 2, 2, 2, 2, 2}},
46 {"little32_4", 0, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* little endian, 4 bytes alignements (win32 and linux x86) */
47 {1, 2, 4, 4, 4, 4, 4, 4, 4}},
49 {"little32_8", 0, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* little endian, 8 bytes alignement (win32) */
50 {1, 2, 4, 4, 8, 4, 4, 4, 8}},
52 {"little64", 0, {1, 2, 4, 8, 8, 8, 8, 4, 8}, /* alpha, ia64 */
53 {1, 2, 4, 8, 8, 8, 8, 4, 8}},
55 {"big32_8", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8},
56 {1, 2, 4, 4, 8, 4, 4, 4, 8}},
58 {"big32_8_4", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* AIX */
59 {1, 2, 4, 4, 8, 4, 4, 4, 4}},
61 {"big32_4", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* G5 */
62 {1, 2, 4, 4, 4, 4, 4, 4, 4}},
64 {"big32_2", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* ARM */
65 {1, 2, 2, 2, 2, 2, 2, 2, 2}},
67 {"big64", 1, {1, 2, 4, 8, 8, 8, 8, 4, 8}, /* sparc */
68 {1, 2, 4, 8, 8, 8, 8, 4, 8}},
70 {"big64_8_4", 1, {1, 2, 4, 8, 8, 8, 8, 4, 8}, /* aix with -maix64 */
71 {1, 2, 4, 8, 8, 8, 8, 4, 4}}
74 const char *gras_datadesc_arch_name(int code)
76 if (code < 0 || code >= gras_arch_count)
77 return "[unknown arch]";
78 return gras_arches[code].name;
83 * Local function doing the grunt work
85 static void gras_dd_reverse_bytes(void *to, const void *from, size_t length);
88 * gras_dd_convert_elm:
90 * Convert the element described by @type comming from architecture @r_arch.
91 * The data to be converted is stored in @src, and is to be stored in @dst.
92 * Both pointers may be the same location if no resizing is needed.
95 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
96 int r_arch, void *src, void *dst)
98 gras_dd_cat_scalar_t scal = type->category.scalar_data;
102 unsigned long r_size, l_size;
103 /* Hexadecimal displayer
110 xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
111 xbt_assert(r_arch != GRAS_THISARCH);
113 r_size = type->size[r_arch];
114 l_size = type->size[GRAS_THISARCH];
115 DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p", r_size, l_size, src, dst);
117 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian ? 'B' : 'l',
118 gras_arches[GRAS_THISARCH].endian ? 'B' : 'l');
120 if (r_size != l_size) {
121 for (cpt = 0, r_data = src, l_data = dst;
124 r_data = (char *) r_data + r_size,
125 l_data = (char *) l_data + l_size) {
128 fprintf(stderr,"r_data=");
129 for (cpt=0; cpt<r_size; cpt++) {
131 tester.c[0]= ((char*)r_data)[cpt];
132 fprintf(stderr,"\\%02x", tester.i);
134 fprintf(stderr,"\n");
137 /* Resize that damn integer, pal */
139 unsigned char *l_sign, *r_sign;
141 int sizeChange = l_size - r_size;
142 int lowOrderFirst = !gras_arches[r_arch].endian ||
143 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
145 DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
146 cpt, r_size, r_data, l_size, l_data);
147 xbt_assert0(r_data != l_data, "Impossible to resize in place");
149 if (sizeChange < 0) {
150 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
151 lowOrderFirst ? "lowOrderFirst" : "bigOrderFirst",
153 e_gras_dd_scalar_encoding_sint ? "signed" : "unsigned");
154 /* Truncate high-order bytes. */
156 gras_arches[r_arch].endian ? ((char *) r_data - sizeChange)
159 if (scal.encoding == e_gras_dd_scalar_encoding_sint) {
160 DEBUG0("This is signed");
161 /* Make sure the high order bit of r_data and l_data are the same */
162 l_sign = gras_arches[GRAS_THISARCH].endian
163 ? ((unsigned char *) l_data + l_size - 1)
164 : (unsigned char *) l_data;
165 r_sign = gras_arches[r_arch].endian
166 ? ((unsigned char *) r_data + r_size - 1)
167 : (unsigned char *) r_data;
168 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign, *l_sign);
170 if ((*r_sign > 127) != (*l_sign > 127)) {
178 DEBUG1("Extend %d bytes", sizeChange);
179 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
180 DEBUG0("This is signed");
181 padding = 0; /* pad unsigned with 0 */
185 gras_arches[r_arch].endian ? ((unsigned char *) r_data + r_size -
187 : (unsigned char *) r_data;
188 padding = (*r_sign > 127) ? 0xff : 0;
191 memset(l_data, padding, l_size);
192 memcpy(!gras_arches[r_arch].endian ? l_data
193 : ((char *) l_data + sizeChange), r_data, r_size);
196 fprintf(stderr,"r_data=");
197 for (cpt=0; cpt<r_size; cpt++) {
199 tester.c[0] = ((char*)r_data)[cpt];
200 fprintf(stderr,"\\%02x", tester.i);
202 fprintf(stderr,"\n");
204 fprintf(stderr,"l_data=");
205 for (cpt=0; cpt<l_size; cpt++) {
207 tester.c[0]= ((char*)l_data)[cpt];
208 fprintf(stderr,"\\%02x", tester.i);
209 } fprintf(stderr,"\n");
215 /* flip bytes if needed */
216 if (gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
217 (l_size * count) > 1) {
219 for (cpt = 0, r_data = dst, l_data = dst; cpt < count; cpt++, r_data = (char *) r_data + l_size, /* resizing already done */
220 l_data = (char *) l_data + l_size) {
222 DEBUG1("Flip elm %d", cpt);
223 gras_dd_reverse_bytes(l_data, r_data, l_size);
229 static void gras_dd_reverse_bytes(void *to, const void *from, size_t length)
233 const char *fromBegin;
238 for (fromBegin = (const char *) from,
239 fromEnd = fromBegin + length - 1,
240 toBegin = (char *) to,
241 toEnd = toBegin + length - 1;
242 fromBegin <= fromEnd; fromBegin++, fromEnd--, toBegin++, toEnd--) {
244 charBegin = *fromBegin;
254 * returns the ID of the architecture the process is running on
256 int gras_arch_selfid(void)
258 return GRAS_THISARCH;