Mercurial > LightClone
view LightClone/Source/Compiler.cpp @ 60:b0f642ee22d3
Additional maps
author | koryspansel <koryspansel@bendbroadband.com> |
---|---|
date | Mon, 03 Oct 2011 08:58:08 -0700 |
parents | dc1f4a668d50 |
children | 1fe27776627e |
line wrap: on
line source
/* * Compiler */ #include "Compiler.h" #include "Program.h" /* * Compiler */ Compiler::Compiler() { for(uint32 i = 0; i < MaximumFunctionCount; ++i) { nFunctionTable[i] = 0; } for(uint32 i = 0; i < MaximumFunctionCount * MaximumInstructionCount; ++i) { nCallTable[i] = 0; } } /* * Compile */ ErrorCode Compiler::Compile(const Program& kProgram, uint8* pData, uint32 nSize) { uint32 nOffset = 0; //PASS 1: Compile all actions into bytecode instructions for(uint32 nFunction = 0; nFunction < MaximumFunctionCount; ++nFunction) { // store the address of the function for later fixup nFunctionTable[nFunction] = nOffset; for(uint32 nInstruction = 0; nInstruction < MaximumInstructionCount; ++nInstruction) { const uint32 nAction = kProgram.GetAction(nFunction, nInstruction); const uint32 nLength = GetInstructionSizeFromAction(nAction); if(nOffset + nLength < nSize) { if(Action_Forward <= nAction && nAction <= Action_Light) { pData[nOffset++] = Instruction_Action; pData[nOffset++] = nAction; } else if(Action_FunctionA <= nAction && nAction <= Action_FunctionB) { pData[nOffset++] = Instruction_Call; pData[nOffset++] = 0; // record the call to the specified function nCallTable[nOffset - 1] = nAction - Action_FunctionA + 1; } //else //{ // pData[nOffset++] = Instruction_Nop; //} } else { return Error_Fail; } } // implicit return at the end of each function pData[nOffset++] = Instruction_Return; } //PASS 2: Fixup function call addresses for(uint32 nAddress = 0; nAddress < MaximumFunctionCount * MaximumInstructionCount; ++nAddress) { const uint32 nFunction = nCallTable[nAddress]; if(nFunction > 0) { //ASSERT(nFunctionTable[nFunction] > 0); // fixup address of function call pData[nAddress] = nFunctionTable[nFunction]; } } return Error_Success; } /* * GetInstructionSizeFromAction */ uint32 Compiler::GetInstructionSizeFromAction(uint32 nAction) const { return (Action_Forward <= nAction && nAction <= Action_FunctionB) ? 2 : 1; }