#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
|
|
|
|
typedef enum {enc_none,enc_reg,enc_rir,enc_cjmp,enc_nir,enc_store,enc_rnn,enc_in,enc_out} ENCODING;
|
typedef enum {enc_none,enc_reg,enc_rir,enc_cjmp,enc_nir,enc_store,enc_rnn,enc_in,enc_out} ENCODING;
|
struct encentry { ENCODING enc;int opcode;char *name;}; //if name==NULL ==>end of list
|
struct encentry { ENCODING enc;int opcode;char *name;}; //if name==NULL ==>end of list
|
struct instr_reg { unsigned opcode:6; unsigned rA:5,rB:5,rC:5;unsigned _pad:11;};
|
struct instr_reg { unsigned opcode:6; unsigned rA:5,rB:5,rC:5;unsigned _pad:11;};
|
struct instr_cjmp { unsigned opcode:6; unsigned rA:5,rB:5; signed offset:16;};
|
struct instr_cjmp { unsigned opcode:6; unsigned rA:5,rB:5; signed offset:16;};
|
struct instr_rir { unsigned opcode:6; unsigned rA:5,con_0_4:5,rC:5; unsigned con_5_15:11;};
|
struct instr_rir { unsigned opcode:6; unsigned rA:5,con_0_4:5,rC:5; unsigned con_5_15:11;};
|
struct instr_store { unsigned opcode:6; unsigned rA:5,rB:5; signed offset:16;};
|
struct instr_store { unsigned opcode:6; unsigned rA:5,rB:5; signed offset:16;};
|
struct instr_io { unsigned opcode:6; unsigned rA:5,rB:5,rC:5;unsigned auxcode:6; unsigned _pad:5;};
|
struct instr_io { unsigned opcode:6; unsigned rA:5,rB:5,rC:5;unsigned auxcode:6; unsigned _pad:5;};
|
|
|
typedef union {struct instr_reg reg; struct instr_cjmp cjmp; struct instr_rir rir; struct instr_store store; struct instr_io io; unsigned int code; } instruction;
|
typedef union {struct instr_reg reg; struct instr_cjmp cjmp; struct instr_rir rir; struct instr_store store; struct instr_io io; unsigned int code; } instruction;
|
|
|
typedef enum {reloc_cjmp,reloc_li,reloc_lih,reloc_imm,reloc_store} RELOCTYPE;
|
typedef enum {reloc_cjmp,reloc_li,reloc_lih,reloc_imm,reloc_store} RELOCTYPE;
|
struct reloc {struct reloc *next;int offset; int section; RELOCTYPE type;};
|
struct reloc {struct reloc *next;int offset; int section; RELOCTYPE type;};
|
struct label {char *name; int offset; int section; int resolved; struct reloc *reloc;};
|
struct label {char *name; int offset; int section; int resolved; struct reloc *reloc;};
|
struct label_array {int size; int count; struct label *labels;} labels[256];
|
struct label_array {int size; int count; struct label *labels;} labels[256];
|
int labelcount=0,labelsize=1024;
|
int labelcount=0,labelsize=1024;
|
|
|
int currentsection=0;
|
int currentsection=0;
|
instruction *program;
|
instruction *program;
|
int instrcount=0,programlength=1024;
|
int instrcount=0,programlength=1024;
|
|
|
struct section { int length;int size; char *data; unsigned int addr; } sectn[3];
|
struct section { int length;int size; char *data; unsigned int addr; } sectn[3];
|
|
|
char *reallocz(char * addr, int old_size, int new_size)
|
char *reallocz(char * addr, int old_size, int new_size)
|
{
|
{
|
char * rtn;
|
char * rtn;
|
rtn=realloc(addr,new_size);
|
rtn=realloc(addr,new_size);
|
if (old_size<new_size) memset(rtn+old_size,0,new_size-old_size);
|
if (old_size<new_size) memset(rtn+old_size,0,new_size-old_size);
|
return rtn;
|
return rtn;
|
}
|
}
|
|
|
int findhash(char *name)
|
int findhash(char *name)
|
{
|
{
|
int hash=0,i;
|
int hash=0,i;
|
for (i=0; name[i]!=0; i++) hash=hash+name[i];
|
for (i=0; name[i]!=0; i++) hash=hash+name[i];
|
return hash & 0xff;
|
return hash & 0xff;
|
}
|
}
|
|
|
int findlabel(char *name)
|
int findlabel(char *name)
|
{
|
{
|
int hash,i,found=0;
|
int hash,i,found=0;
|
hash=findhash(name);
|
hash=findhash(name);
|
for(i=0;i<labels[hash].count;i++)
|
for(i=0;i<labels[hash].count;i++)
|
{
|
{
|
if(strcmp(name,labels[hash].labels[i].name)==0)
|
if(strcmp(name,labels[hash].labels[i].name)==0)
|
{
|
{
|
found=1;
|
found=1;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
if (found) return i;
|
if (found) return i;
|
else return -1;
|
else return -1;
|
}
|
}
|
|
|
int addlabel(char *name, int offset, int section,int resolved)
|
int addlabel(char *name, int offset, int section,int resolved)
|
{
|
{
|
char *szname;
|
char *szname;
|
int hash,labelcount,i;
|
int hash,labelcount,i;
|
hash=findhash(name);
|
hash=findhash(name);
|
i=findlabel(name);
|
i=findlabel(name);
|
if (i>=0)
|
if (i>=0)
|
{
|
{
|
if (resolved)
|
if (resolved)
|
{
|
{
|
labels[hash].labels[i].offset=offset;
|
labels[hash].labels[i].offset=offset;
|
labels[hash].labels[i].section=section;
|
labels[hash].labels[i].section=section;
|
labels[hash].labels[i].resolved=1;
|
labels[hash].labels[i].resolved=1;
|
}
|
}
|
return i;
|
return i;
|
}
|
}
|
else
|
else
|
{
|
{
|
if (labels[hash].count>=labels[hash].size)
|
if (labels[hash].count>=labels[hash].size)
|
{
|
{
|
labels[hash].labels=reallocz(labels[hash].labels,labels[hash].size,labels[hash].size*2);
|
labels[hash].labels=reallocz(labels[hash].labels,labels[hash].size,labels[hash].size*2);
|
labels[hash].size=labels[hash].size*2;
|
labels[hash].size=labels[hash].size*2;
|
}
|
}
|
szname=malloc(strlen(name)+1);
|
szname=malloc(strlen(name)+1);
|
strcpy(szname,name);
|
strcpy(szname,name);
|
labelcount=labels[hash].count;
|
labelcount=labels[hash].count;
|
labels[hash].labels[labelcount].name=szname;
|
labels[hash].labels[labelcount].name=szname;
|
labels[hash].labels[labelcount].offset=offset;
|
labels[hash].labels[labelcount].offset=offset;
|
labels[hash].labels[labelcount].section=section;
|
labels[hash].labels[labelcount].section=section;
|
labels[hash].labels[labelcount].resolved=resolved;
|
labels[hash].labels[labelcount].resolved=resolved;
|
labels[hash].labels[labelcount].reloc=NULL;
|
labels[hash].labels[labelcount].reloc=NULL;
|
labels[hash].count++;
|
labels[hash].count++;
|
return labelcount;
|
return labelcount;
|
}
|
}
|
}
|
}
|
|
|
void addreloc(char *labelname, int offset, int section, RELOCTYPE type)
|
void addreloc(char *labelname, int offset, int section, RELOCTYPE type)
|
{
|
{
|
int i,hash;
|
int i,hash;
|
struct reloc *myreloc;
|
struct reloc *myreloc;
|
hash=findhash(labelname);
|
hash=findhash(labelname);
|
i=addlabel(labelname,0,0,0);
|
i=addlabel(labelname,0,0,0);
|
myreloc=malloc(sizeof(struct reloc));
|
myreloc=malloc(sizeof(struct reloc));
|
myreloc->next=labels[hash].labels[i].reloc;
|
myreloc->next=labels[hash].labels[i].reloc;
|
myreloc->offset=offset;
|
myreloc->offset=offset;
|
myreloc->section=section;
|
myreloc->section=section;
|
myreloc->type=type;
|
myreloc->type=type;
|
labels[hash].labels[i].reloc=myreloc;
|
labels[hash].labels[i].reloc=myreloc;
|
|
|
}
|
}
|
|
|
void apply_relocs()
|
void apply_relocs()
|
{
|
{
|
int i,hash;
|
int i,hash;
|
struct reloc *rel;
|
struct reloc *rel;
|
instruction instr;
|
instruction instr;
|
unsigned short int sh_a;
|
unsigned short int sh_a;
|
|
|
for (hash=0;hash<=255;hash++)
|
for (hash=0;hash<=255;hash++)
|
{
|
{
|
for (i=0; i<labels[hash].count; i++)
|
for (i=0; i<labels[hash].count; i++)
|
{
|
{
|
rel=labels[hash].labels[i].reloc;
|
rel=labels[hash].labels[i].reloc;
|
while (rel!=NULL)
|
while (rel!=NULL)
|
{
|
{
|
switch(rel->type)
|
switch(rel->type)
|
{
|
{
|
case reloc_cjmp:
|
case reloc_cjmp:
|
if (rel->section==0)
|
if (rel->section==0)
|
{
|
{
|
if (labels[hash].labels[i].section==0)
|
if (labels[hash].labels[i].section==0)
|
{
|
{
|
((short *) sectn[0].data)[(rel->offset+2)/2]=(labels[hash].labels[i].offset-rel->offset)/4;
|
((short *) sectn[0].data)[(rel->offset+2)/2]=(labels[hash].labels[i].offset-rel->offset)/4;
|
}
|
}
|
}
|
}
|
break;
|
break;
|
case reloc_li:
|
case reloc_li:
|
if (rel->section==0)
|
if (rel->section==0)
|
{
|
{
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset);
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset);
|
// fprintf(stderr,"*** reloc lc %s, sh_a=%i\n",labels[hash].labels[i].name,sh_a);
|
// fprintf(stderr,"*** reloc lc %s, sh_a=%i\n",labels[hash].labels[i].name,sh_a);
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_5_15=sh_a >> 5;
|
instr.rir.con_5_15=sh_a >> 5;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
}
|
}
|
break;
|
break;
|
case reloc_lih:
|
case reloc_lih:
|
if (rel->section==0)
|
if (rel->section==0)
|
{
|
{
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) >> 16;
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) >> 16;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_5_15=sh_a >> 5;
|
instr.rir.con_5_15=sh_a >> 5;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
}
|
}
|
break;
|
break;
|
case reloc_imm:
|
case reloc_imm:
|
if (rel->section==0)
|
if (rel->section==0)
|
{
|
{
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) >> 16;
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) >> 16;
|
instr.store.offset=sh_a;
|
instr.store.offset=sh_a;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
}
|
}
|
break;
|
break;
|
case reloc_store:
|
case reloc_store:
|
if (rel->section==0)
|
if (rel->section==0)
|
{
|
{
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
instr=((instruction *) sectn[0].data)[rel->offset/4];
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) & 0xffff;
|
sh_a=(sectn[labels[hash].labels[i].section].addr+labels[hash].labels[i].offset) & 0xffff;
|
//fprintf(stderr,"*** reloc lc %s, sh_a=%i\n",labels[hash].labels[i].name,sh_a);
|
//fprintf(stderr,"*** reloc lc %s, sh_a=%i\n",labels[hash].labels[i].name,sh_a);
|
instr.store.offset=sh_a;
|
instr.store.offset=sh_a;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
((instruction *) sectn[0].data)[rel->offset/4]=instr;
|
}
|
}
|
break;
|
break;
|
|
|
}
|
}
|
rel=rel->next;
|
rel=rel->next;
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
int main()
|
int main()
|
{
|
{
|
int chr;
|
int chr;
|
int toplevel=1;
|
int toplevel=1;
|
int tokenchars=0;
|
int tokenchars=0;
|
char token[256];
|
char token[256];
|
int tokencount=0;
|
int tokencount=0;
|
char tokens[16][256];
|
char tokens[16][256];
|
int i,a,b,c,d;
|
int i,a,b,c,d;
|
signed short sh_a;
|
signed short sh_a;
|
int comma=0;
|
int comma=0;
|
int PC=0;
|
int PC=0;
|
int nexttoken=0;
|
int nexttoken=0;
|
int error=0;
|
int error=0;
|
int inquote=0;
|
int inquote=0;
|
int incomment=0;
|
int incomment=0;
|
int lineno=0;
|
int lineno=0;
|
|
|
instruction instr;
|
instruction instr;
|
instruction extraInstr;
|
instruction extraInstr;
|
int hasExtraInstr=0;
|
int hasExtraInstr=0;
|
|
|
static struct encentry instrset[]={
|
static struct encentry instrset[]={
|
{enc_rir,0,"lih"},
|
{enc_rir,0,"lih"},
|
{enc_rir,0,"lch"},
|
{enc_rir,0,"lch"},
|
{enc_nir,1,"li"},
|
{enc_nir,1,"li"},
|
{enc_nir,1,"lc"},
|
{enc_nir,1,"lc"},
|
{enc_reg,2,"and"},
|
{enc_reg,2,"and"},
|
{enc_rir,3,"andi"},
|
{enc_rir,3,"andi"},
|
{enc_reg,4,"or"},
|
{enc_reg,4,"or"},
|
{enc_rir,5,"ori"},
|
{enc_rir,5,"ori"},
|
{enc_reg,6,"xor"},
|
{enc_reg,6,"xor"},
|
{enc_rir,7,"xori"},
|
{enc_rir,7,"xori"},
|
{enc_reg,8,"add"},
|
{enc_reg,8,"add"},
|
{enc_rir,9,"addi"},
|
{enc_rir,9,"addi"},
|
{enc_reg,10,"sub"},
|
{enc_reg,10,"sub"},
|
{enc_rir,11,"subi"},
|
{enc_rir,11,"subi"},
|
{enc_none,12,"nop"},
|
{enc_none,12,"nop"},
|
{enc_rir,13,"andi1"},
|
{enc_rir,13,"andi1"},
|
{ enc_reg, 14, "shl" },
|
{ enc_reg, 14, "shl" },
|
{ enc_rir, 15, "shli" },
|
{ enc_rir, 15, "shli" },
|
{ enc_reg, 16, "shr" },
|
{ enc_reg, 16, "shr" },
|
{ enc_rir, 17, "shri" },
|
{ enc_rir, 17, "shri" },
|
{ enc_reg, 18, "sar" },
|
{ enc_reg, 18, "sar" },
|
{ enc_rir, 19, "sari" },
|
{ enc_rir, 19, "sari" },
|
{ enc_in, 0, "inb" },
|
{ enc_in, 0, "inb" },
|
{enc_in,1,"inw"},
|
{enc_in,1,"inw"},
|
{enc_in,2,"inl"},
|
{enc_in,2,"inl"},
|
{enc_out,4,"outb"},
|
{enc_out,4,"outb"},
|
{enc_out,5,"outw"},
|
{enc_out,5,"outw"},
|
{enc_out,6,"outl"},
|
{enc_out,6,"outl"},
|
{enc_cjmp,32,"cjule"},
|
{enc_cjmp,32,"cjule"},
|
{enc_cjmp,32,"cjc"},
|
{enc_cjmp,32,"cjc"},
|
{enc_cjmp,33,"cjugt"},
|
{enc_cjmp,33,"cjugt"},
|
{enc_cjmp,33,"cjnc"},
|
{enc_cjmp,33,"cjnc"},
|
{enc_cjmp,34,"cjeq"},
|
{enc_cjmp,34,"cjeq"},
|
{enc_cjmp,35,"cjne"},
|
{enc_cjmp,35,"cjne"},
|
{enc_cjmp,36,"cjult"},
|
{enc_cjmp,36,"cjult"},
|
{enc_cjmp,37,"cjuge"},
|
{enc_cjmp,37,"cjuge"},
|
{enc_cjmp,38,"cjn"},
|
{enc_cjmp,38,"cjn"},
|
{enc_cjmp,39,"cjnn"},
|
{enc_cjmp,39,"cjnn"},
|
{enc_cjmp,40,"cjslt"},
|
{enc_cjmp,40,"cjslt"},
|
{enc_cjmp,41,"cjsge"},
|
{enc_cjmp,41,"cjsge"},
|
{enc_cjmp,42,"cjsle"},
|
{enc_cjmp,42,"cjsle"},
|
{enc_cjmp,43,"cjsgt"},
|
{enc_cjmp,43,"cjsgt"},
|
{enc_cjmp,44,"cjo"},
|
{enc_cjmp,44,"cjo"},
|
{enc_cjmp,45,"cjno"},
|
{enc_cjmp,45,"cjno"},
|
{enc_rir,46,"call"},
|
{enc_rir,46,"call"},
|
{enc_rnn,47,"ret"},
|
{enc_rnn,47,"ret"},
|
{enc_rir,56,"ldl"},
|
{enc_rir,56,"ldl"},
|
{enc_rir,57,"ldw"},
|
{enc_rir,57,"ldw"},
|
{enc_rir,58,"ldb"},
|
{enc_rir,58,"ldb"},
|
{enc_store,60,"stl"},
|
{enc_store,60,"stl"},
|
{enc_store,61,"stw"},
|
{enc_store,61,"stw"},
|
{enc_store,62,"stb"},
|
{enc_store,62,"stb"},
|
{enc_none,-1,NULL}
|
{enc_none,-1,NULL}
|
};
|
};
|
|
|
//program=malloc(programlength*(sizeof(instruction)));
|
//program=malloc(programlength*(sizeof(instruction)));
|
for (i=0; i<=255; i++)
|
for (i=0; i<=255; i++)
|
{
|
{
|
labels[i].count=0;
|
labels[i].count=0;
|
labels[i].size=16;
|
labels[i].size=16;
|
labels[i].labels=malloc(16*(sizeof(struct label)));
|
labels[i].labels=malloc(16*(sizeof(struct label)));
|
memset(labels[i].labels,0,16*(sizeof(struct label)));
|
memset(labels[i].labels,0,16*(sizeof(struct label)));
|
}
|
}
|
|
|
for (i=0; i<=2; i++)
|
for (i=0; i<=2; i++)
|
{
|
{
|
sectn[i].length=0;
|
sectn[i].length=0;
|
sectn[i].addr=0;
|
sectn[i].addr=0;
|
sectn[i].size=4096;
|
sectn[i].size=4096;
|
sectn[i].data=malloc(4096);
|
sectn[i].data=malloc(4096);
|
memset(sectn[i].data,0,4096);
|
memset(sectn[i].data,0,4096);
|
}
|
}
|
|
|
while(1)
|
while(1)
|
{
|
{
|
chr=fgetc(stdin);
|
chr=fgetc(stdin);
|
if (chr==';') {incomment=1; continue; }
|
if (chr==';') {incomment=1; continue; }
|
if (incomment) goto handle_newline_eof;
|
if (incomment) goto handle_newline_eof;
|
if (chr=='"')
|
if (chr=='"')
|
{
|
{
|
if (!inquote) {inquote=1; continue;}
|
if (!inquote) {inquote=1; continue;}
|
else { inquote=0; continue;}
|
else { inquote=0; continue;}
|
}
|
}
|
if (inquote && (chr!='\n') && (chr!=EOF))
|
if (inquote && (chr!='\n') && (chr!=EOF))
|
{
|
{
|
if (toplevel) {token[0]=chr;tokenchars=1;toplevel=0;} else {token[tokenchars]=chr;tokenchars++;}
|
if (toplevel) {token[0]=chr;tokenchars=1;toplevel=0;} else {token[tokenchars]=chr;tokenchars++;}
|
continue;
|
continue;
|
}
|
}
|
if ((chr>='a' && chr<='z')||(chr>='A' && chr<='Z')||(chr>='0' && chr<='9')||(chr=='_'))
|
if ((chr>='a' && chr<='z')||(chr>='A' && chr<='Z')||(chr>='0' && chr<='9')||(chr=='_'))
|
{
|
{
|
comma=0;
|
comma=0;
|
if (toplevel) {token[0]=chr;tokenchars=1;toplevel=0;} else {token[tokenchars]=chr;tokenchars++;}
|
if (toplevel) {token[0]=chr;tokenchars=1;toplevel=0;} else {token[tokenchars]=chr;tokenchars++;}
|
}
|
}
|
if (chr==' ' || chr=='\t')
|
if (chr==' ' || chr=='\t')
|
{if (toplevel) {} else
|
{if (toplevel) {} else
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
tokens[tokencount][tokenchars]=0;
|
tokens[tokencount][tokenchars]=0;
|
tokencount++;
|
tokencount++;
|
toplevel=1;
|
toplevel=1;
|
}
|
}
|
}
|
}
|
if (chr==',' || chr==':') //single character tokens
|
if (chr==',' || chr==':') //single character tokens
|
{if (toplevel) {tokens[tokencount][0]=chr;tokens[tokencount][1]=0;tokencount++;} else
|
{if (toplevel) {tokens[tokencount][0]=chr;tokens[tokencount][1]=0;tokencount++;} else
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
tokens[tokencount][tokenchars]=0;
|
tokens[tokencount][tokenchars]=0;
|
tokencount++;
|
tokencount++;
|
tokens[tokencount][0]=chr;tokens[tokencount][1]=0;tokencount++;
|
tokens[tokencount][0]=chr;tokens[tokencount][1]=0;tokencount++;
|
toplevel=1;
|
toplevel=1;
|
}
|
}
|
}
|
}
|
/* if (chr==':')
|
/* if (chr==':')
|
{if (toplevel) {tokens[tokencount][0]=':';tokens[tokencount][1]=0;tokencount++;} else
|
{if (toplevel) {tokens[tokencount][0]=':';tokens[tokencount][1]=0;tokencount++;} else
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
tokens[tokencount][tokenchars]=0;
|
tokens[tokencount][tokenchars]=0;
|
tokencount++;
|
tokencount++;
|
tokens[tokencount][0]=':';tokens[tokencount][1]=0;tokencount++;
|
tokens[tokencount][0]=':';tokens[tokencount][1]=0;tokencount++;
|
toplevel=1;
|
toplevel=1;
|
}
|
}
|
}*/
|
}*/
|
handle_newline_eof:
|
handle_newline_eof:
|
if (chr=='\n' || chr==EOF)
|
if (chr=='\n' || chr==EOF)
|
{
|
{
|
incomment=0;
|
incomment=0;
|
lineno++;
|
lineno++;
|
if (toplevel) {} else
|
if (toplevel) {} else
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
{for(i=0;i<tokenchars;i++) tokens[tokencount][i]=token[i];
|
tokens[tokencount][tokenchars]=0;
|
tokens[tokencount][tokenchars]=0;
|
tokencount++;
|
tokencount++;
|
toplevel=1;
|
toplevel=1;
|
}
|
}
|
//for (i=0;i<tokencount;i++) printf ("t%i:%s\n",i,&tokens[i][0]);
|
//for (i=0;i<tokencount;i++) printf ("t%i:%s\n",i,&tokens[i][0]);
|
nexttoken=0;
|
nexttoken=0;
|
if (tokencount>=2 && !strcmp(tokens[1],":"))
|
if (tokencount>=2 && !strcmp(tokens[1],":"))
|
{addlabel(tokens[0],sectn[currentsection].length,currentsection,1);
|
{addlabel(tokens[0],sectn[currentsection].length,currentsection,1);
|
nexttoken=2;
|
nexttoken=2;
|
}
|
}
|
//i=0;
|
//i=0;
|
error=1;
|
error=1;
|
if (tokencount<=nexttoken) error=0;
|
if (tokencount<=nexttoken) error=0;
|
//printf("eRror %i\n",error);
|
//printf("eRror %i\n",error);
|
if (tokencount>nexttoken+1)
|
if (tokencount>nexttoken+1)
|
{
|
{
|
if (!strcmp(tokens[nexttoken],"section"))
|
if (!strcmp(tokens[nexttoken],"section"))
|
{
|
{
|
if (!strcmp(tokens[nexttoken+1],"code")) {error=0;currentsection=0;}
|
if (!strcmp(tokens[nexttoken+1],"code")) {error=0;currentsection=0;}
|
if (!strcmp(tokens[nexttoken+1],"data")) {error=0;currentsection=1;}
|
if (!strcmp(tokens[nexttoken+1],"data")) {error=0;currentsection=1;}
|
if (!strcmp(tokens[nexttoken+1],"bss" )) {error=0;currentsection=2;}
|
if (!strcmp(tokens[nexttoken+1],"bss" )) {error=0;currentsection=2;}
|
}
|
}
|
|
|
if (!strcmp(tokens[nexttoken],"align"))
|
if (!strcmp(tokens[nexttoken],"align"))
|
{
|
{
|
if (sscanf(tokens[nexttoken+1],"0x%x",&a)==1)
|
if (sscanf(tokens[nexttoken+1],"0x%x",&a)==1)
|
error=0;
|
error=0;
|
else if (sscanf(tokens[nexttoken+1],"%i",&a)==1)
|
else if (sscanf(tokens[nexttoken+1],"%i",&a)==1)
|
error=0;
|
error=0;
|
if (error==0)
|
if (error==0)
|
{
|
{
|
error=1;
|
error=1;
|
b=0xffffffff;
|
b=0xffffffff;
|
for (i=0; (i<7) && (a!=1<<i); i++)
|
for (i=0; (i<7) && (a!=1<<i); i++)
|
{
|
{
|
b=b<<1;
|
b=b<<1;
|
}
|
}
|
if (i<7)
|
if (i<7)
|
{
|
{
|
if (((sectn[currentsection].length+(1<<i)-1) & b) > sectn[currentsection].size)
|
if (((sectn[currentsection].length+(1<<i)-1) & b) > sectn[currentsection].size)
|
{
|
{
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].size+=sectn[currentsection].size;
|
sectn[currentsection].size+=sectn[currentsection].size;
|
}
|
}
|
sectn[currentsection].length=(sectn[currentsection].length+(1<<i)-1) & b;
|
sectn[currentsection].length=(sectn[currentsection].length+(1<<i)-1) & b;
|
error=0;
|
error=0;
|
}
|
}
|
}
|
}
|
|
|
}
|
}
|
|
|
if (!strcmp(tokens[nexttoken],"DS"))
|
if (!strcmp(tokens[nexttoken],"DS"))
|
{
|
{
|
for (i=0; i<strlen(tokens[nexttoken+1]); i++)
|
for (i=0; i<strlen(tokens[nexttoken+1]); i++)
|
{
|
{
|
//if (sscanf(tokens[i],"%i",&a)!=1)
|
//if (sscanf(tokens[i],"%i",&a)!=1)
|
// sscanf(tokens[i],"0x%x",&a);
|
// sscanf(tokens[i],"0x%x",&a);
|
a=tokens[nexttoken+1][i];
|
a=tokens[nexttoken+1][i];
|
if (sectn[currentsection].length>=sectn[currentsection].size)
|
if (sectn[currentsection].length>=sectn[currentsection].size)
|
{
|
{
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].size+=sectn[currentsection].size;
|
sectn[currentsection].size+=sectn[currentsection].size;
|
}
|
}
|
sectn[currentsection].data[sectn[currentsection].length]=a;
|
sectn[currentsection].data[sectn[currentsection].length]=a;
|
sectn[currentsection].length++;
|
sectn[currentsection].length++;
|
}
|
}
|
error=0;
|
error=0;
|
} //DS
|
} //DS
|
|
|
if (!strcmp(tokens[nexttoken],"DB"))
|
if (!strcmp(tokens[nexttoken],"DB"))
|
{
|
{
|
for (i=nexttoken+1; i<tokencount; i++)
|
for (i=nexttoken+1; i<tokencount; i++)
|
{
|
{
|
if (sscanf(tokens[i],"0x%x",&a)!=1)
|
if (sscanf(tokens[i],"0x%x",&a)!=1)
|
sscanf(tokens[i],"%i",&a);
|
sscanf(tokens[i],"%i",&a);
|
if (sectn[currentsection].length>=sectn[currentsection].size)
|
if (sectn[currentsection].length>=sectn[currentsection].size)
|
{
|
{
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].size+=sectn[currentsection].size;
|
sectn[currentsection].size+=sectn[currentsection].size;
|
}
|
}
|
sectn[currentsection].data[sectn[currentsection].length]=a;
|
sectn[currentsection].data[sectn[currentsection].length]=a;
|
sectn[currentsection].length++;
|
sectn[currentsection].length++;
|
}
|
}
|
error=0;
|
error=0;
|
}
|
}
|
if (!strcmp(tokens[nexttoken],"DW"))
|
if (!strcmp(tokens[nexttoken],"DW"))
|
{
|
{
|
for (i=nexttoken+1; i<tokencount; i++)
|
for (i=nexttoken+1; i<tokencount; i++)
|
{
|
{
|
if (sscanf(tokens[i],"0x%x",&a)!=1)
|
if (sscanf(tokens[i],"0x%x",&a)!=1)
|
sscanf(tokens[i],"%i",&a);
|
sscanf(tokens[i],"%i",&a);
|
if (sectn[currentsection].length+2>sectn[currentsection].size)
|
if (sectn[currentsection].length+2>sectn[currentsection].size)
|
{
|
{
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].size+=sectn[currentsection].size;
|
sectn[currentsection].size+=sectn[currentsection].size;
|
}
|
}
|
*((short int *) &(sectn[currentsection].data[sectn[currentsection].length]))=a;
|
*((short int *) &(sectn[currentsection].data[sectn[currentsection].length]))=a;
|
sectn[currentsection].length+=2;
|
sectn[currentsection].length+=2;
|
}
|
}
|
error=0;
|
error=0;
|
}
|
}
|
if (!strcmp(tokens[nexttoken],"DL"))
|
if (!strcmp(tokens[nexttoken],"DL"))
|
{
|
{
|
for (i=nexttoken+1; i<tokencount; i++)
|
for (i=nexttoken+1; i<tokencount; i++)
|
{
|
{
|
if (sscanf(tokens[i],"0x%x",&a)!=1)
|
if (sscanf(tokens[i],"0x%x",&a)!=1)
|
sscanf(tokens[i],"%i",&a);
|
sscanf(tokens[i],"%i",&a);
|
if (sectn[currentsection].length+4>sectn[currentsection].size)
|
if (sectn[currentsection].length+4>sectn[currentsection].size)
|
{
|
{
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].data=reallocz(sectn[currentsection].data,sectn[currentsection].size,sectn[currentsection].size*2);
|
sectn[currentsection].size+=sectn[currentsection].size;
|
sectn[currentsection].size+=sectn[currentsection].size;
|
}
|
}
|
*((int *) &(sectn[currentsection].data[sectn[currentsection].length]))=a;
|
*((int *) &(sectn[currentsection].data[sectn[currentsection].length]))=a;
|
sectn[currentsection].length+=4;
|
sectn[currentsection].length+=4;
|
|
|
}
|
}
|
error=0;
|
error=0;
|
}
|
}
|
}
|
}
|
|
|
if ((tokencount>nexttoken) && (error==1))
|
if ((tokencount>nexttoken) && (error==1))
|
{
|
{
|
for (i=0;;i++)
|
for (i=0;;i++)
|
{
|
{
|
if (instrset[i].name==NULL) break;
|
if (instrset[i].name==NULL) break;
|
if (strcmp(tokens[nexttoken],instrset[i].name)==0) break;
|
if (strcmp(tokens[nexttoken],instrset[i].name)==0) break;
|
}
|
}
|
instr.code=0;
|
instr.code=0;
|
error=0;
|
error=0;
|
hasExtraInstr=0;
|
hasExtraInstr=0;
|
extraInstr.code=0;
|
extraInstr.code=0;
|
if (instrset[i].name==NULL) error=1;
|
if (instrset[i].name==NULL) error=1;
|
else
|
else
|
switch (instrset[i].enc)
|
switch (instrset[i].enc)
|
{
|
{
|
case enc_none: instr.reg.opcode=instrset[i].opcode; break;
|
case enc_none: instr.reg.opcode=instrset[i].opcode; break;
|
case enc_reg:
|
case enc_reg:
|
if (tokencount-nexttoken==6)
|
if (tokencount-nexttoken==6)
|
{
|
{
|
instr.reg.opcode=instrset[i].opcode;
|
instr.reg.opcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
instr.reg.rA=a;
|
instr.reg.rA=a;
|
instr.reg.rB=b;
|
instr.reg.rB=b;
|
instr.reg.rC=c;
|
instr.reg.rC=c;
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
case enc_rir:
|
case enc_rir:
|
if (tokencount-nexttoken==6)
|
if (tokencount-nexttoken==6)
|
{
|
{
|
instr.rir.opcode=instrset[i].opcode;
|
instr.rir.opcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"0x%x",&d)!=1)
|
if (sscanf(tokens[nexttoken+3],"0x%x",&d)!=1)
|
if (sscanf(tokens[nexttoken+3],"%d",&d)!=1)
|
if (sscanf(tokens[nexttoken+3],"%d",&d)!=1)
|
{
|
{
|
addreloc(tokens[nexttoken+3],sectn[0].length,0,reloc_imm);
|
addreloc(tokens[nexttoken+3],sectn[0].length,0,reloc_imm);
|
addreloc(tokens[nexttoken+3],sectn[0].length+4,0,reloc_li);
|
addreloc(tokens[nexttoken+3],sectn[0].length+4,0,reloc_li);
|
d=0xbaadf00d;
|
d=0xbaadf00d;
|
}
|
}
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
sh_a=(signed short) d;
|
sh_a=(signed short) d;
|
instr.rir.rA=a;
|
instr.rir.rA=a;
|
instr.rir.rC=c;
|
instr.rir.rC=c;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_5_15=sh_a >> 5;
|
instr.rir.con_5_15=sh_a >> 5;
|
if (sh_a!=d)
|
if (sh_a!=d)
|
{
|
{
|
extraInstr.store.opcode=30;
|
extraInstr.store.opcode=30;
|
extraInstr.store.offset=d>>16;
|
extraInstr.store.offset=d>>16;
|
hasExtraInstr=1;
|
hasExtraInstr=1;
|
}
|
}
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
case enc_nir:
|
case enc_nir:
|
if (tokencount-nexttoken==4)
|
if (tokencount-nexttoken==4)
|
{
|
{
|
instr.rir.opcode=instrset[i].opcode;
|
instr.rir.opcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"0x%x",&a)!=1)
|
if (sscanf(tokens[nexttoken+1],"0x%x",&a)!=1)
|
if (sscanf(tokens[nexttoken+1],"%d",&a)!=1)
|
if (sscanf(tokens[nexttoken+1],"%d",&a)!=1)
|
{//li
|
{//li
|
addreloc(tokens[nexttoken+1],sectn[0].length+4,0,reloc_li);
|
addreloc(tokens[nexttoken+1],sectn[0].length+4,0,reloc_li);
|
addreloc(tokens[nexttoken+1],sectn[0].length,0,reloc_imm);
|
addreloc(tokens[nexttoken+1],sectn[0].length,0,reloc_imm);
|
a=0xbaadf00d;
|
a=0xbaadf00d;
|
//printf("reloc\n");
|
//printf("reloc\n");
|
}
|
}
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&c)!=1) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&c)!=1) error=1;
|
sh_a=(signed short) a;
|
sh_a=(signed short) a;
|
instr.rir.rA=0;
|
instr.rir.rA=0;
|
instr.rir.rC=c;
|
instr.rir.rC=c;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_0_4=sh_a & 0x1f;
|
instr.rir.con_5_15=sh_a >> 5;
|
instr.rir.con_5_15=sh_a >> 5;
|
//printf("sh_a=%i,token=%s\n",sh_a,tokens[nexttoken+1]);
|
//printf("sh_a=%i,token=%s\n",sh_a,tokens[nexttoken+1]);
|
if (sh_a!=a)
|
if (sh_a!=a)
|
{
|
{
|
hasExtraInstr=1;
|
hasExtraInstr=1;
|
extraInstr.store.opcode=30;
|
extraInstr.store.opcode=30;
|
extraInstr.store.offset=a>>16;
|
extraInstr.store.offset=a>>16;
|
}
|
}
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
|
|
case enc_cjmp:
|
case enc_cjmp:
|
if (tokencount-nexttoken==6)
|
if (tokencount-nexttoken==6)
|
{
|
{
|
instr.reg.opcode=instrset[i].opcode;
|
instr.reg.opcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
//if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
//if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
instr.cjmp.rA=a;
|
instr.cjmp.rA=a;
|
instr.cjmp.rB=b;
|
instr.cjmp.rB=b;
|
if (error==0) addreloc(tokens[nexttoken+5],sectn[0].length,0,reloc_cjmp);
|
if (error==0) addreloc(tokens[nexttoken+5],sectn[0].length,0,reloc_cjmp);
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
|
|
case enc_store:
|
case enc_store:
|
if (tokencount-nexttoken==6)
|
if (tokencount-nexttoken==6)
|
{
|
{
|
instr.rir.opcode=instrset[i].opcode;
|
instr.rir.opcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"0x%x",&d)!=1)
|
if (sscanf(tokens[nexttoken+3],"0x%x",&d)!=1)
|
if (sscanf(tokens[nexttoken+3],"%d",&d)!=1)
|
if (sscanf(tokens[nexttoken+3],"%d",&d)!=1)
|
{
|
{
|
addreloc(tokens[nexttoken+3],sectn[0].length+4,0,reloc_store);
|
addreloc(tokens[nexttoken+3],sectn[0].length+4,0,reloc_store);
|
addreloc(tokens[nexttoken+3],sectn[0].length,0,reloc_imm);
|
addreloc(tokens[nexttoken+3],sectn[0].length,0,reloc_imm);
|
d=0xbaadf00d;
|
d=0xbaadf00d;
|
}
|
}
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (strcmp(tokens[nexttoken+4],",")) error=1;
|
if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
if (sscanf(tokens[nexttoken+5],"r%d",&c)!=1) error=1;
|
sh_a=(signed short) d;
|
sh_a=(signed short) d;
|
instr.cjmp.rA=a;
|
instr.cjmp.rA=a;
|
instr.cjmp.rB=c;
|
instr.cjmp.rB=c;
|
instr.cjmp.offset=sh_a;
|
instr.cjmp.offset=sh_a;
|
if (sh_a!=d)
|
if (sh_a!=d)
|
{
|
{
|
hasExtraInstr=1;
|
hasExtraInstr=1;
|
extraInstr.store.opcode=30;
|
extraInstr.store.opcode=30;
|
extraInstr.store.offset=d>>16;
|
extraInstr.store.offset=d>>16;
|
}
|
}
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
|
|
case enc_rnn:
|
case enc_rnn:
|
if (tokencount-nexttoken==2)
|
if (tokencount-nexttoken==2)
|
{
|
{
|
instr.rir.opcode=instrset[i].opcode;
|
instr.rir.opcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
instr.rir.rA=a;
|
instr.rir.rA=a;
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
|
|
case enc_in:
|
case enc_in:
|
if (tokencount-nexttoken==4)
|
if (tokencount-nexttoken==4)
|
{
|
{
|
instr.io.opcode=31;
|
instr.io.opcode=31;
|
instr.io.auxcode=instrset[i].opcode;
|
instr.io.auxcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
|
|
instr.io.rA=a;
|
instr.io.rA=a;
|
instr.io.rB=0;
|
instr.io.rB=0;
|
instr.io.rC=b;
|
instr.io.rC=b;
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
|
|
case enc_out:
|
case enc_out:
|
if (tokencount-nexttoken==4)
|
if (tokencount-nexttoken==4)
|
{
|
{
|
instr.io.opcode=31;
|
instr.io.opcode=31;
|
instr.io.auxcode=instrset[i].opcode;
|
instr.io.auxcode=instrset[i].opcode;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (sscanf(tokens[nexttoken+1],"r%d",&a)!=1) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (strcmp(tokens[nexttoken+2],",")) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
if (sscanf(tokens[nexttoken+3],"r%d",&b)!=1) error=1;
|
|
|
instr.io.rA=a;
|
instr.io.rA=a;
|
instr.io.rB=b;
|
instr.io.rB=b;
|
instr.io.rC=0;
|
instr.io.rC=0;
|
|
|
}
|
}
|
else error=1;
|
else error=1;
|
break;
|
break;
|
|
|
default: error=1;
|
default: error=1;
|
}
|
}
|
// if (error==0) printf("hexcode %x,name %s\n",instr.code,instrset[i].name);
|
// if (error==0) printf("hexcode %x,name %s\n",instr.code,instrset[i].name);
|
if (error==0)
|
if (error==0)
|
{
|
{
|
// printf("hexcode %x,name %s\n",instr.code,instrset[i].name);
|
// printf("hexcode %x,name %s\n",instr.code,instrset[i].name);
|
if (hasExtraInstr)
|
if (hasExtraInstr)
|
{
|
{
|
if (sectn[0].length+4>sectn[0].size)
|
if (sectn[0].length+4>sectn[0].size)
|
{
|
{
|
sectn[0].data=reallocz(sectn[0].data,sectn[0].size,2*sectn[0].size);
|
sectn[0].data=reallocz(sectn[0].data,sectn[0].size,2*sectn[0].size);
|
sectn[0].size+=sectn[0].size;
|
sectn[0].size+=sectn[0].size;
|
}
|
}
|
((instruction * )sectn[0].data)[(sectn[0].length+3)/4]=extraInstr;
|
((instruction * )sectn[0].data)[(sectn[0].length+3)/4]=extraInstr;
|
PC+=4;
|
PC+=4;
|
sectn[0].length=(sectn[0].length+4+3) & 0xfffffffc;
|
sectn[0].length=(sectn[0].length+4+3) & 0xfffffffc;
|
}
|
}
|
if (sectn[0].length+4>sectn[0].size)
|
if (sectn[0].length+4>sectn[0].size)
|
{
|
{
|
sectn[0].data=reallocz(sectn[0].data,sectn[0].size,2*sectn[0].size);
|
sectn[0].data=reallocz(sectn[0].data,sectn[0].size,2*sectn[0].size);
|
sectn[0].size+=sectn[0].size;
|
sectn[0].size+=sectn[0].size;
|
}
|
}
|
((instruction * )sectn[0].data)[(sectn[0].length+3)/4]=instr;
|
((instruction * )sectn[0].data)[(sectn[0].length+3)/4]=instr;
|
PC+=4;
|
PC+=4;
|
sectn[0].length=(sectn[0].length+4+3) & 0xfffffffc;
|
sectn[0].length=(sectn[0].length+4+3) & 0xfffffffc;
|
}
|
}
|
|
|
} //else error=1;
|
} //else error=1;
|
if (error) printf("error in line %i\n",lineno);
|
if (error) printf("error in line %i\n",lineno);
|
tokencount=0;
|
tokencount=0;
|
if (chr==EOF)
|
if (chr==EOF)
|
{
|
{
|
//printf("EOF\n");
|
//printf("EOF\n");
|
sectn[0].addr=0;//todo:origin
|
sectn[0].addr=0;//todo:origin
|
for (i=1; i<=2; i++)
|
for (i=1; i<=2; i++)
|
sectn[i].addr=(sectn[i-1].addr+sectn[i-1].length+63) & 0xffffffc0;
|
sectn[i].addr=(sectn[i-1].addr+sectn[i-1].length+63) & 0xffffffc0;
|
apply_relocs();
|
apply_relocs();
|
for (a=0; a<=1; a++)
|
for (a=0; a<=1; a++)
|
{
|
{
|
//printf("a=%i\n",a);
|
//printf("a=%i\n",a);
|
for (i=0;i<((sectn[a].length+63)& 0xffffffc0)/4;i++)
|
for (i=0;i<((sectn[a].length+63)& 0xffffffc0)/4;i++)
|
{
|
{
|
printf("%x\n",((unsigned int *) sectn[a].data)[i]);
|
printf("%x\n",((unsigned int *) sectn[a].data)[i]);
|
}
|
}
|
}
|
}
|
exit(0);
|
exit(0);
|
|
|
}
|
}
|
}
|
}
|
|
|
}
|
}
|
|
|
}
|
}
|
|
|