Debian Codegraph @

src/core/chuck_emit.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_emit.h
28 // desc: chuck instruction emitter
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Autumn 2002 - first version
32 // Autumn 2004 - redesign
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 // forward references
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 // name: struct Chuck_Code
55 // desc: ...
56 //-----------------------------------------------------------------------------
57 struct Chuck_Code
58 {
59 public:
60 // name
61 std::string name;
62 // stack depth (in bytes)
63 t_CKUINT stack_depth;
64 // need this
65 t_CKBOOL need_this;
66 // is static (in class def) | 1.4.1.0 (ge) added
67 t_CKBOOL is_static;
68 // frame
69 Chuck_Frame * frame;
70 // code
71 std::vector<Chuck_Instr *> code;
72
73 // continue stack
74 std::vector<Chuck_Instr_Goto *> stack_cont;
75 // break stack
76 std::vector<Chuck_Instr_Goto *> stack_break;
77 // return stack
78 std::vector<Chuck_Instr_Goto *> stack_return;
79
80 // filename this code came from (added 1.3.0.0)
81 std::string filename;
82
83 // constructor
84 Chuck_Code( )
85 {
86 stack_depth = 0;
87 need_this = FALSE;
88 is_static = FALSE; // 1.4.1.0 (ge) added
89 frame = new Chuck_Frame;
90 }
91
92 // destructor
93 ~Chuck_Code()
94 {
95 // clean up frame
96 CK_SAFE_DELETE( frame );
97 // TODO: what about contents of code?
98 // TODO: what about contents of stack_*?
99 // (these may also depend on success/error during emission)
100 }
101 };
102
103
104
105
106 //-----------------------------------------------------------------------------
107 // name: struct Chuck_Emitter
108 // desc: ...
109 //-----------------------------------------------------------------------------
110 struct Chuck_Emitter : public Chuck_VM_Object
111 {
112 // reference to the type checker environment
113 Chuck_Env * env;
114
115 // current code
116 Chuck_Code * code;
117 // current context
118 Chuck_Context * context;
119 // expression namespace
120 Chuck_Namespace * nspc;
121 // current function definition
122 Chuck_Func * func;
123
124 // locals
125 std::vector<Chuck_Local *> locals;
126 // code stack
127 std::vector<Chuck_Code *> code_stack;
128 // stmt stack
129 std::vector<Chuck_Instr_Stmt_Start *> stmt_stack;
130
131 // dump
132 t_CKBOOL dump;
133
134 public:
135 // constructor
136 Chuck_Emitter()
137 { env = NULL; code = NULL; context = NULL;
138 nspc = NULL; func = NULL; dump = FALSE;
139 should_replace_dac = FALSE; }
140
141 // destructor
142 ~Chuck_Emitter() { }
143
144 // append instruction
145 void append( Chuck_Instr * instr )
146 { assert( code != NULL ); code->code.push_back( instr ); }
147 // next instruction index
148 t_CKUINT next_index()
149 { assert( code != NULL ); return code->code.size(); }
150
151 // push scope
152 void push_scope( )
153 { assert( code != NULL ); code->frame->push_scope(); }
154 // alloc local (ge: added is_obj 2012 april | added 1.3.0.0)
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 // add references to locals on current scope (added 1.3.0.0)
160 void addref_on_scope();
161 // pop scope
162 void pop_scope();
163 // traverse scope on jumps such as 'return' 'break' 'continue' | 1.5.0.0 (ge)
164 void traverse_scope_on_jump();
165
166 // default durations
167 t_CKBOOL find_dur( const std::string & name, t_CKDUR * out );
168
169 public:
170 // post REFACTOR-2017: replace-dac
171 std::string dac_replacement;
172 t_CKBOOL should_replace_dac;
173
174 public:
175 // codestr context (for instruction dump) | 1.5.0.8 (ge) added
176 std::vector<std::string> codestr_context;
177 // push codestr context
178 void codestr_context_push( const std::string & prefix )
179 { codestr_context.push_back(prefix); }
180 // pop codestr context
181 void codestr_context_pop()
182 { codestr_context.pop_back(); }
183 // get current codestr context
184 std::string codestr_context_top() const
185 { return codestr_context.size() > 0 ? codestr_context.back() : ""; }
186 };
187
188
189
190
191 // allocate the emitter
192 Chuck_Emitter * emit_engine_init( Chuck_Env * env );
193 // shutdown and free the emitter
194 t_CKBOOL emit_engine_shutdown( Chuck_Emitter *& emit );
195 // emit a program into vm code
196 Chuck_VM_Code * emit_engine_emit_prog( Chuck_Emitter * emit,
197 a_Program prog,
198 te_HowMuch how_much );
199 // helper function to emit code
200 Chuck_VM_Code * emit_to_code( Chuck_Code * in,
201 Chuck_VM_Code * out = NULL,
202 t_CKBOOL dump = FALSE );
203
204 // NOT USED: ...
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