summaryrefslogtreecommitdiff
path: root/processor/struct.m4
blob: ee62c79508fa6adaf93219ad5d11c3ef938521c0 (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
define(`pushdefFUNC_NAME', `
	pushdef(`FUNC_NAME', `xc_`'PROCESSOR_TYPE`'_`'ifelse(`$2', `', `$1', `$2')')
')
dnl {{{ DECL_STRUCT_P_FUNC(1:type, 2:name, 3:comma=;)
define(`DECL_STRUCT_P_FUNC', `translit(
	pushdefFUNC_NAME(`$1', `$2')
	pushdef(`type', `ifelse(`$2', `', `$1', `$2')')
	define(`DEFINED_'type, `')
	ifelse(ifdef(`EXPORT_'PROCESSOR_TYPE`_'type,1)ifdef(`EXPORT_'PROCESSOR_TYPE`_',1)ifdef(`EXPORT__'type,1), `', `static void inline',
		`', `', `void')
	popdef(`type')
	FUNC_NAME`'(
		IFDPRINT( `const $1 * const src, int indent')
		IFCALC(   `xc_processor_t *processor, const $1 * const src')
		IFSTORE(  `xc_processor_t *processor, $1 *dst, const $1 * const src')
		IFRESTORE(`xc_processor_t *processor, $1 *dst, const $1 * const src')
		PROCRELOCATE(`$1 *const dst, ptrdiff_t ptrdiff, ptrdiff_t relocatediff')
		IFDASM(`xc_dasm_t *dasm, zval *dst, const $1 * const src')
		TSRMLS_DC
	)ifelse(`$3', `', `;')
	popdef(`FUNC_NAME')dnl
, `
', ` ')')
dnl }}}
dnl {{{ DEF_STRUCT_P_FUNC(1:type, 2:name, 3:body)
define(`DEF_STRUCT_P_FUNC', `
	pushdefFUNC_NAME(`$1', `$2')
/* {`{'{ FUNC_NAME */
	pushdef(`type', `ifelse(`$2', `', `$1', `$2')')
	define(`DEFINED_'type, `')
	ifelse(ifdef(`EXPORT_'PROCESSOR_TYPE`_'type,1)ifdef(`EXPORT_'PROCESSOR_TYPE`_',1)ifdef(`EXPORT__'type,1), `', `',
		`', `', `EXPORT(`DECL_STRUCT_P_FUNC(`$1', `$2')')')
	popdef(`type')
DECL_STRUCT_P_FUNC(`$1', `$2', ` ')
	{
		pushdef(`ELEMENTS_DONE')
		IFAUTOCHECK(`
			/* {{{ init assert */
			ifdef(`SIZEOF_$1', , `m4_errprint(`Warning: missing SIZEOF_$1, safe to ignore')')
			ifdef(`COUNTOF_$1', , `m4_errprint(`Warning: missing COUNTOF_$1, safe to ignore')')
			dnl SIZEOF_x COUNTOF_x can be both defined or both not
			ifdef(`SIZEOF_$1', `
				ifdef(`COUNTOF_$1', , `m4_errprint(`AUTOCHECK WARN: missing COUNTOF_$1')')
			', `
				define(`SIZEOF_$1', 0)
			')
			ifdef(`COUNTOF_$1', `
				ifdef(`SIZEOF_$1', , `m4_errprint(`AUTOCHECK WARN: missing SIZEOF_$1')')
			', `
				define(`COUNTOF_$1', 0)
			')
			int xc_autocheck_assert_size = SIZEOF_$1, assert_count = COUNTOF_$1;
			int xc_autocheck_done_size = 0, xc_autocheck_done_count = 0;
			ifdef(`ELEMENTSOF_$1', `
				const char *xc_autocheck_assert_names[] = { ELEMENTSOF_$1 };
				size_t xc_autocheck_assert_names_count = sizeof(xc_autocheck_assert_names) / sizeof(xc_autocheck_assert_names[0]);
			', `
				const char **xc_autocheck_assert_names = NULL;
				size_t xc_autocheck_assert_names_count = 0;
			')
			zend_bool xc_autocheck_skip = 0;
			HashTable xc_autocheck_done_names;
			zend_hash_init(&xc_autocheck_done_names, 0, NULL, NULL, 0);
			/* }}} */
			IFRESTORE(`assert(xc_is_shm(SRC()));')
			IFCALCSTORE(`assert(!xc_is_shm(SRC()));')
		')
		ifdef(`SIZEOF_$1', , `m4_errprint(`AUTOCHECK WARN: $1: missing structinfo, dont panic')')

		ifdef(`USEMEMCPY', `IFCOPY(`
			memcpy(DST(), SRC(), sizeof($1));
		')')

		IFDPRINT(`
			fprintf(stderr, "%s", "{\n");
			indent ++;
		')
		{
			$3`'
		}
		IFDPRINT(`
			indent --;
			INDENT()fprintf(stderr, "}\n");
		')

		IFAUTOCHECK(`
		/* {{{ autocheck */
		if (!xc_autocheck_skip) {
			int name_check_errors = xc_check_names(__FILE__, __LINE__, "FUNC_NAME", xc_autocheck_assert_names, xc_autocheck_assert_names_count, &xc_autocheck_done_names);

			if (xc_autocheck_done_count != assert_count) {
				fprintf(stderr
					, "count assertion failed at %s `#'%d FUNC_NAME`' : unexpected:%d - expecting:%d = %d != 0\n"
					, __FILE__, __LINE__
					, xc_autocheck_done_count, assert_count, xc_autocheck_done_count - assert_count
					);
			}
			if (xc_autocheck_done_size != xc_autocheck_assert_size) {
				fprintf(stderr
					, "size assertion failed at %s `#'%d FUNC_NAME`' : unexpected:%d - expecting:%d = %d != 0\n"
					, __FILE__, __LINE__
					, xc_autocheck_done_size, xc_autocheck_assert_size, xc_autocheck_done_size - xc_autocheck_assert_size
					);
			}
			if (name_check_errors || xc_autocheck_done_count != assert_count || xc_autocheck_done_size != xc_autocheck_assert_size) {
				assert(0);
			}
		}
		zend_hash_destroy(&xc_autocheck_done_names);
		/* }}} */
		')
		ifdef(`AUTOCHECK_SKIP', `undefine(`AUTOCHECK_SKIP')', `
			ifdef(`ELEMENTSOF_$1', `
				pushdef(`ELEMENTS_UNDONE', LIST_DIFF(defn(`ELEMENTSOF_$1'), defn(`ELEMENTS_DONE')))
				ifelse(defn(`ELEMENTS_UNDONE'), , `m4_errprint(`AUTOCHECK INFO: $1: processor looks good')', `
					m4_errprint(`AUTOCHECK ERROR: ====' PROCESSOR_TYPE `$1 =================')
					m4_errprint(`AUTOCHECK expected:' defn(`ELEMENTSOF_$1'))
					m4_errprint(`AUTOCHECK missing :' defn(`ELEMENTS_UNDONE'))
					define(`EXIT_PENDING', 1)
				')
				popdef(`ELEMENTS_UNDONE')
			')
		')

		popdef(`ELEMENTS_DONE')
	}
/* }`}'} */
	popdef(`FUNC_NAME')
')
dnl }}}
dnl {{{ STRUCT_P_EX(1:type, 2:dst, 3:src, 4:elm-name, 5:name=type, 6:&)
define(`STRUCT_P_EX', `
	DBG(`$0($*)')
	pushdefFUNC_NAME(`$1', `$5')
	ifdef(`DEFINED_'ifelse(`$5', `', `$1', `$5'), `', `m4_errprint(`AUTOCHECK ERROR: Unknown struct "'ifelse(`$5', `', `$1', `$5')`"')define(`EXIT_PENDING', 1)')
	assert(sizeof($1) == sizeof(($6 $3)[0]));
	ifelse(`$6', `', `ALLOC(`$2', `$1')')
ifdef(`DASM_STRUCT_DIRECT', `', `
	IFDASM(`do {
		zval *zv;
		ALLOC_INIT_ZVAL(zv);
		array_init(zv);
	')
')
	FUNC_NAME`'(
		IFDPRINT( `           $6 $3, indent')
		IFCALC(   `processor, $6 $3')
		IFSTORE(  `processor, $6 $2, $6 $3')
		IFRESTORE(`processor, $6 $2, $6 $3')
		PROCRELOCATE(`
			ifelse(`$6', `', `PTR_FROM_VIRTUAL_EX(`$1', `$3')', `$6 $3')
			, ptrdiff
			, relocatediff
		')
		IFDASM(`dasm, ifdef(`DASM_STRUCT_DIRECT', `DST()', `zv'), $6 $3')
		TSRMLS_CC
	);
ifdef(`DASM_STRUCT_DIRECT', `', `
	IFDASM(`
		ifelse(`$4', `[]', `
			add_next_index_zval(DST(), zv);
		', `
			add_assoc_zval_ex(DST(), XCACHE_STRS("$4"), zv);
		')
	} while (0);
	')
')
	popdef(`FUNC_NAME')
	ifelse(`$6', , `RELOCATE_EX(`$1', `$2')')
')
dnl }}}
dnl {{{ STRUCT_P(1:type, 2:elm, 3:name=type)
define(`STRUCT_P', `
	DBG(`$0($*)')
	if (SRC(`$2')) {
		IFDPRINT(`INDENT()`'fprintf(stderr, "$1:$2 ");')
		STRUCT_P_EX(`$1', `DST(`$2')', `SRC(`$2')', `$2', `$3')
	}
	else {
		IFDPRINT(`INDENT()`'fprintf(stderr, "$1:$2:\tNULL\n");')
		COPYNULL_EX(`DST(`$2')', `$2')
	}
	DONE(`$2')
')
dnl }}}
dnl {{{ STRUCT(1:type, 2:elm, 3:name=type)
define(`STRUCT', `
	DBG(`$0($*)')
	assert(sizeof($1) == sizeof(SRC(`$2')));
	IFDPRINT(`INDENT()`'fprintf(stderr, "$1:$2 ");')
	STRUCT_P_EX(`$1', `DST(`$2')', `SRC(`$2')', `$2', `$3', `&')
	DONE(`$2')
')
dnl }}}
dnl {{{ STRUCT_ARRAY(1:count_type, 2:count, 3:type, 4:elm, 5:name=type, 6:loopcounter)
define(`STRUCT_ARRAY', `
	if (SRC(`$4')) {
		ifelse(
			`$6', `', `ifelse(`$1', `', `size_t', `$1') i; pushdef(`LOOPCOUNTER', `i')',
			`', `', `pushdef(`LOOPCOUNTER', `$6')')
		pushdefFUNC_NAME(`$3', `$5')
		IFDASM(`
			zval *arr;
			ALLOC_INIT_ZVAL(arr);
			array_init(arr);

			for (LOOPCOUNTER = 0;
					ifelse(`$2', `', `SRC(`$4[LOOPCOUNTER]')',
					`', `', `LOOPCOUNTER < $2');
					++LOOPCOUNTER) {
				zval *zv;

				ALLOC_INIT_ZVAL(zv);
				array_init(zv);
				FUNC_NAME (dasm, zv, &(SRC(`$4[LOOPCOUNTER]')) TSRMLS_CC);
				add_next_index_zval(arr, zv);
			}
			add_assoc_zval_ex(DST(), XCACHE_STRS("$4"), arr);
		', `
			dnl find count with NULL
			ifelse(`$2', `', `
				size_t count = 0;
				while (SRC(`$4[count]')) {
					++count;
				}
				++count;
				pushdef(`ARRAY_ELEMENT_COUNT', `count')
			',
			`', `', `pushdef(`ARRAY_ELEMENT_COUNT', `$2')')
			ALLOC(`DST(`$4')', `$3', `ARRAY_ELEMENT_COUNT')
			popdef(`ARRAY_ELEMENT_COUNT')

			for (LOOPCOUNTER = 0;
					ifelse(`$2', `', `SRC(`$4[LOOPCOUNTER]')',
					`', `', `LOOPCOUNTER < $2');
					++LOOPCOUNTER) {
				DISABLECHECK(`
					pushdef(`ALLOC')
					pushdef(`RELOCATE_EX')
					STRUCT_P_EX(`$3', (DST(`$4') + LOOPCOUNTER), (SRC(`$4') + LOOPCOUNTER), `', `$5')
					popdef(`RELOCATE_EX')
					popdef(`ALLOC')
				')
			}
			dnl the end marker
			ifelse(`$2', `', `IFCOPY(`DST(`$4[LOOPCOUNTER]') = NULL;')')
		')dnl IFDASM
		RELOCATE(`$3', `$4')
		DONE(`$4')
		popdef(`FUNC_NAME')
		popdef(`LOOPCOUNTER')
	}
	else {
		COPYNULL(`$4')
	}
')
dnl }}}