3 /* ddt_remote - Stuff needed to get datadescs about remote hosts */
5 /* Authors: Olivier Aumage, Martin Quinson */
6 /* Copyright (C) 2003, 2004 the GRAS posse. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 /************************************************************************/
12 /* C combines the power of assembler with the portability of assembler. */
13 /************************************************************************/
15 #include "gras/DataDesc/datadesc_private.h"
17 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(convert,datadesc);
20 *** Table of all known architectures:
22 *** l C<1/1> I<2/2:4/4:4/4:8/4> P<4/4:4/4> D<4/4:8/4>
23 *** l C<1/1> I<2/2:4/4:8/8:8/8> P<4/4:4/4> D<4/4:8/8>
24 *** B C<1/1> I<2/2:4/4:4/8:8/8> P<4/4:4/4> D<4/4:8/4>
25 *** B C<1/1> I<2/2:4/8:8/8:8/8> P<4/4:4/4> D<4/4:8/4>
29 const gras_arch_desc_t gras_arches[gras_arch_count] = {
30 {"little32", 0, {1,2,4,4,8, 4,4, 4,8}, 4},
31 // {1,2,4,4,4, 4,4, 4,4}},
33 {"little64", 0, {1,2,4,8,8, 8,8, 4,8}, 8},
34 // {1,2,4,8,8, 8,8, 4,8}},
36 {"big32", 1, {1,2,4,4,8, 4,4, 4,8}, 8},
37 // {1,2,4,4,8, 4,4, 4,8}},
39 {"big64", 1, {1,2,4,8,8, 8,8, 4,8}, 8}
40 // {1,2,4,8,8, 8,8, 4,8}}
43 const char *gras_datadesc_arch_name(int code) {
44 if (code < 0 || code >= gras_arch_count)
45 return "[unknown arch]";
46 return gras_arches[code].name;
50 * Local function doing the grunt work
53 gras_dd_reverse_bytes(void *to,
59 * gras_dd_convert_elm:
61 * Convert the element described by @type comming from architecture @r_arch.
62 * The data to be converted is stored in @src, and is to be stored in @dst.
63 * Both pointers may be the same location if no resizing is needed.
66 gras_dd_convert_elm(gras_datadesc_type_t *type, int count,
68 void *src, void *dst) {
69 gras_dd_cat_scalar_t scal = type->category.scalar_data;
73 size_t r_size, l_size;
74 /* Hexadecimal displayer
81 gras_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
82 gras_assert(r_arch != GRAS_THISARCH);
84 r_size = type->size[r_arch];
85 l_size = type->size[GRAS_THISARCH];
86 DEBUG4("r_size=%d l_size=%d, src=%p dst=%p",
87 r_size,l_size,src,dst);
89 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
90 gras_arches[GRAS_THISARCH].endian?'B':'l');
92 if(r_size != l_size) {
93 for(cpt = 0, r_data = src, l_data = dst;
96 r_data = (char *)r_data + r_size,
97 l_data = (char *)l_data + l_size) {
100 fprintf(stderr,"r_data=");
101 for (cpt=0; cpt<r_size; cpt++) {
103 tester.c[0]= ((char*)r_data)[cpt];
104 fprintf(stderr,"\\%02x", tester.i);
106 fprintf(stderr,"\n");
109 /* Resize that damn integer, pal */
111 unsigned char *l_sign, *r_sign;
113 int sizeChange = l_size - r_size;
114 int lowOrderFirst = !gras_arches[r_arch].endian ||
115 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
117 DEBUG5("Resize integer %d from %d @%p to %d @%p",
118 cpt, r_size,r_data, l_size,l_data);
119 gras_assert0(r_data != l_data, "Impossible to resize in place");
122 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
123 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
124 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
125 /* Truncate high-order bytes. */
127 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
131 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
132 DEBUG0("This is signed");
133 /* Make sure the high order bit of r_data and l_data are the same */
134 l_sign = gras_arches[GRAS_THISARCH].endian
135 ? ((unsigned char*)l_data + l_size - 1)
136 : (unsigned char*)l_data;
137 r_sign = gras_arches[r_arch].endian
138 ? ((unsigned char*)r_data + r_size - 1)
139 : (unsigned char*)r_data;
140 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
142 if ((*r_sign > 127) != (*l_sign > 127)) {
150 DEBUG1("Extend %d bytes", sizeChange);
151 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
152 DEBUG0("This is signed");
153 padding = 0; /* pad unsigned with 0 */
156 r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
157 : (unsigned char*)r_data;
158 padding = (*r_sign > 127) ? 0xff : 0;
161 memset(l_data, padding, l_size);
162 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
166 fprintf(stderr,"r_data=");
167 for (cpt=0; cpt<r_size; cpt++) {
169 tester.c[0] = ((char*)r_data)[cpt];
170 fprintf(stderr,"\\%02x", tester.i);
172 fprintf(stderr,"\n");
174 fprintf(stderr,"l_data=");
175 for (cpt=0; cpt<l_size; cpt++) {
177 tester.c[0]= ((char*)l_data)[cpt];
178 fprintf(stderr,"\\%02x", tester.i);
179 } fprintf(stderr,"\n");
185 /* flip bytes if needed */
186 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
187 (l_size * count) > 1) {
189 for(cpt = 0, r_data=dst, l_data=dst;
192 r_data = (char *)r_data + l_size, /* resizing already done */
193 l_data = (char *)l_data + l_size) {
195 DEBUG1("Flip elm %d",cpt);
196 gras_dd_reverse_bytes(l_data, r_data, l_size);
204 gras_dd_reverse_bytes(void *to,
209 const char *fromBegin;
214 for(fromBegin = (const char *)from,
215 fromEnd = fromBegin + length - 1,
216 toBegin = (char *)to,
217 toEnd = toBegin + length - 1;
219 fromBegin <= fromEnd;
221 fromBegin++, fromEnd--,
222 toBegin++, toEnd--) {
224 charBegin = *fromBegin;
234 * returns the ID of the architecture the process is running on
237 gras_arch_selfid(void) {
238 return GRAS_THISARCH;