├── ld ├── __start.o ├── Makefile ├── ld.h ├── trace.h ├── staticize.h ├── trace.c ├── staticize.c └── ld.c ├── cc ├── genbf │ ├── pointer.c │ ├── type_name.c │ ├── enumerator.c │ ├── identifier.c │ ├── initializer.c │ ├── enum_specifier.c │ ├── function_body.c │ ├── type_specifier.c │ ├── unary_operator.c │ ├── enumerator_list.c │ ├── identifier_list.c │ ├── struct_or_union.c │ ├── initializer_list.c │ ├── labeled_statement.c │ ├── struct_declarator.c │ ├── abstract_declarator.c │ ├── assignment_operator.c │ ├── struct_declaration.c │ ├── type_specifier_list.c │ ├── abstract_declarator2.c │ ├── declaration_specifiers.c │ ├── struct_declarator_list.c │ ├── storage_class_specifier.c │ ├── struct_declaration_list.c │ ├── struct_or_union_specifier.c │ ├── file.c │ ├── statement_list.c │ ├── parameter_list.c │ ├── declaration_list.c │ ├── init_declarator_list.c │ ├── external_definition_list.c │ ├── argument_expr_list.c │ ├── expression_statement.c │ ├── parameter_type_list.c │ ├── parameter_identifier_list.c │ ├── expr.c │ ├── declaration.c │ ├── external_definition.c │ ├── compound_statement.c │ ├── and_expr.c │ ├── init_declarator.c │ ├── exclusive_or_expr.c │ ├── logical_or_expr.c │ ├── logical_and_expr.c │ ├── inclusive_or_expr.c │ ├── conditional_expr.c │ ├── cast_expr.c │ ├── declarator.c │ ├── parameter_declaration.c │ ├── statement.c │ ├── shift_expr.c │ ├── Makefile │ ├── additive_expr.c │ ├── builtin_functions.c │ ├── equality_expr.c │ ├── declarator2.c │ ├── unary_expr.c │ ├── multiplicative_expr.c │ ├── relational_expr.c │ ├── selection_statement.c │ ├── jump_statement.c │ ├── iteration_statement.c │ ├── assignment_expr.c │ ├── function_definition.c │ ├── postfix_expr.c │ ├── primary_expr.c │ ├── generator.h │ └── generator.c ├── Makefile ├── tests │ ├── test2.c │ ├── test3.c │ ├── test4.c │ ├── test8.c │ ├── test5.c │ ├── test1.c │ ├── test6.c │ ├── test7.c │ ├── test9.c │ └── test10.c ├── main.c ├── genbfmain.c ├── README.txt ├── scan.l └── genbf.h ├── math ├── Makefile └── bf_math.c ├── 1tox ├── c2bf-1to16bit ├── c2bf-1to32bit ├── Makefile ├── MultiBitwidth.txt ├── BF1to2.txt ├── c2bf-1to2.c ├── BF1to4.txt └── c2bf-1to4.c ├── .gitignore ├── Makefile ├── strip ├── Makefile └── bfstrip.c ├── README ├── wrapper └── c2bf ├── test.sh └── GPL /ld/__start.o: -------------------------------------------------------------------------------- 1 | __start: <<<[>+>+<<-]>[<+>-]>[>>>+<<<-]>>>+>>>(*__start!1)(main) 2 | __start!1: <<<[<<<<<]<<- 3 | -------------------------------------------------------------------------------- /cc/genbf/pointer.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_pointer(struct pointer *a) 4 | { UNIMPL("pointer"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/type_name.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_type_name(struct type_name *a) 4 | { UNIMPL("type_name"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/enumerator.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_enumerator(struct enumerator *a) 4 | { UNIMPL("enumerator"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/identifier.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_identifier(struct identifier *a) 4 | { UNIMPL("identifier"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/initializer.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_initializer(struct initializer *a) 4 | { UNIMPL("initializer"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/enum_specifier.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_enum_specifier(struct enum_specifier *a) 4 | { UNIMPL("enum_specifier"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/function_body.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_function_body(struct function_body *a) 4 | { UNIMPL("function_body"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/type_specifier.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_type_specifier(struct type_specifier *a) 4 | { UNIMPL("type_specifier"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/unary_operator.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_unary_operator(struct unary_operator *a) 4 | { UNIMPL("unary_operator"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/enumerator_list.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_enumerator_list(struct enumerator_list *a) 4 | { UNIMPL("enumerator_list"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/identifier_list.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_identifier_list(struct identifier_list *a) 4 | { UNIMPL("identifier_list"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/struct_or_union.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_struct_or_union(struct struct_or_union *a) 4 | { UNIMPL("struct_or_union"); } 5 | 6 | -------------------------------------------------------------------------------- /math/Makefile: -------------------------------------------------------------------------------- 1 | BF_CC=../cc/c2bf-cc 2 | 3 | all: bf_math.o 4 | 5 | .PREFIXES: .c .o 6 | 7 | .c.o: 8 | $(BF_CC) < $< > $@ 9 | 10 | clean: 11 | rm -f bf_math.o 12 | -------------------------------------------------------------------------------- /cc/genbf/initializer_list.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_initializer_list(struct initializer_list *a) 4 | { UNIMPL("initializer_list"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/labeled_statement.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_labeled_statement(struct labeled_statement *a) 4 | { UNIMPL("labeled_statement"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/struct_declarator.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_struct_declarator(struct struct_declarator *a) 4 | { UNIMPL("struct_declarator"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/abstract_declarator.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_abstract_declarator(struct abstract_declarator *a) 4 | { UNIMPL("abstract_declarator"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/assignment_operator.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_assignment_operator(struct assignment_operator *a) 4 | { UNIMPL("assignment_operator"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/struct_declaration.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_struct_declaration(struct struct_declaration *a) 4 | { UNIMPL("struct_declaration"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/type_specifier_list.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_type_specifier_list(struct type_specifier_list *a) 4 | { UNIMPL("type_specifier_list"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/abstract_declarator2.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_abstract_declarator2(struct abstract_declarator2 *a) 4 | { UNIMPL("abstract_declarator2"); } 5 | 6 | -------------------------------------------------------------------------------- /1tox/c2bf-1to16bit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export PATH="`dirname $0`:$PATH" 3 | 4 | echo -n "++++++++[>++++++++<-]>[<++++>-]+<[>-<" 5 | cat $1 6 | echo -n "[-]>[-]<]>[-<" 7 | cat $1 | c2bf-1to2 8 | echo "[-]]" 9 | -------------------------------------------------------------------------------- /cc/genbf/declaration_specifiers.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_declaration_specifiers(struct declaration_specifiers *a) 4 | { UNIMPL("declaration_specifiers"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/struct_declarator_list.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_struct_declarator_list(struct struct_declarator_list *a) 4 | { UNIMPL("struct_declarator_list"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/storage_class_specifier.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_storage_class_specifier(struct storage_class_specifier *a) 4 | { UNIMPL("storage_class_specifier"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/struct_declaration_list.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_struct_declaration_list(struct struct_declaration_list *a) 4 | { UNIMPL("struct_declaration_list"); } 5 | 6 | -------------------------------------------------------------------------------- /cc/genbf/struct_or_union_specifier.c: -------------------------------------------------------------------------------- 1 | #include "../genbf.h" 2 | #include "generator.h" 3 | void genbf_struct_or_union_specifier(struct struct_or_union_specifier *a) 4 | { UNIMPL("struct_or_union_specifier"); } 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 1tox/c2bf-1to2 2 | strip/c2bf-strip 3 | test 4 | x1tox/c2bf-1to2 5 | 1tox/c2bf-1to4 6 | cc/c2bf-cc 7 | cc/c2bf-dispast 8 | cc/genbf/libgenbf.a 9 | cc/y.output 10 | cc/y.tab.h 11 | ld/c2bf-ld 12 | *.o 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | cd 1tox ; $(MAKE) 3 | cd cc ; $(MAKE) 4 | cd ld ; $(MAKE) 5 | cd math ; $(MAKE) 6 | cd strip ; $(MAKE) 7 | 8 | clean: 9 | cd 1tox ; $(MAKE) clean 10 | cd cc ; $(MAKE) clean 11 | cd ld ; $(MAKE) clean 12 | cd math ; $(MAKE) clean 13 | cd strip ; $(MAKE) clean 14 | -------------------------------------------------------------------------------- /1tox/c2bf-1to32bit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export PATH="`dirname $0`:$PATH" 3 | 4 | echo -n "++++++++[>++++++++<-]>[<++++>-]+<[>-<[>>+>+<<<-]>>>[<[<+<+>>-]<[>+<-]>>-]<[-]<+<[>-<[-]" 5 | cat $1 6 | echo -n "[-]>[-]>[-]<<]>[-<" 7 | cat $1 | c2bf-1to2 8 | echo -n "[-]>[-]<]]>[-<" 9 | cat $1 | c2bf-1to4 10 | echo "[-]]" 11 | -------------------------------------------------------------------------------- /strip/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 -g 3 | LDFLAGS= 4 | 5 | OBJS=bfstrip.o 6 | 7 | all: c2bf-strip 8 | 9 | c2bf-strip: $(OBJS) 10 | $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@ 11 | 12 | .PREFIXES: .y .l .c .o 13 | 14 | .c.o: 15 | $(CC) $(CFLAGS) -c $< -o $@ 16 | 17 | clean: 18 | rm -f $(OBJS) c2bf-strip 19 | -------------------------------------------------------------------------------- /ld/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 -g 3 | LDFLAGS= 4 | 5 | LEX=flex 6 | 7 | YACC=yacc 8 | 9 | LD_OBJS=ld.o staticize.o trace.o 10 | 11 | all: c2bf-ld 12 | 13 | c2bf-ld: $(LD_OBJS) 14 | $(CC) $(CFLAGS) $(LDFLAGS) $(LD_OBJS) -o $@ 15 | 16 | .PREFIXES: .c .o 17 | 18 | .c.o: 19 | $(CC) $(CFLAGS) -c $< -o $@ 20 | 21 | clean: 22 | rm -f $(LD_OBJS) c2bf-ld 23 | -------------------------------------------------------------------------------- /1tox/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 -g 3 | LDFLAGS= 4 | 5 | all: c2bf-1to2 c2bf-1to4 6 | 7 | c2bf-1to2: c2bf-1to2.o 8 | $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ 9 | 10 | c2bf-1to4: c2bf-1to4.o 11 | $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ 12 | 13 | .PREFIXES: .c .o 14 | 15 | .c.o: 16 | $(CC) $(CFLAGS) -c $< -o $@ 17 | 18 | clean: 19 | rm -f c2bf-1to2 c2bf-1to4 c2bf-1to2.o c2bf-1to4.o 20 | -------------------------------------------------------------------------------- /1tox/MultiBitwidth.txt: -------------------------------------------------------------------------------- 1 | Force 32-bit: 2 | 3 | ++++++++[>++++++++<-]>[<++++>-]+< 4 | [>-< More than 8bit 5 | 6 | [>>+>+<<<-]>>>[<[<+<+>>-]<[>+<-]>>-]<[-]<+< 7 | [>-<[-] 1cell code [-]>[-]>[-]<<] 8 | >[-< 2cell code [-]>[-]<] 9 | 10 | ] 11 | >[-< 4cell code [-]] 12 | 13 | 14 | 15 | Force 16-bit: 16 | 17 | ++++++++[>++++++++<-]>[<++++>-]+< 18 | [>-< 1cell code [-]>[-]<] 19 | >[-< 2cell code [-]] 20 | -------------------------------------------------------------------------------- /ld/ld.h: -------------------------------------------------------------------------------- 1 | #ifndef LD_H 2 | #define LD_H 3 | 4 | struct block { 5 | struct block *next, *prev; 6 | int num; 7 | char *label; 8 | char *code; 9 | int used; 10 | }; 11 | 12 | extern struct block *curblock; 13 | 14 | /* blockNum 15 | * input: the name of a symbol 16 | * output: its number (or -1 on error) 17 | * effect: none 18 | */ 19 | int blockNum(const char *label, struct block **into); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /cc/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 -g 3 | LDFLAGS= 4 | 5 | LEX=flex 6 | 7 | YACC=yacc 8 | 9 | DISPAST_OBJS=dispast.o gram.o main.o scan.o 10 | CC_OBJS=dispast.o gram.o genbfmain.o scan.o genbf/libgenbf.a 11 | 12 | all: c2bf-dispast c2bf-cc 13 | 14 | c2bf-dispast: $(DISPAST_OBJS) 15 | $(CC) $(CFLAGS) $(LDFLAGS) $(DISPAST_OBJS) -o $@ 16 | 17 | c2bf-cc: $(CC_OBJS) 18 | $(CC) $(CFLAGS) $(LDFLAGS) $(CC_OBJS) -o $@ 19 | 20 | genbf/libgenbf.a: genbf/*.c 21 | cd genbf ; $(MAKE) 22 | 23 | .PREFIXES: .y .l .c .o 24 | 25 | .y.c: 26 | $(YACC) -dv $< 27 | mv y.tab.c $@ 28 | 29 | .l.c: 30 | $(LEX) -t $< > $@ 31 | 32 | .c.o: 33 | $(CC) $(CFLAGS) -c $< -o $@ 34 | 35 | clean: 36 | rm -f $(DISPAST_OBJS) $(CC_OBJS) gram.c y.tab.h y.output scan.c \ 37 | c2bf-dispast c2bf-cc 38 | cd genbf ; $(MAKE) clean 39 | -------------------------------------------------------------------------------- /cc/tests/test2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void main() 22 | { 23 | int a; 24 | a = 65; 25 | asm("."); 26 | } 27 | -------------------------------------------------------------------------------- /1tox/BF1to2.txt: -------------------------------------------------------------------------------- 1 | 1->2 2 | 3 | [temp][lsc][msc] 4 | 5 | At the beginning of the program, add '>' 6 | 7 | + -> + 8 | [<+>>>+<<-]<[>+<-]+>>>[<<<->>>[-]]<<<[- 9 | >>+<< 10 | ]> 11 | 12 | + -> +[<+>>>+<<-]<[>+<-]+>>>[<<<->>>[-]]<<<[->>+<<]> 13 | 14 | - -> [<+>>>+<<-]<[>+<-]+>>>[<<<->>>[-]]<<<[- 15 | >>-<< 16 | ]>- 17 | 18 | - -> [<+>>>+<<-]<[>+<-]+>>>[<<<->>>[-]]<<<[->>-<<]>- 19 | 20 | OR -> [>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<< 21 | [[-]<<<+>>>]< 22 | [>+>>>+<<<<-]>>>>[<<<<+>>>>-]<<< 23 | [[-]<<<+>>>]<<< 24 | 25 | OR -> [>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<[[-]<<<+>>>]<[>+>>>+<<<<-]>>>>[<<<<+>>>>-]<<<[[-]<<<+>>>]<<< 26 | 27 | [ -> OR [[-]> 28 | [ -> [>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<[[-]<<<+>>>]<[>+>>>+<<<<-]>>>>[<<<<+>>>>-]<<<[[-]<<<+>>>]<<<[[-]> 29 | 30 | ] -> OR ]> 31 | ] -> [>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<[[-]<<<+>>>]<[>+>>>+<<<<-]>>>>[<<<<+>>>>-]<<<[[-]<<<+>>>]<<<]> 32 | 33 | > -> >>> 34 | < -> <<< 35 | , -> , 36 | . -> . 37 | -------------------------------------------------------------------------------- /cc/tests/test3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void main() 22 | { 23 | int a, b, c; 24 | a = 60; 25 | b = 5; 26 | c = a + b; 27 | asm("."); 28 | } 29 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | C2BF is a C compiler targeting brainfuck, written by the twisted mind of Gregor 2 | Richards. 3 | 4 | Like most compilers, C2BF involves a multi-step procedure. Basically, input .c 5 | files are turned into .o files using c2bf-cc. Then, those .o files are linked 6 | using c2bf-ld. The output binary can be stripped, and the bitwidth can be 7 | modified, by c2bf-strip and c2bf-1to*bit, respectively. 8 | 9 | Not all of C is supported yet, but I'm working on it. And you could be too! 10 | Next time c2bf-cc tells you that there's an undefined generation, define it in 11 | cc/genbf/.c :) 12 | 13 | But overall, enjoy. This is a ridiculous project with no real purpose, so just 14 | have some fun with it. Maybe take the abstract syntax tree generation and port 15 | the genration to a different esolang. Maybe change the algorithms to be more 16 | efficient. Or maybe write the most incredible BF program ever written. 17 | 18 | OK, not that last one. That's totally cheating. And we'll be able to tell :) 19 | -------------------------------------------------------------------------------- /cc/tests/test4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | int Afunc() 22 | { 23 | return 65; 24 | } 25 | 26 | void main() 27 | { 28 | int a; 29 | a = Afunc(); 30 | asm("."); 31 | } 32 | -------------------------------------------------------------------------------- /cc/tests/test8.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void main() 22 | { 23 | int i; 24 | for (i = 65; i < 75; i = i + 1) { 25 | i; 26 | asm(">>>>>.<<<<<"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /cc/genbf/file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_file(struct file *a) 25 | { 26 | genbf_external_definition_list(a->v); 27 | printf("\n"); 28 | } 29 | -------------------------------------------------------------------------------- /cc/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "ast.h" 22 | #include "y.tab.h" 23 | 24 | struct file *ast_root; 25 | 26 | int main() 27 | { 28 | if (yyparse() == 0) { 29 | disp_file(1, ast_root); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /cc/tests/test5.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | int fibo(int n) 22 | { 23 | if (n <= 2) return 1; 24 | return fibo(n - 2) + fibo(n - 1); 25 | } 26 | 27 | void main() 28 | { 29 | int a; 30 | a = fibo(11); 31 | asm("."); 32 | } 33 | -------------------------------------------------------------------------------- /cc/tests/test1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void main() 22 | { 23 | asm(">>+>>>>+++++++++[<++++++++>-]<.---.+++++++..+++.>++++++++[<------>-]<+.>+++++++++[<++++++>-]<+.--------.+++.------.--------.>+++++++[<----->-]<.[-]++++++++++.<<<-<<"); 24 | } 25 | -------------------------------------------------------------------------------- /cc/genbfmain.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "ast.h" 22 | #include "y.tab.h" 23 | #include "genbf.h" 24 | 25 | struct file *ast_root; 26 | 27 | int main() 28 | { 29 | if (yyparse() == 0) { 30 | genbf_file(ast_root); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /cc/genbf/statement_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_statement_list(struct statement_list *a) 25 | { 26 | if (!a->end) { 27 | genbf_statement_list(a->v1); 28 | } 29 | genbf_statement(a->v2); 30 | } 31 | -------------------------------------------------------------------------------- /cc/genbf/parameter_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | void genbf_parameter_list(struct parameter_list *a) 24 | { 25 | if (!a->end) { 26 | genbf_parameter_list(a->v1); 27 | } 28 | genbf_parameter_declaration(a->v2); 29 | } 30 | -------------------------------------------------------------------------------- /cc/genbf/declaration_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_declaration_list(struct declaration_list *a) 25 | { 26 | if (!a->end) { 27 | genbf_declaration_list(a->v1); 28 | } 29 | genbf_declaration(a->v2); 30 | } 31 | -------------------------------------------------------------------------------- /ld/trace.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #ifndef TRACE_H 22 | #define TRACE_H 23 | 24 | #include "ld.h" 25 | 26 | /* traceBlock 27 | * input: a block 28 | * output: none 29 | * effect: the 'uses' var is set for this block and all prerequisites 30 | */ 31 | void traceBlock(struct block *tostat); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /cc/genbf/init_declarator_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_init_declarator_list(struct init_declarator_list *a) 25 | { 26 | if (!a->end) { 27 | genbf_init_declarator_list(a->v1); 28 | } 29 | genbf_init_declarator(a->v2); 30 | } 31 | -------------------------------------------------------------------------------- /cc/tests/test6.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | /* BIG FAT NOTE: c2bf doesn't actually support this yet :) */ 22 | 23 | void itoa(int n) 24 | { 25 | int nd, nm; 26 | nd = n / 10; 27 | nm = n % 10; 28 | if (nd) itoa(nd); 29 | nm += '0'; 30 | asm("."); 31 | } 32 | 33 | void main() 34 | { 35 | itoa(123); 36 | } 37 | -------------------------------------------------------------------------------- /cc/genbf/external_definition_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_external_definition_list(struct external_definition_list *a) 25 | { 26 | if (!a->end) { 27 | genbf_external_definition_list(a->v1); 28 | } 29 | genbf_external_definition(a->v2); 30 | } 31 | -------------------------------------------------------------------------------- /cc/genbf/argument_expr_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_argument_expr_list(struct argument_expr_list *a) 25 | { 26 | int cur = 1; 27 | if (!a->end) { 28 | cur += genbf_argument_expr_list(a->v1); 29 | } 30 | genbf_assignment_expr(a->v2, 0, NULL); 31 | return cur; 32 | } 33 | -------------------------------------------------------------------------------- /cc/genbf/expression_statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_expression_statement(struct expression_statement *a) 25 | { 26 | if (a->has_expr) { 27 | genbf_expr(a->v, 0, NULL); 28 | /* expressions always leave a temporary, so get rid of it */ 29 | popVar(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ld/staticize.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #ifndef STATICIZE_H 22 | #define STATICIZE_H 23 | 24 | #include "ld.h" 25 | 26 | /* staticizeBlock 27 | * input: a block 28 | * output: none 29 | * effect: the code in the block has all relocatable elements replaced with 30 | * static elements 31 | */ 32 | void staticizeBlock(struct block *tostat); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /cc/genbf/parameter_type_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_parameter_type_list(struct parameter_type_list *a) 25 | { 26 | /* there is currently no support for varargs */ 27 | if (a->varargs) 28 | ERROR("parameter_identifier_list", "Varargs are not currently supported.\n"); 29 | 30 | genbf_parameter_list(a->v); 31 | } 32 | -------------------------------------------------------------------------------- /cc/genbf/parameter_identifier_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_parameter_identifier_list(struct parameter_identifier_list *a) 25 | { 26 | /* there is currently no support for varargs */ 27 | if (a->varargs) 28 | ERROR("parameter_identifier_list", "Varargs are not currently supported.\n"); 29 | 30 | genbf_identifier_list(a->v); 31 | } 32 | -------------------------------------------------------------------------------- /cc/tests/test7.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void main() 22 | { 23 | char a[14]; 24 | a[0] = 0; 25 | a[1] = 'H'; 26 | a[2] = 'e'; 27 | a[3] = 'l'; 28 | a[4] = 'l'; 29 | a[5] = 'o'; 30 | a[6] = ' '; 31 | a[7] = 'w'; 32 | a[8] = 'o'; 33 | a[9] = 'r'; 34 | a[10] = 'l'; 35 | a[11] = 'd'; 36 | a[12] = '!'; 37 | a[13] = 0; 38 | 39 | asm("<<<<<[<<<<<]>>>>>[.>>>>>]"); 40 | } 41 | -------------------------------------------------------------------------------- /cc/genbf/expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_expr(struct expr *a, int lval, struct type **t) 25 | { 26 | if (!a->end) { 27 | genbf_expr(a->v1, lval, t); 28 | /* FIXME: this is a comma-separated list of expressions, it can't just 29 | * go anywhere ... */ 30 | popVar(); 31 | } 32 | return genbf_assignment_expr(a->v2, lval, t); 33 | } 34 | -------------------------------------------------------------------------------- /cc/genbf/declaration.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_declaration(struct declaration *a) 25 | { 26 | /* FIXME: the declaration specifiers are currently ignored, since all data 27 | * types have the same width 28 | * they will be important when pointer arithmetic is supported */ 29 | if (a->has_init_declarator_list) { 30 | genbf_init_declarator_list(a->v2); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /cc/genbf/external_definition.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_external_definition(struct external_definition *a) 25 | { 26 | switch (a->type) { 27 | case _FUNCTION_DEFINITION: 28 | genbf_function_definition(a->v._function_definition); 29 | break; 30 | 31 | case _DECLARATION: 32 | genbf_declaration(a->v._declaration); 33 | break; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /cc/genbf/compound_statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | /* You must pushBlock or pushNamedBlock BEFORE calling this, since this doesn't 25 | * know what sort of a block you need pushed! */ 26 | void genbf_compound_statement(struct compound_statement *a) 27 | { 28 | if (a->has_declaration_list) { 29 | genbf_declaration_list(a->v1); 30 | } 31 | if (a->has_statement_list) { 32 | genbf_statement_list(a->v2); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /cc/tests/test9.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void main() 22 | { 23 | char a[13]; 24 | int i; 25 | 26 | a[0] = 'H'; 27 | a[1] = 'e'; 28 | a[2] = 'l'; 29 | a[3] = 'l'; 30 | a[4] = 'o'; 31 | a[5] = ' '; 32 | a[6] = 'w'; 33 | a[7] = 'o'; 34 | a[8] = 'r'; 35 | a[9] = 'l'; 36 | a[10] = 'd'; 37 | a[11] = '!'; 38 | a[12] = 0; 39 | 40 | for (i = 0; asm("<<<[<<<<<]>>>#>>[>>>>>]<<"), a[i]; i = i + 1) { 41 | a[i]; 42 | asm(">>>>>.<<<<<"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /cc/tests/test10.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | void putchar(char c) { asm("."); } 22 | 23 | void puts(char *s) 24 | { 25 | int i; 26 | for (i = 0; s[i]; i = i + 1) { 27 | putchar(s[i]); 28 | } 29 | } 30 | 31 | void main() 32 | { 33 | char a[13]; 34 | a[0] = 'H'; 35 | a[1] = 'e'; 36 | a[2] = 'l'; 37 | a[3] = 'l'; 38 | a[4] = 'o'; 39 | a[5] = ' '; 40 | a[6] = 'w'; 41 | a[7] = 'o'; 42 | a[8] = 'r'; 43 | a[9] = 'l'; 44 | a[10] = 'd'; 45 | a[11] = '!'; 46 | a[12] = 0; 47 | 48 | puts(a); 49 | } 50 | -------------------------------------------------------------------------------- /math/bf_math.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | int __builtin_mul(int a, int b) 22 | { 23 | int r; 24 | r = 0; 25 | 26 | for (; a != 0; a = a - 1) { 27 | r = r + b; 28 | } 29 | 30 | return r + b; 31 | } 32 | 33 | int __builtin_div(int a, int b) 34 | { 35 | int r; 36 | r = 0; 37 | 38 | while (a >= b) { 39 | a = a - b; 40 | r = r + 1; 41 | } 42 | 43 | return r; 44 | } 45 | 46 | int __builtin_mod(int a, int b) 47 | { 48 | while (a >= b) { 49 | a = a - b; 50 | } 51 | 52 | return a; 53 | } 54 | -------------------------------------------------------------------------------- /cc/genbf/and_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_and_expr(struct and_expr *a, int lval, struct type **t) 25 | { 26 | if (!a->end) { 27 | if (lval) 28 | ERROR("and_expr", "Invalid l-value."); 29 | 30 | /* genbf_and_expr(a->v1); 31 | SPC; printf("&\n"); */ 32 | UNIMPL("and_expr"); 33 | } 34 | return genbf_equality_expr(a->v2, lval, t); 35 | } 36 | 37 | char *genbf_and_expr_get_primary(int type, struct and_expr *a) 38 | { 39 | if (!a->end) return NULL; 40 | return genbf_equality_expr_get_primary(type, a->v2); 41 | } 42 | -------------------------------------------------------------------------------- /wrapper/c2bf: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export DIR="`dirname $0`" 3 | export PATH="$DIR/../cc:$DIR/../ld:$DIR/../1tox:$DIR/../strip:$PATH" 4 | 5 | # Step one: list of files 6 | export CFILES="" 7 | export OFILES="" 8 | export TFILES="" 9 | export FILTER="cat" 10 | export STRIP="cat" 11 | while [ "$#" != "0" ] 12 | do 13 | if [ "`echo $1 | grep '\.c$'`" ] 14 | then 15 | export CFILES="$CFILES $1" 16 | export OFILES="$OFILES ${1/.c}.o" 17 | export TFILES="$TFILES ${1/.c}.o" 18 | elif [ "`echo $1 | grep '\.o$'`" ] 19 | then 20 | export OFILES="$OFILES $1" 21 | 22 | elif [ "$1" = "-help" -o \ 23 | "$1" = "--help" -o \ 24 | "$1" = "-h" ] 25 | then 26 | echo 'Use: c2bf [-16|-32] [-strip] {filenames}' 27 | exit 0 28 | 29 | elif [ "$1" = "-16" ] 30 | then 31 | export FILTER="c2bf-1to16bit" 32 | elif [ "$1" = "-32" ] 33 | then 34 | export FILTER="c2bf-1to32bit" 35 | 36 | elif [ "$1" = "-strip" ] 37 | then 38 | export STRIP="c2bf-strip" 39 | 40 | else 41 | echo "I don't know how to handle $1!" 42 | fi 43 | 44 | shift 45 | done 46 | 47 | # Step two: compile CFILES 48 | for i in $CFILES 49 | do 50 | c2bf-cc < $i > ${i/.c}.o || exit 1 51 | done 52 | 53 | # Step three: put it all together 54 | cat $OFILES $DIR/../ld/__start.o $DIR/../math/bf_math.o | c2bf-ld > a.b.1 55 | $FILTER a.b.1 | $STRIP > a.b 56 | rm -f $TFILES a.b.1 57 | -------------------------------------------------------------------------------- /cc/genbf/init_declarator.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_init_declarator(struct init_declarator *a) 25 | { 26 | struct type *vt; 27 | int i; 28 | 29 | /* push this variable */ 30 | pushVar(genbf_declarator2_get_identifier(a->v1->v2), 1); 31 | 32 | /* then use genbf_declarator to get its type */ 33 | vt = genbf_declarator(a->v1, NULL); 34 | curvar->type = vt; 35 | curvar->width = vt->size; 36 | 37 | /* and push it in BF */ 38 | for (i = 0; i < curvar->width; i++) 39 | BF_PUSH; 40 | 41 | if (a->assign) { 42 | UNIMPL("init_declarator with assignment"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /cc/genbf/exclusive_or_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_exclusive_or_expr(struct exclusive_or_expr *a, int lval, struct type **t) 25 | { 26 | if (!a->end) { 27 | if (lval) 28 | ERROR("exclusive_or_expr", "Invalid l-value."); 29 | 30 | /* genbf_exclusive_or_expr(a->v1); 31 | SPC; printf("^\n"); */ 32 | UNIMPL("exclusive_or_expr"); 33 | } 34 | return genbf_and_expr(a->v2, lval, t); 35 | } 36 | 37 | char *genbf_exclusive_or_expr_get_primary(int type, struct exclusive_or_expr *a) 38 | { 39 | if (!a->end) return NULL; 40 | return genbf_and_expr_get_primary(type, a->v2); 41 | } 42 | -------------------------------------------------------------------------------- /cc/genbf/logical_or_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_logical_or_expr(struct logical_or_expr *a, int lval, struct type **t) 25 | { 26 | if (!a->end) { 27 | if (lval) 28 | ERROR("logical_or_expr", "Invalid l-value."); 29 | 30 | /* genbf_logical_or_expr(a->v1); 31 | SPC; printf("||\n"); */ 32 | UNIMPL("logical_or_expr"); 33 | } 34 | return genbf_logical_and_expr(a->v2, lval, t); 35 | } 36 | 37 | char *genbf_logical_or_expr_get_primary(int type, struct logical_or_expr *a) 38 | { 39 | if (!a->end) return NULL; 40 | return genbf_logical_and_expr_get_primary(type, a->v2); 41 | } 42 | -------------------------------------------------------------------------------- /cc/genbf/logical_and_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_logical_and_expr(struct logical_and_expr *a, int lval, struct type **t) 25 | { 26 | if (!a->end) { 27 | if (lval) 28 | ERROR("logical_and_expr", "Invalid l-value."); 29 | 30 | /* genbf_logical_and_expr(a->v1); 31 | SPC; printf("&&\n"); */ 32 | UNIMPL("logical_and_expr"); 33 | } 34 | return genbf_inclusive_or_expr(a->v2, lval, t); 35 | } 36 | 37 | char *genbf_logical_and_expr_get_primary(int type, struct logical_and_expr *a) 38 | { 39 | if (!a->end) return NULL; 40 | return genbf_inclusive_or_expr_get_primary(type, a->v2); 41 | } 42 | -------------------------------------------------------------------------------- /cc/genbf/inclusive_or_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_inclusive_or_expr(struct inclusive_or_expr *a, int lval, struct type **t) 25 | { 26 | if (!a->end) { 27 | if (lval) 28 | ERROR("inclusive_or_expr", "Invalid l-value."); 29 | 30 | /* genbf_inclusive_or_expr(a->v1); 31 | SPC; printf("|\n"); */ 32 | UNIMPL("inclusive_or_expr"); 33 | } 34 | return genbf_exclusive_or_expr(a->v2, lval, t); 35 | } 36 | 37 | char *genbf_inclusive_or_expr_get_primary(int type, struct inclusive_or_expr *a) 38 | { 39 | if (!a->end) return NULL; 40 | return genbf_exclusive_or_expr_get_primary(type, a->v2); 41 | } 42 | -------------------------------------------------------------------------------- /cc/genbf/conditional_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_conditional_expr(struct conditional_expr *a, int lval, struct type **t) 25 | { 26 | return genbf_logical_or_expr(a->v1, lval, t); 27 | if (!a->end) { 28 | /* FIXME: how should lval be handled here? */ 29 | 30 | /* SPC; printf("?\n"); 31 | genbf_logical_or_expr(a->v2); 32 | SPC; printf(":\n"); 33 | genbf_conditional_expr(a->v3); */ 34 | UNIMPL("conditional_expr"); 35 | } 36 | } 37 | 38 | char *genbf_conditional_expr_get_primary(int type, struct conditional_expr *a) 39 | { 40 | if (!a->end) return NULL; 41 | return genbf_logical_or_expr_get_primary(type, a->v1); 42 | } 43 | -------------------------------------------------------------------------------- /cc/genbf/cast_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_cast_expr(struct cast_expr *a, int lval, struct type **t) 25 | { 26 | switch (a->type) { 27 | case _UNARY_EXPR: 28 | return genbf_unary_expr(a->v1._unary_expr, lval, t); 29 | break; 30 | 31 | /* case _TYPE_CAST: 32 | SPC; printf("(\n"); 33 | genbf_type_name(a->v1._type_name); 34 | SPC; printf(")\n"); 35 | genbf_cast_expr(a->v2); 36 | break; */ 37 | 38 | default: 39 | UNIMPL("cast_expr"); 40 | } 41 | } 42 | 43 | char *genbf_cast_expr_get_primary(int type, struct cast_expr *a) 44 | { 45 | if (a->type != _UNARY_EXPR) return NULL; 46 | return genbf_unary_expr_get_primary(type, a->v1._unary_expr); 47 | } 48 | -------------------------------------------------------------------------------- /cc/genbf/declarator.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | struct type *genbf_declarator(struct declarator *a, struct type *into) 25 | { 26 | struct type *t, *nt; 27 | 28 | t = genbf_declarator2(a->v2, into); 29 | 30 | if (a->pointer) { 31 | /* FIXME: this should handle type_specifier_list */ 32 | struct pointer *cur = a->v1; 33 | while (cur) { 34 | NEW(nt, struct type); 35 | nt->next = t; 36 | nt->basic_type = TYPE_PTR; 37 | nt->array = 0; 38 | nt->size = 1; 39 | t = nt; 40 | 41 | /* if this is the end, no more curs */ 42 | if (cur->end) { 43 | cur = NULL; 44 | } else { 45 | cur = cur->v2; 46 | } 47 | } 48 | } 49 | 50 | return t; 51 | } 52 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | BFI=egobfi16 3 | 4 | # Test 1 5 | echo -n "Test 1: " 6 | ./wrapper/c2bf cc/tests/test1.c 7 | export OUTP="`$BFI a.b`" 8 | if [ "$OUTP" = "HELLO WORLD!" ] ; then echo "PASS" 9 | else echo "FAIL" 10 | fi 11 | 12 | # Test 2 13 | echo -n "Test 2: " 14 | ./wrapper/c2bf cc/tests/test2.c 15 | export OUTP="`$BFI a.b`" 16 | if [ "$OUTP" = "A" ] ; then echo "PASS" 17 | else echo "FAIL" 18 | fi 19 | 20 | # Test 3 21 | echo -n "Test 3: " 22 | ./wrapper/c2bf cc/tests/test3.c 23 | export OUTP="`$BFI a.b`" 24 | if [ "$OUTP" = "A" ] ; then echo "PASS" 25 | else echo "FAIL" 26 | fi 27 | 28 | # Test 4 29 | echo -n "Test 4: " 30 | ./wrapper/c2bf cc/tests/test4.c 31 | export OUTP="`$BFI a.b`" 32 | if [ "$OUTP" = "A" ] ; then echo "PASS" 33 | else echo "FAIL" 34 | fi 35 | 36 | # Test 5 37 | echo -n "Test 5: " 38 | ./wrapper/c2bf cc/tests/test5.c 39 | export OUTP="`$BFI a.b`" 40 | if [ "$OUTP" = "Y" ] ; then echo "PASS" 41 | else echo "FAIL" 42 | fi 43 | 44 | # Test 7 45 | echo -n "Test 7: " 46 | ./wrapper/c2bf cc/tests/test7.c 47 | export OUTP="`$BFI a.b`" 48 | if [ "$OUTP" = "Hello world!" ] ; then echo "PASS" 49 | else echo "FAIL" 50 | fi 51 | 52 | # Test 8 53 | echo -n "Test 8: " 54 | ./wrapper/c2bf cc/tests/test8.c 55 | export OUTP="`$BFI a.b`" 56 | if [ "$OUTP" = "ABCDEFGHIJ" ] ; then echo "PASS" 57 | else echo "FAIL" 58 | fi 59 | 60 | # Test 9 61 | echo -n "Test 9: " 62 | ./wrapper/c2bf cc/tests/test9.c 63 | export OUTP="`$BFI a.b`" 64 | if [ "$OUTP" = "Hello world!" ] ; then echo "PASS" 65 | else echo "FAIL" 66 | fi 67 | 68 | # Test 10 69 | echo -n "Test 10: " 70 | ./wrapper/c2bf cc/tests/test10.c 71 | export OUTP="`$BFI a.b`" 72 | if [ "$OUTP" = "Hello world!" ] ; then echo "PASS" 73 | else echo "FAIL" 74 | fi 75 | 76 | # Done 77 | rm -f a.b 78 | -------------------------------------------------------------------------------- /cc/genbf/parameter_declaration.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_parameter_declaration(struct parameter_declaration *a) 25 | { 26 | struct type *vt; 27 | 28 | /* the tree here should look like this: 29 | -parameter_declaration 30 | |(type must be _DECLARATION) 31 | | 32 | |-type_specifier_list (v1._type_specifier_list) 33 | ||FIXME: this is ignored at the moment 34 | | 35 | |-declarator (v2) 36 | || 37 | ||-declarator2 (v2) (guaranteed) 38 | |||genbf_declarator2_get_identifier is used to find the identifier 39 | */ 40 | if (a->type != _DECLARATION) 41 | ERROR("parameter_declaration", "Type name parameters are not yet supported.\n"); 42 | 43 | pushVar(genbf_declarator2_get_identifier(a->v2->v2), 1); 44 | 45 | /* find its type */ 46 | vt = genbf_declarator(a->v2, NULL); 47 | curvar->type = vt; 48 | curvar->width = vt->size; 49 | } 50 | -------------------------------------------------------------------------------- /cc/genbf/statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_statement(struct statement *a) 25 | { 26 | switch (a->type) { 27 | case _LABELED_STATEMENT: 28 | genbf_labeled_statement(a->v._labeled_statement); 29 | break; 30 | 31 | case _COMPOUND_STATEMENT: 32 | genbf_compound_statement(a->v._compound_statement); 33 | break; 34 | 35 | case _EXPRESSION_STATEMENT: 36 | genbf_expression_statement(a->v._expression_statement); 37 | break; 38 | 39 | case _SELECTION_STATEMENT: 40 | genbf_selection_statement(a->v._selection_statement); 41 | break; 42 | 43 | case _ITERATION_STATEMENT: 44 | genbf_iteration_statement(a->v._iteration_statement); 45 | break; 46 | 47 | case _JUMP_STATEMENT: 48 | genbf_jump_statement(a->v._jump_statement); 49 | break; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /cc/genbf/shift_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_shift_expr(struct shift_expr *a, int lval, struct type **t) 25 | { 26 | switch (a->type) { 27 | case _ADDITIVE_EXPR: 28 | return genbf_additive_expr(a->v1._additive_expr, lval, t); 29 | break; 30 | 31 | /* case _LEFT_SHIFT: 32 | genbf_shift_expr(a->v1._shift_expr); 33 | SPC; printf("<<\n"); 34 | genbf_additive_expr(a->v2); 35 | break; 36 | 37 | case _RIGHT_SHIFT: 38 | genbf_shift_expr(a->v1._shift_expr); 39 | SPC; printf(">>\n"); 40 | genbf_additive_expr(a->v2); 41 | break; */ 42 | 43 | default: 44 | UNIMPL("shift_expr"); 45 | } 46 | } 47 | 48 | char *genbf_shift_expr_get_primary(int type, struct shift_expr *a) 49 | { 50 | if (a->type != _ADDITIVE_EXPR) return NULL; 51 | return genbf_additive_expr_get_primary(type, a->v1._additive_expr); 52 | } 53 | -------------------------------------------------------------------------------- /cc/genbf/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-O2 -g 3 | 4 | AR=ar 5 | ARFLAGS=rcu 6 | 7 | LIBGENBF_A_OBJS=abstract_declarator2.o \ 8 | abstract_declarator.o \ 9 | additive_expr.o \ 10 | and_expr.o \ 11 | argument_expr_list.o \ 12 | assignment_expr.o \ 13 | assignment_operator.o \ 14 | cast_expr.o \ 15 | compound_statement.o \ 16 | conditional_expr.o \ 17 | declaration.o \ 18 | declaration_list.o \ 19 | declaration_specifiers.o \ 20 | declarator2.o \ 21 | declarator.o \ 22 | enumerator.o \ 23 | enumerator_list.o \ 24 | enum_specifier.o \ 25 | equality_expr.o \ 26 | exclusive_or_expr.o \ 27 | expr.o \ 28 | expression_statement.o \ 29 | external_definition.o \ 30 | external_definition_list.o \ 31 | file.o \ 32 | function_body.o \ 33 | function_definition.o \ 34 | identifier.o \ 35 | identifier_list.o \ 36 | inclusive_or_expr.o \ 37 | init_declarator.o \ 38 | init_declarator_list.o \ 39 | initializer.o \ 40 | initializer_list.o \ 41 | iteration_statement.o \ 42 | jump_statement.o \ 43 | labeled_statement.o \ 44 | logical_and_expr.o \ 45 | logical_or_expr.o \ 46 | multiplicative_expr.o \ 47 | parameter_declaration.o \ 48 | parameter_identifier_list.o \ 49 | parameter_list.o \ 50 | parameter_type_list.o \ 51 | pointer.o \ 52 | postfix_expr.o \ 53 | primary_expr.o \ 54 | relational_expr.o \ 55 | selection_statement.o \ 56 | shift_expr.o \ 57 | statement.o \ 58 | statement_list.o \ 59 | storage_class_specifier.o \ 60 | struct_declaration.o \ 61 | struct_declaration_list.o \ 62 | struct_declarator.o \ 63 | struct_declarator_list.o \ 64 | struct_or_union.o \ 65 | struct_or_union_specifier.o \ 66 | type_name.o \ 67 | type_specifier.o \ 68 | type_specifier_list.o \ 69 | unary_expr.o \ 70 | unary_operator.o \ 71 | generator.o 72 | 73 | all: libgenbf.a 74 | 75 | libgenbf.a: $(LIBGENBF_A_OBJS) 76 | $(AR) $(ARFLAGS) $@ $(LIBGENBF_A_OBJS) 77 | 78 | .PREFIXES: .c .o 79 | 80 | .c.o: 81 | $(CC) $(CFLAGS) -c $< -o $@ 82 | 83 | clean: 84 | rm -f $(LIBGENBF_A_OBJS) libgenbf.a 85 | -------------------------------------------------------------------------------- /cc/genbf/additive_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_additive_expr(struct additive_expr *a, int lval, struct type **t) 25 | { 26 | if (a->type == _MULTIPLICATIVE_EXPR) { 27 | return genbf_multiplicative_expr(a->v1._multiplicative_expr, lval, t); 28 | } 29 | 30 | if (lval) 31 | ERROR("additive_expr", "Invalid l-value."); 32 | 33 | /* generate the left and right side as temps, then do the add or subtract */ 34 | genbf_additive_expr(a->v1._additive_expr, 0, NULL); 35 | genbf_multiplicative_expr(a->v2, 0, NULL); 36 | if (a->type == _ADD) { 37 | /* I should be on top of operand 2 */ 38 | printf("[<<<<<+>>>>>-]"); 39 | } else if (a->type == _SUBTRACT) { 40 | printf("[<<<<<->>>>>-]"); 41 | } 42 | popVar(); 43 | fflush(stdout); 44 | } 45 | 46 | char *genbf_additive_expr_get_primary(int type, struct additive_expr *a) 47 | { 48 | if (a->type != _MULTIPLICATIVE_EXPR) return NULL; 49 | return genbf_multiplicative_expr_get_primary(type, a->v1._multiplicative_expr); 50 | } 51 | -------------------------------------------------------------------------------- /cc/genbf/builtin_functions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | /* this file is #include'd in the middle of postfix_expr.c, to not clutter it 22 | * with builtins */ 23 | 24 | if (!strcmp(f, "asm")) { 25 | char *asmc; 26 | 27 | /* the tree should look like this: 28 | -postfix_expr 29 | |(type must be _FCALL) 30 | | 31 | |-argument_expr_list (v2._argument_expr_list) 32 | ||(end must be 1) 33 | || 34 | ||-assignment_expr (v2) 35 | |||genbf_assignment_expr_get_primary is used to get the deeply nested 36 | ||| string value 37 | */ 38 | if (a->type != _FCALL) 39 | ERROR("builtin_functions", "asm: no parameters provided"); 40 | 41 | if (!a->v2._argument_expr_list->end) 42 | ERROR("builtin_functions", "asm: too many parameters provided"); 43 | 44 | asmc = genbf_assignment_expr_get_primary(_STRING, 45 | a->v2._argument_expr_list->v2); 46 | 47 | if (!asmc) 48 | ERROR("builtin_functions", "asm: wrong parameter type"); 49 | 50 | printf(asmc); 51 | BF_PUSH; 52 | pushTempVar(1); 53 | } 54 | -------------------------------------------------------------------------------- /cc/genbf/equality_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_equality_expr(struct equality_expr *a, int lval, struct type **t) 25 | { 26 | if (a->type == _RELATIONAL_EXPR) { 27 | return genbf_relational_expr(a->v1._relational_expr, lval, t); 28 | } 29 | 30 | if (lval) 31 | ERROR("equality_expr", "Invalid l-value."); 32 | 33 | genbf_equality_expr(a->v1._equality_expr, 0, NULL); 34 | genbf_relational_expr(a->v2, 0, NULL); 35 | switch (a->type) { 36 | case _EQUAL: 37 | printf("<<<<<[>>>>+<<<<-]+" 38 | ">>>>>[<-<+>>-]" 39 | "<<[>>+<<-]" 40 | ">[<<<<->>>>[-]]>"); 41 | break; 42 | 43 | case _NEQUAL: 44 | printf("<<<<<[>>>>+<<<<-]" 45 | ">>>>>[<-<+>>-]" 46 | "<<[>>+<<-]" 47 | ">[<<<<+>>>>[-]]>"); 48 | break; 49 | } 50 | 51 | popVar(); 52 | fflush(stdout); 53 | } 54 | 55 | char *genbf_equality_expr_get_primary(int type, struct equality_expr *a) 56 | { 57 | if (a->type != _RELATIONAL_EXPR) return NULL; 58 | return genbf_relational_expr_get_primary(type, a->v1._relational_expr); 59 | } 60 | -------------------------------------------------------------------------------- /cc/README.txt: -------------------------------------------------------------------------------- 1 | This is Gregor Richards' parser and abstract syntax tree generator for C. I 2 | used a YACC parser from Jeff Lee, the README for which is included below. The 3 | structure can be understood best by looking through dispast.c. 4 | 5 | To get to the code generation step, add generate_* functions to replace the 6 | disp_* functions, then copy dispast.c to generate.c and hack all the disp_* 7 | functions into code generators. It will do depth-first code generation, which 8 | should be fine in most scenarios. 9 | 10 | Note that you will have to add your own semantic checker. You can do this as 11 | part of the code generator, if you please, or you can add yet another depth- 12 | first descender based on the disp_* functions to do semantic checking. 13 | 14 | 15 | Here is the README for the grammar: 16 | 17 | The files in this directory contain the ANSI C grammar from the April 30, 1985 18 | draft of the proposed standard. This copy also incorporates all bug fixes I 19 | have seen since the last two postings. With a little work this grammar can 20 | be made to parse the C that most of us know and love (sort of). 21 | 22 | There is one bug fix to the grammar that is in this posting. On line 295 23 | of gram.y it previously read declaration_specifiers instead of 24 | type_specifier_list as it does now. I believe the folks at the ANSI committee 25 | made a mistake since if you replace the line with what the original read 26 | you will end up with 16 shift/reduce errors and 2 reduce/reduce errors 27 | (the good ones). As it is, it only has 1 shift/reduce error that occurs 28 | on the if/else construct. YACC creates the correct parser and I don't want 29 | to ugly my grammar up. 30 | 31 | Anyway, all cumquats unite and generate this sucker. Then just sit and play 32 | with it. Remember, the grammar accepts things like 33 | 34 | "Hello, world"++; 35 | --1.23456; 36 | *'a' 37 | 38 | but this is not a bug, but simply a shuffling of the checking into the 39 | semantic analysis. If you want to hack it up to do lvalue and rvalue 40 | checking, I'm sure the ANSI committee would be glad to have your changes. 41 | Don't send'em to me though. I don't want'em. Wear this in good health. 42 | 43 | Jeff Lee 44 | gatech!jeff jeff@gatech jeff%gatech.CSNet@CSNet-Relay.ARPA 45 | -------------------------------------------------------------------------------- /1tox/c2bf-1to2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | 23 | int main() 24 | { 25 | int c; 26 | 27 | printf(">"); 28 | 29 | while ((c = getchar()) != EOF) { 30 | switch (c) { 31 | case '>': 32 | printf(">>>"); 33 | break; 34 | 35 | case '<': 36 | printf("<<<"); 37 | break; 38 | 39 | case ',': 40 | printf(","); 41 | break; 42 | 43 | case '.': 44 | printf("."); 45 | break; 46 | 47 | case '+': 48 | printf("+[<+>>>+<<-]<[>+<-]+>>>[<<<->>>[-]]<<<[->>+<<]>"); 49 | break; 50 | 51 | case '-': 52 | printf("[<+>>>+<<-]<[>+<-]+>>>[<<<->>>[-]]<<<[->>-<<]>-"); 53 | break; 54 | 55 | case '[': 56 | printf("[>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<[[-]<<<+>>>]<[>+>" 57 | ">>+<<<<-]>>>>[<<<<+>>>>-]<<<[[-]<<<+>>>]<<<[[-]>"); 58 | break; 59 | 60 | case ']': 61 | printf("[>>+>>>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<[[-]<<<+>>>]<[>+>" 62 | ">>+<<<<-]>>>>[<<<<+>>>>-]<<<[[-]<<<+>>>]<<<]>"); 63 | break; 64 | 65 | case '#': 66 | printf("#"); 67 | break; 68 | } 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /1tox/BF1to4.txt: -------------------------------------------------------------------------------- 1 | 1->4 2 | 3 | [temp][lsc][2][3][msc] 4 | 5 | At the beginning of the program, add '>' 6 | 7 | + -> + 8 | [<+>>>>>+<<<<-]<[>+<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[- 9 | >>+ 10 | [<<+>>>>>+<<<-]<<[>>+<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[- 11 | >>>+ 12 | [<<<+>>>>>+<<-]<<<[>>>+<<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[- 13 | >>>>+<<<< 14 | ] 15 | ] 16 | ]> 17 | 18 | + -> +[<+>>>>>+<<<<-]<[>+<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>+[<<+>>>>>+<<<-]<<[>>+<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>>+[<<<+>>>>>+<<-]<<<[>>>+<<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>>>+<<<<]]]> 19 | 20 | - -> [<+>>>>>+<<<<-]<[>+<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[- 21 | >> 22 | [<<+>>>>>+<<<-]<<[>>+<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[- 23 | >>> 24 | [<<<+>>>>>+<<-]<<<[>>>+<<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[- 25 | >>>>-<<<< 26 | ] 27 | >>>-<<< 28 | ] 29 | >>-<< 30 | ]>- 31 | 32 | - -> [<+>>>>>+<<<<-]<[>+<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>[<<+>>>>>+<<<-]<<[>>+<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>>[<<<+>>>>>+<<-]<<<[>>>+<<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>>>-<<<<]>>>-<<<]>>-<<]>- 33 | 34 | OR -> [>>>>+>>>>>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<< 35 | [[-]<<<<<+>>>>>]<<< 36 | [>>>+>>>>>+<<<<<<<<-]>>>>>>>>[<<<<<<<<+>>>>>>>>-]<<<<< 37 | [[-]<<<<<+>>>>>]<< 38 | [>>+>>>>>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<<<<< 39 | [[-]<<<<<+>>>>>]< 40 | [>+>>>>>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<< 41 | [[-]<<<<<+>>>>>]<<<<< 42 | 43 | OR -> [>>>>+>>>>>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<<[>>>+>>>>>+<<<<<<<<-]>>>>>>>>[<<<<<<<<+>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<[>>+>>>>>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<[>+>>>>>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<<<< 44 | 45 | [ -> OR [[-]> 46 | [ -> [>>>>+>>>>>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<<[>>>+>>>>>+<<<<<<<<-]>>>>>>>>[<<<<<<<<+>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<[>>+>>>>>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<[>+>>>>>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<<<<[[-]> 47 | 48 | ] -> OR ]> 49 | ] -> [>>>>+>>>>>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<<[>>>+>>>>>+<<<<<<<<-]>>>>>>>>[<<<<<<<<+>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<[>>+>>>>>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<[>+>>>>>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<<<<]> 50 | 51 | > -> >>>>> 52 | < -> <<<<< 53 | , -> , 54 | . -> . 55 | -------------------------------------------------------------------------------- /strip/bfstrip.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | bfstrip.c: A Brainf*** program optimizer -sort of- 4 | 5 | Copyright 2003 Erik Bosman 6 | 7 | This program is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation; either version 2 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program; if not, write to the Free Software 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 | 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | int c; 27 | 28 | long count; 29 | 30 | long newline_count; 31 | 32 | void ask_newline() 33 | { 34 | if (++newline_count == 80) 35 | { 36 | fputc('\n', stdout); 37 | newline_count = 0; 38 | } 39 | } 40 | 41 | void print_n_times_x(long n, int x) 42 | { 43 | long i; 44 | 45 | if (n<0) for (i =-n; i>0; i--) 46 | { 47 | fputc(x-1, stdout); 48 | ask_newline(); 49 | } 50 | if (n>0) for (i = n; i>0; i--) 51 | { 52 | fputc(x+1, stdout); 53 | ask_newline(); 54 | } 55 | } 56 | 57 | 58 | void minimalize(int type) 59 | { 60 | count += c - type; 61 | 62 | while (!feof(stdin)) 63 | { 64 | c = fgetc(stdin); 65 | 66 | if (( c != '<' ) && ( c != '>' ) && 67 | ( c != '+' ) && ( c != '-' ) && 68 | ( c != '[' ) && ( c != ']' ) && 69 | ( c != ',' ) && ( c != '.' )) continue; 70 | 71 | if ( (c != type +1) && (c != type -1) ) 72 | { 73 | ungetc(c, stdin); 74 | break; 75 | } 76 | 77 | count += c - type; 78 | } 79 | 80 | print_n_times_x(count, type); 81 | } 82 | 83 | 84 | int main(int argc, char **argv) 85 | { 86 | 87 | newline_count = 0; 88 | 89 | while (!feof(stdin)) 90 | { 91 | count = 0; 92 | 93 | c = fgetc(stdin); 94 | 95 | switch (c) 96 | { 97 | case '<': case '>': 98 | minimalize('='); 99 | break; 100 | case '+': case '-': 101 | minimalize(','); 102 | break; 103 | case '[': case ']': case ',': case '.': 104 | fputc(c, stdout); 105 | ask_newline(); 106 | break; 107 | default: 108 | break; 109 | } 110 | } 111 | 112 | if (newline_count) fputc('\n', stdout); 113 | 114 | exit(0); 115 | } 116 | 117 | -------------------------------------------------------------------------------- /ld/trace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "ld.h" 26 | #include "staticize.h" 27 | 28 | void traceBlock(struct block *tostat) 29 | { 30 | char *code; 31 | int codelen, num; 32 | int i, j, o; 33 | struct block *fblock; 34 | 35 | code = strdup(tostat->code); 36 | if (!code) { perror("strdup"); exit(1); } 37 | 38 | /* loop through the code */ 39 | o = 0; 40 | for (i = 0; code[i]; i++) { 41 | switch (code[i]) { 42 | case '(': 43 | /* find the end and null it */ 44 | for (j = i + 1; code[j] && code[j] != ')'; j++); 45 | if (!code[j]) { 46 | /* no ending ) ! */ 47 | fprintf(stderr, "Invalid reference %s" 48 | " at column %d" 49 | " in label %s\n", code + i, 50 | i, 51 | tostat->label); 52 | exit(1); 53 | } 54 | code[j] = '\0'; 55 | 56 | i++; 57 | if (code[i] == '*') i++; 58 | 59 | /* get the block */ 60 | num = blockNum(code + i, &fblock); 61 | if (num == -1) { 62 | fprintf(stderr, "Undefined symbol: '%s'" 63 | " at column %d" 64 | " in label %s\n", code + i, 65 | i, 66 | tostat->label); 67 | exit(1); 68 | } 69 | 70 | /* recurse if we haven't */ 71 | if (fblock->used == 0) { 72 | fblock->used = 1; 73 | traceBlock(fblock); 74 | } 75 | /* now we can continue */ 76 | i = j; 77 | break; 78 | 79 | case '!': 80 | /* this symbol means "ignore any further input for this block" */ 81 | for (; code[i]; i++); 82 | i--; 83 | break; 84 | } 85 | } 86 | 87 | free(code); 88 | } 89 | -------------------------------------------------------------------------------- /cc/genbf/declarator2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | struct type *genbf_declarator2(struct declarator2 *a, struct type *into) 25 | { 26 | struct type *nt; 27 | char *szs; 28 | int sz; 29 | 30 | switch (a->type) { 31 | case _IDENTIFIER: 32 | NEW(nt, struct type); 33 | nt->next = into; 34 | /* FIXME: there are other types :P */ 35 | nt->basic_type = TYPE_INT; 36 | nt->array = 0; 37 | nt->size = 1; 38 | return nt; 39 | 40 | case _DECLARATOR: 41 | return genbf_declarator(a->v1._declarator, into); 42 | 43 | case _ARRAY: 44 | /* we must add this to the type for the below declarator2 */ 45 | into = genbf_declarator2(a->v1._declarator2, into); 46 | NEW(nt, struct type); 47 | nt->next = into; 48 | 49 | /* get the size out of the expression, hopefully */ 50 | szs = genbf_conditional_expr_get_primary(_CONSTANT, a->v2._conditional_expr); 51 | if (!szs) { 52 | ERROR("declarator2", "Complex array definitions are not yet supported."); 53 | } 54 | 55 | /* turn it into a number */ 56 | if (szs[0] == '\'') { 57 | sz = *genbf_parse_string(szs); 58 | } else { 59 | sz = atoi(szs); 60 | } 61 | 62 | nt->basic_type = TYPE_PTR; 63 | nt->array = sz; 64 | nt->size = sz * into->size; 65 | 66 | return nt; 67 | 68 | default: 69 | UNIMPL("declarator2"); 70 | } 71 | } 72 | 73 | /* this function allows you to take a whole declarator2 tree and just find the 74 | * identifier from it */ 75 | char *genbf_declarator2_get_identifier(struct declarator2 *a) 76 | { 77 | switch (a->type) { 78 | case _IDENTIFIER: 79 | return a->v1._identifier->v; /* here it is! */ 80 | 81 | case _DECLARATOR: 82 | /* we need to recurse through this */ 83 | return genbf_declarator2_get_identifier(a->v1._declarator->v2); 84 | 85 | default: 86 | return genbf_declarator2_get_identifier(a->v1._declarator2); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /cc/genbf/unary_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_unary_expr(struct unary_expr *a, int lval, struct type **t) 25 | { 26 | struct type *tignore; 27 | int d; 28 | 29 | switch (a->type) { 30 | case _POSTFIX_EXPR: 31 | return genbf_postfix_expr(a->v1._postfix_expr, lval, t); 32 | break; 33 | 34 | case _UNARY_OP: 35 | if (lval) 36 | ERROR("unary_expr", "Invalid l-value."); 37 | 38 | switch (a->v1._unary_operator->type) { 39 | case _ADDRESS_OF: 40 | /* simply do it as an lval */ 41 | d = genbf_cast_expr(a->v2, 1, &tignore); 42 | if (d != -1) { 43 | ERROR("unary_expr", "Simple address-of operation is not supported."); 44 | } 45 | break; 46 | 47 | case _LOGIC_NOT: 48 | genbf_cast_expr(a->v2, 0, NULL); 49 | printf("<+>[<->[-]]<[>+<-]>"); 50 | fflush(stdout); 51 | break; 52 | 53 | default: 54 | UNIMPL("unary_operator"); 55 | } 56 | break; 57 | 58 | /* case _INC: 59 | SPC; printf("++\n"); 60 | genbf_unary_expr(a->v1._unary_expr); 61 | break; 62 | 63 | case _DEC: 64 | SPC; printf("--\n"); 65 | genbf_unary_expr(a->v1._unary_expr); 66 | break; 67 | 68 | case _UNARY_OP: 69 | genbf_unary_operator(a->v1._unary_operator); 70 | genbf_cast_expr(a->v2); 71 | break; 72 | 73 | case _SIZEOF_EXPR: 74 | SPC; printf("sizeof\n"); 75 | genbf_unary_expr(a->v1._unary_expr); 76 | break; 77 | 78 | case _SIZEOF_TYPE: 79 | SPC; printf("sizeof\n"); 80 | genbf_type_name(a->v1._type_name); 81 | break; */ 82 | 83 | default: 84 | UNIMPL("unary_expr"); 85 | } 86 | } 87 | 88 | char *genbf_unary_expr_get_primary(int type, struct unary_expr *a) 89 | { 90 | if (a->type != _POSTFIX_EXPR) return NULL; 91 | return genbf_postfix_expr_get_primary(type, a->v1._postfix_expr); 92 | } 93 | -------------------------------------------------------------------------------- /1tox/c2bf-1to4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | 23 | int main() 24 | { 25 | int c; 26 | 27 | printf(">"); 28 | 29 | while ((c = getchar()) != EOF) { 30 | switch (c) { 31 | case '>': 32 | printf(">>>>>>"); 33 | break; 34 | 35 | case '<': 36 | printf("<<<<<<"); 37 | break; 38 | 39 | case ',': 40 | printf(","); 41 | break; 42 | 43 | case '.': 44 | printf("."); 45 | break; 46 | 47 | case '+': 48 | printf("+[<+>>>>>+<<<<-]<[>+<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>" 49 | "+[<<+>>>>>+<<<-]<<[>>+<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[" 50 | "->>>+[<<<+>>>>>+<<-]<<<[>>>+<<<-]+>>>>>[<<<<<->>>>>[-]" 51 | "]<<<<<[->>>>+<<<<]]]>"); 52 | break; 53 | 54 | case '-': 55 | printf("[<+>>>>>+<<<<-]<[>+<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->>[" 56 | "<<+>>>>>+<<<-]<<[>>+<<-]+>>>>>[<<<<<->>>>>[-]]<<<<<[->" 57 | ">>[<<<+>>>>>+<<-]<<<[>>>+<<<-]+>>>>>[<<<<<->>>>>[-]]<<" 58 | "<<<[->>>>-<<<<]>>>-<<<]>>-<<]>-"); 59 | break; 60 | 61 | case '[': 62 | printf("[>>>>+>>>>>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]" 63 | "<<<<<[[-]<<<<<+>>>>>]<<<[>>>+>>>>>+<<<<<<<<-]>>>>>>>>[" 64 | "<<<<<<<<+>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<[>>+>>>>>+<<" 65 | "<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<" 66 | "[>+>>>>>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<<[[-]<<<<<+" 67 | ">>>>>]<<<<<[[-]>"); 68 | break; 69 | 70 | case ']': 71 | printf("[>>>>+>>>>>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]" 72 | "<<<<<[[-]<<<<<+>>>>>]<<<[>>>+>>>>>+<<<<<<<<-]>>>>>>>>[" 73 | "<<<<<<<<+>>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<<[>>+>>>>>+<<" 74 | "<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<<<<<[[-]<<<<<+>>>>>]<" 75 | "[>+>>>>>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<<[[-]<<<<<+" 76 | ">>>>>]<<<<<]>"); 77 | break; 78 | 79 | case '#': 80 | printf("#"); 81 | break; 82 | } 83 | } 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /cc/genbf/multiplicative_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_multiplicative_expr(struct multiplicative_expr *a, int lval, struct type **t) 25 | { 26 | switch (a->type) { 27 | case _CAST_EXPR: 28 | return genbf_cast_expr(a->v1._cast_expr, lval, t); 29 | break; 30 | 31 | case _MULTIPLY: 32 | if (lval) 33 | ERROR("multiplicative_expr", "Invalid l-value."); 34 | /* call the builtin function */ 35 | pushCall("__builtin_mul"); 36 | genbf_multiplicative_expr(a->v1._multiplicative_expr, 0, NULL); 37 | genbf_cast_expr(a->v2, 0, NULL); 38 | ignoreVar(); ignoreVar(); /* these are for the next function */ 39 | outBlock(); 40 | break; 41 | 42 | case _DIVIDE: 43 | if (lval) 44 | ERROR("multiplicative_expr", "Invalid l-value."); 45 | /* call the builtin function */ 46 | pushCall("__builtin_div"); 47 | genbf_multiplicative_expr(a->v1._multiplicative_expr, 0, NULL); 48 | genbf_cast_expr(a->v2, 0, NULL); 49 | ignoreVar(); ignoreVar(); /* these are for the next function */ 50 | outBlock(); 51 | break; 52 | 53 | case _MODULO: 54 | if (lval) 55 | ERROR("multiplicative_expr", "Invalid l-value."); 56 | /* call the builtin function */ 57 | pushCall("__builtin_mod"); 58 | genbf_multiplicative_expr(a->v1._multiplicative_expr, 0, NULL); 59 | genbf_cast_expr(a->v2, 0, NULL); 60 | ignoreVar(); ignoreVar(); /* these are for the next function */ 61 | outBlock(); 62 | break; 63 | 64 | /* case _MULTIPLY: 65 | genbf_multiplicative_expr(a->v1._multiplicative_expr); 66 | SPC; printf("*\n"); 67 | genbf_cast_expr(a->v2); 68 | break; 69 | 70 | case _DIVIDE: 71 | genbf_multiplicative_expr(a->v1._multiplicative_expr); 72 | SPC; printf("/\n"); 73 | genbf_cast_expr(a->v2); 74 | break; 75 | 76 | case _MODULO: 77 | genbf_multiplicative_expr(a->v1._multiplicative_expr); 78 | SPC; printf("%\n"); 79 | genbf_cast_expr(a->v2); 80 | break; */ 81 | 82 | default: 83 | UNIMPL("multiplicative_expr"); 84 | } 85 | } 86 | 87 | char *genbf_multiplicative_expr_get_primary(int type, struct multiplicative_expr *a) 88 | { 89 | if (a->type != _CAST_EXPR) return NULL; 90 | return genbf_cast_expr_get_primary(type, a->v1._cast_expr); 91 | } 92 | -------------------------------------------------------------------------------- /cc/genbf/relational_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_relational_expr(struct relational_expr *a, int lval, struct type **t) 25 | { 26 | if (a->type == _SHIFT_EXPR) { 27 | return genbf_shift_expr(a->v1._shift_expr, lval, t); 28 | } 29 | 30 | /* relational expressions can't be lvals */ 31 | if (lval) 32 | ERROR("relational_expr", "Invalid l-value."); 33 | 34 | /* push the two operands */ 35 | genbf_relational_expr(a->v1._relational_expr, 0, NULL); 36 | genbf_shift_expr(a->v2, 0, NULL); 37 | 38 | /* and do the comparison */ 39 | switch (a->type) { 40 | case _LESS: 41 | /* less is just a <= (b - 1) */ 42 | printf("-"); 43 | case _LE: 44 | /* FIXME: THIS IS THE ONLY TESTED RELATIONAL EXPRESSION! */ 45 | /* layout: 46 | O1 . . points temp *O2 . . temp temp . . . temp temp 47 | A point is given if O1 == O2 or if O1 < O2 */ 48 | printf("[>>>+>+<<<<-]>>>[<<<+>>>-]<<<" 49 | /* O1 . . points 0 *O2 . . 0 O2 */ 50 | "<<<<<[>>>>+>>>>+<<<<<<<<-]>>>>[<<<<+>>>>-]>>>>>" 51 | /* O1 . . points 0 O2 . . O1 *O2 */ 52 | "+<+>" 53 | /* O1 . . points 0 O2 . . O1+1 *O2+1 */ 54 | "[-<-[>>>>>+>+<<<<<<-]>>>>>>[<<<<<<+>>>>>>-]<<<<<<" 55 | "<<<<+>>>>[[-]<<<<->>>>]>>>>>[<<<<<+>>>>>-]<<<<<" 56 | "<<<<[<+>-]>>>>>]" 57 | /* O1 . . points 0 O2 . . ? *0 */ 58 | "<[-]<<<<<" 59 | "<<<[-]>>>[<<<+>>>-]>>"); 60 | break; 61 | 62 | case _GREATER: 63 | /* greater is just a >= (b + 1) */ 64 | printf("+"); 65 | case _GE: 66 | printf("[>>>+>+<<<<-]>>>[<<<+>>>-]<<<" 67 | /* O1 . . points 0 *O2 . . 0 O2 */ 68 | "<<<<<[>>>>+>>>>+<<<<<<<<-]>>>>[<<<<+>>>>-]>>>>" 69 | /* O1 . . points 0 O2 . . *O1 O2 */ 70 | "+>+<" 71 | /* O1 . . points 0 O2 . . *O1+1 O2+1 */ 72 | "[->-[>>>>+>+<<<<<-]>>>>>[<<<<<+>>>>>-]<<<<<" 73 | "<<<<<+>>>>>[[-]<<<<<->>>>>]>>>>[<<<<+>>>>-]<<<<" 74 | "<<<<<[<+>-]>>>>]" 75 | /* O1 . . points 0 O2 . . ? *0 */ 76 | ">[-]<<<<<<" 77 | "<<<[-]>>>[<<<+>>>-]>>"); 78 | break; 79 | } 80 | 81 | popVar(); 82 | fflush(stdout); 83 | } 84 | 85 | char *genbf_relational_expr_get_primary(int type, struct relational_expr *a) 86 | { 87 | if (a->type != _SHIFT_EXPR) return NULL; 88 | return genbf_shift_expr_get_primary(type, a->v1._shift_expr); 89 | } 90 | -------------------------------------------------------------------------------- /cc/genbf/selection_statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | 23 | #include "../genbf.h" 24 | #include "generator.h" 25 | 26 | void genbf_selection_statement(struct selection_statement *a) 27 | { 28 | char *nname, *pblockname; 29 | int pblocknum; 30 | 31 | switch (a->type) { 32 | case _IF: 33 | case _IF_ELSE: 34 | genbf_expr(a->v1, 0, NULL); 35 | 36 | /* this will use a sneaky "subblock" format to make the jump-back 37 | * location predictable. basically: 38 | * main: 39 | * if (blah) { 40 | * main!0!1 41 | * } else { 42 | * main!1!1 43 | * } 44 | * main!2 45 | */ 46 | 47 | /* get an "if-not" as well */ 48 | pblockname = curblock->name; 49 | pblocknum = curblock->num; 50 | printf("[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+" 51 | "<[[-]>-<<<<(%s!%d)>>>]" 52 | ">[-<<<<(%s!%d)>>>>]" 53 | "<<<<", 54 | pblockname, pblocknum + 1, 55 | pblockname, pblocknum + 2); 56 | 57 | popVar(); 58 | 59 | /* go on to the if-block */ 60 | pushSubBlock(0); 61 | outBlock(); 62 | 63 | genbf_statement(a->v2); 64 | 65 | /* this needs to continue to the proper place */ 66 | if (a->type == _IF) { 67 | printf("(%s!%d)", pblockname, pblocknum + 2); 68 | } else { 69 | printf("(%s!%d)", pblockname, pblocknum + 3); 70 | 71 | /* this is an if/else, so now we need yet another subblock */ 72 | popNamedBlock(); 73 | pushSubBlock(1); 74 | outBlock(); 75 | 76 | genbf_statement(a->v3); 77 | 78 | printf("(%s!%d)", pblockname, pblocknum + 3); 79 | } 80 | 81 | /* finally continue with our regularly scheduled programming */ 82 | popNamedBlock(); 83 | pushBlock(); 84 | if (a->type == _IF) { 85 | curblock->num += 1; 86 | } else { 87 | curblock->num += 2; 88 | } 89 | outBlock(); 90 | 91 | break; 92 | 93 | case _SWITCH: 94 | UNIMPL("selection_statement"); 95 | /* SPC; printf("switch (\n"); 96 | genbf_expr(a->v1); 97 | SPC; printf(")\n"); 98 | genbf_statement(a->v2); */ 99 | break; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /cc/genbf/jump_statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_jump_statement(struct jump_statement *a) 25 | { 26 | int i, bd; 27 | struct var *cur; 28 | 29 | switch (a->type) { 30 | case _RETURN: 31 | /* FIXME: if there's anything after this, it's an error, but 32 | * there's no way to verify at present */ 33 | if (a->has_expr) { 34 | genbf_expr(a->v._expr, 0, NULL); 35 | /* this expression is the return code, so put it in place */ 36 | /* FIXME: this ignores the possibility of multi-cell data types */ 37 | bd = blockDepth() - 1; 38 | 39 | if (bd != 0) { 40 | for (i = 0; i < bd; i++) 41 | printf("<<<<<"); 42 | printf("[-]"); 43 | for (i = 0; i < bd; i++) 44 | printf(">>>>>"); 45 | printf("["); 46 | for (i = 0; i < bd; i++) 47 | printf("<<<<<"); 48 | printf("+"); 49 | for (i = 0; i < bd; i++) 50 | printf(">>>>>"); 51 | printf("-]"); 52 | } 53 | 54 | popVar(); 55 | /* pop off the rest of the variables */ 56 | for (cur = curvar; cur; cur = cur->next) 57 | for (i = 0; i < cur->width; i++) 58 | BF_POP; 59 | 60 | /* now the stack should look like this: 61 | * {return address}{return code} 62 | * but the return code is actually above the top of the stack 63 | * so, move the return address into the walk cell, and use it to set the 64 | * return */ 65 | printf("[<<+>>-]>>>>>[<<<<<+>>>>>-]" 66 | "<<<<<<<<[>[<<<<<+>>>>>-]<<<<<<]" 67 | ">[>>>>>+<<<<<-]>>>>>" 68 | "[[>>>>>+<<<<<-]>>>>>-]" 69 | "<<<+" 70 | ">>[>>>>>]<v._identifier); 78 | SPC; printf(";\n"); 79 | break; 80 | 81 | case _CONTINUE: 82 | SPC; printf("continue;\n"); 83 | break; 84 | 85 | case _BREAK: 86 | SPC; printf("break;\n"); 87 | break; 88 | 89 | case _RETURN: 90 | SPC; printf("return\n"); 91 | if (a->has_expr) { 92 | genbf_expr(a->v._expr); 93 | } 94 | SPC; printf(";\n"); 95 | break; */ 96 | 97 | default: 98 | UNIMPL("jump_statement"); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /cc/genbf/iteration_statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_iteration_statement(struct iteration_statement *a) 25 | { 26 | char *pblockname; 27 | int pblocknum; 28 | 29 | if (a->type == _DO_WHILE) { 30 | UNIMPL("iteration_statement"); 31 | exit(1); 32 | } 33 | 34 | /* initializer if applicable */ 35 | if (a->type == _FOR && a->has_initializer) { 36 | /* just run this now */ 37 | genbf_expr(a->v1, 0, NULL); 38 | popVar(); 39 | } 40 | 41 | pblockname = curblock->name; 42 | pblocknum = curblock->num; 43 | 44 | /* now, the condition (1st time) */ 45 | if (a->type != _FOR || a->has_condition) { 46 | if (a->type == _FOR) { 47 | genbf_expr(a->v2, 0, NULL); 48 | } else { 49 | genbf_expr(a->v1, 0, NULL); 50 | } 51 | } else { 52 | struct type *t; 53 | 54 | /* just invent one */ 55 | pushTempVar(1); 56 | NEW(t, struct type); 57 | t->next = NULL; 58 | t->basic_type = TYPE_INT; 59 | t->array = 0; 60 | t->size = 1; 61 | BF_PUSH; 62 | printf("[-]+"); 63 | 64 | curvar->type = t; 65 | } 66 | 67 | /* get an "if-not" as well */ 68 | printf("[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+" 69 | "<[[-]>-<<<<(%s!%d)>>>]" 70 | ">[-<<<<(%s!%d)>>>>]" 71 | "<<<<", 72 | pblockname, pblocknum + 1, 73 | pblockname, pblocknum + 2); 74 | 75 | popVar(); 76 | 77 | /* now push on the for's subblock */ 78 | pushSubBlock(0); 79 | outBlock(); 80 | 81 | /* and the for's content */ 82 | genbf_statement(a->v4); 83 | 84 | /* this follows the other content */ 85 | if (a->type == _FOR && a->has_post) { 86 | genbf_expr(a->v3, 0, NULL); 87 | popVar(); 88 | } 89 | 90 | /* now, the condition (2nd time) */ 91 | if (a->type != _FOR || a->has_condition) { 92 | if (a->type == _FOR) { 93 | genbf_expr(a->v2, 0, NULL); 94 | } else { 95 | genbf_expr(a->v1, 0, NULL); 96 | } 97 | } else { 98 | struct type *t; 99 | 100 | /* just invent one */ 101 | pushTempVar(1); 102 | NEW(t, struct type); 103 | t->next = NULL; 104 | t->basic_type = TYPE_INT; 105 | t->array = 0; 106 | t->size = 1; 107 | BF_PUSH; 108 | printf("[-]+"); 109 | 110 | curvar->type = t; 111 | } 112 | 113 | /* get an "if-not" as well */ 114 | printf("[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+" 115 | "<[[-]>-<<<<(%s!%d)>>>]" 116 | ">[-<<<<(%s!%d)>>>>]" 117 | "<<<<", 118 | pblockname, pblocknum + 1, 119 | pblockname, pblocknum + 2); 120 | 121 | popVar(); 122 | 123 | /* pop off the for's block */ 124 | popNamedBlock(); 125 | pushBlock(); 126 | curblock->num += 1; 127 | outBlock(); 128 | 129 | /* now we're back to normal code */ 130 | fflush(stdout); 131 | } 132 | -------------------------------------------------------------------------------- /cc/scan.l: -------------------------------------------------------------------------------- 1 | D [0-9] 2 | L [a-zA-Z_] 3 | H [a-fA-F0-9] 4 | E [Ee][+-]?{D}+ 5 | FS (f|F|l|L) 6 | IS (u|U|l|L)* 7 | 8 | %{ 9 | #include 10 | #include "y.tab.h" 11 | %} 12 | 13 | %% 14 | "/*" { comment(); } 15 | 16 | "auto" { return(AUTO); } 17 | "break" { return(BREAK); } 18 | "case" { return(CASE); } 19 | "char" { return(CHAR); } 20 | "const" { return(CONST); } 21 | "continue" { return(CONTINUE); } 22 | "default" { return(DEFAULT); } 23 | "do" { return(DO); } 24 | "double" { return(DOUBLE); } 25 | "else" { return(ELSE); } 26 | "enum" { return(ENUM); } 27 | "extern" { return(EXTERN); } 28 | "float" { return(FLOAT); } 29 | "for" { return(FOR); } 30 | "goto" { return(GOTO); } 31 | "if" { return(IF); } 32 | "int" { return(INT); } 33 | "long" { return(LONG); } 34 | "register" { return(REGISTER); } 35 | "return" { return(RETURN); } 36 | "short" { return(SHORT); } 37 | "signed" { return(SIGNED); } 38 | "sizeof" { return(SIZEOF); } 39 | "static" { return(STATIC); } 40 | "struct" { return(STRUCT); } 41 | "switch" { return(SWITCH); } 42 | "typedef" { return(TYPEDEF); } 43 | "union" { return(UNION); } 44 | "unsigned" { return(UNSIGNED); } 45 | "void" { return(VOID); } 46 | "volatile" { return(VOLATILE); } 47 | "while" { return(WHILE); } 48 | 49 | {L}({L}|{D})* { yylval.string = strdup(yytext); return(IDENTIFIER); } 50 | 51 | 0[xX]{H}+{IS}? { yylval.string = strdup(yytext); return(CONSTANT); } 52 | 0[xX]{H}+{IS}? { yylval.string = strdup(yytext); return(CONSTANT); } 53 | 0{D}+{IS}? { yylval.string = strdup(yytext); return(CONSTANT); } 54 | 0{D}+{IS}? { yylval.string = strdup(yytext); return(CONSTANT); } 55 | {D}+{IS}? { yylval.string = strdup(yytext); return(CONSTANT); } 56 | {D}+{IS}? { yylval.string = strdup(yytext); return(CONSTANT); } 57 | '(\\.|[^\\'])+' { yylval.string = strdup(yytext); return(CONSTANT); } 58 | 59 | {D}+{E}{FS}? { yylval.string = strdup(yytext); return(CONSTANT); } 60 | {D}*"."{D}+({E})?{FS}? { yylval.string = strdup(yytext); return(CONSTANT); } 61 | {D}+"."{D}*({E})?{FS}? { yylval.string = strdup(yytext); return(CONSTANT); } 62 | 63 | \"(\\.|[^\\"])*\" { yylval.string = strdup(yytext); return(STRING_LITERAL); } 64 | 65 | ">>=" { return(RIGHT_ASSIGN); } 66 | "<<=" { return(LEFT_ASSIGN); } 67 | "+=" { return(ADD_ASSIGN); } 68 | "-=" { return(SUB_ASSIGN); } 69 | "*=" { return(MUL_ASSIGN); } 70 | "/=" { return(DIV_ASSIGN); } 71 | "%=" { return(MOD_ASSIGN); } 72 | "&=" { return(AND_ASSIGN); } 73 | "^=" { return(XOR_ASSIGN); } 74 | "|=" { return(OR_ASSIGN); } 75 | ">>" { return(RIGHT_OP); } 76 | "<<" { return(LEFT_OP); } 77 | "++" { return(INC_OP); } 78 | "--" { return(DEC_OP); } 79 | "->" { return(PTR_OP); } 80 | "&&" { return(AND_OP); } 81 | "||" { return(OR_OP); } 82 | "<=" { return(LE_OP); } 83 | ">=" { return(GE_OP); } 84 | "==" { return(EQ_OP); } 85 | "!=" { return(NE_OP); } 86 | ";" { return(';'); } 87 | "{" { return('{'); } 88 | "}" { return('}'); } 89 | "," { return(','); } 90 | ":" { return(':'); } 91 | "=" { return('='); } 92 | "(" { return('('); } 93 | ")" { return(')'); } 94 | "[" { return('['); } 95 | "]" { return(']'); } 96 | "." { return('.'); } 97 | "&" { return('&'); } 98 | "!" { return('!'); } 99 | "~" { return('~'); } 100 | "-" { return('-'); } 101 | "+" { return('+'); } 102 | "*" { return('*'); } 103 | "/" { return('/'); } 104 | "%" { return('%'); } 105 | "<" { return('<'); } 106 | ">" { return('>'); } 107 | "^" { return('^'); } 108 | "|" { return('|'); } 109 | "?" { return('?'); } 110 | 111 | [ \t\v\n\f] { /* do nothing */ } 112 | . { /* ignore bad characters */ } 113 | 114 | %% 115 | 116 | yywrap() 117 | { 118 | return(1); 119 | } 120 | 121 | /* for flex */ 122 | #ifndef yytext_ptr 123 | #define yytext_ptr yytext 124 | #endif 125 | 126 | comment() 127 | { 128 | char c, c1; 129 | 130 | loop: 131 | while ((c = input()) != '*' && c != 0); 132 | /*putchar(c);*/ 133 | 134 | if ((c1 = input()) != '/' && c != 0) 135 | { 136 | unput(c1); 137 | goto loop; 138 | } 139 | 140 | /*if (c != 0) 141 | putchar(c1);*/ 142 | } 143 | 144 | int column = 0; 145 | -------------------------------------------------------------------------------- /cc/genbf/assignment_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_assignment_expr(struct assignment_expr *a, int lval, struct type **t) 25 | { 26 | int i, d; 27 | struct type *st; 28 | 29 | switch (a->type) { 30 | case _CONDITIONAL_EXPR: 31 | return genbf_conditional_expr(a->v1._conditional_expr, lval, t); 32 | break; 33 | 34 | case _ASSIGNMENT: 35 | if (lval) 36 | ERROR("assignment_expr", "Invalid l-value."); 37 | 38 | /* get the location as an lval (this will return the depth of the 39 | * variable in the stack, or -1 and push a pointer to the stack) */ 40 | d = genbf_unary_expr(a->v1._unary_expr, 1, &st); 41 | if (d == -1) { 42 | struct type *ttype; 43 | 44 | /* generate the value and put it in the carry */ 45 | genbf_assignment_expr(a->v3, 0, NULL); 46 | printf("[<+<+>>-]<<[>>+<<-]>>"); 47 | 48 | /* then swap-n-pop (essentially, we're leaving the value on 49 | top, even though there's one below it) */ 50 | ttype = curvar->type; 51 | curvar->type = curvar->next->type; 52 | curvar->next->type = ttype; 53 | popVar(); 54 | 55 | /* put this var in walk */ 56 | printf("[>>>+<<<-]"); 57 | 58 | /* get to the proper location */ 59 | STACK_PTR_TO_POS_CARRY; 60 | 61 | /* set it to the carry */ 62 | printf("[-]>>>>[<<<<+>>>>-]" 63 | "<<[>>>>>]<<"); 64 | 65 | /* then move the value into this slot to pass down */ 66 | fflush(stdout); 67 | return 0; 68 | } 69 | 70 | /* FIXME: only primitive (=) assignments :) */ 71 | if (a->v2->type != _ASSIGN) 72 | ERROR("assignment_expr", "Only primitive assignments are supported thusfar."); 73 | 74 | genbf_assignment_expr(a->v3, 0, NULL); 75 | /* this put one more on the stack, so our depth is greater */ 76 | d += curvar->width; 77 | 78 | /* carry this value ... */ 79 | printf("[>>>+>+<<<<-]>>>[<<<+>>>-]>"); 80 | 81 | /* the proper distance */ 82 | printf("["); 83 | for (i = 0; i < d; i++) 84 | printf("<<<<<"); 85 | printf("+"); 86 | for (i = 0; i < d; i++) 87 | printf(">>>>>"); 88 | printf("-]"); 89 | for (i = 0; i < d; i++) 90 | printf("<<<<<"); 91 | /* then put it in place */ 92 | printf("<<<<[-]>>>>[<<<<+>>>>-]<<<<"); 93 | /* and come back */ 94 | for (i = 0; i < d; i++) 95 | printf(">>>>>"); 96 | 97 | fflush(stdout); 98 | 99 | /* leave the value of the expression to pass up */ 100 | break; 101 | } 102 | } 103 | 104 | char *genbf_assignment_expr_get_primary(int type, struct assignment_expr *a) 105 | { 106 | if (a->type != _CONDITIONAL_EXPR) return NULL; 107 | return genbf_conditional_expr_get_primary(type, a->v1._conditional_expr); 108 | } 109 | -------------------------------------------------------------------------------- /cc/genbf/function_definition.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | void genbf_function_definition(struct function_definition *a) 25 | { 26 | struct declarator2 *decla, *declb; 27 | 28 | /* FIXME: this should store a list of declared functions and refer to that 29 | * for function calls */ 30 | 31 | /* the tree here should look like this for a standard definition: 32 | -function_definition 33 | |-declarator (v2) (guaranteed) 34 | || 35 | ||-declarator2 (v2) (guaranteed) 36 | |||(type must be _SIMPLE_FUNC or _FUNC_BY_TYPE) 37 | ||| 38 | |||-declarator2 (v1._declarator2) 39 | ||||(type must be _IDENTIFIER) 40 | |||| 41 | ||||-identifier (v1._identifier) 42 | |||||v is the function name 43 | ||| 44 | |||-parameter_type_list (v2._parameter_type_list) (_FUNC_BY_TYPE) 45 | ||||these must be pushed to stack 46 | | 47 | |-function_body (v3) 48 | ||(has_declaration_list must be 0) 49 | || 50 | ||-compound_statement (v2) 51 | 52 | or like this for an olde-style definition (not yet supported): 53 | -function_definition 54 | |-declarator (v2) (guaranteed) 55 | || 56 | ||-declarator2 (v2) (guaranteed) 57 | |||(type must be _FUNC_OLD_STYLE) 58 | ||| 59 | |||-declarator2 (v1._declarator2) 60 | ||||(type must be _IDENTIFIER) 61 | |||| 62 | ||||-identifier (v1._identifier) 63 | |||||v is the function name 64 | ||| 65 | |||-parameter_identifier_list (v2._parameter_identifier_list) (_FUNC_OLD_STYLE) 66 | ||||these must correspond to the declaration_list below 67 | | 68 | |-function_body (v3) 69 | ||(has_declaration_list must be 1) 70 | || 71 | ||-declaration_list (v1) 72 | |||these must be pushed onto the stack 73 | || 74 | ||-compound_statement (v2) 75 | */ 76 | 77 | decla = a->v2->v2; 78 | /* decla must have type _SIMPLE_FUNC or _FUNC_BY_TYPE */ 79 | if (decla->type != _SIMPLE_FUNC && decla->type != _FUNC_BY_TYPE) 80 | ERROR("function_definition", "Invalid function definition!\n"); 81 | 82 | declb = decla->v1._declarator2; 83 | /* declb must have type _IDENTIFIER */ 84 | if (declb->type != _IDENTIFIER) 85 | ERROR("function_definition", "Invalid function definition!\n"); 86 | 87 | /* now we can start the block */ 88 | pushNamedBlock(declb->v1._identifier->v); 89 | outBlock(); 90 | 91 | /* and push all the parameter_identifier_list variables on */ 92 | if (decla->type != _SIMPLE_FUNC) 93 | genbf_parameter_type_list(decla->v2._parameter_type_list); 94 | 95 | if (a->v3->has_declaration_list) 96 | ERROR("function_definition", "Invalid function definition!\n"); 97 | 98 | /* FIXME: this should transmit down in some way whether the function needs 99 | * to return */ 100 | genbf_compound_statement(a->v3->v2); 101 | 102 | /* now we need to pop the block */ 103 | popNamedBlock(); 104 | 105 | /* now the stack should look like this: 106 | * {return address}{return code} 107 | * but the return code is actually above the top of the stack 108 | * so, move the return address into the walk cell, and use it to set the 109 | * return */ 110 | printf("[<<+>>-]>>>>>[<<<<<+>>>>>-]" 111 | "<<<<<<<<[>[<<<<<+>>>>>-]<<<<<<]" 112 | ">[>>>>>+<<<<<-]>>>>>" 113 | "[[>>>>>+<<<<<-]>>>>>-]" 114 | "<<<+" 115 | ">>[>>>>>]<<"); 116 | fflush(stdout); 117 | } 118 | -------------------------------------------------------------------------------- /cc/genbf/postfix_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "../genbf.h" 22 | #include "generator.h" 23 | 24 | int genbf_postfix_expr(struct postfix_expr *a, int lval, struct type **t) 25 | { 26 | struct primary_expr *p_e; 27 | struct type *vt; 28 | char *f; 29 | int i, loc; 30 | 31 | switch (a->type) { 32 | case _PRIMARY_EXPR: 33 | return genbf_primary_expr(a->v1._primary_expr, lval, t); 34 | break; 35 | 36 | case _SIMPLE_FCALL: 37 | case _FCALL: 38 | if (lval) 39 | ERROR("postfix_expr", "Invalid l-value."); 40 | 41 | /* for builtins, the tree should look like this: 42 | -postfix_expr 43 | | 44 | |-postfix_expr (v1._postfix_expr) 45 | ||(type must be _PRIMARY_EXPR) 46 | || 47 | ||-primary_expr (v1._primary_expr) 48 | |||(type must be _IDENTIFIER) 49 | ||| 50 | |||-identifier (v1._identifier) 51 | ||||v is the name of the function to call 52 | | 53 | |-argument_expr_list (v2._argument_expr_list) (_FCALL) 54 | ||(this contains the arguments) 55 | */ 56 | if (a->v1._postfix_expr->type != _PRIMARY_EXPR) 57 | ERROR("postfix_expr", "Invalid function call!"); 58 | 59 | p_e = a->v1._postfix_expr->v1._primary_expr; 60 | if (p_e->type != _IDENTIFIER) 61 | ERROR("postfix_expr", "Invalid function call!"); 62 | 63 | f = p_e->v._identifier->v; 64 | 65 | #include "builtin_functions.c" 66 | else { 67 | int topop = 0; 68 | 69 | /* this is a real function call */ 70 | pushCall(f); 71 | if (a->type == _FCALL) { 72 | topop = genbf_argument_expr_list(a->v2._argument_expr_list); 73 | for (; topop > 0; topop--) 74 | ignoreVar(); /* these aren't ours */ 75 | } 76 | outBlock(); 77 | } 78 | break; 79 | 80 | case _ARRAY_REF: 81 | /* get the location of the array being referenced */ 82 | genbf_postfix_expr(a->v1._postfix_expr, 0, NULL); 83 | 84 | /* the returned type will be one farther in vt 85 | * FIXME: this should verify that this is indeed an array*/ 86 | if (lval) 87 | *t = curvar->type->next; 88 | 89 | genbf_expr(a->v2._expr, 0, NULL); 90 | 91 | /* add the new location */ 92 | printf("[<<<<<"); 93 | for (i = 0; i < curvar->type->size; i++) 94 | printf("++"); 95 | printf(">>>>>-]"); 96 | 97 | /* pop off this location */ 98 | popVar(); 99 | 100 | if (lval) { 101 | /* it's already on top, we're done */ 102 | return -1; 103 | } 104 | 105 | /* deref */ 106 | /* FIXME: this should do size checking */ 107 | vt = curvar->type; 108 | curvar->type = curvar->type->next; 109 | free(vt); 110 | 111 | /* get there */ 112 | printf("[>>>+<<<-]"); 113 | STACK_PTR_TO_POS; 114 | 115 | /* and move the value up */ 116 | printf("[>>>+>+<<<<-]" /* copied into W and C */ 117 | ">>>>[<<<<+>>>>-]" /* now in S and W */ 118 | "<<[>[>>>>>+<<<<<-]>>>>]" /* walk it over */ 119 | ">[<<<+>>>-]<<<" /* and drop it in */ 120 | ); 121 | break; 122 | 123 | /* case _DOT: 124 | genbf_postfix_expr(a->v1._postfix_expr); 125 | SPC; printf(".\n"); 126 | genbf_identifier(a->v2._identifier); 127 | break; 128 | 129 | case _PTR_OP: 130 | genbf_postfix_expr(a->v1._postfix_expr); 131 | SPC; printf("->\n"); 132 | genbf_identifier(a->v2._identifier); 133 | break; 134 | 135 | case _INC: 136 | genbf_postfix_expr(a->v1._postfix_expr); 137 | SPC; printf("++\n"); 138 | break; 139 | 140 | case _DEC: 141 | genbf_postfix_expr(a->v1._postfix_expr); 142 | SPC; printf("--\n"); 143 | break; */ 144 | 145 | default: 146 | UNIMPL("postfix_expr"); 147 | } 148 | } 149 | 150 | char *genbf_postfix_expr_get_primary(int type, struct postfix_expr *a) 151 | { 152 | if (a->type != _PRIMARY_EXPR) return NULL; 153 | return genbf_primary_expr_get_primary(type, a->v1._primary_expr); 154 | } 155 | -------------------------------------------------------------------------------- /ld/staticize.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "ld.h" 26 | #include "staticize.h" 27 | 28 | void staticizeBlock(struct block *tostat) 29 | { 30 | char *newcode, *code; 31 | int codelen, num; 32 | int i, j, o; 33 | 34 | /* start with newcode the same size as code, and expand as necessary */ 35 | code = tostat->code; 36 | codelen = strlen(code); 37 | newcode = (char *) malloc(codelen + 1); 38 | if (!newcode) { perror("malloc"); exit(1); } 39 | 40 | /* then loop through the original code and associate */ 41 | o = 0; 42 | for (i = 0; code[i]; i++) { 43 | switch (code[i]) { 44 | case '(': 45 | /* find the end and null it */ 46 | for (j = i + 1; code[j] && code[j] != ')'; j++); 47 | if (!code[j]) { 48 | /* no ending ) ! */ 49 | fprintf(stderr, "Invalid reference %s" 50 | " at column %d" 51 | " in label %s\n", code + i, 52 | i, 53 | tostat->label); 54 | exit(1); 55 | } 56 | code[j] = '\0'; 57 | 58 | i++; 59 | /* now switch for * or no * */ 60 | switch (code[i]) { 61 | case '*': 62 | /* just put a pointer */ 63 | num = blockNum(code + i + 1, NULL); 64 | if (num == -1) { 65 | fprintf(stderr, "Undefined symbol: '%s'" 66 | " at column %d" 67 | " in label %s\n", code + i + 1, 68 | i + 1, 69 | tostat->label); 70 | exit(1); 71 | } 72 | 73 | /* FIXME: some better number-generation code is in order */ 74 | codelen += num + 3; 75 | newcode = (char *) realloc(newcode, codelen); 76 | if (!newcode) { perror("realloc"); exit(1); } 77 | 78 | /* zero the cell */ 79 | sprintf(newcode + o, "[-]"); 80 | o += 3; 81 | 82 | /* then just + a bunch */ 83 | for (; num > 0; num--) { 84 | newcode[o] = '+'; 85 | o++; 86 | } 87 | break; 88 | 89 | default: 90 | /* more difficult: go to the proper location and set 91 | * a 1 */ 92 | num = blockNum(code + i, NULL); 93 | if (num == -1) { 94 | fprintf(stderr, "Undefined symbol: '%s'" 95 | " at column %d" 96 | " in label %s\n", code + i, 97 | i, 98 | tostat->label); 99 | exit(1); 100 | } 101 | 102 | codelen += 25 + (5 * num); 103 | newcode = (char *) realloc(newcode, codelen); 104 | if (!newcode) { perror("realloc"); exit(1); } 105 | 106 | /* step one: get back to the beginning of the stack */ 107 | sprintf(newcode + o, "<<<[<<<<<]>>>"); 108 | o += 13; 109 | 110 | /* step two: the right number of >s */ 111 | for (; num > 0; num--) { 112 | sprintf(newcode + o, ">>>>>"); 113 | o += 5; 114 | } 115 | 116 | /* step three: set it properly */ 117 | newcode[o] = '+'; 118 | o++; 119 | 120 | /* step four: get back */ 121 | sprintf(newcode + o, ">>[>>>>>]<<"); 122 | o += 11; 123 | } 124 | /* now we can continue */ 125 | i = j; 126 | break; 127 | 128 | case '!': 129 | /* this symbol means "ignore any further input for this block" */ 130 | for (; code[i]; i++); 131 | i--; 132 | break; 133 | 134 | default: 135 | newcode[o] = code[i]; 136 | o++; 137 | } 138 | } 139 | 140 | newcode[o] = '\0'; 141 | 142 | /* now newcode is in order, so put it in the struct */ 143 | tostat->code = newcode; 144 | } 145 | -------------------------------------------------------------------------------- /ld/ld.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include "ld.h" 26 | 27 | struct block *curblock = NULL; 28 | 29 | int main() 30 | { 31 | char *line; 32 | struct block *newblock, *cur; 33 | int i, osl, cbs, rs; 34 | int startblock; 35 | 36 | /* read in the blocks ... */ 37 | while (!feof(stdin) && !ferror(stdin)) { 38 | cbs = 2048; 39 | line = (char *) malloc(cbs + 1); 40 | if (!line) { perror("malloc"); exit(1); } 41 | line[cbs] = '\0'; 42 | 43 | /* read in the line */ 44 | if (!fgets(line, cbs, stdin)) { 45 | free(line); 46 | continue; 47 | } else { 48 | while (line[cbs - 2]) { 49 | /* expand the line */ 50 | line = (char *) realloc(line, (cbs * 2) + 1); 51 | if (!line) { perror("realloc"); exit(1); } 52 | line[cbs * 2] = '\0'; 53 | 54 | /* read more */ 55 | if (!fgets(line + cbs - 1, cbs + 1, stdin)) 56 | break; 57 | 58 | cbs *= 2; 59 | } 60 | } 61 | 62 | /* get rid of trailing newlines */ 63 | osl = strlen(line) - 1; 64 | while (line[osl] == '\n' || 65 | line[osl] == '\r') { 66 | line[osl] = '\0'; 67 | osl--; 68 | } 69 | 70 | /* make sure it's not blank */ 71 | if (!line[0]) { 72 | free(line); 73 | continue; 74 | } 75 | 76 | /* realloc conservatively */ 77 | osl++; 78 | line = (char *) realloc(line, osl); 79 | if (!line) { perror("realloc"); exit(1); } 80 | 81 | /* start the new block */ 82 | newblock = (struct block *) malloc(sizeof(struct block)); 83 | newblock->next = curblock; 84 | newblock->prev = NULL; 85 | if (curblock) curblock->prev = newblock; 86 | curblock = newblock; 87 | if (curblock->next) { 88 | curblock->num = curblock->next->num + 1; 89 | } else { 90 | curblock->num = 0; 91 | } 92 | curblock->used = 0; 93 | 94 | /* and divide in the string */ 95 | for (i = 0; line[i] && line[i] != ':'; i++); 96 | if (!line[i]) { 97 | /* no :! Bad line! */ 98 | fprintf(stderr, "Bad line: %s\n", line); 99 | free(line); 100 | continue; 101 | } 102 | line[i] = '\0'; 103 | i++; 104 | for (; line[i] && (line[i] == ':' || line[i] == ' '); i++); 105 | 106 | /* put it in curblock */ 107 | curblock->label = line; 108 | curblock->code = line + i; 109 | } 110 | 111 | if (!curblock) { 112 | /* what, no blocks at all?! */ 113 | return 0; 114 | } 115 | 116 | /* trace the blocks (to avoid writing out unused blocks) */ 117 | if ((startblock = blockNum("__start", &cur)) == -1) { 118 | fprintf(stderr, "No __start function (perhaps you forgot to include __start.o?)!\n"); 119 | exit(1); 120 | } 121 | cur->used = 1; 122 | traceBlock(cur); 123 | 124 | /* now that we've read in all the blocks, make the proper associations */ 125 | cur = curblock; 126 | while (cur) { 127 | staticizeBlock(cur); 128 | cur = cur->next; 129 | } 130 | 131 | /* start the labelling code */ 132 | printf(">>"); 133 | for (i = 0; i < curblock->num; i++) { 134 | printf("[>+>>>>+<<<<<-]>[<+>-]>>>>+"); 135 | } 136 | printf(">>>"); 137 | for (i = 0; i < curblock->num; i++) { 138 | printf("<<<<<"); 139 | } 140 | 141 | /* set the proper block active */ 142 | osl = startblock; 143 | 144 | /* get there ... */ 145 | for (; startblock > 0; startblock--) 146 | printf(">>>>>"); 147 | startblock = osl; 148 | 149 | /* set it ... */ 150 | printf("+"); 151 | 152 | /* get back ... */ 153 | for (; startblock > 0; startblock--) 154 | printf("<<<<<"); 155 | 156 | /* get our static 1 to loop off of */ 157 | printf("<<<<<+["); 158 | 159 | /* now, starting with block #0, put in the real magic */ 160 | cur = curblock; 161 | while (cur->next) cur = cur->next; 162 | 163 | while (cur) { 164 | if (cur->used) { 165 | /* if it's used, put in the code */ 166 | printf(">>>>>[->>[>>>>>]<<"); 167 | printf(cur->code); 168 | printf("<<<[<<<<<]>>>"); 169 | for (i = 0; i < cur->num; i++) { 170 | printf(">>>>>"); 171 | } 172 | printf("]"); 173 | } else { 174 | /* otherwise, just stub it */ 175 | printf(">>>>>"); 176 | } 177 | 178 | cur = cur->prev; 179 | } 180 | 181 | /* finally, end our magic loop */ 182 | printf("<<<[<<<<<]<<]"); 183 | 184 | return 0; 185 | } 186 | 187 | /* blockNum 188 | * input: the name of a symbol 189 | * output: its number (or -1 on error) 190 | * effect: none 191 | */ 192 | int blockNum(const char *label, struct block **into) 193 | { 194 | struct block *cur = curblock; 195 | 196 | while (cur) { 197 | if (!strcmp(cur->label, label)) { 198 | if (into) 199 | *into = cur; 200 | return cur->num; 201 | } 202 | cur = cur->next; 203 | } 204 | 205 | return -1; 206 | } 207 | -------------------------------------------------------------------------------- /cc/genbf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include "ast.h" 22 | #include "genbf/generator.h" 23 | 24 | /* _expr functions have a special syntax: 25 | * Input: The expression, a boolean for whether this is an lval, and (if it is 26 | * an lval), a location to put the appropriate struct type. 27 | * Output: If lval: 28 | * -1 if a pointer has been pushed onto the stack 29 | * >=0 is the depth of the variable in the stack 30 | * t is set 31 | * else: 32 | * irrelevent (usu. 0) 33 | */ 34 | 35 | void genbf_abstract_declarator(struct abstract_declarator *a); 36 | void genbf_abstract_declarator2(struct abstract_declarator2 *a); 37 | 38 | int genbf_additive_expr(struct additive_expr *a, int lval, struct type **t); 39 | char *genbf_additive_expr_get_primary(int type, struct additive_expr *a); 40 | 41 | int genbf_and_expr(struct and_expr *a, int lval, struct type **t); 42 | char *genbf_and_expr_get_primary(int type, struct and_expr *a); 43 | 44 | int genbf_argument_expr_list(struct argument_expr_list *a); 45 | 46 | int genbf_assignment_expr(struct assignment_expr *a, int lval, struct type **t); 47 | char *genbf_assignment_expr_get_primary(int type, struct assignment_expr *a); 48 | 49 | void genbf_assignment_operator(struct assignment_operator *a); 50 | 51 | int genbf_cast_expr(struct cast_expr *a, int lval, struct type **t); 52 | char *genbf_cast_expr_get_primary(int type, struct cast_expr *a); 53 | 54 | void genbf_compound_statement(struct compound_statement *a); 55 | 56 | int genbf_conditional_expr(struct conditional_expr *a, int lval, struct type **t); 57 | char *genbf_conditional_expr_get_primary(int type, struct conditional_expr *a); 58 | 59 | struct type *genbf_declarator(struct declarator *a, struct type *into); 60 | 61 | struct type *genbf_declarator2(struct declarator2 *a, struct type *into); 62 | char *genbf_declarator2_get_identifier(struct declarator2 *a); 63 | 64 | void genbf_declaration(struct declaration *a); 65 | void genbf_declaration_list(struct declaration_list *a); 66 | void genbf_declaration_specifiers(struct declaration_specifiers *a); 67 | void genbf_enum_specifier(struct enum_specifier *a); 68 | void genbf_enumerator(struct enumerator *a); 69 | void genbf_enumerator_list(struct enumerator_list *a); 70 | 71 | int genbf_equality_expr(struct equality_expr *a, int lval, struct type **t); 72 | char *genbf_equality_expr_get_primary(int type, struct equality_expr *a); 73 | 74 | int genbf_exclusive_or_expr(struct exclusive_or_expr *a, int lval, struct type **t); 75 | char *genbf_exclusive_or_expr_get_primary(int type, struct exclusive_or_expr *a); 76 | 77 | int genbf_expr(struct expr *a, int lval, struct type **t); 78 | void genbf_expression_statement(struct expression_statement *a); 79 | void genbf_external_definition(struct external_definition *a); 80 | void genbf_external_definition_list(struct external_definition_list *a); 81 | void genbf_file(struct file *a); 82 | void genbf_function_body(struct function_body *a); 83 | void genbf_function_definition(struct function_definition *a); 84 | void genbf_identifier(struct identifier *a); 85 | void genbf_identifier_list(struct identifier_list *a); 86 | 87 | int genbf_inclusive_or_expr(struct inclusive_or_expr *a, int lval, struct type **t); 88 | char *genbf_inclusive_or_expr_get_primary(int type, struct inclusive_or_expr *a); 89 | 90 | void genbf_init_declarator(struct init_declarator *a); 91 | void genbf_init_declarator_list(struct init_declarator_list *a); 92 | void genbf_initializer(struct initializer *a); 93 | void genbf_initializer_list(struct initializer_list *a); 94 | void genbf_iteration_statement(struct iteration_statement *a); 95 | void genbf_jump_statement(struct jump_statement *a); 96 | void genbf_labeled_statement(struct labeled_statement *a); 97 | 98 | int genbf_logical_and_expr(struct logical_and_expr *a, int lval, struct type **t); 99 | char *genbf_logical_and_expr_get_primary(int type, struct logical_and_expr *a); 100 | 101 | int genbf_logical_or_expr(struct logical_or_expr *a, int lval, struct type **t); 102 | char *genbf_logical_or_expr_get_primary(int type, struct logical_or_expr *a); 103 | 104 | int genbf_multiplicative_expr(struct multiplicative_expr *a, int lval, struct type **t); 105 | char *genbf_multiplicative_expr_get_primary(int type, struct multiplicative_expr *a); 106 | 107 | void genbf_parameter_declaration(struct parameter_declaration *a); 108 | void genbf_parameter_identifier_list(struct parameter_identifier_list *a); 109 | void genbf_parameter_list(struct parameter_list *a); 110 | void genbf_parameter_type_list(struct parameter_type_list *a); 111 | void genbf_pointer(struct pointer *a); 112 | 113 | int genbf_postfix_expr(struct postfix_expr *a, int lval, struct type **t); 114 | char *genbf_postfix_expr_get_primary(int type, struct postfix_expr *a); 115 | 116 | int genbf_primary_expr(struct primary_expr *a, int lval, struct type **t); 117 | char *genbf_primary_expr_get_primary(int type, struct primary_expr *a); 118 | char *genbf_parse_string(char *inp); 119 | 120 | int genbf_relational_expr(struct relational_expr *a, int lval, struct type **t); 121 | char *genbf_relational_expr_get_primary(int type, struct relational_expr *a); 122 | 123 | void genbf_selection_statement(struct selection_statement *a); 124 | 125 | int genbf_shift_expr(struct shift_expr *a, int lval, struct type **t); 126 | char *genbf_shift_expr_get_primary(int type, struct shift_expr *a); 127 | 128 | void genbf_statement(struct statement *a); 129 | void genbf_statement_list(struct statement_list *a); 130 | void genbf_storage_class_specifier(struct storage_class_specifier *a); 131 | void genbf_struct_declaration(struct struct_declaration *a); 132 | void genbf_struct_declaration_list(struct struct_declaration_list *a); 133 | void genbf_struct_declarator(struct struct_declarator *a); 134 | void genbf_struct_declarator_list(struct struct_declarator_list *a); 135 | void genbf_struct_or_union(struct struct_or_union *a); 136 | void genbf_struct_or_union_specifier(struct struct_or_union_specifier *a); 137 | void genbf_type_name(struct type_name *a); 138 | void genbf_type_specifier(struct type_specifier *a); 139 | void genbf_type_specifier_list(struct type_specifier_list *a); 140 | 141 | int genbf_unary_expr(struct unary_expr *a, int lval, struct type **t); 142 | char *genbf_unary_expr_get_primary(int type, struct unary_expr *a); 143 | 144 | void genbf_unary_operator(struct unary_operator *a); 145 | -------------------------------------------------------------------------------- /cc/genbf/primary_expr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | 23 | #include "../genbf.h" 24 | #include "generator.h" 25 | 26 | int genbf_primary_expr(struct primary_expr *a, int lval, struct type **t) 27 | { 28 | int i, v; 29 | struct var *sv; 30 | 31 | switch (a->type) { 32 | case _IDENTIFIER: 33 | /* where is this variable? */ 34 | v = getVar(a->v._identifier->v, &sv); 35 | 36 | /* there's a special case: if !lval but it's an array, we actually 37 | * want to treat it as an lval (get the data's location). If lval 38 | * and it's an array, we die (that's not allowed) */ 39 | if (sv->type->basic_type == TYPE_PTR && 40 | sv->type->array) { 41 | if (!lval) { 42 | lval = 1; 43 | } else { 44 | ERROR("primary_expr", "Changing array pointer values is not currently possible."); 45 | } 46 | } 47 | 48 | if (!lval) { 49 | BF_PUSH; 50 | pushTempVar(1); 51 | curvar->type = dupType(sv->type); 52 | 53 | /* FIXME: this needs to support a whole range of other idents */ 54 | if (v == -1) 55 | ERROR("primary_expr", "Undefined identifier."); 56 | 57 | v++; 58 | /* now go and get it */ 59 | printf("[-]"); 60 | for (i = 0; i < v; i++) 61 | printf("<<<<<"); 62 | /* dup */ 63 | printf("[>>>+>+<<<<-]>>>[<<<+>>>-]>["); 64 | /* carry it up */ 65 | for (i = 0; i < v; i++) 66 | printf(">>>>>"); 67 | printf("+"); 68 | for (i = 0; i < v; i++) 69 | printf("<<<<<"); 70 | printf("-]"); 71 | /* and put it in place */ 72 | for (i = 0; i < v; i++) 73 | printf(">>>>>"); 74 | printf("[<<<<+>>>>-]<<<<"); 75 | fflush(stdout); 76 | } else { 77 | /* lval - turn the identifier into a location */ 78 | if (v == -1) 79 | ERROR("primary_expr", "Undefined identifier."); 80 | 81 | if (t) *t = sv->type; 82 | 83 | /* now we must turn this depth into a location */ 84 | BF_PUSH; 85 | pushTempVar(1); 86 | 87 | curvar->type = dupType(sv->type); 88 | /* this is now a pointer at the array */ 89 | curvar->type->array = 0; 90 | curvar->type->size = 1; 91 | 92 | printf("[-]"); 93 | v++; 94 | for (i = 0; i < v; i++) 95 | printf("<<<<<"); 96 | STACK_POS_TO_PTR; 97 | 98 | return -1; 99 | } 100 | break; 101 | 102 | case _CONSTANT: 103 | if (lval) 104 | ERROR("primary_expr", "Invalid l-value."); 105 | 106 | /* FIXME: this is a ridiculous way to generate a constant ... */ 107 | BF_PUSH; 108 | pushTempVar(1); 109 | curvar->type = intType(); 110 | 111 | printf("[-]"); 112 | 113 | /* constants can be either numbers or 'characters' */ 114 | if (a->v._token[0] == '\'') { 115 | v = *genbf_parse_string(a->v._token); 116 | } else { 117 | v = atoi(a->v._token); 118 | } 119 | if (v >= 0) 120 | for (i = 0; i < v; i++) 121 | printf("+"); 122 | else 123 | for (i = 0; i > v; i--) 124 | printf("-"); 125 | 126 | fflush(stdout); 127 | break; 128 | 129 | case _EXPR: 130 | return genbf_expr(a->v._expr, lval, t); 131 | break; 132 | 133 | /*case _IDENTIFIER: 134 | genbf_identifier(a->v._identifier); 135 | break; 136 | 137 | case _CONSTANT: 138 | SPC; printf("CONSTANT: %s\n", a->v._token); 139 | break; 140 | 141 | case _STRING: 142 | SPC; printf("STRING: %s\n", a->v._token); 143 | break; 144 | 145 | case _EXPR: 146 | genbf_expr(a->v._expr); 147 | break;*/ 148 | default: 149 | UNIMPL("primary_expr"); 150 | } 151 | } 152 | 153 | char *genbf_primary_expr_get_primary(int type, struct primary_expr *a) 154 | { 155 | /*if (a->type == _EXPR) 156 | return genbf_expr_get_primary(type, a->v._expr);*/ 157 | 158 | if (type != a->type) return NULL; 159 | if (type == _IDENTIFIER) 160 | return a->v._identifier->v; 161 | if (type == _CONSTANT) 162 | return a->v._token; 163 | if (type == _STRING) 164 | return genbf_parse_string(a->v._token); 165 | } 166 | 167 | char *genbf_parse_string(char *inp) 168 | { 169 | static char *toret = NULL; 170 | int osl, i, o; 171 | 172 | if (!toret) { 173 | toret = (char *) malloc(strlen(inp) + 1); 174 | if (!toret) { perror("malloc"); exit(1); } 175 | } else { 176 | toret = (char *) realloc(toret, strlen(inp) + 1); 177 | if (!toret) { perror("realloc"); exit(1); } 178 | } 179 | 180 | /* now set osl, and walk */ 181 | osl = strlen(inp); 182 | o = 0; 183 | for (i = 1; i < osl - 1; i++) { 184 | switch (inp[i]) { 185 | case '\\': 186 | i++; 187 | switch (inp[i]) { 188 | case 'n': 189 | toret[o] = '\n'; 190 | break; 191 | 192 | case 'r': 193 | toret[o] = '\r'; 194 | break; 195 | 196 | default: 197 | toret[o] = inp[i]; 198 | break; 199 | } 200 | break; 201 | 202 | default: 203 | toret[o] = inp[i]; 204 | break; 205 | } 206 | o++; 207 | } 208 | toret[o] = '\0'; 209 | 210 | return toret; 211 | } 212 | -------------------------------------------------------------------------------- /cc/genbf/generator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #ifndef GENERATOR_H 22 | #define GENERATOR_H 23 | 24 | #include 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | #define UNIMPL(x) \ 31 | { \ 32 | fprintf(stderr, "Unimplemented generation: %s\n", x); \ 33 | exit(1); \ 34 | } 35 | 36 | #define ERROR(x, y) \ 37 | { \ 38 | fprintf(stderr, "ERROR in generation %s: %s\n", x, y); \ 39 | exit(1); \ 40 | } 41 | 42 | #define NEW(x, y) \ 43 | { \ 44 | x = (y *) malloc(sizeof(y)); \ 45 | if (!x) { \ 46 | perror("malloc"); \ 47 | exit(1); \ 48 | } \ 49 | } 50 | 51 | #define BF_PUSH \ 52 | printf("<<<[>+>+<<-]>[<+>-]>" \ 53 | "[>>>+<<<-]>>>+>>>"); \ 54 | fflush(stdout); 55 | #define BF_POP printf("<<<[-]<<"); fflush(stdout) 56 | 57 | #define STACK_POS_TO_PTR \ 58 | printf("<<<[>+>++<<-]" /* get the stack pos of the previous one */ \ 59 | ">[<+>-]>" /* put it back */ \ 60 | "[>>>>+<<<<-]>>>>++" /* copy it into our walk cell */ \ 61 | "<[>[>>>>>+<<<<<-]>>>>]" /* and walk up the stack */ \ 62 | "<<[-]>>>[<<<+>>>-]<<<"); /* then deposit it */ \ 63 | fflush(stdout); 64 | 65 | #define STACK_PTR_TO_POS_CARRY \ 66 | printf(">>>[<<<<<+>>>>>-]" /* move down the walk */ \ 67 | ">[<<<<<+>>>>>-]<<<<<" /* and carry */ \ 68 | "<<[" /* start our walk down */ \ 69 | ">[<<<<<+>>>>>-]>[<<<<<+>>>>>-]" /* move down our walk and carry */ \ 70 | "<<<<<<<]" /* finish the walk down */ \ 71 | ">[" /* start the walk up */ \ 72 | "[>>>>>+<<<<<-]>[>>>>>+<<<<<-]" /* move up walk and carry */ \ 73 | ">>>>--]" /* finish the walk up */ \ 74 | "<<<"); /* get to the stack pos */ \ 75 | fflush(stdout); 76 | 77 | #define STACK_PTR_TO_POS \ 78 | printf(">>>[<<<<<+>>>>>-]<<<<" /* move down the walk */ \ 79 | "<<[" /* start our walk down */ \ 80 | ">[<<<<<+>>>>>-]" /* move down our walk */ \ 81 | "<<<<<<]" /* finish the walk down */ \ 82 | ">[" /* start the walk up */ \ 83 | "[>>>>>+<<<<<-]" /* move up walk */ \ 84 | ">>>>>--]" /* finish the walk up */ \ 85 | "<<<"); /* get to the stack pos */ \ 86 | fflush(stdout); 87 | 88 | /* struct block holds information on what block of code we're in, how many 89 | * variables it uses, and how deep the stack is */ 90 | struct block { 91 | struct block *next; 92 | char *name; 93 | int num; /* the name in the output file is name!num (except where num is 94 | * 0) */ 95 | int vars; 96 | int stack; 97 | }; 98 | extern struct block *curblock; 99 | 100 | /* pushNamedBlock 101 | * input: name of the block to push 102 | * output: none 103 | * effect: curblock now points at a block with the name given 104 | */ 105 | void pushNamedBlock(const char *name); 106 | 107 | /* pushSubBlock 108 | * input: an offset 109 | * output: none 110 | * effect: curblock now points at a block with the previous name!num+1+offset, 111 | * and num 0 112 | */ 113 | void pushSubBlock(int offset); 114 | 115 | /* pushBlock 116 | * input: none 117 | * output: none 118 | * effect: curblock now points at a block with the previous name, but a new 119 | * number 120 | */ 121 | void pushBlock(); 122 | 123 | /* pushCall 124 | * input: the function to call 125 | * output: none 126 | * effect: the function is called with the return address of pushBlock() 127 | */ 128 | void pushCall(const char *func); 129 | 130 | /* popNamedBlock 131 | * input: none 132 | * output: none 133 | * effect: pop down to the last named block 134 | */ 135 | void popNamedBlock(); 136 | 137 | /* popBlock 138 | * input: none 139 | * output: none 140 | * effect: pop a single block 141 | */ 142 | void popBlock(); 143 | 144 | /* outBlock 145 | * input: none 146 | * output: none 147 | * effect: "\n", the appropriate block name and ": " are printed on stdout 148 | */ 149 | void outBlock(); 150 | 151 | /* struct var is a linear linked list of current variables */ 152 | struct var { 153 | struct var *next; 154 | char *name; 155 | int width; 156 | struct type *type; 157 | }; 158 | extern struct var *curvar; 159 | 160 | /* pushVar 161 | * input: a variable name and width in stack cells 162 | * output: none 163 | * effect: a variable is pushed onto the internal variable stack 164 | */ 165 | void pushVar(const char *name, int width); 166 | 167 | /* pushTempVar 168 | * input: a variable width in stack cells 169 | * output: none 170 | * effect: an unnamed variable is pushed onto the internal variable stack 171 | */ 172 | void pushTempVar(int width); 173 | 174 | /* newVar 175 | * input: none 176 | * output: an empty MALLOC'D struct var 177 | * effect: none 178 | */ 179 | struct var *newVar(); 180 | 181 | /* popVar 182 | * input: none 183 | * output: none 184 | * effect: a variable is popped from the variable stack (both internal and BF) 185 | */ 186 | void popVar(); 187 | 188 | /* ignoreVar 189 | * input: none 190 | * output: none 191 | * effect: a variable is popped from the internal stack but left in the BF stack 192 | */ 193 | void ignoreVar(); 194 | 195 | /* getVar 196 | * input: variable name 197 | * output: the depth of the variable in the current stack or -1 on error 198 | * effect: v is set to the variable 199 | */ 200 | int getVar(const char *name, struct var **v); 201 | 202 | /* blockDepth 203 | * input: none 204 | * output: the depth of the current block 205 | * effect: none 206 | */ 207 | int blockDepth(); 208 | 209 | /* struct type is a linear-linked-list of type specifiers. When dereferencing 210 | * a pointer, one is popped off */ 211 | struct type { 212 | struct type *next; 213 | 214 | enum { 215 | TYPE_INT, TYPE_PTR 216 | } basic_type; 217 | 218 | /* if type is pointer, can actually be an in-place array. If array > 0, 219 | * that is the case */ 220 | int array; 221 | 222 | /* this is the total size at this level (a pure pointer will make this 1, 223 | * an array will make this prevsize * array */ 224 | int size; 225 | }; 226 | 227 | /* EXAMPLE: 228 | * type{.basic_type = TYPE_PTR, .array = 0} -> 229 | * type{.basic_type = TYPE_PTR, .array = 0} -> 230 | * type{.basic_type = TYPE_INT, .array = 0} -> NULL 231 | * = 232 | * int ** 233 | */ 234 | 235 | /* dupType 236 | * input: a struct type to be duplicated 237 | * output: the duplicated version, MALLOC'D 238 | * effect: none 239 | */ 240 | struct type *dupType(struct type *t); 241 | 242 | /* freeType 243 | * input: a struct type * 244 | * output: none 245 | * effect: the linear linked list's memory is free'd 246 | */ 247 | void freeType(struct type *t); 248 | 249 | /* intType 250 | * input: none 251 | * output: a MALLOC'D struct type with a simple int 252 | * effect: none 253 | */ 254 | struct type *intType(); 255 | 256 | #endif 257 | -------------------------------------------------------------------------------- /cc/genbf/generator.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006 Gregor Richards 3 | * 4 | * This file is part of C2BF. 5 | * 6 | * C2BF is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * C2BF is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with C2BF; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #include 22 | 23 | #include "generator.h" 24 | 25 | struct block *curblock = NULL; 26 | struct var *curvar = NULL; 27 | 28 | /* pushNamedBlock 29 | * input: name of the block to push 30 | * output: none 31 | * effect: curblock now points at a block with the name given 32 | */ 33 | void pushNamedBlock(const char *name) 34 | { 35 | struct block *prevblock = curblock; 36 | NEW(curblock, struct block); 37 | 38 | curblock->next = prevblock; 39 | curblock->name = strdup(name); 40 | if (!curblock->name) { perror("strdup"); exit(1); } 41 | curblock->num = 0; 42 | curblock->vars = 0; 43 | curblock->stack = 0; 44 | } 45 | 46 | /* pushSubBlock 47 | * input: an offset 48 | * output: none 49 | * effect: curblock now points at a block with the previous name!num+1+offset, 50 | * and num 0 51 | */ 52 | void pushSubBlock(int offset) 53 | { 54 | char *nname; 55 | 56 | pushBlock(); 57 | /* nname contains the new multi-! name */ 58 | nname = (char *) malloc(strlen(curblock->name) + 11); 59 | if (!nname) { perror("malloc"); exit(1); } 60 | /* this num is what makes this a "subblock" */ 61 | sprintf(nname, "%s!%d", curblock->name, curblock->num + offset); 62 | free(curblock->name); 63 | curblock->name = nname; 64 | curblock->num = 0; 65 | } 66 | 67 | /* pushBlock 68 | * input: none 69 | * output: none 70 | * effect: curblock now points at a block with the previous name, but a new 71 | * number 72 | */ 73 | void pushBlock() 74 | { 75 | struct block *prevblock = curblock; 76 | NEW(curblock, struct block); 77 | 78 | if (!prevblock) { 79 | fprintf(stderr, "Internal compiler error in pushBlock()\n"); 80 | exit(1); 81 | } 82 | 83 | curblock->next = prevblock; 84 | curblock->name = strdup(prevblock->name); 85 | if (!curblock->name) { perror("strdup"); exit(1); } 86 | curblock->num = prevblock->num + 1; 87 | curblock->vars = 0; 88 | curblock->stack = 0; 89 | } 90 | 91 | /* pushCall 92 | * input: the function to call 93 | * output: none 94 | * effect: the function is called with the return address of pushBlock() 95 | */ 96 | void pushCall(const char *func) 97 | { 98 | if (!curblock) { 99 | fprintf(stderr, "Internal compiler error in pushCall()\n"); 100 | exit(1); 101 | } 102 | 103 | BF_PUSH; 104 | pushTempVar(1); 105 | 106 | printf("(*%s!%d)(%s)", curblock->name, curblock->num + 1, func); 107 | 108 | pushBlock(); 109 | } 110 | 111 | /* popNamedBlock 112 | * input: none 113 | * output: none 114 | * effect: pop down to the last named block 115 | */ 116 | void popNamedBlock() 117 | { 118 | if (!curblock) { 119 | fprintf(stderr, "Internal compiler error 1 in popNamedBlock()\n"); 120 | exit(1); 121 | } 122 | 123 | /* keep popping until we pop one with num=0 */ 124 | while (curblock->num > 0) { 125 | popBlock(); 126 | 127 | if (!curblock) { 128 | fprintf(stderr, "Internal compiler error 2 in popNamedBlock()\n"); 129 | exit(1); 130 | } 131 | } 132 | 133 | /* this one has num = 0, pop it */ 134 | popBlock(); 135 | } 136 | 137 | /* popBlock 138 | * input: none 139 | * output: none 140 | * effect: pop a single block 141 | */ 142 | void popBlock() 143 | { 144 | struct block *nextblock; 145 | int i; 146 | 147 | if (!curblock) { 148 | fprintf(stderr, "Internal compiler error in popBlock()\n"); 149 | exit(1); 150 | } 151 | 152 | /* pop off the vars */ 153 | for (i = 0; i < curblock->vars; i++) { 154 | popVar(); 155 | } 156 | 157 | free(curblock->name); 158 | 159 | nextblock = curblock->next; 160 | free(curblock); 161 | curblock = nextblock; 162 | } 163 | 164 | /* outBlock 165 | * input: none 166 | * output: none 167 | * effect: "\n", the appropriate block name and ": " are printed on stdout 168 | */ 169 | void outBlock() 170 | { 171 | if (!curblock) { 172 | fprintf(stderr, "Internal compiler error in outBlock()\n"); 173 | exit(1); 174 | } 175 | 176 | /* if the number is nonzero, name!num, otherwise just name */ 177 | if (curblock->num) { 178 | printf("\n%s!%d: ", curblock->name, curblock->num); 179 | } else { 180 | printf("\n%s: ", curblock->name); 181 | } 182 | 183 | fflush(stdout); 184 | } 185 | 186 | /* pushVar 187 | * input: a variable name and width in stack cells 188 | * output: none 189 | * effect: a variable is pushed onto the variable stack, and the previous 190 | * variable's depth is set 191 | */ 192 | void pushVar(const char *name, int width) 193 | { 194 | /* this is pushed like a temp var, then named */ 195 | pushTempVar(width); 196 | 197 | curvar->name = strdup(name); 198 | if (!curvar->name) { perror("strdup"); exit(1); } 199 | 200 | /* and added the var to the block */ 201 | curblock->vars++; 202 | curblock->stack += width; 203 | } 204 | 205 | /* pushTempVar 206 | * input: a variable width in stack cells 207 | * output: none 208 | * effect: an unnamed variable is pushed onto the internal variable stack 209 | */ 210 | void pushTempVar(int width) 211 | { 212 | struct var *prevvar; 213 | 214 | if (!curblock) { 215 | fprintf(stderr, "Internal compiler error in pushVar()\n"); 216 | exit(1); 217 | } 218 | 219 | /* and push the var on the stack */ 220 | prevvar = curvar; 221 | NEW(curvar, struct var); 222 | 223 | curvar->next = prevvar; 224 | curvar->name = NULL; 225 | curvar->width = width; 226 | curvar->type = NULL; 227 | } 228 | 229 | /* newVar 230 | * input: none 231 | * output: an empty MALLOC'D struct var 232 | * effect: none 233 | */ 234 | struct var *newVar() 235 | { 236 | struct var *nv; 237 | 238 | NEW(nv, struct var); 239 | 240 | nv->next = NULL; 241 | nv->name = NULL; 242 | nv->width = 0; 243 | nv->type = NULL; 244 | 245 | return nv; 246 | } 247 | 248 | /* popVar 249 | * input: none 250 | * output: none 251 | * effect: a variable is popped from the variable stack 252 | */ 253 | void popVar() 254 | { 255 | struct var *nextvar; 256 | int i; 257 | 258 | if (!curvar) { 259 | fprintf(stderr, "Internal compiler error in popVar()\n"); 260 | exit(1); 261 | } 262 | 263 | for (i = 0; i < curvar->width; i++) 264 | BF_POP; 265 | 266 | freeType(curvar->type); 267 | 268 | free(curvar->name); 269 | nextvar = curvar->next; 270 | free(curvar); 271 | curvar = nextvar; 272 | } 273 | 274 | /* ignoreVar 275 | * input: none 276 | * output: none 277 | * effect: a variable is popped from the internal stack but left in the BF stack 278 | */ 279 | void ignoreVar() 280 | { 281 | struct var *nextvar; 282 | int i; 283 | 284 | if (!curvar) { 285 | fprintf(stderr, "Internal compiler error in ignoreVar()\n"); 286 | exit(1); 287 | } 288 | 289 | free(curvar->name); 290 | nextvar = curvar->next; 291 | free(curvar); 292 | curvar = nextvar; 293 | } 294 | 295 | /* getVar 296 | * input: variable name 297 | * output: the depth of the variable in the current stack or -1 on error 298 | * effect: v is set to the variable 299 | */ 300 | int getVar(const char *name, struct var **v) 301 | { 302 | int depth = 0; 303 | struct var *cur; 304 | 305 | /* start with the top */ 306 | cur = curvar; 307 | 308 | while (cur) { 309 | /* we need the left end of the variable */ 310 | depth += cur->width - 1; 311 | 312 | /* if it matches, return the current depth */ 313 | if (cur->name && !strcmp(cur->name, name)) { 314 | if (v) { 315 | *v = cur; 316 | } 317 | return depth; 318 | } 319 | 320 | depth += 1; 321 | cur = cur->next; 322 | } 323 | 324 | /* no match! */ 325 | return -1; 326 | } 327 | 328 | /* blockDepth 329 | * input: none 330 | * output: the depth of the current block 331 | * effect: none 332 | */ 333 | int blockDepth() 334 | { 335 | int depth = 0; 336 | struct var *cur; 337 | 338 | /* start with the top */ 339 | cur = curvar; 340 | 341 | while (cur) { 342 | /* add the current depth */ 343 | depth += cur->width; 344 | cur = cur->next; 345 | } 346 | 347 | return depth; 348 | } 349 | 350 | /* dupType 351 | * input: a struct type to be duplicated 352 | * output: the duplicated version, MALLOC'D 353 | * effect: none 354 | */ 355 | struct type *dupType(struct type *t) 356 | { 357 | struct type *cur, *o, *oc, *on; 358 | 359 | o = NULL; 360 | oc = NULL; 361 | on = NULL; 362 | 363 | cur = t; 364 | 365 | /* go through, each time adding a level */ 366 | while (cur) { 367 | if (!o) { 368 | /* start the chain */ 369 | NEW(o, struct type); 370 | oc = o; 371 | } else { 372 | /* continue the chain */ 373 | NEW(on, struct type); 374 | oc->next = on; 375 | oc = on; 376 | } 377 | 378 | /* copy it in */ 379 | memcpy(oc, cur, sizeof(struct type)); 380 | 381 | /* set next to NULL */ 382 | oc->next = NULL; 383 | 384 | /* step */ 385 | cur = cur->next; 386 | } 387 | 388 | /* now o is our newly created list */ 389 | return o; 390 | } 391 | 392 | /* freeType 393 | * input: a struct type * 394 | * output: none 395 | * effect: the linear linked list's memory is free'd 396 | */ 397 | void freeType(struct type *t) 398 | { 399 | struct type *cur, *next; 400 | 401 | cur = t; 402 | 403 | while (cur) { 404 | next = cur->next; 405 | free(cur); 406 | cur = next; 407 | } 408 | } 409 | 410 | /* intType 411 | * input: none 412 | * output: a MALLOC'D struct type with a simple int 413 | * effect: none 414 | */ 415 | struct type *intType() 416 | { 417 | struct type *t; 418 | 419 | NEW(t, struct type); 420 | t->next = NULL; 421 | t->basic_type = TYPE_INT; 422 | t->array = 0; 423 | t->size = 1; 424 | } 425 | -------------------------------------------------------------------------------- /GPL: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | --------------------------------------------------------------------------------