Browse Source

fix node_walk, add symtab_populate pass

Tobias Waldekranz.com 8 years ago
parent
commit
1365017318
7 changed files with 104 additions and 20 deletions
  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,12 +71,6 @@ int __node_dump_post(node_t *n, void *_info)
71 71
 	if (n->ntype == N_EXPR)
72 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 74
 	return 0;
81 75
 }
82 76
 
@@ -97,9 +91,13 @@ int node_walk(node_t *n,
97 91
 		return err;
98 92
 
99 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 103
 	if (post && (err = post(n, ctx)))

+ 8 - 4
node.h

@@ -3,6 +3,8 @@
3 3
 
4 4
 #include <stdint.h>
5 5
 
6
+typedef struct sym sym_t;
7
+
6 8
 typedef struct atom atom_t;
7 9
 typedef struct expr expr_t;
8 10
 typedef struct node node_t;
@@ -52,16 +54,18 @@ struct node {
52 54
 	node_t *up;
53 55
 	node_t *next;
54 56
 
57
+	sym_t *sym;
58
+
55 59
 	union {
56 60
 		atom_t atom;
57 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 71
 /* node constructors */

+ 56 - 7
ply.c

@@ -4,15 +4,24 @@
4 4
 #include "node.h"
5 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 17
 typedef struct prog {
8
-	char *probe;
18
+	const char *probe;
19
+	provider_t *prov;
9 20
 	node_t *ast;
10
-
11
-	symtab_t locals;
12
-	symtab_t *globals;
21
+	symtab_t *st;
13 22
 } prog_t;
14 23
 
15
-symtab_t globals = { .sym = NULL, .len = 0 };
24
+symtab_t symbols = { .sym = NULL, .len = 0 };
16 25
 
17 26
 prog_t *prog_get(void)
18 27
 {
@@ -20,7 +29,7 @@ prog_t *prog_get(void)
20 29
 
21 30
 	p = calloc(1, sizeof(*p));
22 31
 
23
-	p->globals = &globals,
32
+	p->st = &symbols,
24 33
 
25 34
 	/* reads{pid()} @ quantize(arg2) */
26 35
 	p->ast =
@@ -44,6 +53,35 @@ prog_t *prog_get(void)
44 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 85
 int main(void)
48 86
 {
49 87
 	node_dump_info_t info = {
@@ -52,7 +90,18 @@ int main(void)
52 90
 	};
53 91
 
54 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 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,3 +47,20 @@ found:
47 47
 
48 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,4 +13,10 @@ typedef struct symtab {
13 13
 	size_t len;
14 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 22
 #endif	/* _PLY_SYM_H */

+ 4 - 0
type.c

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

+ 6 - 0
type.h

@@ -109,4 +109,10 @@ BUILTIN_TYPES
109 109
 #undef POINTER
110 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 118
 #endif	/* _PLY_TYPE_H */