📄 bldfile.c
字号:
#include "bldfile.h"
#include "linux.h"
#include "common.h"
#include "cmdline.h"
#include "error.h"
#include "symtbl.h"
#include "strtbl.h"
FILE * ofptr; /*pointer to output file*/
FILE * afptr; /*pointer to temporary bytecode file*/
char * ofile; /*pointer to output file name*/
char * afile; /*pointer to temporary file name*/
char ofBuffer[BUFFER_SIZE]; /*buffer for output file*/
int iOFChar; /*index into ofBuffer*/
U1 output[8]; /*holds bytes to write to output file*/
/*manage output buffer*/
void putByteOutBuff(U1 byte);
void flushOutBuff();
/*these functions convert a number to big endian and write to output*/
void commit2Bytes(U2 val);
void commit4Bytes(U4 val);
void commit8Bytes(U8 val);
int BuildFile_init()
{
ofptr = NULL;
afptr = NULL;
ofile = outputFile;
afile = tempFile;
if (ofile == NULL)
{
ERROR0("BuildFile(): output file name has not been specified\n");
return FALSE;
}
if (afile == NULL)
{
ERROR0("BuildFile(): temporary file's name has not been specified\n");
return FALSE;
}
ofptr = fopen(ofile, "wb");
if (ofptr == NULL)
{
ERROR1("BuildFile(): could not open output file->%s\n",ofile);
return FALSE;
}
afptr = fopen(afile, "rb");
if (afptr == NULL)
{
ERROR1("BuildFile(): could not open temporary file->%s\n",afile);
return FALSE;
}
iOFChar = 0;
return TRUE;
}
void BuildFile_free()
{
if (afptr != NULL)
{
if (fclose(afptr))
{
ERROR1("BuildFile(): problem closing temp file ->%s\n", afile);
}
afptr = NULL;
}
if (ofptr != NULL)
{
flushOutBuff();
if (fclose(ofptr))
{
ERROR1("BuildFile(): problem closing output file ->%s\n", ofile);
}
ofptr= NULL;
}
return;
}
void putByteOutBuff(U1 byte)
{
int nbytes;
nbytes = 0;
ofBuffer[iOFChar] = byte;
iOFChar++;
if (iOFChar == BUFFER_SIZE)
{
nbytes = fwrite(ofBuffer, sizeof(U1), BUFFER_SIZE, ofptr);
if (nbytes != BUFFER_SIZE)
{
ERROR1("putByteOutBuff(): error during fwrite to %s\n", ofile);
return;
}
iOFChar = 0;
}
return;
}
void flushOutBuff()
{
int nbytes;
nbytes = 0;
if (iOFChar > 0)
{
PASS3_DEBUG1("BuildFile::flushOutBuff(): trying to flush %lu bytes\n", iOFChar);
nbytes = fwrite(ofBuffer, sizeof(U1), iOFChar, ofptr);
if (nbytes != iOFChar)
{
ERROR2("BuildFile::flushOutBuff(): error during fwrite to %s, only flushed %lu bytes\n", ofile, nbytes);
return;
}
iOFChar = 0;
}
return;
}
void commit2Bytes(U2 val)
{
wordToBytecode(val, output);
putByteOutBuff(output[0]);
putByteOutBuff(output[1]);
return;
}
void commit4Bytes(U4 val)
{
dwordToBytecode(val, output);
putByteOutBuff(output[0]);
putByteOutBuff(output[1]);
putByteOutBuff(output[2]);
putByteOutBuff(output[3]);
return;
}/*end commit4Bytes*/
void commit8Bytes(U8 val)
{
qwordToBytecode(val, output);
putByteOutBuff(output[0]);
putByteOutBuff(output[1]);
putByteOutBuff(output[2]);
putByteOutBuff(output[3]);
putByteOutBuff(output[4]);
putByteOutBuff(output[5]);
putByteOutBuff(output[6]);
putByteOutBuff(output[7]);
return;
}
void buildFileFormat()
{
U8 size_symtbl;
U8 size_strtbl;
U8 size_bc;
U8 i;
U8 j;
int ch;
size_symtbl = 0;
size_strtbl = 0;
size_bc = 0;
/* 1) determine size of symbol table*/
size_symtbl = size_symtbl + 8; /*table of contents */
size_symtbl = size_symtbl + SIZE_GLOBREC * iGlobVar;
size_symtbl = size_symtbl + SIZE_PROCREC * iProc;
for (i = 0; i < iProc; i++)
{
size_symtbl = size_symtbl + SIZE_RETREC * proc[i].nRet;
size_symtbl = size_symtbl + SIZE_ARGREC * proc[i].iArg;
size_symtbl = size_symtbl + SIZE_LOCREC * proc[i].iLocal;
size_symtbl = size_symtbl + SIZE_LBLREC * proc[i].iLabel;
}
/* 2) determine size of string table*/
size_strtbl = strtbl_iStr;
/* 3) determine size of bytecode */
size_bc = getFileSize(afile);
/* 4) commit header to output file */
putByteOutBuff(0xDE); /*magic word to output file */
putByteOutBuff(0xED);
if (omitDebugData == TRUE)
{
commit8Bytes((U8)0);
commit8Bytes((U8)0);
commit8Bytes(size_bc);
ch = fgetc(afptr);
while (ch != EOF)
{
putByteOutBuff((U1)ch);
ch = fgetc(afptr);
}
return;
}
commit8Bytes(size_symtbl);
commit8Bytes(size_strtbl);
commit8Bytes(size_bc);
/* 5) commit tables of contents (# globals and # procs) */
commit4Bytes(iGlobVar);
commit4Bytes(iProc);
/* 6) commit global vars to output file */
for (i = 0; i < iGlobVar; i++)
{
commit8Bytes(globVar[i].text);
putByteOutBuff(globVar[i].dType);
commit8Bytes(globVar[i].len);
commit8Bytes(globVar[i].size);
commit8Bytes( -((S8)globVar[i].offset) );
commit4Bytes(globVar[i].line);
}
/* 7) commit procedures to output file */
for (i = 0; i < iProc; i++)
{
commit8Bytes(proc[i].text);
commit8Bytes(proc[i].address);
commit4Bytes(proc[i].line);
putByteOutBuff(proc[i].nRet);
putByteOutBuff(proc[i].iArg);
putByteOutBuff(proc[i].iLocal);
commit2Bytes(proc[i].iLabel);
for (j = 0; j < proc[i].nRet; j++)
{
commit8Bytes( (proc[i].ret).text );
commit4Bytes( (proc[i].ret).fpOffset );
commit4Bytes( (proc[i].ret).line );
}
for (j = 0; j < proc[i].iArg; j++)
{
commit8Bytes( (proc[i].arg[j]).text );
commit4Bytes( (proc[i].arg[j]).fpOffset );
commit4Bytes( (proc[i].arg[j]).line );
}
for (j = 0; j < proc[i].iLocal; j++)
{
commit8Bytes( (proc[i].local[j]).text );
commit4Bytes( (proc[i].local[j]).fpOffset );
commit4Bytes( (proc[i].local[j]).line );
}
for (j = 0; j < proc[i].iLabel; j++)
{
commit8Bytes( (proc[i].label[j]).text );
commit8Bytes( (proc[i].label[j]).address );
commit4Bytes( (proc[i].label[j]).line );
}
}
/* 8) commit string table to output file */
for (i = 0; i < strtbl_iStr; i++)
{
putByteOutBuff(strtbl_text[i]);
}
/* 9) append bytecode in temp to output file */
ch = fgetc(afptr);
while (ch != EOF)
{
putByteOutBuff((U1)ch);
ch = fgetc(afptr);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -