Procházet zdrojové kódy

rewrite const math expressions

Tobias Waldekranz.com před 8 roky
rodič
revize
9d1fd05e0a
4 změnil soubory, kde provedl 43 přidání a 4 odebrání
  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,6 +40,11 @@ static int kprobe_rewrite_arg(prog_t *prog, node_t *n)
40 40
 			 node_string(reg),
41 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 48
 	return node_replace(n, new);
44 49
 }
45 50
 

+ 1 - 1
node.c

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

+ 1 - 1
node.h

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

+ 36 - 2
ply.c

@@ -101,7 +101,7 @@ ctx_t *ctx_get(void)
101 101
 					      node_list(node_ident("pid")),
102 102
 					      NULL),
103 103
 				   node_vlist(node_ident("quantize"),
104
-					      node_vlist(node_keyword('a', '-'),
104
+					      node_vlist(node_keyword('b', '-'),
105 105
 							 node_list(node_ident("time")),
106 106
 							 node_ident("t0"),
107 107
 							 NULL),
@@ -191,7 +191,7 @@ int infer_type_keyword(prog_t *prog, node_t *n)
191 191
 
192 192
 		return sym_add(dst->sym->st, dst->ident, dst->type, NULL);
193 193
 
194
-	case KW_ARITH:
194
+	case KW_BINOP:
195 195
 		dst = node_next(n);
196 196
 		src = node_next(dst);
197 197
 		assert(dst && src);
@@ -319,6 +319,32 @@ int pass_validate_types(node_t *n, void *_prog)
319 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 348
 int pass_rewrite_ast(node_t *n, void *_prog)
323 349
 {
324 350
 	prog_t *prog = _prog;
@@ -337,6 +363,14 @@ int pass_rewrite_ast(node_t *n, void *_prog)
337 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 374
 	return 0;
341 375
 }
342 376