OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

Compare Revisions

  • This comparison shows the changes necessary to convert path
    /zipcpu/trunk/sw/zasm
    from Rev 14 to Rev 16
    Reverse comparison

Rev 14 → Rev 16

/test.S
34,6 → 34,7
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
#include "sys.i"
sys.bus equ 0xc0000000
sys.breaken equ 0x080
sys.step equ 0x040
381,3 → 382,22
 
// And, in case we miss a halt ...
halt
 
// Now, let's test whether or not we can handle a subroutine
reverse_bit_order:
PUSH(R1,SP)
PUSH(R2,SP)
LDI 32,R1
CLR R2
LSL 1,R2
LSR 1,R0
OR.C 1,R2
SUB 1,R1
BNZ reverse_bit_order_loop
MOV R2,R0
POP(R2,SP)
POP(R1,SP)
RET
fill 512,0
stack:
word 0
/zasm.y
128,10 → 128,17
$$ = new VLINE();
yyerror("ERROR: word list undefined");
}}
| expr COMMA wordlist {
if ($1->isdefined())
$$ = new DLINE($1->eval());
else {
| wordlist COMMA expr {
if ($3->isdefined()) {
LLINE *ln;
if ($1->m_state == 'L') {
ln = ((LLINE *)$1);
} else {
ln = new LLINE();
ln->addline($1);
} ln->addline(new DLINE($3->eval()));
$$ = ln;
} else {
$$ = new VLINE();
yyerror("ERROR: word list undefined\n");
}}
406,7 → 413,7
if (linecp) fprintf(stderr, "Offending line was: %s\n", linecp);
}
 
FILE *run_preprocessor(const char *zname = NULL) {
FILE *run_preprocessor(const char *path = NULL, const char *zname = NULL) {
int pipefd[2];
int pid;
 
425,20 → 432,30
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 the reader: we write only
close(pipefd[0]);
 
// Adjust stdout to write to our pipe instead of anywhere else
fdout = pipefd[1];
close(STDOUT_FILENO);
dup2(fdout, STDOUT_FILENO);
 
char *zpp_path;
if (path != NULL) {
zpp_path = new char[strlen(path+1+strlen("/zpp"))];
strcpy(zpp_path, path);
strcat(zpp_path, "/zpp");
} else zpp_path = strdup("zpp");
 
// This call should never return
execlp("./zpp", "zpp", NULL);
// We have to pass the name here, rather than an open file,
// since zpp needs to mark for us what file it is in at all
// times.
if (zname)
execlp(zpp_path, "zpp", zname, NULL);
else
execlp(zpp_path, "zpp", NULL);
 
fprintf(stderr, "Could not start pre-processor!\n");
perror("O/S Err:");
453,8 → 470,19
int main(int argc, char **argv) {
int skp = 0;
const char *zout_fname = NULL;
char *path_to_zasm = NULL;
bool preprocess_only = false;
master_input_filename = NULL;
 
// Find what directory zasm is in, so that we can find zpp when
// necessary.
path_to_zasm = NULL;
if (strchr(argv[0], '/')) {
path_to_zasm = strdup(argv[0]);
char *ptr = strrchr(path_to_zasm,'/');
*ptr = '\0';
}
 
skp=1;
for(int argn=0; argn+skp<argc; argn++) {
if (argv[argn+skp][0] == '-') {
463,16 → 491,21
free((void *)zout_fname);
zout_fname = strdup(argv[argn+skp+1]);
skp++;
}
} else if (argv[argn+skp][1] == 'E')
preprocess_only = true;
 
skp++;
} argv[argn] = argv[argn+skp];
} argc -= skp;
 
if (!zout_fname)
zout_fname = "z.out";
if (preprocess_only) {
objcode.open("/dev/null");
} else {
if (!zout_fname)
zout_fname = "z.out";
 
objcode.open(zout_fname);
objcode.open(zout_fname);
}
 
master_input_filename = NULL;
 
486,9 → 519,16
if (master_input_filename)
free(master_input_filename);
master_input_filename = strdup(argv[argn]);
yyrestart(run_preprocessor(master_input_filename));
yylineno = 1;
yyparse();
if (preprocess_only) {
FILE *fp = run_preprocessor(path_to_zasm, master_input_filename);
int ch;
while(EOF != (ch = fgetc(fp)))
fputc(ch, stdout);
} else {
yyrestart(run_preprocessor(path_to_zasm, master_input_filename));
yylineno = 1;
yyparse();
}
}
} else { // Run from Stdin
extern FILE *yyin;
496,9 → 536,16
 
create_new_context();
master_input_filename = strdup("(stdin)");
yyin = run_preprocessor(NULL);
yyrestart(yyin);
yyparse();
if (preprocess_only) {
int ch;
FILE *fp = run_preprocessor(path_to_zasm, master_input_filename);
while(EOF != (ch = fgetc(fp)))
fputc(ch, stdout);
} else {
yyin = run_preprocessor(NULL);
yyrestart(yyin);
yyparse();
}
}
 
if (!objcode.reduce())
/zpp.l
49,9 → 49,14
 
using namespace std;
 
extern "C" int yylex();
int yylex();
void mark_line(void);
int end_of_file(void);
void pushf(const char *fname);
 
// #include "zprepr.tab.h"
int ndef = 0;
int ndef = 0, structno = 0;
char *structid = NULL;
void stb_define(const char *str);
void stb_args(const char *str);
void stb_macro(const char *value);
59,11 → 64,12
bool stb_isdefined(const char *str);
const char *stb_getdefn(const char *str);
%}
%x DEF DFA DFV INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT
%x INDEF_EOL INNOTDEF_EOL
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID
%option noyywrap
%option stack
ID [_:A-Za-z][_:A-Za-z0-9]*
IDDOT {ID}("."{ID})*
 
%%
<COMMENT>"*/" { yy_pop_state(); }
73,42 → 79,43
char *ptr = &yytext[9], *start, *end, *str;
while(isspace(*ptr))
ptr++;
start = ptr;
start = ++ptr;
ptr++;
while((*ptr)&&(*ptr != '\"'))
ptr++;
*ptr = '\0';
yypush_buffer_state(yy_create_buffer(
fopen(start, "r"),
YY_BUF_SIZE));
pushf(start);
// push_file_state(yylineno); // and filename ...
fprintf(yyout, "#line 0 \"%s\"\n", start);
mark_line();
}
<INITIAL,INDEF>^"#define"[ \t]+ { yy_push_state(DEF); }
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
<DEF>[_A-Za-z][_:A-Za-z0-9]*/[^(] {
<DEF>{IDDOT}/[^(] {
stb_define(yytext);
BEGIN DFV;
}
<DEF>{ID}/[(] { fprintf(stderr, "DEF::Found MACRO\n");
stb_define(yytext);
BEGIN DFA; }
<DFA>([^)]+) {
/* Process arguments */
<DEF>{IDDOT}/[(] { stb_define(yytext); BEGIN DFA; }
<DFA>"("[^)]+")" {
/* Process macro arguments */
stb_args(yytext);
BEGIN DFV;
}
<DFV>[ \t]+ { /* Ignore initial spaces */ }
<DFV>[.]*$ {/* Parse to end of line, get value for our define */
// printf("End of define, value = \'%s\'\n", yytext);
<DFV>[^ \t\n]* {/* Parse to end of line, get value for our define */
stb_macro(yytext);
}
<DFV>[ \t]*((;|"//").*)?$ {/* Parse to end of line, get value for our define */
yy_pop_state();
}
<DFV>[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
fprintf(yyout, "\n"); mark_line(); yylineno++;
stb_macro("\n");
}
<INITIAL,INDEF>^[ \t]+.[dD][aA][tT][aA]? { fprintf(yyout, "\tWORD"); }
<INITIAL,INDEF>^"#defcont"[ \t]+ { yy_push_state(DFV); }
<INITIAL,INDEF>^"#ifdef"[ \t]* { ndef = 0; yy_push_state(IFDEFV); }
<INITIAL,INDEF>^"#ifndef"[ \t]* { ndef = 1; yy_push_state(IFDEFV); }
<IFDEFV>{ID} {
<IFDEFV>{IDDOT} {
bool known = stb_isdefined(yytext);
if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
BEGIN INDEF_EOL;
124,8 → 131,8
<INDEF_EOL>(;|"//").*$ { BEGIN INDEF; }
<INNOTDEF_EOL>[ \t]*$ { BEGIN INNOTDEF; }
<INNOTDEF_EOL>(;|"//").*$ { BEGIN INNOTDEF; }
<INDEF_EOL>[^ \t\n].*$ { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); }
<INNOTDEF_EOL>[^ \t\n].*$ { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); }
<INDEF_EOL>[^ \t\n].*$ { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); mark_line(); }
<INNOTDEF_EOL>[^ \t\n].*$ { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); mark_line(); }
<INDEF,NVRDEF>^"#else"[ \t]*((;|"//").*)?$ { BEGIN NODEF; }
<INNOTDEF>^"#else"[ \t]*((;|"//").*)?$ { BEGIN INDEF; }
<INNOTDEF>(.*) { }
133,6 → 140,14
<NODEF,INDEF,INNOTDEF>^"#endif"[ \t]*((;|"//").*)?$ { yy_pop_state(); }
<NODEF,INDEF,INNOTDEF>^"#endif"[ \t]*"/*" { BEGIN COMMENT; }
<*>^"#endif"[ \t]* { fprintf(stderr, "ERR: Unknown endif!!\n");}
<INITIAL,INDEF>^"#struct"[ \t]* {
yy_push_state(GETSTRUCTID); structno = 0; }
<GETSTRUCTID>{ID}/[ \t\n;/] { BEGIN INSTRUCT;
structid = strdup(yytext);
}
<INSTRUCT>{ID}("."{ID})* {
fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
<INSTRUCT>^"#endstruct".*$ { yy_pop_state(); }
/* Not yet: ^"#struct"[ \t]* {} */
/* Not yet: ^"#endstruct"[ \t]* {} */
/* Not yet: ^"#seg"[ \t]* {} */
143,16 → 158,13
else
fprintf(yyout, "%s", yytext);
}
<*>^[ \t]*"//".*$ { /* Ignore comment only lines */ }
<*>^[ \t]*";".*$ { /* Ignore comment only lines */ }
<*>"//".* { /* Ignore trailing comments */ }
<*>";".* { /* Ignore trailing comments */ }
<*>[ \t]*"//".*$ { /* Ignore (trailing) comment only lines */ }
<*>[ \t]*";".*$ { /* Ignore (trailing) comment only lines */ }
<*>"/*" { yy_push_state(COMMENT); }
<*>[ \t]+ { ECHO; }
<INITIAL,INDEF>[.]* { ECHO; }
<*>\n { ECHO; }
<*>\n { ECHO; yylineno++; mark_line(); }
/* <*>. { printf("Unmatched \'%c\'\n", yytext[0]); } */
/* <<EOF>> { printf("EOF!\n"); } */
<<EOF>> { if (end_of_file()) yyterminate(); }
 
%%
 
195,7 → 207,7
return *this;
}
 
std::string getdefn(void) {
const std::string &getdefn(void) {
return m_value;
}
};
303,7 → 315,118
return syms.getdefn(str);
}
 
class BUFSTACK {
public:
FILE *m_fp;
char *m_fname;
int m_lineno;
BUFSTACK *m_prev;
YY_BUFFER_STATE m_bs;
 
static BUFSTACK *curbs;
static const char *curfilename;
 
BUFSTACK(void) {
m_fp = stdin;
 
if (curbs)
curbs->m_lineno = yylineno;
m_prev = curbs;
// m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
m_fname = strdup("(stdin)");
// yy_switch_to_buffer(m_bs);
m_bs = NULL;
curbs = this;
m_lineno = 1;
curfilename = m_fname;
 
yyrestart(m_fp);
yylineno = 1;
}
 
BUFSTACK(const char *fname) {
m_fp = fopen(fname, "r");
if (!m_fp) {
fprintf(stderr, "Cannot open %s\n", fname);
perror("O/S Err:");
exit(-1);
}
 
if (curbs)
curbs->m_lineno = yylineno;
m_prev = curbs;
m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
m_fname = strdup(fname);
yy_switch_to_buffer(m_bs);
curbs = this;
m_lineno = 1;
curfilename = m_fname;
 
yyrestart(m_fp);
yylineno = 1;
}
 
~BUFSTACK(void) {
// fprintf(stderr, "DELETING(%s)\n", m_fname);
fclose(m_fp);
free(m_fname);
if (m_bs)
yy_delete_buffer(m_bs);
curbs = m_prev;
 
if (curbs) {
yy_switch_to_buffer(curbs->m_bs);
yylineno = curbs->m_lineno;
curfilename = curbs->m_fname;
}
}
 
void mark(void) {
FILE *fp = yyout;
if (!fp) fp = stdout;
fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
}
 
static void pop(void) {
// fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
if (curbs)
delete curbs;
}
};
 
BUFSTACK *BUFSTACK::curbs = NULL;
const char *BUFSTACK::curfilename = NULL;
 
int last_marked_line = -1;
const char *last_marked_file = NULL;
void mark_line(void) {
if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
BUFSTACK::curbs->mark();
last_marked_line = yylineno;
last_marked_file = BUFSTACK::curfilename;
}
 
int end_of_file(void) {
BUFSTACK::pop();
return (BUFSTACK::curbs == NULL);
}
 
void pushf(const char *fname) {
BUFSTACK *bs = new BUFSTACK(fname);
}
 
int main(int argc, char **argv) {
yylex();
yylineno = 1;
if (argc < 2) { // Stdin only
BUFSTACK::curbs = new BUFSTACK();
yylex();
} else {
for(int argn=1; argn<argc; argn++) {
BUFSTACK *bs = new BUFSTACK(argv[argn]);
mark_line();
yylex();
// delete bs;
}
}
}
 
/zasm.l
51,7 → 51,7
 
%%
 
^"#line"[ \t]+[0-9]+[ \t]+["][^"]*["][ \t]*$ {
^"#line"[ \t]+[0-9]+[ \t]+[\"][^"]*[\"][ \t]*$ {
yylineno = atoi(&yytext[6]);
char *bg, *en;
bg = strchr(yytext, '\"')+1;

powered by: WebSVN 2.1.0

© copyright 1999-2022 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.