|
|
@@ -1,164 +1,132 @@
|
|
|
1
|
+#include <assert.h>
|
|
1
|
2
|
#include <errno.h>
|
|
2
|
3
|
#include <string.h>
|
|
3
|
4
|
|
|
|
5
|
+#include "arch.h"
|
|
4
|
6
|
#include "func.h"
|
|
5
|
7
|
#include "node.h"
|
|
6
|
8
|
#include "ply.h"
|
|
7
|
9
|
#include "sym.h"
|
|
|
10
|
+#include "type.h"
|
|
8
|
11
|
|
|
9
|
|
-#if 0
|
|
10
|
|
-/* pid */
|
|
11
|
12
|
|
|
12
|
|
-int pid_generate_ir(prog_t *prog, node_t *node)
|
|
|
13
|
+static struct type *global_map_ktype(struct node *n)
|
|
13
|
14
|
{
|
|
14
|
|
- /* TODO */
|
|
15
|
|
- return 0;
|
|
16
|
|
-}
|
|
|
15
|
+ struct node *map, *key;
|
|
|
16
|
+ struct type *ktype;
|
|
|
17
|
+ int nargs = node_nargs(n);
|
|
17
|
18
|
|
|
18
|
|
-type_t t_pid_t = {
|
|
19
|
|
- .ttype = T_TYPEDEF,
|
|
|
19
|
+ map = n->expr.args;
|
|
20
|
20
|
|
|
21
|
|
- .t.tdef = {
|
|
22
|
|
- .name = "__ply_pid_t",
|
|
23
|
|
- .type = &t_u32,
|
|
24
|
|
- },
|
|
25
|
|
-};
|
|
|
21
|
+ if (node_nargs(n) == 2)
|
|
|
22
|
+ return map->next->sym->type;
|
|
26
|
23
|
|
|
27
|
|
-type_t t_pid_func = {
|
|
28
|
|
- .ttype = T_FUNC,
|
|
29
|
|
-
|
|
30
|
|
- .t.func = {
|
|
31
|
|
- .name = "pid",
|
|
32
|
|
- .type = &t_pid_t,
|
|
33
|
|
- .args = NULL,
|
|
34
|
|
- .generate_ir = pid_generate_ir,
|
|
35
|
|
- },
|
|
36
|
|
-};
|
|
|
24
|
+ assert(0);
|
|
|
25
|
+ return NULL;
|
|
|
26
|
+}
|
|
37
|
27
|
|
|
38
|
|
-type_t *ts_pid[] = { &t_pid_t, &t_pid_func, NULL };
|
|
|
28
|
+static int global_assign_type_infer_map(struct node *n)
|
|
|
29
|
+{
|
|
|
30
|
+ struct node *map, *key;
|
|
|
31
|
+ struct type *ktype;
|
|
39
|
32
|
|
|
|
33
|
+ map = n->expr.args;
|
|
40
|
34
|
|
|
41
|
|
-/* quantize */
|
|
|
35
|
+ for (key = map->next; key; key = key->next) {
|
|
|
36
|
+ node_print(key, stdout);printf("WKZ\n");
|
|
|
37
|
+ if (type_sizeof(key->sym->type) < 0)
|
|
|
38
|
+ return 0;
|
|
|
39
|
+ node_print(key, stdout);printf("WKZ\n");
|
|
|
40
|
+ }
|
|
42
|
41
|
|
|
43
|
|
-int quantize_generate_ir(prog_t *prog, node_t *node)
|
|
44
|
|
-{
|
|
45
|
|
- /* TODO */
|
|
|
42
|
+ map->sym->type = type_map_of(global_map_ktype(n), n->sym->type);
|
|
46
|
43
|
return 0;
|
|
47
|
44
|
}
|
|
48
|
45
|
|
|
49
|
|
-type_t t_u64_64 = {
|
|
50
|
|
- .ttype = T_ARRAY,
|
|
51
|
|
- .t.array = {
|
|
52
|
|
- .type = &t_u64,
|
|
53
|
|
- .len = 64,
|
|
54
|
|
- },
|
|
55
|
|
-};
|
|
|
46
|
+static int global_assign_type_infer(struct prog *prog, struct node *n)
|
|
|
47
|
+{
|
|
|
48
|
+ struct node *lval, *rval;
|
|
56
|
49
|
|
|
57
|
|
-type_t t_quantize_t = {
|
|
58
|
|
- .ttype = T_TYPEDEF,
|
|
|
50
|
+ lval = n->expr.args;
|
|
|
51
|
+ rval = lval->next;
|
|
59
|
52
|
|
|
60
|
|
- .t.tdef = {
|
|
61
|
|
- .name = "__ply_quantize_t",
|
|
62
|
|
- .type = &t_u64_64,
|
|
63
|
|
- },
|
|
64
|
|
-};
|
|
|
53
|
+ if (!rval->sym->type)
|
|
|
54
|
+ return 0;
|
|
65
|
55
|
|
|
66
|
|
-field_t f_quantize_args[] = {
|
|
67
|
|
- { .type = &t_s64 },
|
|
68
|
|
- { .type = NULL }
|
|
69
|
|
-};
|
|
|
56
|
+ if (!lval->sym->type) {
|
|
|
57
|
+ lval->sym->type = rval->sym->type;
|
|
70
|
58
|
|
|
71
|
|
-type_t t_quantize_func = {
|
|
72
|
|
- .ttype = T_FUNC,
|
|
|
59
|
+ if (node_is_map(lval))
|
|
|
60
|
+ return global_assign_type_infer_map(lval);
|
|
73
|
61
|
|
|
74
|
|
- .t.func = {
|
|
75
|
|
- .name = "quantize",
|
|
76
|
|
- .type = &t_quantize_t,
|
|
77
|
|
- .args = f_quantize_args,
|
|
78
|
|
- .generate_ir = quantize_generate_ir,
|
|
79
|
|
- },
|
|
80
|
|
-};
|
|
|
62
|
+ return 0;
|
|
|
63
|
+ }
|
|
81
|
64
|
|
|
82
|
|
-type_t *ts_quantize[] = { &t_u64_64, &t_quantize_t, &t_quantize_func, NULL };
|
|
|
65
|
+ if (type_compatible(lval->sym->type, rval->sym->type))
|
|
|
66
|
+ return 0;
|
|
83
|
67
|
|
|
|
68
|
+ if (lval->ntype == N_IDENT) {
|
|
|
69
|
+ node_error(n, stderr, "conflicting types for '%s'",
|
|
|
70
|
+ lval->ident.name);
|
|
|
71
|
+ } else {
|
|
|
72
|
+ node_error(n, stderr, "conflicting types for '%s'",
|
|
|
73
|
+ lval->expr.args->ident.name);
|
|
|
74
|
+ }
|
|
84
|
75
|
|
|
85
|
|
-/* time */
|
|
|
76
|
+ return -EINVAL;
|
|
|
77
|
+}
|
|
86
|
78
|
|
|
87
|
|
-int time_generate_ir(prog_t *prog, node_t *node)
|
|
|
79
|
+static int global_assign_static_verify(struct node *n)
|
|
88
|
80
|
{
|
|
89
|
|
- ir_emit_call(prog->ir, BPF_FUNC_ktime_get_ns);
|
|
|
81
|
+ struct node *lval;
|
|
|
82
|
+ int nargs = node_nargs(n);
|
|
90
|
83
|
|
|
91
|
|
- irs_alloc_reg(&node->irs, prog->ir);
|
|
92
|
|
- ir_emit_insn(prog->ir, MOV(0, 0), node->irs.reg, BPF_REG_0);
|
|
93
|
|
- return 0;
|
|
|
84
|
+ if (nargs != 2) {
|
|
|
85
|
+ node_error(n, stderr, "assignment operator expects 2 arguments,"
|
|
|
86
|
+ " %d given", nargs);
|
|
|
87
|
+ return -EINVAL;
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ lval = n->expr.args;
|
|
|
91
|
+
|
|
|
92
|
+ if (node_is_map(lval) || (lval->ntype == N_IDENT))
|
|
|
93
|
+ return 0;
|
|
|
94
|
+
|
|
|
95
|
+ node_error(n, stderr, "assignment destination must be a variable or map");
|
|
|
96
|
+ return -EINVAL;
|
|
94
|
97
|
}
|
|
95
|
98
|
|
|
96
|
|
-type_t t_time_t = {
|
|
|
99
|
+struct type t_pid = {
|
|
97
|
100
|
.ttype = T_TYPEDEF,
|
|
98
|
101
|
|
|
99
|
|
- .t.tdef = {
|
|
100
|
|
- .name = "__ply_time_t",
|
|
101
|
|
- .type = &t_s64,
|
|
102
|
|
- },
|
|
|
102
|
+ .tdef = { .name = ":pid", .type = &t_u32 },
|
|
103
|
103
|
};
|
|
104
|
104
|
|
|
105
|
|
-type_t t_time_func = {
|
|
106
|
|
- .ttype = T_FUNC,
|
|
|
105
|
+struct type t_time = {
|
|
|
106
|
+ .ttype = T_TYPEDEF,
|
|
107
|
107
|
|
|
108
|
|
- .t.func = {
|
|
109
|
|
- .name = "time",
|
|
110
|
|
- .type = &t_time_t,
|
|
111
|
|
- .args = NULL,
|
|
112
|
|
- .generate_ir = time_generate_ir,
|
|
113
|
|
- },
|
|
|
108
|
+ .tdef = { .name = ":time", .type = &t_s64 },
|
|
114
|
109
|
};
|
|
115
|
110
|
|
|
116
|
|
-type_t *ts_time[] = { &t_time_t, &t_time_func, NULL };
|
|
117
|
|
-
|
|
118
|
|
-#endif
|
|
119
|
|
-
|
|
120
|
|
-/* int global_resolve(prog_t *prog, node_t *n) */
|
|
121
|
|
-/* { */
|
|
122
|
|
-/* type_t **ts = NULL; */
|
|
123
|
|
-/* type_t *t; */
|
|
124
|
|
-/* int err; */
|
|
125
|
|
-
|
|
126
|
|
-/* if (!strcmp(n->ident, "pid")) { */
|
|
127
|
|
-/* ts = ts_pid; */
|
|
128
|
|
-/* t = &t_pid_func; */
|
|
129
|
|
-/* } else if (!strcmp(n->ident, "quantize")) { */
|
|
130
|
|
-/* ts = ts_quantize; */
|
|
131
|
|
-/* t = &t_quantize_func; */
|
|
132
|
|
-/* } else if (!strcmp(n->ident, "time")) { */
|
|
133
|
|
-/* ts = ts_time; */
|
|
134
|
|
-/* t = &t_time_func; */
|
|
135
|
|
-/* } else { */
|
|
136
|
|
-/* return -ENOENT; */
|
|
137
|
|
-/* } */
|
|
138
|
|
-
|
|
139
|
|
-/* if (ts) { */
|
|
140
|
|
-/* err = type_add_list(ts); */
|
|
141
|
|
-/* if (err) */
|
|
142
|
|
-/* return err; */
|
|
143
|
|
-/* } */
|
|
144
|
|
-
|
|
145
|
|
-/* n->type = t; */
|
|
146
|
|
-/* return sym_add(prog->locals, n->ident, t, &n->sym); */
|
|
147
|
|
-/* } */
|
|
148
|
|
-
|
|
149
|
111
|
static const struct func global_funcs[] = {
|
|
150
|
|
-
|
|
151
|
|
- /* block */
|
|
152
|
|
- { .name = "{}", },
|
|
|
112
|
+ { .name = ":block", .type = &t_void, },
|
|
153
|
113
|
|
|
154
|
114
|
{ .name = "+", },
|
|
155
|
115
|
{ .name = "-", },
|
|
156
|
116
|
|
|
157
|
|
- { .name = "=", },
|
|
158
|
|
- { .name = "[]", },
|
|
|
117
|
+ {
|
|
|
118
|
+ .name = ":assign",
|
|
|
119
|
+ .static_verify = global_assign_static_verify,
|
|
|
120
|
+ .type_infer = global_assign_type_infer,
|
|
|
121
|
+ },
|
|
|
122
|
+ {
|
|
|
123
|
+ .name = ":map",
|
|
|
124
|
+ /* .static_verify = global_map_static_verify, */
|
|
|
125
|
+ /* .type_infer = global_map_type_infer, */
|
|
|
126
|
+ },
|
|
159
|
127
|
|
|
160
|
|
- { .name = "pid", },
|
|
161
|
|
- { .name = "time", },
|
|
|
128
|
+ { .name = "pid", .type = &t_pid, },
|
|
|
129
|
+ { .name = "time", .type = &t_time, },
|
|
162
|
130
|
|
|
163
|
131
|
{ .name = "quantize", },
|
|
164
|
132
|
|
|
|
@@ -167,43 +135,83 @@ static const struct func global_funcs[] = {
|
|
167
|
135
|
|
|
168
|
136
|
static const struct func global_num_func = {
|
|
169
|
137
|
.name = ":num",
|
|
|
138
|
+ .type = &t_int,
|
|
|
139
|
+};
|
|
|
140
|
+
|
|
|
141
|
+struct type t_string_array = {
|
|
|
142
|
+ .ttype = T_ARRAY,
|
|
|
143
|
+
|
|
|
144
|
+ .array = { .type = &t_char, .len = 64 }, /* TODO: tunable */
|
|
|
145
|
+};
|
|
|
146
|
+
|
|
|
147
|
+struct type t_string = {
|
|
|
148
|
+ .ttype = T_TYPEDEF,
|
|
|
149
|
+
|
|
|
150
|
+ .tdef = { .name = ":string", .type = &t_string_array },
|
|
170
|
151
|
};
|
|
171
|
152
|
|
|
172
|
153
|
static const struct func global_string_func = {
|
|
173
|
154
|
.name = ":string",
|
|
|
155
|
+ .type = &t_string,
|
|
174
|
156
|
};
|
|
175
|
157
|
|
|
176
|
158
|
static const struct func global_ident_func = {
|
|
177
|
159
|
.name = ":ident",
|
|
178
|
160
|
};
|
|
179
|
161
|
|
|
|
162
|
+static const struct func *global_sym_alloc_expr(struct node *n)
|
|
|
163
|
+{
|
|
|
164
|
+ const struct func *func;
|
|
|
165
|
+ int err;
|
|
|
166
|
+
|
|
|
167
|
+ for (func = global_funcs; func->name; func++) {
|
|
|
168
|
+ if (strcmp(func->name, n->expr.func))
|
|
|
169
|
+ continue;
|
|
|
170
|
+
|
|
|
171
|
+ return func;
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ return NULL;
|
|
|
175
|
+}
|
|
|
176
|
+
|
|
180
|
177
|
int global_sym_alloc(struct prog *prog, struct node *n)
|
|
181
|
178
|
{
|
|
182
|
179
|
const struct func *func;
|
|
|
180
|
+ struct symtab *st = prog->locals;
|
|
|
181
|
+ int err;
|
|
183
|
182
|
|
|
184
|
183
|
switch (n->ntype) {
|
|
185
|
184
|
case N_EXPR:
|
|
186
|
|
- for (func = global_funcs; func->name; func++) {
|
|
187
|
|
- if (strcmp(func->name, n->expr.func))
|
|
188
|
|
- continue;
|
|
189
|
|
-
|
|
190
|
|
- n->sym = sym_alloc(prog->locals, n, func);
|
|
191
|
|
- return 0;
|
|
192
|
|
- }
|
|
193
|
|
-
|
|
194
|
|
- return -ENOENT;
|
|
|
185
|
+ func = global_sym_alloc_expr(n);
|
|
|
186
|
+ break;
|
|
195
|
187
|
case N_IDENT:
|
|
196
|
|
- n->sym = sym_alloc(prog->globals, n, &global_ident_func);
|
|
197
|
|
- return 0;
|
|
|
188
|
+ st = prog->globals;
|
|
|
189
|
+ func = &global_ident_func;
|
|
|
190
|
+ break;
|
|
198
|
191
|
case N_NUM:
|
|
199
|
|
- n->sym = sym_alloc(prog->locals, n, &global_num_func);
|
|
200
|
|
- return 0;
|
|
|
192
|
+ func = &global_num_func;
|
|
|
193
|
+ break;
|
|
201
|
194
|
case N_STRING:
|
|
202
|
|
- n->sym = sym_alloc(prog->locals, n, &global_string_func);
|
|
203
|
|
- return 0;
|
|
|
195
|
+ func = &global_string_func;
|
|
|
196
|
+ break;
|
|
204
|
197
|
}
|
|
205
|
198
|
|
|
206
|
|
- return -ENOENT;
|
|
|
199
|
+ if (!func)
|
|
|
200
|
+ return -ENOENT;
|
|
|
201
|
+
|
|
|
202
|
+ if (func->static_verify) {
|
|
|
203
|
+ err = func->static_verify(n);
|
|
|
204
|
+ if (err)
|
|
|
205
|
+ return err;
|
|
|
206
|
+ }
|
|
|
207
|
+
|
|
|
208
|
+ n->sym = sym_alloc(st, n, func);
|
|
|
209
|
+
|
|
|
210
|
+ if (func->type)
|
|
|
211
|
+ /* return type is static, fill it now */
|
|
|
212
|
+ n->sym->type = func->type;
|
|
|
213
|
+
|
|
|
214
|
+ return 0;
|
|
207
|
215
|
}
|
|
208
|
216
|
|
|
209
|
217
|
int global_probe(struct prog *prog)
|