This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fcall.c
140 lines (108 loc) · 4.16 KB
/
fcall.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
/*
+--------------------------------------------------------------------------+
| Zephir Language |
+--------------------------------------------------------------------------+
| Copyright (c) 2013-2014 Zephir Team and contributors |
+--------------------------------------------------------------------------+
| This source file is subject the MIT license, that is bundled with |
| this package in the file LICENSE, and is available through the |
| world-wide-web at the following url: |
| http://zephir-lang.com/license.html |
| |
| If you did not receive a copy of the MIT license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+--------------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include "php_zephir.h"
#include "zephir.h"
#include "utils.h"
#include "errors.h"
#include "expr.h"
#include "kernel/main.h"
#include "kernel/string.h"
LLVMValueRef *zephir_resolve_parameters(zephir_context *context, zval *parameters TSRMLS_DC)
{
HashTable *ht = Z_ARRVAL_P(parameters);
HashPosition pos = {0};
zval **expr, *parameter;
LLVMValueRef *args;
zephir_compiled_expr *compiled_expr;
int i = 0;
args = emalloc(sizeof(LLVMValueRef) * zend_hash_num_elements(Z_ARRVAL_P(parameters)));
zend_hash_internal_pointer_reset_ex(ht, &pos);
for (
; zend_hash_get_current_data_ex(ht, (void**) &expr, &pos) == SUCCESS
; zend_hash_move_forward_ex(ht, &pos)
) {
_zephir_array_fetch_string(¶meter, *expr, SS("parameter") TSRMLS_CC);
if (Z_TYPE_P(parameter) != IS_ARRAY) {
zephir_error(parameter, "Corrupt parameter");
}
compiled_expr = zephir_expr(context, parameter);
switch (compiled_expr->type) {
case ZEPHIR_T_TYPE_VAR:
args[i] = LLVMBuildLoad(context->builder, compiled_expr->variable->value_ref, "");
break;
default:
fprintf(stderr, "%d\n", compiled_expr->type);
}
efree(compiled_expr);
i++;
}
return args;
}
static LLVMValueRef zephir_get_strlen(zephir_context *context)
{
LLVMValueRef function;
LLVMTypeRef arg_tys[1];
function = LLVMGetNamedFunction(context->module, "zephir_strlen");
if (!function) {
arg_tys[0] = context->types.zval_pointer_type;
#if ZEPHIR_32
function = LLVMAddFunction(context->module, "zephir_strlen", LLVMFunctionType(LLVMInt32Type(), arg_tys, 1, 0));
#else
function = LLVMAddFunction(context->module, "zephir_strlen", LLVMFunctionType(LLVMInt64Type(), arg_tys, 1, 0));
#endif
if (!function) {
zend_error(E_ERROR, "Cannot register zephir_strlen");
}
LLVMAddGlobalMapping(context->engine, function, zephir_fast_strlen_ev);
LLVMSetFunctionCallConv(function, LLVMCCallConv);
LLVMAddFunctionAttr(function, LLVMNoUnwindAttribute);
}
return function;
}
/**
*
*/
zephir_compiled_expr *zephir_strlen_optimizer(zephir_context *context, zval *expr TSRMLS_DC) {
zval *parameters;
zephir_compiled_expr *compiled_expr;
LLVMValueRef *args;
_zephir_array_fetch_string(¶meters, expr, SS("parameters") TSRMLS_CC);
if (Z_TYPE_P(parameters) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(parameters)) != 1) {
zephir_error(expr, "'strlen' optimizer requires 1 parameter");
}
compiled_expr = emalloc(sizeof(zephir_compiled_expr));
compiled_expr->type = ZEPHIR_T_TYPE_LONG;
args = zephir_resolve_parameters(context, parameters);
compiled_expr->value = LLVMBuildCall(context->builder, zephir_get_strlen(context), args, 1, "");
efree(args);
return compiled_expr;
}
zephir_compiled_expr *zephir_fcall_compile(zephir_context *context, zval *expr TSRMLS_DC) {
zval *name;
_zephir_array_fetch_string(&name, expr, SS("name") TSRMLS_CC);
if (Z_TYPE_P(name) != IS_STRING) {
return NULL;
}
if (!memcmp(Z_STRVAL_P(name), SS("strlen"))) {
return zephir_strlen_optimizer(context, expr);
}
return NULL;
}