summaryrefslogtreecommitdiff
path: root/src/decomp/expr/block.c
blob: e7d11a7c96e6fff2f0aa75ae9b5ae4b348cc6a21 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * block.c - regroupement d'un lot d'instructions
 *
 * Copyright (C) 2010-2013 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 "block.h"


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


#include "../expression-int.h"



/* Définition d'un ensemble d'instructions décompilées (instance) */
struct _GExprBlock
{
    GDecExpression parent;                  /* A laisser en premier        */

    BlockBordeBehavior behavior;            /* Type de Rendu des bordures  */

    GDecInstruction **list;                 /* Instructions contenues      */
    size_t count;                           /* Taille de cette liste       */

};


/* Définition d'un ensemble d'instructions décompilées (classe) */
struct _GExprBlockClass
{
    GDecExpressionClass parent;             /* A laisser en premier        */

};



/* Initialise la classe des ensembles d'instructions. */
static void g_expr_block_class_init(GExprBlockClass *);

/* Initialise une instance d'ensemble d'instructions. */
static void g_expr_block_init(GExprBlock *);

/* Visite un ensemble hiérarchique d'instructions décompilées. */
static bool g_expr_block_visit(GExprBlock *, dec_instr_visitor_cb, DecInstrVisitFlags, void *);

/* Remplace une instruction décompilée par une autre. */
static bool g_expr_block_replace(GExprBlock *, GDecInstruction *, GDecInstruction *);

/* Imprime pour l'écran un version humaine d'une expression. */
static GBufferLine *g_expr_block_print(const GExprBlock *, GCodeBuffer *, GBufferLine *, GLangOutput *);



/* Indique le type défini pour un ensemble d'instructions décompilées. */
G_DEFINE_TYPE(GExprBlock, g_expr_block, G_TYPE_DEC_EXPRESSION);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des ensembles d'instructions.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_expr_block_class_init(GExprBlockClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = instance à initialiser.                              *
*                                                                             *
*  Description : Initialise une instance d'ensemble d'instructions.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_expr_block_init(GExprBlock *block)
{
    GDecInstruction *instr;                 /* Autre version de l'objet    */

    instr = G_DEC_INSTRUCTION(block);

    instr->visit = (dec_instr_visit_fc)g_expr_block_visit;
    instr->replace = (dec_instr_replace_fc)g_expr_block_replace;
    instr->print = (dec_instr_print_fc)g_expr_block_print;

    block->behavior = BBB_AUTO;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : item = premier élément du nouvel ensemble.                   *
*                                                                             *
*  Description : Constuit un conteneur pour diverses instructions décompilées.*
*                                                                             *
*  Retour      : Conteneur d'instructions mis en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDecInstruction *g_expr_block_new(GDecInstruction *item)
{
    GExprBlock *result;                /* Groupe d'instructions à renvoyer */

    result = g_object_new(G_TYPE_EXPR_BLOCK, NULL);

    g_expr_block_add_item(result, item);

    return G_DEC_INSTRUCTION(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = première instruction à venir visiter.                *
*                                                                             *
*  Description : Fournit le comportement du bloc pour le rendu de ses bords.  *
*                                                                             *
*  Retour      : Comportement pour le rendu des bordures.                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

BlockBordeBehavior g_expr_block_get_border_behavior(const GExprBlock *block)
{
    return block->behavior;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block    = première instruction à venir visiter.             *
*                behavior = comportement pour le rendu des bordures.          *
*                                                                             *
*  Description : Définit le comportement du bloc pour le rendu de ses bords.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_expr_block_set_border_behavior(GExprBlock *block, BlockBordeBehavior behavior)
{
    block->behavior = behavior;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block    = première instruction à venir visiter.             *
*                callback = procédure à appeler à chaque instruction visitée. *
*                flags    = moments des appels à réaliser en retour.          *
*                data     = données quelconques associées au visiteur.        *
*                                                                             *
*  Description : Visite un ensemble hiérarchique d'instructions décompilées.  *
*                                                                             *
*  Retour      : true si le parcours a été jusqu'à son terme, false sinon.    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_expr_block_visit(GExprBlock *block, dec_instr_visitor_cb callback, DecInstrVisitFlags flags, void *data)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */

    result = true;

    for (i = 0; i < block->count && result; i++)
        result = _g_dec_instruction_visit(block->list[i], G_DEC_INSTRUCTION(block),
                                          callback, flags, data);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = première instruction à venir ausculter.              *
*                old   = instruction décompilée à venir remplacer.            *
*                new   = instruction décompilée à utiliser dorénavant.        *
*                                                                             *
*  Description : Remplace une instruction décompilée par une autre.           *
*                                                                             *
*  Retour      : true si un remplacement a été effectué, false sinon.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_expr_block_replace(GExprBlock *block, GDecInstruction *old, GDecInstruction *new)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours          */

    result = false;

    for (i = 0; i < block->count; i++)
    {
        if (block->list[i] == old)
        {
            g_object_unref(G_OBJECT(block->list[i]));
            g_object_ref(G_OBJECT(new));
            block->list[i] = new;

            result = true;

        }
        else
            result |= g_dec_instruction_replace(block->list[i], old, new);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block  = expression à transcrire en version humaine.         *
*                buffer = tampon où doit se réaliser l'insertion.             *
*                line   = ligne d'impression prête à emploi ou NULL.          *
*                output = langage de programmation de sortie.                 *
*                                                                             *
*  Description : Imprime pour l'écran un version humaine d'une expression.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GBufferLine *g_expr_block_print(const GExprBlock *block, GCodeBuffer *buffer, GBufferLine *line, GLangOutput *output)
{
    GBufferLine *result;                    /* Ligne à retourner           */
    size_t expr_count;                      /* Taille officielle           */
    size_t i;                               /* Boucle de parcours          */

    switch (block->behavior)
    {
        case BBB_AUTO:
            expr_count = block->count;
            break;
        case BBB_FORCE_OFF:
            expr_count = 1;
            break;
        case BBB_FORCE_ON:
            expr_count = 2;
            break;
    }

    result = g_lang_output_start_code_block(output, buffer, line, expr_count);

    for (i = 0; i < block->count; i++)
    {
        if (i > 0)
            result = g_code_buffer_append_new_line_fixme(buffer);   /* FIXME : n° de ligne */

        result = g_dec_instruction_print(block->list[i], buffer, result, output);

    }

    result = g_lang_output_end_code_block(output, buffer, result, expr_count);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = ensemble à faire évoluer.                            *
*                item  = nouvel élément à placer dans l'ensemble.             *
*                                                                             *
*  Description : Ajoute une instruction décompilée au conteneur existant.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_expr_block_add_item(GExprBlock *block, GDecInstruction *item)
{
    block->list = (GDecInstruction **)realloc(block->list, 
                                              ++block->count * sizeof(GDecInstruction *));
    block->list[block->count - 1] = item;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = ensemble à consulter.                                *
*                index = indice de l'instruction décompilée recherchée.       *
*                                                                             *
*  Description : Fournit une instruction décompilée donnée du conteneur.      *
*                                                                             *
*  Retour      : Instruction décompilée, ou NULL si l'indice n'est pas valide.*
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDecInstruction *g_expr_block_get_item(const GExprBlock *block, size_t index)
{
    GDecInstruction *result;                /* Elément à retourner         */

    result = NULL;

    if (index < block->count)
        result = block->list[index];

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = ensemble à faire évoluer.                            *
*                item  = nouvel élément à retirer de l'ensemble.              *
*                                                                             *
*  Description : Supprime une instruction décompilée du conteneur existant.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_expr_block_delete_item(GExprBlock *block, GDecInstruction *item)
{
    size_t i;                               /* Boucle de parcours          */

    for (i = 0; i < block->count; i++)
        if (block->list[i] == item)
            break;

    if (i < block->count)
    {
        if ((i + 1) < block->count)
            memmove(&block->list[i], &block->list[i + 1],
                    (block->count - i - 1) * sizeof(GDecInstruction *));

        block->list = (GDecInstruction **)realloc(block->list, 
                                                  --block->count * sizeof(GDecInstruction *));

        g_object_unref(G_OBJECT(item));

    }

}