Aer Interpreter Source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
9.4 KiB

2 years ago
  1. #ifndef _COMPILER_H_
  2. #define _COMPILER_H_
  3. #include "ph7int.h"
  4. /* Forward declaration */
  5. typedef struct LangConstruct LangConstruct;
  6. typedef struct JumpFixup JumpFixup;
  7. typedef struct Label Label;
  8. /* Block [i.e: set of statements] control flags */
  9. #define GEN_BLOCK_LOOP 0x001 /* Loop block [i.e: for,while,...] */
  10. #define GEN_BLOCK_PROTECTED 0x002 /* Protected block */
  11. #define GEN_BLOCK_COND 0x004 /* Conditional block [i.e: if(condition){} ]*/
  12. #define GEN_BLOCK_FUNC 0x008 /* Function body */
  13. #define GEN_BLOCK_GLOBAL 0x010 /* Global block (always set)*/
  14. #define GEN_BLOCK_NESTED_FUNC 0x020 /* Nested function body */
  15. #define GEN_BLOCK_EXPR 0x040 /* Expression */
  16. #define GEN_BLOCK_STD 0x080 /* Standard block */
  17. #define GEN_BLOCK_EXCEPTION 0x100 /* Exception block [i.e: try{ } }*/
  18. #define GEN_BLOCK_SWITCH 0x200 /* Switch statement */
  19. #define GEN_BLOCK_CLASS 0x400 /* Class definition */
  20. #define GEN_BLOCK_NAMESPACE 0x800 /* Namespace body */
  21. /*
  22. * Each label seen in the input is recorded in an instance
  23. * of the following structure.
  24. * A label is a target point [i.e: a jump destination] that is specified
  25. * by an identifier followed by a colon.
  26. */
  27. struct Label {
  28. ph7_vm_func *pFunc; /* Compiled function where the label was declared.NULL otherwise */
  29. sxu32 nJumpDest; /* Jump destination */
  30. SyString sName; /* Label name */
  31. sxu32 nLine; /* Line number this label occurs */
  32. sxu8 bRef; /* True if the label was referenced */
  33. };
  34. /*
  35. * Compilation of some Aer constructs such as if, for, while, the logical or
  36. * (||) and logical and (&&) operators in expressions requires the
  37. * generation of forward jumps.
  38. * Since the destination PC target of these jumps isn't known when the jumps
  39. * are emitted, we record each forward jump in an instance of the following
  40. * structure. Those jumps are fixed later when the jump destination is resolved.
  41. */
  42. struct JumpFixup {
  43. sxi32 nJumpType; /* Jump type. Either TRUE jump, FALSE jump or Unconditional jump */
  44. sxu32 nInstrIdx; /* Instruction index to fix later when the jump destination is resolved. */
  45. SyString sLabel; /* Label name */
  46. ph7_vm_func *pFunc; /* Compiled function inside which the goto was emitted. NULL otherwise */
  47. sxu32 nLine; /* Track line number */
  48. };
  49. /*
  50. * Each language construct is represented by an instance
  51. * of the following structure.
  52. */
  53. struct LangConstruct {
  54. sxu32 nID; /* Language construct ID [i.e: PH7_KEYWORD_WHILE,PH7_KEYWORD_FOR,PH7_KEYWORD_IF...] */
  55. ProcLangConstruct xConstruct; /* C function implementing the language construct */
  56. };
  57. /* Compilation flags */
  58. #define PH7_COMPILE_STATEMENT 0x001 /* Compile a single statement */
  59. /* Token stream synchronization macros */
  60. #define SWAP_TOKEN_STREAM(GEN,START,END)\
  61. pTmp = GEN->pEnd;\
  62. pGen->pIn = START;\
  63. pGen->pEnd = END
  64. #define UPDATE_TOKEN_STREAM(GEN)\
  65. if( GEN->pIn < pTmp ){\
  66. GEN->pIn++;\
  67. }\
  68. GEN->pEnd = pTmp
  69. #define SWAP_DELIMITER(GEN,START,END)\
  70. pTmpIn = GEN->pIn;\
  71. pTmpEnd = GEN->pEnd;\
  72. GEN->pIn = START;\
  73. GEN->pEnd = END
  74. #define RE_SWAP_DELIMITER(GEN)\
  75. GEN->pIn = pTmpIn;\
  76. GEN->pEnd = pTmpEnd
  77. /* Flags related to expression compilation */
  78. #define EXPR_FLAG_LOAD_IDX_STORE 0x001 /* Set the iP2 flag when dealing with the LOAD_IDX instruction */
  79. #define EXPR_FLAG_RDONLY_LOAD 0x002 /* Read-only load, refer to the 'PH7_OP_LOAD' VM instruction for more information */
  80. #define EXPR_FLAG_COMMA_STATEMENT 0x004 /* Treat comma expression as a single statement (used by class attributes) */
  81. /* Forward declaration */
  82. static sxi32 PH7_CompileExpr(ph7_gen_state *pGen, sxi32 iFlags, sxi32(*xTreeValidator)(ph7_gen_state *, ph7_expr_node *));
  83. static GenBlock *PH7_GenStateFetchBlock(GenBlock *pCurrent, sxi32 iBlockType, sxi32 iCount);
  84. static void PH7_GenStateInitBlock(ph7_gen_state *pGen, GenBlock *pBlock, sxi32 iType, sxu32 nFirstInstr, void *pUserData);
  85. static sxi32 PH7_GenStateEnterBlock(ph7_gen_state *pGen, sxi32 iType, sxu32 nFirstInstr, void *pUserData, GenBlock **ppBlock);
  86. static void PH7_GenStateReleaseBlock(GenBlock *pBlock);
  87. static void PH7_GenStateFreeBlock(GenBlock *pBlock);
  88. static sxi32 PH7_GenStateLeaveBlock(ph7_gen_state *pGen, GenBlock **ppBlock);
  89. static sxi32 PH7_GenStateNewJumpFixup(GenBlock *pBlock, sxi32 nJumpType, sxu32 nInstrIdx);
  90. static sxu32 PH7_GenStateFixJumps(GenBlock *pBlock, sxi32 nJumpType, sxu32 nJumpDest);
  91. static sxi32 PH7_GenStateFindLiteral(ph7_gen_state *pGen, const SyString *pValue, sxu32 *pIdx);
  92. static sxi32 PH7_GenStateInstallLiteral(ph7_gen_state *pGen, ph7_value *pObj, sxu32 nIdx);
  93. static ph7_value *PH7_GenStateInstallNumLiteral(ph7_gen_state *pGen, sxu32 *pIdx);
  94. static sxi32 PH7_CompileNumLiteral(ph7_gen_state *pGen, sxi32 iCompileFlag);
  95. PH7_PRIVATE sxi32 PH7_CompileSimpleString(ph7_gen_state *pGen, sxi32 iCompileFlag);
  96. static sxi32 PH7_GenStateProcessStringExpression(ph7_gen_state *pGen, sxu32 nLine, const char *zIn, const char *zEnd);
  97. static ph7_value *PH7_GenStateNewStrObj(ph7_gen_state *pGen, sxi32 *pCount);
  98. static sxi32 PH7_GenStateCompileString(ph7_gen_state *pGen);
  99. PH7_PRIVATE sxi32 PH7_CompileString(ph7_gen_state *pGen, sxi32 iCompileFlag);
  100. static sxi32 PH7_GenStateCompileArrayEntry(ph7_gen_state *pGen, SyToken *pIn, SyToken *pEnd, sxi32 iFlags, sxi32(*xValidator)(ph7_gen_state *, ph7_expr_node *));
  101. PH7_PRIVATE sxi32 PH7_CompileArray(ph7_gen_state *pGen, sxi32 iCompileFlag);
  102. static sxi32 PH7_GenStateCompileFunc(ph7_gen_state *pGen, SyString *pName, sxi32 iFlags, int bHandleClosure, ph7_vm_func **ppFunc);
  103. static sxi32 GenStateDefineNodeValidator(ph7_gen_state *pGen, ph7_expr_node *pRoot);
  104. PH7_PRIVATE sxi32 PH7_CompileDefine(ph7_gen_state *pGen);
  105. PH7_PRIVATE sxi32 PH7_CompileVariable(ph7_gen_state *pGen, sxi32 iCompileFlag);
  106. static sxi32 PH7_GenStateLoadLiteral(ph7_gen_state *pGen);
  107. static sxi32 PH7_GenStateResolveNamespaceLiteral(ph7_gen_state *pGen);
  108. PH7_PRIVATE sxi32 PH7_CompileLiteral(ph7_gen_state *pGen, sxi32 iCompileFlag);
  109. static int PH7_GenStateIsReservedConstant(SyString *pName);
  110. static sxi32 PH7_CompileConstant(ph7_gen_state *pGen);
  111. static sxi32 PH7_CompileContinue(ph7_gen_state *pGen);
  112. static sxi32 PH7_CompileBreak(ph7_gen_state *pGen);
  113. static sxi32 PH7_GenStateNextChunk(ph7_gen_state *pGen);
  114. static sxi32 PH7_CompileBlock(ph7_gen_state *pGen);
  115. static sxi32 PH7_CompileWhile(ph7_gen_state *pGen);
  116. static sxi32 PH7_CompileDoWhile(ph7_gen_state *pGen);
  117. static sxi32 PH7_CompileFor(ph7_gen_state *pGen);
  118. static sxi32 GenStateForEachNodeValidator(ph7_gen_state *pGen, ph7_expr_node *pRoot);
  119. static sxi32 PH7_CompileForeach(ph7_gen_state *pGen);
  120. static sxi32 PH7_CompileIf(ph7_gen_state *pGen);
  121. static sxi32 PH7_CompileReturn(ph7_gen_state *pGen);
  122. static sxi32 PH7_CompileHalt(ph7_gen_state *pGen);
  123. static sxi32 PH7_CompileVar(ph7_gen_state *pGen);
  124. static sxi32 PH7_CompileNamespace(ph7_gen_state *pGen);
  125. static sxi32 PH7_CompileUsing(ph7_gen_state *pGen);
  126. static sxi32 PH7_CompileInclude(ph7_gen_state *pGen);
  127. static sxi32 PH7_GenStateProcessArgValue(ph7_gen_state *pGen, ph7_vm_func_arg *pArg, SyToken *pIn, SyToken *pEnd);
  128. static sxi32 PH7_GenStateCollectFuncArgs(ph7_vm_func *pFunc, ph7_gen_state *pGen, SyToken *pEnd);
  129. static sxi32 PH7_GenStateCompileFuncBody(ph7_gen_state *pGen, ph7_vm_func *pFunc);
  130. static sxi32 PH7_GenStateCompileFunc(ph7_gen_state *pGen, SyString *pName, sxi32 iFlags, int bHandleClosure, ph7_vm_func **ppFunc);
  131. static sxi32 PH7_GetProtectionLevel(sxi32 nKeyword);
  132. static sxi32 PH7_GenStateCompileClassConstant(ph7_gen_state *pGen, sxi32 iProtection, sxi32 iFlags, ph7_class *pClass);
  133. static sxi32 PH7_GenStateCompileClassAttr(ph7_gen_state *pGen, sxi32 iProtection, sxi32 iFlags, sxu32 nType, ph7_class *pClass);
  134. static sxi32 PH7_GenStateCompileClassMethod(ph7_gen_state *pGen, sxu32 nType, sxi32 iProtection, sxi32 iFlags, int doBody, ph7_class *pClass);
  135. static sxi32 PH7_CompileClassInterface(ph7_gen_state *pGen);
  136. static sxi32 PH7_GenStateCompileClass(ph7_gen_state *pGen, sxi32 iFlags);
  137. static sxi32 PH7_CompileClass(ph7_gen_state *pGen);
  138. static sxi32 PH7_GenStateThrowNodeValidator(ph7_gen_state *pGen, ph7_expr_node *pRoot);
  139. static sxi32 PH7_CompileThrow(ph7_gen_state *pGen);
  140. static sxi32 PH7_CompileCatch(ph7_gen_state *pGen, ph7_exception *pException);
  141. static sxi32 PH7_CompileTry(ph7_gen_state *pGen);
  142. static sxi32 PH7_GenStateCompileSwitchBlock(ph7_gen_state *pGen, sxu32 *pBlockStart);
  143. static sxi32 PH7_GenStateCompileCaseExpr(ph7_gen_state *pGen, ph7_case_expr *pExpr);
  144. static sxi32 PH7_CompileSwitch(ph7_gen_state *pGen);
  145. static sxi32 PH7_GenStateEmitExprCode(ph7_gen_state *pGen, ph7_expr_node *pNode, sxi32 iFlags);
  146. static sxi32 PH7_CompileExpr(ph7_gen_state *pGen, sxi32 iFlags, sxi32(*xTreeValidator)(ph7_gen_state *, ph7_expr_node *));
  147. PH7_PRIVATE ProcNodeConstruct PH7_GetNodeHandler(sxu32 nNodeType);
  148. static ProcLangConstruct PH7_GenStateGetStatementHandler(sxu32 nKeywordID, SyToken *pLookahead);
  149. static int PH7_IsLangConstruct(sxu32 nKeywordID);
  150. static sxi32 PH7_GenStateCompileChunk(ph7_gen_state *pGen, sxi32 iFlags);
  151. static sxi32 PH7_GenStateCompileGlobalScope(ph7_gen_state *pGen);
  152. static sxi32 PH7_CompileScript(ph7_gen_state *pGen, SySet *pTokenSet, sxi32 iFlags);
  153. PH7_PRIVATE sxi32 PH7_InitCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData);
  154. PH7_PRIVATE sxi32 PH7_ResetCodeGenerator(ph7_vm *pVm, ProcConsumer xErr, void *pErrData);
  155. PH7_PRIVATE sxi32 PH7_GenCompileError(ph7_gen_state *pGen, sxi32 nErrType, sxu32 nLine, const char *zFormat, ...);
  156. #endif /* _COMPILER_H_ */