Fixed some more bugs, added #if/#else/#endif and more opcodes, and .DATA stat

This commit is contained in:
David Anderson 2004-08-09 05:23:32 +00:00
parent 1dc16b835e
commit b14708d6f2
9 changed files with 215 additions and 51 deletions

View File

@ -28,6 +28,7 @@ Compiler::Compiler()
cellsize = 4; cellsize = 4;
Output = 0; Output = 0;
stacksize = cellsize * 4096; stacksize = cellsize * 4096;
debug = false;
Init(); Init();
} }
@ -51,9 +52,19 @@ Compiler::Compiler(std::string &f)
Output = 0; Output = 0;
filename.assign(f); filename.assign(f);
stacksize = cellsize * 4096; stacksize = cellsize * 4096;
debug = false;
Init(); Init();
} }
bool Compiler::SetDebug()
{
bool state = debug;
debug = debug ? false : true;
return state;
}
void Compiler::Load(std::string &f) void Compiler::Load(std::string &f)
{ {
filename.assign(f); filename.assign(f);
@ -108,7 +119,7 @@ bool Compiler::Compile()
} }
int32_t fileSize = 0; int32_t fileSize = 0;
int16_t magic = AMX_MAGIC; int16_t magic = (int16_t)AMX_MAGIC;
char file_version = CUR_FILE_VERSION; char file_version = CUR_FILE_VERSION;
char amx_version = MIN_AMX_VERSION; char amx_version = MIN_AMX_VERSION;
int16_t flags = 0; int16_t flags = 0;
@ -145,24 +156,24 @@ bool Compiler::Compile()
n.Name = (*pl)->Symbol->sym.c_str(); n.Name = (*pl)->Symbol->sym.c_str();
a.addr = (*pl)->ASM->cip; a.addr = (*pl)->ASM->cip;
printf("%s cip = %d\n", n.Name, a.addr); printf("%s cip = %d\n", n.Name, a.addr);
a.offset = Nametbl.size(); a.offset = (int)Nametbl.size();
Nametbl.push_back(n); Nametbl.push_back(n);
PublicsTable.push_back(a); PublicsTable.push_back(a);
} }
natives = publics + (PublicsTable.size() * (sizeof(int32_t) * 2)); natives = publics + (int)(PublicsTable.size() * (sizeof(int32_t) * 2));
for (nl = NativeList.begin(); nl != NativeList.end(); nl++) for (nl = NativeList.begin(); nl != NativeList.end(); nl++)
{ {
NameRecord n; NameRecord n;
AddrTable a; AddrTable a;
n.Name = (*nl)->S->sym.c_str(); n.Name = (*nl)->S->sym.c_str();
a.addr = 0; a.addr = 0;
a.offset = Nametbl.size(); a.offset = (int)Nametbl.size();
Nametbl.push_back(n); Nametbl.push_back(n);
NativesTable.push_back(a); NativesTable.push_back(a);
} }
libraries = natives + (NativesTable.size() * (sizeof(int32_t) * 2)); libraries = natives + (int)(NativesTable.size() * (sizeof(int32_t) * 2));
pubvars = libraries; pubvars = libraries;
tags = pubvars; tags = pubvars;
names = tags; names = tags;
@ -175,14 +186,14 @@ bool Compiler::Compile()
int off = (*ai).offset; int off = (*ai).offset;
NameMap[cOffset] = off; NameMap[cOffset] = off;
(*ai).offset = cOffset; (*ai).offset = cOffset;
cOffset += strlen(Nametbl.at(off).Name) + 1; cOffset += (int)strlen(Nametbl.at(off).Name) + 1;
} }
for (ai = NativesTable.begin(); ai != NativesTable.end(); ai++) for (ai = NativesTable.begin(); ai != NativesTable.end(); ai++)
{ {
int off = (*ai).offset; int off = (*ai).offset;
NameMap[cOffset] = off; NameMap[cOffset] = off;
(*ai).offset = cOffset; (*ai).offset = cOffset;
cOffset += strlen(Nametbl.at(off).Name) + 1; cOffset += (int)strlen(Nametbl.at(off).Name) + 1;
} }
cod = cOffset; cod = cOffset;
@ -280,16 +291,25 @@ bool Compiler::Compile()
const char *s = 0; const char *s = 0;
for (dmi = dm.begin(); dmi != dm.end(); dmi++) for (dmi = dm.begin(); dmi != dm.end(); dmi++)
{ {
if ( (*dmi)->e.GetType() == Val_Number ) if ( (*dmi)->db )
{ {
val = (*dmi)->e.GetNumber(); if ( (*dmi)->e.GetType() == Val_Number )
fwrite((void *)&val, sizeof(int32_t), 1, fp);
} else {
s = (*dmi)->e.GetString();
for (int q = 0; q < (*dmi)->e.Size(); q++)
{ {
val = s[q]; val = (*dmi)->e.GetNumber();
fwrite((void*)&val, sizeof(int32_t), 1, fp); fwrite((void *)&val, sizeof(int32_t), 1, fp);
} else {
s = (*dmi)->e.GetString();
for (int q = 0; q < (*dmi)->e.Size(); q++)
{
val = s[q];
fwrite((void*)&val, sizeof(int32_t), 1, fp);
}
}
} else {
char c = (*dmi)->fill;
for (int iter=0; iter<=(*dmi)->e.GetNumber(); iter++)
{
fwrite((void*)&c, sizeof(char), 1, fp);
} }
} }
} }
@ -314,6 +334,7 @@ bool Compiler::Parse()
{ {
std::ifstream fp(filename.c_str()); std::ifstream fp(filename.c_str());
char buffer[256] = {0}; char buffer[256] = {0};
std::stack<int> DefStack;
curLine = 0; curLine = 0;
AsmSection sec = Asm_None; AsmSection sec = Asm_None;
lastCip = 0-cellsize; lastCip = 0-cellsize;
@ -328,15 +349,73 @@ bool Compiler::Parse()
{ {
fp.getline(buffer, 255); fp.getline(buffer, 255);
curLine++; curLine++;
/* Check for preprocessor directives */ /* Check for preprocessor directives */
if (buffer[0] == '#') if (buffer[0] == '#')
{ {
std::string procline(buffer); std::string procline(buffer);
ProcessDirective(procline); if (procline.substr(0, 3).compare("#if") == 0)
{
std::string def;
std::string temp;
std::string comp;
StringBreak(procline, def, temp);
StringBreak(temp, def, comp);
DefineMngr::Define *D = 0;
if ((D = CDefines->FindDefine(def)) == 0)
{
DefStack.push(0);
} else if (D->GetDefine()->compare(comp) == 0) {
DefStack.push(1);
} else {
DefStack.push(0);
}
} else if (procline.substr(0, 5).compare("#else") == 0) {
if (DefStack.size())
{
if (DefStack.top() == 1)
{
DefStack.pop();
DefStack.push(0);
} else if (DefStack.top() == 0) {
DefStack.pop();
DefStack.push(1);
}
} else {
CError->ErrorMsg(Err_Misplaced_Directive);
}
continue;
} else if (procline.substr(0, 6).compare("#endif") == 0) {
if (DefStack.size())
{
DefStack.pop();
} else {
CError->ErrorMsg(Err_Misplaced_Directive);
}
continue;
} else {
/* Check for previous operations */
if (DefStack.size())
{
if (DefStack.top() < 1)
{
continue;
}
}
ProcessDirective(procline);
}
continue; continue;
} }
/* Check for previous operations */
if (DefStack.size())
{
if (DefStack.top() < 1)
{
continue;
}
}
/* Strip the line */ /* Strip the line */
std::string line(buffer); std::string line(buffer);
StripComments(line); StripComments(line);
@ -400,13 +479,35 @@ bool Compiler::Parse()
continue; continue;
} }
/* Add and evaluate the expression */ if (fmt.compare("db") == 0)
CExpr e(CError); {
e.Set(data); /* Add and evaluate the expression */
e.Evaluate(); CExpr e(CError);
e.Set(data);
/* Add into the DAT section */ e.Evaluate();
DAT->Add(symbol, e, (fmt.compare("db")==0)?true:false);
/* Add into the DAT section */
DAT->Add(symbol, e, true);
} else if (fmt.compare("stat") == 0) {
CExpr e(CError);
if (data.find("fill") != std::string::npos)
{
std::string fill, amt;
StringBreak(data, amt, buf);
StringBreak(buf, data, fill);
CExpr t(CError);
t.Set(fill);
t.Evaluate();
e.Set(amt);
e.Evaluate();
DAT->Add(symbol, e, false, (char)t.GetNumber());
} else {
e.Set(data);
e.Evaluate();
DAT->Add(symbol, e, false, 0);
}
}
CSymbols->AddSymbol(symbol, Sym_Dat, CurLine()); CSymbols->AddSymbol(symbol, Sym_Dat, CurLine());
} else if (sec == Asm_Public) { } else if (sec == Asm_Public) {
if (!IsValidSymbol(line)) if (!IsValidSymbol(line))
@ -504,7 +605,21 @@ bool Compiler::Parse()
continue; continue;
} }
Asm *ASM = new Asm; Asm *ASM = 0;
if (debug)
{
ASM = new Asm;
ASM->cip = lastCip+cellsize;
ASM->op = OP_LINE;
ASM->params.push_back(curLine);
ASM->params.push_back(0);
CodeList.push_back(ASM);
lastCip+=cellsize*3;
}
ASM = new Asm;
std::vector<std::string *> paramList; std::vector<std::string *> paramList;
if (params.size() > 0) if (params.size() > 0)
@ -1221,12 +1336,9 @@ bool Compiler::Parse()
} }
case OP_LINE: case OP_LINE:
{ {
/* Not yet implemented */ CHK_PARAMS(2);
assert(0);
/*CHK_PARAMS(3);
PUSH_PARAM(1, Sym_Dat); PUSH_PARAM(1, Sym_Dat);
PUSH_PARAM(1, Sym_Dat); PUSH_PARAM(1, Sym_Dat);
PUSH_PARAM(1, Sym_Dat);*/
break; break;
} }
case OP_SYMBOL: case OP_SYMBOL:
@ -1241,12 +1353,9 @@ bool Compiler::Parse()
} }
case OP_SRANGE: case OP_SRANGE:
{ {
/* Not yet implemented */ CHK_PARAMS(2);
assert(0);
/*CHK_PARAMS(3);
PUSH_PARAM(1, Sym_Dat); PUSH_PARAM(1, Sym_Dat);
PUSH_PARAM(1, Sym_Dat); PUSH_PARAM(1, Sym_Dat);
PUSH_PARAM(1, Sym_Dat);*/
break; break;
} }
case OP_JUMP_PRI: case OP_JUMP_PRI:

View File

@ -82,7 +82,9 @@ public:
int FindArguments(std::string &text, std::vector<std::string*> &List, int &end, bool simple = false); int FindArguments(std::string &text, std::vector<std::string*> &List, int &end, bool simple = false);
void Clear(); void Clear();
int CipCount(); int CipCount();
private: int CurCip() { return lastCip; }
bool SetDebug();
public: //private
void ProcessDirective(std::string &text); void ProcessDirective(std::string &text);
void Init(); void Init();
void InitOpcodes(); void InitOpcodes();
@ -107,6 +109,7 @@ private:
int lastCip; int lastCip;
int cellsize; int cellsize;
int stacksize; int stacksize;
bool debug;
}; };
#endif //_INCLUDE_AMXCOMPILER_H #endif //_INCLUDE_AMXCOMPILER_H

View File

@ -44,26 +44,40 @@ DataMngr::Datum::Datum()
{ {
db = false; db = false;
offset = -1; offset = -1;
fill = 0;
} }
void DataMngr::Add(std::string &s, CExpr &expr, bool db) void DataMngr::Add(std::string &s, CExpr &expr, bool db, char fill)
{ {
DataMngr::Datum *D = new DataMngr::Datum(); DataMngr::Datum *D = new DataMngr::Datum();
D->symbol.assign(s); D->symbol.assign(s);
D->e = expr; D->e = expr;
D->fill = fill;
int size = ((D->e.GetType() == Val_Number) ? int size = 0;
cellsize : D->e.Size() * cellsize);
if (db)
{
size = ((D->e.GetType() == Val_Number) ?
cellsize : D->e.Size() * cellsize);
} else {
size = D->e.GetNumber();
}
if (List.size() == 0) if (List.size() == 0)
{ {
D->offset = 0; D->offset = 0;
} else { } else {
DataMngr::Datum *p = List[List.size()-1]; DataMngr::Datum *p = List[List.size()-1];
D->offset = p->offset + if (p->db)
((p->e.GetType() == Val_Number) ? {
cellsize : p->e.Size() * cellsize); D->offset = p->offset +
((p->e.GetType() == Val_Number) ?
cellsize : p->e.Size() * cellsize);
} else {
D->offset = p->offset + p->e.GetNumber();
}
} }
cursize += size; cursize += size;

View File

@ -34,12 +34,13 @@ public:
CExpr e; CExpr e;
bool db; bool db;
int offset; int offset;
char fill;
}; };
public: public:
~DataMngr(); ~DataMngr();
DataMngr() { cellsize = 4; lastOffset = 0; cursize = 0; } DataMngr() { cellsize = 4; lastOffset = 0; cursize = 0; }
DataMngr(int cell) { lastOffset = 0; cellsize = cell; cursize = 0; } DataMngr(int cell) { lastOffset = 0; cellsize = cell; cursize = 0; }
void Add(std::string &s, CExpr &expr, bool db = false); void Add(std::string &s, CExpr &expr, bool db = false, char fill = 0);
DataMngr::Datum *FindData(std::string &sym); DataMngr::Datum *FindData(std::string &sym);
void GetData(std::vector<DataMngr::Datum *> &dList); void GetData(std::vector<DataMngr::Datum *> &dList);
int GetOffset(std::string &sym); int GetOffset(std::string &sym);

View File

@ -38,6 +38,16 @@ void ErrorMngr::Clear()
HighestError = Err_None; HighestError = Err_None;
} }
int ErrorMngr::CurLine()
{
return ((Compiler *)Cmp)->CurLine();
}
int ErrorMngr::CurCip()
{
return ((Compiler *)Cmp)->CurCip();
}
ErrorMngr::ErrorMngr(void *c) ErrorMngr::ErrorMngr(void *c)
{ {
Cmp = c; Cmp = c;
@ -82,6 +92,8 @@ void ErrorMngr::DefineErrors()
List.at(Err_Opcode) = "Invalid or unrecognized opcode"; List.at(Err_Opcode) = "Invalid or unrecognized opcode";
List.at(Err_Unmatched_Token) = "Unmatched token '%c'"; List.at(Err_Unmatched_Token) = "Unmatched token '%c'";
List.at(Err_Param_Count) = "Expected %d parameters, found %d"; List.at(Err_Param_Count) = "Expected %d parameters, found %d";
List.at(Err_Unknown_Define) = "Unknown define referenced";
List.at(Err_Misplaced_Directive) = "Misplaced preprocessor directive.";
List.at(Err_FileNone) = "No file specified"; List.at(Err_FileNone) = "No file specified";
List.at(Err_FileOpen) = "Could not open file \"%s\""; List.at(Err_FileOpen) = "Could not open file \"%s\"";

View File

@ -57,6 +57,8 @@ typedef enum
Err_Opcode, Err_Opcode,
Err_Unmatched_Token, Err_Unmatched_Token,
Err_Param_Count, Err_Param_Count,
Err_Unknown_Define,
Err_Misplaced_Directive,
errors_end, errors_end,
fatals_start, fatals_start,
@ -91,6 +93,8 @@ public:
void ErrorMsg(ErrorCode error, ...); void ErrorMsg(ErrorCode error, ...);
ErrorType GetStatus() { return HighestError; } ErrorType GetStatus() { return HighestError; }
void PrintReport(); void PrintReport();
int CurLine();
int CurCip();
}; };
#endif //_INCLUDE_AMX_ERROR #endif //_INCLUDE_AMX_ERROR

View File

@ -87,6 +87,9 @@ void StringBreak(std::string &Source, std::string &Left, std::string &Right)
int l=0; int l=0;
unsigned int i=0; unsigned int i=0;
Left.clear();
Right.clear();
for (i=0; i<Source.size(); i++) for (i=0; i<Source.size(); i++)
{ {
if (isspace(Source[i]) && !done_flag) if (isspace(Source[i]) && !done_flag)

View File

@ -26,21 +26,25 @@ std::string filename;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
printf("debug clamp.\n");
getchar();
get_options(argc, argv);
Compiler Program; Compiler Program;
get_options(argc, argv, Program);
if (filename.size() < 1)
{
print_version();
exit(0);
}
Program.Load(filename); Program.Load(filename);
Program.Parse(); if (Program.Parse())
Program.Compile(); if (Program.Compile())
printf("Done.\n");
exit(0); exit(0);
} }
void get_options(int argc, char **argv) void get_options(int argc, char **argv, Compiler &Prog)
{ {
int i = 0; /* index */ int i = 0; /* index */
int opt_flag = 0; /* flag for option detection */ int opt_flag = 0; /* flag for option detection */
@ -56,9 +60,15 @@ void get_options(int argc, char **argv)
case 'v': case 'v':
{ {
opt_flag = 0; /* no options expected */ opt_flag = 0; /* no options expected */
//print_version(); print_version();
break; break;
} /* case */ } /* case */
case 'd':
{
opt_flag = 0;
Prog.SetDebug();
break;
}
} /* switch */ } /* switch */
} else { /* - */ } else { /* - */
if (!opt_flag) if (!opt_flag)
@ -70,3 +80,10 @@ void get_options(int argc, char **argv)
} /* if */ } /* if */
} }
} }
void print_version()
{
printf("AMX Assembler 1.00\n");
printf("(C)2004 David 'BAILOPAN' Anderson\n");
exit(0);
}

View File

@ -215,8 +215,9 @@ typedef enum {
OP_NUM_OPCODES OP_NUM_OPCODES
} OPCODE; } OPCODE;
void get_options(int argc, char **argv); void get_options(int argc, char **argv, Compiler &Prog);
void InitOpcodes(); void InitOpcodes();
void DestroyArgList(std::vector<std::string *> &List); void DestroyArgList(std::vector<std::string *> &List);
void print_version();
#endif //_INCLUDE_AMXASM_H #endif //_INCLUDE_AMXASM_H