source: trunk/processor/main.m4 @ 1492

Last change on this file since 1492 was 1492, checked in by moo, 8 weeks ago

disassembler: fix support for assoc/next

  • Property svn:eol-style set to native
File size: 9.4 KB
Line 
1divert(-1)
2dnl ================ start ======================
3dnl define(`XCACHE_ENABLE_TEST')
4dnl define(`DEBUG_SIZE')
5define(`USEMEMCPY')
6
7dnl ================ main
8
9dnl {{{ basic
10define(`REDEF', `ifdef(`$1', `undefine(`$1')') define(`$1', `$2')')
11define(`MAKE_MACRONAME', `translit(`$1', ` ():
12', `_____')')
13define(`ONCE', `ifdef(MAKE_MACRONAME(`ONCE $1'), `', `define(MAKE_MACRONAME(`ONCE $1'))$1')')
14define(`m4_errprint', `ONCE(`errprint(`$1
15')')')
16ifdef(`len', `
17define(`m4_len', defn(`len'))
18undefine(`len')
19')
20define(`dirof', `patsubst(`$1', `[/\\][^/\\]*$', `')')
21ifdef(`__dir__', `', `
22define(`__dir__', `dirof(__file__)')
23')
24define(`XCACHE_STRS', `($1), (sizeof($1))')
25define(`XCACHE_STRL', `($1), (sizeof($1) - 1)')
26define(`SRC', `ifelse(`$1', `', `src', `src->$1')')
27define(`DST', `ifelse(`$1', `', `dst', `dst->$1')')
28dnl ============
29define(`INDENT', `xc_dprint_indent(indent);')
30dnl }}}
31dnl {{{ PTR_FROM_VIRTUAL_EX(1:type, 2:elm)
32define(`PTR_FROM_VIRTUAL_EX', `$2')
33dnl }}}
34dnl {{{ ALLOC(1:dst, 2:type, 3:count=1, 4:clean=false, 5:realtype=$2)
35define(`ALLOC', `
36    pushdef(`COUNT', `ifelse(`$3', `', `1', `$3')')
37    ifdef(`ALLOC_SIZE_HELPER', `
38        pushdef(`SIZE', `ALLOC_SIZE_HELPER()')
39    ', `
40        pushdef(`SIZE', `sizeof($2)ifelse(`$3', `', `', ` * $3')')
41    ')
42    pushdef(`REALTYPE', `ifelse(`$5', , `$2', `$5')')
43    /* allocate */
44    IFCALC(`
45        IFAUTOCHECK(`
46            xc_stack_push(&processor->allocsizes, (void *) (long) (SIZE));
47            xc_stack_push(&processor->allocsizes, (void *) (long) (__LINE__));
48        ')
49        processor->size = (size_t) ALIGN(processor->size);
50        processor->size += SIZE;
51    ')
52    IFSTORE(`
53        IFAUTOCHECK(`{
54            if (!xc_stack_count(&processor->allocsizes)) {
55                fprintf(stderr, "mismatch `$@' at line %d\n", __LINE__);
56            }
57            else {
58                unsigned long expect = (unsigned long) xc_stack_pop(&processor->allocsizes);
59                unsigned long atline = (unsigned long) xc_stack_pop(&processor->allocsizes);
60                unsigned long real = SIZE;
61                if (expect != real) {
62                    fprintf(stderr, "mismatch `$@' at line %d(was %lu): real %lu - expect %lu = %lu\n", __LINE__, atline, real, expect, real - expect);
63                }
64            }
65        }')
66        ifdef(`DEBUG_SIZE', ` {
67            void *oldp = processor->p;
68        ')
69        $1 = (REALTYPE *) (processor->p = (char *) ALIGN(processor->p));
70        ifelse(`$4', `', `
71                IFAUTOCHECK(`memsetptr($1, (void *) (unsigned long) __LINE__, SIZE);')
72            ', `
73                memset($1, 0, SIZE);
74        ')
75        processor->p += SIZE;
76
77        ifdef(`DEBUG_SIZE', `
78            xc_totalsize += (char *) processor->p - (char *) oldp;
79            fprintf(stderr, "%d\t%d\t`'SIZE()\n", (char *) processor->p - (char *) oldp, xc_totalsize);
80        }
81        ')
82    ')
83    IFRESTORE(`ifelse(`$4', `', `
84            ifelse(
85                REALTYPE*COUNT, `zval*1', `ALLOC_ZVAL($1);',
86                REALTYPE*COUNT, `HashTable*1', `ALLOC_HASHTABLE($1);',
87                `', `', `$1 = (REALTYPE *) emalloc(SIZE);')
88            IFAUTOCHECK(`memsetptr($1, (void *) __LINE__, SIZE);')
89        ', `
90            $1 = (REALTYPE *) ecalloc(COUNT, sizeof($2));
91        ')
92    ')
93    popdef(`REALTYPE')
94    popdef(`COUNT')
95    popdef(`SIZE')
96')
97dnl CALLOC(1:dst, 2:type [, 3:count=1, 4:realtype=$2 ])
98define(`CALLOC', `ALLOC(`$1', `$2', `$3', `1', `$4')')
99dnl }}}
100dnl {{{ PROC_CLASS_ENTRY_P(1:elm)
101define(`PROC_CLASS_ENTRY_P', `PROC_CLASS_ENTRY_P_EX(`DST(`$1')', `SRC(`$1')', `$1')`'DONE(`$1')')
102dnl PROC_CLASS_ENTRY_P_EX(1:dst, 2:src, 3:elm-name)
103define(`PROC_CLASS_ENTRY_P_EX', `
104    if ($2) {
105        IFSTORE(`$1 = (zend_class_entry *) xc_get_class_num(processor, $2);')
106        IFRESTORE(`$1 = xc_get_class(processor, (zend_ulong) $2);')
107#ifdef IS_UNICODE
108        IFDASM(`add_assoc_unicodel_ex(DST(), XCACHE_STRS("$3"), ZSTR_U($2->name), $2->name_length, 1);')
109#else
110        IFDASM(`add_assoc_stringl_ex(DST(), XCACHE_STRS("$3"), (char *) $2->name, $2->name_length, 1);')
111#endif
112    }
113    else {
114        COPYNULL_EX(`$1', `$3')
115    }
116')
117dnl }}}
118dnl {{{ IFAUTOCHECKEX
119define(`IFAUTOCHECKEX', `ifdef(`XCACHE_ENABLE_TEST', `$1', `$2')')
120dnl }}}
121dnl {{{ IFAUTOCHECK
122define(`IFAUTOCHECK', `IFAUTOCHECKEX(`
123#ifndef NDEBUG
124        $1
125#endif
126')')
127dnl }}}
128dnl {{{ DBG
129define(`DBG', `ifdef(`XCACHE_ENABLE_TEST', `
130    /* `$1' */
131')')
132dnl }}}
133dnl {{{ EXPORT(1:code)
134define(`EXPORT', `/* export: $1 :export */')
135define(`EXPORTED', `EXPORT(`$1')
136$1')
137define(`EXPORTED_FUNCTION', `EXPORT(`$1;')
138$1')
139dnl }}}
140dnl {{{ EXPORT_PROCESSOR(1:type, 2:processor)
141define(`EXPORT_PROCESSOR', `define(`EXPORT_$1_$2', 1)')
142dnl }}}
143dnl {{{ RELOCATE(1:type, 2:ele)
144define(`RELOCATE', `RELOCATE_EX(`$1', `DST(`$2')')')
145dnl }}}
146dnl {{{ RELOCATE_EX(1:type, 2:dst)
147define(`RELOCATE_EX', `')
148dnl }}}
149dnl {{{ IFNOTMEMCPY
150define(`IFNOTMEMCPY', `ifdef(`USEMEMCPY', `', `$1')')
151dnl }}}
152dnl {{{ COPY
153define(`COPY', `IFNOTMEMCPY(`IFCOPY(`DST(`$1') = SRC(`$1');')')DONE(`$1')')
154dnl }}}
155dnl {{{ COPY_N_EX(1:count, 2:type, 3:dst)
156define(`COPY_N_EX', `
157    ALLOC(`DST(`$3')', `$2', `SRC(`$1')')
158    IFCOPY(`
159        memcpy(DST(`$3'), SRC(`$3'), sizeof(DST(`$3[0]')) * SRC(`$1'));
160        ')
161')
162dnl }}}
163dnl {{{ COPYPOINTER
164define(`COPYPOINTER', `COPY(`$1')')
165dnl }}}
166dnl {{{ SETNULL_EX
167define(`SETNULL_EX', `
168    IFDASM(`
169        ifelse(`$2', `[]', `
170            add_next_index_null(DST());
171        ', `
172            add_assoc_null_ex(DST(), XCACHE_STRS("ifelse(`$2', `', `$1', `$2')"));
173        ')
174    ')
175    IFCOPY(`$1 = NULL;')
176')
177define(`SETNULL', `SETNULL_EX(`DST(`$1')')DONE(`$1')')
178dnl }}}
179dnl {{{ COPYNULL_EX(1:dst, 2:elm-name)
180define(`COPYNULL_EX', `
181    IFDASM(`
182        ifelse(`$2', `[]', `
183            add_next_index_null(DST());
184        ', `
185            add_assoc_null_ex(DST(), XCACHE_STRS("ifelse(`$2', `', `$1', `$2')"));
186        ')
187    ')
188    IFNOTMEMCPY(`IFCOPY(`$1 = NULL;')')
189    assert(patsubst($1, DST(), SRC()) == NULL);
190')
191dnl }}}
192dnl {{{ COPYNULL(1:elm)
193define(`COPYNULL', `
194    COPYNULL_EX(`DST(`$1')', `$1')DONE(`$1')
195')
196dnl }}}
197dnl {{{ COPYZERO_EX(1:dst, 2:elm-name)
198define(`COPYZERO_EX', `
199    IFDASM(`add_assoc_long_ex(DST(), XCACHE_STRS("$2"), 0);')
200    IFNOTMEMCPY(`IFCOPY(`$1 = 0;')')
201    assert(patsubst($1, DST(), SRC()) == 0);
202')
203dnl }}}
204dnl {{{ COPYZERO(1:elm)
205define(`COPYZERO', `
206    COPYZERO_EX(`DST(`$1')', `$1')DONE(`$1')
207')
208dnl }}}
209dnl {{{ LIST_DIFF(1:left-list, 2:right-list)
210define(`foreach',
211       `pushdef(`$1')_foreach(`$1', `$2', `$3')popdef(`$1')')
212define(`_arg1', `$1')
213define(`_foreach',                             
214       `ifelse(`$2', `()', ,                       
215       `define(`$1', _arg1$2)$3`'_foreach(`$1',
216                                                       (shift$2),
217                                                       `$3')')')
218define(`LIST_DIFF', `dnl
219foreach(`i', `($1)', `pushdef(`item_'defn(`i'))')dnl allocate variable for items in $1
220foreach(`i', `($2)', `pushdef(`item_'defn(`i'))undefine(`item_'defn(`i'))')dnl allocate variable for items in $2, and undefine it
221foreach(`i', `($1)', `ifdef(`item_'defn(`i'), `defn(`i') ')')dnl see what is still defined
222foreach(`i', `($2)', `define(`item_'defn(`i'))popdef(`item_'defn(`i'))')dnl
223foreach(`i', `($1)', `popdef(`item_'defn(`i'))')dnl
224')
225dnl }}}
226dnl {{{ DONE_*
227define(`DONE_SIZE', `IFAUTOCHECK(`dnl
228    xc_autocheck_done_size += (int) $1`';
229    xc_autocheck_done_count ++;
230')')
231define(`DONE', `
232    define(`ELEMENTS_DONE', defn(`ELEMENTS_DONE')`,"$1"')
233    IFAUTOCHECK(`dnl
234        if (zend_u_hash_exists(&xc_autocheck_done_names, IS_STRING, "$1", sizeof("$1"))) {
235            fprintf(stderr
236                , "duplicate field at %s `#'%d FUNC_NAME`' : %s\n"
237                , __FILE__, __LINE__
238                , "$1"
239                );
240        }
241        else {
242            zend_uchar b = 1;
243            zend_hash_add(&xc_autocheck_done_names, "$1", sizeof("$1"), (void*)&b, sizeof(b), NULL);
244        }
245    ')
246    DONE_SIZE(`sizeof(SRC(`$1'))')
247')
248define(`DISABLECHECK', `
249    pushdef(`DONE_SIZE')
250    pushdef(`DONE')
251$1
252    popdef(`DONE_SIZE')
253    popdef(`DONE')
254')
255dnl }}}
256dnl {{{ IF**
257define(`IFCALC', `ifelse(PROCESSOR_TYPE, `calc', `$1', `$2')')
258define(`IFSTORE', `ifelse(PROCESSOR_TYPE, `store', `$1', `$2')')
259define(`IFCALCSTORE', `IFSTORE(`$1', `IFCALC(`$1', `$2')')')
260define(`IFRESTORE', `ifelse(PROCESSOR_TYPE, `restore', `$1', `$2')')
261define(`IFCOPY', `IFSTORE(`$1', `IFRESTORE(`$1', `$2')')')
262define(`IFCALCCOPY', `IFCALC(`$1', `IFCOPY(`$1', `$2')')')
263define(`PROCRELOCATE', `ifelse(PROCESSOR_TYPE, `relocate', `$1', `$2')')
264define(`IFRELOCATE', `ifelse(defn(`RELOCATE_EX'), `', `$2', `$1')')
265define(`IFDPRINT', `ifelse(PROCESSOR_TYPE, `dprint', `$1', `$2')')
266define(`IFDASM', `ifelse(PROCESSOR_TYPE, `dasm', `$1', `$2')')
267dnl }}}
268
269EXPORT_PROCESSOR(`dasm',   `zend_op_array')
270EXPORT_PROCESSOR(`dasm',   `zend_function')
271EXPORT_PROCESSOR(`dasm',   `zend_class_entry')
272EXPORT_PROCESSOR(`dasm',   `zend_ast')
273EXPORT_PROCESSOR(`dprint', `zval')
274
275include(__dir__`/hashtable.m4')
276include(__dir__`/string.m4')
277include(__dir__`/struct.m4')
278include(__dir__`/process.m4')
279include(__dir__`/head.m4')
280
281dnl ==== calc ====
282REDEF(`PROCESSOR_TYPE', `calc')
283include(__dir__`/processor.m4')
284
285dnl ==== store ====
286pushdef(`RELOCATE_EX', `$2 = ptradd($1 *, notnullable($2), processor->relocatediff);')
287REDEF(`PROCESSOR_TYPE', `store')
288include(__dir__`/processor.m4')
289popdef(`RELOCATE_EX')
290
291dnl ==== restore ====
292REDEF(`PROCESSOR_TYPE', `restore')
293include(__dir__`/processor.m4')
294
295dnl ==== relocate ====
296pushdef(`PTR_FROM_VIRTUAL_EX', `ptradd($1 *, notnullable($2), ptrdiff)')
297pushdef(`RELOCATE_EX', `$2 = ptradd($1 *, notnullable($2), relocatediff);')
298pushdef(`SRC', defn(`DST'))
299REDEF(`PROCESSOR_TYPE', `relocate')
300include(__dir__`/processor.m4')
301popdef(`SRC')
302popdef(`RELOCATE_EX')
303popdef(`PTR_FROM_VIRTUAL_EX')
304
305dnl ==== dprint ====
306#ifdef HAVE_XCACHE_DPRINT
307REDEF(`PROCESSOR_TYPE', `dprint') include(__dir__`/processor.m4')
308#endif /* HAVE_XCACHE_DPRINT */
309
310dnl ==== dasm ====
311#ifdef HAVE_XCACHE_DISASSEMBLER
312REDEF(`PROCESSOR_TYPE', `dasm') include(__dir__`/processor.m4')
313#endif /* HAVE_XCACHE_DISASSEMBLER */
314
315undefine(`PROCESSOR_TYPE')
316
317include(__dir__`/foot.m4')
318
319ifdef(`EXIT_PENDING', `m4exit(EXIT_PENDING)')
Note: See TracBrowser for help on using the repository browser.