URL
https://opencores.org/ocsvn/zipcpu/zipcpu/trunk
Subversion Repositories zipcpu
[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zasm.y] - Rev 14
Go to most recent revision | Compare with Previous | Blame | View Log
/*******************************************************************************
**
** Filename: zasm.y
**
** Project: Zip CPU -- a small, lightweight, RISC CPU core
**
** Purpose: The parser for the Zip Assembler. This is actually not just
** the parser, but the main program as well.
**
** Creator: Dan Gisselquist, Ph.D.
** Gisselquist Tecnology, LLC
**
********************************************************************************
**
** Copyright (C) 2015, Gisselquist Technology, LLC
**
** This program is free software (firmware): 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 MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no
** target there if the PDF file isn't present.) If not, see
** <http://www.gnu.org/licenses/> for a copy.
**
** License: GPL, v3, as defined and found on www.gnu.org,
** http://www.gnu.org/licenses/gpl.html
**
**
*******************************************************************************/
%{
#include <stdio.h>
#include <string.h>
#include "asmdata.h"
extern "C" int yylex(void);
extern "C" int yyparse(void);
// extern "C" FILE *yyin;
void yyerror(const char *);
unsigned int global_parser_pc;
char *master_input_filename = NULL;
extern int yylineno;
char *linecp = NULL; // A copy of the input line
%}
%token COMMA EQU PLUS MINUS TIMES HERE DOLLAR COLON
%token BOOLEANOR BITWISEOR BOOLEANAND BITWISEAND BITWISEXOR DOT
%token WORD FILL
%token LOADOP STOROP LDIOP
%token BAREOP BRANCHOP COND DUALOP IDENTIFIER INT LDHLOP REG SINGLOP
%union {
ZPARSER::ZIPREG u_reg;
ZPARSER::ZIPCOND u_cond;
int u_ival;
LEXOPCODE u_op;
char *u_id;
ASMLINE *u_ln;
AST *u_ast;
}
%type <u_reg> REG
%type <u_cond> COND opcond
%type <u_ival> INT
%type <u_id> IDENTIFIER
%type <u_op> BAREOP SINGLOP DUALOP BRANCHOP LDHLOP
%type <u_ln> unlabeledline instruction wordlist fillist opb
%type <u_ln> bareop singlop dualop loadop storop line
%type <u_ast> expr value multident
%% /* The grammar follows */
input:
%empty
| input line { if ($2) {objcode += $2; global_parser_pc += $2->nlines(); if ($2->isdefined()) delete $2; } }
;
line:
'\n' { $$ = NULL; }
| unlabeledline '\n' { $$ = $1; }
| multident COLON unlabeledline '\n' {
if ($1->m_node_type == 'I') {
if (((AST_IDENTIFIER*)$1)->m_id[0] == 'L')
stb_define(((AST_IDENTIFIER *)$1)->m_id, new AST_NUMBER(global_parser_pc));
else
gbl_define(((AST_IDENTIFIER *)$1)->m_id, new AST_NUMBER(global_parser_pc));
delete $1;
}
$$ = $3;
}
| multident COLON '\n' {
if ($1->m_node_type == 'I') {
if (((AST_IDENTIFIER*)$1)->m_id[0] == 'L')
stb_define(((AST_IDENTIFIER *)$1)->m_id, new AST_NUMBER(global_parser_pc));
else
gbl_define(((AST_IDENTIFIER *)$1)->m_id, new AST_NUMBER(global_parser_pc));
delete $1;
}
$$ = new VLINE();
}
| multident EQU expr '\n' {
if ($1->m_node_type == 'I') {
stb_define(((AST_IDENTIFIER *)$1)->m_id, $3);
delete $1;
}
$$ = new VLINE();
}
;
unlabeledline:
instruction { $$ = $1; }
| WORD wordlist { $$ = $2; }
| FILL fillist { $$ = $2; }
;
wordlist:
expr {
if ($1->isdefined())
$$ = new DLINE($1->eval());
else {
$$ = new VLINE();
yyerror("ERROR: word list undefined");
}}
| expr COMMA wordlist {
if ($1->isdefined())
$$ = new DLINE($1->eval());
else {
$$ = new VLINE();
yyerror("ERROR: word list undefined\n");
}}
;
fillist:
expr COMMA expr {
if (($1->isdefined())&&($3->isdefined())) {
int ntimes = $1->eval(),
val = $3->eval();
LLINE *ln = new LLINE();
for(int i=0; i<ntimes; i++)
ln->addline(new DLINE(val));
$$ = ln;
} else {
yyerror("Fill list undefined\n");
$$ = new VLINE();
}
}
;
instruction:
dualop opb COMMA REG {
$$ = $1;
((TLINE*)$1)->m_imm = ((TLINE*)$2)->m_imm; ((TLINE*)$2)->m_imm = NULL;
((TLINE*)$1)->m_opb = ((TLINE*)$2)->m_opb;
((TLINE*)$1)->m_opa = $4;
if ($1->isdefined()) {
$$ = ((TLINE*)$1)->eval();
delete $1;
delete $2;
}
}
| dualop opb COMMA multident {
char buf[256];
sprintf(buf, "%s is not a register", ((AST_IDENTIFIER *)$4)->m_id.c_str());
yyerror(buf);
$$ = new VLINE();
delete $1;
delete $2;
delete $4;
}
| singlop opb {
$$ = $1;
((TLINE*)$1)->m_imm = ((TLINE*)$2)->m_imm; ((TLINE*)$2)->m_imm = NULL;
((TLINE*)$1)->m_opb = ((TLINE*)$2)->m_opb;
if ($1->isdefined()) {
$$ = ((TLINE *)$1)->eval();
delete $1;
}
}
| bareop { $$ = $1; }
| LDHLOP opcond expr COMMA REG {
TLINE *tln = new TLINE;
tln->m_opcode = $1;
tln->m_cond = $2;
tln->m_imm = $3;
tln->m_opa = $5;
if (tln->isdefined()) {
$$ = tln->eval();
delete tln;
} else
$$ = tln;
}
| LDHLOP opcond expr COMMA multident {
char buf[256];
sprintf(buf, "%s is not a register", ((AST_IDENTIFIER *)$5)->m_id.c_str());
yyerror(buf);
$$ = new VLINE();
delete $3;
delete $5;
}
| LDIOP expr COMMA REG {
TLINE *tln = new TLINE;
tln->m_opcode = OP_LDI;
tln->m_cond = ZPARSER::ZIPC_ALWAYS;
tln->m_imm = $2;
tln->m_opa = $4;
if (tln->isdefined()) {
$$ = tln->eval();
delete tln;
} else
$$ = tln;
}
| LDIOP expr COMMA multident {
char buf[256];
sprintf(buf, "%s is not a register", ((AST_IDENTIFIER *)$4)->m_id.c_str());
yyerror(buf);
$$ = new VLINE();
delete $2;
delete $4;
}
| BRANCHOP expr {
TLINE *tln = new TLINE;
tln->m_opcode = $1;
tln->m_imm = $2;
if (tln->isdefined()) {
$$ = tln->eval();
delete tln;
} else
$$ = tln;
}
| loadop opb COMMA REG {
TLINE *tln = new TLINE;
((TLINE*)$2)->m_opcode = OP_LOD;
((TLINE*)$2)->m_cond = ((TLINE*)$1)->m_cond;
((TLINE*)$2)->m_opa = $4;
delete $1;
if (((TLINE *)$2)->isdefined()) {
$$ = ((TLINE *)$2)->eval();
delete $2;
} else
$$ = $2;
}
| loadop opb COMMA multident {
char buf[256];
sprintf(buf, "%s is not a register", ((AST_IDENTIFIER *)$4)->m_id.c_str());
yyerror(buf);
$$ = new VLINE();
delete $1;
delete $2;
delete $4;
}
| storop REG COMMA opb {
TLINE *tln = new TLINE;
tln->m_opcode = OP_STO;
tln->m_cond = ((TLINE*)$1)->m_cond;
tln->m_imm = ((TLINE*)$4)->m_imm;
tln->m_opb = ((TLINE*)$4)->m_opb;
tln->m_opa = $2;
delete $1;
if (tln->isdefined()) {
$$ = tln->eval();
delete tln;
} else
$$ = tln;
}
| storop multident COMMA opb {
char buf[256];
sprintf(buf, "%s is not a register", ((AST_IDENTIFIER *)$2)->m_id.c_str());
yyerror(buf);
$$ = new VLINE();
delete $1;
delete $2;
delete $4;
}
;
dualop: DUALOP opcond {
TLINE *tln = new TLINE();
tln->m_opcode = $1;
tln->m_cond = $2;
$$ = tln;
}
;
singlop: SINGLOP opcond {
TLINE *tln = new TLINE();
tln->m_opcode = $1;
tln->m_cond = $2;
$$ = tln;
}
;
storop: STOROP opcond {
TLINE *tln = new TLINE();
tln->m_opcode = OP_STO;
tln->m_cond = $2;
$$ = tln;
}
;
loadop: LOADOP opcond {
TLINE *tln = new TLINE();
tln->m_opcode = OP_LOD;
tln->m_cond = $2;
$$ = tln;
}
;
bareop: BAREOP opcond {
TLINE *tln = new TLINE();
tln->m_opcode = $1;
tln->m_cond = $2;
$$ = tln;
}
;
opcond:
COND { $$ = $1; }
| %empty { $$ = ZPARSER::ZIPC_ALWAYS; }
;
opb:
expr {
TLINE *tln = new TLINE();
tln->m_imm = $1;
$$ = tln;
}
| expr PLUS REG {
TLINE *tln = new TLINE();
tln->m_imm = $1;
tln->m_opb = $3;
$$ = tln;
}
| expr '(' REG ')' {
TLINE *tln = new TLINE();
tln->m_imm = $1;
tln->m_opb = $3;
$$ = tln;
}
| '(' REG ')' {
TLINE *tln = new TLINE();
tln->m_imm = new AST_NUMBER(0);
tln->m_opb = $2;
$$ = tln;
}
| REG {
TLINE *tln = new TLINE();
tln->m_imm = new AST_NUMBER(0);
tln->m_opb = $1;
$$ = tln;
}
;
expr:
value { $$ = $1; }
| MINUS value { $$ = new AST_BRANCH('-',new AST_NUMBER(0), $2); }
| expr PLUS value { $$ = new AST_BRANCH('+',$1,$3); }
| expr MINUS value { $$ = new AST_BRANCH('-',$1,$3); }
| expr TIMES value { $$ = new AST_BRANCH('*',$1,$3); }
| expr BOOLEANOR value { $$ = new AST_BRANCH('o',$1,$3); }
| expr BITWISEOR value { $$ = new AST_BRANCH('|',$1,$3); }
| expr BOOLEANAND value { $$ = new AST_BRANCH('a',$1,$3); }
| expr BITWISEAND value { $$ = new AST_BRANCH('&',$1,$3); }
| expr BITWISEXOR value { $$ = new AST_BRANCH('^',$1,$3); }
| '(' expr ')' { $$ = $2; }
;
/* expr OR (|) value */
/* expr XOR (^) value */
/* expr AND (&) value */
value:
INT { $$ = new AST_NUMBER($1); }
| multident { $$ = $1; }
| HERE { $$ = new AST_NUMBER(global_parser_pc); }
;
multident:
IDENTIFIER { $$ = new AST_IDENTIFIER($1); delete $1; }
| multident DOT IDENTIFIER { $$ = new AST_IDENTIFIER($1,$3); delete $3; }
;
%%
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <assert.h>
OBJFILE objcode;
void yyerror(const char *str) {
fprintf(stderr, "%s:%d: ERROR: %s\n", master_input_filename, yylineno, str);
if (linecp) fprintf(stderr, "Offending line was: %s\n", linecp);
}
FILE *run_preprocessor(const char *zname = NULL) {
int pipefd[2];
int pid;
if (pipe(pipefd)!=0) {
fprintf(stderr, "PIPE FAILED!\n");
perror("O/S Err:");
exit(-2);
} if ((zname)&&(access(zname, R_OK)!=0)) { // if !zname, then use stdin
fprintf(stderr, "Cannot open %s\n", zname);
perror("O/S Err:");
exit(-2);
}
if (0 == (pid = fork())) {
int fdin, fdout;
// Child process -- run the preprocessor
if (zname) {
fdin = open(zname, O_RDONLY);
close(STDIN_FILENO);
dup2(fdin, STDIN_FILENO);
} // else use stdin, already set up
close(pipefd[0]);
fdout = pipefd[1];
close(STDOUT_FILENO);
dup2(fdout, STDOUT_FILENO);
// This call should never return
execlp("./zpp", "zpp", NULL);
fprintf(stderr, "Could not start pre-processor!\n");
perror("O/S Err:");
exit(-5);
}
close(pipefd[1]);
return fdopen(pipefd[0], "r");
}
int main(int argc, char **argv) {
int skp = 0;
const char *zout_fname = NULL;
master_input_filename = NULL;
skp=1;
for(int argn=0; argn+skp<argc; argn++) {
if (argv[argn+skp][0] == '-') {
if (argv[argn+skp][1] == 'o') {
if (zout_fname)
free((void *)zout_fname);
zout_fname = strdup(argv[argn+skp+1]);
skp++;
}
skp++;
} argv[argn] = argv[argn+skp];
} argc -= skp;
if (!zout_fname)
zout_fname = "z.out";
objcode.open(zout_fname);
master_input_filename = NULL;
if (argc > 0) {
for(int argn=0; argn<argc; argn++) {
extern FILE *yyin;
extern void yyrestart(FILE *);
FILE *tst;
create_new_context();
if (master_input_filename)
free(master_input_filename);
master_input_filename = strdup(argv[argn]);
yyrestart(run_preprocessor(master_input_filename));
yylineno = 1;
yyparse();
}
} else { // Run from Stdin
extern FILE *yyin;
extern void yyrestart(FILE *);
create_new_context();
master_input_filename = strdup("(stdin)");
yyin = run_preprocessor(NULL);
yyrestart(yyin);
yyparse();
}
if (!objcode.reduce())
fprintf(stderr, "Not all symbols defined!\n");
}
Go to most recent revision | Compare with Previous | Blame | View Log