forked from chung-leong/qb
/
qb.c
396 lines (345 loc) · 12.3 KB
/
qb.c
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Chung Leong <cleong@cal.berkeley.edu> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_qb.h"
#include "qb.h"
ZEND_DECLARE_MODULE_GLOBALS(qb)
/* True global resources - no need for thread safety here */
/* {{{ qb_functions[]
*
* Every user visible function must have an entry in qb_functions[].
*/
const zend_function_entry qb_functions[] = {
PHP_FE(qb_compile, NULL)
PHP_FE(qb_extract, NULL)
#ifdef PHP_FE_END
PHP_FE_END /* Must be the last line in qb_functions[] */
#else
{NULL, NULL, NULL}
#endif
};
/* }}} */
/* {{{ qb_module_entry
*/
zend_module_entry qb_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"qb",
qb_functions,
PHP_MINIT(qb),
PHP_MSHUTDOWN(qb),
PHP_RINIT(qb), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(qb), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(qb),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_QB
ZEND_GET_MODULE(qb)
#endif
static ZEND_INI_MH(OnUpdatePath) {
if(new_value_length > 1) {
uint32_t i = new_value_length - 1;
int last_char = new_value[i];
if(last_char == '\\' || last_char == '/') {
new_value_length--;
}
#ifdef ZEND_WIN32
for(i = 0; i < new_value_length; i++) {
if(new_value[i] == '/') {
new_value[i] = '\\';
}
}
#endif
}
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
}
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("qb.allow_native_compilation", "0", PHP_INI_SYSTEM, OnUpdateBool, allow_native_compilation, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.allow_memory_map", "1", PHP_INI_SYSTEM, OnUpdateBool, allow_memory_map, zend_qb_globals, qb_globals)
STD_PHP_INI_ENTRY("qb.compiler_path", "", PHP_INI_SYSTEM, OnUpdatePath, compiler_path, zend_qb_globals, qb_globals)
STD_PHP_INI_ENTRY("qb.compiler_env_path", "", PHP_INI_SYSTEM, OnUpdatePath, compiler_env_path, zend_qb_globals, qb_globals)
STD_PHP_INI_ENTRY("qb.native_code_cache_path", "", PHP_INI_SYSTEM, OnUpdatePath, native_code_cache_path, zend_qb_globals, qb_globals)
STD_PHP_INI_ENTRY("qb.execution_log_path", "", PHP_INI_SYSTEM, OnUpdatePath, execution_log_path, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.allow_bytecode_interpretation", "1", PHP_INI_ALL, OnUpdateBool, allow_bytecode_interpretation, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.allow_debugger_inspection", "1", PHP_INI_ALL, OnUpdateBool, allow_debugger_inspection, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.allow_debug_backtrace", "0", PHP_INI_ALL, OnUpdateBool, allow_debug_backtrace, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.debug_with_exact_type", "0", PHP_INI_ALL, OnUpdateBool, debug_with_exact_type, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.compile_to_native", "0", PHP_INI_ALL, OnUpdateBool, compile_to_native, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.show_opcodes", "0", PHP_INI_ALL, OnUpdateBool, show_opcodes, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.show_native_source", "0", PHP_INI_ALL, OnUpdateBool, show_native_source, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.show_compiler_errors", "0", PHP_INI_ALL, OnUpdateBool, show_compiler_errors, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.show_zend_opcodes", "0", PHP_INI_ALL, OnUpdateBool, show_zend_opcodes, zend_qb_globals, qb_globals)
STD_PHP_INI_BOOLEAN("qb.show_pbj_opcodes", "0", PHP_INI_ALL, OnUpdateBool, show_pbj_opcodes, zend_qb_globals, qb_globals)
PHP_INI_END()
/* }}} */
/* {{{ php_qb_init_globals
*/
static void php_qb_init_globals(zend_qb_globals *qb_globals)
{
}
/* }}} */
#if ZEND_ENGINE_2_1
int zend_startup_strtod(void);
int zend_shutdown_strtod(void);
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(qb)
{
ZEND_INIT_MODULE_GLOBALS(qb, php_qb_init_globals, NULL);
REGISTER_INI_ENTRIES();
REGISTER_LONG_CONSTANT("QB_SCAN_FILE", QB_SCAN_FILE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QB_SCAN_ALL", QB_SCAN_ALL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QB_START_DEFERRAL", QB_START_DEFERRAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QB_END_DEFERRAL", QB_END_DEFERRAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QB_PBJ_DETAILS", QB_PBJ_DETAILS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("QB_PBJ_DECLARATION", QB_PBJ_DECLARATION, CONST_CS | CONST_PERSISTENT);
#ifdef VC6_MSVCRT
if(qb_get_vc6_msvcrt_functions() != SUCCESS) {
return FAILURE;
}
#endif
qb_initialize_compiler(TSRMLS_C);
qb_initialize_interpreter(TSRMLS_C);
#if ZEND_ENGINE_2_1
zend_startup_strtod();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(qb)
{
UNREGISTER_INI_ENTRIES();
#if ZEND_ENGINE_2_1
zend_shutdown_strtod();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(qb)
{
uint32_t i;
for(i = 0; i < sizeof(QB_G(static_zvals)) / sizeof(zval); i++) {
zval *value = &QB_G(static_zvals)[i];
#if !ZEND_ENGINE_2_2 && !ZEND_ENGINE_2_1
value->refcount__gc = 1;
value->is_ref__gc = 0;
#else
value->refcount = 2;
value->is_ref = 0;
#endif
value->type = IS_STRING;
}
QB_G(static_zval_index) = 0;
QB_G(current_filename) = NULL;
QB_G(current_line_number) = 0;
QB_G(build_context) = NULL;
QB_G(interpreter_context) = NULL;
QB_G(compiled_functions) = NULL;
QB_G(compiled_function_count) = 0;
QB_G(native_code_bundles) = NULL;
QB_G(native_code_bundle_count) = 0;
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(qb)
{
uint32_t i;
if(QB_G(compiled_functions)) {
for(i = 0; i < QB_G(compiled_function_count); i++) {
qb_function *qfunc = QB_G(compiled_functions)[i];
qb_free_function(qfunc);
}
qb_destroy_array((void **) &QB_G(compiled_functions));
}
if(QB_G(native_code_bundles)) {
for(i = 0; i < QB_G(native_code_bundle_count); i++) {
qb_native_code_bundle *bundle = &QB_G(native_code_bundles)[i];
qb_free_native_code(bundle);
}
qb_destroy_array((void **) &QB_G(native_code_bundles));
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(qb)
{
char buffer[256];
uint32_t i;
qb_diagnostics diag;
php_info_print_table_start();
php_info_print_table_header(2, "qb support", "enabled");
snprintf(buffer, sizeof(buffer), "%d.%d", (int) QB_MAJOR_VERSION, (int) QB_MINOR_VERSION);
php_info_print_table_row(2, "Version", buffer);
php_info_print_table_row(2, "Release Name", QB_FULL_RELEASE_NAME);
#if __SSE4_2__
php_info_print_table_row(2, "SSE version", "4.2");
#elif __SSE4_1__
php_info_print_table_row(2, "SSE version", "4.1");
#elif __SSE3__
php_info_print_table_row(2, "SSE version", "3");
#elif __SSE2__
php_info_print_table_row(2, "SSE version", "2");
#elif __SSE__
php_info_print_table_row(2, "SSE version", "1");
#endif
#if __AVX__
php_info_print_table_row(2, "AVX version", "1");
#endif
#if __AVX2__
php_info_print_table_row(2, "AVX version", "2");
#endif
qb_run_diagnostics(&diag TSRMLS_CC);
php_info_print_table_colspan_header(2, "Diagnostics");
for(i = 1; i < QB_DIAGNOSTIC_SPEED_TEST_COUNTS; i++) {
static const char *test_types[] = {
NULL,
"Integer addition throughput",
"Integer multiplication throughput",
"Integer division throughput",
"Integer multiply-accumulate throughput",
"Floating point addition throughput",
"Floating point multiplication throughput",
"Floating point division throughput",
"Floating point multiply-accumulate throughput",
"Vector addition throughput",
"Vector multiplication throughput",
"Vector division throughput",
"Vector multiply-accumulate throughput",
};
snprintf(buffer, sizeof(buffer), "%g million operations per second", round(diag.instruction_speeds[i] / 1000000));
php_info_print_table_row(2, test_types[i], buffer);
}
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* Remove the following function when you have succesfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto bool qb_compile(callable name, string return_type, array variable_types [, integer options])
Convert PHP instructions to qb instructions */
PHP_FUNCTION(qb_compile)
{
zval *arg1 = NULL, *arg2 = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &arg1, &arg2) == FAILURE) {
return;
}
qb_compile(arg1, arg2 TSRMLS_CC);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool qb_extract(callable name, integer output_type)
Extract information from a given resource */
PHP_FUNCTION(qb_extract)
{
zval *input = NULL;
long output_type;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl|", &input, &output_type) == FAILURE) {
return;
}
qb_extract(input, output_type, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto string qb_execute(string arg)
*/
PHP_FUNCTION(qb_execute)
{
zend_function *zfunc = EG(current_execute_data)->function_state.function;
zval ***args = emalloc(ZEND_NUM_ARGS() * sizeof(zval **));
zval *this = EG(This);
double start_time, end_time, duration;
if(UNEXPECTED(QB_G(execution_log_path)[0])) {
start_time = qb_get_high_res_timestamp();
}
if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
return;
}
qb_execute(zfunc, this, args, ZEND_NUM_ARGS(), return_value TSRMLS_CC);
efree(args);
if(UNEXPECTED(QB_G(execution_log_path)[0])) {
end_time = qb_get_high_res_timestamp();
duration = end_time - start_time;
if(duration > 0) {
qb_function *qfunc = zfunc->op_array.reserved[0];
php_stream *stream = php_stream_open_wrapper_ex(QB_G(execution_log_path), "a", USE_PATH | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL);
if(stream) {
php_stream_printf(stream TSRMLS_CC, "%s\t%s\t%f\n", qfunc->filename, qfunc->name, duration);
php_stream_close(stream);
}
}
}
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
ZEND_ATTRIBUTE_FORMAT(printf, 1, 2)
NO_RETURN void qb_abort(const char *format, ...) {
const char *filename;
uint32_t lineno;
va_list args;
TSRMLS_FETCH();
if(QB_G(current_filename)) {
filename = QB_G(current_filename);
} else {
filename = zend_get_executed_filename(TSRMLS_C);
}
if(QB_G(current_line_number)) {
lineno = QB_G(current_line_number);
} else {
lineno = zend_get_executed_lineno(TSRMLS_C);
}
va_start(args, format);
zend_error_cb(E_ERROR, filename, lineno, format, args);
va_end(args);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/