CE Environment.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /******************************************************************************/
  2. #if EE_PRIVATE
  3. namespace Edit{
  4. /******************************************************************************/
  5. #define CE_STACK_SIZE 1024*1024 // 1 MB
  6. /******************************************************************************/
  7. struct CodeEnvironment
  8. {
  9. struct FuncBody
  10. {
  11. Int stack_size; // size of stack that is required for this function
  12. Mems<Byte> code; // function body codes
  13. Str8 name; // function name
  14. void zero () {stack_size=0;}
  15. void del ();
  16. void create (Symbol &func, Memc<Message> &msgs, CompilerContext &ctx);
  17. void compile (Symbol &func, Memc<Message> &msgs, Memc<Command> &cmds, Memc<Byte> &code);
  18. void link ( Memc<Message> &msgs, CodeEnvironment &ce);
  19. void optimize( Memc<Message> &msgs, CodeEnvironment &ce);
  20. FuncBody() {zero();}
  21. };
  22. struct Thread
  23. {
  24. struct Level
  25. {
  26. FuncBody *func;
  27. Byte *func_code_pos,
  28. *func_param_stack,
  29. *func_this,
  30. *func_result;
  31. };
  32. Byte *func_code_pos , // code position
  33. *func_code_end , // code position end
  34. *func_stack , // stack of the function
  35. *func_param_stack, // parameter stack of the function
  36. *func_this , // this object of the function
  37. *func_result , // result address of the function
  38. *heap ; // environment heap
  39. FuncBody *func ; // in which function we're currently located
  40. Bool debug ; // if run in debug mode
  41. Mems<Byte> stack ;
  42. Memc<Level> levels ; // call-stack
  43. void zero () {func_code_pos=func_code_end=func_stack=func_param_stack=func_this=func_result=heap=null; func=null; debug=false;}
  44. void del ();
  45. void create(CodeEnvironment &env);
  46. Bool test ();
  47. Bool start (FuncBody &func);
  48. void call (FuncBody &func, Int param_stack_offset, Ptr func_this, Ptr result);
  49. Thread() {zero();}
  50. };
  51. static Bool VerifySymbols(Memc<Message> &msgs, Memc<Symbol*> &sorted_classes);
  52. static void SetupChildren();
  53. Bool debug;
  54. UInt heap_size;
  55. Mems<Byte > heap;
  56. Memc<FuncBody> func_bodies;
  57. Thread main_thread;
  58. void createGlobalVarSetup(Int func_index, Memc<Message> &msgs, CompilerContext &ctx);
  59. void del ();
  60. Bool create(Memc<Message> &msgs);
  61. CodeEnvironment() {debug=false; heap_size=0;}
  62. NO_COPY_CONSTRUCTOR(CodeEnvironment);
  63. };
  64. /******************************************************************************/
  65. struct Call
  66. {
  67. enum ADDR_TYPE : Byte
  68. {
  69. ADDR_STACK , // address is relative to CodeEnvironment function stack
  70. ADDR_STACK_REF , // address is relative to CodeEnvironment function stack (in that address there is Ptr that points to the actual element)
  71. ADDR_PARAM_STACK , // address is relative to CodeEnvironment function parameter stack
  72. ADDR_PARAM_STACK_REF, // address is relative to CodeEnvironment function parameter stack (in that address there is Ptr that points to the actual element)
  73. ADDR_THIS , // address is relative to CodeEnvironment function this
  74. ADDR_THIS_REF , // address is relative to CodeEnvironment function this (in that address there is Ptr that points to the actual element)
  75. ADDR_RESULT , // address is relative to CodeEnvironment function result
  76. ADDR_HEAP , // address is relative to CodeEnvironment heap (used only during compilation, upon loading is replaced with ADDR_GLOBAL )
  77. ADDR_HEAP_REF , // address is relative to CodeEnvironment heap (used only during compilation, upon loading is replaced with ADDR_GLOBAL_REF) (in that address there is Ptr that points to the actual element)
  78. ADDR_GLOBAL , // address is in global C++ space
  79. ADDR_GLOBAL_REF , // address is in global C++ space (in that address there is Ptr that points to the actual element)
  80. ADDR_INSTRUCT , // if constant value is encoded in the instruction itself, in the 'addr' member (can be used only for constants! which size is <= SIZE(Ptr))
  81. };
  82. struct Param
  83. {
  84. ADDR_TYPE type;
  85. union
  86. {
  87. struct
  88. {
  89. union
  90. {
  91. Ptr addr;
  92. Int offset;
  93. };
  94. Int offset2; // offset applied after getting the reference
  95. };
  96. U64 raw;
  97. };
  98. void set (Expr &expr, Compiler &compiler, Bool auto_map=true);
  99. void setRef (Expr &expr, Compiler &compiler);
  100. void setGlobal (Ptr addr , Int offset2, Bool ref);
  101. void setHeap (Int offset, Int offset2, Bool ref);
  102. void setStack (Int offset, Int offset2, Bool ref);
  103. void setParamStack (Int offset, Int offset2, Bool ref);
  104. void setThis (Int offset, Int offset2, Bool ref);
  105. void setResult ( );
  106. void setInstruct (U64 raw );
  107. void setInstructI (Int raw );
  108. void setInstructP (Ptr addr);
  109. void setInstructPI2(Ptr addr, Int i);
  110. Int getInstructI ( ) {return offset ;}
  111. Int getInstructI2 ( ) {return offset2;}
  112. Ptr getInstructP ( ) {return addr ;}
  113. #if 0 // this is slightly slower (4.768s vs 4.653s on big loop)
  114. Ptr address(CodeEnvironment::Thread &thread);
  115. T1(TYPE) INLINE TYPE& value (CodeEnvironment::Thread &thread) {return *(TYPE*)address(thread);}
  116. #else // slightly faster
  117. T1(TYPE) TYPE& value (CodeEnvironment::Thread &thread);
  118. Ptr address(CodeEnvironment::Thread &thread);
  119. #endif
  120. CPtr& ref (CodeEnvironment::Thread &thread);
  121. };
  122. typedef void (*Func)(Call &call, CodeEnvironment::Thread &thread);
  123. static INLINE Int Size(Int params) {return SIZE(Call) + SIZE(Param)*params;}
  124. Func func;
  125. Int params;
  126. INLINE Param& param(Int i) {RANGE_ASSERT(i, params); return ((Param*)(this+1))[i];}
  127. };
  128. /******************************************************************************/
  129. } // namespace
  130. /******************************************************************************/
  131. #endif
  132. /******************************************************************************/