Compare View
Commits (16)
-
sample output new in git
Showing
11 changed files
Show diff stats
fz/.gitignore
fz/Makefile
| ... | ... | @@ -6,8 +6,12 @@ OTHER = flatzinc.output |
| 6 | 6 | PLFILES = $(filter-out out-%.pl,$(wildcard *.pl)) |
| 7 | 7 | PLOFILES = $(patsubst %.pl,%.o,$(PLFILES)) |
| 8 | 8 | |
| 9 | -# out-XXX.pl produces fz-XXX, the flatzinc-to-XXX compiler | |
| 10 | -FZSEM = $(patsubst out-%.pl,fz-%,$(wildcard out-*.pl)) | |
| 9 | +# XXX/output.pl produces fz-XXX, the flatzinc-to-XXX compiler | |
| 10 | +FZBACKS = $(patsubst %/output.pl,%,$(wildcard */output.pl)) | |
| 11 | +FZSEM = $(patsubst %/output.pl,fz-%,$(wildcard */output.pl)) | |
| 12 | +FZPLFILES = $(foreach B,$(FZBACKS),$(wildcard $B/*.pl)) | |
| 13 | +FZOFILES = $(patsubst %.pl,%.o,$(FZPLFILES)) | |
| 14 | +FZAFILES = $(patsubst %,lib%.a,$(FZBACKS)) | |
| 11 | 15 | |
| 12 | 16 | # NOTE: we need to pass the `-l' flag to ensure the scanner internal |
| 13 | 17 | # variable `yylineno' is available. |
| ... | ... | @@ -29,28 +33,34 @@ RM = /bin/rm -f |
| 29 | 33 | %.ast:: %.fzn |
| 30 | 34 | fzp < $< > $@ |
| 31 | 35 | |
| 32 | -all: fzp $(FZSEM) $(PLOFILES) $(patsubst %.pl,%.o,$(wildcard out-*.pl)) | |
| 36 | +all: fzp $(FZSEM) $(PLOFILES) $(FZOFILES) $(FZAFILES) | |
| 33 | 37 | |
| 34 | 38 | fzp: $(OBJ) |
| 35 | 39 | $(CC) $(CFLAGS) -o $@ $(OBJ) $(LEXLIB) |
| 36 | 40 | |
| 37 | -fz-%: out-%.o $(PLOFILES) | |
| 41 | +fz-%: $(PLOFILES) lib%.a | |
| 38 | 42 | $(GPLC) -o $@ $+ |
| 39 | 43 | |
| 44 | +lib%.a: $(FZOFILES) | |
| 45 | + ar rv $@ $+ | |
| 46 | + | |
| 40 | 47 | %.o: %.pl |
| 41 | 48 | $(GPLC) $(GPLCFLAGS) -c $< |
| 42 | 49 | |
| 43 | 50 | flatzinc.tab.c: flatzinc.y lex.yy.c |
| 44 | - $(YACC) $(YACCFLAGS) flatzinc.y | |
| 51 | + $(YACC) $(YACCFLAGS) flatzinc.y | |
| 45 | 52 | |
| 46 | 53 | lex.yy.c: flatzinc.l |
| 47 | - $(LEX) $(LEXFLAGS) flatzinc.l | |
| 54 | + $(LEX) $(LEXFLAGS) flatzinc.l | |
| 48 | 55 | |
| 49 | 56 | clean: |
| 50 | - $(RM) $(OBJ) $(GENCFILES) $(OTHER) core | |
| 57 | + $(RM) \ | |
| 58 | + $(OBJ) $(PLOFILES) \ | |
| 59 | + $(FZOFILES) $(FZAFILES) \ | |
| 60 | + $(GENCFILES) $(OTHER) core | |
| 51 | 61 | |
| 52 | 62 | clobber: clean |
| 53 | - $(RM) $(EXE_FILE) | |
| 63 | + $(RM) $(EXE_FILE) | |
| 54 | 64 | |
| 55 | 65 | .PHONY: distclean |
| 56 | 66 | distclean: clobber | ... | ... |
fz/README
| 1 | +Sample usage: | |
| 2 | + | |
| 3 | + ./fzp < examples/aust.fzn | ./fz-paccs load name type code halt | |
| 4 | + | |
| 5 | +Notes: | |
| 6 | + | |
| 1 | 7 | - AST nodes for fzn parser |
| 2 | 8 | |
| 3 | 9 | TYPE : VALUE |
| 4 | 10 | |
| 5 | 11 | - acceptable types: |
| 6 | 12 | |
| 7 | -- driver process fznslurp: | |
| 13 | +- driver process fz-BACK (BACK is back-end specific): | |
| 8 | 14 | |
| 9 | 15 | reads stdin (as produced by bison-generated parser), which results |
| 10 | 16 | in a Prolog term which is the AST. It then does semantic analysis | ... | ... |
| ... | ... | @@ -0,0 +1,25 @@ |
| 1 | +#include "paccs.h" | |
| 2 | + | |
| 3 | +main() | |
| 4 | +{ | |
| 5 | + int i, j; | |
| 6 | + fd_int t = fd_new(1, 3); | |
| 7 | + fd_int v = fd_new(1, 3); | |
| 8 | + fd_int nsw = fd_new(1, 3); | |
| 9 | + fd_int q = fd_new(1, 3); | |
| 10 | + fd_int sa = fd_new(1, 3); | |
| 11 | + fd_int nt = fd_new(1, 3); | |
| 12 | + fd_int wa = fd_new(1, 3); | |
| 13 | + int X_INTRODUCED_0[2] = { 1, -1 }; | |
| 14 | + | |
| 15 | + if (fd_solve()) { | |
| 16 | + fd_println(t); | |
| 17 | + fd_println(v); | |
| 18 | + fd_println(nsw); | |
| 19 | + fd_println(q); | |
| 20 | + fd_println(sa); | |
| 21 | + fd_println(nt); | |
| 22 | + fd_println(wa); | |
| 23 | + } | |
| 24 | +} | |
| 25 | + | ... | ... |
| ... | ... | @@ -0,0 +1,127 @@ |
| 1 | +% == No, Emacs this is -*-Prolog-*- code, not what you thought... ============= | |
| 2 | + | |
| 3 | +% == Code generation for PaCCS ================================================ | |
| 4 | + | |
| 5 | +cg_emit(fzn(preds(PS), vars(VS), constrs(CS), G), ST) :- | |
| 6 | + cg_emit(PS, VS, CS, G, ST, TEXT, []), | |
| 7 | + format("~s\n", [TEXT]). | |
| 8 | + | |
| 9 | +cg_emit(PS, VS, CS, G, ST) --> | |
| 10 | + cg_prefix, | |
| 11 | + cg_preds(PS, ST), | |
| 12 | + cg_vars(decl, VS, ST), | |
| 13 | + cg_vars(init, VS, ST), | |
| 14 | + cg_constrs(CS, ST), | |
| 15 | + cg_goal(G, VS, ST), | |
| 16 | + cg_suffix. | |
| 17 | + | |
| 18 | +% == DCGs for code generation ================================================= | |
| 19 | + | |
| 20 | + | |
| 21 | +% -- predicates --------------------------------------------------------------- | |
| 22 | + | |
| 23 | +cg_preds(_PS, _ST) --> []. | |
| 24 | + | |
| 25 | + | |
| 26 | +% -- variables ---------------------------------------------------------------- | |
| 27 | + | |
| 28 | +cg_vars(OP, [V|Vs], ST) --> cg_var(OP, V, ST), cg_vars(OP, Vs, ST). | |
| 29 | +cg_vars(_OP, [], _) --> []. | |
| 30 | + | |
| 31 | +cg_var(OP, var(N,int,I,A), ST) --> | |
| 32 | + { ! }, | |
| 33 | + cg_var(OP, var(N,int(1,999),I,A), ST). % FIXME | |
| 34 | + | |
| 35 | + | |
| 36 | +cg_var(decl, var(N,int(LB,UB),_I,_A), _ST) --> | |
| 37 | + { format_to_codes(S, " fd_int ~w = fd_new(~d, ~d);\n", [N, LB, UB]) }, | |
| 38 | + S. | |
| 39 | + | |
| 40 | +cg_var(init, var(_,int(_,_),_,_), _ST) --> []. | |
| 41 | + | |
| 42 | +cg_var(decl, val(N,int,lit(INIT,_),_), _) --> | |
| 43 | + { format_to_codes(S, " const int ~w = ~w;\n", [N, INIT]) }, | |
| 44 | + S. | |
| 45 | + | |
| 46 | +cg_var(init, val(_,int,_,_), _ST) --> []. | |
| 47 | + | |
| 48 | +% -- array of variables - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
| 49 | + | |
| 50 | +cg_var(decl, var(N,array(_T,LB,UB),[],_), _ST) --> !, | |
| 51 | + { SZ is UB-LB+1, | |
| 52 | + format_to_codes(S, " fd_int ~w[~d];\n", [N, SZ]) }, | |
| 53 | + S. | |
| 54 | + | |
| 55 | +cg_var(init, var(N,array(T,ALB,AUB),[],_), _ST) --> !, | |
| 56 | + { SZ is AUB-ALB+1, bound(lb, T, LB), bound(ub, T, UB) }, | |
| 57 | + { format_to_codes(S1, " for (i=0; i<~d; ++i)\n", [SZ]) }, S1, | |
| 58 | + { format_to_codes(S2, " ~w[i] = fd_new(~d, ~d);\n", [N, LB, UB]) }, S2. | |
| 59 | + | |
| 60 | +% -- array of constants - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
| 61 | + | |
| 62 | +cg_var(decl, val(N,array(_T,LB,UB),lit(INIT,array(_)),_), _ST) --> | |
| 63 | + { nonvar(INIT), INIT=[_|_] }, !, | |
| 64 | + { SZ is UB-LB+1, | |
| 65 | + format_to_codes(S, " const int ~w[~d] = {", [N, SZ]) }, | |
| 66 | + S, | |
| 67 | + cg_constant_list(INIT, " "), | |
| 68 | + " };\n". | |
| 69 | + | |
| 70 | +cg_var(init, val(_N,array(_T,_LB,_UB),lit(INIT,array(_)),_), _ST) --> | |
| 71 | + { nonvar(INIT), INIT=[_|_] }, !, | |
| 72 | + []. | |
| 73 | + | |
| 74 | + | |
| 75 | +% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
| 76 | + | |
| 77 | +cg_constant_list([], _) --> []. | |
| 78 | +cg_constant_list([C|Cs], PFX) --> | |
| 79 | + PFX, cg_constant(C), | |
| 80 | + cg_constant_list(Cs, ", "). | |
| 81 | + | |
| 82 | +cg_constant(lit(N,int)) --> { format_to_codes(S, "~w", [N]) }, S. | |
| 83 | + | |
| 84 | + | |
| 85 | +% -- constraints -------------------------------------------------------------- | |
| 86 | + | |
| 87 | +cg_constrs([], _) --> {!}, []. | |
| 88 | +cg_constrs(CS, ST) --> cg_coverage(CS, CR, ST), cg_constrs(CR, ST). | |
| 89 | + | |
| 90 | +% ++ cg_coverage(CI, CO, ST) [DCG] - - - - - - - - - - - - - - - - - - - - - - | |
| 91 | +% | |
| 92 | +% emit an instruction which partly covers CI, leaving CO untreated | |
| 93 | +% FIXME: for now this just puts out comments | |
| 94 | + | |
| 95 | +cg_coverage([C|CS], CS, _ST) --> | |
| 96 | + { C=constraint(CE, _A), | |
| 97 | + format_to_codes(S, "// ~w\n", [CE]) }, S. | |
| 98 | + | |
| 99 | + | |
| 100 | +% -- goal --------------------------------------------------------------------- | |
| 101 | + | |
| 102 | +cg_goal(solve(satisfy,_), VS, ST) --> % FIXME: add minimize and maximize | |
| 103 | + "\n", | |
| 104 | + " if (fd_solve()) {\n", | |
| 105 | + cg_var_print(VS, ST, " "), | |
| 106 | + " }\n", | |
| 107 | + " fd_end();\n". | |
| 108 | + | |
| 109 | +cg_var_print([], _, _) --> []. | |
| 110 | +cg_var_print([var(N,_,_,A)|Vs], ST, PFX) --> | |
| 111 | + { member(output, A), ! }, | |
| 112 | + { format_to_codes(S, "~sfd_println(~w);\n", [PFX, N]) }, S, | |
| 113 | + cg_var_print(Vs, ST, PFX). | |
| 114 | +cg_var_print([_|Vs], ST, PFX) --> cg_var_print(Vs, ST, PFX). | |
| 115 | + | |
| 116 | + | |
| 117 | +% -- prefix and suffix -------------------------------------------------------- | |
| 118 | + | |
| 119 | +cg_prefix --> | |
| 120 | + "#include \"paccs.h\"\n\n", | |
| 121 | + "main()\n", | |
| 122 | + "{\n", | |
| 123 | + " int i, j;\n". | |
| 124 | + | |
| 125 | +cg_suffix --> | |
| 126 | + "}\n". | |
| 127 | + | ... | ... |
fz/semantic.pl
| ... | ... | @@ -35,17 +35,25 @@ sa_n_traverse(N, NN, ST) :- sa_n(N, NN, ST). % non-list case |
| 35 | 35 | % |
| 36 | 36 | % handle all AST cases: |
| 37 | 37 | % var(N,T,I,A) - Name, Type, Initializer, Attrib -- variable declaration |
| 38 | +% val(N,T,I,A) - Name, Type, Initializer, Attrib -- (constant) value | |
| 38 | 39 | % lit(V,T) - Value, Type -- literal with type |
| 39 | 40 | % id(N) - Name -- identifier |
| 40 | 41 | % constraint(C,A) - Constraint, Attrib -- constraint |
| 41 | 42 | % |
| 43 | +% variables with an initializer are treated as SSA & will now stand for | |
| 44 | +% the literal in the initializer itself. | |
| 42 | 45 | |
| 43 | -sa_n(var(N,T,I,A), V, ST) :- | |
| 46 | +sa_n(var(N,T,[],A), V, ST) :- !, | |
| 44 | 47 | sa_attribs(A, AX, ST), |
| 45 | - sa_n_traverse(I, NI, ST), % parse initializer | |
| 46 | - V=var(N,T,NI,AX), % new AST node becomes ST entry value | |
| 48 | + V=var(N,T,[],AX), % new AST node becomes ST entry value | |
| 47 | 49 | st_insert(ST, N, V). |
| 48 | 50 | |
| 51 | +sa_n(var(N,T,I,A), V, ST) :- % non-empty initializer | |
| 52 | + sa_n_traverse(I, _NI, ST), % parse initializer (& ignore T and A), | |
| 53 | + sa_attribs(A, AX, ST), | |
| 54 | + V=val(N,T,I,AX), % -- not var(N,T,NI,AX) -- | |
| 55 | + st_insert(ST, N, V). % and it becomes the ST entry value | |
| 56 | + | |
| 49 | 57 | |
| 50 | 58 | sa_n(lit(E,array(T)), lit(NE, array(T)), ST) :- |
| 51 | 59 | sa_n_traverse(E, NE, ST). |
| ... | ... | @@ -59,8 +67,9 @@ sa_n(id(N), V, ST) :- V=var(N,_,_,_), % type as yet unknown |
| 59 | 67 | st_insert(ST, N, V). |
| 60 | 68 | |
| 61 | 69 | |
| 62 | -sa_n(constraint(CE, AT), constraint(NCE, AT), ST) :- | |
| 70 | +sa_n(constraint(CE, A), constraint(NCE, AX), ST) :- | |
| 63 | 71 | CE=..[C|AS], |
| 72 | + sa_attribs(A, AX, ST), | |
| 64 | 73 | sa_n_traverse(AS, NAS, ST), |
| 65 | 74 | NCE=..[C|NAS]. |
| 66 | 75 | |
| ... | ... | @@ -91,6 +100,7 @@ sa_t_traverse(N, ST) :- sa_t(N, ST). % non-list case |
| 91 | 100 | % -- sa_t(NODE, ST) ----------------------------------------------------------- |
| 92 | 101 | |
| 93 | 102 | sa_t(var(_N,T,I,_A), _ST) :- type(I,T). |
| 103 | +sa_t(val(_N,T,I,_A), _ST) :- type(I,T). | |
| 94 | 104 | sa_t(lit(E,T), _ST) :- type(lit(E,T), T). |
| 95 | 105 | sa_t(constraint(CE, _AT), ST) :- |
| 96 | 106 | CE=..[_|AS], | ... | ... |
| ... | ... | @@ -0,0 +1,12 @@ |
| 1 | +% == No, Emacs this is -*-Prolog-*- code, not what you thought... ============= | |
| 2 | + | |
| 3 | +% -- utility predicates ------------------------------------------------------- | |
| 4 | + | |
| 5 | +% -- subset (SUB, SET, REST) -------------------------------------------------- | |
| 6 | + | |
| 7 | +subset(L, L, []). | |
| 8 | +subset(S, [H|T], R) :- subset_aux(T, H, S, R). | |
| 9 | + | |
| 10 | + subset_aux(S, R, S, [R]). | |
| 11 | + subset_aux([H|T], R1, S, [R1|R]) :- subset_aux(T, H, S, R). | |
| 12 | + subset_aux([H|T], X, [X|S], R) :- subset_aux(T, H, S, R). | ... | ... |