summaryrefslogtreecommitdiff
path: root/src/arch/instruction.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/arch/instruction.c')
-rw-r--r--src/arch/instruction.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/arch/instruction.c b/src/arch/instruction.c
index ea19dab..52c3aa0 100644
--- a/src/arch/instruction.c
+++ b/src/arch/instruction.c
@@ -27,11 +27,14 @@
#include <assert.h>
#include <malloc.h>
#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "instruction-int.h"
#include "storage.h"
+#include "../core/logs.h"
#include "../glibext/gbinarycursor.h"
#include "../glibext/linegen-int.h"
#include "../gtkext/gtkblockdisplay.h"
@@ -819,6 +822,146 @@ bool _g_arch_instruction_detach_operand(GArchInstruction *instr, GArchOperand *t
}
+/******************************************************************************
+* *
+* Paramètres : instr = instance à consulter. *
+* target = instruction à venir retrouver. *
+* *
+* Description : Détermine le chemin conduisant à un opérande. *
+* *
+* Retour : Chemin d'accès à l'opérande ou NULL en cas d'absence. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+char *g_arch_instruction_find_operand_path(GArchInstruction *instr, const GArchOperand *target)
+{
+ char *result; /* Chemin à retourner */
+ size_t count; /* Nombre d'opérandes en place */
+ size_t i; /* Boucle de parcours */
+ GArchOperand *op; /* Opérande à manipuler */
+ int ret; /* Bilan d'une construction */
+ char *sub_path; /* Sous-chemin emprunté */
+
+ result = NULL;
+
+ g_arch_instruction_lock_operands(instr);
+
+ count = _g_arch_instruction_count_operands(instr);
+
+ /* Première passe : accès direct */
+
+ for (i = 0; i < count && result == NULL; i++)
+ {
+ op = _g_arch_instruction_get_operand(instr, i);
+
+ if (op == target)
+ {
+ ret = asprintf(&result, "%zu", i);
+ if (ret == -1)
+ {
+ LOG_ERROR_N("asprintf");
+ result = NULL;
+ }
+ }
+
+ g_object_unref(G_OBJECT(op));
+
+ }
+
+ /* Seconde passe : accès profond */
+
+ for (i = 0; i < count && result == NULL; i++)
+ {
+ op = _g_arch_instruction_get_operand(instr, i);
+
+ sub_path = g_arch_operand_find_inner_operand_path(op, target);
+
+ if (sub_path != NULL)
+ {
+ ret = asprintf(&result, "%zu:%s", i, sub_path);
+ if (ret == -1)
+ {
+ LOG_ERROR_N("asprintf");
+ result = NULL;
+ }
+
+ free(sub_path);
+
+ }
+
+ g_object_unref(G_OBJECT(op));
+
+ }
+
+ g_arch_instruction_unlock_operands(instr);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : instr = instance à consulter. *
+* path = chemin d'accès à un opérande à retrouver. *
+* *
+* Description : Obtient l'opérande correspondant à un chemin donné. *
+* *
+* Retour : Opérande trouvé ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GArchOperand *g_arch_instruction_get_operand_from_path(GArchInstruction *instr, const char *path)
+{
+ GArchOperand *result; /* Opérande trouvée à renvoyer */
+ size_t index; /* Indice de l'opérande visé */
+ char *end; /* Poursuite du parcours ? */
+ GArchOperand *found; /* Opérande trouvé */
+
+ result = NULL;
+
+ g_arch_instruction_lock_operands(instr);
+
+ /* Recherche au premier niveau */
+
+ index = strtoul(path, &end, 10);
+
+ if ((index == ULONG_MAX && errno == ERANGE) || (index == 0 && errno == EINVAL))
+ {
+ LOG_ERROR_N("strtoul");
+ goto done;
+ }
+
+ found = _g_arch_instruction_get_operand(instr, index);
+ if (found == NULL) goto done;
+
+ if (*end == '\0')
+ {
+ result = found;
+ goto done;
+ }
+
+ /* Recherche en profondeur */
+
+ assert(*end == ':');
+
+ result = g_arch_operand_get_inner_operand_from_path(found, end + 1);
+
+ g_object_unref(G_OBJECT(found));
+
+ done:
+
+ g_arch_instruction_unlock_operands(instr);
+
+ return result;
+
+}
+
+
/* ---------------------------------------------------------------------------------- */
/* DEFINITION DES LIAISONS ENTRE INSTRUCTIONS */