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}}
56 const char *gras_datadesc_arch_name(int code) {
57 if (code < 0 || code >= gras_arch_count)
58 return "[unknown arch]";
59 return gras_arches[code].name;
64 * Local function doing the grunt work
67 gras_dd_reverse_bytes(void *to,
72 * gras_dd_convert_elm:
74 * Convert the element described by @type comming from architecture @r_arch.
75 * The data to be converted is stored in @src, and is to be stored in @dst.
76 * Both pointers may be the same location if no resizing is needed.
79 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
81 void *src, void *dst) {
82 gras_dd_cat_scalar_t scal = type->category.scalar_data;
86 unsigned long r_size, l_size;
87 /* Hexadecimal displayer
94 xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
95 xbt_assert(r_arch != GRAS_THISARCH);
97 r_size = type->size[r_arch];
98 l_size = type->size[GRAS_THISARCH];
99 DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p",
100 r_size,l_size,src,dst);
102 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
103 gras_arches[GRAS_THISARCH].endian?'B':'l');
105 if(r_size != l_size) {
106 for(cpt = 0, r_data = src, l_data = dst;
109 r_data = (char *)r_data + r_size,
110 l_data = (char *)l_data + l_size) {
113 fprintf(stderr,"r_data=");
114 for (cpt=0; cpt<r_size; cpt++) {
116 tester.c[0]= ((char*)r_data)[cpt];
117 fprintf(stderr,"\\%02x", tester.i);
119 fprintf(stderr,"\n");
122 /* Resize that damn integer, pal */
124 unsigned char *l_sign, *r_sign;
126 int sizeChange = l_size - r_size;
127 int lowOrderFirst = !gras_arches[r_arch].endian ||
128 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
130 DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
131 cpt, r_size,r_data, l_size,l_data);
132 xbt_assert0(r_data != l_data, "Impossible to resize in place");
135 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
136 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
137 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
138 /* Truncate high-order bytes. */
140 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
144 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
145 DEBUG0("This is signed");
146 /* Make sure the high order bit of r_data and l_data are the same */
147 l_sign = gras_arches[GRAS_THISARCH].endian
148 ? ((unsigned char*)l_data + l_size - 1)
149 : (unsigned char*)l_data;
150 r_sign = gras_arches[r_arch].endian
151 ? ((unsigned char*)r_data + r_size - 1)
152 : (unsigned char*)r_data;
153 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
155 if ((*r_sign > 127) != (*l_sign > 127)) {
163 DEBUG1("Extend %d bytes", sizeChange);
164 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
165 DEBUG0("This is signed");
166 padding = 0; /* pad unsigned with 0 */
169 r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
170 : (unsigned char*)r_data;
171 padding = (*r_sign > 127) ? 0xff : 0;
174 memset(l_data, padding, l_size);
175 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
179 fprintf(stderr,"r_data=");
180 for (cpt=0; cpt<r_size; cpt++) {
182 tester.c[0] = ((char*)r_data)[cpt];
183 fprintf(stderr,"\\%02x", tester.i);
185 fprintf(stderr,"\n");
187 fprintf(stderr,"l_data=");
188 for (cpt=0; cpt<l_size; cpt++) {
190 tester.c[0]= ((char*)l_data)[cpt];
191 fprintf(stderr,"\\%02x", tester.i);
192 } fprintf(stderr,"\n");
198 /* flip bytes if needed */
199 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
200 (l_size * count) > 1) {
202 for(cpt = 0, r_data=dst, l_data=dst;
205 r_data = (char *)r_data + l_size, /* resizing already done */
206 l_data = (char *)l_data + l_size) {
208 DEBUG1("Flip elm %d",cpt);
209 gras_dd_reverse_bytes(l_data, r_data, l_size);
217 gras_dd_reverse_bytes(void *to,
222 const char *fromBegin;
227 for(fromBegin = (const char *)from,
228 fromEnd = fromBegin + length - 1,
229 toBegin = (char *)to,
230 toEnd = toBegin + length - 1;
232 fromBegin <= fromEnd;
234 fromBegin++, fromEnd--,
235 toBegin++, toEnd--) {
237 charBegin = *fromBegin;
247 * returns the ID of the architecture the process is running on
250 gras_arch_selfid(void) {
251 return GRAS_THISARCH;