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
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
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"
39 #include "chuck_type.h"
40 #include "chuck_frame.h"
45 struct Chuck_Instr_Goto
;
47 struct Chuck_VM_Shred
;
51 //-----------------------------------------------------------------------------
52 // name: struct Chuck_Code
54 //-----------------------------------------------------------------------------
67 std::vector
<Chuck_Instr
*> code
;
70 std::vector
<Chuck_Instr_Goto
*> stack_cont
;
72 std::vector
<Chuck_Instr_Goto
*> stack_break
;
74 std::vector
<Chuck_Instr_Goto
*> stack_return
;
81 frame
= new Chuck_Frame
;
85 ~Chuck_Code() { delete frame
; frame
= NULL
; }
91 //-----------------------------------------------------------------------------
92 // name: struct Chuck_Emitter
94 //-----------------------------------------------------------------------------
95 struct Chuck_Emitter
: public Chuck_VM_Object
97 // reference to the type checker environment
105 Chuck_Context
* context
;
106 // expression namespace
107 Chuck_Namespace
* nspc
;
108 // current function definition
112 std::vector
<Chuck_Code
*> stack
;
114 std::vector
<Chuck_Local
*> locals
;
121 { env
= NULL
; vm
= NULL
; code
= NULL
; context
= NULL
;
122 nspc
= NULL
; func
= NULL
; dump
= FALSE
; }
128 // append instruction
129 void append( Chuck_Instr
* instr
)
130 { assert( code
!= NULL
); code
->code
.push_back( instr
); }
132 t_CKUINT
next_index()
133 { assert( code
!= NULL
); return code
->code
.size(); }
137 { assert( code
!= NULL
); code
->frame
->push_scope(); }
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
); }
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
,
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
);
165 t_CKBOOL
emit_engine_addr_map( Chuck_Emitter
* emit
, Chuck_VM_Shred
* shred
);
166 t_CKBOOL
emit_engine_resolve( );