133 lines
2.3 KiB
C++
133 lines
2.3 KiB
C++
#include "asmrigs/as.h"
|
|
#include "tier2/tokenizer.h"
|
|
#include "asmrigs/tokenparser.h"
|
|
|
|
struct ObjectHeader_t
|
|
{
|
|
uint8_t m_magic[4];
|
|
uint32_t m_uSymbolCount;
|
|
uint32_t m_uDatasCount;
|
|
};
|
|
|
|
enum EDataFlags: uint32_t
|
|
{
|
|
DATA_TYPE_DECLARE,
|
|
DATA_TYPE_USED,
|
|
};
|
|
|
|
struct Data_t
|
|
{
|
|
uint64_t m_uDataSize;
|
|
uint64_t m_uDataOffsetSize;
|
|
};
|
|
|
|
struct Symbol_t
|
|
{
|
|
uint32_t m_uName;
|
|
uint32_t m_uData;
|
|
EDataFlags m_eDataFlags;
|
|
};
|
|
|
|
enum EInstruction: uint8_t
|
|
{
|
|
I_NOP = 0,
|
|
|
|
I_PUSH = 3,
|
|
I_POP = 4,
|
|
|
|
I_CMP = 5,
|
|
|
|
I_JUMP = 6,
|
|
I_JZ = 7,
|
|
I_JNZ = 8,
|
|
I_JG = 9,
|
|
I_JGE = 10,
|
|
I_JLE = 11,
|
|
|
|
I_LOAD = 12,
|
|
I_STORE = 13,
|
|
I_LOADPTR = 14,
|
|
I_STOREPTR = 15,
|
|
I_LOADLIT = 16,
|
|
|
|
I_JSR = 20,
|
|
I_RET = 21,
|
|
};
|
|
|
|
CUtlBuffer<uint8_t> Assemble( const char *szAssembly, AsOptions_t stOptions )
|
|
{
|
|
CUtlVector<CUtlString> externalFunctions;
|
|
CUtlVector<CUtlString> publicFunctions;
|
|
CUtlVector<CUtlString> labels;
|
|
CUtlVector<Token_t> tokens = Tokenize(szAssembly);
|
|
Token_t *pTokens = tokens.GetData();
|
|
for ( int i = 0; i < tokens.GetSize()-2; )
|
|
{
|
|
if ( !tokens[i].m_bIsQuoted && tokens[i].m_szValue == "/" )
|
|
if ( !tokens[i+1].m_bIsQuoted && tokens[i+1].m_szValue == "/" )
|
|
if ( !tokens[i+2].m_bIsQuoted && tokens[i+2].m_szValue == "/" )
|
|
{
|
|
tokens.RemoveAt(i, 3);
|
|
continue;
|
|
}
|
|
i++;
|
|
};
|
|
CTokenParser parser;
|
|
parser.m_pTokens = tokens.GetData();
|
|
parser.m_pTokensEnd = tokens.GetData()+tokens.GetSize();
|
|
parser.m_pCurrentToken = tokens.GetData()-1;
|
|
parser.Continue();
|
|
|
|
CUtlVector<uint8_t> commandStream;
|
|
|
|
for (;;)
|
|
{
|
|
if (parser.IsEOF())
|
|
break;
|
|
CUtlString szCommand = parser.PeekToken();
|
|
parser.Continue();
|
|
if ( szCommand == "extrn" )
|
|
{
|
|
externalFunctions.AppendTail(parser.PeekToken());
|
|
parser.Continue();
|
|
continue;
|
|
}
|
|
if ( szCommand == "public" )
|
|
{
|
|
publicFunctions.AppendTail(parser.PeekToken());
|
|
parser.Continue();
|
|
continue;
|
|
}
|
|
if ( szCommand == "store" )
|
|
{
|
|
continue;
|
|
}
|
|
if ( szCommand == "load" )
|
|
{
|
|
continue;
|
|
}
|
|
if ( szCommand == "jsr" )
|
|
{
|
|
commandStream.AppendTail(I_JSR);
|
|
continue;
|
|
}
|
|
if ( szCommand == "ret" )
|
|
{
|
|
commandStream.AppendTail(I_RET);
|
|
continue;
|
|
}
|
|
if (parser.IsEOF())
|
|
break;
|
|
if (!V_strcmp(parser.PeekToken(), ":"))
|
|
{
|
|
parser.Continue();
|
|
labels.AppendTail("");
|
|
}
|
|
else
|
|
{
|
|
V_printf("%s is not a valid instuction or label\n", szCommand.GetString());
|
|
}
|
|
}
|
|
return {};
|
|
};
|