Merge branch 'master' into console-func-refactor

Conflicts:
	Engine/source/app/net/net.cpp
	Engine/source/console/astNodes.cpp
	Engine/source/console/compiledEval.cpp
	Engine/source/console/console.h
	Engine/source/console/consoleInternal.h
	Engine/source/console/engineAPI.h
This commit is contained in:
Daniel Buckmaster 2014-10-14 14:40:17 +11:00
commit b507dc9555
6487 changed files with 315149 additions and 609761 deletions

View file

@ -24,6 +24,12 @@
#ifndef _COMPILER_H_
#define _COMPILER_H_
//#define DEBUG_CODESTREAM
#ifdef DEBUG_CODESTREAM
#include <stdio.h>
#endif
class Stream;
class DataChunker;
@ -31,6 +37,9 @@ class DataChunker;
#include "console/ast.h"
#include "console/codeBlock.h"
#ifndef _TVECTOR_H_
#include "core/util/tVector.h"
#endif
namespace Compiler
{
@ -49,7 +58,7 @@ namespace Compiler
OP_JMPIFF,
OP_JMPIF,
OP_JMPIFNOT_NP,
OP_JMPIF_NP,
OP_JMPIF_NP, // 10
OP_JMP,
OP_RETURN,
// fixes a bug when not explicitly returning a value
@ -63,7 +72,7 @@ namespace Compiler
OP_CMPLT,
OP_CMPLE,
OP_CMPNE,
OP_XOR,
OP_XOR, // 20
OP_MOD,
OP_BITAND,
OP_BITOR,
@ -74,7 +83,7 @@ namespace Compiler
OP_SHR,
OP_SHL,
OP_AND,
OP_OR,
OP_OR, // 30
OP_ADD,
OP_SUB,
@ -87,7 +96,7 @@ namespace Compiler
OP_SETCURVAR_ARRAY,
OP_SETCURVAR_ARRAY_CREATE,
OP_LOADVAR_UINT,
OP_LOADVAR_UINT,// 40
OP_LOADVAR_FLT,
OP_LOADVAR_STR,
OP_LOADVAR_VAR,
@ -102,7 +111,7 @@ namespace Compiler
OP_SETCUROBJECT_INTERNAL,
OP_SETCURFIELD,
OP_SETCURFIELD_ARRAY,
OP_SETCURFIELD_ARRAY, // 50
OP_SETCURFIELD_TYPE,
OP_LOADFIELD_UINT,
@ -115,7 +124,7 @@ namespace Compiler
OP_STR_TO_UINT,
OP_STR_TO_FLT,
OP_STR_TO_NONE,
OP_STR_TO_NONE, // 60
OP_FLT_TO_UINT,
OP_FLT_TO_STR,
OP_FLT_TO_NONE,
@ -127,7 +136,7 @@ namespace Compiler
OP_LOADIMMED_UINT,
OP_LOADIMMED_FLT,
OP_TAG_TO_STR,
OP_LOADIMMED_STR,
OP_LOADIMMED_STR, // 70
OP_DOCBLOCK_STR,
OP_LOADIMMED_IDENT,
@ -139,7 +148,7 @@ namespace Compiler
OP_ADVANCE_STR_COMMA,
OP_ADVANCE_STR_NUL,
OP_REWIND_STR,
OP_TERMINATE_REWIND_STR,
OP_TERMINATE_REWIND_STR, // 80
OP_COMPARE_STR,
OP_PUSH, // String
@ -156,14 +165,14 @@ namespace Compiler
OP_ITER, ///< Enter foreach loop.
OP_ITER_END, ///< End foreach loop.
OP_INVALID
OP_INVALID // 90
};
//------------------------------------------------------------
F64 consoleStringToNumber(const char *str, StringTableEntry file = 0, U32 line = 0);
U32 precompileBlock(StmtNode *block, U32 loopCount);
U32 compileBlock(StmtNode *block, U32 *codeStream, U32 ip, U32 continuePoint, U32 breakPoint);
U32 compileBlock(StmtNode *block, CodeStream &codeStream, U32 ip);
//------------------------------------------------------------
@ -227,15 +236,19 @@ namespace Compiler
//------------------------------------------------------------
inline StringTableEntry U32toSTE(U32 u)
inline StringTableEntry CodeToSTE(U32 *code, U32 ip)
{
return *((StringTableEntry *) &u);
#ifdef TORQUE_CPU_X64
return (StringTableEntry)(*((U64*)(code+ip)));
#else
return (StringTableEntry)(*(code+ip));
#endif
}
extern U32 (*STEtoU32)(StringTableEntry ste, U32 ip);
U32 evalSTEtoU32(StringTableEntry ste, U32);
U32 compileSTEtoU32(StringTableEntry ste, U32 ip);
extern void (*STEtoCode)(StringTableEntry ste, U32 ip, U32 *ptr);
void evalSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr);
void compileSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr);
CompilerStringTable *getCurrentStringTable();
CompilerStringTable &getGlobalStringTable();
@ -253,9 +266,6 @@ namespace Compiler
void precompileIdent(StringTableEntry ident);
CodeBlock *getBreakCodeBlock();
void setBreakCodeBlock(CodeBlock *cb);
/// Helper function to reset the float, string, and ident tables to a base
/// starting state.
void resetTables();
@ -266,4 +276,170 @@ namespace Compiler
extern bool gSyntaxError;
};
/// Utility class to emit and patch bytecode
class CodeStream
{
public:
enum FixType
{
// For loops
FIXTYPE_LOOPBLOCKSTART,
FIXTYPE_BREAK,
FIXTYPE_CONTINUE
};
enum Constants
{
BlockSize = 16384,
};
protected:
typedef struct PatchEntry
{
U32 addr; ///< Address to patch
U32 value; ///< Value to place at addr
PatchEntry() {;}
PatchEntry(U32 a, U32 v) : addr(a), value(v) {;}
} PatchEntry;
typedef struct CodeData
{
U8 *data; ///< Allocated data (size is BlockSize)
U32 size; ///< Bytes used in data
CodeData *next; ///< Next block
};
/// @name Emitted code
/// {
CodeData *mCode;
CodeData *mCodeHead;
U32 mCodePos;
/// }
/// @name Code fixing stacks
/// {
Vector<U32> mFixList;
Vector<U32> mFixStack;
Vector<bool> mFixLoopStack;
Vector<PatchEntry> mPatchList;
/// }
Vector<U32> mBreakLines; ///< Line numbers
public:
CodeStream() : mCode(0), mCodeHead(NULL), mCodePos(0)
{
}
~CodeStream()
{
reset();
if (mCode)
{
dFree(mCode->data);
delete mCode;
}
}
U8 *allocCode(U32 sz);
inline U32 emit(U32 code)
{
U32 *ptr = (U32*)allocCode(4);
*ptr = code;
#ifdef DEBUG_CODESTREAM
printf("code[%u] = %u\n", mCodePos, code);
#endif
return mCodePos++;
}
inline void patch(U32 addr, U32 code)
{
#ifdef DEBUG_CODESTREAM
printf("patch[%u] = %u\n", addr, code);
#endif
mPatchList.push_back(PatchEntry(addr, code));
}
inline U32 emitSTE(const char *code)
{
U64 *ptr = (U64*)allocCode(8);
*ptr = 0;
Compiler::STEtoCode(code, mCodePos, (U32*)ptr);
#ifdef DEBUG_CODESTREAM
printf("code[%u] = %s\n", mCodePos, code);
#endif
mCodePos += 2;
return mCodePos-2;
}
inline U32 tell()
{
return mCodePos;
}
inline bool inLoop()
{
for (U32 i=0; i<mFixLoopStack.size(); i++)
{
if (mFixLoopStack[i])
return true;
}
return false;
}
inline U32 emitFix(FixType type)
{
U32 *ptr = (U32*)allocCode(4);
*ptr = (U32)type;
#ifdef DEBUG_CODESTREAM
printf("code[%u] = [FIX:%u]\n", mCodePos, (U32)type);
#endif
mFixList.push_back(mCodePos);
mFixList.push_back((U32)type);
return mCodePos++;
}
inline void pushFixScope(bool isLoop)
{
mFixStack.push_back(mFixList.size());
mFixLoopStack.push_back(isLoop);
}
inline void popFixScope()
{
AssertFatal(mFixStack.size() > 0, "Fix stack mismatch");
U32 newSize = mFixStack[mFixStack.size()-1];
while (mFixList.size() > newSize)
mFixList.pop_back();
mFixStack.pop_back();
mFixLoopStack.pop_back();
}
void fixLoop(U32 loopBlockStart, U32 breakPoint, U32 continuePoint);
inline void addBreakLine(U32 lineNumber, U32 ip)
{
mBreakLines.push_back(lineNumber);
mBreakLines.push_back(ip);
}
inline U32 getNumLineBreaks()
{
return mBreakLines.size() / 2;
}
void emitCodeStream(U32 *size, U32 **stream, U32 **lineBreaks);
void reset();
};
#endif