source: trunk/processor/main.m4 @ 1475

Last change on this file since 1475 was 1475, checked in by moo, 9 months ago

processor: code clean up

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