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/4> P<4/4:4/4> D<4/4:8/4>
25 *** l C<1/1> I<2/2:4/4:8/8:8/8> P<4/4:4/4> D<4/4:8/8>
26 *** B C<1/1> I<2/2:4/4:4/8:8/8> P<4/4:4/4> D<4/4:8/4>
27 *** B C<1/1> I<2/2:4/8:8/8:8/8> P<4/4:4/4> D<4/4:8/4>
28 *** B C:1/1: I:2/2:4/4:4/4:8/8: P:4/4:4/4: D:4/4:8/4: AIX
29 *** B C:1/1: I:2/2:4/2:4/2:8/2: P:4/2:4/2: D:4/2:8/2: ARM
30 *** l C<1/1> I<2/2:4/4:4/4:8/8> P<4/4:4/4> D<4/4:8/8> win32
33 const gras_arch_desc_t gras_arches[gras_arch_count] = {
34 {"little32", 0, {1,2,4,4,8, 4,4, 4,8},
35 {1,2,4,4,4, 4,4, 4,4}},
37 {"little64", 0, {1,2,4,8,8, 8,8, 4,8},
38 {1,2,4,8,8, 8,8, 4,8}},
40 {"big32", 1, {1,2,4,4,8, 4,4, 4,8},
41 {1,2,4,4,8, 4,4, 4,8}},
43 {"big64", 1, {1,2,4,8,8, 8,8, 4,8},
44 {1,2,4,8,8, 8,8, 4,8}},
46 {"aix", 1, {1,2,4,4,8, 4,4, 4,8},
47 {1,2,4,4,8, 4,4, 4,4}},
49 {"arm", 1, {1,2,4,4,8, 4,4, 4,8},
50 {1,2,2,2,2, 2,2, 2,2}},
52 {"win32", 0, {1,2,4,4,8, 4,4, 4,8},
53 {1,2,4,4,8, 4,4, 4,8}},
55 {"g5", 1, {1,2,4,4,8, 4,4, 4,8},
56 {1,2,4,4,4, 4,4, 4,4}}
59 const char *gras_datadesc_arch_name(int code) {
60 if (code < 0 || code >= gras_arch_count)
61 return "[unknown arch]";
62 return gras_arches[code].name;
67 * Local function doing the grunt work
70 gras_dd_reverse_bytes(void *to,
75 * gras_dd_convert_elm:
77 * Convert the element described by @type comming from architecture @r_arch.
78 * The data to be converted is stored in @src, and is to be stored in @dst.
79 * Both pointers may be the same location if no resizing is needed.
82 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
84 void *src, void *dst) {
85 gras_dd_cat_scalar_t scal = type->category.scalar_data;
89 unsigned long r_size, l_size;
90 /* Hexadecimal displayer
97 xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
98 xbt_assert(r_arch != GRAS_THISARCH);
100 r_size = type->size[r_arch];
101 l_size = type->size[GRAS_THISARCH];
102 DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p",
103 r_size,l_size,src,dst);
105 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
106 gras_arches[GRAS_THISARCH].endian?'B':'l');
108 if(r_size != l_size) {
109 for(cpt = 0, r_data = src, l_data = dst;
112 r_data = (char *)r_data + r_size,
113 l_data = (char *)l_data + l_size) {
116 fprintf(stderr,"r_data=");
117 for (cpt=0; cpt<r_size; cpt++) {
119 tester.c[0]= ((char*)r_data)[cpt];
120 fprintf(stderr,"\\%02x", tester.i);
122 fprintf(stderr,"\n");
125 /* Resize that damn integer, pal */
127 unsigned char *l_sign, *r_sign;
129 int sizeChange = l_size - r_size;
130 int lowOrderFirst = !gras_arches[r_arch].endian ||
131 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
133 DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
134 cpt, r_size,r_data, l_size,l_data);
135 xbt_assert0(r_data != l_data, "Impossible to resize in place");
138 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
139 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
140 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
141 /* Truncate high-order bytes. */
143 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
147 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
148 DEBUG0("This is signed");
149 /* Make sure the high order bit of r_data and l_data are the same */
150 l_sign = gras_arches[GRAS_THISARCH].endian
151 ? ((unsigned char*)l_data + l_size - 1)
152 : (unsigned char*)l_data;
153 r_sign = gras_arches[r_arch].endian
154 ? ((unsigned char*)r_data + r_size - 1)
155 : (unsigned char*)r_data;
156 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
158 if ((*r_sign > 127) != (*l_sign > 127)) {
166 DEBUG1("Extend %d bytes", sizeChange);
167 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
168 DEBUG0("This is signed");
169 padding = 0; /* pad unsigned with 0 */
172 r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
173 : (unsigned char*)r_data;
174 padding = (*r_sign > 127) ? 0xff : 0;
177 memset(l_data, padding, l_size);
178 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
182 fprintf(stderr,"r_data=");
183 for (cpt=0; cpt<r_size; cpt++) {
185 tester.c[0] = ((char*)r_data)[cpt];
186 fprintf(stderr,"\\%02x", tester.i);
188 fprintf(stderr,"\n");
190 fprintf(stderr,"l_data=");
191 for (cpt=0; cpt<l_size; cpt++) {
193 tester.c[0]= ((char*)l_data)[cpt];
194 fprintf(stderr,"\\%02x", tester.i);
195 } fprintf(stderr,"\n");
201 /* flip bytes if needed */
202 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
203 (l_size * count) > 1) {
205 for(cpt = 0, r_data=dst, l_data=dst;
208 r_data = (char *)r_data + l_size, /* resizing already done */
209 l_data = (char *)l_data + l_size) {
211 DEBUG1("Flip elm %d",cpt);
212 gras_dd_reverse_bytes(l_data, r_data, l_size);
220 gras_dd_reverse_bytes(void *to,
225 const char *fromBegin;
230 for(fromBegin = (const char *)from,
231 fromEnd = fromBegin + length - 1,
232 toBegin = (char *)to,
233 toEnd = toBegin + length - 1;
235 fromBegin <= fromEnd;
237 fromBegin++, fromEnd--,
238 toBegin++, toEnd--) {
240 charBegin = *fromBegin;
250 * returns the ID of the architecture the process is running on
253 gras_arch_selfid(void) {
254 return GRAS_THISARCH;