summaryrefslogtreecommitdiff
path: root/gdb/rdi-share/params.c
blob: 436b4dd0d1939403b8a3bbdd1e8906db3b227f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* 
 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
 * 
 * This software may be freely used, copied, modified, and distributed
 * provided that the above copyright notice is preserved in all copies of the
 * software.
 */

/* -*-C-*-
 *
 * $Revision: 1.3 $
 *     $Date: 1999/11/01 13:31:39 $
 *
 *
 *   Project: ANGEL
 *
 *     Title: Parameter negotiation utility functions
 */

#include "params.h"

#include "angel_endian.h"
#include "logging.h"


/*
 * Function: Angel_FindParam
 *  Purpose: find the value of a given parameter from a config.
 *
 * see params.h for details
 */
bool Angel_FindParam( ADP_Parameter          type,
                      const ParameterConfig *config,
                      unsigned int          *value )
{
    unsigned int i;

    for ( i = 0; i < config->num_parameters; ++i )
       if ( config->param[i].type == type )
       {
           *value = config->param[i].value;
           return TRUE;
       }

    return FALSE;
}


#if !defined(TARGET) || !defined(MINIMAL_ANGEL) || MINIMAL_ANGEL == 0

ParameterList *Angel_FindParamList( const ParameterOptions *options,
                                    ADP_Parameter           type )
{
    unsigned int i;

    for ( i = 0; i < options->num_param_lists; ++i )
       if ( options->param_list[i].type == type )
          return &options->param_list[i];

    return NULL;
}


#if defined(TARGET) || defined(TEST_PARAMS)
/*
 * Function: Angel_MatchParams
 *  Purpose: find a configuration from the requested options which is
 *           the best match from the supported options.
 *
 * see params.h for details
 */
const ParameterConfig *Angel_MatchParams( const ParameterOptions *requested,
                                          const ParameterOptions *supported )
{
    static Parameter        chosen_params[AP_NUM_PARAMS];
    static ParameterConfig  chosen_config = {
        AP_NUM_PARAMS,
        chosen_params
    };
    unsigned int i;

    ASSERT( requested != NULL, "requested is NULL" );
    ASSERT( requested != NULL, "requested is NULL" );
    ASSERT( supported->num_param_lists <= AP_NUM_PARAMS, "supp_num too big" );

    if ( requested->num_param_lists > supported->num_param_lists )
    {
        WARN( "req_num exceeds supp_num" );
        return NULL;
    }

    for ( i = 0; i < requested->num_param_lists; ++i )
    {
        bool           match;
        unsigned int   j;

        const ParameterList *req_list = &requested->param_list[i];
        ADP_Parameter        req_type = req_list->type;
        const ParameterList *sup_list = Angel_FindParamList(
                                            supported, req_type );

        if ( sup_list == NULL )
        {
#ifdef ASSERTIONS_ENABLED
            __rt_warning( "option %x not supported\n", req_type );
#endif
            return NULL;
        }

        for ( j = 0, match = FALSE;
              (j < req_list->num_options) && !match;
              ++j
            )
        {
            unsigned int k;

            for ( k = 0;
                  (k < sup_list->num_options) && !match;
                  ++k
                )
            {
                if ( req_list->option[j] == sup_list->option[k] )
                {
#ifdef DEBUG
                    __rt_info( "chose value %d for option %x\n",
                               req_list->option[j], req_type );
#endif
                    match = TRUE;
                    chosen_config.param[i].type = req_type;
                    chosen_config.param[i].value = req_list->option[j];
                }
            }
        }

        if ( !match )
        {
#ifdef ASSERTIONS_ENABLED
            __rt_warning( "no match found for option %x\n", req_type );
#endif
            return NULL;
        }
    }

    chosen_config.num_parameters = i;
    INFO( "match succeeded" );
    return &chosen_config;
}
#endif /* defined(TARGET) || defined(TEST_PARAMS) */


#if !defined(TARGET) || defined(TEST_PARAMS)
/*
 * Function: Angel_StoreParam
 *  Purpose: store the value of a given parameter to a config.
 *
 * see params.h for details
 */
bool Angel_StoreParam( ParameterConfig *config,
                       ADP_Parameter    type,
                       unsigned int     value )
{
    unsigned int i;

    for ( i = 0; i < config->num_parameters; ++i )
       if ( config->param[i].type == type )
       {
           config->param[i].value = value;
           return TRUE;
       }

    return FALSE;
}
#endif /* !defined(TARGET) || defined(TEST_PARAMS) */


#if defined(TARGET) || defined(LINK_RECOVERY) || defined(TEST_PARAMS)
/*
 * Function: Angel_BuildParamConfigMessage
 *  Purpose: write a parameter config to a buffer in ADP format.
 *
 * see params.h for details
 */
unsigned int Angel_BuildParamConfigMessage( unsigned char         *buffer,
                                            const ParameterConfig *config )
{
    unsigned char *start = buffer;
    unsigned int i;

    PUT32LE( buffer, config->num_parameters );
    buffer += sizeof( word );

    for ( i = 0; i < config->num_parameters; ++i )
    {
        PUT32LE( buffer, config->param[i].type );
        buffer += sizeof( word );
        PUT32LE( buffer, config->param[i].value );
        buffer += sizeof( word );
    }

    return (buffer - start);
}
#endif /* defined(TARGET) || defined(LINK_RECOVERY) || defined(TEST_PARAMS) */


#if !defined(TARGET) || defined(TEST_PARAMS)
/*
 * Function: Angel_BuildParamOptionsMessage
 *  Purpose: write a parameter Options to a buffer in ADP format.
 *
 * see params.h for details
 */
unsigned int Angel_BuildParamOptionsMessage( unsigned char          *buffer,
                                             const ParameterOptions *options )
{
    unsigned char *start = buffer;
    unsigned int i, j;

    PUT32LE( buffer, options->num_param_lists );
    buffer += sizeof( word );

    for ( i = 0; i < options->num_param_lists; ++i )
    {
        PUT32LE( buffer, options->param_list[i].type );
        buffer += sizeof( word );
        PUT32LE( buffer, options->param_list[i].num_options );
        buffer += sizeof( word );
        for ( j = 0; j < options->param_list[i].num_options; ++j )
        {
            PUT32LE( buffer, options->param_list[i].option[j] );
            buffer += sizeof( word );
        }
    }

    return (buffer - start);
}
#endif /* !defined(TARGET) || defined(TEST_PARAMS) */


#if !defined(TARGET) || defined(LINK_RECOVERY) || defined(TEST_PARAMS)
/*
 * Function: Angel_ReadParamConfigMessage
 *  Purpose: read a parameter config from a buffer where it is in ADP format.
 *
 * see params.h for details
 */
bool Angel_ReadParamConfigMessage( const unsigned char *buffer,
                                   ParameterConfig     *config )
{
    unsigned int word;
    unsigned int i;

    word = GET32LE( buffer );
    buffer += sizeof( word );
    if ( word > config->num_parameters )
    {
        WARN( "not enough space" );
        return FALSE;
    }
    config->num_parameters = word;

    for ( i = 0; i < config->num_parameters; ++i )
    {
        config->param[i].type = (ADP_Parameter)GET32LE( buffer );
        buffer += sizeof( word );
        config->param[i].value = GET32LE( buffer );
        buffer += sizeof( word );
    }

    return TRUE;
}
#endif /* !defined(TARGET) || defined(LINK_RECOVERY) || defined(TEST_PARAMS) */


#if defined(TARGET) || defined(TEST_PARAMS)
/*
 * Function: Angel_ReadParamOptionsMessage
 *  Purpose: read a parameter options block from a buffer
 *             where it is in ADP format.
 *
 * see params.h for details
 */
bool Angel_ReadParamOptionsMessage( const unsigned char *buffer,
                                    ParameterOptions    *options )
{
    unsigned int word;
    unsigned int i, j;

    word = GET32LE( buffer );
    buffer += sizeof( word );
    if ( word > options->num_param_lists )
    {
        WARN( "not enough space" );
        return FALSE;
    }
    options->num_param_lists = word;

    for ( i = 0; i < options->num_param_lists; ++i )
    {
        ParameterList *list = &options->param_list[i];

        list->type = (ADP_Parameter)GET32LE( buffer );
        buffer += sizeof( word );
        word = GET32LE( buffer );
        buffer += sizeof( word );
        if ( word > list->num_options )
        {
            WARN( "not enough list space" );
            return FALSE;
        }
        list->num_options = word;

        for ( j = 0; j < list->num_options; ++j )
        {
            list->option[j] = GET32LE( buffer );
            buffer += sizeof( word );
        }
    }

    return TRUE;
}
#endif /* defined(TARGET) || defined(TEST_PARAMS) */

#endif /* !define(TARGET) || !defined(MINIMAL_ANGEL) || MINIMAL_ANGEL == 0 */

/* EOF params.c */