Torque3D/Engine/source/console/torquescript/codeBlock.h

171 lines
6.3 KiB
C
Raw Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _CODEBLOCK_H_
#define _CODEBLOCK_H_
#include <vector>
2021-08-14 05:37:01 +00:00
#include <unordered_map>
2023-04-23 08:39:54 +00:00
#include "parser.h"
#include "console/runtime.h"
2021-08-14 05:37:01 +00:00
struct CompilerLocalVariableToRegisterMappingTable
{
struct RemappingTable
{
std::vector<StringTableEntry> varList;
2021-08-14 05:37:01 +00:00
};
std::unordered_map<StringTableEntry, RemappingTable> localVarToRegister;
void add(StringTableEntry functionName, StringTableEntry namespaceName, StringTableEntry varName);
2021-08-20 02:05:43 +00:00
S32 lookup(StringTableEntry namespaceName, StringTableEntry functionName, StringTableEntry varName);
2021-08-14 05:37:01 +00:00
CompilerLocalVariableToRegisterMappingTable copy();
void reset();
void write(Stream& stream);
2021-08-14 05:37:01 +00:00
};
2023-04-23 08:39:54 +00:00
#include "compiler.h"
2012-09-19 15:15:01 +00:00
class Stream;
class ConsoleValue;
2012-09-19 15:15:01 +00:00
/// Core TorqueScript code management class.
///
/// This class represents a block of code, usually mapped directly to a file.
2023-04-23 08:39:54 +00:00
class CodeBlock : Con::Module
2012-09-19 15:15:01 +00:00
{
private:
static CodeBlock* smCodeBlockList;
2017-11-06 04:33:32 +00:00
2012-09-19 15:15:01 +00:00
public:
static bool smInFunction;
2023-04-23 08:39:54 +00:00
static TorqueScriptParser * smCurrentParser;
2012-09-19 15:15:01 +00:00
static CodeBlock *getCodeBlockList()
{
return smCodeBlockList;
}
static CodeBlock *find(StringTableEntry);
CodeBlock();
2023-04-23 08:39:54 +00:00
~CodeBlock() override;
2012-09-19 15:15:01 +00:00
2015-03-04 00:55:30 +00:00
StringTableEntry name;
StringTableEntry fullPath;
StringTableEntry modPath;
2012-09-19 15:15:01 +00:00
2015-03-04 00:55:30 +00:00
char *globalStrings;
char *functionStrings;
2012-09-19 15:15:01 +00:00
2015-03-04 00:55:30 +00:00
U32 functionStringsMaxLen;
U32 globalStringsMaxLen;
2012-09-19 15:15:01 +00:00
2015-03-04 00:55:30 +00:00
F64 *globalFloats;
F64 *functionFloats;
2012-09-19 15:15:01 +00:00
2015-03-04 00:55:30 +00:00
U32 codeSize;
U32 *code;
2012-09-19 15:15:01 +00:00
2021-08-14 05:37:01 +00:00
CompilerLocalVariableToRegisterMappingTable variableRegisterTable;
2015-03-04 00:55:30 +00:00
U32 refCount;
U32 lineBreakPairCount;
U32 *lineBreakPairs;
2023-04-23 08:39:54 +00:00
Vector<U32> breakList;
2015-03-04 00:55:30 +00:00
CodeBlock *nextFile;
2012-09-19 15:15:01 +00:00
void addToCodeList();
void removeFromCodeList();
void calcBreakList();
2023-04-23 08:39:54 +00:00
void clearAllBreaks() override;
void setAllBreaks() override;
2017-11-06 04:33:32 +00:00
void dumpInstructions(U32 startIp = 0, bool upToReturn = false);
2012-09-19 15:15:01 +00:00
/// Returns the first breakable line or 0 if none was found.
/// @param lineNumber The one based line number.
2023-04-23 08:39:54 +00:00
U32 findFirstBreakLine(U32 lineNumber) override;
2012-09-19 15:15:01 +00:00
2023-04-23 08:39:54 +00:00
void clearBreakpoint(U32 lineNumber) override;
2012-09-19 15:15:01 +00:00
2023-04-23 08:39:54 +00:00
/// Set a OP_BREAK instruction on a line. If a break
2012-09-19 15:15:01 +00:00
/// is not possible on that line it returns false.
/// @param lineNumber The one based line number.
2023-04-23 08:39:54 +00:00
bool setBreakpoint(U32 lineNumber) override;
2012-09-19 15:15:01 +00:00
2023-04-23 08:39:54 +00:00
void findBreakLine(U32 ip, U32 &line, U32 &instruction) override;
const char *getFileLine(U32 ip) override;
2012-09-19 15:15:01 +00:00
2023-04-23 08:39:54 +00:00
///
2017-11-06 04:33:32 +00:00
String getFunctionArgs(U32 offset);
2012-09-19 15:15:01 +00:00
bool read(StringTableEntry fileName, Stream &st);
bool compile(const char *dsoName, StringTableEntry fileName, const char *script, bool overrideNoDso = false);
/// Compiles and executes a block of script storing the compiled code in this
2023-04-23 08:39:54 +00:00
/// CodeBlock. If there is no filename breakpoints will not be generated and
/// the CodeBlock will not be added to the linked list of loaded CodeBlocks.
2012-09-19 15:15:01 +00:00
/// Note that if the script contains no executable statements the CodeBlock
2023-04-23 08:39:54 +00:00
/// will delete itself on return an empty string. The return string is any
2012-09-19 15:15:01 +00:00
/// result of the code executed, if any, or an empty string.
///
2023-04-23 08:39:54 +00:00
/// @param fileName The file name, including path and extension, for the
2012-09-19 15:15:01 +00:00
/// block of code or an empty string.
/// @param script The script code to compile and execute.
/// @param noCalls Skips calling functions from the script.
2023-04-23 08:39:54 +00:00
/// @param setFrame A zero based index of the stack frame to execute the code
2012-09-19 15:15:01 +00:00
/// with, zero being the top of the stack. If the the index is
/// -1 a new frame is created. If the index is out of range the
/// top stack frame is used.
2023-04-23 08:39:54 +00:00
Con::EvalResult compileExec(StringTableEntry fileName, const char *script,
2017-11-06 04:33:32 +00:00
bool noCalls, S32 setFrame = -1);
2012-09-19 15:15:01 +00:00
2023-04-23 08:39:54 +00:00
/// Executes the existing code in the CodeBlock. The return string is any
2012-09-19 15:15:01 +00:00
/// result of the code executed, if any, or an empty string.
///
/// @param offset The instruction offset to start executing from.
/// @param fnName The name of the function to execute or null.
/// @param ns The namespace of the function to execute or null.
/// @param argc The number of parameters passed to the function or
/// zero to execute code outside of a function.
/// @param argv The function parameter list.
/// @param noCalls Skips calling functions from the script.
2023-04-23 08:39:54 +00:00
/// @param setFrame A zero based index of the stack frame to execute the code
2012-09-19 15:15:01 +00:00
/// with, zero being the top of the stack. If the the index is
/// -1 a new frame is created. If the index is out of range the
/// top stack frame is used.
/// @param packageName The code package name or null.
2023-04-23 08:39:54 +00:00
Con::EvalResult exec(U32 offset, const char* fnName, Namespace* ns, U32 argc,
ConsoleValue* argv, bool noCalls, StringTableEntry packageName,
S32 setFrame = -1) override;
const char* getFunctionArgs(StringTableEntry functionName, U32 functionOffset) override { return getFunctionArgs(functionOffset); }
const char* getPath() override { return fullPath; }
const char* getName() override { return name; }
Vector<U32> getBreakableLines() override { return breakList; }
2012-09-19 15:15:01 +00:00
};
2021-03-31 03:58:07 +00:00
#endif