2
0

gmCodeGenHooks.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. _____ __ ___ __ ____ _ __
  3. / ___/__ ___ _ ___ / |/ /__ ___ / /_____ __ __/ __/_______(_)__ / /_
  4. / (_ / _ `/ ' \/ -_) /|_/ / _ \/ _ \/ '_/ -_) // /\ \/ __/ __/ / _ \/ __/
  5. \___/\_,_/_/_/_/\__/_/ /_/\___/_//_/_/\_\\__/\_, /___/\__/_/ /_/ .__/\__/
  6. /___/ /_/
  7. See Copyright Notice in gmMachine.h
  8. */
  9. #ifndef _GMCODEGENHOOKS_H_
  10. #define _GMCODEGENHOOKS_H_
  11. #include "gmConfig.h"
  12. /// \struct gmLineInfo
  13. /// \brief gmLineInfo describes the debug info required for line number debugging
  14. struct gmLineInfo
  15. {
  16. int m_address; //!< byte code address
  17. int m_lineNumber; //!< code line number
  18. };
  19. /// \struct gmFunctionInfo
  20. /// \brief gmFunctionInfo
  21. struct gmFunctionInfo
  22. {
  23. gmptr m_id; //!< unique id of the function (as used in BC_PUSHFN)
  24. bool m_root; //!< is this function the root function '__main'
  25. const void * m_byteCode; //!< byte code
  26. int m_byteCodeLength; //!< byte code length in bytes
  27. int m_numParams; //!< parameter count
  28. int m_numLocals; //!< local variable count (includes registers)
  29. int m_maxStackSize; //!< required temporary storage
  30. const char * m_debugName; //!< name of variable function was assigned to... may be NULL
  31. const char ** m_symbols; //!< param and local variable names, sizeof m_numParams + m_numLocals; (indexed by stack offset)
  32. int m_lineInfoCount; //!< number of entries in the line info array
  33. const gmLineInfo * m_lineInfo; //!< line - instruction address mapping for debugging purposes.
  34. };
  35. /// \class gmCodeGenHooks
  36. /// \brief gmCodeGenHooks is an interface that is fed to the compiler. basically the code gen hooks class allows you
  37. /// to compile script directly into the runtime vm, or into a libary.
  38. class gmCodeGenHooks
  39. {
  40. public:
  41. gmCodeGenHooks() {}
  42. virtual ~gmCodeGenHooks() {}
  43. /// \brief Begin() will be called by gmCodeGen at the start of compilation.
  44. /// \param a_debug is true if this is a debug build.
  45. virtual bool Begin(bool a_debug) = 0;
  46. /// \brief AddFunction() is called each time the byte code for a function has been created. The memory passed in
  47. /// the info structure is not valid after AddFunction returns.
  48. /// \return true on success
  49. virtual bool AddFunction(gmFunctionInfo &a_functionInfo) = 0;
  50. /// \brief End() is called by gmCodeGen at the end of a compilation.
  51. /// \param a_errors is the number of compilation errors.
  52. virtual bool End(int a_errors) = 0;
  53. /// \brief GetFunctionId() is called for the creation of unique function ids.
  54. /// \return a unique id.
  55. virtual gmptr GetFunctionId() = 0;
  56. /// \brief GetSymbolId() is called by the compiler to get a unique symbol id. this sybol id is a machine size int
  57. /// id written into the byte code.
  58. /// \return a unique id for each unique a_symbol.
  59. virtual gmptr GetSymbolId(const char * a_symbol) = 0;
  60. /// \brief GetStringId() is called by the compiler to get a constant string id. the returned value is written into
  61. /// the byte code for string lookups.
  62. /// \return a unique id for each unique string.
  63. virtual gmptr GetStringId(const char * a_string) = 0;
  64. /// \brief SwapEndian() returns true if the byte code is being compiled for a machine of differing endian
  65. virtual bool SwapEndian() const { return false; }
  66. };
  67. /// \class gmCodeGenHooksNull
  68. /// \brief used for syntax checking etc.
  69. class gmCodeGenHooksNull : public gmCodeGenHooks
  70. {
  71. public:
  72. gmCodeGenHooksNull() {}
  73. virtual ~gmCodeGenHooksNull() {}
  74. virtual bool Begin(bool a_debug) { return true; }
  75. virtual bool AddFunction(gmFunctionInfo &a_functionInfo) { return true; }
  76. virtual bool End(int a_errors) { return true; }
  77. virtual gmptr GetFunctionId() { return 0; }
  78. virtual gmptr GetSymbolId(const char * a_symbol) { return 0; }
  79. virtual gmptr GetStringId(const char * a_string) { return 0; }
  80. };
  81. #endif // _GMCODEGENHOOKS_H_