introduced custom instruction formatters

This commit is contained in:
Ende! 2015-03-19 21:05:14 +01:00
parent b93ddf848b
commit 7e26fa1683
3 changed files with 97 additions and 5 deletions

View File

@ -75,8 +75,6 @@ static void VXBaseSymbolResolver_Destruct(VXBaseSymbolResolverContext *ctx);
typedef void(*VXBaseInstructionFormatter_DestructionCallback)(
VXBaseInstructionFormatterContext *ctx);
typedef void(*VXBaseInstructionFormatter_InternalFormatInstructionCallback)(
VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
typedef struct _VXBaseInstructionFormatter
{
@ -263,6 +261,27 @@ static void VXIntelInstructionFormatter_FormatOperand(VXBaseInstructionFormatter
static void VXIntelInstructionFormatter_InternalFormatInstruction(
VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
/* VXCustomInstructionFormatter ---------------------------------------------------------------- */
typedef struct _VXCustomInstructionFormatter
{
VXBaseInstructionFormatter super;
} VXCustomInstructionFormatter;
/**
* @brief Contructor.
* @param ctx The context.
* @param formatInsnCb The callback formatting the instruction.
*/
static void VXCustomInstructionFormatter_Construct(VXBaseInstructionFormatterContext *ctx,
VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb);
/**
* @brief Destructor.
* @param ctx The context.
*/
static void VXCustomInstructionFormatter_Destruct(VXBaseInstructionFormatterContext *ctx);
/* Implementation ============================================================================== */
/* VXBaseSymbolResolver ------------------------------------------------------------------------ */
@ -1069,6 +1088,49 @@ static void VXIntelInstructionFormatter_InternalFormatInstruction(
}
}
/* VXCustomInstructionFormatter ---------------------------------------------------------------- */
static void VXCustomInstructionFormatter_Construct(VXBaseInstructionFormatterContext *ctx,
VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb)
{
VXBaseInstructionFormatter_Construct(ctx, NULL);
VXCustomInstructionFormatter *thiz = VXCustomInstructionFormatter_thiz(ctx);
thiz->super.internalFormat = formatInsnCb;
}
static void VXCustomInstructionFormatter_Destruct(VXBaseInstructionFormatterContext *ctx)
{
VXBaseInstructionFormatter_Destruct(ctx);
}
VX_EXPORT VXBaseInstructionFormatterContext* VXCustomInstructionFormatter_Create(
VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb)
{
VXCustomInstructionFormatter *thiz = malloc(sizeof(VXCustomInstructionFormatter));
VXBaseInstructionFormatterContext *ctx = malloc(sizeof(VXBaseInstructionFormatterContext));
if (!thiz || !ctx)
{
if (thiz)
{
free(thiz);
}
if (ctx)
{
free(ctx);
}
return NULL;
}
ctx->d.type = TYPE_CUSTOMINSTRUCTIONFORMATTER;
ctx->d.ptr = thiz;
VXCustomInstructionFormatter_Construct(ctx, formatInsnCb);
return ctx;
}
/* --------------------------------------------------------------------------------------------- */
/* ============================================================================================= */

View File

@ -97,6 +97,9 @@ typedef struct _VXBaseInstructionFormatterContext
VXContextDescriptor d;
} VXBaseInstructionFormatterContext;
typedef void(*VXBaseInstructionFormatter_InternalFormatInstructionCallback)(
VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
/**
* @brief Formats a decoded instruction.
* @param ctx The instruction formatter context.
@ -134,7 +137,7 @@ VX_EXPORT void VXBaseInstructionFormatter_SetSymbolResolver(
VX_EXPORT void VXBaseInstructionFormatter_Release(
VXBaseInstructionFormatterContext *ctx);
/* VXIntelInstructionFormatter ================================================================ */
/* VXIntelInstructionFormatter ================================================================= */
/**
* @brief Creates an Intel-syntax instruction formatter.
@ -152,6 +155,16 @@ VX_EXPORT VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_Create(
VX_EXPORT VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_CreateEx(
VXBaseSymbolResolverContext *resolver);
/* VXCustomInstructionFormatter ================================================================ */
/**
* @brief Creats a custom instruction formatter.
* @param formatInsnCb The callback formatting the instruction.
* @return @c NULL if it fails, else a custom instruction formatter context.
*/
VX_EXPORT VXBaseInstructionFormatterContext* VXCustomInstructionFormatter_Create(
VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb);
/* ============================================================================================= */
#ifdef __cplusplus

View File

@ -51,6 +51,7 @@ typedef enum _VXTypeId
TYPE_CUSTOMSYMBOLRESOLVER,
TYPE_BASEINSTRUCTIONFORMATTER,
TYPE_INTELINSTRUCTIONFORMATTER,
TYPE_CUSTOMINSTRUCTIONFORMATTER,
} VXTypeId;
/* Context conversion helpers ================================================================== */
@ -149,7 +150,8 @@ VX_INLINE struct _VXBaseInstructionFormatter* VXBaseInstructionFormatter_thiz(
VXBaseInstructionFormatterContext *ctx)
{
assert(ctx->d.type == TYPE_BASEINSTRUCTIONFORMATTER
|| ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER);
|| ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER
|| ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
return (struct _VXBaseInstructionFormatter*)ctx->d.ptr;
}
@ -157,7 +159,8 @@ VX_INLINE const struct _VXBaseInstructionFormatter* VXBaseInstructionFormatter_c
const VXBaseInstructionFormatterContext *ctx)
{
assert(ctx->d.type == TYPE_BASEINSTRUCTIONFORMATTER
|| ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER);
|| ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER
|| ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
return (const struct _VXBaseInstructionFormatter*)ctx->d.ptr;
}
@ -175,6 +178,20 @@ VX_INLINE const struct _VXIntelInstructionFormatter* VXIntelInstructionFormatter
return (const struct _VXIntelInstructionFormatter*)ctx->d.ptr;
}
VX_INLINE struct _VXCustomInstructionFormatter* VXCustomInstructionFormatter_thiz(
VXBaseInstructionFormatterContext *ctx)
{
assert(ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
return (struct _VXCustomInstructionFormatter*)ctx->d.ptr;
}
VX_INLINE const struct _VXCustomInstructionFormatter* VXCustomInstructionFormatter_cthiz(
const VXBaseInstructionFormatterContext *ctx)
{
assert(ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
return (struct _VXCustomInstructionFormatter*)ctx->d.ptr;
}
/* ============================================================================================= */
#endif /* _VDE_VXINTERNALHELPERS_H_ */