1/* SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause */
2#ifndef _TOOLS_DIS_ASM_COMPAT_H
3#define _TOOLS_DIS_ASM_COMPAT_H
4
5#include <stdio.h>
6#include <dis-asm.h>
7
8/* define types for older binutils version, to centralize ifdef'ery a bit */
9#ifndef DISASM_INIT_STYLED
10enum disassembler_style {DISASSEMBLER_STYLE_NOT_EMPTY};
11typedef int (*fprintf_styled_ftype) (void *, enum disassembler_style, const char*, ...);
12#endif
13
14/*
15 * Trivial fprintf wrapper to be used as the fprintf_styled_func argument to
16 * init_disassemble_info_compat() when normal fprintf suffices.
17 */
18static inline int fprintf_styled(void *out,
19				 enum disassembler_style style,
20				 const char *fmt, ...)
21{
22	va_list args;
23	int r;
24
25	(void)style;
26
27	va_start(args, fmt);
28	r = vfprintf(out, fmt, args);
29	va_end(args);
30
31	return r;
32}
33
34/*
35 * Wrapper for init_disassemble_info() that hides version
36 * differences. Depending on binutils version and architecture either
37 * fprintf_func or fprintf_styled_func will be called.
38 */
39static inline void init_disassemble_info_compat(struct disassemble_info *info,
40						void *stream,
41						fprintf_ftype unstyled_func,
42						fprintf_styled_ftype styled_func)
43{
44#ifdef DISASM_INIT_STYLED
45	init_disassemble_info(info, stream,
46			      unstyled_func,
47			      styled_func);
48#else
49	(void)styled_func;
50	init_disassemble_info(info, stream,
51			      unstyled_func);
52#endif
53}
54
55#endif /* _TOOLS_DIS_ASM_COMPAT_H */
56