1 2 3 4 5 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/* front-end compiler for vmgen example
Copyright (C) 2001,2002,2003,2007 Free Software Foundation, Inc.
This file is part of Gforth.
Gforth is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see http://www.gnu.org/licenses/.
*/
/* I use yacc/bison here not because I think it's the best tool for
the job, but because it's widely available and popular; it's also
(barely) adequate for this job. */
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mini.h"
/* BB_BOUNDARY is needed on basic blocks without a preceding VM branch */
#define BB_BOUNDARY (last_compiled = NULL, /* suppress peephole opt */ \
block_insert(vmcodep)) /* for accurate profiling */
Inst *vm_prim;
Inst *vmcodep;
FILE *vm_out;
int vm_debug;
void yyerror(char *s)
{
#if 1
/* for pure flex call */
fprintf(stderr, "%s: %s\n", program_name, s);
#else
/* lex or flex -l supports yylineno */
fprintf (stderr, "%s: %d: %s\n", program_name, yylineno, s);
#endif
}
#include "mini-gen.i"
void gen_main_end(void)
{
gen_call(&vmcodep, func_addr("main"), func_calladjust("main"));
gen_end(&vmcodep);
BB_BOUNDARY; /* for profiling; see comment in mini.vmg:end */
}
int locals=0;
int nonparams=0;
int yylex();
%}
%token FUNC RETURN END VAR IF THEN ELSE WHILE DO BECOMES PRINT NUM IDENT
%union {
long num;
char *string;
Inst *instp;
}
%type <string> IDENT;
%type <num> NUM;
%type <instp> elsepart;
%%
program: program function
| ;
function: FUNC IDENT { locals=0; nonparams=0; } '(' params ')'
vars { insert_func($2,vmcodep,locals,nonparams); }
stats RETURN expr ';'
END FUNC ';' { gen_return(&vmcodep, -adjust(locals)); }
;
params: IDENT ',' { insert_local($1); } params
| IDENT { insert_local($1); }
| ;
vars: vars VAR IDENT ';' { insert_local($3); nonparams++; }
| ;
stats: stats stat ';'
| ;
stat: IF expr THEN { gen_zbranch(&vmcodep, 0); $<instp>$ = vmcodep; }
stats { $<instp>$ = $<instp>4; }
elsepart END IF { BB_BOUNDARY; $<instp>7[-1] = (Inst)vmcodep; }
| WHILE { BB_BOUNDARY; $<instp>$ = vmcodep; }
expr DO { gen_zbranch(&vmcodep, 0); $<instp>$ = vmcodep; }
stats END WHILE { gen_branch(&vmcodep, $<instp>2); $<instp>5[-1] = (Inst)vmcodep; }
| IDENT BECOMES expr { gen_storelocal(&vmcodep, var_offset($1)); }
| PRINT expr { gen_print(&vmcodep); }
| expr { gen_drop(&vmcodep); }
;
elsepart: ELSE { gen_branch(&vmcodep, 0); $<instp>$ = vmcodep; $<instp>0[-1] = (Inst)vmcodep; }
stats { $$ = $<instp>2; }
| { $$ = $<instp>0; }
;
expr: term '+' term { gen_add(&vmcodep); }
| term '-' term { gen_sub(&vmcodep); }
| term '*' term { gen_mul(&vmcodep); }
| term '&' term { gen_and(&vmcodep); }
| term '|' term { gen_or(&vmcodep); }
| term '<' term { gen_lessthan(&vmcodep); }
| term '=' term { gen_equals(&vmcodep); }
| '!' term { gen_not(&vmcodep); }
| '-' term { gen_negate(&vmcodep); }
| term
;
term: '(' expr ')'
| IDENT '(' args ')' { gen_call(&vmcodep, func_addr($1), func_calladjust($1)); }
| IDENT { gen_loadlocal(&vmcodep, var_offset($1)); }
| NUM { gen_lit(&vmcodep, $1); }
;
/* missing: argument counting and checking against called function */
args: expr ',' args
| expr
| ;
%%
int yywrap(void)
{
return 1;
}
#include "lex.yy.c"
|