Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 7 commits
  • 9 files changed
  • 0 comments
  • 1 contributor
Dec 31, 2011
Werner Almesberger test/Common: change indentation from 4 spaces to 1 tab
This is just cosmetic. No change in function.
7c8a1d2
Jan 02, 2012
Werner Almesberger test/Data/flame.fnp: reformatted "Krash - Digital Flame.fnp" with new…
… syntax

Also added infrastructure for comparing two patches to the test
framework.
aceb5b5
Werner Almesberger compiler: added constant folding of sqrt()
This also means that ptest now requires libm.
709f98b
Jan 03, 2012
Werner Almesberger compiler/Makefile: new targets "leak" and "leaks" to search for memor…
…y leaks

This also uses valgrind. Note that we still have plenty of memory
leaks, so the regression tests won't pass with "make leak".
55ca1d1
Werner Almesberger compiler.c: also free identifiers when parsing failed 23d34b7
Werner Almesberger ptest.c: plugged various memory leaks in test framework 61ab85c
Werner Almesberger parser.y: don't explicitly free items we don't need anyway
We never access some identifiers, so the only effect of explicitly
free(3)ing them is that this creates a reference which in turn prevents
lemon from generating code that does the freeing automatically.
b86463c
6  src/compiler/Makefile
... ...
@@ -1,5 +1,5 @@
1 1
 .PHONY:		all clean
2  
-.PHONY:		test tests valgrind
  2
+.PHONY:		test tests valgrind leak leaks
3 3
 
4 4
 all:
5 5
 		$(MAKE) -C ptest
@@ -16,6 +16,10 @@ test tests:     all
16 16
 valgrind:
17 17
 		VALGRIND="valgrind -q" $(MAKE) tests
18 18
 
  19
+leak leaks:
  20
+		VALGRIND="valgrind -q --leak-check=full --show-reachable=yes" \
  21
+		    $(MAKE) tests
  22
+
19 23
 # ----- Cleanup ---------------------------------------------------------------
20 24
 
21 25
 clean:
1  src/compiler/compiler.c
@@ -683,6 +683,7 @@ struct patch *patch_compile(const char *basedir, const char *patch_code,
683 683
 	return p;
684 684
 
685 685
 fail:
  686
+	unique_free();
686 687
 	free(sc->p);
687 688
 	free(sc);
688 689
 	return NULL;
32  src/compiler/parser.y
@@ -398,9 +398,12 @@ primary_expr(N) ::= unary_misc(I) TOK_LPAREN expr(A) TOK_RPAREN. {
398 398
 	free(I);
399 399
 }
400 400
 
401  
-primary_expr(N) ::= TOK_SQR(I) TOK_LPAREN expr(A) TOK_RPAREN. {
  401
+primary_expr(N) ::= TOK_SQR TOK_LPAREN expr(A) TOK_RPAREN. {
402 402
 	FOLD_UNARY(N, op_sqr, "sqr", A, a*a);
403  
-	free(I);
  403
+}
  404
+
  405
+primary_expr(N) ::= TOK_SQRT TOK_LPAREN expr(A) TOK_RPAREN. {
  406
+	FOLD_UNARY(N, op_sqrt, "sqrt", A, sqrtf(a));
404 407
 }
405 408
 
406 409
 
@@ -413,44 +416,33 @@ primary_expr(N) ::= binary_misc(I) TOK_LPAREN expr(A) TOK_COMMA expr(B)
413 416
 	free(I);
414 417
 }
415 418
 
416  
-primary_expr(N) ::= TOK_ABOVE(I) TOK_LPAREN expr(A) TOK_COMMA expr(B)
417  
-     TOK_RPAREN. {
  419
+primary_expr(N) ::= TOK_ABOVE TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
418 420
 	FOLD_BINARY(N, op_above, "above", A, B, a > b);
419  
-	free(I);
420 421
 }
421 422
 
422  
-primary_expr(N) ::= TOK_BELOW(I) TOK_LPAREN expr(A) TOK_COMMA expr(B)
423  
-     TOK_RPAREN. {
  423
+primary_expr(N) ::= TOK_BELOW TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
424 424
 	FOLD_BINARY(N, op_below, "below", A, B, a < b);
425  
-	free(I);
426 425
 }
427 426
 
428  
-primary_expr(N) ::= TOK_EQUAL(I) TOK_LPAREN expr(A) TOK_COMMA expr(B)
429  
-     TOK_RPAREN. {
  427
+primary_expr(N) ::= TOK_EQUAL TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
430 428
 	FOLD_BINARY(N, op_equal, "equal", A, B, a == b);
431  
-	free(I);
432 429
 }
433 430
 
434  
-primary_expr(N) ::= TOK_MAX(I) TOK_LPAREN expr(A) TOK_COMMA expr(B)
435  
-     TOK_RPAREN. {
  431
+primary_expr(N) ::= TOK_MAX TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
436 432
 	FOLD_BINARY(N, op_max, "max", A, B, a > b ? a : b);
437  
-	free(I);
438 433
 }
439 434
 
440  
-primary_expr(N) ::= TOK_MIN(I) TOK_LPAREN expr(A) TOK_COMMA expr(B)
441  
-     TOK_RPAREN. {
  435
+primary_expr(N) ::= TOK_MIN TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_RPAREN. {
442 436
 	FOLD_BINARY(N, op_min, "min", A, B, a < b ? a : b);
443  
-	free(I);
444 437
 }
445 438
 
446 439
 
447 440
 /* ----- Trinary functions ------------------------------------------------- */
448 441
 
449 442
 
450  
-primary_expr(N) ::= TOK_IF(I) TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_COMMA
  443
+primary_expr(N) ::= TOK_IF TOK_LPAREN expr(A) TOK_COMMA expr(B) TOK_COMMA
451 444
     expr(C) TOK_RPAREN. {
452 445
 	N = conditional(A, B, C);
453  
-	free(I);
454 446
 }
455 447
 
456 448
 
@@ -502,7 +494,7 @@ unary_misc(O) ::= TOK_ISIN(I).	{ O = I; }
502 494
 unary_misc(O) ::= TOK_QUAKE(I).	{ O = I; }
503 495
 unary_misc(O) ::= TOK_SIN(I).	{ O = I; }
504 496
 unary(O) ::= TOK_SQR(I).	{ O = I; }
505  
-unary_misc(O) ::= TOK_SQRT(I).	{ O = I; }
  497
+unary(O) ::= TOK_SQRT(I).	{ O = I; }
506 498
 
507 499
 binary(O) ::= TOK_ABOVE(I).	{ O = I; }
508 500
 binary(O) ::= TOK_BELOW(I).	{ O = I; }
3  src/compiler/ptest/Makefile
@@ -6,6 +6,7 @@ CFLAGS_STANDALONE = -DSTANDALONE=\"standalone.h\"
6 6
 CFLAGS = -Wall -g -I.. -I. $(CFLAGS_STANDALONE)
7 7
 OBJS = ptest.o scanner.o parser.o parser_helper.o unique.o compiler.o \
8 8
        libfpvm.a
  9
+LDLIBS = -lm
9 10
 
10 11
 # ----- Verbosity control -----------------------------------------------------
11 12
 
@@ -32,7 +33,7 @@ fpvm:
32 33
 		$(GEN) ln -s $(FPVM_H) fpvm
33 34
 
34 35
 ptest:		$(OBJS)
35  
-		$(CC) $(CFLAGS) -o $@ $^
  36
+		$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)
36 37
 
37 38
 %.o:		../%.c
38 39
 		$(CC) $(CFLAGS) -c -o $@ $<
23  src/compiler/ptest/ptest.c
@@ -19,10 +19,12 @@
19 19
 #include "../parser_helper.h"
20 20
 #include "../parser.h"
21 21
 #include "../compiler.h"
  22
+#include "../unique.h"
22 23
 
23 24
 
24 25
 static int quiet = 0;
25 26
 static const char *fail = NULL;
  27
+static const char *buffer;
26 28
 
27 29
 
28 30
 static void dump_ast(const struct ast_node *ast);
@@ -190,7 +192,6 @@ static const char *assign_image_name(struct parser_comm *comm,
190 192
 static void report(const char *s)
191 193
 {
192 194
 	fprintf(stderr, "%s\n", s);
193  
-	exit(1);
194 195
 }
195 196
 
196 197
 
@@ -237,6 +238,7 @@ static void parse_only(const char *pgm)
237 238
 	const char *error;
238 239
 
239 240
 	error = parse(pgm, TOK_START_ASSIGN, &comm);
  241
+	unique_free();
240 242
 	if (!error)
241 243
 		return;
242 244
 	fflush(stdout);
@@ -306,6 +308,12 @@ static void compile(const char *pgm)
306 308
 }
307 309
 
308 310
 
  311
+static void free_buffer(void)
  312
+{
  313
+	free((void *) buffer);
  314
+}
  315
+
  316
+
309 317
 static void usage(const char *name)
310 318
 {
311 319
 	fprintf(stderr,
@@ -322,7 +330,6 @@ static void usage(const char *name)
322 330
 int main(int argc, char **argv)
323 331
 {
324 332
 	int c;
325  
-	const char *buf;
326 333
 	int codegen = 0;
327 334
 	unsigned long repeat = 1;
328 335
 	char *end;
@@ -352,10 +359,11 @@ int main(int argc, char **argv)
352 359
 
353 360
 	switch (argc-optind) {
354 361
 	case 0:
355  
-		buf = read_stdin();
  362
+		buffer = read_stdin();
  363
+		atexit(free_buffer);
356 364
 		break;
357 365
 	case 1:
358  
-		buf = argv[optind];
  366
+		buffer = argv[optind];
359 367
 		break;
360 368
 	default:
361 369
 		usage(*argv);
@@ -363,13 +371,10 @@ int main(int argc, char **argv)
363 371
 
364 372
 	while (repeat--) {
365 373
 		if (codegen)
366  
-			compile(buf);
  374
+			compile(buffer);
367 375
 		else
368  
-			parse_only(buf);
  376
+			parse_only(buffer);
369 377
 	}
370 378
 
371  
-	if (argc == optind)
372  
-		free((void *) buf);
373  
-
374 379
 	return 0;
375 380
 }
91  src/compiler/test/Common
@@ -14,39 +14,82 @@
14 14
 
15 15
 ptest()
16 16
 {
17  
-    echo -n "$1: " 1>&2
18  
-    shift
19  
-    $VALGRIND ${PTST:-../ptest/ptest} "$@" >_out 2>&1 || {
20  
-	echo FAILED "($SCRIPT)" 1>&2
21  
-	cat _out
22  
-	rm -f _out
23  
-	exit 1
24  
-    }
  17
+	echo -n "$1: " 1>&2
  18
+	shift
  19
+	$VALGRIND ${PTST:-../ptest/ptest} "$@" >_out 2>&1 || {
  20
+		echo FAILED "($SCRIPT)" 1>&2
  21
+		cat _out
  22
+		rm -f _out
  23
+		exit 1
  24
+	}
25 25
 }
26 26
 
27 27
 
28 28
 ptest_fail()
29 29
 {
30  
-    echo -n "$1: " 1>&2
31  
-    shift
32  
-    $VALGRIND ${PTST:-../ptest/ptest} "$@" >_out 2>&1 && {
33  
-	echo FAILED "($SCRIPT)" 1>&2
34  
-	cat _out
35  
-	rm -f _out
36  
-	exit 1
37  
-    }
  30
+	echo -n "$1: " 1>&2
  31
+	shift
  32
+	$VALGRIND ${PTST:-../ptest/ptest} "$@" >_out 2>&1 && {
  33
+		echo FAILED "($SCRIPT)" 1>&2
  34
+		cat _out
  35
+		rm -f _out
  36
+		exit 1
  37
+	}
38 38
 }
39 39
 
40 40
 
41 41
 expect()
42 42
 {
43  
-    diff -u - "$@" _out >_diff || {
44  
-	echo FAILED "($SCRIPT)" 1>&2
45  
-	cat _diff 1>&2
  43
+	diff -u - "$@" _out >_diff || {
  44
+		echo FAILED "($SCRIPT)" 1>&2
  45
+		cat _diff 1>&2
  46
+		rm -f _out _diff
  47
+		exit 1
  48
+	}
  49
+	echo PASSED 1>&2
46 50
 	rm -f _out _diff
47  
-	exit 1
48  
-    }
49  
-    echo PASSED 1>&2
50  
-    rm -f _out _diff
51  
-    passed=`expr ${passed:-0} + 1`
  51
+	passed=`expr ${passed:-0} + 1`
  52
+}
  53
+
  54
+
  55
+sedit()
  56
+{
  57
+	sed "$1" _out >_tmp || exit 1
  58
+	mv _tmp _out
  59
+}
  60
+
  61
+
  62
+equiv1()
  63
+{
  64
+	echo -n "$1: " 1>&2
  65
+	one=$2
  66
+	shift 2
  67
+	$VALGRIND ${PTST:-../ptest/ptest} "$@" <"$one" >_out 2>&1 || {
  68
+		echo FAILED "($SCRIPT)" 1>&2
  69
+		cat _out
  70
+		rm -f _out
  71
+		exit 1
  72
+	}
  73
+}
  74
+
  75
+
  76
+equiv2()
  77
+{
  78
+	two=$1
  79
+	shift
  80
+	$VALGRIND ${PTST:-../ptest/ptest} "$@" <"$two" >_two 2>&1 || {
  81
+		echo FAILED "($SCRIPT)" 1>&2
  82
+		cat _two
  83
+		rm -f _out _two
  84
+		exit 1
  85
+	}
  86
+	diff -u _out _two >_diff || {
  87
+		echo FAILED "($SCRIPT)" 1>&2
  88
+		cat _diff 1>&2
  89
+		rm -f _out _two _diff
  90
+		exit 1
  91
+	}
  92
+	echo PASSED 1>&2
  93
+	rm -f _out _two _diff
  94
+	passed=`expr ${passed:-0} + 1`
52 95
 }
83  src/compiler/test/Data/flame.fnp
... ...
@@ -0,0 +1,83 @@
  1
+/*
  2
+ * Krash - Digital Flame.fnp
  3
+ *
  4
+ * Formatted for improved syntax.
  5
+ */
  6
+ 
  7
+fDecay = 0.9
  8
+fVideoEchoZoom = 1
  9
+fVideoEchoAlpha = 0
  10
+nVideoEchoOrientation = 0
  11
+nWaveMode = 6
  12
+bAdditiveWaves = 0
  13
+bWaveDots = 0
  14
+bMaximizeWaveColor = 0
  15
+bTexWrap = 0
  16
+fWaveAlpha = 1
  17
+fWaveScale = 0.3697
  18
+fWarpAnimSpeed = 53.523884
  19
+fWarpScale = 0.408391
  20
+zoom = 1
  21
+rot = 0
  22
+cx = 0.5
  23
+cy = 0.5
  24
+dx = 0
  25
+dy = 0
  26
+warp = 1
  27
+sx = 1
  28
+sy = 1
  29
+wave_r = 0.6999
  30
+wave_g = 0.6
  31
+wave_b = 0.8
  32
+wave_x = 0
  33
+wave_y = 0.5
  34
+ob_size = 0
  35
+ob_r = 0
  36
+ob_g = 0
  37
+ob_b = 0
  38
+ob_a = 0
  39
+ib_size = 0
  40
+ib_r = 0
  41
+ib_g = 0
  42
+ib_b = 0
  43
+ib_a = 0
  44
+nMotionVectorsX = 12
  45
+nMotionVectorsY = 9
  46
+mv_l = 0.9
  47
+mv_r = 1
  48
+mv_g = 1
  49
+mv_b = 1
  50
+mv_a = 0
  51
+
  52
+per_frame:
  53
+	q1 = (bass_att + mid_att + treb_att)/3;
  54
+	q2 = time + 1000;
  55
+
  56
+	bass_thresh = (bass_att > bass_thresh)*2 +
  57
+	    (1-(bass_att > bass_thresh))*((bass_thresh-1.4)*0.95+1.4);
  58
+	treb_thresh = (treb_att > treb_thresh)*2 +
  59
+	    (1-(treb_att > treb_thresh))*((treb_thresh-1.5)*0.85+1.2);
  60
+
  61
+	bass_on = bass_thresh > 1.9;
  62
+	treb_on = treb_thresh > 1.9;
  63
+	swap = bass_on - treb_on;
  64
+
  65
+	//	            bass_on |            same | treb_on
  66
+	red_aim =   swap == 1 ? 1   : swap == 0 ? 0.9 : 0.7;
  67
+	green_aim = swap == 1 ? 0.7 : swap == 0 ? 0.3 : 0.6;
  68
+	blue_aim =  swap == 1 ? 0   : swap == 0 ? 0.2 : 0.8;
  69
+	//                    orange              red   white
  70
+
  71
+	red = red + (red_aim - red)*0.5;
  72
+	green = green + (green_aim - green)*0.5;
  73
+	blue = blue + (blue_aim - blue)*0.5;
  74
+
  75
+	wave_r = red;
  76
+	wave_g = green;
  77
+	wave_b = blue;
  78
+
  79
+per_vertex:
  80
+	dy = 0.1*(1-q1);
  81
+	dy = (dy < 0.02)*dy - 0.02;
  82
+	dy = dy + 0.01*(sin((x*q2*0.483) + (y*q2*1.238)) +
  83
+	    sin((x*q2*1.612) + (y*q2*0.648)));
9  src/compiler/test/fold
@@ -143,4 +143,13 @@ expect <<EOF
143 143
 x = 9
144 144
 EOF
145 145
 
  146
+#------------------------------------------------------------------------------
  147
+
  148
+ptest "constant folding: sqrt" << EOF
  149
+x = sqrt(2.25)
  150
+EOF
  151
+expect <<EOF
  152
+x = 1.5
  153
+EOF
  154
+
146 155
 ###############################################################################
13  src/compiler/test/rewrite
... ...
@@ -0,0 +1,13 @@
  1
+#!/bin/sh
  2
+. ./Common
  3
+
  4
+###############################################################################
  5
+
  6
+PATCHDIR=../../../patches
  7
+
  8
+equiv1 "rewrite: prettier Krash - Digital Flame" \
  9
+    "$PATCHDIR/Krash - Digital Flame.fnp"
  10
+sedit s/swapcolour/swap/g
  11
+equiv2 Data/flame.fnp
  12
+
  13
+###############################################################################

No commit comments for this range

Something went wrong with that request. Please try again.