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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_convert,datadesc,
18 "Inter-architecture convertions");
21 *** Table of all known architectures:
23 *** l C<1/1> I<2/2:4/4:4/4:8/4> P<4/4:4/4> D<4/4:8/4>
24 *** l C<1/1> I<2/2:4/4:8/8:8/8> P<4/4:4/4> D<4/4:8/8>
25 *** B C<1/1> I<2/2:4/4:4/8:8/8> P<4/4:4/4> D<4/4:8/4>
26 *** B C<1/1> I<2/2:4/8:8/8:8/8> P<4/4:4/4> D<4/4:8/4>
27 *** B C:1/1: I:2/2:4/4:4/4:8/8: P:4/4:4/4: D:4/4:8/4: AIX
28 *** B C:1/1: I:2/2:4/2:4/2:8/2: P:4/2:4/2: D:4/2:8/2: ARM
31 const gras_arch_desc_t gras_arches[gras_arch_count] = {
32 {"little32", 0, {1,2,4,4,8, 4,4, 4,8},
33 {1,2,4,4,4, 4,4, 4,4}},
35 {"little64", 0, {1,2,4,8,8, 8,8, 4,8},
36 {1,2,4,8,8, 8,8, 4,8}},
38 {"big32", 1, {1,2,4,4,8, 4,4, 4,8},
39 {1,2,4,4,8, 4,4, 4,8}},
41 {"big64", 1, {1,2,4,8,8, 8,8, 4,8},
42 {1,2,4,8,8, 8,8, 4,8}},
44 {"aix", 1, {1,2,4,4,8, 4,4, 4,8},
45 {1,2,4,4,8, 4,4, 4,4}},
47 {"arm", 1, {1,2,4,4,8, 4,4, 4,8},
48 {1,2,2,2,2, 2,2, 2,2}}
52 const char *gras_datadesc_arch_name(int code) {
53 if (code < 0 || code >= gras_arch_count)
54 return "[unknown arch]";
55 return gras_arches[code].name;
59 * Local function doing the grunt work
62 gras_dd_reverse_bytes(void *to,
68 * gras_dd_convert_elm:
70 * Convert the element described by @type comming from architecture @r_arch.
71 * The data to be converted is stored in @src, and is to be stored in @dst.
72 * Both pointers may be the same location if no resizing is needed.
75 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
77 void *src, void *dst) {
78 gras_dd_cat_scalar_t scal = type->category.scalar_data;
82 unsigned long r_size, l_size;
83 /* Hexadecimal displayer
90 xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
91 xbt_assert(r_arch != GRAS_THISARCH);
93 r_size = type->size[r_arch];
94 l_size = type->size[GRAS_THISARCH];
95 DEBUG4("r_size=%lu l_size=%lu, src=%p dst=%p",
96 r_size,l_size,src,dst);
98 DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
99 gras_arches[GRAS_THISARCH].endian?'B':'l');
101 if(r_size != l_size) {
102 for(cpt = 0, r_data = src, l_data = dst;
105 r_data = (char *)r_data + r_size,
106 l_data = (char *)l_data + l_size) {
109 fprintf(stderr,"r_data=");
110 for (cpt=0; cpt<r_size; cpt++) {
112 tester.c[0]= ((char*)r_data)[cpt];
113 fprintf(stderr,"\\%02x", tester.i);
115 fprintf(stderr,"\n");
118 /* Resize that damn integer, pal */
120 unsigned char *l_sign, *r_sign;
122 int sizeChange = l_size - r_size;
123 int lowOrderFirst = !gras_arches[r_arch].endian ||
124 gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
126 DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
127 cpt, r_size,r_data, l_size,l_data);
128 xbt_assert0(r_data != l_data, "Impossible to resize in place");
131 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
132 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
133 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
134 /* Truncate high-order bytes. */
136 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
140 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
141 DEBUG0("This is signed");
142 /* Make sure the high order bit of r_data and l_data are the same */
143 l_sign = gras_arches[GRAS_THISARCH].endian
144 ? ((unsigned char*)l_data + l_size - 1)
145 : (unsigned char*)l_data;
146 r_sign = gras_arches[r_arch].endian
147 ? ((unsigned char*)r_data + r_size - 1)
148 : (unsigned char*)r_data;
149 DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
151 if ((*r_sign > 127) != (*l_sign > 127)) {
159 DEBUG1("Extend %d bytes", sizeChange);
160 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
161 DEBUG0("This is signed");
162 padding = 0; /* pad unsigned with 0 */
165 r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
166 : (unsigned char*)r_data;
167 padding = (*r_sign > 127) ? 0xff : 0;
170 memset(l_data, padding, l_size);
171 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
175 fprintf(stderr,"r_data=");
176 for (cpt=0; cpt<r_size; cpt++) {
178 tester.c[0] = ((char*)r_data)[cpt];
179 fprintf(stderr,"\\%02x", tester.i);
181 fprintf(stderr,"\n");
183 fprintf(stderr,"l_data=");
184 for (cpt=0; cpt<l_size; cpt++) {
186 tester.c[0]= ((char*)l_data)[cpt];
187 fprintf(stderr,"\\%02x", tester.i);
188 } fprintf(stderr,"\n");
194 /* flip bytes if needed */
195 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
196 (l_size * count) > 1) {
198 for(cpt = 0, r_data=dst, l_data=dst;
201 r_data = (char *)r_data + l_size, /* resizing already done */
202 l_data = (char *)l_data + l_size) {
204 DEBUG1("Flip elm %d",cpt);
205 gras_dd_reverse_bytes(l_data, r_data, l_size);
213 gras_dd_reverse_bytes(void *to,
218 const char *fromBegin;
223 for(fromBegin = (const char *)from,
224 fromEnd = fromBegin + length - 1,
225 toBegin = (char *)to,
226 toEnd = toBegin + length - 1;
228 fromBegin <= fromEnd;
230 fromBegin++, fromEnd--,
231 toBegin++, toEnd--) {
233 charBegin = *fromBegin;
243 * returns the ID of the architecture the process is running on
246 gras_arch_selfid(void) {
247 return GRAS_THISARCH;