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) {
75 if (code < 0 || code >= gras_arch_count)
76 return "[unknown arch]";
77 return gras_arches[code].name;
82 * Local function doing the grunt work
85 gras_dd_reverse_bytes(void *to,
90 * gras_dd_convert_elm:
92 * Convert the element described by @type comming from architecture @r_arch.
93 * The data to be converted is stored in @src, and is to be stored in @dst.
94 * Both pointers may be the same location if no resizing is needed.
97 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
99 void *src, void *dst) {
100 gras_dd_cat_scalar_t scal = type->category.scalar_data;
104 unsigned long r_size, l_size;
105 /* Hexadecimal displayer
112 xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
113 xbt_assert(r_arch != GRAS_THISARCH);
115 r_size = type->size[r_arch];
116 l_size = type->size[GRAS_THISARCH];
117 DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p",
118 r_size,l_size,src,dst);
120 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
121 gras_arches[GRAS_THISARCH].endian?'B':'l');
123 if(r_size != l_size) {
124 for(cpt = 0, r_data = src, l_data = dst;
127 r_data = (char *)r_data + r_size,
128 l_data = (char *)l_data + l_size) {
131 fprintf(stderr,"r_data=");
132 for (cpt=0; cpt<r_size; cpt++) {
134 tester.c[0]= ((char*)r_data)[cpt];
135 fprintf(stderr,"\\%02x", tester.i);
137 fprintf(stderr,"\n");
140 /* Resize that damn integer, pal */
142 unsigned char *l_sign, *r_sign;
144 int sizeChange = l_size - r_size;
145 int lowOrderFirst = !gras_arches[r_arch].endian ||
146 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
148 DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
149 cpt, r_size,r_data, l_size,l_data);
150 xbt_assert0(r_data != l_data, "Impossible to resize in place");
153 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
154 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
155 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
156 /* Truncate high-order bytes. */
158 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
162 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
163 DEBUG0("This is signed");
164 /* Make sure the high order bit of r_data and l_data are the same */
165 l_sign = gras_arches[GRAS_THISARCH].endian
166 ? ((unsigned char*)l_data + l_size - 1)
167 : (unsigned char*)l_data;
168 r_sign = gras_arches[r_arch].endian
169 ? ((unsigned char*)r_data + r_size - 1)
170 : (unsigned char*)r_data;
171 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
173 if ((*r_sign > 127) != (*l_sign > 127)) {
181 DEBUG1("Extend %d bytes", sizeChange);
182 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
183 DEBUG0("This is signed");
184 padding = 0; /* pad unsigned with 0 */
187 r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
188 : (unsigned char*)r_data;
189 padding = (*r_sign > 127) ? 0xff : 0;
192 memset(l_data, padding, l_size);
193 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
197 fprintf(stderr,"r_data=");
198 for (cpt=0; cpt<r_size; cpt++) {
200 tester.c[0] = ((char*)r_data)[cpt];
201 fprintf(stderr,"\\%02x", tester.i);
203 fprintf(stderr,"\n");
205 fprintf(stderr,"l_data=");
206 for (cpt=0; cpt<l_size; cpt++) {
208 tester.c[0]= ((char*)l_data)[cpt];
209 fprintf(stderr,"\\%02x", tester.i);
210 } fprintf(stderr,"\n");
216 /* flip bytes if needed */
217 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
218 (l_size * count) > 1) {
220 for(cpt = 0, r_data=dst, l_data=dst;
223 r_data = (char *)r_data + l_size, /* resizing already done */
224 l_data = (char *)l_data + l_size) {
226 DEBUG1("Flip elm %d",cpt);
227 gras_dd_reverse_bytes(l_data, r_data, l_size);
234 gras_dd_reverse_bytes(void *to,
239 const char *fromBegin;
244 for(fromBegin = (const char *)from,
245 fromEnd = fromBegin + length - 1,
246 toBegin = (char *)to,
247 toEnd = toBegin + length - 1;
249 fromBegin <= fromEnd;
251 fromBegin++, fromEnd--,
252 toBegin++, toEnd--) {
254 charBegin = *fromBegin;
264 * returns the ID of the architecture the process is running on
267 gras_arch_selfid(void) {
268 return GRAS_THISARCH;