Line 25... |
Line 25... |
//
|
//
|
#include "stdafx.h"
|
#include "stdafx.h"
|
|
|
#define MAX_PASS 6
|
#define MAX_PASS 6
|
|
|
|
FilenameStack fns;
|
|
|
int gCpu = 888;
|
int gCpu = 888;
|
int verbose = 1;
|
int verbose = 1;
|
int debug = 1;
|
int debug = 1;
|
int listing = 1;
|
int listing = 1;
|
int binary_out = 1;
|
int binary_out = 1;
|
Line 56... |
Line 58... |
int64_t code_address;
|
int64_t code_address;
|
int64_t data_address;
|
int64_t data_address;
|
int64_t bss_address;
|
int64_t bss_address;
|
int64_t start_address;
|
int64_t start_address;
|
FILE *ofp, *vfp;
|
FILE *ofp, *vfp;
|
|
std::ofstream mofs;
|
|
|
int regno;
|
int regno;
|
char first_org = 1;
|
char first_org = 1;
|
char current_label[500];
|
char current_label[500];
|
|
|
|
std::string mname;
|
char buf[10000];
|
char buf[10000];
|
char masterFile[10000000];
|
int masterFileLength = 0;
|
|
char *masterFile;
|
char segmentFile[10000000];
|
char segmentFile[10000000];
|
int NumSections = 12;
|
int NumSections = 12;
|
clsElf64Section sections[12];
|
clsElf64Section sections[12];
|
NameTable nmTable;
|
NameTable nmTable;
|
char codebuf[10000000];
|
char codebuf[10000000];
|
Line 113... |
Line 119... |
extern void FT64_processMaster();
|
extern void FT64_processMaster();
|
extern void FT64x36_processMaster();
|
extern void FT64x36_processMaster();
|
extern void SymbolInit();
|
extern void SymbolInit();
|
extern void dsd9_VerilogOut(FILE *fp);
|
extern void dsd9_VerilogOut(FILE *fp);
|
|
|
|
Arg gArgs[12];
|
|
int gArgCount;
|
|
Arglist gArglist;
|
|
|
|
FILE *mfp; // master file pointer
|
|
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
|
|
void displayHelp()
|
void displayHelp()
|
{
|
{
|
Line 1028... |
Line 1040... |
if (gCpu==888 || gCpu==889)
|
if (gCpu==888 || gCpu==889)
|
Table888_bump_address();
|
Table888_bump_address();
|
}
|
}
|
|
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
|
// macro <name> (<arg1, arg2, arg3, ...>)
|
|
// < macro body >
|
|
// endm
|
|
// ---------------------------------------------------------------------------
|
|
|
|
void process_macro()
|
|
{
|
|
SYM *sym;
|
|
Macro *macr;
|
|
bool alreadyDef = false;
|
|
char *p;
|
|
|
|
if (pass == 3) {
|
|
macr = new Macro;
|
|
SkipSpaces();
|
|
getIdentifier();
|
|
sym = find_symbol(lastid);
|
|
if (sym != nullptr) {
|
|
printf("Macro already defined %d.", lineno);
|
|
alreadyDef = true;
|
|
}
|
|
else {
|
|
sym = new_symbol(lastid);
|
|
sym->defined = 1;
|
|
sym->isMacro = true;
|
|
sym->macro = macr;
|
|
}
|
|
NextToken();
|
|
if (token == '(') {
|
|
macr->GetParmList();
|
|
NextToken();
|
|
need(')');
|
|
}
|
|
p = inptr;
|
|
macr->GetBody();
|
|
if (alreadyDef)
|
|
delete macr;
|
|
}
|
|
else if (pass > 3) {
|
|
Macro mthrowaway;
|
|
SkipSpaces();
|
|
getIdentifier();
|
|
NextToken();
|
|
if (token == '(') {
|
|
mthrowaway.GetParmList();
|
|
NextToken();
|
|
need(')');
|
|
}
|
|
mthrowaway.GetBody();
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
void process_message()
|
void process_message()
|
{
|
{
|
char buf[200];
|
char buf[200];
|
int nn;
|
int nn;
|
Line 1217... |
Line 1282... |
void processSegments()
|
void processSegments()
|
{
|
{
|
char *pinptr;
|
char *pinptr;
|
int segment = codeseg;
|
int segment = codeseg;
|
int inComment;
|
int inComment;
|
|
std::string fname;
|
|
bool setname = false;
|
|
|
if (verbose)
|
if (verbose)
|
printf("Processing segments.\r\n");
|
printf("Processing segments.\r\n");
|
inptr = &masterFile[0];
|
inptr = &masterFile[0];
|
pinptr = inptr;
|
pinptr = inptr;
|
codendx = 0;
|
codendx = 0;
|
datandx = 0;
|
datandx = 0;
|
rodatandx = 0;
|
rodatandx = 0;
|
tlsndx = 0;
|
tlsndx = 0;
|
bssndx = 0;
|
bssndx = 0;
|
memset(codebuf,0,sizeof(codebuf));
|
ZeroMemory(codebuf,sizeof(codebuf));
|
memset(databuf,0,sizeof(databuf));
|
ZeroMemory(databuf,sizeof(databuf));
|
memset(rodatabuf,0,sizeof(rodatabuf));
|
ZeroMemory(rodatabuf,sizeof(rodatabuf));
|
memset(tlsbuf,0,sizeof(tlsbuf));
|
ZeroMemory(tlsbuf,sizeof(tlsbuf));
|
memset(bssbuf,0,sizeof(bssbuf));
|
ZeroMemory(bssbuf,sizeof(bssbuf));
|
inComment = 0;
|
inComment = 0;
|
|
|
|
lineno = 1;
|
while (*inptr) {
|
while (*inptr) {
|
|
if (*inptr == '\n')
|
|
lineno++;
|
SkipSpaces();
|
SkipSpaces();
|
if (*inptr==';')
|
if (*inptr==';')
|
goto j1;
|
goto j1;
|
if (inptr[0]=='/' && inptr[1]=='/')
|
if (inptr[0]=='/' && inptr[1]=='/')
|
goto j1;
|
goto j1;
|
Line 1249... |
Line 1319... |
// if (inComment && inptr[0]=='*' && inptr[1]=='/')
|
// if (inComment && inptr[0]=='*' && inptr[1]=='/')
|
// inComment = 0;
|
// inComment = 0;
|
// if (inComment)
|
// if (inComment)
|
// goto j1;
|
// goto j1;
|
if (*inptr=='.') inptr++;
|
if (*inptr=='.') inptr++;
|
if ((_strnicmp(inptr,"code",4)==0) && !isIdentChar(inptr[4])) {
|
if ((_strnicmp(inptr, "file", 4)==0) && !isIdentChar(inptr[4])) {
|
|
inptr += 4;
|
|
NextToken();
|
|
if (token == tk_strconst)
|
|
fname = std::string(laststr);
|
|
else
|
|
fname = std::string("<unknown file>");
|
|
NextToken();
|
|
lineno = expr();
|
|
setname = true;
|
|
}
|
|
else if ((_strnicmp(inptr,"code",4)==0) && !isIdentChar(inptr[4])) {
|
segment = codeseg;
|
segment = codeseg;
|
|
setname = true;
|
}
|
}
|
else if ((_strnicmp(inptr,"data",4)==0) && !isIdentChar(inptr[4])) {
|
else if ((_strnicmp(inptr,"data",4)==0) && !isIdentChar(inptr[4])) {
|
segment = dataseg;
|
segment = dataseg;
|
}
|
}
|
else if ((_strnicmp(inptr,"rodata",6)==0) && !isIdentChar(inptr[6])) {
|
else if ((_strnicmp(inptr,"rodata",6)==0) && !isIdentChar(inptr[6])) {
|
Line 1269... |
Line 1351... |
j1:
|
j1:
|
ScanToEOL();
|
ScanToEOL();
|
inptr++;
|
inptr++;
|
switch(segment) {
|
switch(segment) {
|
case codeseg:
|
case codeseg:
|
|
if (setname) {
|
|
setname = false;
|
|
if (fname.length() > 0) {
|
|
sprintf(&codebuf[codendx], ".file \x22%s\x22,%d\n", fname.c_str(), lineno);
|
|
codendx += strlen(&codebuf[codendx]);
|
|
}
|
|
}
|
strncpy(&codebuf[codendx], pinptr, inptr-pinptr);
|
strncpy(&codebuf[codendx], pinptr, inptr-pinptr);
|
codendx += inptr-pinptr;
|
codendx += inptr-pinptr;
|
break;
|
break;
|
case dataseg:
|
case dataseg:
|
strncpy(&databuf[datandx], pinptr, inptr-pinptr);
|
strncpy(&databuf[datandx], pinptr, inptr-pinptr);
|
Line 1291... |
Line 1380... |
bssndx += inptr-pinptr;
|
bssndx += inptr-pinptr;
|
break;
|
break;
|
}
|
}
|
pinptr = inptr;
|
pinptr = inptr;
|
}
|
}
|
memset(masterFile,0,sizeof(masterFile));
|
ZeroMemory(masterFile,sizeof(masterFile));
|
strcat(masterFile, codebuf);
|
strcat(masterFile, codebuf);
|
strcat(masterFile, rodatabuf);
|
strcat(masterFile, rodatabuf);
|
strcat(masterFile, "\r\n\trodata\r\n");
|
strcat(masterFile, "\r\n\trodata\r\n");
|
strcat(masterFile, "\talign 8\r\n");
|
strcat(masterFile, "\talign 8\r\n");
|
strcat(masterFile, "begin_init_data:\r\n");
|
strcat(masterFile, "begin_init_data:\r\n");
|
Line 1314... |
Line 1403... |
fclose(fp);
|
fclose(fp);
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
|
void ProcessSegments2()
|
|
{
|
|
char buf[1000];
|
|
char *lptr;
|
|
char fname[600];
|
|
bool setname = false;
|
|
|
|
std::ifstream ifs("as64-master.asm");
|
|
std::ofstream codeofs("as64-code.asm");
|
|
std::ofstream dataofs("as64-data.asm");
|
|
std::ofstream idataofs("as64-idata.asm");
|
|
std::ofstream rodataofs("as64-rodata.asm");
|
|
std::ofstream bssofs("as64-bss.asm");
|
|
std::ofstream tlsofs("as64-tls.asm");
|
|
|
|
ZeroMemory(buf, sizeof(buf));
|
|
ZeroMemory(fname, sizeof(fname));
|
|
while (ifs.getline(buf, sizeof(buf))) {
|
|
inptr = buf;
|
|
SkipSpaces();
|
|
if (*inptr == ';')
|
|
goto j1;
|
|
if (inptr[0] == '/' && inptr[1] == '/')
|
|
goto j1;
|
|
if (*inptr == '.') inptr++;
|
|
if ((_strnicmp(inptr, "file", 4)==0) && !isIdentChar(inptr[4])) {
|
|
inptr += 4;
|
|
if (inptr[0] == ':')
|
|
inptr++;
|
|
getIdentifier();
|
|
strcpy_s(fname, sizeof(fname), lastid);
|
|
}
|
|
if ((_strnicmp(inptr, "code", 4) == 0) && !isIdentChar(inptr[4])) {
|
|
segment = codeseg;
|
|
setname = true;
|
|
}
|
|
else if ((_strnicmp(inptr, "data", 4) == 0) && !isIdentChar(inptr[4])) {
|
|
segment = dataseg;
|
|
}
|
|
else if ((_strnicmp(inptr, "rodata", 6) == 0) && !isIdentChar(inptr[6])) {
|
|
segment = rodataseg;
|
|
}
|
|
else if ((_strnicmp(inptr, "tls", 3) == 0) && !isIdentChar(inptr[3])) {
|
|
segment = tlsseg;
|
|
}
|
|
else if ((_strnicmp(inptr, "bss", 3) == 0) && !isIdentChar(inptr[3])) {
|
|
segment = bssseg;
|
|
}
|
|
j1:
|
|
switch (segment) {
|
|
case codeseg:
|
|
if (setname) {
|
|
setname = false;
|
|
codeofs << ".file: ";
|
|
codeofs << fname;
|
|
}
|
|
codeofs << buf;
|
|
break;
|
|
case dataseg:
|
|
dataofs << buf;
|
|
break;
|
|
case rodataseg:
|
|
rodataofs << buf;
|
|
break;
|
|
case tlsseg:
|
|
tlsofs << buf;
|
|
break;
|
|
case bssseg:
|
|
bssofs << buf;
|
|
break;
|
|
}
|
|
}
|
|
codeofs.close();
|
|
dataofs.close();
|
|
idataofs.close();
|
|
rodataofs.close();
|
|
bssofs.close();
|
|
tlsofs.close();
|
|
ifs.close();
|
|
system("type as64-code.asm > as64-segments.asm");
|
|
system("type as64-rodata.asm >> as64-segments.asm");
|
|
std::ofstream ofs("as64-segments.asm", std::ofstream::out | std::ofstream::app);
|
|
ofs << "\nrodata\n";
|
|
ofs << "\talign 8\n";
|
|
ofs << "begin_init_data:\n";
|
|
ofs.close();
|
|
system("type as64-data.asm >> as64-segments.asm");
|
|
ofs.open("as64-segments.asm", std::ofstream::out | std::ofstream::app);
|
|
ofs << "\nrodata\n";
|
|
ofs << "\talign 8\n";
|
|
ofs << "end_init_data:\n";
|
|
ofs.close();
|
|
system("type as64-data.asm >> as64-segments.asm");
|
|
system("type as64-bss.asm >> as64-segments.asm");
|
|
system("type as64-tls.asm >> as64-segments.asm");
|
|
}
|
|
|
void skipif(int64_t val)
|
void skipif(int64_t val)
|
{
|
{
|
int iflevel = 1;
|
int iflevel = 1;
|
char *p1, *p2, *p3;
|
char *p1, *p2, *p3;
|
bool codecut = false;
|
bool codecut = false;
|
Line 1422... |
Line 1608... |
char *fname;
|
char *fname;
|
int nn;
|
int nn;
|
int lb;
|
int lb;
|
|
|
p = line;
|
p = line;
|
|
fns.GetTos()->lineno = lineno;
|
while(isspace(*p)) p++;
|
while(isspace(*p)) p++;
|
if (!*p) goto addToMaster;
|
if (!*p) goto addToMaster;
|
// see if the first thing on the line is an include directive
|
// see if the first thing on the line is an include directive
|
if (*p=='.') p++;
|
if (*p=='.') p++;
|
if (strnicmp(p, "include", 7)==0 && !isIdentChar(p[7]))
|
if (strnicmp(p, "include", 7)==0 && !isIdentChar(p[7]))
|
Line 1453... |
Line 1640... |
free(fname);
|
free(fname);
|
return;
|
return;
|
}
|
}
|
// Not an include directive, then just copy the line to the master buffer.
|
// Not an include directive, then just copy the line to the master buffer.
|
addToMaster:
|
addToMaster:
|
strcpy(&masterFile[mfndx], line);
|
//strcpy(&masterFile[mfndx], line);
|
mfndx += strlen(line);
|
//mfndx += strlen(line);
|
|
mofs << line;
|
}
|
}
|
|
|
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
// Build a aggregate of all the included files into a single master buffer.
|
// Build a aggregate of all the included files into a single master buffer.
|
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
|
|
void processFile(char *fname, int searchincl)
|
void processFile(char *fname, int searchincl)
|
{
|
{
|
FILE *fp;
|
FILE *fp;
|
|
std::ifstream ifs;
|
char *pathname;
|
char *pathname;
|
|
char buf[700];
|
|
|
|
fns.Push(mname, lineno);
|
|
mname = std::string(fname);
|
|
lineno = 1;
|
|
mofs << ".file \x22";
|
|
mofs << mname.c_str();
|
|
mofs << "\x22," << lineno << "\n";
|
if (verbose)
|
if (verbose)
|
printf("Processing file:%s\r\n", fname);
|
printf("Processing file:%s\n", fname);
|
pathname = (char *)NULL;
|
pathname = (char *)NULL;
|
fp = fopen(fname, "r");
|
ifs.open(fname);
|
if (!fp) {
|
if (ifs.fail()) {
|
if (searchincl) {
|
if (searchincl) {
|
searchenv(fname, "INCLUDE", &pathname);
|
searchenv(fname, "INCLUDE", &pathname);
|
if (strlen(pathname)) {
|
if (strlen(pathname)) {
|
fp = fopen(pathname, "r");
|
ifs.open(pathname);
|
if (fp) goto j1;
|
if (!ifs.fail()) goto j1;
|
}
|
}
|
}
|
}
|
printf("Can't open file <%s>\r\n", fname);
|
printf("Can't open file <%s>\n", fname);
|
goto j2;
|
goto j2;
|
}
|
}
|
j1:
|
j1:
|
while (!feof(fp)) {
|
while (ifs.getline(buf, sizeof(buf))) {
|
fgets(buf, sizeof(buf)/sizeof(char), fp);
|
strcat(buf,"\n");
|
processLine(buf);
|
processLine(buf);
|
ZeroMemory(buf,sizeof(buf));
|
|
}
|
}
|
fclose(fp);
|
ifs.close();
|
j2:
|
j2:
|
if (pathname)
|
if (pathname)
|
free(pathname);
|
free(pathname);
|
|
fns.Pop(&mname, &lineno);
|
|
mofs << ".file \x22";
|
|
mofs << mname.c_str();
|
|
mofs << "\x22," << lineno << "\n";
|
}
|
}
|
|
|
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
|
|
Line 1876... |
Line 2075... |
uint64_t lsa; // last start address
|
uint64_t lsa; // last start address
|
double bpi;
|
double bpi;
|
int64_t i64;
|
int64_t i64;
|
uint32_t u32;
|
uint32_t u32;
|
float nc2;
|
float nc2;
|
|
std::ifstream ifs;
|
|
|
processOpt = 1;
|
processOpt = 1;
|
sections[bssseg].storebyte = 0;
|
sections[bssseg].storebyte = 0;
|
ofp = stdout;
|
ofp = stdout;
|
nn = processOptions(argc, argv);
|
nn = processOptions(argc, argv);
|
Line 1896... |
Line 2096... |
data_address = 0;
|
data_address = 0;
|
isInitializationData = 0;
|
isInitializationData = 0;
|
for (qq = 0; qq < 12; qq++)
|
for (qq = 0; qq < 12; qq++)
|
sections[qq].Clear();
|
sections[qq].Clear();
|
nmTable.Clear();
|
nmTable.Clear();
|
memset(masterFile,0,sizeof(masterFile));
|
mofs.open("as64-master.asm");
|
if (verbose) printf("Pass 1 - collect all input files.\r\n");
|
if (verbose) printf("Pass 1 - collect all input files.\r\n");
|
//PreProcessFile(fname);
|
//PreProcessFile(fname);
|
//strcat_s(fname,sizeof(fname),".app.asm");
|
//strcat_s(fname,sizeof(fname),".app.asm");
|
|
mname = std::string(fname);
|
processFile(fname,0); // Pass 1, collect all include files
|
processFile(fname,0); // Pass 1, collect all include files
|
if (debug) {
|
masterFileLength = mofs.tellp();
|
FILE *fp;
|
mofs.close();
|
fopen_s(&fp, "a64-master.asm", "w");
|
masterFile = new char[masterFileLength + 10000];
|
if (fp) {
|
//if (debug) {
|
fwrite(masterFile, 1, strlen(masterFile), fp);
|
// FILE *fp;
|
fclose(fp);
|
// fopen_s(&fp, "a64-master.asm", "w");
|
}
|
// if (fp) {
|
}
|
// fwrite(masterFile, 1, strlen(masterFile), fp);
|
|
// fclose(fp);
|
|
// }
|
|
//}
|
|
ZeroMemory(masterFile, masterFileLength + 10000);
|
|
ifs.open("as64-master.asm");
|
|
ifs.read(masterFile, masterFileLength + 10000);
|
|
ifs.close();
|
if (verbose) printf("Pass 2 - group and reorder segments\r\n");
|
if (verbose) printf("Pass 2 - group and reorder segments\r\n");
|
first_org = 1;
|
first_org = 1;
|
processSegments(); // Pass 2, group and order segments
|
processSegments(); // Pass 2, group and order segments
|
|
// ProcessSegments2();
|
|
|
pass = 3;
|
pass = 3;
|
processMaster(); // Pass 3 collect up opcodes
|
processMaster(); // Pass 3 collect up opcodes
|
printf("Qsorting\r\n");
|
printf("Qsorting\r\n");
|
qsort((HTBLE*)hTable, htblmax, sizeof(HTBLE), hcmp);
|
qsort((HTBLE*)hTable, htblmax, sizeof(HTBLE), hcmp);
|
Line 2294... |
Line 2502... |
fprintf(vfp, ":00000001FF\n%c",26); // end of file record
|
fprintf(vfp, ":00000001FF\n%c",26); // end of file record
|
fclose(vfp);
|
fclose(vfp);
|
}
|
}
|
else
|
else
|
printf("Can't create .hex file.\r\n");
|
printf("Can't create .hex file.\r\n");
|
return 0;
|
delete[] masterFile;
|
|
return (0);
|
}
|
}
|
|
|
bool IsNBit(int64_t val, int64_t n)
|
bool IsNBit(int64_t val, int64_t n)
|
{
|
{
|
int64_t low, high;
|
int64_t low, high;
|