summaryrefslogtreecommitdiff
path: root/include/decomp.h
blob: f678e0d884d52561e34b1f55299adcaa7fe29f11 (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
#ifndef DECOMP_H
#define DECOMP_H

/*******************************************************************************
 * headers
 */

/*******************************************************************************
 * Macro helpers
 */

#define DF_CONCAT3_(a, b, c)	a ## b ## c
#define DF_CONCAT3(a, b, c)		DF_CONCAT3_(a, b, c)

#define DF_UNIQUE_IDENT(ident_)	DF_CONCAT3(ident_, _, __LINE__)

#define DF_TYPEOF				typeof

#define DF_SWALLOW_SEMICOLON()	static_assert(1, "")

#if defined(__clang__)

# define DF_SUPPRESS_WARNINGS()									\
	_Pragma("clang diagnostic push")							\
	_Pragma("clang diagnostic ignored \"-Wnull-dereference\"")
# define DF_UNSUPPRESS_WARNINGS()								\
	_Pragma("clang diagnostic pop")

#elif defined(__MWERKS__)

# pragma section RX ".decomp"

# define DF_SUPPRESS_WARNINGS()		__attribute__((section(".decomp")))
# define DF_UNSUPPRESS_WARNINGS()

#else

# define DF_SUPPRESS_WARNINGS()
# define DF_UNSUPPRESS_WARNINGS()

#endif

/*******************************************************************************
 * DECOMP_FORCE macro internals
 */

#define DF_FUNCTION_DECLARATOR_WITH_PROTO(ident_)	\
	extern void (ident_)(void);						\
	extern void (ident_)(void)

// this is done to prevent default promotion of arguments to variadic functions
#define DF_FUNCTION_CALL(ident_, arg_)		\
	extern void (ident_)(DF_TYPEOF(arg_));	\
	(ident_)(arg_)

/*******************************************************************************
 * DECOMP_FORCE macros
 */

/* Forcefully generate orphaned data, early references, or other shenanigans.
 * Only works at file or namespace scope.
 *
 * Examples:
 * DECOMP_FORCE(1.0f);
 * DECOMP_FORCE("I am a string");
 * DECOMP_FORCE(UI2D_CONSTANT);
 */

#define DECOMP_FORCE(arg_)												\
	DF_SUPPRESS_WARNINGS()												\
	DF_FUNCTION_DECLARATOR_WITH_PROTO(DF_UNIQUE_IDENT(DECOMP_FORCE))	\
	{																	\
		DF_FUNCTION_CALL(DF_UNIQUE_IDENT(DECOMP_FORCE_CALL), arg_);		\
	}																	\
	DF_UNSUPPRESS_WARNINGS()											\
	DF_SWALLOW_SEMICOLON()

/* Forcefully instantiate a class's method. For method_, write it as you would
 * an actual function call.
 * Only works at file or namespace scope.
 *
 * If class_  uses commas outside of nested parentheses, you must enclose the
 * entire argument in DF_TYPEOF(). If method_ uses commas outside of nested
 * parentheses, they are handled via __VA_ARGS__.
 *
 * Examples:
 * DECOMP_FORCE_CLASS_METHOD(Class1, ~Class1());
 * DECOMP_FORCE_CLASS_METHOD(DF_TYPEOF(Class2<int, 1>), func<long, int>(2, 3));
 * DECOMP_FORCE_CLASS_METHOD(Class3, operator ,(4));
 */

#if defined(__cplusplus)
# define DECOMP_FORCE_CLASS_METHOD(class_, ...)	\
	DECOMP_FORCE(((void)(static_cast<DF_TYPEOF(class_) *>(0)->__VA_ARGS__), 0))
#endif

/*******************************************************************************
 * DECOMP_FORCE helpers
 */

// Conversion constants from integer to floating-point

extern   signed int DECOMP_SI;
extern unsigned int DECOMP_UI;

#define SI2D_CONSTANT	((float)(DECOMP_SI))
#define UI2D_CONSTANT	((float)(DECOMP_UI))

// clang-format on

#endif // DECOMP_H