*** empty log message ***
[chuck-blob.git] / v2 / chuck_emit.h
blob3a44e4f6915fd287a62f286ea5724090a3d68087
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_emit.h
27 // desc: chuck instruction emitter
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // date: Autumn 2002 - first version
32 // Autumn 2004 - redesign
33 //-----------------------------------------------------------------------------
34 #ifndef __CHUCK_EMIT_H__
35 #define __CHUCK_EMIT_H__
37 #include "chuck_def.h"
38 #include "chuck_oo.h"
39 #include "chuck_type.h"
40 #include "chuck_frame.h"
43 // forward references
44 struct Chuck_Instr;
45 struct Chuck_Instr_Goto;
46 struct Chuck_VM_Code;
47 struct Chuck_VM_Shred;
51 //-----------------------------------------------------------------------------
52 // name: struct Chuck_Code
53 // desc: ...
54 //-----------------------------------------------------------------------------
55 struct Chuck_Code
57 public:
58 // name
59 std::string name;
60 // stack depth
61 t_CKUINT stack_depth;
62 // need this
63 t_CKBOOL need_this;
64 // frame
65 Chuck_Frame * frame;
66 // code
67 std::vector<Chuck_Instr *> code;
69 // continue stack
70 std::vector<Chuck_Instr_Goto *> stack_cont;
71 // break stack
72 std::vector<Chuck_Instr_Goto *> stack_break;
73 // return stack
74 std::vector<Chuck_Instr_Goto *> stack_return;
76 // constructor
77 Chuck_Code( )
79 stack_depth = 0;
80 need_this = FALSE;
81 frame = new Chuck_Frame;
84 // destructor
85 ~Chuck_Code() { delete frame; frame = NULL; }
91 //-----------------------------------------------------------------------------
92 // name: struct Chuck_Emitter
93 // desc: ...
94 //-----------------------------------------------------------------------------
95 struct Chuck_Emitter : public Chuck_VM_Object
97 // reference to the type checker environment
98 Chuck_Env * env;
99 // reference to VM
100 Chuck_VM * vm;
102 // current code
103 Chuck_Code * code;
104 // current context
105 Chuck_Context * context;
106 // expression namespace
107 Chuck_Namespace * nspc;
108 // current function definition
109 Chuck_Func * func;
111 // code stack
112 std::vector<Chuck_Code *> stack;
113 // locals
114 std::vector<Chuck_Local *> locals;
116 // dump
117 t_CKBOOL dump;
119 // constructor
120 Chuck_Emitter()
121 { env = NULL; vm = NULL; code = NULL; context = NULL;
122 nspc = NULL; func = NULL; dump = FALSE; }
124 // destructor
125 ~Chuck_Emitter()
128 // append instruction
129 void append( Chuck_Instr * instr )
130 { assert( code != NULL ); code->code.push_back( instr ); }
131 // index
132 t_CKUINT next_index()
133 { assert( code != NULL ); return code->code.size(); }
135 // push scope
136 void push_scope( )
137 { assert( code != NULL ); code->frame->push_scope(); }
138 // alloc local
139 Chuck_Local * alloc_local( t_CKUINT size, const std::string & name, t_CKBOOL is_ref )
140 { assert( code != NULL ); return code->frame->alloc_local( size, name, is_ref ); }
141 // pop scope
142 void pop_scope( );
144 // default durations
145 t_CKBOOL find_dur( const std::string & name, t_CKDUR * out );
151 // allocate the emitter
152 Chuck_Emitter * emit_engine_init( Chuck_Env * env );
153 // shutdown and free the emitter
154 t_CKBOOL emit_engine_shutdown( Chuck_Emitter *& emit );
155 // emit a program into vm code
156 Chuck_VM_Code * emit_engine_emit_prog( Chuck_Emitter * emit,
157 a_Program prog,
158 te_HowMuch how_much = te_do_all );
159 // helper function to emit code
160 Chuck_VM_Code * emit_to_code( Chuck_Code * in,
161 Chuck_VM_Code * out = NULL,
162 t_CKBOOL dump = FALSE );
164 // NOT USED: ...
165 t_CKBOOL emit_engine_addr_map( Chuck_Emitter * emit, Chuck_VM_Shred * shred );
166 t_CKBOOL emit_engine_resolve( );
171 #endif