summaryrefslogtreecommitdiff
path: root/tools/d2c/syntax/manager.c
blob: cfbf826c4cf0925ba13316ed435977c42d257262 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

/* Chrysalide - Outil d'analyse de fichiers binaires
 * manager.c - prise en compte d'une syntaxe du langage d'assemblage
 *
 * Copyright (C) 2016-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "manager.h"


#include <malloc.h>
#include <string.h>


#include "../helpers.h"



/* Propriétés particulières pour les opérandes */
typedef enum _SyntaxItemFlags
{
    SIF_NONE     = (0 << 0),                /* Aucune propriété            */
    SIF_DECIMAL  = (1 << 0),                /* Affichage en décimal        */
    SIF_OPTIONAL = (1 << 1)                 /* Absence tolérée             */

} SyntaxItemFlags;

/* Elément défini dans une syntaxe */
typedef struct _syntax_item
{
    char *name;                             /* Désignation humaine         */
    SyntaxItemType impact;                  /* Portée de l'élément         */
    SyntaxItemFlags flags;                  /* Propriétés supplémentaires  */

} syntax_item;

/* Syntaxe d'une ligne d'assembleur */
struct _asm_syntax
{
    syntax_item *items;                     /* Eléments de la syntaxe      */
    size_t items_count;                     /* Nombre de ces éléments      */

};



/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée un nouvel indicateur pour l'écriture d'une instruction. *
*                                                                             *
*  Retour      : Nouvelle structure prête à emploi.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

asm_syntax *create_asm_syntax(void)
{
    asm_syntax *result;                    /* Définition vierge à renvoyer*/

    result = (asm_syntax *)calloc(1, sizeof(asm_syntax));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : syntax = gestionnaire d'un ensemble d'éléments de syntaxe.   *
*                                                                             *
*  Description : Supprime de la mémoire un indicateur d'écriture ASM.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void delete_asm_syntax(asm_syntax *syntax)
{
    size_t i;                               /* Boucle de parcours          */

    for (i = 0; i < syntax->items_count; i++)
        free(syntax->items[i].name);

    if (syntax->items != NULL)
        free(syntax->items);

    free(syntax);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : syntax = gestionnaire d'un ensemble d'éléments de syntaxe.   *
*                name   = désignation de l'opérande dans la spécification.    *
*                impact = précise la portée effective de l'opérande           *
*                                                                             *
*  Description : Enregistre la présence d'un nouvel opérande dans la syntaxe. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void register_syntax_item(asm_syntax *syntax, char *name, SyntaxItemType impact)
{
    syntax_item *item;                      /* Nouvelle prise en compte    */
    size_t len;                             /* Taille du nom fourni        */

    syntax->items = (syntax_item *)realloc(syntax->items, ++syntax->items_count * sizeof(syntax_item));

    item = &syntax->items[syntax->items_count - 1];

    /* Récupération des drapeaux */

    item->flags = SIF_NONE;

    for (len = strlen(name); len > 0; len--)
        switch (name[0])
        {
            case '#':
                item->flags |= SIF_DECIMAL;
                memmove(name, name + 1, len);
                break;

            case '?':
                item->flags |= SIF_OPTIONAL;
                memmove(name, name + 1, len);
                break;

            default:
                len = 1;
                break;

        }

    if (impact == SIT_KEYWORD)
        item->name = name;
    else
        item->name = make_string_lower(name);

    item->impact = impact;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : syntax = gestionnaire d'un ensemble d'éléments de syntaxe.   *
*                bits   = gestionnaire des bits d'encodage.                   *
*                list   = liste de l'ensemble des fonctions de conversion.    *
*                                                                             *
*  Description : Marque les champs de bits effectivement utilisés.            *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool mark_syntax_items(const asm_syntax *syntax, const coding_bits *bits, const conv_list *list)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */
    syntax_item *item;                      /* Lien vers un opérande       */
    conv_func *func;                        /* Fonction de conversion      */

    result = true;

    for (i = 0; i < syntax->items_count && result; i++)
    {
        item = &syntax->items[i];
        if (item->impact == SIT_KEYWORD) continue;

        func = find_named_conv_in_list(list, item->name);
        if (func == NULL)
        {
            fprintf(stderr, "Error: expected conversion for '%s'.\n", item->name);
            result = false;
        }

        result = mark_conv_func(func, false, bits, list);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : syntax = gestionnaire d'un ensemble d'éléments de syntaxe.   *
*                fd     = descripteur d'un flux ouvert en écriture.           *
*                bits   = gestionnaire des bits d'encodage.                   *
*                list   = liste de l'ensemble des fonctions de conversion.    *
*                wide   = taille des mots décodés.                            *
*                                                                             *
*  Description : Déclare les variables C associées aux opérandes de syntaxe.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool declare_syntax_items(const asm_syntax *syntax, int fd, const coding_bits *bits, const conv_list *list, unsigned int wide)
{
    bool result;                            /* Bilan à retourner           */
    bool has_operand;                       /* Présence d'un opérande      */
    size_t i;                               /* Boucle de parcours          */
    syntax_item *item;                      /* Lien vers un opérande       */

    result = true;

    has_operand = false;

    for (i = 0; i < syntax->items_count && result; i++)
    {
        item = &syntax->items[i];
        if (item->impact == SIT_KEYWORD) continue;

        has_operand |= (item->impact == SIT_EXT_OPERAND);

    }

    if (has_operand)
        dprintf(fd, "\t\tGArchOperand *op;\n");

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : syntax = gestionnaire d'un ensemble d'éléments de syntaxe.   *
*                                                                             *
*  Description : Fournit si elle existe un nom nouveau pour une instruction.  *
*                                                                             *
*  Retour      : Eventuelle chaîne de caractères trouvée ou NULL.             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *get_new_keyword_from_syntax_items(const asm_syntax *syntax)
{
    const char *result;                     /* Nom éventuel à renvoyer     */

    result = NULL;

    if (syntax->items_count > 0
        && syntax->items[0].impact == SIT_KEYWORD)
    {
        result = syntax->items[0].name;
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : syntax = gestionnaire d'un ensemble d'éléments de syntaxe.   *
*                fd     = descripteur d'un flux ouvert en écriture.           *
*                arch   = architecture visée par l'opération globale.         *
*                bits   = gestionnaire des bits d'encodage.                   *
*                list   = liste de l'ensemble des fonctions de conversion.    *
*                pp     = pré-processeur pour les échanges de chaînes.        *
*                exit   = exprime le besoin d'une voie de sortie. [OUT]       *
*                                                                             *
*  Description : Définit les variables C associées aux opérandes de syntaxe.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool define_syntax_items(const asm_syntax *syntax, int fd, const char *arch, const coding_bits *bits, const conv_list *list, const pre_processor *pp, bool *exit)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */
    syntax_item *item;                      /* Lien vers un opérande       */
    conv_func *func;                        /* Fonction de conversion      */
    bool internal;                          /* Usage interne ou non ?      */

    result = true;

    for (i = 0; i < syntax->items_count && result; i++)
    {
        item = &syntax->items[i];

        switch (item->impact)
        {
            case SIT_KEYWORD:

                /**
                 * TODO : à faire évoluer vers extend...
                 */
                //_exit(123);

                // rev_A88146
                /*
                if (i > 0)
                    dprintf(fd, "\t\tg_arch_instruction_append_suffix(instr, \"%s\");\n", item->name);
                else
                    continue;
                */

                break;

            case SIT_INT_OPERAND:   // A supprimer
            case SIT_EXT_OPERAND:

                internal = (item->impact == SIT_INT_OPERAND);

                func = find_named_conv_in_list(list, item->name);
                if (func == NULL)
                {
                    fprintf(stderr, "Error: expected conversion for '%s'.\n", item->name);
                    result = false;
                }

                /* Appel proprement dit */

                if (is_conv_func_already_defined(func))
                {
                    dprintf(fd, "\t\top = val_%s;\n", item->name);

                    dprintf(fd, "\n");

                    if (item->flags & SIF_DECIMAL)
                        dprintf(fd, "\t\tg_imm_operand_set_default_display(G_IMM_OPERAND(op), IOD_DEC);\n");

                    dprintf(fd, "\t\tg_arch_instruction_attach_extra_operand(instr, op);\n");

                }

                else
                {
                    result &= define_conv_func(func, true, internal, fd, arch, bits, list, pp, exit);
                    if (!result) break;

                    /* Raccordement : propriété ou opérande ? */

                    if (internal)
                    {
                        dprintf(fd, "\t\t\tgoto bad_exit;\n");
                        *exit = true;
                    }

                    else
                    {
                        dprintf(fd, "\t\tif (op == NULL) goto bad_exit;\n");
                        *exit = true;

                        dprintf(fd, "\n");

                        if (item->flags & SIF_DECIMAL)
                            dprintf(fd, "\t\tg_imm_operand_set_default_display(G_IMM_OPERAND(op), IOD_DEC);\n");

                        dprintf(fd, "\t\tg_arch_instruction_attach_extra_operand(instr, op);\n");

                    }

                }

                *exit = true;
                break;

        }

        dprintf(fd, "\n");

    }

    return result;

}