| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#ifndef __CHUCK_EMIT_H__ |
| 35 |
#define __CHUCK_EMIT_H__ |
| 36 |
|
| 37 |
#include "chuck_def.h" |
| 38 |
#include "chuck_oo.h" |
| 39 |
#include "chuck_type.h" |
| 40 |
#include "chuck_frame.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
struct Chuck_Instr; |
| 45 |
struct Chuck_Instr_Goto; |
| 46 |
struct Chuck_Instr_Stmt_Start; |
| 47 |
struct Chuck_VM_Code; |
| 48 |
struct Chuck_VM_Shred; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
struct Chuck_Code |
| 58 |
{ |
| 59 |
public: |
| 60 |
|
| 61 |
std::string name; |
| 62 |
|
| 63 |
t_CKUINT stack_depth; |
| 64 |
|
| 65 |
t_CKBOOL need_this; |
| 66 |
|
| 67 |
t_CKBOOL is_static; |
| 68 |
|
| 69 |
Chuck_Frame * frame; |
| 70 |
|
| 71 |
std::vector<Chuck_Instr *> code; |
| 72 |
|
| 73 |
|
| 74 |
std::vector<Chuck_Instr_Goto *> stack_cont; |
| 75 |
|
| 76 |
std::vector<Chuck_Instr_Goto *> stack_break; |
| 77 |
|
| 78 |
std::vector<Chuck_Instr_Goto *> stack_return; |
| 79 |
|
| 80 |
|
| 81 |
std::string filename; |
| 82 |
|
| 83 |
|
| 84 |
Chuck_Code( ) |
| 85 |
{ |
| 86 |
stack_depth = 0; |
| 87 |
need_this = FALSE; |
| 88 |
is_static = FALSE; |
| 89 |
frame = new Chuck_Frame; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
~Chuck_Code() |
| 94 |
{ |
| 95 |
|
| 96 |
CK_SAFE_DELETE( frame ); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
struct Chuck_Emitter : public Chuck_VM_Object |
| 111 |
{ |
| 112 |
|
| 113 |
Chuck_Env * env; |
| 114 |
|
| 115 |
|
| 116 |
Chuck_Code * code; |
| 117 |
|
| 118 |
Chuck_Context * context; |
| 119 |
|
| 120 |
Chuck_Namespace * nspc; |
| 121 |
|
| 122 |
Chuck_Func * func; |
| 123 |
|
| 124 |
|
| 125 |
std::vector<Chuck_Local *> locals; |
| 126 |
|
| 127 |
std::vector<Chuck_Code *> code_stack; |
| 128 |
|
| 129 |
std::vector<Chuck_Instr_Stmt_Start *> stmt_stack; |
| 130 |
|
| 131 |
|
| 132 |
t_CKBOOL dump; |
| 133 |
|
| 134 |
public: |
| 135 |
|
| 136 |
Chuck_Emitter() |
| 137 |
{ env = NULL; code = NULL; context = NULL; |
| 138 |
nspc = NULL; func = NULL; dump = FALSE; |
| 139 |
should_replace_dac = FALSE; } |
| 140 |
|
| 141 |
|
| 142 |
~Chuck_Emitter() { } |
| 143 |
|
| 144 |
|
| 145 |
void append( Chuck_Instr * instr ) |
| 146 |
{ assert( code != NULL ); code->code.push_back( instr ); } |
| 147 |
|
| 148 |
t_CKUINT next_index() |
| 149 |
{ assert( code != NULL ); return code->code.size(); } |
| 150 |
|
| 151 |
|
| 152 |
void push_scope( ) |
| 153 |
{ assert( code != NULL ); code->frame->push_scope(); } |
| 154 |
|
| 155 |
Chuck_Local * alloc_local( t_CKUINT size, const std::string & name, |
| 156 |
t_CKBOOL is_ref, t_CKBOOL is_obj, t_CKBOOL is_global ) |
| 157 |
{ assert( code != NULL ); return code->frame->alloc_local( size, name, |
| 158 |
is_ref, is_obj, is_global ); } |
| 159 |
|
| 160 |
void addref_on_scope(); |
| 161 |
|
| 162 |
void pop_scope(); |
| 163 |
|
| 164 |
void traverse_scope_on_jump(); |
| 165 |
|
| 166 |
|
| 167 |
t_CKBOOL find_dur( const std::string & name, t_CKDUR * out ); |
| 168 |
|
| 169 |
public: |
| 170 |
|
| 171 |
std::string dac_replacement; |
| 172 |
t_CKBOOL should_replace_dac; |
| 173 |
|
| 174 |
public: |
| 175 |
|
| 176 |
std::vector<std::string> codestr_context; |
| 177 |
|
| 178 |
void codestr_context_push( const std::string & prefix ) |
| 179 |
{ codestr_context.push_back(prefix); } |
| 180 |
|
| 181 |
void codestr_context_pop() |
| 182 |
{ codestr_context.pop_back(); } |
| 183 |
|
| 184 |
std::string codestr_context_top() const |
| 185 |
{ return codestr_context.size() > 0 ? codestr_context.back() : ""; } |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
Chuck_Emitter * emit_engine_init( Chuck_Env * env ); |
| 193 |
|
| 194 |
t_CKBOOL emit_engine_shutdown( Chuck_Emitter *& emit ); |
| 195 |
|
| 196 |
Chuck_VM_Code * emit_engine_emit_prog( Chuck_Emitter * emit, |
| 197 |
a_Program prog, |
| 198 |
te_HowMuch how_much ); |
| 199 |
|
| 200 |
Chuck_VM_Code * emit_to_code( Chuck_Code * in, |
| 201 |
Chuck_VM_Code * out = NULL, |
| 202 |
t_CKBOOL dump = FALSE ); |
| 203 |
|
| 204 |
|
| 205 |
t_CKBOOL emit_engine_addr_map( Chuck_Emitter * emit, Chuck_VM_Shred * shred ); |
| 206 |
t_CKBOOL emit_engine_resolve( ); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#endif |