Browse Source

rewrite const math expressions

Tobias Waldekranz.com 8 years ago
parent
commit
9d1fd05e0a
4 changed files with 43 additions and 4 deletions
  1. 5 0
      kprobe.c
  2. 1 1
      node.c
  3. 1 1
      node.h
  4. 36 2
      ply.c

+ 5 - 0
kprobe.c

40
 			 node_string(reg),
40
 			 node_string(reg),
41
 			 NULL);
41
 			 NULL);
42
 
42
 
43
+	new->type = n->type;
44
+	new->list->type = &t_void;
45
+	new->list->next->type = &t_pt_regs;
46
+	new->list->next->list->type = &t_void;
47
+	new->list->next->list->next->type = type_ptr_of(&t_pt_regs);
43
 	return node_replace(n, new);
48
 	return node_replace(n, new);
44
 }
49
 }
45
 
50
 

+ 1 - 1
node.c

35
 		fputs(n->ident, fp);
35
 		fputs(n->ident, fp);
36
 		break;
36
 		break;
37
 	case N_NUM:
37
 	case N_NUM:
38
-		fprintf(fp, "%#"PRIx64, n->num);
38
+		fprintf(fp, "%"PRId64, n->num);
39
 		break;
39
 		break;
40
 	case N_STRING:
40
 	case N_STRING:
41
 		fprintf(fp, "\"%s\"", n->string);
41
 		fprintf(fp, "\"%s\"", n->string);

+ 1 - 1
node.h

12
 typedef enum kwclass {
12
 typedef enum kwclass {
13
 	KW_SUBSCRIPT = '[',
13
 	KW_SUBSCRIPT = '[',
14
 	KW_ASSIGN = '=',
14
 	KW_ASSIGN = '=',
15
-	KW_ARITH = 'a',
15
+	KW_BINOP = 'b',
16
 } kwclass_t;
16
 } kwclass_t;
17
 
17
 
18
 typedef enum kwarith {
18
 typedef enum kwarith {

+ 36 - 2
ply.c

101
 					      node_list(node_ident("pid")),
101
 					      node_list(node_ident("pid")),
102
 					      NULL),
102
 					      NULL),
103
 				   node_vlist(node_ident("quantize"),
103
 				   node_vlist(node_ident("quantize"),
104
-					      node_vlist(node_keyword('a', '-'),
104
+					      node_vlist(node_keyword('b', '-'),
105
 							 node_list(node_ident("time")),
105
 							 node_list(node_ident("time")),
106
 							 node_ident("t0"),
106
 							 node_ident("t0"),
107
 							 NULL),
107
 							 NULL),
191
 
191
 
192
 		return sym_add(dst->sym->st, dst->ident, dst->type, NULL);
192
 		return sym_add(dst->sym->st, dst->ident, dst->type, NULL);
193
 
193
 
194
-	case KW_ARITH:
194
+	case KW_BINOP:
195
 		dst = node_next(n);
195
 		dst = node_next(n);
196
 		src = node_next(dst);
196
 		src = node_next(dst);
197
 		assert(dst && src);
197
 		assert(dst && src);
319
 	return validate_func(n->list);
319
 	return validate_func(n->list);
320
 }
320
 }
321
 
321
 
322
+int rewrite_const_math(node_t *n)
323
+{
324
+	int64_t result;
325
+	node_t *a, *b, *new;
326
+	int op;
327
+
328
+	op = n->list->keyword.op;
329
+	a = node_next(n->list);
330
+	b = node_next(a);
331
+
332
+	switch (op) {
333
+	case '*': result = a->num *  b->num; break;
334
+	case '/': result = a->num /  b->num; break;
335
+	case '%': result = a->num %  b->num; break;
336
+	case '+': result = a->num +  b->num; break;
337
+	case '-': result = a->num -  b->num; break;
338
+	case '<': result = a->num << b->num; break;
339
+	case '>': result = a->num >> b->num; break;
340
+	default: return 0;
341
+	}
342
+
343
+	new = node_num(result);
344
+	new->type = n->type;
345
+	return node_replace(n, new);
346
+}
347
+
322
 int pass_rewrite_ast(node_t *n, void *_prog)
348
 int pass_rewrite_ast(node_t *n, void *_prog)
323
 {
349
 {
324
 	prog_t *prog = _prog;
350
 	prog_t *prog = _prog;
337
 			return err;
363
 			return err;
338
 	}
364
 	}
339
 
365
 
366
+	/* pre-compute binops where both sides are constants */
367
+	if ((n->ntype == N_LIST)
368
+	    && (n->list->ntype == N_KEYWORD)
369
+	    && (n->list->keyword.class == KW_BINOP)
370
+	    && (node_next(n->list)->ntype == N_NUM)
371
+	    && (node_next(node_next(n->list))->ntype == N_NUM))
372
+		return rewrite_const_math(n);
373
+
340
 	return 0;
374
 	return 0;
341
 }
375
 }
342
 
376