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(ddt_convert,datadesc,
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] = {
39 {"little32", 0, {1,2,4,4,8, 4,4, 4,8}, /* win32 */
40 {1,2,4,4,8, 4,4, 4,8}},
42 {"little32_4", 0, {1,2,4,4,8, 4,4, 4,8}, /* linux x86 */
43 {1,2,4,4,4, 4,4, 4,4}},
45 {"little64", 0, {1,2,4,8,8, 8,8, 4,8}, /* alpha, ia64 */
46 {1,2,4,8,8, 8,8, 4,8}},
48 {"big32", 1, {1,2,4,4,8, 4,4, 4,8},
49 {1,2,4,4,8, 4,4, 4,8}},
51 {"big32_8_4", 1, {1,2,4,4,8, 4,4, 4,8}, /* AIX */
52 {1,2,4,4,8, 4,4, 4,4}},
54 {"big32_4", 1, {1,2,4,4,8, 4,4, 4,8}, /* G5 */
55 {1,2,4,4,4, 4,4, 4,4}},
57 {"big32_2", 1, {1,2,4,4,8, 4,4, 4,8}, /* ARM */
58 {1,2,2,2,2, 2,2, 2,2}},
60 {"big64", 1, {1,2,4,8,8, 8,8, 4,8}, /* sparc */
61 {1,2,4,8,8, 8,8, 4,8}},
63 {"big64_8_4", 1, {1,2,4,8,8, 8,8, 4,8}, /* aix with -maix64 */
64 {1,2,4,8,8, 8,8, 4,4}}
67 const char *gras_datadesc_arch_name(int code) {
68 if (code < 0 || code >= gras_arch_count)
69 return "[unknown arch]";
70 return gras_arches[code].name;
75 * Local function doing the grunt work
78 gras_dd_reverse_bytes(void *to,
83 * gras_dd_convert_elm:
85 * Convert the element described by @type comming from architecture @r_arch.
86 * The data to be converted is stored in @src, and is to be stored in @dst.
87 * Both pointers may be the same location if no resizing is needed.
90 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
92 void *src, void *dst) {
93 gras_dd_cat_scalar_t scal = type->category.scalar_data;
97 unsigned long r_size, l_size;
98 /* Hexadecimal displayer
105 xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
106 xbt_assert(r_arch != GRAS_THISARCH);
108 r_size = type->size[r_arch];
109 l_size = type->size[GRAS_THISARCH];
110 DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p",
111 r_size,l_size,src,dst);
113 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
114 gras_arches[GRAS_THISARCH].endian?'B':'l');
116 if(r_size != l_size) {
117 for(cpt = 0, r_data = src, l_data = dst;
120 r_data = (char *)r_data + r_size,
121 l_data = (char *)l_data + l_size) {
124 fprintf(stderr,"r_data=");
125 for (cpt=0; cpt<r_size; cpt++) {
127 tester.c[0]= ((char*)r_data)[cpt];
128 fprintf(stderr,"\\%02x", tester.i);
130 fprintf(stderr,"\n");
133 /* Resize that damn integer, pal */
135 unsigned char *l_sign, *r_sign;
137 int sizeChange = l_size - r_size;
138 int lowOrderFirst = !gras_arches[r_arch].endian ||
139 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
141 DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
142 cpt, r_size,r_data, l_size,l_data);
143 xbt_assert0(r_data != l_data, "Impossible to resize in place");
146 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
147 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
148 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
149 /* Truncate high-order bytes. */
151 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
155 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
156 DEBUG0("This is signed");
157 /* Make sure the high order bit of r_data and l_data are the same */
158 l_sign = gras_arches[GRAS_THISARCH].endian
159 ? ((unsigned char*)l_data + l_size - 1)
160 : (unsigned char*)l_data;
161 r_sign = gras_arches[r_arch].endian
162 ? ((unsigned char*)r_data + r_size - 1)
163 : (unsigned char*)r_data;
164 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
166 if ((*r_sign > 127) != (*l_sign > 127)) {
174 DEBUG1("Extend %d bytes", sizeChange);
175 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
176 DEBUG0("This is signed");
177 padding = 0; /* pad unsigned with 0 */
180 r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
181 : (unsigned char*)r_data;
182 padding = (*r_sign > 127) ? 0xff : 0;
185 memset(l_data, padding, l_size);
186 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
190 fprintf(stderr,"r_data=");
191 for (cpt=0; cpt<r_size; cpt++) {
193 tester.c[0] = ((char*)r_data)[cpt];
194 fprintf(stderr,"\\%02x", tester.i);
196 fprintf(stderr,"\n");
198 fprintf(stderr,"l_data=");
199 for (cpt=0; cpt<l_size; cpt++) {
201 tester.c[0]= ((char*)l_data)[cpt];
202 fprintf(stderr,"\\%02x", tester.i);
203 } fprintf(stderr,"\n");
209 /* flip bytes if needed */
210 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
211 (l_size * count) > 1) {
213 for(cpt = 0, r_data=dst, l_data=dst;
216 r_data = (char *)r_data + l_size, /* resizing already done */
217 l_data = (char *)l_data + l_size) {
219 DEBUG1("Flip elm %d",cpt);
220 gras_dd_reverse_bytes(l_data, r_data, l_size);
227 gras_dd_reverse_bytes(void *to,
232 const char *fromBegin;
237 for(fromBegin = (const char *)from,
238 fromEnd = fromBegin + length - 1,
239 toBegin = (char *)to,
240 toEnd = toBegin + length - 1;
242 fromBegin <= fromEnd;
244 fromBegin++, fromEnd--,
245 toBegin++, toEnd--) {
247 charBegin = *fromBegin;
257 * returns the ID of the architecture the process is running on
260 gras_arch_selfid(void) {
261 return GRAS_THISARCH;