浏览代码

fix node_walk, add symtab_populate pass

Tobias Waldekranz.com 8 年之前
父节点
当前提交
1365017318
共有 7 个文件被更改,包括 104 次插入20 次删除
  1. 7 9
      node.c
  2. 8 4
      node.h
  3. 56 7
      ply.c
  4. 17 0
      sym.c
  5. 6 0
      sym.h
  6. 4 0
      type.c
  7. 6 0
      type.h

+ 7 - 9
node.c

71
 	if (n->ntype == N_EXPR)
71
 	if (n->ntype == N_EXPR)
72
 		info->indent -= 2;
72
 		info->indent -= 2;
73
 
73
 
74
-	if (n->up && n->up->ntype == N_EXPR && n->up->expr.arg == n) {
75
-		for (next = n->next; next; next = next->next) {
76
-			node_dump(next, info);
77
-		}
78
-	}
79
-
80
 	return 0;
74
 	return 0;
81
 }
75
 }
82
 
76
 
97
 		return err;
91
 		return err;
98
 
92
 
99
 	if (n->ntype == N_EXPR) {
93
 	if (n->ntype == N_EXPR) {
100
-		err = node_walk(n->expr.arg, pre, post, ctx);
101
-		if (err)
102
-			return err;
94
+		node_t *a = n->expr.arg;
95
+
96
+		for (a = n->expr.arg; a; a = a->next) {
97
+			err = node_walk(a, pre, post, ctx);
98
+			if (err)
99
+				return err;
100
+		}
103
 	}
101
 	}
104
 
102
 
105
 	if (post && (err = post(n, ctx)))
103
 	if (post && (err = post(n, ctx)))

+ 8 - 4
node.h

3
 
3
 
4
 #include <stdint.h>
4
 #include <stdint.h>
5
 
5
 
6
+typedef struct sym sym_t;
7
+
6
 typedef struct atom atom_t;
8
 typedef struct atom atom_t;
7
 typedef struct expr expr_t;
9
 typedef struct expr expr_t;
8
 typedef struct node node_t;
10
 typedef struct node node_t;
52
 	node_t *up;
54
 	node_t *up;
53
 	node_t *next;
55
 	node_t *next;
54
 
56
 
57
+	sym_t *sym;
58
+
55
 	union {
59
 	union {
56
 		atom_t atom;
60
 		atom_t atom;
57
 		expr_t expr;
61
 		expr_t expr;
58
 	};
62
 	};
59
 };
63
 };
60
 
64
 
61
-int node_walk(node_t *n,
62
-	      int (*pre)(node_t *, void *),
63
-	      int (*post)(node_t *, void *),
64
-	      void *ctx);
65
+/* walk a node tree */
66
+
67
+typedef int (*walk_fn)(node_t *, void *);
68
+int node_walk(node_t *n, walk_fn pre, walk_fn post, void *ctx);
65
 
69
 
66
 
70
 
67
 /* node constructors */
71
 /* node constructors */

+ 56 - 7
ply.c

4
 #include "node.h"
4
 #include "node.h"
5
 #include "sym.h"
5
 #include "sym.h"
6
 
6
 
7
+typedef struct pass {
8
+	int (*run)(void *);
9
+	walk_fn pre;
10
+	walk_fn post;
11
+} pass_t;
12
+
13
+typedef struct provider {
14
+	
15
+} provider_t;
16
+
7
 typedef struct prog {
17
 typedef struct prog {
8
-	char *probe;
18
+	const char *probe;
19
+	provider_t *prov;
9
 	node_t *ast;
20
 	node_t *ast;
10
-
11
-	symtab_t locals;
12
-	symtab_t *globals;
21
+	symtab_t *st;
13
 } prog_t;
22
 } prog_t;
14
 
23
 
15
-symtab_t globals = { .sym = NULL, .len = 0 };
24
+symtab_t symbols = { .sym = NULL, .len = 0 };
16
 
25
 
17
 prog_t *prog_get(void)
26
 prog_t *prog_get(void)
18
 {
27
 {
20
 
29
 
21
 	p = calloc(1, sizeof(*p));
30
 	p = calloc(1, sizeof(*p));
22
 
31
 
23
-	p->globals = &globals,
32
+	p->st = &symbols,
24
 
33
 
25
 	/* reads{pid()} @ quantize(arg2) */
34
 	/* reads{pid()} @ quantize(arg2) */
26
 	p->ast =
35
 	p->ast =
44
 	return p;
53
 	return p;
45
 }
54
 }
46
 
55
 
56
+int symtab_populate(node_t *n, void *_prog)
57
+{
58
+	prog_t *prog = _prog;
59
+
60
+	if ((n->ntype != N_ATOM) || (n->atom.atype != A_IDENT))
61
+		return 0;
62
+
63
+	/* .IDENT/->IDENT is a struct/union member, skip */
64
+	if (n->up
65
+	    && (n->up->ntype == N_EXPR)
66
+	    && ((n->up->expr.etype == E_DOT) || (n->up->expr.etype == E_DEREF))
67
+	    && (n != n->up->expr.arg))
68
+		return 0;
69
+
70
+	return sym_add(prog->st, n->atom.ident, tid_none, &n->sym);
71
+}
72
+
73
+int symtab_resolve(void *_prog)
74
+{
75
+	prog_t *prog = _prog;
76
+	return 0;
77
+}
78
+
79
+pass_t passes[] = {
80
+	{ .pre = symtab_populate },
81
+	{ .run = symtab_resolve },
82
+	{ NULL, NULL, NULL }
83
+};
84
+
47
 int main(void)
85
 int main(void)
48
 {
86
 {
49
 	node_dump_info_t info = {
87
 	node_dump_info_t info = {
52
 	};
90
 	};
53
 
91
 
54
 	prog_t *p = prog_get();
92
 	prog_t *p = prog_get();
93
+	pass_t *pass;
94
+	int err;
95
+
96
+	for (pass = passes; pass->run, pass->pre || pass->post; pass++) {
97
+		err = pass->run ?
98
+			pass->run(p) :
99
+			node_walk(p->ast, pass->pre, pass->post, p);
100
+		if (err)
101
+			break;
102
+	}
55
 
103
 
56
 	node_dump(p->ast, &info);
104
 	node_dump(p->ast, &info);
57
-	return 0;
105
+	symtab_dump(p->st, stdout);
106
+	return err;
58
 }
107
 }

+ 17 - 0
sym.c

47
 
47
 
48
 	return 0;
48
 	return 0;
49
 }
49
 }
50
+
51
+void sym_dump(sym_t *sym, FILE *fp)
52
+{
53
+	type_dump_cdecl(type_info(sym->tid), fp);
54
+	fputc(' ', fp);
55
+	fputs(sym->name, fp);
56
+}
57
+
58
+void symtab_dump(symtab_t *st, FILE *fp)
59
+{
60
+	size_t i;
61
+
62
+	for (i = 0; i < st->len; i++) {
63
+		sym_dump(&st->sym[i], fp);
64
+		fputc('\n', fp);
65
+	}
66
+}

+ 6 - 0
sym.h

13
 	size_t len;
13
 	size_t len;
14
 } symtab_t;
14
 } symtab_t;
15
 
15
 
16
+sym_t *sym_get(symtab_t *st, const char *name);
17
+int    sym_add(symtab_t *st, const char *name, tid_t tid, sym_t **new);
18
+
19
+void sym_dump(sym_t *sym, FILE *fp);
20
+void symtab_dump(symtab_t *st, FILE *fp);
21
+
16
 #endif	/* _PLY_SYM_H */
22
 #endif	/* _PLY_SYM_H */

+ 4 - 0
type.c

54
 void type_dump(type_t *t, FILE *fp)
54
 void type_dump(type_t *t, FILE *fp)
55
 {
55
 {
56
 	switch (t->type){
56
 	switch (t->type){
57
+	case T_NONE:
58
+		fputs("<NONE>", fp);
59
+		break;
57
 	case T_ALIAS:
60
 	case T_ALIAS:
58
 		fputs(t->t.alias.name, fp);
61
 		fputs(t->t.alias.name, fp);
59
 		break;
62
 		break;
82
 		type_dump(type_info(t->t.alias.type), fp);
85
 		type_dump(type_info(t->t.alias.type), fp);
83
 		fprintf(fp, " %s", t->t.alias.name);
86
 		fprintf(fp, " %s", t->t.alias.name);
84
 		break;
87
 		break;
88
+	case T_NONE:
85
 	case T_SCALAR:
89
 	case T_SCALAR:
86
 	case T_POINTER:
90
 	case T_POINTER:
87
 	case T_ARRAY:
91
 	case T_ARRAY:

+ 6 - 0
type.h

109
 #undef POINTER
109
 #undef POINTER
110
 } builtin_type_t;
110
 } builtin_type_t;
111
 
111
 
112
+
113
+type_t *type_info(tid_t tid);
114
+
115
+void type_dump      (type_t *t, FILE *fp);
116
+void type_dump_cdecl(type_t *t, FILE *fp);
117
+
112
 #endif	/* _PLY_TYPE_H */
118
 #endif	/* _PLY_TYPE_H */