Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: m-labs/flickernoise
base: a4b3833
...
head fork: m-labs/flickernoise
compare: a5468c2
  • 5 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jan 28, 2012
@wpwrak wpwrak ptest: -c -c compiles into FPVM code and dumps the result
Also changed compiler.c to export init_fpvm, for sharing with ptest.
ed83089
@wpwrak wpwrak tests: new command "pvm" to generate and pretty-print FPVM code
The prettification is done by ptest/prvm.pl
1a05965
@wpwrak wpwrak compiler: completed boolean "and" and "or"
This commit:

- fixes a bug in the parser that made it not recognize && and ||
  (it did accept band and bor),
- adds op_band and op_bor to ptest.c, and
- adds test cases "and" and "or"
6e741aa
@wpwrak wpwrak test/Common: don't require argument of equiv1 and equiv2 to be a file…
… name

This changes the use from  equiv1 FILE  to  equiv1 <FILE
With the change, we can also pass simpler expressions directly
on the command line, with  equiv1 "EXPRESSION"

Also updated test/rewrite
933ac8f
@wpwrak wpwrak test/rewrite: added two examples from Unchained - A Matter Of Taste (…
…Remix)
a5468c2
View
2  src/compiler/compiler.c
@@ -60,7 +60,7 @@ static void comp_report(struct compiler_sc *sc, const char *format, ...)
sc->rmc(outbuf);
}
-static void init_fpvm(struct fpvm_fragment *fragment, int vector_mode)
+void init_fpvm(struct fpvm_fragment *fragment, int vector_mode)
{
/*
* We need to pass these through unique() because the parser does
View
1  src/compiler/compiler.h
@@ -247,6 +247,7 @@ struct patch {
typedef void (*report_message)(const char *);
+void init_fpvm(struct fpvm_fragment *fragment, int vector_mode);
struct patch *patch_compile(const char *basedir, const char *patch_code, report_message rmc);
struct patch *patch_compile_filename(const char *filename, const char *patch_code, report_message rmc);
struct patch *patch_copy(struct patch *p);
View
2  src/compiler/parser.y
@@ -351,7 +351,7 @@ expr(N) ::= cond_expr(A). {
N = A;
}
-cond_expr(N) ::= equal_expr(A). {
+cond_expr(N) ::= bool_or_expr(A). {
N = A;
}
View
29 src/compiler/ptest/prvm.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+#
+# prvm.pl - FPVM code dump prettifier
+#
+# Copyright 2012 by Werner Almesberger
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+
+$r{"R0002"} = "R2";
+while (<>) {
+ next if /^==/;
+ if (/^(R\S+)\s(.*)/) {
+ $r = $1;
+ $v = $2;
+ $v = sprintf("%g", $v) if $v =~ /^[-0-9]/;
+ $r{$r} = $v if $r ne "R0002";
+ next;
+ }
+ for $r (keys %r) {
+ s/\b$r\b/$r{$r}/g;
+ }
+ s/<R2>//;
+ s/ +/ /g;
+ s/^\d{4}: //;
+ print;
+}
View
100 src/compiler/ptest/ptest.c
@@ -138,6 +138,12 @@ static void dump_ast(const struct ast_node *ast)
case op_bnot:
op("!", ast);
break;
+ case op_band:
+ op("&&", ast);
+ break;
+ case op_bor:
+ op("||", ast);
+ break;
default:
abort();
}
@@ -344,33 +350,62 @@ static void compile(const char *pgm)
}
-#if 0
-static void compile_raw(const char *pgm)
+static const char *assign_raw(struct parser_comm *comm,
+ struct sym *sym, struct ast_node *node)
{
- struct patch patch;
- struct compiler_sc sc = {
- .p = &patch
- };
- struct patch *p;
+ struct fpvm_fragment *frag = comm->u.fragment;
- memset(&patch, 0, sizeof(patch));
- patch.rmc = report;
+ if(fpvm_do_assign(frag, &sym->fpvm_sym, node))
+ return NULL;
+ else
+ return strdup(fpvm_get_last_error(frag));
+}
+
+
+static const char *assign_raw_fail(struct parser_comm *comm,
+ struct sym *sym, struct ast_node *node)
+{
+ return strdup("unsupported assignment mode");
+}
- if (!parse_patch(&sc, pgm)) {
- symtab_free();
- exit(1);
- }
- patch.perframe_prog_length = fpvm_default_schedule(&sc.pfv_fragment,
- (unsigned *) patch.perframe_prog,
- (unsigned *) patch.perframe_regs);
- patch.pervertex_prog_length = fpvm_default_schedule(&sc.pvv_fragment,
- (unsigned *) patch.pervertex_prog,
- (unsigned *) patch.pervertex_regs);
+static const char *assign_image_fail(struct parser_comm *comm,
+ int number, const char *name)
+{
if (!quiet)
- show_patch(p);
+ printf("image %d = \"%s\"\n", number, name);
+ return NULL;
+}
+
+
+static void compile_vm(const char *pgm)
+{
+ struct fpvm_fragment fragment;
+ struct parser_comm comm = {
+ .u.fragment = &fragment,
+ .assign_default = assign_raw,
+ .assign_per_frame = assign_raw,
+ .assign_per_vertex = assign_raw_fail,
+ .assign_image_name = assign_image_fail,
+ };
+ int ok;
+
+ init_fpvm(&fragment, 0);
+ fpvm_set_bind_mode(&fragment, FPVM_BIND_ALL);
+ symtab_init();
+ ok = parse(pgm, TOK_START_ASSIGN, &comm);
+
+ if (ok)
+ fpvm_dump(&fragment);
+ symtab_free();
+ if (ok)
+ return;
+
+ fflush(stdout);
+ fprintf(stderr, "%s\n", comm.msg);
+ free((void *) comm.msg);
+ exit(1);
}
-#endif
static void free_buffer(void)
@@ -382,8 +417,9 @@ static void free_buffer(void)
static void usage(const char *name)
{
fprintf(stderr,
-"usage: %s [-c|-f error] [-n runs] [-q] [-s] [-Wwarning...] [expr]\n\n"
+"usage: %s [-c [-c]|-f error] [-n runs] [-q] [-s] [-Wwarning...] [expr]\n\n"
" -c generate code and dump generated code (unless -q is set)\n"
+" -c -c generate and dump VM code\n"
" -f error fail any assignment with specified error message\n"
" -n runs run compilation repeatedly (default: run only once)\n"
" -q quiet operation\n"
@@ -407,7 +443,7 @@ int main(int argc, char **argv)
while ((c = getopt(argc, argv, "cf:n:qsW:")) != EOF)
switch (c) {
case 'c':
- codegen = 1;
+ codegen++;
break;
case 'f':
fail = optarg;
@@ -450,12 +486,20 @@ int main(int argc, char **argv)
usage(*argv);
}
- while (repeat--) {
- if (codegen)
- compile(buffer);
- else
+ while (repeat--)
+ switch (codegen) {
+ case 0:
parse_only(buffer);
- }
+ break;
+ case 1:
+ compile(buffer);
+ break;
+ case 2:
+ compile_vm(buffer);
+ break;
+ default:
+ usage(*argv);
+ }
return 0;
}
View
19 src/compiler/test/Common
@@ -67,9 +67,8 @@ sedit()
equiv1()
{
echo -n "$1: " 1>&2
- one=$2
- shift 2
- $VALGRIND ${PTST:-../ptest/ptest} "$@" <"$one" >_out 2>&1 || {
+ shift
+ $VALGRIND ${PTST:-../ptest/ptest} "$@" >_out 2>&1 || {
echo FAILED "($SCRIPT)" 1>&2
cat _out
rm -f _out
@@ -80,9 +79,7 @@ equiv1()
equiv2()
{
- two=$1
- shift
- $VALGRIND ${PTST:-../ptest/ptest} "$@" <"$two" >_two 2>&1 || {
+ $VALGRIND ${PTST:-../ptest/ptest} "$@" >_two 2>&1 || {
echo FAILED "($SCRIPT)" 1>&2
cat _two
rm -f _out _two
@@ -98,3 +95,13 @@ equiv2()
rm -f _out _two _diff
passed=`expr ${passed:-0} + 1`
}
+
+
+fpvm()
+{
+ one=$1
+ shift
+ ptest "$one" -c -c "$@"
+ ${PRVM:-../ptest/prvm.pl} _out >_tmp || exit 1
+ mv _tmp _out
+}
View
79 src/compiler/test/and
@@ -0,0 +1,79 @@
+#!/bin/sh
+. ./Common
+
+###############################################################################
+
+ptest "and: band(a, b)" << EOF
+sx = band(a, b)
+EOF
+expect <<EOF
+sx = (&& a b)
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "and: a && b" << EOF
+sx = a && b
+EOF
+expect <<EOF
+sx = (&& a b)
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "and: 0 && 0" << EOF
+sx = 0 && 0
+EOF
+expect <<EOF
+sx = 0
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "and: 1 && 0" << EOF
+sx = 1 && 0
+EOF
+expect <<EOF
+sx = 0
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "and: 0 && 1" << EOF
+sx = 0 && 1
+EOF
+expect <<EOF
+sx = 0
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "and: 1 && 1" << EOF
+sx = 1 && 1
+EOF
+expect <<EOF
+sx = 1
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "and: a && b == c" << EOF
+sx = a && b == c
+EOF
+expect <<EOF
+sx = (&& a (equal b c))
+EOF
+
+#------------------------------------------------------------------------------
+
+fpvm "and: a && b (generate code)" <<EOF
+sx = a && b
+EOF
+expect <<EOF
+COPY a -> R2
+IF 1,0 -> R-003
+COPY b -> R2
+IF R-003,0 -> sx
+EOF
+
+###############################################################################
View
79 src/compiler/test/or
@@ -0,0 +1,79 @@
+#!/bin/sh
+. ./Common
+
+###############################################################################
+
+ptest "or: bor(a, b)" << EOF
+sx = bor(a, b)
+EOF
+expect <<EOF
+sx = (|| a b)
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "or: a || b" << EOF
+sx = a || b
+EOF
+expect <<EOF
+sx = (|| a b)
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "or: 0 || 0" << EOF
+sx = 0 || 0
+EOF
+expect <<EOF
+sx = 0
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "or: 1 || 0" << EOF
+sx = 1 || 0
+EOF
+expect <<EOF
+sx = 1
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "or: 0 || 1" << EOF
+sx = 0 || 1
+EOF
+expect <<EOF
+sx = 1
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "or: 1 || 1" << EOF
+sx = 1 || 1
+EOF
+expect <<EOF
+sx = 1
+EOF
+
+#------------------------------------------------------------------------------
+
+ptest "or: a || b && c" << EOF
+sx = a || b && c
+EOF
+expect <<EOF
+sx = (|| a (&& b c))
+EOF
+
+#------------------------------------------------------------------------------
+
+fpvm "or: a || b (generate code)" <<EOF
+sx = a || b
+EOF
+expect <<EOF
+COPY a -> R2
+IF 1,0 -> R-003
+COPY b -> R2
+IF 1,R-003 -> sx
+EOF
+
+###############################################################################
View
45 src/compiler/test/rewrite
@@ -6,8 +6,49 @@
PATCHDIR=../../../patches
equiv1 "rewrite: prettier Krash - Digital Flame" \
- "$PATCHDIR/Krash - Digital Flame.fnp"
+ <"$PATCHDIR/Krash - Digital Flame.fnp"
sedit s/swapcolour/swap/g
-equiv2 Data/flame.fnp
+equiv2 <Data/flame.fnp
+
+#------------------------------------------------------------------------------
+
+#
+# The original used rand(3) instead of sin(3), but we don't have that (yet ?)
+#
+
+equiv1 "rewrite: per_frame_2 from Unchained - A Matter Of Taste (Remix)" <<EOF
+per_frame_2=entropy=if(bnot(entropy),2,if(equal(pulse,-9.42),1+sin(3),entropy));
+EOF
+equiv2 <<EOF
+per_frame:
+ entropy = !entropy ? 2 :
+ pulse == -9.42 ? 1+sin(3) : entropy;
+EOF
+
+#------------------------------------------------------------------------------
+
+equiv1 "rewrite: per_frame_12 from Unchained - A Matter Of Taste (Remix)" <<EOF
+per_frame_12=pulse=if(above(abs(pulse),9.42),-9.42,pulse+.1*bor(bor(bass_changed*bnot(treb_changed),treb_changed*bnot(bass_changed))*bnot(mid_changed),mid_changed)+(mid+bass+treb)*entropy*.01);
+EOF
+equiv2 <<EOF
+per_frame:
+ pulse = abs(pulse) > 9.42 ? -9.42 :
+ pulse+
+ 0.1*(
+ (bass_changed*!treb_changed || treb_changed*!bass_changed)*
+ !mid_changed ||
+ mid_changed)+
+ (mid+bass+treb)*entropy*0.01;
+EOF
+
+#
+# This could be written even more cleanly as
+#
+# pulse = abs(pulse) > 9.42 ? -9.42 :
+# pulse+
+# 0.1*(mid_changed ? 1 :
+# bass_changed*!treb_changed || treb_changed*!bass_changed)+
+# 0.01*(mid+bass+treb)*entropy;
+#
###############################################################################

No commit comments for this range

Something went wrong with that request. Please try again.